winlin

get self proc and system proc stat

@@ -118,6 +118,8 @@ SrsApiV1::SrsApiV1() @@ -118,6 +118,8 @@ SrsApiV1::SrsApiV1()
118 handlers.push_back(new SrsApiVersion()); 118 handlers.push_back(new SrsApiVersion());
119 handlers.push_back(new SrsApiSummaries()); 119 handlers.push_back(new SrsApiSummaries());
120 handlers.push_back(new SrsApiRusages()); 120 handlers.push_back(new SrsApiRusages());
  121 + handlers.push_back(new SrsApiSelfProcStats());
  122 + handlers.push_back(new SrsApiSystemProcStats());
121 handlers.push_back(new SrsApiAuthors()); 123 handlers.push_back(new SrsApiAuthors());
122 } 124 }
123 125
@@ -140,7 +142,8 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -140,7 +142,8 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
140 << JFIELD_STR("versions", "the version of SRS") << JFIELD_CONT 142 << JFIELD_STR("versions", "the version of SRS") << JFIELD_CONT
141 << JFIELD_STR("summaries", "the summary(pid, argv, pwd, cpu, mem) of SRS") << JFIELD_CONT 143 << JFIELD_STR("summaries", "the summary(pid, argv, pwd, cpu, mem) of SRS") << JFIELD_CONT
142 << JFIELD_STR("rusages", "the rusage of SRS") << JFIELD_CONT 144 << JFIELD_STR("rusages", "the rusage of SRS") << JFIELD_CONT
143 - << JFIELD_STR("proc_stats", "the /proc/self/stat of SRS") << JFIELD_CONT 145 + << JFIELD_STR("self_proc_stats", "the self process stats") << JFIELD_CONT
  146 + << JFIELD_STR("system_proc_stats", "the system process stats") << JFIELD_CONT
144 << JFIELD_STR("authors", "the primary authors and contributors") 147 << JFIELD_STR("authors", "the primary authors and contributors")
145 << JOBJECT_END 148 << JOBJECT_END
146 << JOBJECT_END; 149 << JOBJECT_END;
@@ -196,8 +199,8 @@ int SrsApiSummaries::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -196,8 +199,8 @@ int SrsApiSummaries::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
196 std::stringstream ss; 199 std::stringstream ss;
197 200
198 SrsRusage* r = srs_get_system_rusage(); 201 SrsRusage* r = srs_get_system_rusage();
199 - SrsCpuSelfStat* u = srs_get_self_cpu_stat();  
200 - SrsCpuSystemStat* s = srs_get_system_cpu_stat(); 202 + SrsProcSelfStat* u = srs_get_self_proc_stat();
  203 + SrsProcSystemStat* s = srs_get_system_proc_stat();
