Add sub-controls for Hack array compat runtime checks
[hiphop-php.git] / hphp / runtime / base / stats.cpp
blob3eb4b3c9127ceb9d3a4121cdd6c707eb9f27d785
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
39 __thread uint64_t tl_counters[kNumStatCounters];
41 typedef hphp_const_char_map<hphp_const_char_map<uint64_t>> StatGroupMap;
42 __thread StatGroupMap* tl_stat_groups = nullptr;
44 void init() {
45 if (!enabledAny()) return;
46 assert(tl_stat_groups == nullptr);
47 tl_stat_groups = new StatGroupMap();
50 static __thread int64_t epoch;
51 void dump() {
52 if (!enabledAny()) return;
54 auto url = g_context->getRequestUrl(50);
55 TRACE(0, "STATS %" PRId64 " %s\n", epoch, url.c_str());
56 #include "hphp/runtime/vm/stats-opcodeDef.h"
57 #define STAT(s) \
58 if (!tl_counters[s]) {} else \
59 TRACE(0, "STAT %-50s %15" PRId64 "\n", #s, tl_counters[s]);
60 STATS
61 #undef STAT
62 #undef O
64 typedef std::pair<const char*, uint64_t> StatPair;
65 for (auto const& group : *tl_stat_groups) {
66 std::string stats;
67 auto const& map = group.second;
68 uint64_t total = 0, accum = 0;
69 size_t nameWidth = 0;
71 std::vector<StatPair> rows(map.begin(), map.end());
72 for (auto const& p : rows) {
73 nameWidth = std::max(nameWidth, strlen(p.first));
74 total += p.second;
76 auto gt = [](const StatPair& a, const StatPair& b) {
77 return a.second > b.second;
79 std::sort(rows.begin(), rows.end(), gt);
81 folly::format(&stats, "{:-^80}\n",
82 folly::format(" group {} ",group.first, url));
83 folly::format(&stats,
84 folly::format("{{:>{}}} {{:>9}} {{:>8}} {{:>8}}\n",
85 nameWidth + 2).str(),
86 "name", "count", "% total", "accum %");
88 static const auto maxGroupEnv = getenv("HHVM_STATS_GROUPMAX");
89 static const auto maxGroup = maxGroupEnv ? atoi(maxGroupEnv) : INT_MAX;
91 int counter = 0;
92 auto const fmt = folly::format("{{:>{}}} : {{:9}} {{:8.2%}} {{:8.2%}}\n",
93 nameWidth + 2).str();
94 for (auto const& row : rows) {
95 accum += row.second;
96 folly::format(&stats, fmt, row.first, row.second,
97 (double)row.second / total, (double)accum / total);
98 if (++counter >= maxGroup) break;
100 FTRACE(0, "{}\n", stats);
104 void clear() {
105 if (!enabledAny()) return;
106 ++epoch;
107 memset(&tl_counters[0], 0, sizeof(tl_counters));
109 assert(tl_stat_groups);
110 delete tl_stat_groups;
111 tl_stat_groups = nullptr;
114 void incStatGrouped(const StringData* category, const StringData* name, int n) {
115 assert(tl_stat_groups);
116 (*tl_stat_groups)[category->data()][name->data()] += n;