Support of Arch Linux in `getdeps.py`
[hiphop-php.git] / hphp / runtime / base / stats.cpp
blobda0fc7cf04885ba0df33262274fddda588489426
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/runtime/base/stats.h"
18 #include <algorithm>
19 #include <atomic>
20 #include <utility>
21 #include <vector>
23 #include "hphp/runtime/base/execution-context.h"
24 #include "hphp/runtime/base/string-data.h"
25 #include "hphp/util/data-block.h"
27 namespace HPHP {
28 namespace Stats {
30 TRACE_SET_MOD(stats);
32 const char* g_counterNames[] = {
33 #include "hphp/runtime/vm/stats-opcodeDef.h"
34 #define STAT(s) #s ,
35 STATS
36 #undef STAT
37 #undef O
40 using StatGroupMap = hphp_const_char_map<hphp_const_char_map<uint64_t>>;
42 RDS_LOCAL(StatCounters, rl_counters);
43 RDS_LOCAL(StatGroupMap*, rl_stat_groups);
45 void init() {
46 if (!enabledAny()) return;
47 assertx(*rl_stat_groups == nullptr);
48 *rl_stat_groups = new StatGroupMap();
51 static RDS_LOCAL(int64_t, epoch);
52 void dump() {
53 if (!enabledAny()) return;
55 auto url = g_context->getRequestUrl(50);
56 TRACE(0, "STATS %" PRId64 " %s\n", *epoch, url.c_str());
57 #include "hphp/runtime/vm/stats-opcodeDef.h"
58 #define STAT(s) \
59 if (!rl_counters->counters[s]) {} else \
60 TRACE(0, "STAT %-50s %15" PRId64 "\n", #s, rl_counters->counters[s]);
61 STATS
62 #undef STAT
63 #undef O
65 using StatPair = std::pair<const char*, uint64_t>;
66 for (auto const& group : **rl_stat_groups) {
67 std::string stats;
68 auto const& map = group.second;
69 uint64_t total = 0, accum = 0;
70 size_t nameWidth = 0;
72 std::vector<StatPair> rows(map.begin(), map.end());
73 for (auto const& p : rows) {
74 nameWidth = std::max(nameWidth, strlen(p.first));
75 total += p.second;
77 auto gt = [](const StatPair& a, const StatPair& b) {
78 return a.second > b.second;
80 std::sort(rows.begin(), rows.end(), gt);
82 folly::format(&stats, "{:-^80}\n",
83 folly::format(" group {} ",group.first, url));
84 folly::format(&stats,
85 folly::format("{{:>{}}} {{:>9}} {{:>8}} {{:>8}}\n",
86 nameWidth + 2).str(),
87 "name", "count", "% total", "accum %");
89 static const auto maxGroupEnv = getenv("HHVM_STATS_GROUPMAX");
90 static const auto maxGroup = maxGroupEnv ? atoi(maxGroupEnv) : INT_MAX;
92 int counter = 0;
93 auto const fmt = folly::format("{{:>{}}} : {{:9}} {{:8.2%}} {{:8.2%}}\n",
94 nameWidth + 2).str();
95 for (auto const& row : rows) {
96 accum += row.second;
97 folly::format(&stats, fmt, row.first, row.second,
98 (double)row.second / total, (double)accum / total);
99 if (++counter >= maxGroup) break;
101 FTRACE(0, "{}\n", stats);
105 void clear() {
106 if (!enabledAny()) return;
107 ++*epoch;
108 memset(&rl_counters->counters[0], 0, sizeof(rl_counters->counters));
110 assertx(*rl_stat_groups);
111 delete *rl_stat_groups;
112 *rl_stat_groups = nullptr;
115 void incStatGrouped(const StringData* category, const StringData* name, int n) {
116 assertx(*rl_stat_groups);
117 (**rl_stat_groups)[category->data()][name->data()] += n;