2 +----------------------------------------------------------------------+
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"
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
;
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
;
53 if (caller
->addr
== addr
) {
55 caller
->next
= n
->callers
;
56 return n
->callers
= caller
;
59 caller
= caller
->next
;
62 return makeCaller(n
, addr
);
65 StackTraceSample::StackTraceSample() {
66 if (enable_stacktrace_profiler
) {
67 depth
= backtrace(addrs
, kMaxDepth
);
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
);
79 for (int i
= m_skip
; i
< sample
.depth
; i
++) {
80 Node
* caller
= findCaller(node
, sample
.addrs
[i
]);
86 void StackTraceProfiler::count() {
87 if (!enable_stacktrace_profiler
|| finishing
) return;
88 StackTraceSample 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;
99 for (Node
* c
= n
->callers
; c
; c
= c
->next
) count
+= numLeaves(c
);
103 void StackTraceProfiler::print(Node
* n
, std::string indent
) {
104 fprintf(stderr
, "%s%lu ", indent
.c_str(), n
->hits
);
106 std::string s
= StackTrace::Translate(n
->addr
)->toString();
107 fprintf(stderr
, "%s\n", s
.c_str());
109 fprintf(stderr
, "%s\n", m_name
.c_str());
111 if (numLeaves(n
) <= 1) return;
113 std::vector
<Node
*> v
;
114 for (Node
* caller
= n
->callers
; caller
; caller
= caller
->next
) {
117 std::sort(v
.begin(), v
.end(), compareNodes
);
119 if (i
->hits
> n
->hits
/ 100.0) print(i
, indent
);
123 StackTraceProfiler::~StackTraceProfiler() {
124 if (!enable_stacktrace_profiler
) return;
129 BoolProfiler::BoolProfiler(std::string 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();
139 fprintf(stderr
, "%s: total=%lu false=%.1f%% true=%.1f%%\n", name
.c_str(),
141 100.0 * p0
.hits() / total
,
142 100.0 * p1
.hits() / total
);
146 bool BoolProfiler::operator()(bool b
) {
147 (b
? &p1
: &p0
)->count();
151 IntProfiler::IntProfiler(std::string 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)
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();
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
) {