075玩软件园
首页 > 资讯教程 > 软件教程 > 使用教程 > LinuxIO工具iotop的替代品iopp

LinuxIO工具iotop的替代品iopp

作者:075玩 来源:互联网 2023-08-25 19:00:07

系统大全为您提供iotop无疑在linuxIO检测上是个不错的工具,但苦于要求内核版本和Python版本,不少朋友放弃了,我也是。偶然间找到了iopp,用c写的,这个跟iotop是一个作用,nice!给大家分享下安装方法很简单,首先复制下面源代码保存为iopp.c文件#include#include#include#include#include#include#include#include#include#include#definePROC"/proc"#defineGET_VALUE(v)p=strchr(p,':');++p;++p;q=strchr(p,'');length=q-p;if(length>=BUFFERLEN){printf("ERROR-valueislargerthanthebuffer:%d",__LINE__);exit(1);}strncpy(value,p,length);value[length]='';v=atoll(value);#defineBTOKB(b)b>>10#defineBTOMB(b)b>>20#defineBUFFERLEN255#defineCOMMANDLEN1024#defineVALUELEN63#defineNUM_STRINGS8structio_node{intpid;longlongrchar;longlongwchar;longlongsyscr;longlongsyscw;longlongread_bytes;longlongwrite_bytes;longlongcancelled_write_bytes;charcommand[COMMANDLEN+1];structio_node*next;};structio_node*head=NULL;intcommand_flag=0;intidle_flag=0;intmb_flag=0;intkb_flag=0;inthr_flag=0;/*Prototypes*/char*format_b(longlong);structio_node*get_ion(int);structio_node*new_ion(char*);voidupsert_data(structio_node*);char*format_b(longlongamt){staticcharretarray[NUM_STRINGS][16];staticint index=0;registerchar*ret;registerchartag='B';ret=retarray[index];index=(index+1)%NUM_STRINGS;if(amt>=10000){amt=(amt+512)/1024;tag='K';if(amt>=10000){amt=(amt+512)/1024;tag='B';if(amt>=10000){amt=(amt+512)/1024;tag='G';}}}snprintf(ret,sizeof(retarray[index])-1,"%lld%c",amt,tag);return(ret);}intget_cmdline(structio_node*ion){intfd;intlength;charfilename[BUFFERLEN+1];charbuffer[COMMANDLEN+1];char*p;char*q;length=snprintf(filename,BUFFERLEN,"%s/%d/cmdline",PROC,ion->pid);if(length==BUFFERLEN)printf("WARNING-filenamelengthmaybetoobigforbuffer:%d",__LINE__);fd=open(filename,O_RDONLY);if(fd==-1)return1;length=read(fd,buffer,sizeof(buffer)-1);close(fd);buffer[length]='';if(length==0)return2;if(command_flag==0){/**Thecommandisnearthebeginning;wedon'tneedtobeableto*theentirestatfile.*/p=strchr(buffer,'(');++p;q=strchr(p,')');length=q-p;}elsep=buffer;length=lengthcommand,p,length);ion->command[length]='';return0;}structio_node*get_ion(intpid){structio_node*c=head;while(c!=NULL){if(c->pid==pid)break;c=c->next;}returnc;}intget_tcomm(structio_node*ion){intfd;intlength;charfilename[BUFFERLEN+1];charbuffer[BUFFERLEN+1];char*p;char*q;length=snprintf(filename,BUFFERLEN,"%s/%d/stat",PROC,ion->pid);if(length==BUFFERLEN)printf("WARNING-filenamelengthmaybetoobigforbuffer:%d",__LINE__);fd=open(filename,O_RDONLY);if(fd==-1)return1;length=read(fd,buffer,sizeof(buffer)-1);close(fd);/**Thecommandisnearthebeginning;wedon'tneedtobeableto*theentirestatfile.*/p=strchr(buffer,'(');++p;q=strchr(p,')');length=q-p;length=lengthcommand,p,length);ion->command[length]='';return0;}structio_node*insert_ion(structio_node*ion){structio_node*c;structio_node*p;/*Checktheheadofthelistasaspecialcase.*/if(ion->pidpid){ion->next=head;head=ion;returnhead;}c=head->next;p=head;while(c!=NULL){if(ion->pidpid){ion->next=c;p->next=ion;returnhead;}p=c;c=c->next;}/*Appendtotheendofthelist.*/if(c==NULL)p->next=ion;returnhead;}voidget_stats(){DIR*dir=opendir(PROC);structdirent*ent;charfilename[BUFFERLEN+1];charbuffer[BUFFERLEN+1];charvalue[BUFFERLEN+1];/*Displaycolumnheaders.*/if(hr_flag==1)printf("%5s%5s%5s%8s%8s%5s%6s%7s%s","pid","rchar","wchar","syscr","syscw","reads","writes","cwrites","command");elseif(kb_flag==1)printf("%5s%8s%8s%8s%8s%8s%8s%8s%s","pid","rchar","wchar","syscr","syscw","rkb","wkb","cwkb","command");elseif(mb_flag==1)printf("%5s%8s%8s%8s%8s%8s%8s%8s%s","pid","rchar","wchar","syscr","syscw","rmb","wmb","cwmb","command");elseprintf("%5s%8s%8s%8s%8s%8s%8s%8s%s","pid","rchar","wchar","syscr","syscw","rbytes","wbytes","cwbytes","command");/*Loopthroughtheprocesstableanddisplayalineperpid.*/while((ent=readdir(dir))!=NULL){intrc;intfd;intlength;char*p;char*q;structio_node*ion;structio_node*old_ion;longlongrchar;longlongwchar;longlongsyscr;longlongsyscw;longlongread_bytes;longlongwrite_bytes;longlongcancelled_write_bytes;if(!isdigit(ent->d_name[0]))continue;ion=new_ion(ent->d_name);if(command_flag==1)rc=get_cmdline(ion);if(command_flag==0||rc!=0)/*Ifthefullcommandlineisnotaskedfororisempty...*/rc=get_tcomm(ion);if(rc!=0){free(ion);continue;}/*Read'io'file.*/length=snprintf(filename,BUFFERLEN,"%s/%s/io",PROC,ent->d_name);if(length==BUFFERLEN)printf("WARNING-filenamelengthmaybetoobigforbuffer:%d",__LINE__);fd=open(filename,O_RDONLY);if(fd==-1){free(ion);continue;}length=read(fd,buffer,sizeof(buffer)-1);close(fd);buffer[length]='';/*Parsingtheiofiledata.*/p=buffer;GET_VALUE(ion->rchar);GET_VALUE(ion->wchar);GET_VALUE(ion->syscr);GET_VALUE(ion->syscw);GET_VALUE(ion->read_bytes);GET_VALUE(ion->write_bytes);GET_VALUE(ion->cancelled_write_bytes);old_ion=get_ion(ion->pid);/*Displaythepid'siodata.*/if(old_ion!=NULL){rchar=ion->rchar-old_ion->rchar;wchar=ion->wchar-old_ion->wchar;syscr=ion->syscr-old_ion->syscr;syscw=ion->syscw-old_ion->syscw;read_bytes=ion->read_bytes-old_ion->read_bytes;write_bytes=ion->write_bytes-old_ion->write_bytes;cancelled_write_bytes=ion->cancelled_write_bytes-old_ion->cancelled_write_bytes;if(kb_flag==1&&hr_flag==0){rchar=BTOKB(rchar);wchar=BTOKB(wchar);syscr=BTOKB(syscr);syscw=BTOKB(syscw);read_bytes=BTOKB(read_bytes);write_bytes=BTOKB(write_bytes);cancelled_write_bytes=BTOKB(cancelled_write_bytes);}elseif(mb_flag==1&&hr_flag==0){rchar=BTOMB(rchar);wchar=BTOMB(wchar);syscr=BTOMB(syscr);syscw=BTOMB(syscw);read_bytes=BTOMB(read_bytes);write_bytes=BTOMB(write_bytes);cancelled_write_bytes=BTOMB(cancelled_write_bytes);}if(!(idle_flag==1&&rchar==0&&wchar==0&&syscr==0&&syscw==0&&read_bytes==0&&write_bytes==0&&cancelled_write_bytes==0)){if(hr_flag==0)printf("%5d%8lld%8lld%8lld%8lld%8lld%8lld%8lld%s",ion->pid,rchar,wchar,syscr,syscw,read_bytes,write_bytes,cancelled_write_bytes,ion->command);elseprintf("%5d%5s%5s%8lld%8lld%5s%6s%7s%s",ion->pid,format_b(rchar),format_b(wchar),syscr,syscw,format_b(read_bytes),format_b(write_bytes),format_b(cancelled_write_bytes),ion->command);}}elseif(idle_flag!=1)/**Nopreviousdata,show0'sinsteadofcalculatingnegatives*onlyifweareshoringidleprocesses.*/printf("%5d%8d%8d%8d%8d%8d%8d%8d%s",ion->pid,0,0,0,0,0,0,0,ion->command);upsert_data(ion);}closedir(dir);return;}structio_node*new_ion(char*pid){structio_node*ion;ion=(structio_node*)malloc(sizeof(structio_node));bzero(ion,sizeof(structio_node));ion->pid=atoi(pid);returnion;}voidupsert_data(structio_node*ion){structio_node*n;/*Listisempty.*/if(head==NULL){head=ion;return;}/*Checkifwehaveseenthispidbefore.*/n=head;while(n!=NULL){if(n->pid==ion->pid){n->rchar=ion->rchar;n->wchar=ion->wchar;n->syscr=ion->syscr;n->syscw=ion->syscw;n->read_bytes=ion->read_bytes;n->write_bytes=ion->write_bytes;n->cancelled_write_bytes=ion->cancelled_write_bytes;/**Ifthepidswrap,thenthecommandmaybedifferentthenbefore.*/strcpy(n->command,ion->command);free(ion);return;}n=n->next;}/*Addthispidtothelist.*/head=insert_ion(ion);return;}voidusage(){printf("usage:iopp-h|--help");printf("usage:iopp[-ci][-k|-m][delay[count]]");printf("-c,--commanddisplayfullcommandline");printf("-h,--helpdisplayhelp");printf("-i,--idlehidesidleprocesses");printf("-k,--kilobytesdisplaydatainkilobytes");printf("-m,--megabytesdisplaydatainmegabytes");printf("-u,--human-readabledisplaydatainkilo-,mega-,orgiga-bytes");}intmain(intargc,char*argv[]){intc;intdelay=0;intcount=0;intmax_count=1;while(1){intoption_index=0;staticstructoptionlong_options[]={{"command",no_argument,0,'c'},{"help",no_argument,0,'h'},{"human-readable",no_argument,0,'u'},{"idle",no_argument,0,'i'},{"kilobytes",no_argument,0,'k'},{"megabytes",no_argument,0,'m'},{0,0,0,0}};c=getopt_long(argc,argv,"chikmu",long_options,&option_index);if(c==-1){/*Handledelayandcountarguments.*/if(argc==optind)break;/*Noadditionalarguments.*/elseif((argc-optind)==1){delay=atoi(argv[optind]);max_count=-1;}elseif((argc-optind)==2){delay=atoi(argv[optind]);max_count=atoi(argv[optind+1]);}else{/*Toomanyadditionalarguments.*/usage();return3;}break;}switch(c){case'c':command_flag=1;break;case'h':usage();return0;case'i':idle_flag=1;break;case'k':kb_flag=1;break;case'm':mb_flag=1;break;case'u':hr_flag=1;break;default:usage();return2;}}while(max_count==-1||count++ io.log 这个命令就可以把实时的io信息打印出来啦打印出来的各项含义:pid 进程IDrchar 将要从磁盘读取的字节数wchar 已经写入或应该要写入磁盘的字节数syscr 读I/O数syscw 写I/O数rbytes 真正从磁盘读取的字节数wbytes 真正写入到磁盘的字节数cwbytes 因为清空页面缓存而导致没有发生操作的字节数command 执行的命令以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。本文来自系统大全http://www.win7cn.com/如需转载请注明!推荐:win7纯净版

关 键 词:
返回顶部