Delete BoolProfiler uses behind if (false) in bytecode.cpp
[hiphop-php.git] / hphp / util / stacktrace-profiler.cpp
blobbf46ab2de536284f4018d8c20bc5e3c79a4e95f0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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 #include "hphp/util/stacktrace-profiler.h"
18 #include "hphp/util/stack-trace.h"
20 #ifndef _MSC_VER
21 #include <execinfo.h>
22 #endif
24 #include <algorithm>
26 namespace HPHP {
28 std::atomic<bool> enable_stacktrace_profiler {
29 getenv("STACKTRACE_PROFILER") != nullptr
32 StackTraceProfiler::StackTraceProfiler(std::string name, int skip) :
33 m_name(name), finishing(false), m_root(nullptr), m_skip(skip) {
36 // Make a new caller node and link it into n's caller list.
37 StackTraceProfiler::Node* StackTraceProfiler::makeCaller(Node* n, void* addr) {
38 Node* caller = new (m_arena) Node(addr);
39 caller->next = n->callers;
40 n->callers = caller;
41 return caller;
44 // find a caller node with the given address, or make a new one.
45 // The new node is placed or moved to the front of n's caller list,
46 // in the hopes that the next search on n will be the same addr.
47 StackTraceProfiler::Node* StackTraceProfiler::findCaller(Node* n, void* addr) {
48 if (Node* caller = n->callers) {
49 if (caller->addr == addr) return caller;
50 Node** prev = &caller->next;
51 caller = caller->next;
52 while (caller) {
53 if (caller->addr == addr) {
54 *prev = caller->next;
55 caller->next = n->callers;
56 return n->callers = caller;
58 prev = &caller->next;
59 caller = caller->next;
62 return makeCaller(n, addr);
65 StackTraceSample::StackTraceSample() {
66 if (enable_stacktrace_profiler) {
67 depth = backtrace(addrs, kMaxDepth);
68 } else {
69 depth = 0;
73 void StackTraceProfiler::count(const StackTraceSample& sample) {
74 if (!enable_stacktrace_profiler || finishing) return;
75 if (sample.depth <= 0) return;
76 std::lock_guard<std::mutex> lock(m_mutex);
77 m_root.hits++;
78 Node* node = &m_root;
79 for (int i = m_skip; i < sample.depth; i++) {
80 Node* caller = findCaller(node, sample.addrs[i]);
81 caller->hits++;
82 node = caller;
86 void StackTraceProfiler::count() {
87 if (!enable_stacktrace_profiler || finishing) return;
88 StackTraceSample sample;
89 count(sample);
92 bool StackTraceProfiler::compareNodes(Node* a, Node* b) {
93 return a->hits > b->hits;
96 int StackTraceProfiler::numLeaves(Node* n) {
97 if (!n->callers) return 1;
98 int count = 0;
99 for (Node* c = n->callers; c; c = c->next) count += numLeaves(c);
100 return count;
103 void StackTraceProfiler::print(Node* n, std::string indent) {
104 fprintf(stderr, "%s%lu ", indent.c_str(), n->hits);
105 if (n->addr) {
106 std::string s = StackTrace::Translate(n->addr)->toString();
107 fprintf(stderr, "%s\n", s.c_str());
108 } else {
109 fprintf(stderr, "%s\n", m_name.c_str());
111 if (numLeaves(n) <= 1) return;
112 indent += " ";
113 std::vector<Node*> v;
114 for (Node* caller = n->callers; caller; caller = caller->next) {
115 v.push_back(caller);
117 std::sort(v.begin(), v.end(), compareNodes);
118 for (auto i : v) {
119 if (i->hits > n->hits / 100.0) print(i, indent);
123 StackTraceProfiler::~StackTraceProfiler() {
124 if (!enable_stacktrace_profiler) return;
125 finishing = true;
126 print(&m_root, "");
129 BoolProfiler::BoolProfiler(std::string name)
130 : name(name)
131 , p1(name + "=true", 2)
132 , p0(name + "=false", 2)
135 BoolProfiler::~BoolProfiler() {
136 if (!enable_stacktrace_profiler) return;
137 auto total = p0.hits() + p1.hits();
138 if (total) {
139 fprintf(stderr, "%s: total=%lu false=%.1f%% true=%.1f%%\n", name.c_str(),
140 total,
141 100.0 * p0.hits() / total,
142 100.0 * p1.hits() / total);
146 bool BoolProfiler::operator()(bool b) {
147 (b ? &p1 : &p0)->count();
148 return b;
151 IntProfiler::IntProfiler(std::string name)
152 : name(name)
153 , pN(name + ">=65", 2)
154 , p64(name + "=33-64", 2)
155 , p32(name + "=17-32", 2)
156 , p16(name + "=9-16", 2)
157 , p8(name + "=5-8", 2)
158 , p4(name + "=3-4", 2)
159 , p2(name + "=2", 2)
160 , p1(name + "=1", 2)
161 , p0(name + "=0", 2)
164 IntProfiler::~IntProfiler() {
165 if (!enable_stacktrace_profiler) return;
166 auto total = p0.hits() + p1.hits() + p2.hits() + p4.hits() + p8.hits() +
167 p16.hits() + p32.hits() + p64.hits() + pN.hits();
168 if (total) {
169 fprintf(stderr, "%s: total=%lu 0=%.1f%% 1=%.1f%% 2=%.1f%% "
170 "3-4=%.1f%% 5-8=%.1f%% 9-16=%.1f%% 17-32=%.1f%% 33-64=%.1f%% "
171 "65+=%.1f%%\n", name.c_str(), total,
172 100.0 * p0.hits() / total,
173 100.0 * p1.hits() / total,
174 100.0 * p2.hits() / total,
175 100.0 * p4.hits() / total,
176 100.0 * p8.hits() / total,
177 100.0 * p16.hits() / total,
178 100.0 * p32.hits() / total,
179 100.0 * p64.hits() / total,
180 100.0 * pN.hits() / total);
184 void IntProfiler::operator()(unsigned i) {
185 (i == 0 ? &p0 :
186 i == 1 ? &p1 :
187 i == 2 ? &p2 :
188 i <= 4 ? &p4 :
189 i <= 8 ? &p8 :
190 i <= 16 ? &p16 :
191 i <= 32 ? &p32 :
192 i <= 64 ? &p64 :
193 &pN)->count();