Portál AbcLinuxu, 26. dubna 2024 05:18
free -b | grep Swap | cut -fx -d" "
jenze pokud se v tom radku meni pocet mezer diky ruznemu poctu cifer, tak to takhle proste nepujde nenapadlo by prosim nekoho neco efektivnejsiho?
[petr@soban ~]$ cat /proc/meminfo | grep Swap SwapCached: 0 kB SwapTotal: 977248 kB SwapFree: 977248 kB
/proc/swaps
[petr@soban ~]$ gawk '/SwapCached/ {print $2 }' /proc/meminfo 0
Můžete např. použít awk
nebo to nejdřív prohnat přes
tr -s ' '
free -b | grep Swap | tr -s ' ' | cut -f3 -d" "
gawk '/SwapCached/ {print $2 }' /proc/meminfoJinak i bude rychlejší:
[petr@soban ~]$ time free -b | grep Swap | tr -s ' ' | cut -f3 -d" " 0 real 0m0.009s user 0m0.003s sys 0m0.006s [petr@soban ~]$ time gawk '/SwapCached/ {print $2 }' /proc/meminfo 0 real 0m0.003s user 0m0.001s sys 0m0.003s
meminfo(); ...... printf( "%-7s %10Lu %10Lu %10Lu\n", "Swap:", S(kb_swap_total), S(kb_swap_used), S(kb_swap_free) ); ......A meminfo je:
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; 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; }
Tiskni Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.