201 204
202 ss << JOBJECT_START 205 ss << JOBJECT_START
203 << JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT 206 << JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
@@ -261,6 +264,117 @@ int SrsApiRusages::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -261,6 +264,117 @@ int SrsApiRusages::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
261 return res_json(skt, req, ss.str()); 264 return res_json(skt, req, ss.str());
262 } 265 }
263 266
  267 +SrsApiSelfProcStats::SrsApiSelfProcStats()
  268 +{
  269 +}
  270 +
  271 +SrsApiSelfProcStats::~SrsApiSelfProcStats()
  272 +{
  273 +}
  274 +
  275 +bool SrsApiSelfProcStats::can_handle(const char* path, int length, const char** /*pchild*/)
  276 +{
  277 + return srs_path_equals("/self_proc_stats", path, length);
  278 +}
  279 +
  280 +int SrsApiSelfProcStats::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
  281 +{
  282 + std::stringstream ss;
  283 +
  284 + SrsProcSelfStat* u = srs_get_self_proc_stat();
  285 +
  286 + ss << JOBJECT_START
  287 + << JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
  288 + << JFIELD_ORG("data", JOBJECT_START)
  289 + << JFIELD_ORG("self_cpu_stat_ok", (u->ok? "true":"false")) << JFIELD_CONT
  290 + << JFIELD_ORG("pid", u->pid) << JFIELD_CONT
  291 + << JFIELD_STR("comm", u->comm) << JFIELD_CONT
  292 + << JFIELD_STR("state", u->state) << JFIELD_CONT
  293 + << JFIELD_ORG("ppid", u->ppid) << JFIELD_CONT
  294 + << JFIELD_ORG("pgrp", u->pgrp) << JFIELD_CONT
  295 + << JFIELD_ORG("session", u->session) << JFIELD_CONT
  296 + << JFIELD_ORG("tty_nr", u->tty_nr) << JFIELD_CONT
  297 + << JFIELD_ORG("tpgid", u->tpgid) << JFIELD_CONT
  298 + << JFIELD_ORG("flags", u->flags) << JFIELD_CONT
  299 + << JFIELD_ORG("minflt", u->minflt) << JFIELD_CONT
  300 + << JFIELD_ORG("cminflt", u->cminflt) << JFIELD_CONT
  301 + << JFIELD_ORG("majflt", u->majflt) << JFIELD_CONT
  302 + << JFIELD_ORG("cmajflt", u->cmajflt) << JFIELD_CONT
  303 + << JFIELD_ORG("utime", u->utime) << JFIELD_CONT
  304 + << JFIELD_ORG("stime", u->stime) << JFIELD_CONT
  305 + << JFIELD_ORG("cutime", u->cutime) << JFIELD_CONT
  306 + << JFIELD_ORG("cstime", u->cstime) << JFIELD_CONT
  307 + << JFIELD_ORG("priority", u->priority) << JFIELD_CONT
  308 + << JFIELD_ORG("nice", u->nice) << JFIELD_CONT
  309 + << JFIELD_ORG("num_threads", u->num_threads) << JFIELD_CONT
  310 + << JFIELD_ORG("itrealvalue", u->itrealvalue) << JFIELD_CONT
  311 + << JFIELD_ORG("starttime", u->starttime) << JFIELD_CONT
  312 + << JFIELD_ORG("vsize", u->vsize) << JFIELD_CONT
  313 + << JFIELD_ORG("rss", u->rss) << JFIELD_CONT
  314 + << JFIELD_ORG("rsslim", u->rsslim) << JFIELD_CONT
  315 + << JFIELD_ORG("startcode", u->startcode) << JFIELD_CONT
  316 + << JFIELD_ORG("endcode", u->endcode) << JFIELD_CONT
  317 + << JFIELD_ORG("startstack", u->startstack) << JFIELD_CONT
  318 + << JFIELD_ORG("kstkesp", u->kstkesp) << JFIELD_CONT
  319 + << JFIELD_ORG("kstkeip", u->kstkeip) << JFIELD_CONT
  320 + << JFIELD_ORG("signal", u->signal) << JFIELD_CONT
  321 + << JFIELD_ORG("blocked", u->blocked) << JFIELD_CONT
  322 + << JFIELD_ORG("sigignore", u->sigignore) << JFIELD_CONT
  323 + << JFIELD_ORG("sigcatch", u->sigcatch) << JFIELD_CONT
  324 + << JFIELD_ORG("wchan", u->wchan) << JFIELD_CONT
  325 + << JFIELD_ORG("nswap", u->nswap) << JFIELD_CONT
  326 + << JFIELD_ORG("cnswap", u->cnswap) << JFIELD_CONT
  327 + << JFIELD_ORG("exit_signal", u->exit_signal) << JFIELD_CONT
  328 + << JFIELD_ORG("processor", u->processor) << JFIELD_CONT
  329 + << JFIELD_ORG("rt_priority", u->rt_priority) << JFIELD_CONT
  330 + << JFIELD_ORG("policy", u->policy) << JFIELD_CONT
  331 + << JFIELD_ORG("delayacct_blkio_ticks", u->delayacct_blkio_ticks) << JFIELD_CONT
  332 + << JFIELD_ORG("guest_time", u->guest_time) << JFIELD_CONT
  333 + << JFIELD_ORG("cguest_time", u->cguest_time)
  334 + << JOBJECT_END
  335 + << JOBJECT_END;
  336 +
  337 + return res_json(skt, req, ss.str());
  338 +}
  339 +
  340 +SrsApiSystemProcStats::SrsApiSystemProcStats()
  341 +{
  342 +}
  343 +
  344 +SrsApiSystemProcStats::~SrsApiSystemProcStats()
  345 +{
  346 +}
  347 +
  348 +bool SrsApiSystemProcStats::can_handle(const char* path, int length, const char** /*pchild*/)
  349 +{
  350 + return srs_path_equals("/system_proc_stats", path, length);
  351 +}
  352 +
  353 +int SrsApiSystemProcStats::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
  354 +{
  355 + std::stringstream ss;
  356 +
  357 + SrsProcSystemStat* s = srs_get_system_proc_stat();
  358 +
  359 + ss << JOBJECT_START
  360 + << JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
  361 + << JFIELD_ORG("data", JOBJECT_START)
  362 + << JFIELD_ORG("system_cpu_stat_ok", (s->ok? "true":"false")) << JFIELD_CONT
  363 + << JFIELD_ORG("user", s->user) << JFIELD_CONT
  364 + << JFIELD_ORG("nice", s->nice) << JFIELD_CONT
  365 + << JFIELD_ORG("sys", s->sys) << JFIELD_CONT
  366 + << JFIELD_ORG("idle", s->idle) << JFIELD_CONT
  367 + << JFIELD_ORG("iowait", s->iowait) << JFIELD_CONT
  368 + << JFIELD_ORG("irq", s->irq) << JFIELD_CONT
  369 + << JFIELD_ORG("softirq", s->softirq) << JFIELD_CONT
  370 + << JFIELD_ORG("steal", s->steal) << JFIELD_CONT
  371 + << JFIELD_ORG("guest", s->guest)
  372 + << JOBJECT_END
  373 + << JOBJECT_END;
  374 +
  375 + return res_json(skt, req, ss.str());
  376 +}
  377 +
