?? sysinfo.c
字號:
/***********************************************************************/void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) { double avg_1=0, avg_5=0, avg_15=0; char *restrict savelocale; FILE_TO_BUF(LOADAVG_FILE,loadavg_fd); savelocale = setlocale(LC_NUMERIC, NULL); setlocale(LC_NUMERIC, "C"); if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) { fputs("bad data in " LOADAVG_FILE "\n", stderr); exit(1); } setlocale(LC_NUMERIC, savelocale); SET_IF_DESIRED(av1, avg_1); SET_IF_DESIRED(av5, avg_5); SET_IF_DESIRED(av15, avg_15);} static char buff[BUFFSIZE]; /* used in the procedures *//***********************************************************************/static void crash(const char *filename) { perror(filename); exit(EXIT_FAILURE);}/***********************************************************************/static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) { struct direct *ent; DIR *proc; *running=0; *blocked=0; if((proc=opendir("/proc"))==NULL) crash("/proc"); while(( ent=readdir(proc) )) { char tbuf[32]; char *cp; int fd; char c; if (!isdigit(ent->d_name[0])) continue; sprintf(tbuf, "/proc/%s/stat", ent->d_name); fd = open(tbuf, O_RDONLY, 0); if (fd == -1) continue; memset(tbuf, '\0', sizeof tbuf); // didn't feel like checking read() read(fd, tbuf, sizeof tbuf - 1); // need 32 byte buffer at most close(fd); cp = strrchr(tbuf, ')'); if(!cp) continue; c = cp[2]; if (c=='R') { (*running)++; continue; } if (c=='D') { (*blocked)++; continue; } } closedir(proc);}/***********************************************************************/void getstat(jiff *restrict cuse, jiff *restrict cice, jiff *restrict csys, jiff *restrict cide, jiff *restrict ciow, jiff *restrict cxxx, jiff *restrict cyyy, jiff *restrict czzz, unsigned long *restrict pin, unsigned long *restrict pout, unsigned long *restrict s_in, unsigned long *restrict sout, unsigned *restrict intr, unsigned *restrict ctxt, unsigned int *restrict running, unsigned int *restrict blocked, unsigned int *restrict btime, unsigned int *restrict processes) { static int fd; unsigned long long llbuf = 0; int need_vmstat_file = 0; int need_proc_scan = 0; const char* b; buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */ if(fd){ lseek(fd, 0L, SEEK_SET); }else{ fd = open("/proc/stat", O_RDONLY, 0); if(fd == -1) crash("/proc/stat"); } read(fd,buff,BUFFSIZE-1); *intr = 0; *ciow = 0; /* not separated out until the 2.5.41 kernel */ *cxxx = 0; /* not separated out until the 2.6.0-test4 kernel */ *cyyy = 0; /* not separated out until the 2.6.0-test4 kernel */ *czzz = 0; /* not separated out until the 2.6.11 kernel */ b = strstr(buff, "cpu "); if(b) sscanf(b, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", cuse, cice, csys, cide, ciow, cxxx, cyyy, czzz); b = strstr(buff, "page "); if(b) sscanf(b, "page %lu %lu", pin, pout); else need_vmstat_file = 1; b = strstr(buff, "swap "); if(b) sscanf(b, "swap %lu %lu", s_in, sout); else need_vmstat_file = 1; b = strstr(buff, "intr "); if(b) sscanf(b, "intr %Lu", &llbuf); *intr = llbuf; b = strstr(buff, "ctxt "); if(b) sscanf(b, "ctxt %Lu", &llbuf); *ctxt = llbuf; b = strstr(buff, "btime "); if(b) sscanf(b, "btime %u", btime); b = strstr(buff, "processes "); if(b) sscanf(b, "processes %u", processes); b = strstr(buff, "procs_running "); if(b) sscanf(b, "procs_running %u", running); else need_proc_scan = 1; b = strstr(buff, "procs_blocked "); if(b) sscanf(b, "procs_blocked %u", blocked); else need_proc_scan = 1; if(need_proc_scan){ /* Linux 2.5.46 (approximately) and below */ getrunners(running, blocked); } (*running)--; // exclude vmstat itself if(need_vmstat_file){ /* Linux 2.5.40-bk4 and above */ vminfo(); *pin = vm_pgpgin; *pout = vm_pgpgout; *s_in = vm_pswpin; *sout = vm_pswpout; }}/***********************************************************************//* * Copyright 1999 by Albert Cahalan; all rights reserved. * This file may be used subject to the terms and conditions of the * GNU Library General Public License Version 2, or any later version * at your option, as published by the Free Software Foundation. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. */typedef struct mem_table_struct { const char *name; /* memory type name */ unsigned long *slot; /* slot in return struct */} mem_table_struct;static int compare_mem_table_structs(const void *a, const void *b){ return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);}/* example data, following junk, with comments added: * * MemTotal: 61768 kB old * MemFree: 1436 kB old * MemShared: 0 kB old (now always zero; not calculated) * Buffers: 1312 kB old * Cached: 20932 kB old * Active: 12464 kB new * Inact_dirty: 7772 kB new * Inact_clean: 2008 kB new * Inact_target: 0 kB new * Inact_laundry: 0 kB new, and might be missing too * HighTotal: 0 kB * HighFree: 0 kB * LowTotal: 61768 kB * LowFree: 1436 kB * SwapTotal: 122580 kB old * SwapFree: 60352 kB old * Inactive: 20420 kB 2.5.41+ * Dirty: 0 kB 2.5.41+ * Writeback: 0 kB 2.5.41+ * Mapped: 9792 kB 2.5.41+ * Slab: 4564 kB 2.5.41+ * Committed_AS: 8440 kB 2.5.41+ * PageTables: 304 kB 2.5.41+ * ReverseMaps: 5738 2.5.41+ * SwapCached: 0 kB 2.5.??+ * HugePages_Total: 220 2.5.??+ * HugePages_Free: 138 2.5.??+ * Hugepagesize: 4096 kB 2.5.??+ *//* obsolete */unsigned long kb_main_shared;/* old but still kicking -- the important stuff */unsigned long kb_main_buffers;unsigned long kb_main_cached;unsigned long kb_main_free;unsigned long kb_main_total;unsigned long kb_swap_free;unsigned long kb_swap_total;/* recently introduced */unsigned long kb_high_free;unsigned long kb_high_total;unsigned long kb_low_free;unsigned long kb_low_total;/* 2.4.xx era */unsigned long kb_active;unsigned long kb_inact_laundry;unsigned long kb_inact_dirty;unsigned long kb_inact_clean;unsigned long kb_inact_target;unsigned long kb_swap_cached; /* late 2.4 and 2.6+ only *//* derived values */unsigned long kb_swap_used;unsigned long kb_main_used;/* 2.5.41+ */unsigned long kb_writeback;unsigned long kb_slab;unsigned long nr_reversemaps;unsigned long kb_committed_as;unsigned long kb_dirty;unsigned long kb_inactive;unsigned long kb_mapped;unsigned long kb_pagetables;// seen on a 2.6.x kernel:static unsigned long kb_vmalloc_chunk;static unsigned long kb_vmalloc_total;static unsigned long kb_vmalloc_used;void meminfo(void){ char namebuf[16]; /* big enough to hold any row name */ mem_table_struct findme = { namebuf, NULL}; mem_table_struct *found; char *head; char *tail; static const mem_table_struct mem_table[] = { {"Active", &kb_active}, // important {"Buffers", &kb_main_buffers}, // important {"Cached", &kb_main_cached}, // important {"Committed_AS", &kb_committed_as}, {"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty {"HighFree", &kb_high_free}, {"HighTotal", &kb_high_total}, {"Inact_clean", &kb_inact_clean}, {"Inact_dirty", &kb_inact_dirty}, {"Inact_laundry",&kb_inact_laundry}, {"Inact_target", &kb_inact_target}, {"Inactive", &kb_inactive}, // important {"LowFree", &kb_low_free}, {"LowTotal", &kb_low_total}, {"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped {"MemFree", &kb_main_free}, // important {"MemShared", &kb_main_shared}, // important, but now gone! {"MemTotal", &kb_main_total}, // important {"PageTables", &kb_pagetables}, // kB version of vmstat nr_page_table_pages {"ReverseMaps", &nr_reversemaps}, // same as vmstat nr_page_table_pages {"Slab", &kb_slab}, // kB version of vmstat nr_slab {"SwapCached", &kb_swap_cached}, {"SwapFree", &kb_swap_free}, // important {"SwapTotal", &kb_swap_total}, // important {"VmallocChunk", &kb_vmalloc_chunk}, {"VmallocTotal", &kb_vmalloc_total}, {"VmallocUsed", &kb_vmalloc_used}, {"Writeback", &kb_writeback}, // kB version of vmstat nr_writeback }; const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct); FILE_TO_BUF(MEMINFO_FILE,meminfo_fd); kb_inactive = ~0UL; 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, mem_table, mem_table_count, sizeof(mem_table_struct), compare_mem_table_structs ); head = tail+1; if(!found) goto nextline; *(found->slot) = strtoul(head,&tail,10);nextline: tail = strchr(head, '\n'); if(!tail) break; head = tail+1; } if(!kb_low_total){ /* low==main except with large-memory support */ kb_low_total = kb_main_total;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -