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

📄 sa_common.c

📁 linux下查看系统工具原码,如IOSTAT等
💻 C
📖 第 1 页 / 共 2 页
字号:
		    if ((st_net_dev_i->rx_bytes < st_net_dev_j->rx_bytes) &&		(st_net_dev_i->rx_packets > st_net_dev_j->rx_packets) &&		(st_net_dev_j->rx_bytes > (~0UL >> 1)))	       ovfw = TRUE;	    if ((st_net_dev_i->tx_bytes < st_net_dev_j->tx_bytes) &&		(st_net_dev_i->tx_packets > st_net_dev_j->tx_packets) &&		(st_net_dev_j->tx_bytes > (~0UL >> 1)))	       ovfw = TRUE;	    if ((st_net_dev_i->rx_packets < st_net_dev_j->rx_packets) &&		(st_net_dev_i->rx_bytes > st_net_dev_j->rx_bytes) &&		(st_net_dev_j->rx_packets > (~0UL >> 1)))	       ovfw = TRUE;	    if ((st_net_dev_i->tx_packets < st_net_dev_j->tx_packets) &&		(st_net_dev_i->tx_bytes > st_net_dev_j->tx_bytes) &&		(st_net_dev_j->tx_packets > (~0UL >> 1)))	       ovfw = TRUE;	    if (!ovfw) {	       /* OK: assume here that the device was actually unregistered */	       memset(st_net_dev_j, 0, STATS_NET_DEV_SIZE);	       strcpy(st_net_dev_j->interface, st_net_dev_i->interface);	    }	 }	 return index;      }      index++;   }   /* Network interface not found: Look for the first free structure */   for (index = 0; index < file_hdr->sa_iface; index++) {      st_net_dev_j = st_net_dev[ref] + index;      if (!strcmp(st_net_dev_j->interface, "?")) {	 memset(st_net_dev_j, 0, STATS_NET_DEV_SIZE);	 strcpy(st_net_dev_j->interface, st_net_dev_i->interface);	 break;      }   }   if (index >= file_hdr->sa_iface)      /* No free structure: Default is structure of same rank */      index = pos;   st_net_dev_j = st_net_dev[ref] + index;   /* Since the name is not the same, reset all the structure */   memset(st_net_dev_j, 0, STATS_NET_DEV_SIZE);   strcpy(st_net_dev_j->interface, st_net_dev_i->interface);   return  index;}/* *************************************************************************** * Disks may be registered dynamically (true in /proc/stat file). * This is what we try to guess here. *************************************************************************** */int check_disk_reg(struct file_hdr *file_hdr, struct disk_stats *st_disk[],		   short curr, short ref, int pos){   struct disk_stats *st_disk_i, *st_disk_j;   int index = 0;   st_disk_i = st_disk[curr] + pos;   while (index < file_hdr->sa_nr_disk) {      st_disk_j = st_disk[ref] + index;      if ((st_disk_i->major == st_disk_j->major) &&	  (st_disk_i->minor == st_disk_j->minor)) {	 /*	  * Disk found.	  * If a counter has decreased, then we may assume that the	  * corresponding device was unregistered, then registered again.	  * NB: AFAIK, such a device cannot be unregistered with current	  * kernels.	  */	 if ((st_disk_i->nr_ios < st_disk_j->nr_ios) ||	     (st_disk_i->rd_sect < st_disk_j->rd_sect) ||	     (st_disk_i->wr_sect < st_disk_j->wr_sect)) {	    memset(st_disk_j, 0, DISK_STATS_SIZE);	    st_disk_j->major = st_disk_i->major;	    st_disk_j->minor = st_disk_i->minor;	 }	 return index;      }      index++;   }   /* Disk not found: Look for the first free structure */   for (index = 0; index < file_hdr->sa_nr_disk; index++) {      st_disk_j = st_disk[ref] + index;      if (!(st_disk_j->major + st_disk_j->minor)) {	 memset(st_disk_j, 0, DISK_STATS_SIZE);	 st_disk_j->major = st_disk_i->major;	 st_disk_j->minor = st_disk_i->minor;	 break;      }   }   if (index >= file_hdr->sa_nr_disk)      /* No free structure found: Default is structure of same rank */      index = pos;   st_disk_j = st_disk[ref] + index;   /* Since the device is not the same, reset all the structure */   memset(st_disk_j, 0, DISK_STATS_SIZE);   st_disk_j->major = st_disk_i->major;   st_disk_j->minor = st_disk_i->minor;   return index;}/* *************************************************************************** * Since ticks may vary slightly from cpu to cpu, we'll want * to recalculate itv based on this cpu's tick count, rather * than that reported by the "cpu" line.  Otherwise we * occasionally end up with slightly skewed figures, with * the skew being greater as the time interval grows shorter. *************************************************************************** */unsigned long long get_per_cpu_interval(struct stats_one_cpu *st_cpu_i,					struct stats_one_cpu *st_cpu_j){   return ((st_cpu_i->per_cpu_user + st_cpu_i->per_cpu_nice +	    st_cpu_i->per_cpu_system + st_cpu_i->per_cpu_iowait +	    st_cpu_i->per_cpu_idle + st_cpu_i->per_cpu_steal) -	   (st_cpu_j->per_cpu_user + st_cpu_j->per_cpu_nice +	    st_cpu_j->per_cpu_system + st_cpu_j->per_cpu_iowait +	    st_cpu_j->per_cpu_idle + st_cpu_j->per_cpu_steal));}/* *************************************************************************** * Read data from a sa data file *************************************************************************** */int sa_fread(int ifd, void *buffer, int size, int mode){   int n;   if ((n = read(ifd, buffer, size)) < 0) {      fprintf(stderr, _("Error while reading system activity file: %s\n"),	      strerror(errno));      exit(2);   }   if (!n && (mode == SOFT_SIZE))      return 1;	/* EOF */   if (n < size) {      fprintf(stderr, _("End of system activity file unexpected\n"));      exit(2);   }   return 0;}/* *************************************************************************** * Open a data file, and perform various checks before reading *************************************************************************** */void prep_file_for_reading(int *ifd, char *dfile, struct file_hdr *file_hdr,			    unsigned int *actflag, unsigned int flags){   int nb;   /* Open sa data file */   if ((*ifd = open(dfile, O_RDONLY)) < 0) {      fprintf(stderr, _("Cannot open %s: %s\n"), dfile, strerror(errno));      exit(2);   }   /* Read sa data file header */   nb = read(*ifd, file_hdr, FILE_HDR_SIZE);   if ((nb != FILE_HDR_SIZE) || (file_hdr->sa_magic != SA_MAGIC)) {      fprintf(stderr, _("Invalid system activity file: %s (%#x)\n"),	      dfile, file_hdr->sa_magic);      close(*ifd);      exit(3);   }   *actflag &= file_hdr->sa_actflag;   if (!(*actflag) ||       (WANT_PER_PROC(flags) && !WANT_ALL_PROC(flags) && !file_hdr->sa_proc	&& !(*actflag & ~(A_CPU + A_IRQ)))) {      /*       * We want stats that are not available,       * maybe because this is an old version of the sa data file.       * Error message is displayed if:       * -> no activities remain in sar_actflag       * -> or if the user entered eg 'sar -u -P 0 -f file' or       * 'sar -I SUM -P 0 -f file', with file created on a UP machine.       * NOTE1: If A_ONE_IRQ stats are available, stats       * concerning _all_ the IRQs are available.       * NOTE2: If file_hdr.sa_proc > 0, stats       * concerning _all_ the CPUs are available.       * NOTE3: If file_hdr.sa_irqcpu != 0, stats       * concerning the IRQs per processor are available.       */      fprintf(stderr, _("Requested activities not available in file\n"));      close(*ifd);      exit(1);   }}/* *************************************************************************** * Parse sar activities options (also used by sadf) *************************************************************************** */int parse_sar_opt(char *argv[], int opt, unsigned int *actflag,		  unsigned int *flags, short *dis_hdr, int caller,		  unsigned char irq_bitmap[], unsigned char cpu_bitmap[]){   int i;   for (i = 1; *(argv[opt] + i); i++) {      switch (*(argv[opt] + i)) {       case 'A':	 *actflag |= A_PROC + A_PAGE + A_IRQ + A_IO + A_CPU +	    A_CTXSW + A_SWAP + A_MEMORY + A_SERIAL +	    A_MEM_AMT + A_KTABLES + A_NET_DEV +	    A_NET_EDEV + A_NET_SOCK + A_NET_NFS + A_NET_NFSD +	    A_QUEUE + A_DISK + A_ONE_IRQ;	 /* Force '-P ALL -I XALL' */	 *flags |= S_F_A_OPTION + S_F_ALL_PROC + S_F_PER_PROC;	 init_bitmap(irq_bitmap, ~0, NR_IRQS);	 init_bitmap(cpu_bitmap, ~0, NR_CPUS);	 break;       case 'B':	 *actflag |= A_PAGE;	 (*dis_hdr)++;	 break;       case 'b':	 *actflag |= A_IO;	 (*dis_hdr)++;	 break;       case 'c':	 *actflag |= A_PROC;	 (*dis_hdr)++;	 break;       case 'd':	 *actflag |= A_DISK;	 (*dis_hdr)++;	 break;       case 'p':	 *flags |= S_F_DEV_PRETTY;	 break;       case 'q':	 *actflag |= A_QUEUE;	 (*dis_hdr)++;	 break;       case 'r':	 *actflag |= A_MEM_AMT;	 (*dis_hdr)++;	 break;       case 'R':	 *actflag |= A_MEMORY;	 (*dis_hdr)++;	 break;       case 't':	 if (caller == C_SAR)	    *flags |= S_F_TRUE_TIME;	 else	    return 1;	 break;       case 'u':	 *actflag |= A_CPU;	 (*dis_hdr)++;	 break;       case 'v':	 *actflag |= A_KTABLES;	 (*dis_hdr)++;	 break;       case 'w':	 *actflag |= A_CTXSW;	 (*dis_hdr)++;	 break;       case 'W':	 *actflag |= A_SWAP;	 (*dis_hdr)++;	 break;       case 'y':	 *actflag |= A_SERIAL;	 (*dis_hdr)++;	 break;       case 'V':	 print_version();	 break;       default:	 return 1;      }   }   return 0;}/* *************************************************************************** * Parse sar "-n" option *************************************************************************** */int parse_sar_n_opt(char *argv[], int *opt, unsigned int *actflag,		    short *dis_hdr){   if (!strcmp(argv[*opt], K_DEV))      *actflag |= A_NET_DEV;   else if (!strcmp(argv[*opt], K_EDEV))      *actflag |= A_NET_EDEV;   else if (!strcmp(argv[*opt], K_SOCK))      *actflag |= A_NET_SOCK;   else if (!strcmp(argv[*opt], K_NFS))      *actflag |= A_NET_NFS;   else if (!strcmp(argv[*opt], K_NFSD))      *actflag |= A_NET_NFSD;   else if (!strcmp(argv[*opt], K_ALL)) {      *actflag |= A_NET_DEV + A_NET_EDEV + A_NET_SOCK + A_NET_NFS + A_NET_NFSD;      *dis_hdr = 9;   }   else      return 1;   (*opt)++;   return 0;}/* *************************************************************************** * Parse sar "-I" option *************************************************************************** */int parse_sar_I_opt(char *argv[], int *opt, unsigned int *actflag,		    short *dis_hdr, unsigned char irq_bitmap[]){   int i;   if (!strcmp(argv[*opt], K_SUM))      *actflag |= A_IRQ;   else {      *actflag |= A_ONE_IRQ;      if (!strcmp(argv[*opt], K_ALL)) {	 *dis_hdr = 9;	 /* Set bit for the first 16 irq */	 irq_bitmap[0] = 0xff;	 irq_bitmap[1] = 0xff;      }      else if (!strcmp(argv[*opt], K_XALL)) {	 *dis_hdr = 9;	 /* Set every bit */	 init_bitmap(irq_bitmap, ~0, NR_IRQS);      }      else {	 /*	  * Get irq number.	  */	 if (strspn(argv[*opt], DIGITS) != strlen(argv[*opt]))	    return 1;	 i = atoi(argv[*opt]);	 if ((i < 0) || (i >= NR_IRQS))	    return 1;	 irq_bitmap[i >> 3] |= 1 << (i & 0x07);      }   }   (*opt)++;   return 0;}/* *************************************************************************** * Parse sar and sadf "-P" option *************************************************************************** */int parse_sa_P_opt(char *argv[], int *opt, unsigned int *flags,		   short *dis_hdr, unsigned char cpu_bitmap[]){   int i;   if (argv[++(*opt)]) {      *flags |= S_F_PER_PROC;      (*dis_hdr)++;      if (!strcmp(argv[*opt], K_ALL)) {	 *dis_hdr = 9;	 /*	  * Set bit for every processor.	  * We still don't know if we are going to read stats	  * from a file or not...	  */	 init_bitmap(cpu_bitmap, ~0, NR_CPUS);	 *flags |= S_F_ALL_PROC;      }      else {	 if (strspn(argv[*opt], DIGITS) != strlen(argv[*opt]))	    return 1;	 i = atoi(argv[*opt]);	/* Get cpu number */	 if ((i < 0) || (i >= NR_CPUS))	    return 1;	 cpu_bitmap[i >> 3] |= 1 << (i & 0x07);      }      (*opt)++;   }   else      return 1;   return 0;}

⌨️ 快捷键说明

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