1. #include <vector>
  2. #include "sys/config.h"
  3. SYS_NAMESPACE_BEGIN
  4.  
  5. /***
  6. * 用来获取系统、内核和进程的各类实时信息,如CPU和内存数据
  7. */
  8. class CInfo
  9. {
  10. public:
  11. /***
  12. * 系统当前实时信息
  13. */
  14. typedef struct
  15. {
  16. long uptime_second; /* Seconds since boot */
  17. unsigned long average_load[3]; /* 1, 5, and 15 minute load averages */
  18. unsigned long ram_total; /* Total usable main memory size */
  19. unsigned long ram_free; /* Available memory size */
  20. unsigned long ram_shared; /* Amount of shared memory */
  21. unsigned long ram_buffer; /* Memory used by buffers */
  22. unsigned long swap_total; /* Total swap space size */
  23. unsigned long swap_free; /* swap space still available */
  24. unsigned short process_number; /* Number of current processes */
  25. }sys_info_t;
  26.  
  27. /***
  28. * 当前进程时间信息
  29. */
  30. typedef struct
  31. {
  32. long user_time; /* user time */
  33. long system_time; /* system time */
  34. long user_time_children; /* user time of children */
  35. long system_time_children; /* system time of children */
  36. }process_time_t;
  37.  
  38. /***
  39. * 当前系统CPU信息
  40. */
  41. typedef struct
  42. {
  43. // 单位: jiffies, 1jiffies=0.01秒
  44. uint64_t total;
  45. uint32_t user; /** 从系统启动開始累计到当前时刻。处于用户态的执行时间,不包括 nice值为负进程 */
  46. uint32_t nice; /** 从系统启动開始累计到当前时刻。nice值为负的进程所占用的CPU时间 */
  47. uint32_t system; /** 从系统启动開始累计到当前时刻,处于核心态的执行时间 */
  48. uint32_t idle; /** 从系统启动開始累计到当前时刻,除IO等待时间以外的其他等待时间 */
  49. uint32_t iowait; /** 从系统启动開始累计到当前时刻。IO等待时间(2.5.41) */
  50. uint32_t irq; /** 从系统启动開始累计到当前时刻,硬中断时间(2.6.0) */
  51. uint32_t softirq; /** 从系统启动開始累计到当前时刻,软中断时间(2.6.0) */
  52. //uint32_t stealstolen; /** which is the time spent in other operating systems when running in a virtualized environment(2.6.11) */
  53. //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) */
  54. }cpu_info_t;
  55.  
  56. /***
  57. * 当前系统内存信息
  58. */
  59. typedef struct
  60. {
  61. uint32_t mem_total;
  62. uint32_t mem_free;
  63. uint32_t buffers;
  64. uint32_t cached;
  65. uint32_t swap_cached;
  66. uint32_t swap_total;
  67. uint32_t swap_free;
  68. }mem_info_t;
  69.  
  70. /***
  71. * 内核版本
  72. */
  73. typedef struct
  74. {
  75. int16_t major; /** 主版本 */
  76. int16_t minor; /** 次版本(假设次版本是偶数。那么内核是稳定版。若是奇数则是开发版) */
  77. int16_t revision; /** 修订版本 */
  78. }kernel_version_t;
  79.  
  80. /***
  81. * 当时进程状态信息
  82. *
  83. * 进程的状态值:
  84. D Uninterruptible sleep (usually IO)
  85. R Running or runnable (on run queue)
  86. S Interruptible sleep (waiting for an event to complete)
  87. T Stopped, either by a job control signal or because it is being traced.
  88. W paging (not valid since the 2.6.xx kernel)
  89. X dead (should never be seen)
  90. Z Defunct ("zombie") process, terminated but not reaped by its parent.
  91. */
  92. typedef struct
  93. {
  94. /** 01 */ pid_t pid; /** 进程号。其同意的最大值,请查看/proc/sys/kernel/pid_max */
  95. /** 02 */ char comm[FILENAME_MAX]; /** 进程的名字,不包括路径 */
  96. /** 03 */ char state; /** 进程的状态 */
  97. /** 04 */ pid_t ppid; /** 父进程号 */
  98. /** 05 */ pid_t pgrp; /** 进程组号 */
  99. /** 06 */ pid_t session; /** 进程会话号 */
  100. /** 07 */ int tty_nr; /** The tty the process uses */
  101. /** 08 */ pid_t tpgid; /** The tty the process uses */
  102. /** 09 */ unsigned int flags; /** The kernel flags word of the process (%lu before Linux 2.6.22) */
  103. /** 10 */ unsigned long minflt; /** The number of minor faults the process has made which have not required loading a memory page from disk */
  104. /** 11 */ unsigned long cminflt; /** The number of minor faults that the process's waited-for children have made */
  105. /** 12 */ unsigned long majflt; /** The number of major faults the process has made which have required loading a memory page from disk */
  106. /** 13 */ unsigned long cmajflt; /** The number of major faults that the process's waited-for children have made */
  107. /** 14 */ unsigned long utime; /** The number of jiffies that this process has been scheduled in user mode */
  108. /** 15 */ unsigned long stime; /** The number of jiffies that this process has been scheduled in kernel mode */
  109. /** 16 */ long cutime; /** The number of jiffies that this process's waited-for children have been scheduled in user mode */
  110. /** 17 */ long cstime; /** The number of jiffies that this process's waited-for children have been scheduled in kernel mode */
  111. /** 18 */ long priority; /** The standard nice value, plus fifteen. The value is never negative in the kernel */
  112. /** 19 */ long nice; /** The nice value ranges from 19 (nicest) to -19 (not nice to others) */
  113. /** 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 */
  114. /** 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 */
  115. /** 22 */ long long starttime; /** The time in jiffies the process started after system boot */
  116. /** 23 */ unsigned long vsize; /** Virtual memory size in bytes */
  117. /** 24 */ long rss; /** Resident Set Size: number of pages the process has in real memory, minus 3 for administrative purposes */
  118. /** 25 */ unsigned long rlim; /** Current limit in bytes on the rss of the process (usually 4294967295 on i386) */
  119. /** 26 */ unsigned long startcode; /** The address above which program text can run */
  120. /** 27 */ unsigned long endcode; /** The address below which program text can run */
  121. /** 28 */ unsigned long startstack; /** The address of the start of the stack */
  122. /** 29 */ unsigned long kstkesp; /** The current value of esp (stack pointer), as found in the kernel stack page for the process */
  123. /** 30 */ unsigned long kstkeip; /** The current EIP (instruction pointer) */
  124. /** 31 */ unsigned long signal; /** The bitmap of pending signals */
  125. /** 32 */ unsigned long blocked; /** The bitmap of blocked signals */
  126. /** 33 */ unsigned long sigignore; /** The bitmap of ignored signals */
  127. /** 34 */ unsigned long sigcatch; /** The bitmap of caught signals */
  128. /** 35 */ unsigned long nswap; /** Number of pages swapped (not maintained). */
  129. /** 36 */ unsigned long cnswap; /** Cumulative nswap for child processes (not maintained) */
  130. /** 37 */ int exit_signal; /** Signal to be sent to parent when we die (since Linux 2.1.22) */
  131. /** 38 */ int processor; /** CPU number last executed on (since Linux 2.2.8) */
  132. }process_info_t;
  133.  
  134. /***
  135. * 网卡流量数据结构
  136. */
  137. typedef struct
  138. {
  139. /** 01 */ char interface_name[INTERFACE_NAME_MAX]; /** 网卡名。如eth0 */
  140.  
  141. /** 接收数据 */
  142. /** 02 */ unsigned long receive_bytes; /** 此网卡接收到的字节数 */
  143. /** 03 */ unsigned long receive_packets;
  144. /** 04 */ unsigned long receive_errors;
  145. /** 05 */ unsigned long receive_dropped;
  146. /** 06 */ unsigned long receive_fifo_errors;
  147. /** 07 */ unsigned long receive_frame;
  148. /** 08 */ unsigned long receive_compressed;
  149. /** 09 */ unsigned long receive_multicast;
  150.  
  151. /** 发送数据 */
  152. /** 10 */ unsigned long transmit_bytes; /** 此网卡已发送的字节数 */
  153. /** 11 */ unsigned long transmit_packets;
  154. /** 12 */ unsigned long transmit_errors;
  155. /** 13 */ unsigned long transmit_dropped;
  156. /** 14 */ unsigned long transmit_fifo_errors;
  157. /** 15 */ unsigned long transmit_collisions;
  158. /** 16 */ unsigned long transmit_carrier;
  159. /** 17 */ unsigned long transmit_compressed;
  160. }net_info_t;
  161.  
  162. /***
  163. * 进程页信息结构
  164. */
  165. typedef struct
  166. {
  167. long size; /** 程序大小 */
  168. long resident; /** 常驻内存空间大小 */
  169. long share; /** 共享内存页数 */
  170. long text; /** 代码段占用内存页数 */
  171. long lib; /** 数据/堆栈段占用内存页数 */
  172. long data; /** 引用库占用内存页数 */
  173. }process_page_info_t;
  174.  
  175. public:
  176. /** 获取系统信息。详细请參考sys_info_t的描写叙述 */
  177. static bool get_sys_info(sys_info_t& sys_info);
  178.  
  179. /** 获取内存信息,详细请參考mem_info_t的描写叙述 */
  180. static bool get_mem_info(mem_info_t& mem_info);
  181.  
  182. /** 获取总CPU信息。详细请參考cpu_info_t的描写叙述 */
  183. static bool get_cpu_info(cpu_info_t& cpu_info);
  184.  
  185. /** 获取全部CPU信息,详细请參考cpu_info_t的描写叙述 */
  186. static int get_cpu_info_array(std::vector<cpu_info_t>& cpu_info_array);
  187.  
  188. /** 得到内核版本 */
  189. static bool get_kernel_version(kernel_version_t& kernel_version);
  190.  
  191. /** 获取进程信息,详细请參考process_info_t的描写叙述 */
  192. static bool get_process_info(process_info_t& process_info);
  193.  
  194. /** 获取进程页信息。详细请參考process_page_info_t的描写叙述 */
  195. static bool get_process_page_info(process_page_info_t& process_page_info);
  196.  
  197. /** 获取进程执行时间数据,详细请參考process_time_t的描写叙述 */
  198. static bool get_process_times(process_time_t& process_time);
  199.  
  200. /***
  201. * 获取网卡流量等信息
  202. * 流量 = (当前获取的值 - 上一时间获取的值) / 两次间隔的时长
  203. * @interface_name: 网卡名,如eth0等
  204. * @net_info: 存储网卡流量等数据
  205. */
  206. static bool get_net_info(const char* interface_name, net_info_t& net_info);
  207. static bool get_net_info_array(std::vector<net_info_t>& net_info_array);
  208.  
  209. private:
  210. static bool do_get_net_info_array(const char* interface_name, std::vector<net_info_t>& net_info_array);
  211. };
  212.  
  213. SYS_NAMESPACE_END

