Linux proc文件分析以及系统信息获取

Proc文件

        proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取proc文件时,proc文件系统是动态从系统内核读出所需信息并提交的。它的目录结构如下表

 

目录名称

目录内容

/proc/apm            

高级电源管理信息

/proc/cmdline        

内核命令行

/proc/cpuinfo        

关于Cpu信息

/proc/devices        

可以用到的设备(块设备/字符设备)

/proc/filesystems    

支持的文件系统

/proc/dma

使用的DMA通道

/proc/interrupts     

中断的使用

/proc/ioports

I/O端口的使用

/proc/kcore

内核核心印象

/proc/kmsg

内核消息

/proc/loadavg

负载均衡

/proc/locks

内核锁

/proc/meminfo

内存信息

/proc/misc

杂项

/proc/modules

加载模块列表

/proc/mounts

加载的文件系统

/proc/partitions

系统识别的分区表

/proc/rtc

实时时钟

/proc/slabinfo

Slab池信息

/proc/stat

全面统计状态表

/proc/swaps

对换空间的利用情况

/proc/version

内核版本

/proc/uptime

系统正常运行时间

 

         CPU使用率文件信息保存在/proc/stat文件里,某时刻其内容如下:

cpu 432661 13295 86656 422145968 171474 233 5346

cpu0 123075 2462 23494 105543694 16586 0 4615

cpu1 111917 4124 23858 105503820 69697 123 371

cpu2 103164 3554 21530 105521167 64032 106 334

cpu3 94504 3153 17772 105577285 21158 4 24

intr 1065711094 1057275779 92 0 6 6 0 4 0 3527 0 0 070 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7376958 0 0 0 0 0 00 1054602 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

ctxt 19067887

btime 1139187531

processes 270014

procs_running 1

procs_blocked 0

       各标识符的说明见下表

 

参数(值)

参数说明(时间单位:jiffies,1jiffies=0.01秒)

user (432661)

从系统启动开始累计到当前时刻,用户态的CPU时间,不包含 nice值为负进程。

nice (13295)

从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间

system (86656)

从系统启动开始累计到当前时刻,核心时间

idle (422145968)

从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间

iowait (171474)

从系统启动开始累计到当前时刻,硬盘IO等待时间

irq (233)

从系统启动开始累计到当前时刻,硬中断时间

softirq (5346)

从系统启动开始累计到当前时刻,软中断时间

intr

这行给出中断的信息,第一个为自系统启动以来,发生的所有的中断的次数;然后每个数对应一个特定的中断自系统启动以来所发生的次数

ctxt

给出了自系统启动以来CPU发生的上下文交换的次数

btime

给出了从系统启动到现在为止的时间,单位为秒

processes  (total_forks)

自系统启动以来所创建的任务的个数目

procs_running

当前运行队列的任务的数目

procs_blocked

当前被阻塞的任务的数目

 

    获取信息值的函数源码(不够好):

int get_info(char* file, char* buf, char* start, char* end)
{
    FILE *f = NULL;
    int n;
    char buf3[512];
    char* char1 = NULL;
    char* char2 = NULL;
    char* char3 = NULL;
    char* char4 = NULL;

    for(n = 0; n < 512; ++n) { buf[n] = 0; }
    if( (start == NULL) && (end == NULL) )  {
		f = fopen(file,"r");
        fgets(buf, 512, f);
        fclose(f);
		return 1;
    }
    if(file && start && end)
    {
		f = fopen(file,"r");
		fgets(buf, 512, f);
		while( (char1 = strstr(buf,start)) == NULL ){
	    	char4 = fgets(buf, 512, f);
	    	if( !char4 ){ 
				break; 
			}
		}
		if( !char1 ){
	    	fclose(f);
	    	return 0;	
		}
		char1 += strlen(start);
		char2 = strstr(buf, end);  
		for(char3 = char1; char3 < char2; ++char3){
	  		 buf3[char3-char1] = *char3;
		}
		buf3[char3-char1] = '\0';
		strcpy(buf, buf3);
		fclose(f);
    }
    
    if( (file==NULL) && start && end)
    {
		char1 = strstr(buf, start);
		if( !char1) { 
			return 0; 
		}
		char1 += strlen(start);
		char2 = strstr(buf, end);
		for(char3 = char1; char3 < char2; ++char3){
	   	 	buf3[char3-char1] = *char3;
		}	
		buf3[char3-char1] = '\0';
		strcpy(buf, buf3);
    }
    return 1;
}


 

 

获取文件系统信息用到的两个系统调用结构体

       获取文件系统信息时,不知道在哪个文件可以直接读取信息,所以使用了两个系统调用结构体,如下:

struct statfs {

     long   f_type;     /* type of file system(see below) */

     long   f_bsize;    /* optimal transferblock size */

     long   f_blocks;   /* total data blocksin file system */

     long   f_bfree;    /* free blocks in fs*/

     long   f_bavail;   /* free blocks availto non-superuser */

     long   f_files;    /* total file nodes infile system */

     long   f_ffree;    /* free file nodes infs */

     fsid_t f_fsid;     /* file system id */

     long   f_namelen;  /* maximum length offilenames */

};

 

struct mntent {

     char *mnt_fsname;   /* name of mounted file system */

     char*mnt_dir;      /* file system path prefix*/

     char *mnt_type;     /* mount type (see mntent.h) */

     char *mnt_opts;     /* mount options (see mntent.h) */

     int  mnt_freq;     /* dump frequency indays */

     int  mnt_passno;   /* pass number onparallel fsck */

};

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值