Get the target computer is running Linux system hardware occupation, wrote a few small program directly after.
Proc file is read to obtain. cpu usage: / proc / stat, memory usage: / proc / meminfo Look at the program: / ************************************************* ************** * @ File: statusinfo.c * * @ Brief: obtained from a Linux system CPU and memory usage * * @ Version 1.0 * ************************************************** ************* / typedef struct PACKED / / define a cpu occupy the structure { char name [20]; / / define an array of type char name name 20 elements unsigned int the user; / / define the user of an unsigned int type unsigned int nice; / / define an unsigned int type nice unsigned int system ;/ / define an unsigned int type system unsigned int idle; / / Define an unsigned int type idle } CPU_OCCUPY; typedef struct PACKED / / define a mem occupy the structure { char name [20]; / / define an array of type char name name 20 elements unsigned long total; char name2 [20]; unsigned long free; } MEM_OCCUPY; get_memoccupy (MEM_OCCUPY * mem) / / untyped get function contains a parameter structure class get pointer O { FILE * fd; int n; char BUFF [256]; MEM_OCCUPY * m; m = mem; fd = fopen ("/ proc / meminfo", "r"); fgets (buff, sizeof (buff), fd); fgets (buff, sizeof (buff), fd); fgets (buff, sizeof (buff), fd); fgets (buff, sizeof (buff), fd); sscanf (buff, "% s% u% s", m-> name, & m-> total, m-> name2); fgets (buff, sizeof (buff), fd); / / read from fd file length buff string and then saved to the starting address of this space buff sscanf (buff, "% s% u", m-> name2, & m-> free, m-> name2); fclose (fd); / / close the file fd } int cal_cpuoccupy (CPU_OCCUPY * o, CPU_OCCUPY * n) { unsigned long od, nd; unsigned long id, sd; int cpu_use = 0; OD = (unsigned long) (o-> user + O-> nice + o-> system + o-> idle) ;/ / first (user + priority + system + idle) time and then assigned to the od nd = (unsigned long) (n-> user + nice + n-> system + n-> idle) ;/ / second (user + priority + system + idle) and then assigned to the od id = (unsigned long) (n-> user - o-> user); / / user the first time and the second time the difference is assigned to id sd = (unsigned long) (n-> system - o-> system) ;/ / system first and the second time the difference is assigned to sd if ((nd-od)! = 0) cpu_use = (int) ((sd + id) * 10000) / (nd-od); / / ((user + system) well-behaved 100) in addition to the (first and second time) is assigned to g_cpu_used else cpu_use = 0; / / Printf ("cpu:% u / n", cpu_use); return cpu_use; } get_cpuoccupy (CPU_OCCUPY * cpust) / / untyped get function contains a parameter structure class get pointer O { FILE * fd; int n; char BUFF [256]; CPU_OCCUPY * cpu_occupy; cpu_occupy = cpust; fd = fopen ("/ proc / stat", "r"); fgets (buff, sizeof (buff), fd); sscanf (buff, "% s% u% u% u% u", cpu_occupy-> name, & cpu_occupy-> user, & cpu_occupy-> nice, & cpu_occupy-> system, & cpu_occupy-> idle); fclose (fd); } int main () { CPU_OCCUPY cpu_stat1; CPU_OCCUPY cpu_stat2; MEM_OCCUPY mem_stat; int cpu; / / Get the memory get_memoccupy ((MEM_OCCUPY *) & mem_stat); / / First get cpu usage get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1); sleep (10); / / Get CPU usage get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat2); / / Calculate the cpu utilization cpu = cal_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1, (CPU_OCCUPY *) & cpu_stat2); return 0; } When we engage in the performance testing of the back-end server CPU utilization monitoring is a commonly used means.Server CPU utilization is high, it indicates that the server is very busy. If the front desk response time is increasing, background CPU utilization is always do not increase, indicating a bottleneck somewhere, the system needs tuning. This is a non-technical people are easy to understand things. Above understanding correct? I personally do not think it is very accurate. This depends on the background of your test process is what type of. If it is a compute-intensive process, the end increasing pressure, it is easy to CPU utilization beat up.But if the I / O network-intensive process, even if the client requests more and more, the server CPU may not be able to go up, this is the process you want to test the natural properties of the decision. More common, large files frequently read and write CPU overhead is much smaller than the overhead of frequently read and write small files. I / O throughput certain small file read and write more frequently, require more CPU to handle I / O interrupt. Linux / Unix, the CPU utilization is divided into user mode, the system state and the idle state, respectively, indicates that the CPU is in user mode execution time, the time of the implementation of the system kernel, and idle system process execution time. CPU utilization is usually said means: CPU execution time of the System Idle Process / CPU total execution time. In the Linux kernel, a global variable: Jiffies. Representative of Jiffies time. Units with different hardware platform. Lane definition a constant HZ, represents the number per second, the minimum interval. Jiffies units 1/HZ. Jiffies units of the Intel platform is 1/100 second, this is the system can distinguish the minimum time interval. Each CPU time slice, Jiffies shall be added. CPU utilization with perform user state + state Jiffies divided by the total Jifffies. In Linux systems, you can use the / proc / stat file to calculate the cpu utilization (detailed explanation may refer to: ). This file contains information about all CPU activity, all the values ??in the file are started from the system start to accrue to thepresent moment.
原文地址:http://www.cprogramdevelop.com/1347078/
|
linux下获取内存和cpu使用情况
最新推荐文章于 2024-08-07 20:50:54 发布
linux c program for CPU usage and memory usage
2009-05-17 23:10