Linux下用来获取各种系统信息的C++类的更多相关文章

  1. Linux下用C获取当前时间

    Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() {  ti ...

  2. linux下dmidecode命令获取硬件信息

    linux下dmidecode命令获取硬件信息 2 A+ 所属分类:Linux 运维工具 dmidecode在 Linux 系统下获取有关硬件方面的信息.dmidecode 遵循 SMBIOS/DMI ...

  3. 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)

    目录 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载) Http协议简述 HttpRequest类设计 请求部分 接收部分 关于上传和下载 Cpp实现 关于源码中的Lo ...

  4. Linux下使用java获取cpu、内存使用率

    原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...

  5. Linux下百度云盘报 获取bdstoken失败

    在用linux下百度云盘工具(bcloud),登录时,报获取bdstoken失败. 在网上搜了一下,解决办法如下. 找到auth.py文件 locate auth.py |grep bcloud 结果 ...

  6. 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

    CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...

  7. 有关Linux下request.getRealPath("/")获取路径的问题

    request.getRealPath("/") 在window获取的是服务器的根目录,结尾包含分隔符, 如E:\apache-tomcat-6.0.29-bak\apache-t ...

  8. Linux下onvif客户端获取ipc摄像头 GetServices:获取媒体地址(有的h265摄像头必须要这个接口)

    GetServices:获取媒体地址(有些h265的摄像头必须用到这个接口,得到获取能力时没获取到的另一个媒体地址) 鉴权:但是在使用这个接口之前是需要鉴权的.ONVIF协议规定,部分接口需要鉴权,部 ...

  9. Linux下onvif客户端获取h265 IPC摄像头的RTSP地址

    1. 设备搜索,去获取webserver 的地址 ,目的是在获取能力提供服务地址,demo:https://www.cnblogs.com/croxd/p/10683429.html 2. GetCa ...

