编程获取linuxservercpu、内存和磁盘使用
proc文件系統(tǒng)簡(jiǎn)介
/proc文件系統(tǒng)是一個(gè)偽文件系統(tǒng)。它是唯一的,其中存儲(chǔ)器,如果不采取外部存儲(chǔ)空間。
它是文件系統(tǒng)提供了與內(nèi)核進(jìn)程進(jìn)行通信的接口的方法。用程序能夠通過(guò)/proc得到系統(tǒng)的信息。并能夠改變內(nèi)核的某些參數(shù)。
因?yàn)橄到y(tǒng)的信息。如進(jìn)程。是動(dòng)態(tài)改變的,所以用戶或應(yīng)用程序讀取/proc文件夾中的文件時(shí)。proc文件系統(tǒng)是動(dòng)態(tài)從系統(tǒng)內(nèi)核讀出所需信息并提交的。
/proc文件夾中有一些以數(shù)字命名的文件夾。它們是進(jìn)程文件夾。系統(tǒng)中當(dāng)前執(zhí)行的每個(gè)進(jìn)程在/proc下都相應(yīng)一個(gè)以進(jìn)程號(hào)為文件夾名的文件夾/proc/pid,它們是讀取進(jìn)程信息的接口。
此外,在Linux 2.6.0-test6以上的版本號(hào)中/proc/pid文件夾中有一個(gè)task文件夾,/proc/pid/task文件夾中也有一些以該進(jìn)程所擁有的線程的線程號(hào)命名的文件夾/proc/pid/task/tid,它們是讀取線程信息的接口。
CPU使用率
要想計(jì)算CPU使用率,首先要了解文件/proc/stat中的內(nèi)容,例如以下是本人所使用server中該文件里的內(nèi)容:
CPU 以及CPU0、CPU1、CPU2、CPU3、CPU4中每行的每一個(gè)參數(shù)意思(以第一行為例)解釋:
user (28201) :從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻。用戶態(tài)的CPU時(shí)間(單位:jiffies) ,不包括 nice值為負(fù)進(jìn)程。1jiffies=0.01秒
nice (389) :從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻,nice值為負(fù)的進(jìn)程所占用的CPU時(shí)間(單位:jiffies)
system (10975) :從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻,核心時(shí)間(單位:jiffies)
idle (6552431) :從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻,除硬盤IO等待時(shí)間以外其他等待時(shí)間(單位:jiffies)
iowait (19704) :從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻,硬盤IO等待時(shí)間(單位:jiffies) 。
irq (0) :從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻。硬中斷時(shí)間(單位:jiffies)
softirq (208): 從系統(tǒng)啟動(dòng)開(kāi)始累計(jì)到當(dāng)前時(shí)刻。軟中斷時(shí)間(單位:jiffies)
獲取cpu使用率的方法:
1、記錄某個(gè)時(shí)刻cpu的使用情況
2、等上一個(gè)時(shí)間段
3、再記錄此刻的cpu使用情況
4、計(jì)算總的時(shí)間片
把第一次的全部cpu使用情況求和。得到j(luò)1,把第二次的全部cpu使用情況求和,得到j(luò)2。則j2-j1得到這個(gè)時(shí)間段的全部時(shí)間片。即total=j2-j1=第二次的全部列的和-第一次的全部列的和
5、計(jì)算idle時(shí)間
idle相應(yīng)第五列的數(shù)據(jù),用第二次的減去第一次的就可以。idle=第二次的第五列-第一次的第五列
6、計(jì)算cpu使用率
ate=(total-idle)/total
在代碼里實(shí)現(xiàn)例如以下所看到的:
SysCPUInfo* _GetHostCPUInfo() {SysCPUInfo *cpuinfo = (SysCPUInfo *)malloc(sizeof(SysCPUInfo));if (cpuinfo == NULL)err_dump("_GetCPUInfo: malloc struct SysCPUInfo error");FILE *fd;char buff[256];memset(buff, '\0', 256);fd = fopen("/proc/stat", "r");fgets(buff, sizeof(buff), fd);sscanf(buff, "%s %lu %lu %lu %lu", cpuinfo->name, &cpuinfo->user, &cpuinfo->nic, &cpuinfo->system, &cpuinfo->idle);fclose(fd);return cpuinfo; }float _CalculateHostCPURate(SysCPUInfo *first, SysCPUInfo *second) {unsigned long old_CPU_Time, new_CPU_Time;unsigned long usr_Time_Diff, sys_Time_Diff, nic_Time_Diff;float cpu_use = 0.0;old_CPU_Time = (unsigned long)(first->user + first->nic + first->system + first->idle);new_CPU_Time = (unsigned long)(second->user + second->nic + second->system + second->idle);usr_Time_Diff = (unsigned long)(second->user - first->user);sys_Time_Diff = (unsigned long)(second->system - first->system);nic_Time_Diff = (unsigned long)(second->nic -first->nic);if ((new_CPU_Time - old_CPU_Time) != 0)cpu_use = (float)100*(usr_Time_Diff + sys_Time_Diff + nic_Time_Diff)/(new_CPU_Time - old_CPU_Time);elsecpu_use = 0.0;return cpu_use; }float GetHostCPURate() {float cpu_rate;SysCPUInfo *first, *second;first = _GetHostCPUInfo();sleep(10);second = _GetHostCPUInfo();cpu_rate = _CalculateHostCPURate(first, second);/* clean auxiliary memory */free(first);free(second);first = second = NULL;return cpu_rate; } 內(nèi)存使用率內(nèi)存使用率的計(jì)算比較方便。能夠直接調(diào)用Linux系統(tǒng)的一個(gè)庫(kù)函數(shù)sysinfo(),該函數(shù)返回例如以下的一個(gè)結(jié)構(gòu)體:
struct sysinfo {long uptime; /* Seconds since boot */unsigned long loads[3]; /* 1, 5, and 15 minute load averages */unsigned long totalram; /* Total usable main memory size */unsigned long freeram; /* Available memory size */unsigned long sharedram; /* Amount of shared memory */unsigned long bufferram; /* Memory used by buffers */unsigned long totalswap; /* Total swap space size */unsigned long freeswap; /* swap space still available */unsigned short procs; /* Number of current processes */unsigned long totalhigh; /* Total high memory size */unsigned long freehigh; /* Available high memory size */unsigned int mem_unit; /* Memory unit size in bytes */char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */}; 該結(jié)構(gòu)體中freeram表示可用內(nèi)存的大小,totalram表示總內(nèi)存大小。所以通過(guò)這兩個(gè)值就能夠計(jì)算內(nèi)存使用率了。代碼實(shí)現(xiàn)例如以下所看到的: SysMemInfo * GetHostMemInfo() {SysMemInfo *memInfo = (SysMemInfo *)malloc(sizeof(SysMemInfo));if (NULL == memInfo)err_dump("GetMemInfo: malloc SysMemInfo Struct error");struct sysinfo tmp;int ret = 0;ret = sysinfo(&tmp);if (ret == 0) {memInfo->MemFree = (unsigned long)tmp.freeram/(1024*1024);memInfo->MemTotal = (unsigned long)tmp.totalram/(1024*1024);} else {err_dump("GetMemInfo: sysinfo() error");}return memInfo; }磁盤利用率本來(lái)打算通過(guò)讀文件/proc/partitions來(lái)獲取磁盤分區(qū)的使用情況,只是這樣僅僅能夠獲取分區(qū)的大小。使用情況是無(wú)法獲取的。只是能夠通過(guò)讀取文件/etc/mtab來(lái)讀取系統(tǒng)中全部文件系統(tǒng)的信息。然后統(tǒng)計(jì)全部文件系統(tǒng)占用的磁盤總大小和能夠磁盤的總大小,這樣就能夠計(jì)算出系統(tǒng)中文件系統(tǒng)的總磁盤利用率。
代碼實(shí)現(xiàn)例如以下所看到的:
版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。
總結(jié)
以上是生活随笔為你收集整理的编程获取linuxservercpu、内存和磁盘使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: nodejs mongoose建模实践
- 下一篇: Linux下的字符集问题