linux+平均磁盘请求数量_《Linux操作系统内核实习》之练习一

#include #define STANDARD 0

#define SHORT 1

#define LONG 2

#define LB_SIZE 300

typedef struct{

int processor;    //cpu number

char vendor_id[50];

int cpu_family;

int model;

char model_name[50];

int stepping;

float cpu_MHz;

int cache_size;

char fdiv_bug;

char hlt_bug;

char f00f_bug;

char fpu;

char fpu_exception;

int cpuid_level;

char wp;

char flags[100];

float bogomips;

int clflush_size;

}CPUINFO;

typedef struct{

time_t cpu_usr;    //user model

time_t cpu_nice;    //user model with low priority

time_t  cpu_sys;    //system model

time_t  cpu_idle;    //idle model

long intrTotal;    //total counts of interrupts serviced since boot time

long ctxt;    //context switches

time_t btime;    //boot time, in seconds since the Epoch (January 1,

//1970

long procs_n;    //Number of forks since boot

} STAT;

void repShort(void);

void repLong(int interval, int duration);

char* findstr(char *buf, const char *str, char c);

int main (int argc, char **argv)

{

CPUINFO cpuinfo;

STAT stat;

char kversion[100];    //linux kernel version

int totalMem, freeMem;

time_t timeCur;

time_t runTimeCal;

FILE *fpstat, *fpcpuinfo, *fpversion, *fpmeminfo, *fpdiskstats;

char buf[100];

int interval;

int duration;

char repTypeName[16];

int repType = STANDARD;    /* default is standard */

strcpy(repTypeName, "Standard");

/* Get option and arg */

char oc;

while ((oc = getopt(argc, argv, "sl:")) != -1) {

switch (oc) {

case 's':

repType = SHORT;

strcpy(repTypeName, "Short");

break;

case 'l':

repType = LONG;

strcpy(repTypeName, "Long");

interval = atoi(optarg);

if (argv[optind] == NULL) {

fprintf(stderr, "%s: option `-l` must have two argument!\n", argv[0]);

exit(-1);

}

duration = atoi(argv[optind++]);

break;

default:

return 0;

} /* end switch */

}/* end while */

/* get time now */

struct timeval now;

gettimeofday(&now, NULL);

printf("Status report type %s at %s\n",

repTypeName, ctime(&(now.tv_sec)));

FILE *thisProcFile = fopen("/proc/sys/kernel/hostname", "r");

fgets(buf, 99, thisProcFile);

printf("Machine hostname: %s\n", buf);

fclose(thisProcFile);

if ((fpcpuinfo = fopen("/proc/cpuinfo", "r")) == NULL) {

perror("Can not open the file \"/proc/cpuinfo\" !");

exit(-1);

}

int i;

char c;

char *p;    //temp pointer

//cpuinfo

while( !feof(fpcpuinfo) ){

if( fgets(buf, 99, fpcpuinfo) == NULL) {

perror("read file /proc/cpuinfo failed !\n");

exit(-1);

}

if(strstr(buf, "model") != NULL){    //finded "model"

p = strrchr(buf, ':');

p++;

cpuinfo.model = atoi(p);

}

if(strstr(buf, "model name") != NULL){    //finded "model name"

p = strrchr(buf, ':');

p++;

strcpy(cpuinfo.model_name, p);

break;

}

}

printf("cpu's model : %d\n", cpuinfo.model);

printf("cpu's model_name : %s\n", cpuinfo.model_name);

fclose(fpcpuinfo);

//version

if( (freopen("/proc/version", "r", stdin)) == NULL ) {

perror("Can not open /proc/version !\n");

exit(-1);

}

gets(buf);

printf("linux version : %s\n", buf);

fclose(stdin);

//system run time

if( (fpstat = fopen("/proc/stat", "r")) == NULL) {

perror("Can not open /proc/version !\n");

exit(-1);

}

while(!feof(fpstat)) {

fgets(buf, 99, fpstat);

//printf("%s\n", buf);

if( strstr(buf, "btime") != NULL ) {

sscanf(buf, "%*s%s", buf);

//p = strchr(buf, ' ');

stat.btime = atol(buf);

break;

}

}

timeCur = time(NULL);    /*current time*/

runTimeCal = timeCur - stat.btime;

/* printf("timeCur = %ld, btime = %ld \n", timeCur, stat.btime); */

int day;

int h;

int m;

int s;

day = runTimeCal/(24*60*60);

runTimeCal %= 24*60*60;

h = runTimeCal/(60*60);

runTimeCal %= 60*60;

m = runTimeCal/60;

s = runTimeCal%60;

printf("System run time is %d:%d:%d:%d\n", day, h, m, s);

fclose(fpstat);

/* Short mode */

if (repType == SHORT)

repShort();

/* Long mode */

if (repType == LONG)

repLong(interval, duration);

return 0;

}

