日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

编程获取linuxservercpu、内存和磁盘使用

發(fā)布時間:2024/1/17 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程获取linuxservercpu、内存和磁盘使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

proc文件系統(tǒng)簡介

/proc文件系統(tǒng)是一個偽文件系統(tǒng)。它是唯一的,其中存儲器,如果不采取外部存儲空間。

它是文件系統(tǒng)提供了與內(nèi)核進(jìn)程進(jìn)行通信的接口的方法。用程序能夠通過/proc得到系統(tǒng)的信息。并能夠改變內(nèi)核的某些參數(shù)。

因?yàn)橄到y(tǒng)的信息。如進(jìn)程。是動態(tài)改變的,所以用戶或應(yīng)用程序讀取/proc文件夾中的文件時。proc文件系統(tǒng)是動態(tài)從系統(tǒng)內(nèi)核讀出所需信息并提交的。


/proc文件夾中有一些以數(shù)字命名的文件夾。它們是進(jìn)程文件夾。系統(tǒng)中當(dāng)前執(zhí)行的每個進(jìn)程在/proc下都相應(yīng)一個以進(jìn)程號為文件夾名的文件夾/proc/pid,它們是讀取進(jìn)程信息的接口。

此外,在Linux 2.6.0-test6以上的版本號中/proc/pid文件夾中有一個task文件夾,/proc/pid/task文件夾中也有一些以該進(jìn)程所擁有的線程的線程號命名的文件夾/proc/pid/task/tid,它們是讀取線程信息的接口。


CPU使用率

要想計(jì)算CPU使用率,首先要了解文件/proc/stat中的內(nèi)容,例如以下是本人所使用server中該文件里的內(nèi)容:


CPU 以及CPU0、CPU1、CPU2、CPU3、CPU4中每行的每一個參數(shù)意思(以第一行為例)解釋:
user (28201) :從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻。用戶態(tài)的CPU時間(單位:jiffies) ,不包括 nice值為負(fù)進(jìn)程。1jiffies=0.01秒
nice (389) :從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻,nice值為負(fù)的進(jìn)程所占用的CPU時間(單位:jiffies)
system (10975) :從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻,核心時間(單位:jiffies)
idle (6552431) :從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻,除硬盤IO等待時間以外其他等待時間(單位:jiffies)
iowait (19704) :從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻,硬盤IO等待時間(單位:jiffies) 。
irq (0) :從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻。硬中斷時間(單位:jiffies)
softirq (208): 從系統(tǒng)啟動開始累計(jì)到當(dāng)前時刻。軟中斷時間(單位:jiffies)

獲取cpu使用率的方法:

1、記錄某個時刻cpu的使用情況

2、等上一個時間段

3、再記錄此刻的cpu使用情況

4、計(jì)算總的時間片

把第一次的全部cpu使用情況求和。得到j(luò)1,把第二次的全部cpu使用情況求和,得到j(luò)2。則j2-j1得到這個時間段的全部時間片。即total=j2-j1=第二次的全部列的和-第一次的全部列的和

5、計(jì)算idle時間

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)的一個庫函數(shù)sysinfo(),該函數(shù)返回例如以下的一個結(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)存大小。所以通過這兩個值就能夠計(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; }磁盤利用率

本來打算通過讀文件/proc/partitions來獲取磁盤分區(qū)的使用情況,只是這樣僅僅能夠獲取分區(qū)的大小。使用情況是無法獲取的。只是能夠通過讀取文件/etc/mtab來讀取系統(tǒng)中全部文件系統(tǒng)的信息。然后統(tǒng)計(jì)全部文件系統(tǒng)占用的磁盤總大小和能夠磁盤的總大小,這樣就能夠計(jì)算出系統(tǒng)中文件系統(tǒng)的總磁盤利用率。

代碼實(shí)現(xiàn)例如以下所看到的:

SysDiskInfo * GetHostDiskInfo() {SysDiskInfo *sys_disk_info = (SysDiskInfo *)malloc(sizeof(SysDiskInfo));DiskInfo *disk_info;struct statfs fs_info;struct mntent *mnt_info;FILE *fh;float percent;unsigned long sum_Total = 0, total_free = 0;if ((fh = setmntent("/etc/mtab", "r")) == NULL) {printf("Cannot open /etc/mtab file!:%s\n",strerror(errno));return NULL;}while ((mnt_info = getmntent(fh)) != NULL) {if (statfs(mnt_info->mnt_dir, &fs_info) < 0) {continue;}if ((disk_info = (DiskInfo *)malloc(sizeof(DiskInfo))) == NULL) {continue;}if (strcmp(mnt_info->mnt_type, "proc") &&strcmp(mnt_info->mnt_type, "devfs") &&strcmp(mnt_info->mnt_type, "usbfs") &&strcmp(mnt_info->mnt_type, "sysfs") &&strcmp(mnt_info->mnt_type, "tmpfs") &&strcmp(mnt_info->mnt_type, "devpts") &&strcmp(mnt_info->mnt_type, "fusectl") &&strcmp(mnt_info->mnt_type, "debugfs") &&strcmp(mnt_info->mnt_type, "binfmt_misc") &&strcmp(mnt_info->mnt_type, "fuse.gvfs_fuse_daemon")&&strcmp(mnt_info->mnt_type, "securityfs")) {if (fs_info.f_blocks != 0) {percent = ((float)fs_info.f_blocks - (float)fs_info.f_bfree * 100.0/(float)fs_info.f_blocks);} else {percent = 0;}} else {continue;}strcpy(disk_info->type, mnt_info->mnt_type);strcpy(disk_info->device, mnt_info->mnt_fsname);strcpy(disk_info->mntpnt, mnt_info->mnt_dir);unsigned long block_size = fs_info.f_bsize/1024;disk_info->blocks = fs_info.f_blocks * block_size / 1024;disk_info->bfree = fs_info.f_bfree * block_size / 1024;disk_info->availiable_disk = fs_info.f_bavail * block_size / 1024;disk_info->bused = (fs_info.f_blocks - fs_info.f_bfree) * block_size / 1024;disk_info->bused_percent = percent;sum_Total += disk_info->blocks;total_free += disk_info->availiable_disk;/* clean auxiliary memory */free(disk_info);disk_info = NULL;}sys_disk_info->Disk_Total = sum_Total/1024;sys_disk_info->Disk_Free = total_free/1024;return sys_disk_info; }




版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的编程获取linuxservercpu、内存和磁盘使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。