SetStr and AddStr methods on a Vector do the same thing.
[hiphop-php.git] / hphp / runtime / base / profile_dump.h
blob6d7630c03e081ee7ced520955fb8990bfb3cbb45
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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"
22 #include <mutex>
23 #include <condition_variable>
25 namespace HPHP {
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 {
35 size_t m_count;
36 size_t m_bytes;
38 // operators for merging shorthand
39 SiteAllocations &operator+=(size_t bytes) {
40 m_count++;
41 m_bytes += bytes;
42 return *this;
44 SiteAllocations &operator-=(size_t bytes) {
45 m_count--;
46 m_bytes -= bytes;
47 return *this;
50 SiteAllocations &operator+=(const SiteAllocations &allocs) {
51 m_count += allocs.m_count;
52 m_bytes += allocs.m_bytes;
53 return *this;
55 SiteAllocations &operator-=(const SiteAllocations &allocs) {
56 m_count -= allocs.m_count;
57 m_bytes -= allocs.m_bytes;
58 return *this;
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.
64 struct ProfileDump {
65 void clear() {
66 m_currentlyAllocated.clear();
67 m_accumAllocated.clear();
68 m_numDumps = 0;
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 &current = m_currentlyAllocated[trace];
77 current -= size;
78 assert(current.m_count >= 0 && current.m_bytes >= 0);
81 std::string toPProfFormat() const;
83 template<typename F>
84 void forEachAddress(F fun) const {
85 for (const auto &data : m_accumAllocated) {
86 for (const SrcKey &sk : data.first) {
87 fun(sk);
92 // merge operation: takes another dump and adds all of its data.
93 // used for global dumps that require logging from multiple VM
94 // threads
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;
102 m_numDumps++;
103 return *this;
106 private:
107 std::map<ProfileStackTrace, SiteAllocations> m_currentlyAllocated;
108 std::map<ProfileStackTrace, SiteAllocations> m_accumAllocated;
110 int m_numDumps;
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 {
119 // request API
120 static bool requestNext();
121 static bool requestNextURL(const std::string &url);
122 static bool requestGlobal();
123 static void cancelRequest();
125 // give API
126 static void offerProfile(const ProfileDump &dump);
128 // get API
129 static ProfileDump waitForProfile();
134 #endif // incl_HPHP_PROFILE_DUMP_H_