随机推荐

  1. Axure RP Pro 6.5修改站点地图,只显示需要的节点

    1. 原始页面   2. 原始代码(用记事本打开生成原型所在文件夹下的data\sitemap.js) var sitemap = (function() { var _ = function() { ...

  2. 把硬盘格式化成ext格式的cpu占用率就下来了

    把硬盘格式化成ext格式的cpu占用率就下来了我是使用ext4格式 @Paulz 还有这种事情? 现在是什么格式?- - ,你自己用top命令看一下啊就知道什么东西在占用cpu了下载软件一半cpu都用 ...

  3. 【图片处理】cocos2dx png图片压缩处理

    一.介绍 美术用photoshop出图有时候会包含一些无用的信息,这时候image magick可以把这些信息裁掉. 二.使用方法 1.下载并安装Image Magick 2.将脚本里的目录名改成Im ...

  4. ruby 编写迭代器

    class My def initialize(name,age) @name=name @age=age end def sayName puts @name end def sayAge puts ...

  5. C#实现多国语言的界面切换

    在PictureStudio中,我需要实现多国语言的界面切换,而且切换各种语言版本的时候希望程序是动态的加载语言,不希望切换语言后重新启动程序. 实现这样的功能可以有很愚蠢的方法,比如说你可以在程序中 ...

  6. amaze UI的使用

    1.放置在独立的位置 2.引入核心css与js <link href="{sh::PUB}amaze-ui/css/amazeui.min.css" rel="st ...

  7. Wordpress prettyPhoto插件跨站脚本漏洞

    漏洞名称: Wordpress prettyPhoto插件跨站脚本漏洞 CNNVD编号: CNNVD-201311-413 发布时间: 2013-11-28 更新时间: 2013-11-28 危害等级 ...

  8. App性能优化

    http://www.cocoachina.com/ios/20150429/11712.html http://blog.csdn.net/jasonblog/article/details/765 ...

  9. 一步步写STM32 OS【一】 序言

    一直想写个类似uCOS的OS,近段时间考研复习之余忙里偷闲,总算有点成果了.言归正传,我觉得OS最难的部分首先便是上下文切换的问题,他和MCU的架构有关,所以对于不同的MCU,这部分需要移植.一旦这个 ...

  10. NEsper使用的事件类型 z

    NEsper使用的事件类型来描述事件的类型信息.你的应用在启动时可能预先配置定义事件类型,或者在运行时通过API或EPL语法动态的增加事件类型. EPL中的create schema 的语法允许在运行 ...