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

📄 sysinfo.c

📁 linux下获取一些环境信息的代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    kb_low_free  = kb_main_free;  }  if(kb_inactive==~0UL){    kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;  }  kb_swap_used = kb_swap_total - kb_swap_free;  kb_main_used = kb_main_total - kb_main_free;}/*****************************************************************//* read /proc/vminfo only for 2.5.41 and above */typedef struct vm_table_struct {  const char *name;     /* VM statistic name */  unsigned long *slot;       /* slot in return struct */} vm_table_struct;static int compare_vm_table_structs(const void *a, const void *b){  return strcmp(((const vm_table_struct*)a)->name,((const vm_table_struct*)b)->name);}// see include/linux/page-flags.h and mm/page_alloc.cunsigned long vm_nr_dirty;           // dirty writable pagesunsigned long vm_nr_writeback;       // pages under writebackunsigned long vm_nr_pagecache;       // pages in pagecache -- gone in 2.5.66+ kernelsunsigned long vm_nr_page_table_pages;// pages used for pagetablesunsigned long vm_nr_reverse_maps;    // includes PageDirectunsigned long vm_nr_mapped;          // mapped into pagetablesunsigned long vm_nr_slab;            // in slabunsigned long vm_pgpgin;             // kB disk reads  (same as 1st num on /proc/stat page line)unsigned long vm_pgpgout;            // kB disk writes (same as 2nd num on /proc/stat page line)unsigned long vm_pswpin;             // swap reads     (same as 1st num on /proc/stat swap line)unsigned long vm_pswpout;            // swap writes    (same as 2nd num on /proc/stat swap line)unsigned long vm_pgalloc;            // page allocationsunsigned long vm_pgfree;             // page freeingsunsigned long vm_pgactivate;         // pages moved inactive -> activeunsigned long vm_pgdeactivate;       // pages moved active -> inactiveunsigned long vm_pgfault;           // total faults (major+minor)unsigned long vm_pgmajfault;       // major faultsunsigned long vm_pgscan;          // pages scanned by page reclaimunsigned long vm_pgrefill;       // inspected by refill_inactive_zoneunsigned long vm_pgsteal;       // total pages reclaimedunsigned long vm_kswapd_steal; // pages reclaimed by kswapd// next 3 as defined by the 2.5.52 kernelunsigned long vm_pageoutrun;  // times kswapd ran page reclaimunsigned long vm_allocstall; // times a page allocator ran direct reclaimunsigned long vm_pgrotated; // pages rotated to the tail of the LRU for immediate reclaim// seen on a 2.6.8-rc1 kernel, apparently replacing old fieldsstatic unsigned long vm_pgalloc_dma;          // static unsigned long vm_pgalloc_high;         // static unsigned long vm_pgalloc_normal;       // static unsigned long vm_pgrefill_dma;         // static unsigned long vm_pgrefill_high;        // static unsigned long vm_pgrefill_normal;      // static unsigned long vm_pgscan_direct_dma;    // static unsigned long vm_pgscan_direct_high;   // static unsigned long vm_pgscan_direct_normal; // static unsigned long vm_pgscan_kswapd_dma;    // static unsigned long vm_pgscan_kswapd_high;   // static unsigned long vm_pgscan_kswapd_normal; // static unsigned long vm_pgsteal_dma;          // static unsigned long vm_pgsteal_high;         // static unsigned long vm_pgsteal_normal;       // // seen on a 2.6.8-rc1 kernelstatic unsigned long vm_kswapd_inodesteal;    //static unsigned long vm_nr_unstable;          //static unsigned long vm_pginodesteal;         //static unsigned long vm_slabs_scanned;        //void vminfo(void){  char namebuf[16]; /* big enough to hold any row name */  vm_table_struct findme = { namebuf, NULL};  vm_table_struct *found;  char *head;  char *tail;  static const vm_table_struct vm_table[] = {  {"allocstall",          &vm_allocstall},  {"kswapd_inodesteal",   &vm_kswapd_inodesteal},  {"kswapd_steal",        &vm_kswapd_steal},  {"nr_dirty",            &vm_nr_dirty},           // page version of meminfo Dirty  {"nr_mapped",           &vm_nr_mapped},          // page version of meminfo Mapped  {"nr_page_table_pages", &vm_nr_page_table_pages},// same as meminfo PageTables  {"nr_pagecache",        &vm_nr_pagecache},       // gone in 2.5.66+ kernels  {"nr_reverse_maps",     &vm_nr_reverse_maps},    // page version of meminfo ReverseMaps GONE  {"nr_slab",             &vm_nr_slab},            // page version of meminfo Slab  {"nr_unstable",         &vm_nr_unstable},  {"nr_writeback",        &vm_nr_writeback},       // page version of meminfo Writeback  {"pageoutrun",          &vm_pageoutrun},  {"pgactivate",          &vm_pgactivate},  {"pgalloc",             &vm_pgalloc},  // GONE (now separate dma,high,normal)  {"pgalloc_dma",         &vm_pgalloc_dma},  {"pgalloc_high",        &vm_pgalloc_high},  {"pgalloc_normal",      &vm_pgalloc_normal},  {"pgdeactivate",        &vm_pgdeactivate},  {"pgfault",             &vm_pgfault},  {"pgfree",              &vm_pgfree},  {"pginodesteal",        &vm_pginodesteal},  {"pgmajfault",          &vm_pgmajfault},  {"pgpgin",              &vm_pgpgin},     // important  {"pgpgout",             &vm_pgpgout},     // important  {"pgrefill",            &vm_pgrefill},  // GONE (now separate dma,high,normal)  {"pgrefill_dma",        &vm_pgrefill_dma},  {"pgrefill_high",       &vm_pgrefill_high},  {"pgrefill_normal",     &vm_pgrefill_normal},  {"pgrotated",           &vm_pgrotated},  {"pgscan",              &vm_pgscan},  // GONE (now separate direct,kswapd and dma,high,normal)  {"pgscan_direct_dma",   &vm_pgscan_direct_dma},  {"pgscan_direct_high",  &vm_pgscan_direct_high},  {"pgscan_direct_normal",&vm_pgscan_direct_normal},  {"pgscan_kswapd_dma",   &vm_pgscan_kswapd_dma},  {"pgscan_kswapd_high",  &vm_pgscan_kswapd_high},  {"pgscan_kswapd_normal",&vm_pgscan_kswapd_normal},  {"pgsteal",             &vm_pgsteal},  // GONE (now separate dma,high,normal)  {"pgsteal_dma",         &vm_pgsteal_dma},  {"pgsteal_high",        &vm_pgsteal_high},  {"pgsteal_normal",      &vm_pgsteal_normal},  {"pswpin",              &vm_pswpin},     // important  {"pswpout",             &vm_pswpout},     // important  {"slabs_scanned",       &vm_slabs_scanned},  };  const int vm_table_count = sizeof(vm_table)/sizeof(vm_table_struct);  vm_pgalloc = 0;  vm_pgrefill = 0;  vm_pgscan = 0;  vm_pgsteal = 0;  FILE_TO_BUF(VMINFO_FILE,vminfo_fd);  head = buf;  for(;;){    tail = strchr(head, ' ');    if(!tail) break;    *tail = '\0';    if(strlen(head) >= sizeof(namebuf)){      head = tail+1;      goto nextline;    }    strcpy(namebuf,head);    found = bsearch(&findme, vm_table, vm_table_count,        sizeof(vm_table_struct), compare_vm_table_structs    );    head = tail+1;    if(!found) goto nextline;    *(found->slot) = strtoul(head,&tail,10);nextline://if(found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));//else      fprintf(stderr,"%s not found\n",findme.name);    tail = strchr(head, '\n');    if(!tail) break;    head = tail+1;  }  if(!vm_pgalloc)    vm_pgalloc  = vm_pgalloc_dma + vm_pgalloc_high + vm_pgalloc_normal;  if(!vm_pgrefill)    vm_pgrefill = vm_pgrefill_dma + vm_pgrefill_high + vm_pgrefill_normal;  if(!vm_pgscan)    vm_pgscan   = vm_pgscan_direct_dma + vm_pgscan_direct_high + vm_pgscan_direct_normal                + vm_pgscan_kswapd_dma + vm_pgscan_kswapd_high + vm_pgscan_kswapd_normal;  if(!vm_pgsteal)    vm_pgsteal  = vm_pgsteal_dma + vm_pgsteal_high + vm_pgsteal_normal;}///////////////////////////////////////////////////////////////////////// based on Fabian Frederick's /proc/diskstats parserunsigned int getpartitions_num(struct disk_stat *disks, int ndisks){  int i=0;  int partitions=0;  for (i=0;i<ndisks;i++){	partitions+=disks[i].partitions;  }  return partitions;}/////////////////////////////////////////////////////////////////////////////unsigned int getdiskstat(struct disk_stat **disks, struct partition_stat **partitions){  FILE* fd;  int cDisk = 0;  int cPartition = 0;  int fields;  unsigned dummy;  *disks = NULL;  *partitions = NULL;  buff[BUFFSIZE-1] = 0;   fd = fopen("/proc/diskstats", "rb");  if(!fd) crash("/proc/diskstats");  for (;;) {    if (!fgets(buff,BUFFSIZE-1,fd)){      fclose(fd);      break;    }    fields = sscanf(buff, " %*d %*d %*s %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u", &dummy);    if (fields == 1){      (*disks) = realloc(*disks, (cDisk+1)*sizeof(struct disk_stat));      sscanf(buff,  "   %*d    %*d %15s %u %u %llu %u %u %u %llu %u %u %u %u",        //&disk_major,        //&disk_minor,        (*disks)[cDisk].disk_name,        &(*disks)[cDisk].reads,        &(*disks)[cDisk].merged_reads,        &(*disks)[cDisk].reads_sectors,        &(*disks)[cDisk].milli_reading,        &(*disks)[cDisk].writes,        &(*disks)[cDisk].merged_writes,        &(*disks)[cDisk].written_sectors,        &(*disks)[cDisk].milli_writing,        &(*disks)[cDisk].inprogress_IO,        &(*disks)[cDisk].milli_spent_IO,        &(*disks)[cDisk].weighted_milli_spent_IO      );        (*disks)[cDisk].partitions=0;      cDisk++;    }else{      (*partitions) = realloc(*partitions, (cPartition+1)*sizeof(struct partition_stat));      fflush(stdout);      sscanf(buff,  "   %*d    %*d %15s %u %llu %u %u",        //&part_major,        //&part_minor,        (*partitions)[cPartition].partition_name,        &(*partitions)[cPartition].reads,        &(*partitions)[cPartition].reads_sectors,        &(*partitions)[cPartition].writes,        &(*partitions)[cPartition].requested_writes      );      (*partitions)[cPartition++].parent_disk = cDisk-1;      (*disks)[cDisk-1].partitions++;	    }  }  return cDisk;}/////////////////////////////////////////////////////////////////////////////// based on Fabian Frederick's /proc/slabinfo parserunsigned int getslabinfo (struct slab_cache **slab){  FILE* fd;  int cSlab = 0;  buff[BUFFSIZE-1] = 0;   *slab = NULL;  fd = fopen("/proc/slabinfo", "rb");  if(!fd) crash("/proc/slabinfo");  while (fgets(buff,BUFFSIZE-1,fd)){    if(!memcmp("slabinfo - version:",buff,19)) continue; // skip header    if(*buff == '#')                           continue; // skip comments    (*slab) = realloc(*slab, (cSlab+1)*sizeof(struct slab_cache));    sscanf(buff,  "%47s %u %u %u %u",  // allow 47; max seen is 24      (*slab)[cSlab].name,      &(*slab)[cSlab].active_objs,      &(*slab)[cSlab].num_objs,      &(*slab)[cSlab].objsize,      &(*slab)[cSlab].objperslab    ) ;    cSlab++;  }  fclose(fd);  return cSlab;}///////////////////////////////////////////////////////////////////////////unsigned get_pid_digits(void){  char pidbuf[24];  char *endp;  long rc;  int fd;  static unsigned ret;  if(ret) goto out;  ret = 5;  fd = open("/proc/sys/kernel/pid_max", O_RDONLY);  if(fd==-1) goto out;  rc = read(fd, pidbuf, sizeof pidbuf);  close(fd);  if(rc<3) goto out;  pidbuf[rc] = '\0';  rc = strtol(pidbuf,&endp,10);  if(rc<42) goto out;  if(*endp && *endp!='\n') goto out;  rc--;  // the pid_max value is really the max PID plus 1  ret = 0;  while(rc){    rc /= 10;    ret++;  }out:  return ret;}

⌨️ 快捷键说明

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