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 #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>
33 ///////////////////////////////////////////////////////////////////////////////
37 enum class ThreadMode
{
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
);
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
,
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
;
76 static void GetLogger();
79 UDF_NONE
= 1, // count
80 UDF_HIT
= 2, // count per hit
81 UDF_SEC
= 4, // count per second
85 using KeyMap
= hphp_fast_string_map
<int>;
86 using CounterMap
= hphp_fast_string_map
<int64_t>;
89 static std::vector
<ServerStats
*> s_loggers
;
90 static THREAD_LOCAL_NO_CHECK(ServerStats
, s_logger
);
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
) {
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
);
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();
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
{
158 pthread_t m_threadId
;
163 int64_t m_requestCount
;
164 int64_t m_writeBytes
;
171 // Whether or not an io is in process.
174 // If an io is in process, the time that it started.
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
{
196 TRACK_MEMORY
= 0x00000001,
197 TRACK_HWINST
= 0x00000002,
199 explicit ServerStatsHelper(const char* section
, uint32_t track
= 0);
200 ~ServerStatsHelper();
203 const char* m_section
;
204 timespec m_wallStart
;
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
,
216 * Recording I/O status in a scoped manner.
218 struct IOStatusHelper
{
219 explicit IOStatusHelper(const char* name
, const char* address
= nullptr,
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_