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

📄 sadc.c

📁 linux下查看系统工具原码,如IOSTAT等
💻 C
📖 第 1 页 / 共 4 页
字号:
 */void read_proc_meminfo(void){   FILE *fp;   static char line[128];   if ((fp = fopen(MEMINFO, "r")) == NULL)      return;   while (fgets(line, 128, fp) != NULL) {      if (!strncmp(line, "MemTotal:", 9))	 /* Read the total amount of memory in kB */	 sscanf(line + 9, "%lu", &(file_stats.tlmkb));      else if (!strncmp(line, "MemFree:", 8))	 /* Read the amount of free memory in kB */	 sscanf(line + 8, "%lu", &(file_stats.frmkb));      else if (!strncmp(line, "Buffers:", 8))	 /* Read the amount of buffered memory in kB */	 sscanf(line + 8, "%lu", &(file_stats.bufkb));      else if (!strncmp(line, "Cached:", 7))	 /* Read the amount of cached memory in kB */	 sscanf(line + 7, "%lu", &(file_stats.camkb));      else if (!strncmp(line, "SwapCached:", 11))	 /* Read the amount of cached swap in kB */	 sscanf(line + 11, "%lu", &(file_stats.caskb));      else if (!strncmp(line, "SwapTotal:", 10))	 /* Read the total amount of swap memory in kB */	 sscanf(line + 10, "%lu", &(file_stats.tlskb));      else if (!strncmp(line, "SwapFree:", 9))	 /* Read the amount of free swap memory in kB */	 sscanf(line + 9, "%lu", &(file_stats.frskb));   }   fclose(fp);}/* *************************************************************************** * Read stats from /proc/vmstat (post 2.5 kernels) *************************************************************************** */void read_proc_vmstat(void){   FILE *fp;   static char line[128];   if ((fp = fopen(VMSTAT, "r")) == NULL)      return;   while (fgets(line, 128, fp) != NULL) {      /*       * Some of these stats may have already been read       * in /proc/stat file (pre 2.5 kernels).       */      if (!strncmp(line, "pgpgin", 6))	 /* Read number of pages the system paged in */	 sscanf(line + 6, "%lu", &(file_stats.pgpgin));      else if (!strncmp(line, "pgpgout", 7))	 /* Read number of pages the system paged out */	 sscanf(line + 7, "%lu", &(file_stats.pgpgout));      else if (!strncmp(line, "pswpin", 6))	 /* Read number of swap pages brought in */	 sscanf(line + 6, "%lu", &(file_stats.pswpin));      else if (!strncmp(line, "pswpout", 7))	 /* Read number of swap pages brought out */	 sscanf(line + 7, "%lu", &(file_stats.pswpout));      else if (!strncmp(line, "pgfault", 7))	 /* Read number of faults (major+minor) made by the system */	 sscanf(line + 7, "%lu", &(file_stats.pgfault));      else if (!strncmp(line, "pgmajfault", 10))	 /* Read number of faults (major only) made by the system */	 sscanf(line + 10, "%lu", &(file_stats.pgmajfault));   }   fclose(fp);}/* *************************************************************************** * Read stats from /proc/<pid>/stat *************************************************************************** */void read_pid_stat(unsigned int flags){   FILE *fp;   int pid;   static char filename[24];   if (WANT_ALL_PIDS(flags))      get_pid_list();   for (pid = 0; pid < pid_idx; pid++) {      if (!all_pids[pid])	 continue;      sprintf(filename, PID_STAT, all_pids[pid]);      if ((fp = fopen(filename, "r")) == NULL) {	 /* No such process */	 all_pids[pid] = 0;	 pid_stats[pid]->pid = 0;	 continue;      }      fscanf(fp, "%*d %*s %*s %*d %*d %*d %*d %*d %*u %lu %lu %lu %lu %lu %lu %lu "	         "%lu %*d %*d %*u %*u %*d %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u "	         "%*u %*u %*u %lu %lu %*u %u\n",	     &(pid_stats[pid]->minflt), &(pid_stats[pid]->cminflt),	     &(pid_stats[pid]->majflt), &(pid_stats[pid]->cmajflt),	     &(pid_stats[pid]->utime),  &(pid_stats[pid]->stime),	     &(pid_stats[pid]->cutime), &(pid_stats[pid]->cstime),	     &(pid_stats[pid]->nswap),  &(pid_stats[pid]->cnswap),	     &(pid_stats[pid]->processor));      pid_stats[pid]->pid  = all_pids[pid];      pid_stats[pid]->flag = f_pids[pid];      fclose(fp);   }}/* *************************************************************************** * Read stats from /proc/tty/driver/serial *************************************************************************** */void read_serial_stat(void){   struct stats_serial *st_serial_i;   unsigned int sl = 0;#ifndef SMP_RACE   FILE *fp;   static char line[256];   char *p;	      if ((fp = fopen(SERIAL, "r")) != NULL) {	 while ((fgets(line, 256, fp) != NULL) && (sl < serial_nr)) {	    if ((p = strstr(line, "tx:")) != NULL) {	       st_serial_i = st_serial + sl;	       sscanf(line, "%u", &(st_serial_i->line));	       /*		* Read the number of chars transmitted and received by		* current serial line.		*/	       sscanf(p + 3, "%u", &(st_serial_i->tx));	       if ((p = strstr(line, "rx:")) != NULL)		  sscanf(p + 3, "%u", &(st_serial_i->rx));	       if ((p = strstr(line, "fe:")) != NULL)		  sscanf(p + 3, "%u", &(st_serial_i->frame));	       if ((p = strstr(line, "pe:")) != NULL)		  sscanf(p + 3, "%u", &(st_serial_i->parity));	       if ((p = strstr(line, "brk:")) != NULL)		  sscanf(p + 4, "%u", &(st_serial_i->brk));	       if ((p = strstr(line, "oe:")) != NULL)		  sscanf(p + 3, "%u", &(st_serial_i->overrun));	       sl++;	    }	 }	 fclose(fp);      }#endif   while (sl < serial_nr) {      /*       * Nb of serial lines has changed, or appending data to an old file       * with more serial lines than are actually available now.       */      st_serial_i = st_serial + sl++;      st_serial_i->line = ~0;   }}/* *************************************************************************** * Read stats from /proc/interrupts *************************************************************************** */void read_interrupts_stat(void){   FILE *fp;   static char line[INTERRUPTS_LINE];   unsigned int irq = 0, cpu;   struct stats_irq_cpu *p;   if ((fp = fopen(INTERRUPTS, "r")) != NULL) {      while ((fgets(line, INTERRUPTS_LINE, fp) != NULL) && (irq < irqcpu_nr)) {	 if (isdigit(line[2])) {		    p = st_irq_cpu + irq;	    sscanf(line, "%3u", &(p->irq));		    for (cpu = 0; cpu <= cpu_nr; cpu++) {	       p = st_irq_cpu + cpu * irqcpu_nr + irq;	       /*		* No need to set (st_irq_cpu + cpu * irqcpu_nr)->irq:		* same as st_irq_cpu->irq.		*/	       sscanf(line + 4 + 11 * cpu, " %10u", &(p->interrupt));	    }	    irq++;	 }      }      fclose(fp);   }   while (irq < irqcpu_nr) {      /*       * Nb of interrupts per processor has changed, or appending data to an       * old file with more interrupts than are actually available now.       */      p = st_irq_cpu + irq;      p->irq = ~0;	/* This value means this is a dummy interrupt */      irq++;   }}/* *************************************************************************** * Read stats from /proc/sys/fs/... * Some files may not exist, depending on the kernel configuration. *************************************************************************** */void read_ktables_stat(void){   FILE *fp;   unsigned int parm;   /* Open /proc/sys/fs/dentry-state file */   if ((fp = fopen(FDENTRY_STATE, "r")) != NULL) {      fscanf(fp, "%*d %u",	     &(file_stats.dentry_stat));      fclose(fp);   }   /* Open /proc/sys/fs/file-nr file */   if ((fp = fopen(FFILE_NR, "r")) != NULL) {      fscanf(fp, "%u %u",	     &(file_stats.file_used), &parm);      fclose(fp);      /*       * The number of used handles is the number of allocated ones       * minus the number of free ones.       */      file_stats.file_used -= parm;   }   /* Open /proc/sys/fs/inode-state file */   if ((fp = fopen(FINODE_STATE, "r")) != NULL) {      fscanf(fp, "%u %u",	     &(file_stats.inode_used), &parm);      fclose(fp);      /*       * The number of inuse inodes is the number of allocated ones       * minus the number of free ones.       */      file_stats.inode_used -= parm;   }   /* Open /proc/sys/fs/super-max file */   if ((fp = fopen(FSUPER_MAX, "r")) != NULL) {      fscanf(fp, "%u\n",	     &(file_stats.super_max));      fclose(fp);      /* Open /proc/sys/fs/super-nr file */      if ((fp = fopen(FSUPER_NR, "r")) != NULL) {	 fscanf(fp, "%u\n",		&(file_stats.super_used));	 fclose(fp);      }   }   /* Open /proc/sys/fs/dquot-max file */   if ((fp = fopen(FDQUOT_MAX, "r")) != NULL) {      fscanf(fp, "%u\n",	     &(file_stats.dquot_max));      fclose(fp);      /* Open /proc/sys/fs/dquot-nr file */      if ((fp = fopen(FDQUOT_NR, "r")) != NULL) {	 fscanf(fp, "%u",		&(file_stats.dquot_used));	 fclose(fp);      }   }   /* Open /proc/sys/kernel/rtsig-max file */   if ((fp = fopen(FRTSIG_MAX, "r")) != NULL) {      fscanf(fp, "%u\n",	     &(file_stats.rtsig_max));      fclose(fp);      /* Open /proc/sys/kernel/rtsig-nr file */      if ((fp = fopen(FRTSIG_NR, "r")) != NULL) {	 fscanf(fp, "%u\n",		&(file_stats.rtsig_queued));	 fclose(fp);      }   }}/* *************************************************************************** * Read stats from /proc/net/dev *************************************************************************** */void read_net_dev_stat(void){   FILE *fp;   struct stats_net_dev *st_net_dev_i;   static char line[256];   char iface[MAX_IFACE_LEN];   unsigned int dev = 0;   int pos;   if ((fp = fopen(NET_DEV, "r")) != NULL) {      while ((fgets(line, 256, fp) != NULL) && (dev < iface_nr)) {		 pos = strcspn(line, ":");	 if (pos < strlen(line)) {	    st_net_dev_i = st_net_dev + dev;  	    strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));	    iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';	    sscanf(iface, "%s", st_net_dev_i->interface); /* Skip heading spaces */	    sscanf(line + pos + 1, "%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "		                   "%lu %lu %lu %lu %lu %lu",		   &(st_net_dev_i->rx_bytes),		   &(st_net_dev_i->rx_packets),		   &(st_net_dev_i->rx_errors),		   &(st_net_dev_i->rx_dropped),		   &(st_net_dev_i->rx_fifo_errors),		   &(st_net_dev_i->rx_frame_errors),		   &(st_net_dev_i->rx_compressed),		   &(st_net_dev_i->multicast),		   &(st_net_dev_i->tx_bytes),		   &(st_net_dev_i->tx_packets),		   &(st_net_dev_i->tx_errors),		   &(st_net_dev_i->tx_dropped),		   &(st_net_dev_i->tx_fifo_errors),		   &(st_net_dev_i->collisions),		   &(st_net_dev_i->tx_carrier_errors),		   &(st_net_dev_i->tx_compressed));	    dev++;	 }      }      fclose(fp);   }   if (dev < iface_nr) {      /* Reset unused structures */      memset(st_net_dev + dev, 0, STATS_NET_DEV_SIZE * (iface_nr - dev));      while (dev < iface_nr) {	 /*	  * Nb of network interfaces has changed, or appending data to an	  * old file with more interfaces than are actually available now.	  */	 st_net_dev_i = st_net_dev + dev++;	 strcpy(st_net_dev_i->interface, "?");      }   }}/* *************************************************************************** * Read stats from /proc/net/sockstat *************************************************************************** */void read_net_sock_stat(void){   FILE *fp;   static char line[96];   if ((fp = fopen(NET_SOCKSTAT, "r")) == NULL)      return;   while (fgets(line, 96, fp) != NULL) {	      if (!strncmp(line, "sockets:", 8))	 /* Sockets */	 sscanf(line + 14, "%u", &(file_stats.sock_inuse));      else if (!strncmp(line, "TCP:", 4))	 /* TCP sockets */	 sscanf(line + 11, "%u", &(file_stats.tcp_inuse));      else if (!strncmp(line, "UDP:", 4))	 /* UDP sockets */	 sscanf(line + 11, "%u", &(file_stats.udp_inuse));      else if (!strncmp(line, "RAW:", 4))	 /* RAW sockets */	 sscanf(line + 11, "%u", &(file_stats.raw_inuse));      else if (!strncmp(line, "FRAG:", 5))	 /* FRAGments */	 sscanf(line + 12, "%u", &(file_stats.frag_inuse));   }   fclose(fp);}/* *************************************************************************** * Read stats from /proc/net/rpc/nfs *************************************************************************** */void read_net_nfs_stat(void){   FILE *fp;   static char line[256];   if ((fp = fopen(NET_RPC_NFS, "r")) == NULL)      return;   while (fgets(line, 256, fp) != NULL) {	      if (!strncmp(line, "rpc", 3))	 sscanf(line + 3, "%u %u",		&(file_stats.nfs_rpccnt), &(file_stats.nfs_rpcretrans));	      else if (!strncmp(line, "proc3", 5))	 sscanf(line + 5, "%*u %*u %u %*u %*u %u %*u %u %u",		&(file_stats.nfs_getattcnt), &(file_stats.nfs_accesscnt),		&(file_stats.nfs_readcnt), &(file_stats.nfs_writecnt));   }   fclose(fp);}/* *************************************************************************** * Read stats from /proc/net/rpc/nfsd *************************************************************************** */void read_net_nfsd_stat(void){   FILE *fp;   static char line[256];   if ((fp = fopen(NET_RPC_NFSD, "r")) == NULL)      return;   while (fgets(line, 256, fp) != NULL) {	

⌨️ 快捷键说明

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