2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_PROFILE_DUMP_H_
18 #define incl_HPHP_PROFILE_DUMP_H_
20 #include "hphp/runtime/vm/srckey.h"
23 #include <condition_variable>
27 // A StackTrace is represented by SrcKeys, which uniquely identify logical
28 // source locations. The lowermost stack frame recorded should go in the
29 // lowest index of the vector.
30 typedef std::vector
<SrcKey
> ProfileStackTrace
;
32 // pprof format requires us to keep track of the number of live objects
33 // (or total in the accumulative case), and the number of bytes they occupy.
34 struct SiteAllocations
{
38 // operators for merging shorthand
39 SiteAllocations
&operator+=(size_t bytes
) {
44 SiteAllocations
&operator-=(size_t bytes
) {
50 SiteAllocations
&operator+=(const SiteAllocations
&allocs
) {
51 m_count
+= allocs
.m_count
;
52 m_bytes
+= allocs
.m_bytes
;
55 SiteAllocations
&operator-=(const SiteAllocations
&allocs
) {
56 m_count
-= allocs
.m_count
;
57 m_bytes
-= allocs
.m_bytes
;
62 // Allocation data for each stack trace. pprof wants both what is currently
63 // being used and what was allocated across the lifetime of the heap.
66 m_currentlyAllocated
.clear();
67 m_accumAllocated
.clear();
71 void addAlloc(size_t size
, const ProfileStackTrace
&trace
) {
72 m_currentlyAllocated
[trace
] += size
;
73 m_accumAllocated
[trace
] += size
;
75 void removeAlloc(size_t size
, const ProfileStackTrace
&trace
) {
76 auto ¤t
= m_currentlyAllocated
[trace
];
78 assert(current
.m_count
>= 0 && current
.m_bytes
>= 0);
81 std::string
toPProfFormat() const;
84 void forEachAddress(F fun
) const {
85 for (const auto &data
: m_accumAllocated
) {
86 for (const SrcKey
&sk
: data
.first
) {
92 // merge operation: takes another dump and adds all of its data.
93 // used for global dumps that require logging from multiple VM
95 ProfileDump
&operator+=(const ProfileDump
&dump
) {
96 for (const auto &pair
: dump
.m_currentlyAllocated
) {
97 m_currentlyAllocated
[pair
.first
] += pair
.second
;
99 for (const auto &pair
: dump
.m_accumAllocated
) {
100 m_accumAllocated
[pair
.first
] += pair
.second
;
107 std::map
<ProfileStackTrace
, SiteAllocations
> m_currentlyAllocated
;
108 std::map
<ProfileStackTrace
, SiteAllocations
> m_accumAllocated
;
113 // Static controller for requesting and fetching profile dumps. The pprof
114 // server will place requests for dumps, and the VM threads will give
115 // their dumps to the controller if they satisfy the currently-active
116 // request. The pprof server will come and fetch the profile later, and if
117 // it needs to wait for a request to finish, it will.
118 struct ProfileController
{
120 static bool requestNext();
121 static bool requestNextURL(const std::string
&url
);
122 static bool requestGlobal();
123 static void cancelRequest();
126 static void offerProfile(const ProfileDump
&dump
);
129 static ProfileDump
waitForProfile();
134 #endif // incl_HPHP_PROFILE_DUMP_H_