/* find string and get string after the first  ':' */

char* findstr(char *buf, const char *str, char c)

{

char *result;

char *pstr;

if((pstr = strstr(buf, str)) != NULL){    //finded str

result = strchr(pstr, c);

result++;

return result;

}

return NULL;

}

void repShort(void)

{

FILE *thisProcFile;

time_t cpuUser, cpuSys, cpuIdle;

char lineBuf[LB_SIZE];

char *p;

if ((thisProcFile = fopen("/proc/stat", "r")) == NULL) {

fprintf(stderr, "Can not open /proc/stat!\n");

exit(-1);

}

/* cpu time */

fscanf(thisProcFile, "%*s%s", lineBuf);

cpuUser = atol(lineBuf);

fscanf(thisProcFile, "%*s%s", lineBuf);

cpuSys = atol(lineBuf);

fscanf(thisProcFile, "%s", lineBuf);

cpuIdle = atol(lineBuf);

printf("Cpu time is %lds in user, %lds in system, %lds in idle.\n",

cpuUser, cpuSys, cpuIdle);

/* cpu context */

while (!feof(thisProcFile)) {

fgets(lineBuf, LB_SIZE-1, thisProcFile);

if ((p=findstr(lineBuf, "ctxt", ' ')) != NULL) {

printf("cpu change context is %s\n", p);

}

if ((p=findstr(lineBuf, "btime", ' ')) != 0) {

time_t bootTime = atol(p);

printf("system boot time is %s\n", ctime(&bootTime));

}

if ((p=findstr(lineBuf, "processes", ' ')) != 0) {

printf("processes number is %d\n", atoi(p));

break;

}

} /* end while */

fclose(thisProcFile);

/* disk require */

if ((thisProcFile = fopen("/proc/diskstats", "r")) == NULL) {

fprintf(stderr, "Can not read /prc/diskstats");

exit(-1);

}

while (!feof(thisProcFile)) {

fgets(lineBuf, LB_SIZE-1, thisProcFile);

if ((p=findstr(lineBuf, "sda", ' ')) != 0) {

long rt, wt;

sscanf(p, "%ld", &rt);

sscanf(p, "%*ld%*ld%*ld%*ld%ld", &wt);

printf("rt = %ld, wt = %ld\n", rt, wt);

printf("disk read time plus write time is %ld\n", (rt+wt));

break;

}

} /* end while */

fclose(thisProcFile);

}

void repLong(int interval, int duration)

{

FILE *thisProcFile;

char lineBuf[LB_SIZE];

char *p;

if ((thisProcFile = fopen("/proc/meminfo", "r")) == NULL) {

fprintf(stderr, "Can not open /proc/meminfo\n");

exit(-1);

}

int i;

for (i = 1; i <= 2; i++) {

fgets(lineBuf, LB_SIZE-1, thisProcFile);

printf("%s\n", lineBuf);

} /* end for */

fclose(thisProcFile);

/* load average */

/* 求高手指教了,我现在还想不到什么办法求这个平均负载:(

* 这个是要求在程序开始后,600秒后每隔10秒计算一次平均负载

* 等我想到办法再来更新吧!呵呵 。。。

* printf("--------------------load average-------------------------\n");

* if ((thisProcFile = fopen("/proc/

*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值