264 SrsApiAuthors::SrsApiAuthors() 378 SrsApiAuthors::SrsApiAuthors()
265 { 379 {
266 } 380 }
@@ -109,6 +109,28 @@ protected: @@ -109,6 +109,28 @@ protected:
109 virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req); 109 virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
110 }; 110 };
111 111
  112 +class SrsApiSelfProcStats : public SrsHttpHandler
  113 +{
  114 +public:
  115 + SrsApiSelfProcStats();
  116 + virtual ~SrsApiSelfProcStats();
  117 +public:
  118 + virtual bool can_handle(const char* path, int length, const char** pchild);
  119 +protected:
  120 + virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
  121 +};
  122 +
  123 +class SrsApiSystemProcStats : public SrsHttpHandler
  124 +{
  125 +public:
  126 + SrsApiSystemProcStats();
  127 + virtual ~SrsApiSystemProcStats();
  128 +public:
  129 + virtual bool can_handle(const char* path, int length, const char** pchild);
  130 +protected:
  131 + virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
  132 +};
  133 +
112 class SrsApiAuthors : public SrsHttpHandler 134 class SrsApiAuthors : public SrsHttpHandler
113 { 135 {
114 public: 136 public:
@@ -447,7 +447,7 @@ int SrsServer::cycle() @@ -447,7 +447,7 @@ int SrsServer::cycle()
447 srs_update_system_rusage(); 447 srs_update_system_rusage();
448 } 448 }
449 if (i == SRS_SYS_CPU_STAT_RESOLUTION_TIMES) { 449 if (i == SRS_SYS_CPU_STAT_RESOLUTION_TIMES) {
450 - srs_update_system_cpu_stat(); 450 + srs_update_proc_stat();
451 } 451 }
452 } 452 }
453 } 453 }
@@ -81,30 +81,30 @@ void srs_update_system_rusage() @@ -81,30 +81,30 @@ void srs_update_system_rusage()
81 _srs_system_rusage.ok = true; 81 _srs_system_rusage.ok = true;
82 } 82 }
83 83
84 -static SrsCpuSelfStat _srs_system_cpu_self_stat;  
85 -static SrsCpuSystemStat _srs_system_cpu_system_stat; 84 +static SrsProcSelfStat _srs_system_cpu_self_stat;
  85 +static SrsProcSystemStat _srs_system_cpu_system_stat;
