procps/vmstat.c

697 lines
22 KiB
C
Raw Normal View History

2002-11-28 14:37:59 +05:30
// old: "Copyright 1994 by Henry Ware <al172@yfn.ysu.edu>. Copyleft same year."
// most code copyright 2002 Albert Cahalan
2003-08-11 05:10:41 +05:30
//
// 27/05/2003 (Fabian Frederick) : Add unit conversion + interface
2003-05-31 06:08:55 +05:30
// Export proc/stat access to libproc
// Adapt vmstat helpfile
2003-06-08 22:58:06 +05:30
// 31/05/2003 (Fabian) : Add diskstat support (/libproc)
// June 2003 (Fabian) : -S <x> -s & -s -S <x> patch
// June 2003 (Fabian) : -Adding diskstat against 3.1.9, slabinfo
// -patching 'header' in disk & slab
2003-08-11 05:10:41 +05:30
// July 2003 (Fabian) : -Adding disk partition output
// -Adding disk table
// -Syncing help / usage
2002-11-28 14:37:59 +05:30
2002-02-02 04:17:29 +05:30
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <termios.h>
2002-02-02 04:17:29 +05:30
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/dir.h>
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
2002-02-02 04:17:29 +05:30
#include "c.h"
#include "nls.h"
2002-11-26 02:42:25 +05:30
#include "proc/sysinfo.h"
#include "proc/version.h"
2003-06-08 22:58:06 +05:30
2003-08-11 05:10:41 +05:30
#define UNIT_B 1
#define UNIT_k 1000
#define UNIT_K 1024
#define UNIT_m 1000000
#define UNIT_M 1048576
static unsigned long dataUnit=UNIT_K;
static char szDataUnit[3] = "K";
2003-08-11 05:10:41 +05:30
#define VMSTAT 0
#define DISKSTAT 0x00000001
#define VMSUMSTAT 0x00000002
#define SLABSTAT 0x00000004
#define PARTITIONSTAT 0x00000008
#define DISKSUMSTAT 0x00000010
2003-06-08 22:58:06 +05:30
static int statMode=VMSTAT;
2002-11-26 02:42:25 +05:30
2002-10-06 22:16:06 +05:30
static int a_option; /* "-a" means "show active/inactive" */
2002-11-09 04:59:34 +05:30
2002-11-08 13:12:54 +05:30
static unsigned sleep_time = 1;
static unsigned long num_updates;
2002-11-09 04:59:34 +05:30
static unsigned int height; // window height
2002-11-08 13:12:54 +05:30
static unsigned int moreheaders=TRUE;
2002-02-02 04:17:29 +05:30
static void __attribute__ ((__noreturn__))
usage(FILE * out)
{
fputs(USAGE_HEADER, out);
fprintf(out,
" %s [options] [delay [count]]\n", program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
fputs(_(" -a, --active active/inactive memory\n"), out);
fputs(_(" -f, --forks number of forks since boot\n"), out);
fputs(_(" -m, --slabs slabinfo\n"), out);
fputs(_(" -n, --one-header do not redisplay header\n"), out);
fputs(_(" -s, --stats event counter statistics\n"), out);
fputs(_(" -d, --disk disk statistics\n"), out);
fputs(_(" -D, --disk-sum summarize disk statistics\n"), out);
fputs(_(" -p, --partition <dev> partition specific statistics\n"), out);
fputs(_(" -S, --unit <char> define display unit\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
fputs(USAGE_VERSION, out);
fprintf(out, USAGE_MAN_TAIL("vmstat(8)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
2002-02-02 04:17:29 +05:30
}
2002-11-09 07:30:52 +05:30
#if 0
2002-11-09 04:59:34 +05:30
// produce: " 6 ", "123 ", "123k ", etc.
2002-11-26 02:42:25 +05:30
static int format_1024(unsigned long long val64, char *restrict dst){
2002-11-09 04:59:34 +05:30
unsigned oldval;
const char suffix[] = " kmgtpe";
unsigned level = 0;
unsigned val32;
if(val64 < 1000){ // special case to avoid "6.0 " when plain " 6 " would do
val32 = val64;
return sprintf(dst,"%3u ",val32);
}
while(val64 > 0xffffffffull){
level++;
val64 /= 1024;
}
val32 = val64;
while(val32 > 999){
level++;
oldval = val32;
val32 /= 1024;
}
if(val32 < 10){
unsigned fract = (oldval % 1024) * 10 / 1024;
return sprintf(dst, "%u.%u%c ", val32, fract, suffix[level]);
}
return sprintf(dst, "%3u%c ", val32, suffix[level]);
}
// produce: " 6 ", "123 ", "123k ", etc.
2002-11-26 02:42:25 +05:30
static int format_1000(unsigned long long val64, char *restrict dst){
2002-11-09 04:59:34 +05:30
unsigned oldval;
const char suffix[] = " kmgtpe";
unsigned level = 0;
unsigned val32;
if(val64 < 1000){ // special case to avoid "6.0 " when plain " 6 " would do
val32 = val64;
return sprintf(dst,"%3u ",val32);
}
while(val64 > 0xffffffffull){
level++;
val64 /= 1000;
}
val32 = val64;
while(val32 > 999){
level++;
oldval = val32;
val32 /= 1000;
}
if(val32 < 10){
unsigned fract = (oldval % 1000) / 100;
return sprintf(dst, "%u.%u%c ", val32, fract, suffix[level]);
}
return sprintf(dst, "%3u%c ", val32, suffix[level]);
}
2002-11-09 07:30:52 +05:30
#endif
2002-11-09 04:59:34 +05:30
2003-08-11 05:10:41 +05:30
////////////////////////////////////////////////////////////////////////////
2002-11-09 04:59:34 +05:30
static void new_header(void){
printf("procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----\n");
2002-11-09 07:30:52 +05:30
printf(
"%2s %2s %6s %6s %6s %6s %4s %4s %5s %5s %4s %4s %2s %2s %2s %2s\n",
"r","b",
2002-11-09 07:30:52 +05:30
"swpd", "free", a_option?"inact":"buff", a_option?"active":"cache",
"si","so",
"bi","bo",
"in","cs",
"us","sy","id","wa"
);
2002-11-09 04:59:34 +05:30
}
2003-05-31 06:08:55 +05:30
////////////////////////////////////////////////////////////////////////////
2003-06-08 22:58:06 +05:30
static unsigned long unitConvert(unsigned int size){
2003-05-31 06:08:55 +05:30
float cvSize;
2003-06-08 22:58:06 +05:30
cvSize=(float)size/dataUnit*((statMode==SLABSTAT)?1:1024);
2003-05-31 06:08:55 +05:30
return ((unsigned long) cvSize);
}
////////////////////////////////////////////////////////////////////////////
2002-11-09 04:59:34 +05:30
static void new_format(void) {
const char format[]="%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %4u %2u %2u %2u %2u\n";
2002-11-09 04:59:34 +05:30
unsigned int tog=0; /* toggle switch for cleaner code */
unsigned int i;
unsigned int hz = Hertz;
2002-11-28 14:37:59 +05:30
unsigned int running,blocked,dummy_1,dummy_2;
jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], cpu_xxx[2], cpu_yyy[2], cpu_zzz[2];
jiff duse, dsys, didl, diow, dstl, Div, divo2;
2003-01-23 11:18:27 +05:30
unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
2002-11-28 14:37:59 +05:30
unsigned int intr[2], ctxt[2];
2002-11-09 04:59:34 +05:30
unsigned int sleep_half;
2003-01-23 11:18:27 +05:30
unsigned long kb_per_page = sysconf(_SC_PAGESIZE) / 1024ul;
2002-11-09 04:59:34 +05:30
int debt = 0; // handle idle ticks running backwards
sleep_half=(sleep_time/2);
2002-11-09 07:30:52 +05:30
new_header();
2002-11-09 04:59:34 +05:30
meminfo();
2003-05-31 06:08:55 +05:30
getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl,cpu_iow,cpu_xxx,cpu_yyy,cpu_zzz,
2002-11-09 04:59:34 +05:30
pgpgin,pgpgout,pswpin,pswpout,
2002-11-28 14:37:59 +05:30
intr,ctxt,
&running,&blocked,
&dummy_1, &dummy_2);
2003-05-31 06:08:55 +05:30
2002-11-09 04:59:34 +05:30
duse= *cpu_use + *cpu_nic;
2003-09-08 07:09:49 +05:30
dsys= *cpu_sys + *cpu_xxx + *cpu_yyy;
2002-11-09 04:59:34 +05:30
didl= *cpu_idl;
diow= *cpu_iow;
dstl= *cpu_zzz;
Div= duse+dsys+didl+diow+dstl;
2002-11-09 04:59:34 +05:30
divo2= Div/2UL;
printf(format,
running, blocked,
2003-05-31 06:08:55 +05:30
unitConvert(kb_swap_used), unitConvert(kb_main_free),
unitConvert(a_option?kb_inactive:kb_main_buffers),
unitConvert(a_option?kb_active:kb_main_cached),
(unsigned)( (*pswpin * unitConvert(kb_per_page) * hz + divo2) / Div ),
(unsigned)( (*pswpout * unitConvert(kb_per_page) * hz + divo2) / Div ),
2002-11-09 04:59:34 +05:30
(unsigned)( (*pgpgin * hz + divo2) / Div ),
(unsigned)( (*pgpgout * hz + divo2) / Div ),
2002-11-28 14:37:59 +05:30
(unsigned)( (*intr * hz + divo2) / Div ),
2002-11-09 04:59:34 +05:30
(unsigned)( (*ctxt * hz + divo2) / Div ),
(unsigned)( (100*duse + divo2) / Div ),
(unsigned)( (100*dsys + divo2) / Div ),
(unsigned)( (100*didl + divo2) / Div ),
(unsigned)( (100*diow + divo2) / Div ) /* ,
(unsigned)( (100*dstl + divo2) / Div ) */
2002-11-09 04:59:34 +05:30
);
for(i=1;i<num_updates;i++) { /* \\\\\\\\\\\\\\\\\\\\ main loop ////////////////// */
sleep(sleep_time);
2002-11-09 07:30:52 +05:30
if (moreheaders && ((i%height)==0)) new_header();
2002-11-09 04:59:34 +05:30
tog= !tog;
meminfo();
2003-05-31 06:08:55 +05:30
getstat(cpu_use+tog,cpu_nic+tog,cpu_sys+tog,cpu_idl+tog,cpu_iow+tog,cpu_xxx+tog,cpu_yyy+tog,cpu_zzz+tog,
2002-11-09 04:59:34 +05:30
pgpgin+tog,pgpgout+tog,pswpin+tog,pswpout+tog,
2002-11-28 14:37:59 +05:30
intr+tog,ctxt+tog,
&running,&blocked,
&dummy_1,&dummy_2);
2003-05-31 06:08:55 +05:30
2002-11-09 04:59:34 +05:30
duse= cpu_use[tog]-cpu_use[!tog] + cpu_nic[tog]-cpu_nic[!tog];
2003-09-08 07:09:49 +05:30
dsys= cpu_sys[tog]-cpu_sys[!tog] + cpu_xxx[tog]-cpu_xxx[!tog] + cpu_yyy[tog]-cpu_yyy[!tog];
2002-11-09 04:59:34 +05:30
didl= cpu_idl[tog]-cpu_idl[!tog];
diow= cpu_iow[tog]-cpu_iow[!tog];
dstl= cpu_zzz[tog]-cpu_zzz[!tog];
2002-11-09 04:59:34 +05:30
/* idle can run backwards for a moment -- kernel "feature" */
if(debt){
didl = (int)didl + debt;
debt = 0;
}
if( (int)didl < 0 ){
debt = (int)didl;
didl = 0;
}
Div= duse+dsys+didl+diow+dstl;
2002-11-09 04:59:34 +05:30
divo2= Div/2UL;
printf(format,
running, blocked,
2003-05-31 06:08:55 +05:30
unitConvert(kb_swap_used),unitConvert(kb_main_free),
unitConvert(a_option?kb_inactive:kb_main_buffers),
unitConvert(a_option?kb_active:kb_main_cached),
(unsigned)( ( (pswpin [tog] - pswpin [!tog])*unitConvert(kb_per_page)+sleep_half )/sleep_time ), /*si*/
(unsigned)( ( (pswpout[tog] - pswpout[!tog])*unitConvert(kb_per_page)+sleep_half )/sleep_time ), /*so*/
(unsigned)( ( pgpgin [tog] - pgpgin [!tog] +sleep_half )/sleep_time ), /*bi*/
(unsigned)( ( pgpgout[tog] - pgpgout[!tog] +sleep_half )/sleep_time ), /*bo*/
(unsigned)( ( intr [tog] - intr [!tog] +sleep_half )/sleep_time ), /*in*/
(unsigned)( ( ctxt [tog] - ctxt [!tog] +sleep_half )/sleep_time ), /*cs*/
(unsigned)( (100*duse+divo2)/Div ), /*us*/
(unsigned)( (100*dsys+divo2)/Div ), /*sy*/
(unsigned)( (100*didl+divo2)/Div ), /*id*/
(unsigned)( (100*diow+divo2)/Div )/*, //wa
(unsigned)( (100*dstl+divo2)/Div ) //st */
2002-02-02 04:17:29 +05:30
);
}
}
2002-11-08 13:12:54 +05:30
2003-05-31 06:08:55 +05:30
////////////////////////////////////////////////////////////////////////////
2002-11-09 04:59:34 +05:30
2004-05-05 04:59:40 +05:30
static void diskpartition_header(const char *partition_name){
printf("%-10s %10s %10s %10s %10s\n",partition_name, _("reads "), _("read sectors"), _("writes "), _("requested writes"));
2004-05-05 04:59:40 +05:30
}
////////////////////////////////////////////////////////////////////////////
2003-08-11 07:29:53 +05:30
static int diskpartition_format(const char* partition_name){
2003-08-17 02:45:53 +05:30
FILE *fDiskstat;
struct disk_stat *disks;
struct partition_stat *partitions, *current_partition=NULL;
unsigned long ndisks, j, k, npartitions;
const char format[] = "%20u %10llu %10u %10llu\n";
2003-08-17 02:45:53 +05:30
fDiskstat=fopen("/proc/diskstats","rb");
if(!fDiskstat)
errx(EXIT_FAILURE, _("Your kernel doesn't support diskstat. (2.5.70 or above required)"));
2003-08-11 05:10:41 +05:30
fclose(fDiskstat);
ndisks=getdiskstat(&disks,&partitions);
npartitions=getpartitions_num(disks, ndisks);
for(k=0; k<npartitions; k++){
if(!strcmp(partition_name, partitions[k].partition_name)){
current_partition=&(partitions[k]);
}
}
if(!current_partition){
2011-10-06 20:32:06 +05:30
free(disks);
free(partitions);
2003-08-11 05:10:41 +05:30
return -1;
}
2003-08-11 07:29:53 +05:30
diskpartition_header(partition_name);
2003-08-11 05:10:41 +05:30
printf (format,
current_partition->reads,current_partition->reads_sectors,current_partition->writes,current_partition->requested_writes);
fflush(stdout);
free(disks);
free(partitions);
for(j=1; j<num_updates; j++){
2003-08-11 07:29:53 +05:30
if (moreheaders && ((j%height)==0)) diskpartition_header(partition_name);
2003-08-11 05:10:41 +05:30
sleep(sleep_time);
ndisks=getdiskstat(&disks,&partitions);
npartitions=getpartitions_num(disks, ndisks);
current_partition=NULL;
for(k=0; k<npartitions; k++){
if(!strcmp(partition_name, partitions[k].partition_name)){
current_partition=&(partitions[k]);
}
}
if(!current_partition){
2011-10-06 20:32:06 +05:30
free(disks);
free(partitions);
2003-08-11 05:10:41 +05:30
return -1;
}
printf (format,
current_partition->reads,current_partition->reads_sectors,current_partition->writes,current_partition->requested_writes);
fflush(stdout);
free(disks);
free(partitions);
}
2003-08-17 02:45:53 +05:30
return 0;
2003-08-11 05:10:41 +05:30
}
////////////////////////////////////////////////////////////////////////////
2004-05-05 04:59:40 +05:30
static void diskheader(void){
printf(_("disk- ------------reads------------ ------------writes----------- -----IO------\n"));
2004-05-05 04:59:40 +05:30
printf("%5s %6s %6s %7s %7s %6s %6s %7s %7s %6s %6s\n",
" ", _("total"), _("merged"),_("sectors"),_("ms"),_("total"),_("merged"),_("sectors"),_("ms"),_("cur"),_("sec"));
2004-05-05 04:59:40 +05:30
}
////////////////////////////////////////////////////////////////////////////
2003-08-11 07:29:53 +05:30
static void diskformat(void){
2003-06-08 22:58:06 +05:30
FILE *fDiskstat;
struct disk_stat *disks;
struct partition_stat *partitions;
unsigned long ndisks,i,j,k;
2004-05-05 04:59:40 +05:30
const char format[]="%-5s %6u %6u %7llu %7u %6u %6u %7llu %7u %6u %6u\n";
2003-06-08 22:58:06 +05:30
if ((fDiskstat=fopen("/proc/diskstats", "rb"))){
fclose(fDiskstat);
ndisks=getdiskstat(&disks,&partitions);
if (!moreheaders) diskheader();
2003-06-08 22:58:06 +05:30
for(k=0; k<ndisks; k++){
2003-08-11 07:29:53 +05:30
if (moreheaders && ((k%height)==0)) diskheader();
2003-06-08 22:58:06 +05:30
printf(format,
2004-05-05 04:59:40 +05:30
disks[k].disk_name,
disks[k].reads,
disks[k].merged_reads,
disks[k].reads_sectors,
disks[k].milli_reading,
disks[k].writes,
disks[k].merged_writes,
disks[k].written_sectors,
disks[k].milli_writing,
disks[k].inprogress_IO?disks[k].inprogress_IO/1000:0,
disks[k].milli_spent_IO?disks[k].milli_spent_IO/1000:0/*,
disks[i].weighted_milli_spent_IO/1000*/
);
2003-06-08 22:58:06 +05:30
fflush(stdout);
}
free(disks);
free(partitions);
for(j=1; j<num_updates; j++){
sleep(sleep_time);
ndisks=getdiskstat(&disks,&partitions);
for(i=0; i<ndisks; i++,k++){
2003-08-11 07:29:53 +05:30
if (moreheaders && ((k%height)==0)) diskheader();
2003-06-08 22:58:06 +05:30
printf(format,
disks[i].disk_name,
disks[i].reads,
disks[i].merged_reads,
disks[i].reads_sectors,
disks[i].milli_reading,
disks[i].writes,
disks[i].merged_writes,
disks[i].written_sectors,
disks[i].milli_writing,
disks[i].inprogress_IO?disks[i].inprogress_IO/1000:0,
disks[i].milli_spent_IO?disks[i].milli_spent_IO/1000:0/*,
disks[i].weighted_milli_spent_IO/1000*/
);
fflush(stdout);
}
free(disks);
free(partitions);
}
}else
errx(EXIT_FAILURE, _("Your kernel doesn't support diskstat (2.5.70 or above required)"));
2003-06-08 22:58:06 +05:30
}
////////////////////////////////////////////////////////////////////////////
2004-05-05 04:59:40 +05:30
static void slabheader(void){
printf("%-24s %6s %6s %6s %6s\n",_("Cache"),_("Num"), _("Total"), _("Size"), _("Pages"));
2004-05-05 04:59:40 +05:30
}
////////////////////////////////////////////////////////////////////////////
2003-08-11 07:29:53 +05:30
static void slabformat (void){
2003-06-08 22:58:06 +05:30
FILE *fSlab;
struct slab_cache *slabs;
unsigned long nSlab,i,j,k;
const char format[]="%-24s %6u %6u %6u %6u\n";
fSlab=fopen("/proc/slabinfo", "rb");
if(!fSlab){
warnx(_("Your kernel doesn't support slabinfo or your permissions are insufficient."));
2003-06-08 22:58:06 +05:30
return;
}
if (!moreheaders) slabheader();
2003-06-08 22:58:06 +05:30
nSlab = getslabinfo(&slabs);
for(k=0; k<nSlab; k++){
2003-08-11 07:29:53 +05:30
if (moreheaders && ((k%height)==0)) slabheader();
2003-06-08 22:58:06 +05:30
printf(format,
slabs[k].name,
slabs[k].active_objs,
slabs[k].num_objs,
slabs[k].objsize,
slabs[k].objperslab
);
}
free(slabs);
for(j=1,k=1; j<num_updates; j++) {
sleep(sleep_time);
nSlab = getslabinfo(&slabs);
for(i=0; i<nSlab; i++,k++){
2003-08-11 07:29:53 +05:30
if (moreheaders && ((k%height)==0)) slabheader();
2003-06-08 22:58:06 +05:30
printf(format,
slabs[i].name,
slabs[i].active_objs,
slabs[i].num_objs,
slabs[i].objsize,
slabs[i].objperslab
);
}
free(slabs);
}
2011-10-06 20:32:06 +05:30
free(fSlab);
2003-06-08 22:58:06 +05:30
}
////////////////////////////////////////////////////////////////////////////
2003-08-11 05:10:41 +05:30
static void disksum_format(void) {
FILE *fDiskstat;
struct disk_stat *disks;
struct partition_stat *partitions;
int ndisks, i;
unsigned long reads, merged_reads, read_sectors, milli_reading, writes,
merged_writes, written_sectors, milli_writing, inprogress_IO,
milli_spent_IO, weighted_milli_spent_IO;
reads=merged_reads=read_sectors=milli_reading=writes=merged_writes= \
written_sectors=milli_writing=inprogress_IO=milli_spent_IO= \
weighted_milli_spent_IO=0;
if ((fDiskstat=fopen("/proc/diskstats", "rb"))){
fclose(fDiskstat);
ndisks=getdiskstat(&disks, &partitions);
printf(_("%13d disks \n"), ndisks);
printf(_("%13d partitions \n"), getpartitions_num(disks, ndisks));
2003-08-11 05:10:41 +05:30
for(i=0; i<ndisks; i++){
reads+=disks[i].reads;
merged_reads+=disks[i].merged_reads;
read_sectors+=disks[i].reads_sectors;
milli_reading+=disks[i].milli_reading;
writes+=disks[i].writes;
merged_writes+=disks[i].merged_writes;
written_sectors+=disks[i].written_sectors;
milli_writing+=disks[i].milli_writing;
inprogress_IO+=disks[i].inprogress_IO?disks[i].inprogress_IO/1000:0;
milli_spent_IO+=disks[i].milli_spent_IO?disks[i].milli_spent_IO/1000:0;
}
printf(_("%13lu total reads\n"),reads);
printf(_("%13lu merged reads\n"),merged_reads);
printf(_("%13lu read sectors\n"),read_sectors);
printf(_("%13lu milli reading\n"),milli_reading);
printf(_("%13lu writes\n"),writes);
printf(_("%13lu merged writes\n"),merged_writes);
printf(_("%13lu written sectors\n"),written_sectors);
printf(_("%13lu milli writing\n"),milli_writing);
printf(_("%13lu inprogress IO\n"),inprogress_IO);
printf(_("%13lu milli spent IO\n"),milli_spent_IO);
2003-08-11 05:10:41 +05:30
free(disks);
free(partitions);
}
}
////////////////////////////////////////////////////////////////////////////
2002-11-28 14:37:59 +05:30
static void sum_format(void) {
unsigned int running, blocked, btime, processes;
jiff cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy, cpu_zzz;
2003-01-23 11:18:27 +05:30
unsigned long pgpgin, pgpgout, pswpin, pswpout;
2002-11-28 14:37:59 +05:30
unsigned int intr, ctxt;
meminfo();
2003-05-31 06:08:55 +05:30
getstat(&cpu_use, &cpu_nic, &cpu_sys, &cpu_idl,
&cpu_iow, &cpu_xxx, &cpu_yyy, &cpu_zzz,
2002-11-28 14:37:59 +05:30
&pgpgin, &pgpgout, &pswpin, &pswpout,
&intr, &ctxt,
&running, &blocked,
&btime, &processes);
2003-05-31 06:08:55 +05:30
printf(_("%13lu %s total memory\n"), unitConvert(kb_main_total),szDataUnit);
printf(_("%13lu %s used memory\n"), unitConvert(kb_main_used),szDataUnit);
printf(_("%13lu %s active memory\n"), unitConvert(kb_active),szDataUnit);
printf(_("%13lu %s inactive memory\n"), unitConvert(kb_inactive),szDataUnit);
printf(_("%13lu %s free memory\n"), unitConvert(kb_main_free),szDataUnit);
printf(_("%13lu %s buffer memory\n"), unitConvert(kb_main_buffers),szDataUnit);
printf(_("%13lu %s swap cache\n"), unitConvert(kb_main_cached),szDataUnit);
printf(_("%13lu %s total swap\n"), unitConvert(kb_swap_total),szDataUnit);
printf(_("%13lu %s used swap\n"), unitConvert(kb_swap_used),szDataUnit);
printf(_("%13lu %s free swap\n"), unitConvert(kb_swap_free),szDataUnit);
printf(_("%13Lu non-nice user cpu ticks\n"), cpu_use);
printf(_("%13Lu nice user cpu ticks\n"), cpu_nic);
printf(_("%13Lu system cpu ticks\n"), cpu_sys);
printf(_("%13Lu idle cpu ticks\n"), cpu_idl);
printf(_("%13Lu IO-wait cpu ticks\n"), cpu_iow);
printf(_("%13Lu IRQ cpu ticks\n"), cpu_xxx);
printf(_("%13Lu softirq cpu ticks\n"), cpu_yyy);
printf(_("%13Lu stolen cpu ticks\n"), cpu_zzz);
printf(_("%13lu pages paged in\n"), pgpgin);
printf(_("%13lu pages paged out\n"), pgpgout);
printf(_("%13lu pages swapped in\n"), pswpin);
printf(_("%13lu pages swapped out\n"), pswpout);
printf(_("%13u interrupts\n"), intr);
printf(_("%13u CPU context switches\n"), ctxt);
printf(_("%13u boot time\n"), btime);
printf(_("%13u forks\n"), processes);
2002-11-28 14:37:59 +05:30
}
2003-06-08 22:58:06 +05:30
////////////////////////////////////////////////////////////////////////////
2002-11-28 14:37:59 +05:30
static void fork_format(void) {
unsigned int running, blocked, btime, processes;
jiff cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy, cpu_zzz;
2003-01-23 11:18:27 +05:30
unsigned long pgpgin, pgpgout, pswpin, pswpout;
2002-11-28 14:37:59 +05:30
unsigned int intr, ctxt;
getstat(&cpu_use, &cpu_nic, &cpu_sys, &cpu_idl,
&cpu_iow, &cpu_xxx, &cpu_yyy, &cpu_zzz,
2002-11-28 14:37:59 +05:30
&pgpgin, &pgpgout, &pswpin, &pswpout,
&intr, &ctxt,
&running, &blocked,
&btime, &processes);
2003-05-31 06:08:55 +05:30
printf(_("%13u forks\n"), processes);
2002-11-28 14:37:59 +05:30
}
2003-06-08 22:58:06 +05:30
////////////////////////////////////////////////////////////////////////////
2002-11-28 14:37:59 +05:30
2002-11-09 04:59:34 +05:30
static int winhi(void) {
struct winsize win;
int rows = 24;
if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_row > 0)
rows = win.ws_row;
return rows;
}
2003-06-08 22:58:06 +05:30
////////////////////////////////////////////////////////////////////////////
2002-11-09 04:59:34 +05:30
2002-11-08 13:12:54 +05:30
int main(int argc, char *argv[]) {
char *partition = NULL;
int c;
static const struct option longopts[] = {
{"active", no_argument, NULL, 'a'},
{"forks", no_argument, NULL, 'f'},
{"slabs", no_argument, NULL, 'm'},
{"one-header", no_argument, NULL, 'n'},
{"stats", no_argument, NULL, 's'},
{"disk", no_argument, NULL, 'd'},
{"disk-sum", no_argument, NULL, 'D'},
{"partition", required_argument, NULL, 'p'},
{"unit", required_argument, NULL, 'S'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
while((c = getopt_long(argc, argv, "afmnsdDp:S:hV", longopts, NULL)) != EOF) switch(c) {
2002-11-08 13:12:54 +05:30
case 'V':
printf(PROCPS_NG_VERSION);
return EXIT_SUCCESS;
case 'h':
usage(stdout);
2003-06-08 22:58:06 +05:30
case 'd':
statMode |= DISKSTAT;
break;
2002-11-08 13:12:54 +05:30
case 'a':
/* active/inactive mode */
a_option=1;
break;
2002-11-28 14:37:59 +05:30
case 'f':
// FIXME: check for conflicting args
fork_format();
exit(0);
2003-06-08 22:58:06 +05:30
case 'm':
statMode |= SLABSTAT;
2003-06-08 22:58:06 +05:30
break;
2003-08-11 05:10:41 +05:30
case 'D':
statMode |= DISKSUMSTAT;
break;
2002-11-08 13:12:54 +05:30
case 'n':
/* print only one header */
moreheaders=FALSE;
break;
2003-08-11 05:10:41 +05:30
case 'p':
statMode |= PARTITIONSTAT;
partition = optarg;
if (memcmp(partition, "/dev/", 5) == 0) partition += 5;
2003-08-11 05:10:41 +05:30
break;
2003-05-31 06:08:55 +05:30
case 'S':
switch(optarg[0]) {
case 'b': case 'B': dataUnit = UNIT_B; break;
case 'k': dataUnit = UNIT_k; break;
case 'K': dataUnit = UNIT_K; break;
case 'm': dataUnit = UNIT_m; break;
case 'M': dataUnit = UNIT_M; break;
default:
errx(EXIT_FAILURE, _("-S requires k, K, m or M (default is kb)"));
}
szDataUnit[0] = optarg[0];
2003-05-31 06:08:55 +05:30
break;
2002-11-28 14:37:59 +05:30
case 's':
statMode |= VMSUMSTAT;
2003-06-08 22:58:06 +05:30
break;
2002-11-08 13:12:54 +05:30
default:
/* no other aguments defined yet. */
usage(stderr);
}
if (optind < argc) {
if ((sleep_time = atoi(argv[optind++])) == 0)
usage(stderr);
num_updates = ULONG_MAX;
2002-11-08 13:12:54 +05:30
}
if (optind < argc)
num_updates = atol(argv[optind++]);
if (optind < argc)
usage(stderr);
2002-11-08 13:12:54 +05:30
if (moreheaders) {
int tmp=winhi()-3;
height=((tmp>0)?tmp:22);
}
setlinebuf(stdout);
2003-06-08 22:58:06 +05:30
switch(statMode){
2003-08-11 05:10:41 +05:30
case(VMSTAT): new_format();
break;
case(VMSUMSTAT): sum_format();
break;
2003-08-11 07:29:53 +05:30
case(DISKSTAT): diskformat();
2003-08-11 05:10:41 +05:30
break;
2003-08-11 07:29:53 +05:30
case(PARTITIONSTAT): if(diskpartition_format(partition)==-1)
printf(_("Partition was not found\n"));
2003-08-11 05:10:41 +05:30
break;
2003-08-11 07:29:53 +05:30
case(SLABSTAT): slabformat();
2003-08-11 05:10:41 +05:30
break;
case(DISKSUMSTAT): disksum_format();
break;
default: usage(stderr);
2003-08-11 05:10:41 +05:30
break;
2003-06-08 22:58:06 +05:30
}
2002-11-08 13:12:54 +05:30
return 0;
}