track total size of static array and Unit/Class/Func
[hiphop-php.git] / hphp / runtime / server / server-stats.h
blobed07791c51d5ca5857ca6f085197acbfc316770a
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 #ifndef incl_HPHP_SERVERSTATS_H_
18 #define incl_HPHP_SERVERSTATS_H_
20 #include "hphp/runtime/base/execution-profiler.h"
21 #include "hphp/runtime/base/string-data.h"
22 #include "hphp/runtime/server/writer.h"
23 #include "hphp/util/hash-map.h"
24 #include "hphp/util/lock.h"
25 #include "hphp/util/thread-local.h"
27 #include <curl/curl.h>
28 #include <map>
29 #include <time.h>
30 #include <vector>
32 namespace HPHP {
33 ///////////////////////////////////////////////////////////////////////////////
35 struct ServerStats {
37 enum class ThreadMode {
38 Idling = 0,
39 Processing,
40 Writing,
41 PostProcessing
44 public:
45 static void Log(const std::string& name, int64_t value);
46 static int64_t Get(const std::string& name);
47 static void LogPage(const std::string& url, int code);
48 static void Reset();
49 static void Clear();
50 static std::string GetKeys();
51 static std::string Report(const std::string& keys,
52 const std::string& prefix);
54 // thread status functions
55 static void LogBytes(int64_t bytes);
56 static void StartRequest(const char* url, const char* clientIP,
57 const char* vhost);
58 static void SetThreadMode(ThreadMode mode);
59 static ThreadMode GetThreadMode();
60 static const char* ThreadModeString(ThreadMode mode);
61 static std::string ReportStatus(Writer::Format format);
63 // io status functions
64 static void SetThreadIOStatus(const char* name, const char* addr,
65 int64_t usWallTime = -1);
66 static Array GetThreadIOStatuses();
67 static void StartNetworkProfile();
68 static Array EndNetworkProfile();
70 static bool s_profile_network;
72 public:
73 ServerStats();
74 ~ServerStats();
76 static void GetLogger();
77 private:
78 enum UDF {
79 UDF_NONE = 1, // count
80 UDF_HIT = 2, // count per hit
81 UDF_SEC = 4, // count per second
83 PRECISION = 1000
85 using KeyMap = hphp_fast_string_map<int>;
86 using CounterMap = hphp_fast_string_map<int64_t>;
88 static Mutex s_lock;
89 static std::vector<ServerStats*> s_loggers;
90 static THREAD_LOCAL_NO_CHECK(ServerStats, s_logger);
92 struct TimeSlot {
93 uint32_t m_time{0};
94 uint32_t m_hits{0};
95 CounterMap m_values;
96 void clear() {
97 m_time = 0;
98 m_hits = 0;
99 m_values.clear();
101 bool empty() const {
102 return m_hits == 0;
106 static uint32_t curr() {
107 auto const now = static_cast<uint64_t>(time(nullptr));
108 return now / RuntimeOption::StatsSlotDuration;
111 static void Merge(CounterMap& dest, const CounterMap& src,
112 const KeyMap& wanted);
113 static void Merge(TimeSlot& dest, const TimeSlot& src,
114 const KeyMap& wanted);
115 static KeyMap CompileKeys(const std::string& keys);
116 static std::string Report(const TimeSlot& s, const std::string& prefix);
118 template<typename F> static void VisitAllSlots(F fun) {
119 Lock lock(s_lock, false);
120 for (auto& l : s_loggers) {
121 Lock _(l->m_lock, false);
122 for (auto& slot : l->m_slots) {
123 fun(slot);
128 Mutex m_lock;
129 std::vector<TimeSlot> m_slots;
130 CounterMap m_values; // current page's name value pairs
132 void log(const std::string& name, int64_t value);
133 int64_t get(const std::string& name);
134 void logPage(const std::string& url, int code);
135 void reset();
138 * Live status, instead of historical statistics.
140 void logBytes(int64_t bytes);
141 void startRequest(const char* url, const char* clientIP, const char* vhost);
142 void setThreadMode(ThreadMode mode) { m_threadStatus.m_mode = mode; }
144 void setThreadIOStatus(const char* name, const char* addr,
145 int64_t usWallTime = -1);
146 Array getThreadIOStatuses();
148 struct IOStatus {
149 int64_t count{0};
150 int64_t wall_time{0}; // micro-seconds
152 // keys: "url==>name" and "name==>address"
153 using IOStatusMap = hphp_fast_string_map<IOStatus>;
155 struct ThreadStatus {
156 ThreadStatus();
158 pthread_t m_threadId;
159 pid_t m_threadPid;
160 MemoryManager* m_mm;
162 // total traffic
163 int64_t m_requestCount;
164 int64_t m_writeBytes;
166 // current request
167 timeval m_start;
168 timeval m_done;
169 ThreadMode m_mode;
171 // Whether or not an io is in process.
172 bool m_ioInProcess;
174 // If an io is in process, the time that it started.
175 timespec m_ioStart;
177 char m_ioName[64];
178 char m_ioAddr[64];
179 char m_clientIP[64];
180 char m_vhost[64];
181 char m_url[512];
183 IOStatusMap m_ioStatuses;
185 ThreadStatus m_threadStatus;
186 IOStatusMap m_ioProfiles;
189 ///////////////////////////////////////////////////////////////////////////////
192 * Taking server stats at different time point of execution.
194 struct ServerStatsHelper {
195 enum {
196 TRACK_MEMORY = 0x00000001,
197 TRACK_HWINST = 0x00000002,
199 explicit ServerStatsHelper(const char* section, uint32_t track = 0);
200 ~ServerStatsHelper();
202 private:
203 const char* m_section;
204 timespec m_wallStart;
205 timespec m_cpuStart;
206 int64_t m_instStart;
207 uint32_t m_track;
209 void logTime(const std::string& prefix, const timespec& start,
210 const timespec& end);
211 void logTime(const std::string& prefix, const int64_t& start,
212 const int64_t& end);
216 * Recording I/O status in a scoped manner.
218 struct IOStatusHelper {
219 explicit IOStatusHelper(const char* name, const char* address = nullptr,
220 int port = 0);
221 ~IOStatusHelper();
223 private:
224 ExecutionProfiler m_exeProfiler;
228 * For profiling CURL calls.
230 void set_curl_statuses(CURL *cp, const char* url);
233 * For profiling mutexes.
235 void server_stats_log_mutex(const std::string& stack, int64_t elapsed_us);
237 ///////////////////////////////////////////////////////////////////////////////
240 #endif // incl_HPHP_SERVERSTATS_H_