86 86
87 -SrsCpuSelfStat::SrsCpuSelfStat() 87 +SrsProcSelfStat::SrsProcSelfStat()
88 { 88 {
89 ok = false; 89 ok = false;
90 } 90 }
91 91
92 -SrsCpuSystemStat::SrsCpuSystemStat() 92 +SrsProcSystemStat::SrsProcSystemStat()
93 { 93 {
94 ok = false; 94 ok = false;
95 } 95 }
96 96
97 -SrsCpuSelfStat* srs_get_self_cpu_stat() 97 +SrsProcSelfStat* srs_get_self_proc_stat()
98 { 98 {
99 return &_srs_system_cpu_self_stat; 99 return &_srs_system_cpu_self_stat;
100 } 100 }
101 101
102 -SrsCpuSystemStat* srs_get_system_cpu_stat() 102 +SrsProcSystemStat* srs_get_system_proc_stat()
103 { 103 {
104 return &_srs_system_cpu_system_stat; 104 return &_srs_system_cpu_system_stat;
105 } 105 }
106 106
107 -void srs_update_system_cpu_stat() 107 +void srs_update_proc_stat()
108 { 108 {
109 // system cpu stat 109 // system cpu stat
110 if (true) { 110 if (true) {
@@ -114,7 +114,7 @@ void srs_update_system_cpu_stat() @@ -114,7 +114,7 @@ void srs_update_system_cpu_stat()
114 return; 114 return;
115 } 115 }
116 116
117 - SrsCpuSystemStat& r = _srs_system_cpu_system_stat; 117 + SrsProcSystemStat& r = _srs_system_cpu_system_stat;
118 for (;;) { 118 for (;;) {
119 int ret = fscanf(f, "%4s %lu %lu %lu %lu %lu " 119 int ret = fscanf(f, "%4s %lu %lu %lu %lu %lu "
120 "%lu %lu %lu %lu\n", 120 "%lu %lu %lu %lu\n",
@@ -143,7 +143,7 @@ void srs_update_system_cpu_stat() @@ -143,7 +143,7 @@ void srs_update_system_cpu_stat()
143 return; 143 return;
144 } 144 }
145 145
146 - SrsCpuSelfStat& r = _srs_system_cpu_self_stat; 146 + SrsProcSelfStat& r = _srs_system_cpu_self_stat;
147 int ret = fscanf(f, "%d %32s %c %d %d %d %d " 147 int ret = fscanf(f, "%d %32s %c %d %d %d %d "
148 "%d %u %lu %lu %lu %lu " 148 "%d %u %lu %lu %lu %lu "
149 "%lu %lu %ld %ld %ld %ld " 149 "%lu %lu %ld %ld %ld %ld "
@@ -52,7 +52,7 @@ extern SrsRusage* srs_get_system_rusage(); @@ -52,7 +52,7 @@ extern SrsRusage* srs_get_system_rusage();
52 extern void srs_update_system_rusage(); 52 extern void srs_update_system_rusage();
53 53
54 // @see: man 5 proc, /proc/[pid]/stat 54 // @see: man 5 proc, /proc/[pid]/stat
55 -struct SrsCpuSelfStat 55 +struct SrsProcSelfStat
56 { 56 {
57 // whether the data is ok. 57 // whether the data is ok.
58 bool ok; 58 bool ok;
@@ -195,11 +195,11 @@ struct SrsCpuSelfStat @@ -195,11 +195,11 @@ struct SrsCpuSelfStat
195 // Guest time of the process’s children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). 195 // Guest time of the process’s children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
196 long cguest_time; 196 long cguest_time;
197 197
198 - SrsCpuSelfStat(); 198 + SrsProcSelfStat();
199 }; 199 };
200 200
201 // @see: man 5 proc, /proc/stat 201 // @see: man 5 proc, /proc/stat
202 -struct SrsCpuSystemStat 202 +struct SrsProcSystemStat
203 { 203 {
204 // whether the data is ok. 204 // whether the data is ok.
205 bool ok; 205 bool ok;
@@ -238,14 +238,14 @@ struct SrsCpuSystemStat @@ -238,14 +238,14 @@ struct SrsCpuSystemStat
238 // operating systems under the control of the Linux kernel. 238 // operating systems under the control of the Linux kernel.
239 unsigned long guest; 239 unsigned long guest;
240 240
241 - SrsCpuSystemStat(); 241 + SrsProcSystemStat();
242 }; 242 };
243 243
244 // get system cpu stat, use cache to avoid performance problem. 244 // get system cpu stat, use cache to avoid performance problem.
245 -extern SrsCpuSelfStat* srs_get_self_cpu_stat(); 245 +extern SrsProcSelfStat* srs_get_self_proc_stat();
246 // get system cpu stat, use cache to avoid performance problem. 246 // get system cpu stat, use cache to avoid performance problem.
247 -extern SrsCpuSystemStat* srs_get_system_cpu_stat(); 247 +extern SrsProcSystemStat* srs_get_system_proc_stat();
248 // the deamon st-thread will update it. 248 // the deamon st-thread will update it.
249 -extern void srs_update_system_cpu_stat(); 249 +extern void srs_update_proc_stat();
250 250
251 #endif 251 #endif