⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpstat.c

📁 linux下查看系统工具原码,如IOSTAT等
💻 C
📖 第 1 页 / 共 2 页
字号:
		&(st_mp_cpu[curr]->cpu_softirq),		&(st_mp_cpu[curr]->cpu_steal));	 /*	  * Compute the uptime of the system in jiffies (1/100ths of a second	  * if HZ=100).	  * Machine uptime is multiplied by the number of processors here.	  */	 st_mp_tstamp[curr].uptime = st_mp_cpu[curr]->cpu_user +	                             st_mp_cpu[curr]->cpu_nice +	                             st_mp_cpu[curr]->cpu_system +	                             st_mp_cpu[curr]->cpu_idle +	                             st_mp_cpu[curr]->cpu_iowait +	    			     st_mp_cpu[curr]->cpu_hardirq +	    			     st_mp_cpu[curr]->cpu_softirq +	    			     st_mp_cpu[curr]->cpu_steal;      }      else if (!strncmp(line, "cpu", 3)) {	 /*	  * Read the number of jiffies spent in the different modes	  * (user, nice, etc.) for current proc.	  * This is done only on SMP machines.	  */	 cc_iowait = cc_hardirq = cc_softirq = cc_steal = 0;	 sscanf(line + 3, "%d %llu %llu %llu %llu %llu %llu %llu %llu",		&proc_nb,		&cc_user, &cc_nice, &cc_system, &cc_idle, &cc_iowait,		&cc_hardirq, &cc_softirq, &cc_steal);	 if (proc_nb <= cpu_nr) {	    st_mp_cpu_i = st_mp_cpu[curr] + proc_nb + 1;	    st_mp_cpu_i->cpu_user    = cc_user;	    st_mp_cpu_i->cpu_nice    = cc_nice;	    st_mp_cpu_i->cpu_system  = cc_system;	    st_mp_cpu_i->cpu_idle    = cc_idle;	    st_mp_cpu_i->cpu_iowait  = cc_iowait;	    st_mp_cpu_i->cpu_hardirq = cc_hardirq;	    st_mp_cpu_i->cpu_softirq = cc_softirq;	    st_mp_cpu_i->cpu_steal   = cc_steal;	 }	 /* else:	  * Additional CPUs have been dynamically registered in /proc/stat.	  * mpstat won't crash, but the CPU stats might be false...	  */		 if (!proc_nb)	    /*	     * Compute uptime reduced for one proc,	     * using jiffies count for proc#0.	     */	    st_mp_tstamp[curr].uptime0 = cc_user + cc_nice + cc_system +	       				 cc_idle + cc_iowait + cc_hardirq +	    				 cc_softirq + cc_steal;      }      else if (!strncmp(line, "intr ", 5))	 /*	  * Read total number of interrupts received since system boot.	  * Interrupts counter became unsigned long long with kernel 2.6.5.	  */	 sscanf(line + 5, "%llu", &(st_mp_cpu[curr]->irq));   }   fclose(fp);}/* *************************************************************************** * Read stats from /proc/interrupts *************************************************************************** */void read_interrupts_stat(short curr){   FILE *fp;   struct mp_stats *st_mp_cpu_i;   static char line[INTERRUPTS_LINE];   unsigned long irq = 0;   unsigned int cpu;   for (cpu = 0; cpu <= cpu_nr; cpu++) {      st_mp_cpu_i = st_mp_cpu[curr] + cpu +1;      st_mp_cpu_i->irq = 0;   }   if ((fp = fopen(INTERRUPTS, "r")) != NULL) {      while (fgets(line, INTERRUPTS_LINE, fp) != NULL) {	 if (isdigit(line[2])) {		    for (cpu = 0; cpu <= cpu_nr; cpu++) {	       st_mp_cpu_i = st_mp_cpu[curr] + cpu + 1;	       sscanf(line + 4 + 11 * cpu, " %10lu", &irq);	       st_mp_cpu_i->irq += irq;	    }	 }      }      fclose(fp);   }}/* *************************************************************************** * Main loop: read stats from the relevant sources, * and display them. *************************************************************************** */void rw_mp_stat_loop(short dis_hdr, unsigned long lines, int rows,		     struct tm *loc_time){   short curr = 1, dis = 1;   st_mp_tstamp[0].hour   = loc_time->tm_hour;   st_mp_tstamp[0].minute = loc_time->tm_min;   st_mp_tstamp[0].second = loc_time->tm_sec;   /* Read stats */   read_proc_stat(0);   read_interrupts_stat(0);   if (!interval) {      /* Display since boot time */      st_mp_tstamp[1] = st_mp_tstamp[0];      memset(st_mp_cpu[1], 0, MP_STATS_SIZE * (cpu_nr + 2));      write_stats(0, DISP_HDR, loc_time);      exit(0);   }   /* Set a handler for SIGALRM */   alarm_handler(0);   /* Save the first stats collected. Will be used to compute the average */   st_mp_tstamp[2] = st_mp_tstamp[0];   memcpy(st_mp_cpu[2], st_mp_cpu[0], MP_STATS_SIZE * (cpu_nr + 2));   pause();   do {      /* Resetting the structure not needed since every fields will be set */      /* Save time */      get_localtime(loc_time);      st_mp_tstamp[curr].hour   = loc_time->tm_hour;      st_mp_tstamp[curr].minute = loc_time->tm_min;      st_mp_tstamp[curr].second = loc_time->tm_sec;      /* Read stats */      read_proc_stat(curr);      read_interrupts_stat(curr);      /* Write stats */      if (!dis_hdr) {	 dis = lines / rows;	 if (dis)	    lines %= rows;	 lines++;      }      write_stats(curr, dis, loc_time);      /* Flush data */      fflush(stdout);      if (count > 0)	 count--;      if (count) {	 curr ^= 1;	 pause();      }   }   while (count);   /* Write stats average */   write_stats_avg(curr, dis_hdr);}/* *************************************************************************** * Main entry to the program *************************************************************************** */int main(int argc, char **argv){   int opt = 0, i;   struct utsname header;   short dis_hdr = -1, opt_used = 0;   unsigned long lines = 0;   int rows = 23;   struct tm loc_time;#ifdef USE_NLS   /* Init National Language Support */   init_nls();#endif   /* How many processors on this machine ? */   cpu_nr = get_cpu_nr(~0);   /*    * cpu_nr: a value of 1 means there are 2 processors (0 and 1).    * In this case, we have to allocate 3 structures: global, proc0 and proc1.    */   salloc_mp_cpu(cpu_nr + 2);   while (++opt < argc) {      if (!strcmp(argv[opt], "-V"))	 print_version();      else if (!strcmp(argv[opt], "-P")) {	 /* '-P ALL' can be used on UP machines */	 if (argv[++opt]) {	    opt_used = 1;	    dis_hdr++;	    if (!strcmp(argv[opt], K_ALL)) {	       if (cpu_nr)		  dis_hdr = 9;	       /*		* Set bit for every processor.		* Also indicate to display stats for CPU 'all'.		*/	       memset(cpu_bitmap, 0xff, ((cpu_nr + 1 + (cpu_nr > 0)) >> 3) + 1);	    }	    else {	       if (strspn(argv[opt], DIGITS) != strlen(argv[opt]))		  usage(argv[0]);	       i = atoi(argv[opt]);	/* Get cpu number */	       if (i > cpu_nr) {		  fprintf(stderr, _("Not that many processors!\n"));		  exit(1);	       }	       i++;	       *(cpu_bitmap + (i >> 3)) |= 1 << (i & 0x07);	    }	 }	 else	    usage(argv[0]);      }      else if (interval < 0) {		/* Get interval */	 if (strspn(argv[opt], DIGITS) != strlen(argv[opt]))	    usage(argv[0]);	 interval = atol(argv[opt]);	 if (interval < 0)	    usage(argv[0]);	 count = -1;      }      else if (count <= 0) {		/* Get count value */	 if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||	     !interval)	    usage(argv[0]);	 count = atol(argv[opt]);	 if (count < 1)	   usage(argv[0]);      }      else	 usage(argv[0]);   }   if (!opt_used)      /* Option -P not used: set bit 0 (global stats among all proc) */      *cpu_bitmap = 1;   if (dis_hdr < 0)      dis_hdr = 0;   if (!dis_hdr) {      /* Get window size */      rows = get_win_height();      lines = rows;   }   if (interval < 0)      /* Interval not set => display stats since boot time */      interval = 0;   /* Get time */   get_localtime(&loc_time);   /* Get system name, release number and hostname */   uname(&header);   print_gal_header(&loc_time, header.sysname, header.release, header.nodename);   /* Main loop */   rw_mp_stat_loop(dis_hdr, lines, rows, &loc_time);   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -