CPU times are reported with 100% at full machine utilization, all cores
Costa Tsaousis (ktsaou) committed
May 8, 2014 at 02:28 UTC
52618c794c9dcb89c7080010c856186eaca0521c
1 file changed
+37
-36
apps_plugin.c
+37
-36
@@ -27,6 +27,7 @@
27
28
unsigned long long Hertz = 1;
29
30
+long processors = 1;
31
long pid_max = 32768;
32
int debug = 0;
33
@@ -94,15 +95,17 @@ void parse_args(int argc, char **argv)
95
int i = 1;
96
97
debug = 0;
97
- if(strcmp(argv[i], "debug") == 0) {
98
+ if(i < argc && strcmp(argv[i], "debug") == 0) {
99
debug = 1;
100
i++;
101
}
102
102
- update_every = atoi(argv[i++]);
103
- if(update_every == 0) {
104
- i = 1;
105
- update_every = 1;
103
+ if(i < argc) {
104
+ update_every = atoi(argv[i++]);
105
+ if(update_every == 0) {
106
+ i = 1;
107
+ update_every = 1;
108
+ }
109
}
110
111
for(; i < argc ; i++) {
@@ -707,56 +710,56 @@ void show_charts(void)
710
711
// we have something new to show
712
// update the charts
710
- fprintf(stdout, "CHART apps.cpu '' 'Applications CPU Time' 'cpu time %%' apps apps stacked 20001 %d\n", update_every);
713
+ fprintf(stdout, "CHART apps.cpu '' 'Apps CPU Time (100%% = %ld core%s)' 'cpu time %%' apps apps stacked 20001 %d\n", processors, (processors>1)?"s":"", update_every);
714
for (w = wanted_root; w ; w = w->next) {
715
if(w->target || (!w->processes && !w->exposed)) continue;
716
714
- fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz);
717
+ fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors);
718
}
719
717
- fprintf(stdout, "CHART apps.rss '' 'Applications Memory' 'MB' apps apps stacked 20002 %d\n", update_every);
720
+ fprintf(stdout, "CHART apps.rss '' 'Apps Memory' 'MB' apps apps stacked 20002 %d\n", update_every);
721
for (w = wanted_root; w ; w = w->next) {
722
if(w->target || (!w->processes && !w->exposed)) continue;
723
724
fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
725
}
726
724
- fprintf(stdout, "CHART apps.threads '' 'Applications Threads' 'threads' apps apps stacked 20005 %d\n", update_every);
727
+ fprintf(stdout, "CHART apps.threads '' 'Apps Threads' 'threads' apps apps stacked 20005 %d\n", update_every);
728
for (w = wanted_root; w ; w = w->next) {
729
if(w->target || (!w->processes && !w->exposed)) continue;
730
731
fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
732
}
733
731
- fprintf(stdout, "CHART apps.processes '' 'Applications Processes' 'processes' apps apps stacked 20004 %d\n", update_every);
734
+ fprintf(stdout, "CHART apps.processes '' 'Apps Processes' 'processes' apps apps stacked 20004 %d\n", update_every);
735
for (w = wanted_root; w ; w = w->next) {
736
if(w->target || (!w->processes && !w->exposed)) continue;
737
738
fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
739
}
740
738
- fprintf(stdout, "CHART apps.cpu_user '' 'Applications CPU User Time' 'cpu time %%' apps none stacked 20020 %d\n", update_every);
741
+ fprintf(stdout, "CHART apps.cpu_user '' 'Apps CPU User Time (100%% = %ld core%s)' 'cpu time %%' apps none stacked 20020 %d\n", processors, (processors>1)?"s":"", update_every);
742
for (w = wanted_root; w ; w = w->next) {
743
if(w->target || (!w->processes && !w->exposed)) continue;
744
742
- fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz);
745
+ fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors);
746
}
747
745
- fprintf(stdout, "CHART apps.cpu_system '' 'Applications CPU System Time' 'cpu time %%' apps none stacked 20021 %d\n", update_every);
748
+ fprintf(stdout, "CHART apps.cpu_system '' 'Apps CPU System Time (100%% = %ld core%s)' 'cpu time %%' apps none stacked 20021 %d\n", processors, (processors>1)?"s":"", update_every);
749
for (w = wanted_root; w ; w = w->next) {
750
if(w->target || (!w->processes && !w->exposed)) continue;
751
749
- fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz);
752
+ fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors);
753
}
754
752
- fprintf(stdout, "CHART apps.major_faults '' 'Applications Major Page Faults' 'page faults/s' apps apps stacked 20010 %d\n", update_every);
755
+ fprintf(stdout, "CHART apps.major_faults '' 'Apps Major Page Faults (swaps in)' 'page faults/s' apps apps stacked 20010 %d\n", update_every);
756
for (w = wanted_root; w ; w = w->next) {
757
if(w->target || (!w->processes && !w->exposed)) continue;
758
759
fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w->name);
760
}
761
759
- fprintf(stdout, "CHART apps.minor_faults '' 'Applications Minor Page Faults' 'page faults/s' apps apps stacked 20011 %d\n", update_every);
762
+ fprintf(stdout, "CHART apps.minor_faults '' 'Apps Minor Page Faults' 'page faults/s' apps apps stacked 20011 %d\n", update_every);
763
for (w = wanted_root; w ; w = w->next) {
764
if(w->target || (!w->processes && !w->exposed)) continue;
765
@@ -787,6 +790,22 @@ unsigned long long get_hertz(void)
790
return hz;
791
}
792
793
+long get_processors(void)
794
+{
795
+ char buffer[1025], *s;
796
+ int processors = 0;
797
+
798
+ FILE *fp = fopen("/proc/stat", "r");
799
+ if(!fp) return 1;
800
+
801
+ while((s = fgets(buffer, 1024, fp))) {
802
+ if(strncmp(buffer, "cpu", 3) == 0) processors++;
803
+ }
804
+ fclose(fp);
805
+ processors--;
806
+ if(processors < 1) processors = 1;
807
+ return processors;
808
+}
809
810
long get_pid_max(void)
811
{
@@ -797,29 +816,11 @@ unsigned long long usecdiff(struct timeval *now, struct timeval *last) {
816
return ((((now->tv_sec * 1000000ULL) + now->tv_usec) - ((last->tv_sec * 1000000ULL) + last->tv_usec)));
817
}
818
800
-void mysleep(unsigned long long susec)
801
-{
802
- while(susec) {
803
- struct timeval t1, t2;
804
-
805
- gettimeofday(&t1, NULL);
806
-
807
- if(debug) fprintf(stderr, "Sleeping for %llu microseconds\n", susec);
808
- usleep(susec);
809
-
810
- gettimeofday(&t2, NULL);
811
-
812
- unsigned long long diff = usecdiff(&t2, &t1);
813
-
814
- if(diff < susec) susec -= diff;
815
- else susec = 0;
816
- }
817
-}
818
-
819
int main(int argc, char **argv)
820
{
821
Hertz = get_hertz();
822
pid_max = get_pid_max();
823
+ processors = get_processors();
824
825
parse_args(argc, argv);
826
@@ -857,7 +858,7 @@ int main(int argc, char **argv)
858
if(usec < (update_every * 1000000ULL)) susec = (update_every * 1000000ULL) - usec;
859
else susec = 0;
860
860
- mysleep(susec);
861
+ usleep(susec);
862
863
bcopy(&now, &last, sizeof(struct timeval));
864
}