C ++ class used to obtain various system information in Linux

Source: Internet
Author: User

The following is the header file content, the code is part of the mooon, the corresponding CPP file, please directly browse: http://code.google.com/p/mooon/source/browse/trunk/common_library/src/sys/info.cpp

# Include <vector> # include "sys/config. h "SYS_NAMESPACE_BEGIN/*** is used to obtain various real-time information about the system, kernel, and process, such as CPU and memory data */class CInfo {public: /***** current real-time system information */typedef struct {long uptime_second;/* Seconds since boot */unsigned long average_load [3];/* 1, 5, and 15 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, 1 jiffies = 0.01 seconds uint64_t total; uint32_t user; /** indicates the running time of the user State from the system startup to the current time, excluding the nice value for the negative process */uint32_t nice; /** the CPU time occupied by processes with a negative nice value from system startup to the current time */uint32_t system, the running time in the core State */uint32_t idle;/** the wait time except the IO 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 system start to current time, hard interrupt time (2.6.0) */uint32_t softirq;/** indicates the Soft Interrupt time (2.6.0) * // uint32_t stealstolen from the start of the system to the current time; /** which is the time spent in other operating systems when running in a receivalized environment (2.6.11) * // uint32_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; Memory mem_free; memory buffers; Memory cached; uint32_t swap_cached; swap_total; swap_free;} mem_info_t; /***** kernel version number */typedef struct {int16_t major;/** main version number */int16_t minor;/** Times version number (if the minor version number is an even number, then the kernel is a stable version; if it is an odd number, it is a development version) */int16_t revision;/** revision version */} kernel_version_t; /***** current process status information ** Process status value: D Uninterruptible sleep (usually IO) R Running or runnable (on run queue) S Interruptible sleep (waiting for an event to complete) T Stopped, either by a job control signal or because it is being traced. W paging (not valid since the 2.6.xx kernel) X dead (shocould never be seen) Z Defunct ("zombie") process, terminated but not reaped by its parent. */typedef struct {/** 01 */pid_t pid;/** process number, which has the maximum value allowed, please check the name of the/proc/sys/kernel/pid_max * // ** 02 */char comm [FILENAME_MAX];/** process, path * // ** 03 */char state;/** Process status * // ** 04 */pid_t ppid; /** parent process Number */** 05 */pid_t pgrp;/** Process Group Number */** 06 */pid_t session; /** process session number * // ** 07 */int tty_nr;/** The tty the process uses * // ** 08 */pid_t tpgid; /** The tty the process uses * // ** 09 */unsigned int flags;/** The kernel flags word of the process (% lu before Linux 2.6.22) * // ** 10 */unsigned long minflt; /** The number of minor faults the process has made which have not required loading a memory page from disk * // ** 11 */unsigned long cminflt; /** The number of minor faults that the process's waited-for children have made * // ** 12 */unsigned long majflt; /** The number of major faults the process has made which have required loading a memory page from disk * // ** 13 */unsigned long cmajflt; /** The number of major faults that the process's waited-for children have made * // ** 14 */unsigned long utime; /** The number of jiffies that this process has been scheduled in user mode * // ** 15 */unsigned long stime; /** The number of jiffies that this process has been scheduled in kernel mode * // ** 16 */long cutime; /** The number of jiffies that this process's waited-for children have been scheduled in user mode * // ** 17 */long cstime; /** The number of jiffies that this process's waited-for children have been scheduled in kernel mode * // ** 18 */long priority; /** The standard nice value, plus deleteen. the value is never negative in the kernel * // ** 19 */long nice;/** The nice value ranges from 19 (nicest) to-19 (not nice to others) * // ** 20 */long num_threads;/** Number of threads in this process (since Linux 2.6 ). before kernel 2.6, this field was hard coded to 0 as a placeholder * // ** 21 */long itrealvalue; /** The time in jiffies before the next SIGALRM is sent to the process due to an interval timer.2.6.17, this field is no longer maintained, and is hard coded as 0 * // ** 22 */long starttime; /** The time in jiffies the process started after system boot * // ** 23 */unsigned long vsize; /** Virtual memory size in bytes * // ** 24 */long rss;/** Resident Set Size: number of pages the process has in real memory, minus 3 for administrative purposes * // *** 25 */unsigned long rlim;/** Current limit in bytes on the rss of the process (usually 4294967295 on i386) * // ** 26 */unsigned long startcode;/** The address above which program text can run * // ** 27 */unsigned long endcode; /** The address below which program text can run * // ** 28 */unsigned long startstack; /** The address of the start of the stack * // ** 29 */unsigned long kstkesp;/** The current value of esp (stack pointer ), as found in the kernel stack page for the process * // ** 30 */unsigned long kstkeip;/** The current EIP (instruction pointer) * // ** 31 */unsigned long signal;/** The bitmap of pending signals * // ** 32 */unsigned long blocked; /** The bitmap of blocked signals * // ** 33 */unsigned long sigignore;/** The bitmap of ignored signals */** 34 */unsigned long sigcatch; /** The bitmap of caught signals * // ** 35 */unsigned long nswap;/** Number of pages swapped (not maintained ). * // ** 36 */unsigned long cnswap;/** Cumulative nswap for child processes (not maintained) * // ** 37 */int exit_signal; /** Signal to be sent to parent when we die (since Linux 2.1.22) * // ** 38 */int processor; /** CPU number last executed on (since Linux 2.2.8) */} process_info_t; /***** network card Traffic Data Structure */typedef struct {/** 01 */char interface_name [INTERFACE_NAME_MAX];/*** Network Card Name, for example, eth0 * // ** receives data * // ** 02 */unsigned long receive_bytes; /** number of bytes received by this Nic * // ** 03 */unsigned long receive_packets;/** 04 */unsigned long receive_errors; /** 05 */unsigned long receive_dropped;/** 06 */unsigned long accept;/** 07 */unsigned long receive_frame;/** 08 */unsigned long receive_compressed; /** 09 */unsigned long receive_multicast;/** send data * // ** 10 */unsigned long transmit_bytes; /** number of bytes sent by this Nic * // ** 11 */unsigned long transmit_packets;/** 12 */unsigned long transmit_errors; /** 13 */unsigned long transmit_dropped;/** 14 */unsigned long queue;/** 15 */unsigned long transmit_collisions;/** 16 */unsigned long transmit_carrier; /** 17 */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;/** number of pages in shared memory */long text;/** number of pages in memory occupied by code segments */long lib; /** data/number of pages in the memory occupied by the stack segment */long data;/** number of pages in the memory occupied by the referenced database */} process_page_info_t; public:/** obtain system information, for details, refer to sys_info_t Description */static bool get_sys_info (sys_info_t & sys_info);/** to obtain memory information, for details, refer to the description of mem_info_t */static bool get_mem_info (mem_info_t & mem_info);/** to obtain the total CPU information, for details, see cpu_info_t Description */static bool get_cpu_info (cpu_info_t & cpu_info);/** to obtain all CPU information. For details, see cpu_info_t Description */static int get_cpu_info_array (std :: vector <cpu_info_t> & cpu_info_array);/** get the kernel version */static bool get_kernel_version (kernel_version_t & kernel_version);/** get process information, for details, refer to process_info_t Description */static bool get_process_info (process_info_t & process_info);/** to obtain the process page information, for details, see process_page_info_t Description */static bool get_process_page_info (process_page_info_t & process_page_info);/** get process running time data, for details, see process_time_t Description */static bool get_process_times (process_time_t & process_time ); /***** obtain Nic traffic and other information * traffic = (current value-value obtained in the previous time)/duration of the two intervals * @ interface_name: Nic name, for example, eth0 * @ net_info: stores 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_array);}; SYS_NAMESPACE_END

This article is from the "Fei Yue" blog, please be sure to keep this source http://mooon.blog.51cto.com/1246491/966165

Related Article

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.