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/runtime/base/simple-counter.h"
18 #include "hphp/runtime/ext/std/ext_std_math.h"
19 #include "hphp/util/stack-trace.h"
20 #include "hphp/util/lock.h"
26 ///////////////////////////////////////////////////////////////////////////////
28 bool SimpleCounter::Enabled
= false;
29 int SimpleCounter::SampleStackCount
= 0;
30 int SimpleCounter::SampleStackDepth
= 5;
32 IMPLEMENT_STATIC_REQUEST_LOCAL(SimpleCounter
, s_counter
);
34 void SimpleCounter::requestInit() {
39 bool SimpleCounter::Comparer::operator()(const std::string
&s1
,
40 const std::string
&s2
) {
41 return m_map
[s1
] > m_map
[s2
];
44 static Mutex s_outputMutex
;
46 void SimpleCounter::requestShutdown() {
47 Lock
lock(s_outputMutex
);
48 for (CounterMap::const_iterator it
= m_counters
.begin();
49 it
!= m_counters
.end(); ++it
) {
50 fprintf(stderr
, "====================\n");
51 fprintf(stderr
, "%s : %d\n", it
->first
.c_str(), it
->second
);
52 if (SampleStackCount
> 0) {
54 std::vector
<std::string
> &stackVec
= m_stacks
[it
->first
];
55 for (size_t i
= 0; i
< stackVec
.size(); i
++) {
58 std::vector
<std::string
> unique
;
59 for (CounterMap::const_iterator jt
= cm
.begin(); jt
!= cm
.end(); ++jt
) {
60 unique
.push_back(jt
->first
);
62 std::sort(unique
.begin(), unique
.end(), Comparer(cm
));
63 for (size_t i
= 0; i
< unique
.size(); i
++) {
64 fprintf(stderr
, "Stack #%d: %d/%d\n",
65 (int)(i
+ 1), cm
[unique
[i
]], (int)stackVec
.size());
66 StackTrace
st(unique
[i
]);
67 fprintf(stderr
, "%s", st
.toString().c_str());
73 void SimpleCounter::Count(const std::string
&name
) {
75 int count
= ++s_counter
->m_counters
[name
];
76 if (SampleStackCount
> 0) {
77 assertx(StackTrace::Enabled
);
78 std::vector
<std::string
> &stackVec
= s_counter
->m_stacks
[name
];
79 if ((int)stackVec
.size() < SampleStackCount
||
80 HHVM_FN(rand
)(0, count
- 1) < SampleStackCount
) {
82 if ((int)stackVec
.size() < SampleStackCount
) {
83 // skip StackTrace methods and the Count() call.
84 stackVec
.push_back(st
.hexEncode(3, 3 + SampleStackDepth
));
86 // skip StackTrace methods and the Count() call.
87 stackVec
[HHVM_FN(rand
)(0, SampleStackCount
- 1)] =
88 st
.hexEncode(3, 3 + SampleStackDepth
);
95 ///////////////////////////////////////////////////////////////////////////////