Extensible implementation of reading proc information

Source: Internet
Author: User

Demand

1. Register the memory, the number of threads and other information on ZK for monitoring

2. Statistical information to prepare for the next load balancing.

Realize

This article only resolves issue 1.

From the online query, this information can be obtained from the proc file system, if you do not know proc, you can Google.

The Internet has read proc information Lib--libproc, that is Procps, is said to Htop and other implementation is based on it.

I downloaded it, and both the include and Lib were generated, and finally found a tutorial, the results

StackOverflow, see someone said there is a memory leak, need to do the following methods.

int Main (intChar**//  fillarg  //  Fillstat Used for cmd proctab* proc = openproc (Proc_fillarg |   while (proc_t* proc_info = readproc (proc, NULL)) {    // do something    freeproc (Proc_info)} Closeproc (proc);}


So look at the definition of proc_t, full of the style of the last century, seems to be lost in the information of various information, so helpless to give up, direct hand-to-face. The code is attached later, as follows are a few key technical points

1. The shell name can be obtained proc information, in C, we can build the pipeline through Popen, get the output of the shell command.

2. Get the process number, which can be obtained by getpid ()

3. sizeof (XX)/sizeof (xx[0]) can get the size of the array.

4. The zerohelper structure is designed to facilitate the expansion

5. offsetof macro to get the offset of an element

6. The application of function pointers is just right.

#include <time.h>#include<stddef.h>#include<string.h>#include<stdint.h>#include<ctype.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>structstatinfo{time_t start_time;    uint32_t duration_sec;    uint32_t conn_cnt;   uint32_t PMEM_MB; //Peak Virtual Memoryuint32_t VMEM_MB;//Virtual Memoryuint32_t RMEM_MB;//Real Memoryuint32_t thread_cnt;//Number of Threadsuint32_t cpu_usage;//CPU};typedefint(*CONVERT_FN) (Const Char* STR,void*value_ptr);structZerohelper {Const Char*key;    uint32_t Key_len;    uint32_t Value_offset; CONVERT_FN fn;};intProc_stat_mem_convert (Const Char* STR,void*value_ptr) {uint32_t* Value = (uint32_t*) Value_ptr;  while(*str && isspace (*str)) ++str; *value =atoi (str); return 0;}Static ConstZerohelper proc_mem_convert_array[] = {    {"Vmpeak:",sizeof("Vmpeak:") -1, Offsetof (Statinfo, PMEM_MB), Proc_stat_mem_convert}, {"vmsize:",sizeof("vmsize:") -1, Offsetof (Statinfo, VMEM_MB), Proc_stat_mem_convert}, {"Vmrss:",sizeof("Vmrss:") -1, Offsetof (Statinfo, RMEM_MB), Proc_stat_mem_convert}, {"Threads:",sizeof("Threads:") -1, Offsetof (Statinfo, thread_cnt), Proc_stat_mem_convert},};intFresh_memstat_info (pid_t pid, statinfo*info) {    CharProc_cmd [1024x768]; snprintf (Proc_cmd,sizeof(Proc_cmd),"Cat/proc/%d/status", PID); FILE* fp = Popen (Proc_cmd,"R"); Charproc_line[1024x768]; Constzerohelper* helper = & (proc_mem_convert_array[0]); Const intKhelperlen =sizeof(Proc_mem_convert_array)/sizeof(proc_mem_convert_array[0]); intj =0;  while(Fgets (Proc_line,sizeof(proc_line), fp)! =NULL) {        if(J >=Khelperlen) {             Break; }        //Ignore key Header        if(strncmp (proc_line, helper->key, helper->key_len) = =0) {Helper-&GT;FN (Proc_line + helper->Key_len, (Char*) Info + helper->value_offset); ++Helper; ++J;    }} pclose (FP); return 0;}intMain () {Statinfo info; pid_t PID=Getpid (); Fresh_memstat_info (PID,&info); printf ("%d\t%ukb\t%ukb\t%ukb\t%u\n", PID, INFO.PMEM_MB, Info.vmem_mb, INFO.RMEM_MB, info.thread_cnt); Sleep ( +); return 0;}
View Code

Extensible implementation of reading proc information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.