#include <vector> #include "sys/config.h" sys_namespace_begin/*** * To obtain various types of real-time information about systems, cores, and processes, such as CPU and memory data */class cinfo {public:/*** * System Current Real-time information */typedef struct {long uptime_sec Ond /* Seconds since boot */unsigned long average_load[3]; /* 1, 5, and minute load averages */unsigned long ram_total; /* Total usable main memory size */unsigned long ram_free; /* Available Memory Size */unsigned long ram_shared; /* Amount of Shared memory */unsigned long ram_buffer; /* Memory used by buffers */unsigned long swap_total; /* Total swap space size */unsigned long swap_free; /* Swap space still available */unsigned short process_number; /* Number of current processes */}sys_info_t; /*** * Current Process time information */typedef struct {long user_time; /* User Time */ Long System_time; /* System Time * */long User_time_children; /* User Time of children */long system_time_children; /* System time of children */}process_time_t; /*** * Current system CPU information */typedef struct {//unit: jiffies, 1jiffies=0.01 s uint64_t tot Al uint32_t user; /** from the start of the system to the current time, in the user state run time, does not contain the Nice value is negative process */uint32_t nice; /** from the start of the system start to the current moment, the nice value is negative the CPU time occupied by the process * */uint32_t system; /** from the start of the system to accumulate to the current moment, in the nuclear mentality of the running time * * uint32_t idle; /** from the start of the system to the current moment, in addition to the IO wait time wait time * * * uint32_t iowait; /** from the start of the system to the current time, IO wait time (2.5.41) */uint32_t IRQ; /** from the start of the system to the current moment, hard interrupt Time (2.6.0) */uint32_t SOFTIRQ; /** from the start of the system to the current time, soft interrupt Time (2.6.0) *///uint32_t Stealstolen; /** which is the time spent in other operating systems when running in a virtualized environment (2.6.11) *///ui nt32_t Guest; /** which is the time spent running a VirtuaL CPU for guest operating systems under the control of the Linux kernel (2.6.24) */}cpu_info_t; /*** * Current system memory information */typedef struct {uint32_t mem_total; uint32_t Mem_free; uint32_t buffers; uint32_t cached; uint32_t swap_cached; uint32_t swap_total; uint32_t Swap_free; }mem_info_t; /*** * Kernel Version number */typedef struct {int16_t major; /** Main Version number */int16_t minor; /** Minor Version number (if the minor version number is even, then the kernel is stable version, if the odd is the development version) */int16_t revision; /** Revision number */}kernel_version_t; /*** * Process status information * * Status value of process: D uninterruptible sleep (usually IO) R Runni ng or runnable (on run queue) S interruptible sleep (waiting for a event to complete) T Stopped , either by a job control signal or because it is being traced. W paging (not valid sinceThe 2.6.xx kernel) X dead (should never is seen) Z Defunct ("zombie") process, terminated but no T reaped by its parent. */typedef struct {/** */pid_t pid; /** the process number, its maximum allowable value, please see/proc/sys/kernel/pid_max *//** (*/char Comm[filename_max]; The name of the/** process, excluding the path *//** */char state; Status of the/** process *//** */pid_t ppid; /** Parent Process Number *//** */pid_t pgrp; /** Process Group Number *//** */pid_t session; /** Process Session Number *//** */int tty_nr; /** The TTY the process uses * *//**/pid_t Tpgid; /** The TTY the process uses *//** */unsigned int flags; /** The kernel flags Word of the process (%lu before Linux 2.6.22) *//** */unsigned long minflt; /** the number of minor faults the process has made which had not required loading a MEmory page from disk *//** one */unsigned long cminflt; /** the number of minor faults that the process ' s waited-for children has made *//**/unsigned long majflt; /** the number of major faults the process has made which having required loading a memory page from disk */ /** */unsigned long cmajflt; /** the number of major faults that the process ' s waited-for children has made *//**/unsigned long utime; /** the number of jiffies the This process have been scheduled in user mode *//** */unsigned long St Ime /** the number of jiffies the This process have been scheduled in kernel mode *//** */long cutime; /** the number of jiffies that this process ' s waited-for children has been scheduled in user mode */* * * */long cstime; /** the number of jiffies that this process ' s waited-for children has been scheduled in kernel mode *//** */long priority; /** the standard nice value, plus fifteen. The value is never negative in the kernel *//** */long nice; /** the Nice value ranges from (nicest) to-19 (not nice to others) *//** */long num_threads; /** number of threads in this process (since Linux 2.6). Before kernel 2.6, this field is hard coded to 0 as a placeholder *//** */long itrealvalue; /** the time in Jiffies before the next sigalrm are sent to the process due to a interval timer.2.6.17, this field is n o longer maintained, and is hard coded as 0 *//** * */long long starttime; /** the time in jiffies the process started after system boot *//** */unsigned long vsize; /** Virtual Memory Size in bytes *//** */Long RSS; /** resident Set Size:number of pages The process have in real memory, minus 3 for administrative purPoses *//** */unsigned long rlim; /** current limit in bytes on the RSS of the process (usually 4294967295 on i386) *//**-*/unsigned long STARTC Ode /** the address above which program text can run *//** */unsigned long endcode; /** the address below which program text can run *//** */unsigned long startstack; /** the address of the start of the stack * */** */unsigned long kstkesp; /** the current value of ESP (stack pointer), as found in the kernel stack page for the process *//** */unsign Ed Long Kstkeip; /** the current EIP (instruction Pointer) *//** */unsigned long signal; /** the bitmap of pending signals *//** */unsigned long blocked; /** the bitmap of blocked signals *//** */unsigned long sigignore; /** the bitmap of ignored signals *//**/unsigned long sigcatch; /** the bitmap of CaugHT Signals *//** */unsigned long nswap; /** number of pages swapped (not maintained). *//** */unsigned long cnswap; /** cumulative Nswap for child processes (not maintained) *//** PNS */int exit_signal; /** Signal to is sent to the parent when we die (since Linux 2.1.22) *//** */INT processor; /** CPU number last executed on (since Linux 2.2.8) */}process_info_t; /*** * NIC Traffic data structure */typedef struct {/** */char Interface_name[interface_name_max];/** Network card name, such as Eth0 *//** Receive data *//** */unsigned long receive_bytes; /** the number of bytes received by this NIC *//** */unsigned long receive_packets; /** */unsigned long receive_errors; /** * */unsigned long receive_dropped; /** */unsigned long receive_fifo_errors; /** */unsigned long receive_frame; /** */UnsigNed Long receive_compressed; /** * * * unsigned long receive_multicast; /** Send data *//** */unsigned long transmit_bytes; /** the number of bytes sent by this NIC *//** One */unsigned long transmit_packets; /** */unsigned long transmit_errors; /** */unsigned long transmit_dropped; /** */unsigned long transmit_fifo_errors; /** */unsigned long transmit_collisions; /** */unsigned long transmit_carrier; /** */unsigned long transmit_compressed; }net_info_t; /*** * Process Page Information structure */typedef struct {long size; /** Program Size */long resident; /** resident Memory Space size */long share; /** Shared Memory pages */long text; /** code segment occupies memory page */long lib; /** data/Stack segment occupies memory page */Long data; /** Reference Library occupies memory pages */}process_page_info_t; Public:/** Get system Information, please refer to the description of sys_info_t */static bool get_sYs_info (sys_info_t& sys_info); /** Get memory information, please refer to the description of mem_info_t */static bool Get_mem_info (mem_info_t& mem_info); /** Get total CPU information, please refer to the description of cpu_info_t */static bool Get_cpu_info (cpu_info_t& cpu_info); /** get all CPU information, please refer to cpu_info_t description */static int get_cpu_info_array (std::vector<cpu_info_t>& cpu_info_array) ; /** get the kernel version number */static bool Get_kernel_version (kernel_version_t& kernel_version); /** Get process information, please refer to the description of process_info_t */static bool Get_process_info (process_info_t& process_info); /** Get the Process page information, please refer to the description of process_page_info_t */static bool Get_process_page_info (process_page_info_t& Process_page_ info); /** get the process runtime data, please refer to the description of process_time_t */static bool Get_process_times (process_time_t& process_time); /*** * Get information such as network card traffic * traffic = (currently acquired value-the value obtained at the last time)/length of two intervals * @interface_name: Network card name, such as eth0, etc. * @net_inFO: Storage network card traffic and other data * * static bool Get_net_info (const char* interface_name, net_info_t& net_info); static bool Get_net_info_array (std::vector<net_info_t>& Net_info_array); private:static bool Do_get_net_info_array (const char* interface_name, std::vector<net_info_t>& net_info_a Rray); }; Sys_namespace_end
C + + classes for obtaining various system information under Linux