Linux下用来获取各种系统信息的C++类
生活随笔
收集整理的這篇文章主要介紹了
Linux下用来获取各种系统信息的C++类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <vector>
#include "sys/config.h"
SYS_NAMESPACE_BEGIN /*** * 用來獲取系統(tǒng)、內(nèi)核和進(jìn)程的各類實(shí)時(shí)信息,如CPU和內(nèi)存數(shù)據(jù) */
class CInfo
{
public: /*** * 系統(tǒng)當(dāng)前實(shí)時(shí)信息 */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; /*** * 當(dāng)前進(jìn)程時(shí)間信息 */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; /*** * 當(dāng)前系統(tǒng)CPU信息 */typedef struct{ // 單位: jiffies, 1jiffies=0.01秒 uint64_t total; uint32_t user; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻。處于用戶態(tài)的執(zhí)行時(shí)間,不包括 nice值為負(fù)進(jìn)程 */uint32_t nice; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻。nice值為負(fù)的進(jìn)程所占用的CPU時(shí)間 */uint32_t system; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻,處于核心態(tài)的執(zhí)行時(shí)間 */uint32_t idle; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻,除IO等待時(shí)間以外的其他等待時(shí)間 */uint32_t iowait; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻。IO等待時(shí)間(2.5.41) */uint32_t irq; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻,硬中斷時(shí)間(2.6.0) */uint32_t softirq; /** 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時(shí)刻,軟中斷時(shí)間(2.6.0) *///uint32_t stealstolen; /** which is the time spent in other operating systems when running in a virtualized 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; /*** * 當(dāng)前系統(tǒng)內(nèi)存信息 */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; /*** * 內(nèi)核版本 */typedef struct{ int16_t major; /** 主版本 */int16_t minor; /** 次版本(假設(shè)次版本是偶數(shù)。那么內(nèi)核是穩(wěn)定版。若是奇數(shù)則是開發(fā)版) */int16_t revision; /** 修訂版本 */}kernel_version_t; /*** * 當(dāng)時(shí)進(jìn)程狀態(tài)信息 * * 進(jìn)程的狀態(tài)值: 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 (should never be seen) Z Defunct ("zombie") process, terminated but not reaped by its parent. */typedef struct{ /** 01 */ pid_t pid; /** 進(jìn)程號。其同意的最大值,請查看/proc/sys/kernel/pid_max *//** 02 */ char comm[FILENAME_MAX]; /** 進(jìn)程的名字,不包括路徑 *//** 03 */ char state; /** 進(jìn)程的狀態(tài) *//** 04 */ pid_t ppid; /** 父進(jìn)程號 *//** 05 */ pid_t pgrp; /** 進(jìn)程組號 *//** 06 */ pid_t session; /** 進(jìn)程會話號 *//** 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 fifteen. 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 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; /*** * 網(wǎng)卡流量數(shù)據(jù)結(jié)構(gòu) */typedef struct{ /** 01 */ char interface_name[INTERFACE_NAME_MAX]; /** 網(wǎng)卡名。如eth0 *//** 接收數(shù)據(jù) *//** 02 */ unsigned long receive_bytes; /** 此網(wǎng)卡接收到的字節(jié)數(shù) *//** 03 */ unsigned long receive_packets; /** 04 */ unsigned long receive_errors; /** 05 */ unsigned long receive_dropped; /** 06 */ unsigned long receive_fifo_errors; /** 07 */ unsigned long receive_frame; /** 08 */ unsigned long receive_compressed; /** 09 */ unsigned long receive_multicast; /** 發(fā)送數(shù)據(jù) *//** 10 */ unsigned long transmit_bytes; /** 此網(wǎng)卡已發(fā)送的字節(jié)數(shù) *//** 11 */ unsigned long transmit_packets; /** 12 */ unsigned long transmit_errors; /** 13 */ unsigned long transmit_dropped; /** 14 */ unsigned long transmit_fifo_errors; /** 15 */ unsigned long transmit_collisions; /** 16 */ unsigned long transmit_carrier; /** 17 */ unsigned long transmit_compressed; }net_info_t; /*** * 進(jìn)程頁信息結(jié)構(gòu) */typedef struct{ long size; /** 程序大小 */long resident; /** 常駐內(nèi)存空間大小 */long share; /** 共享內(nèi)存頁數(shù) */long text; /** 代碼段占用內(nèi)存頁數(shù) */long lib; /** 數(shù)據(jù)/堆棧段占用內(nèi)存頁數(shù) */long data; /** 引用庫占用內(nèi)存頁數(shù) */}process_page_info_t; public: /** 獲取系統(tǒng)信息。詳細(xì)請參考sys_info_t的描寫敘述 */static bool get_sys_info(sys_info_t& sys_info); /** 獲取內(nèi)存信息,詳細(xì)請參考mem_info_t的描寫敘述 */static bool get_mem_info(mem_info_t& mem_info); /** 獲取總CPU信息。詳細(xì)請參考cpu_info_t的描寫敘述 */static bool get_cpu_info(cpu_info_t& cpu_info); /** 獲取全部CPU信息,詳細(xì)請參考cpu_info_t的描寫敘述 */
static int get_cpu_info_array(std::vector<cpu_info_t>& cpu_info_array); /** 得到內(nèi)核版本 */static bool get_kernel_version(kernel_version_t& kernel_version); /** 獲取進(jìn)程信息,詳細(xì)請參考process_info_t的描寫敘述 */static bool get_process_info(process_info_t& process_info); /** 獲取進(jìn)程頁信息。詳細(xì)請參考process_page_info_t的描寫敘述 */static bool get_process_page_info(process_page_info_t& process_page_info); /** 獲取進(jìn)程執(zhí)行時(shí)間數(shù)據(jù),詳細(xì)請參考process_time_t的描寫敘述 */static bool get_process_times(process_time_t& process_time); /*** * 獲取網(wǎng)卡流量等信息 * 流量 = (當(dāng)前獲取的值 - 上一時(shí)間獲取的值) / 兩次間隔的時(shí)長 * @interface_name: 網(wǎng)卡名,如eth0等 * @net_info: 存儲網(wǎng)卡流量等數(shù)據(jù) */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
轉(zhuǎn)載于:https://www.cnblogs.com/zfyouxi/p/5333232.html
總結(jié)
以上是生活随笔為你收集整理的Linux下用来获取各种系统信息的C++类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】关于TP3.2 验证码不显示的问题
- 下一篇: codevs 1183 泥泞的道路 二分