winlin

add comments for utility, the USER_HZ for /proc/stat

@@ -283,6 +283,17 @@ bool get_proc_self_stat(SrsProcSelfStat& r) @@ -283,6 +283,17 @@ bool get_proc_self_stat(SrsProcSelfStat& r)
283 283
284 void srs_update_proc_stat() 284 void srs_update_proc_stat()
285 { 285 {
  286 + // always assert the USER_HZ is 1/100ths
  287 + // @see: http://stackoverflow.com/questions/7298646/calculating-user-nice-sys-idle-iowait-irq-and-sirq-from-proc-stat/7298711
  288 + static bool user_hz_assert = false;
  289 + if (!user_hz_assert) {
  290 + user_hz_assert = true;
  291 +
  292 + int USER_HZ = sysconf(_SC_CLK_TCK);
  293 + srs_trace("USER_HZ=%d", USER_HZ);
  294 + srs_assert(USER_HZ == 100);
  295 + }
  296 +
286 // system cpu stat 297 // system cpu stat
287 if (true) { 298 if (true) {
288 SrsProcSystemStat r; 299 SrsProcSystemStat r;
@@ -49,6 +49,7 @@ extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_ @@ -49,6 +49,7 @@ extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_
49 */ 49 */
50 extern int srs_get_log_level(std::string level); 50 extern int srs_get_log_level(std::string level);
51 51
  52 +// current process resouce usage.
52 // @see: man getrusage 53 // @see: man getrusage
53 class SrsRusage 54 class SrsRusage
54 { 55 {
@@ -68,6 +69,7 @@ extern SrsRusage* srs_get_system_rusage(); @@ -68,6 +69,7 @@ extern SrsRusage* srs_get_system_rusage();
68 // the deamon st-thread will update it. 69 // the deamon st-thread will update it.
69 extern void srs_update_system_rusage(); 70 extern void srs_update_system_rusage();
70 71
  72 +// to stat the process info.
71 // @see: man 5 proc, /proc/[pid]/stat 73 // @see: man 5 proc, /proc/[pid]/stat
72 class SrsProcSelfStat 74 class SrsProcSelfStat
73 { 75 {
@@ -220,7 +222,23 @@ public: @@ -220,7 +222,23 @@ public:
220 SrsProcSelfStat(); 222 SrsProcSelfStat();
221 }; 223 };
222 224
  225 +// to stat the cpu time.
223 // @see: man 5 proc, /proc/stat 226 // @see: man 5 proc, /proc/stat
  227 +/**
  228 +* about the cpu time, @see: http://stackoverflow.com/questions/16011677/calculating-cpu-usage-using-proc-files
  229 +* for example, for ossrs.net, a single cpu machine:
  230 +* [winlin@SRS ~]$ cat /proc/uptime && cat /proc/stat
  231 +* 5275153.01 4699624.99
  232 +* cpu 43506750 973 8545744 466133337 4149365 190852 804666 0 0
  233 +* where the uptime is 5275153.01s
  234 +* generally, USER_HZ sysconf(_SC_CLK_TCK)=100, which means the unit of /proc/stat is "1/100ths seconds"
  235 +* that is, USER_HZ=1/100 seconds
  236 +* cpu total = 43506750+973+8545744+466133337+4149365+190852+804666+0+0 (USER_HZ)
  237 +* = 523331687 (USER_HZ)
  238 +* = 523331687 * 1/100 (seconds)
  239 +* = 5233316.87 seconds
  240 +* the cpu total seconds almost the uptime, the delta is more precise.
  241 +*/
224 class SrsProcSystemStat 242 class SrsProcSystemStat
225 { 243 {
226 public: 244 public:
@@ -234,7 +252,8 @@ public: @@ -234,7 +252,8 @@ public:
234 // always be cpu 252 // always be cpu
235 char label[32]; 253 char label[32];
236 254
237 - //The amount of time, measured in units of USER_HZ (1/100ths of a second on most architectures, use 255 + // The amount of time, measured in units of USER_HZ
  256 + // (1/100ths of a second on most architectures, use
238 // sysconf(_SC_CLK_TCK) to obtain the right value) 257 // sysconf(_SC_CLK_TCK) to obtain the right value)
239 // 258 //
240 // the system spent in user mode, 259 // the system spent in user mode,
@@ -275,6 +294,7 @@ extern SrsProcSystemStat* srs_get_system_proc_stat(); @@ -275,6 +294,7 @@ extern SrsProcSystemStat* srs_get_system_proc_stat();
275 // the deamon st-thread will update it. 294 // the deamon st-thread will update it.
276 extern void srs_update_proc_stat(); 295 extern void srs_update_proc_stat();
277 296
  297 +// stat system memory info
278 // @see: cat /proc/meminfo 298 // @see: cat /proc/meminfo
279 class SrsMemInfo 299 class SrsMemInfo
280 { 300 {
@@ -312,6 +332,7 @@ extern SrsMemInfo* srs_get_meminfo(); @@ -312,6 +332,7 @@ extern SrsMemInfo* srs_get_meminfo();
312 // the deamon st-thread will update it. 332 // the deamon st-thread will update it.
313 extern void srs_update_meminfo(); 333 extern void srs_update_meminfo();
314 334
  335 +// system cpu hardware info.
315 // @see: cat /proc/cpuinfo 336 // @see: cat /proc/cpuinfo
316 class SrsCpuInfo 337 class SrsCpuInfo
317 { 338 {
@@ -330,7 +351,7 @@ public: @@ -330,7 +351,7 @@ public:
330 // get system cpu info, use cache to avoid performance problem. 351 // get system cpu info, use cache to avoid performance problem.
331 extern SrsCpuInfo* srs_get_cpuinfo(); 352 extern SrsCpuInfo* srs_get_cpuinfo();
332 353
333 -// platform(os, srs) summary 354 +// platform(os, srs) uptime/load summary
334 class SrsPlatformInfo 355 class SrsPlatformInfo
335 { 356 {
336 public: 357 public:
@@ -341,7 +362,11 @@ public: @@ -341,7 +362,11 @@ public:
341 int64_t srs_startup_time; 362 int64_t srs_startup_time;
342 363
343 // @see: cat /proc/uptime 364 // @see: cat /proc/uptime
  365 + // system startup time in seconds.
344 double os_uptime; 366 double os_uptime;
  367 + // system all cpu idle time in seconds.
  368 + // @remark to cal the cpu ustime percent:
  369 + // os_ilde_time % (os_uptime * SrsCpuInfo.nb_processors_online)
345 double os_ilde_time; 370 double os_ilde_time;
346 371
347 // @see: cat /proc/loadavg 372 // @see: cat /proc/loadavg
@@ -446,6 +471,7 @@ std::string srs_get_local_ip(int fd); @@ -446,6 +471,7 @@ std::string srs_get_local_ip(int fd);
446 // where peer ip is the client public ip which connected to server. 471 // where peer ip is the client public ip which connected to server.
447 std::string srs_get_peer_ip(int fd); 472 std::string srs_get_peer_ip(int fd);
448 473
  474 +// dump summaries for /api/v1/summaries.
449 void srs_api_dump_summaries(std::stringstream& ss); 475 void srs_api_dump_summaries(std::stringstream& ss);
450 476
451 #endif 477 #endif