#include #include #include #include #include #include #include "defs.h" #include "proc.h" #include "visualise.h" struct __CPULoad CPULoad = { .system=0, .user=0, .nice=0, .idle=0, .sum=0, .file="/proc/stat", .readError=0 }; struct __Options Options = { .summarize=0, .all=0, .refresh=3.0f, .mult=1, .debug=0, .verbose=0, .continuous=0 }; LIST_HEAD(Root); int ProcessCount = -1; /***************************************************************************************************\ * usage() \***************************************************************************************************/ static void usage(void) { printf("Usage: /sbin/cpu [options]\n "); printf("Simple tool monitoring cpu load\n\n"); printf("Arguments:\n"); printf(" -p, --pid=value\t\tPID of process to monitor\n"); printf(" -a, --all\t\t\tMonitor all processes\n"); printf(" -s, --summarize\t\tSummarize cpu load\n"); printf("\t\t\t\t\t\t(Shows only total cpu load\n"); printf("\t\t\t\t\t\tand running processes)\n"); printf(" -r, --refresh=value\tRefresh rate in s\n"); printf(" -c, --continuous\t\tShow CPU Load continously\n"); printf(" -v --verbose\t\tBe verbose\n"); printf(" -h, --help\t\t\tPrint this help\n\n"); } /***************************************************************************************************\ * parseCmdLn() \***************************************************************************************************/ int parseCmdLn(int argc, char *argv[]) { int next_option; struct __Process *p; char *endptr; /*short options */ const char* const short_options = "hp:sr:am:dcv"; /*long options*/ const struct option long_options[] = { { "help", 0, NULL, 'h' }, { "pid", 1, NULL, 'p' }, { "summarize", 0, NULL, 's' }, { "all", 0, NULL, 'a' }, { "refresh", 1, NULL, 'r' }, { "mult", 1, NULL, 'm' }, { "debug", 0, NULL, 'd' }, { "continuous", 0, NULL, 'c' }, { "verbose", 0, NULL, 'v' }, { NULL, 0, NULL, 0 } }; do { next_option = getopt_long (argc, argv, short_options, long_options, NULL); switch (next_option) { case 'h': usage(); exit(0); case 's': Options.summarize = 1; break; case 'a': Options.all = 1; break; case 'p': /*FIXME: */ if ( ( p = initProcess(atoi(optarg)) ) == NULL) return ERROR; list_add_tail(&p->list, &Root); break; case 'r': Options.refresh = strtod(optarg, &endptr); if (optarg == endptr) { fprintf(stderr, "cpu: Unknown refresh rate \"%s\"\n", optarg); return ERROR; } break; case 'm': Options.mult = atoi(optarg); break; case 'd': Options.debug = 1; break; case 'c': Options.continuous = 1; break; case 'v': Options.verbose = 1; break; /* Done with options. */ case -1: break; /* Something else: unexpected. */ default: return ERROR; } } while (next_option != -1); return NERROR; } /***************************************************************************************************\ * main() \***************************************************************************************************/ int main(int argc, char *argv[]) { if ( parseCmdLn(argc,argv) == ERROR ) exit(1); if ( initCPULoad() == ERROR ) exit(1); if ( initProcesses() == ERROR ) exit(1); usleep(500000); while(1) { calculateCPULoad(); updateProcesses(); calculateProcesses(); visualise(); usleep((int)(Options.refresh*1000000)); } }