-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowertop.c
1238 lines (1056 loc) · 30.7 KB
/
powertop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2007, Intel Corporation
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
*/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <dirent.h>
#include <libintl.h>
#include <ctype.h>
#include <assert.h>
#include <locale.h>
#include <time.h>
#include <limits.h>
#include <sys/stat.h>
#include "powertop.h"
uint64_t start_usage[8], start_duration[8];
uint64_t last_usage[8], last_duration[8];
char cnames[8][16];
double ticktime = 15.0;
int interrupt_0, total_interrupt;
int showpids = 0;
static int maxcstate = 0;
int topcstate = 0;
int dump = 0;
#define IRQCOUNT 150
struct irqdata {
int active;
int number;
uint64_t count;
char description[256];
};
struct irqdata interrupts[IRQCOUNT];
#define FREQ_ACPI 3579.545
static unsigned long FREQ;
int nostats;
struct line *lines;
int linehead;
int linesize;
int linectotal;
double last_bat_cap = 0;
double prev_bat_cap = 0;
time_t last_bat_time = 0;
time_t prev_bat_time = 0;
double displaytime = 0.0;
void push_line(char *string, int count)
{
int i;
assert(string != NULL);
for (i = 0; i < linehead; i++)
if (strcmp(string, lines[i].string) == 0) {
lines[i].count += count;
return;
}
if (linehead == linesize)
lines = realloc (lines, (linesize ? (linesize *= 2) : (linesize = 64)) * sizeof (struct line));
lines[linehead].string = strdup (string);
lines[linehead].count = count;
lines[linehead].pid[0] = 0;
linehead++;
}
void push_line_pid(char *string, int count, char *pid)
{
int i;
assert(string != NULL);
for (i = 0; i < linehead; i++)
if (strcmp(string, lines[i].string) == 0) {
lines[i].count += count;
if (pid && strcmp(lines[i].pid, pid)!=0)
lines[i].pid[0] = 0;
return;
}
if (linehead == linesize)
lines = realloc (lines, (linesize ? (linesize *= 2) : (linesize = 64)) * sizeof (struct line));
lines[linehead].string = strdup (string);
lines[linehead].count = count;
if (pid)
strcpy(lines[linehead].pid, pid);
linehead++;
}
void clear_lines(void)
{
int i;
for (i = 0; i < linehead; i++)
free (lines[i].string);
free (lines);
linehead = linesize = 0;
lines = NULL;
}
void count_lines(void)
{
uint64_t q = 0;
int i;
for (i = 0; i < linehead; i++)
q += lines[i].count;
linectotal = q;
}
int update_irq(int irq, uint64_t count, char *name)
{
int i;
int firstfree = IRQCOUNT;
if (!name)
return 0;
for (i = 0; i < IRQCOUNT; i++) {
if (interrupts[i].active && interrupts[i].number == irq) {
uint64_t oldcount;
oldcount = interrupts[i].count;
interrupts[i].count = count;
return count - oldcount;
}
if (!interrupts[i].active && firstfree > i)
firstfree = i;
}
interrupts[firstfree].active = 1;
interrupts[firstfree].count = count;
interrupts[firstfree].number = irq;
strcpy(interrupts[firstfree].description, name);
if (strcmp(name,"i8042\n")==0)
strcpy(interrupts[firstfree].description, _("PS/2 keyboard/mouse/touchpad"));
return count;
}
static int percpu_hpet_timer(char *name)
{
static int timer_list_read;
static int percpu_hpet_start = INT_MAX, percpu_hpet_end = INT_MIN;
char *c;
long hpet_chan;
if (!timer_list_read) {
char file_name[20];
char ln[80];
FILE *fp;
timer_list_read = 1;
snprintf(file_name, sizeof(file_name), "/proc/timer_list");
fp = fopen(file_name, "r");
if (fp == NULL)
return 0;
while (fgets(ln, sizeof(ln), fp) != NULL)
{
c = strstr(ln, "Clock Event Device: hpet");
if (!c)
continue;
c += 24;
if (!isdigit(c[0]))
continue;
hpet_chan = strtol(c, NULL, 10);
if (hpet_chan < percpu_hpet_start)
percpu_hpet_start = hpet_chan;
if (hpet_chan > percpu_hpet_end)
percpu_hpet_end = hpet_chan;
}
fclose(fp);
}
c = strstr(name, "hpet");
if (!c)
return 0;
c += 4;
if (!isdigit(c[0]))
return 0;
hpet_chan = strtol(c, NULL, 10);
if (percpu_hpet_start <= hpet_chan && hpet_chan <= percpu_hpet_end)
return 1;
return 0;
}
static void do_proc_irq(void)
{
FILE *file;
char line[1024];
char line2[1024];
char *name;
uint64_t delta;
interrupt_0 = 0;
total_interrupt = 0;
file = fopen("/proc/interrupts", "r");
if (!file)
return;
while (!feof(file)) {
char *c;
int nr = -1;
uint64_t count = 0;
int special = 0;
memset(line, 0, sizeof(line));
if (fgets(line, 1024, file) == NULL)
break;
c = strchr(line, ':');
if (!c)
continue;
/* deal with NMI and the like.. make up fake nrs */
if (line[0] != ' ' && (line[0] < '0' || line[0] > '9')) {
if (strncmp(line,"NMI:", 4)==0)
nr=20000;
if (strncmp(line,"RES:", 4)==0)
nr=20001;
if (strncmp(line,"CAL:", 4)==0)
nr=20002;
if (strncmp(line,"TLB:", 4)==0)
nr=20003;
if (strncmp(line,"TRM:", 4)==0)
nr=20004;
if (strncmp(line,"THR:", 4)==0)
nr=20005;
if (strncmp(line,"SPU:", 4)==0)
nr=20006;
special = 1;
} else
nr = strtoull(line, NULL, 10);
if (nr==-1)
continue;
*c = 0;
c++;
while (c && strlen(c)) {
char *newc;
count += strtoull(c, &newc, 10);
if (newc == c)
break;
c = newc;
}
c = strchr(c, ' ');
if (!c)
continue;
while (c && *c == ' ')
c++;
if (!special) {
c = strchr(c, ' ');
if (!c)
continue;
while (c && *c == ' ')
c++;
}
name = c;
delta = update_irq(nr, count, name);
c = strchr(name, '\n');
if (c)
*c = 0;
if (strcmp(name, "i8042")) {
if (special)
sprintf(line2, _(" <kernel IPI> : %s"), name);
else
sprintf(line2, _(" <interrupt> : %s"), name);
}
else
sprintf(line2, _(" <interrupt> : %s"), _("PS/2 keyboard/mouse/touchpad"));
/* skip per CPU timer interrupts */
if (percpu_hpet_timer(name))
delta = 0;
if (nr > 0 && delta > 0)
push_line(line2, delta);
if (nr==0)
interrupt_0 = delta;
else
total_interrupt += delta;
}
fclose(file);
}
static void read_data_acpi(uint64_t * usage, uint64_t * duration)
{
DIR *dir;
struct dirent *entry;
FILE *file = NULL;
char line[4096];
char *c;
int clevel = 0;
memset(usage, 0, 64);
memset(duration, 0, 64);
dir = opendir("/proc/acpi/processor");
if (!dir)
return;
while ((entry = readdir(dir))) {
if (strlen(entry->d_name) < 3)
continue;
sprintf(line, "/proc/acpi/processor/%s/power", entry->d_name);
file = fopen(line, "r");
if (!file)
continue;
clevel = 0;
while (!feof(file)) {
memset(line, 0, 4096);
if (fgets(line, 4096, file) == NULL)
break;
c = strstr(line, "age[");
if (!c)
continue;
c += 4;
usage[clevel] += 1+strtoull(c, NULL, 10);
c = strstr(line, "ation[");
if (!c)
continue;
c += 6;
duration[clevel] += strtoull(c, NULL, 10);
clevel++;
if (clevel > maxcstate)
maxcstate = clevel;
}
fclose(file);
}
closedir(dir);
}
static void read_data_cpuidle(uint64_t * usage, uint64_t * duration)
{
DIR *cpudir;
DIR *dir;
struct dirent *entry;
FILE *file = NULL;
char line[4096];
char filename[128], *f;
int len, clevel = 0;
memset(usage, 0, 64);
memset(duration, 0, 64);
cpudir = opendir("/sys/devices/system/cpu");
if (!cpudir)
return;
/* Loop over cpuN entries */
while ((entry = readdir(cpudir))) {
if (strlen(entry->d_name) < 3)
continue;
if (!isdigit(entry->d_name[3]))
continue;
len = sprintf(filename, "/sys/devices/system/cpu/%s/cpuidle",
entry->d_name);
dir = opendir(filename);
if (!dir)
return;
clevel = 0;
/* For each C-state, there is a stateX directory which
* contains a 'usage' and a 'time' (duration) file */
while ((entry = readdir(dir))) {
if (strlen(entry->d_name) < 3)
continue;
sprintf(filename + len, "/%s/desc", entry->d_name);
file = fopen(filename, "r");
if (file) {
memset(line, 0, 4096);
f = fgets(line, 4096, file);
fclose(file);
if (f == NULL)
break;
f = strstr(line, "MWAIT ");
if (f) {
f += 6;
clevel = (strtoull(f, NULL, 16)>>4) + 1;
sprintf(cnames[clevel], "C%i mwait", clevel);
} else
sprintf(cnames[clevel], "C%i\t", clevel);
f = strstr(line, "POLL IDLE");
if (f) {
clevel = 0;
sprintf(cnames[clevel], "%s\t", _("polling"));
}
f = strstr(line, "ACPI HLT");
if (f) {
clevel = 1;
sprintf(cnames[clevel], "%s\t", "C1 halt");
}
}
sprintf(filename + len, "/%s/usage", entry->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 4096);
f = fgets(line, 4096, file);
fclose(file);
if (f == NULL)
break;
usage[clevel] += 1+strtoull(line, NULL, 10);
sprintf(filename + len, "/%s/time", entry->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 4096);
f = fgets(line, 4096, file);
fclose(file);
if (f == NULL)
break;
duration[clevel] += 1+strtoull(line, NULL, 10);
clevel++;
if (clevel > maxcstate)
maxcstate = clevel;
}
closedir(dir);
}
closedir(cpudir);
}
static void read_data(uint64_t * usage, uint64_t * duration)
{
int r;
struct stat s;
/* Then check for CPUidle */
r = stat("/sys/devices/system/cpu/cpu0/cpuidle", &s);
if (!r) {
read_data_cpuidle(usage, duration);
/* perform residency calculations based on usecs */
FREQ = 1000;
return;
}
/* First, check for ACPI */
r = stat("/proc/acpi/processor", &s);
if (!r) {
read_data_acpi(usage, duration);
/* perform residency calculations based on ACPI timer */
FREQ = FREQ_ACPI;
return;
}
}
void stop_timerstats(void)
{
FILE *file;
file = fopen("/proc/timer_stats", "w");
if (!file) {
nostats = 1;
return;
}
fprintf(file, "0\n");
fclose(file);
}
void start_timerstats(void)
{
FILE *file;
file = fopen("/proc/timer_stats", "w");
if (!file) {
nostats = 1;
return;
}
fprintf(file, "1\n");
fclose(file);
}
int line_compare (const void *av, const void *bv)
{
const struct line *a = av, *b = bv;
return b->count - a->count;
}
void sort_lines(void)
{
qsort (lines, linehead, sizeof (struct line), line_compare);
}
int print_battery_proc_acpi(void)
{
DIR *dir;
struct dirent *dirent;
FILE *file;
double rate = 0;
double cap = 0;
char filename[256];
dir = opendir("/proc/acpi/battery");
if (!dir)
return 0;
while ((dirent = readdir(dir))) {
int dontcount = 0;
double voltage = 0.0;
double amperes_drawn = 0.0;
double watts_drawn = 0.0;
double amperes_left = 0.0;
double watts_left = 0.0;
char line[1024];
if (strlen(dirent->d_name) < 3)
continue;
sprintf(filename, "/proc/acpi/battery/%s/state", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 1024);
while (fgets(line, 1024, file) != NULL) {
char *c;
if (strstr(line, "present:") && strstr(line, "no"))
break;
if (strstr(line, "charging state:")
&& !strstr(line, "discharging"))
dontcount = 1;
c = strchr(line, ':');
if (!c)
continue;
c++;
if (strstr(line, "present voltage"))
voltage = strtoull(c, NULL, 10) / 1000.0;
if (strstr(line, "remaining capacity") && strstr(c, "mW"))
watts_left = strtoull(c, NULL, 10) / 1000.0;
if (strstr(line, "remaining capacity") && strstr(c, "mAh"))
amperes_left = strtoull(c, NULL, 10) / 1000.0;
if (strstr(line, "present rate") && strstr(c, "mW"))
watts_drawn = strtoull(c, NULL, 10) / 1000.0 ;
if (strstr(line, "present rate") && strstr(c, "mA"))
amperes_drawn = strtoull(c, NULL, 10) / 1000.0;
}
fclose(file);
if (!dontcount) {
rate += watts_drawn + voltage * amperes_drawn;
}
cap += watts_left + voltage * amperes_left;
}
closedir(dir);
if (prev_bat_cap - cap < 0.001 && rate < 0.001)
last_bat_time = 0;
if (!last_bat_time) {
last_bat_time = prev_bat_time = time(NULL);
last_bat_cap = prev_bat_cap = cap;
}
if (time(NULL) - last_bat_time >= 400) {
prev_bat_cap = last_bat_cap;
prev_bat_time = last_bat_time;
last_bat_time = time(NULL);
last_bat_cap = cap;
}
show_acpi_power_line(rate, cap, prev_bat_cap - cap, time(NULL) - prev_bat_time);
return 1;
}
int print_battery_proc_pmu(void)
{
char line[80];
int i;
int power_present = 0;
int num_batteries = 0;
/* unsigned rem_time_sec = 0; */
unsigned charge_mAh = 0, max_charge_mAh = 0, voltage_mV = 0;
int discharge_mA = 0;
FILE *fd;
fd = fopen("/proc/pmu/info", "r");
if (fd == NULL)
return 0;
while ( fgets(line, sizeof(line), fd) != NULL )
{
if (strncmp("AC Power", line, strlen("AC Power")) == 0)
sscanf(strchr(line, ':')+2, "%d", &power_present);
else if (strncmp("Battery count", line, strlen("Battery count")) == 0)
sscanf(strchr(line, ':')+2, "%d", &num_batteries);
}
fclose(fd);
for (i = 0; i < num_batteries; ++i)
{
char file_name[20];
int flags = 0;
/* int battery_charging, battery_full; */
/* unsigned this_rem_time_sec = 0; */
unsigned this_charge_mAh = 0, this_max_charge_mAh = 0;
unsigned this_voltage_mV = 0, this_discharge_mA = 0;
snprintf(file_name, sizeof(file_name), "/proc/pmu/battery_%d", i);
fd = fopen(file_name, "r");
if (fd == NULL)
continue;
while (fgets(line, sizeof(line), fd) != NULL)
{
if (strncmp("flags", line, strlen("flags")) == 0)
sscanf(strchr(line, ':')+2, "%x", &flags);
else if (strncmp("charge", line, strlen("charge")) == 0)
sscanf(strchr(line, ':')+2, "%d", &this_charge_mAh);
else if (strncmp("max_charge", line, strlen("max_charge")) == 0)
sscanf(strchr(line, ':')+2, "%d", &this_max_charge_mAh);
else if (strncmp("voltage", line, strlen("voltage")) == 0)
sscanf(strchr(line, ':')+2, "%d", &this_voltage_mV);
else if (strncmp("current", line, strlen("current")) == 0)
sscanf(strchr(line, ':')+2, "%d", &this_discharge_mA);
/* else if (strncmp("time rem.", line, strlen("time rem.")) == 0) */
/* sscanf(strchr(line, ':')+2, "%d", &this_rem_time_sec); */
}
fclose(fd);
if ( !(flags & 0x1) )
/* battery isn't present */
continue;
/* battery_charging = flags & 0x2; */
/* battery_full = !battery_charging && power_present; */
charge_mAh += this_charge_mAh;
max_charge_mAh += this_max_charge_mAh;
voltage_mV += this_voltage_mV;
discharge_mA += this_discharge_mA;
/* rem_time_sec += this_rem_time_sec; */
}
show_pmu_power_line(voltage_mV, charge_mAh, max_charge_mAh,
discharge_mA);
return 1;
}
void print_battery_sysfs(void)
{
DIR *dir;
struct dirent *dirent;
FILE *file;
double rate = 0;
double cap = 0;
char filename[256];
if (print_battery_proc_acpi())
return;
if (print_battery_proc_pmu())
return;
dir = opendir("/sys/class/power_supply");
if (!dir) {
return;
}
while ((dirent = readdir(dir))) {
int dontcount = 0;
double voltage = 0.0;
double amperes_drawn = 0.0;
double watts_drawn = 0.0;
double watts_left = 0.0;
char line[1024];
if (strstr(dirent->d_name, "AC"))
continue;
sprintf(filename, "/sys/class/power_supply/%s/present", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
int s;
if ((s = getc(file)) != EOF) {
if (s == 0)
break;
}
fclose(file);
sprintf(filename, "/sys/class/power_supply/%s/status", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 1024);
if (fgets(line, 1024, file) != NULL) {
if (!strstr(line, "Discharging"))
dontcount = 1;
}
fclose(file);
sprintf(filename, "/sys/class/power_supply/%s/voltage_now", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 1024);
if (fgets(line, 1024, file) != NULL) {
voltage = strtoull(line, NULL, 10) / 1000000.0;
}
fclose(file);
sprintf(filename, "/sys/class/power_supply/%s/energy_now", dirent->d_name);
file = fopen(filename, "r");
watts_left = 1;
if (!file) {
sprintf(filename, "/sys/class/power_supply/%s/charge_now", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
/* W = A * V */
watts_left = voltage;
}
memset(line, 0, 1024);
if (fgets(line, 1024, file) != NULL)
watts_left *= strtoull(line, NULL, 10) / 1000000.0;
fclose(file);
sprintf(filename, "/sys/class/power_supply/%s/current_now", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 1024);
if (fgets(line, 1024, file) != NULL) {
watts_drawn = strtoull(line, NULL, 10) / 1000000.0;
}
fclose(file);
if (!dontcount) {
rate += watts_drawn + voltage * amperes_drawn;
}
cap += watts_left;
}
closedir(dir);
if (prev_bat_cap - cap < 0.001 && rate < 0.001)
last_bat_time = 0;
if (!last_bat_time) {
last_bat_time = prev_bat_time = time(NULL);
last_bat_cap = prev_bat_cap = cap;
}
if (time(NULL) - last_bat_time >= 400) {
prev_bat_cap = last_bat_cap;
prev_bat_time = last_bat_time;
last_bat_time = time(NULL);
last_bat_cap = cap;
}
show_acpi_power_line(rate, cap, prev_bat_cap - cap, time(NULL) - prev_bat_time);
}
char cstate_lines[12][200];
void usage()
{
printf(_("Usage: powertop [OPTION...]\n"));
printf(_(" -d, --dump read wakeups once and print list of top offenders\n"));
printf(_(" -t, --time=DOUBLE default time to gather data in seconds\n"));
printf(_(" -p, --pids show pids in wakeups list\n"));
printf(_(" -h, --help Show this help message\n"));
printf(_(" -v, --version Show version information and exit\n"));
exit(0);
}
void version()
{
printf(_("powertop version %s\n"), VERSION);
exit(0);
}
int main(int argc, char **argv)
{
char line[1024];
int ncursesinited=0;
FILE *file = NULL;
uint64_t cur_usage[8], cur_duration[8];
double wakeups_per_second = 0;
setlocale (LC_ALL, "");
bindtextdomain ("powertop", "/usr/share/locale");
textdomain ("powertop");
while (1) {
static struct option opts[] = {
{ "dump", 0, NULL, 'd' },
{ "time", 1, NULL, 't' },
{ "pids", 0, NULL, 'p' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ 0, 0, NULL, 0 }
};
int index2 = 0, c;
c = getopt_long(argc, argv, "dt:phv", opts, &index2);
if (c == -1)
break;
switch (c) {
case 'd':
dump = 1;
break;
case 't':
ticktime = strtod(optarg, NULL);
break;
case 'p':
showpids = 1;
break;
case 'h':
usage();
break;
case 'v':
version();
break;
default:
;
}
}
if (!dump)
ticktime = 5.0;
system("/sbin/modprobe cpufreq_stats &> /dev/null");
read_data(&start_usage[0], &start_duration[0]);
memcpy(last_usage, start_usage, sizeof(last_usage));
memcpy(last_duration, start_duration, sizeof(last_duration));
do_proc_irq();
do_proc_irq();
do_cpufreq_stats();
count_usb_urbs();
count_usb_urbs();
memset(cur_usage, 0, sizeof(cur_usage));
memset(cur_duration, 0, sizeof(cur_duration));
printf("PowerTOP " VERSION " (C) 2007, 2008 Intel Corporation \n\n");
if (geteuid() != 0)
printf(_("PowerTOP needs to be run as root to collect enough information\n"));
printf(_("Collecting data for %i seconds \n"), (int)ticktime);
printf("\n\n");
print_intel_cstates();
stop_timerstats();
while (1) {
double maxsleep = 0.0;
int64_t totalticks;
int64_t totalevents;
fd_set rfds;
struct timeval tv;
int key;
int i = 0;
double c0 = 0;
char *c;
FD_ZERO(&rfds);
if (!dump)
FD_SET(0, &rfds);
tv.tv_sec = ticktime;
tv.tv_usec = (ticktime - tv.tv_sec) * 1000000;;
do_proc_irq();
start_timerstats();
key = select(1, &rfds, NULL, NULL, &tv);
if (key && tv.tv_sec) ticktime = ticktime - tv.tv_sec - tv.tv_usec/1000000.0;
stop_timerstats();
clear_lines();
do_proc_irq();
read_data(&cur_usage[0], &cur_duration[0]);
totalticks = 0;
totalevents = 0;
for (i = 0; i < 8; i++)
if (cur_usage[i]) {
totalticks += cur_duration[i] - last_duration[i];
totalevents += cur_usage[i] - last_usage[i];
}
if (!dump) {
if (!ncursesinited) {
initialize_curses();
ncursesinited++;
}
setup_windows();
show_title_bar();
}
memset(&cstate_lines, 0, sizeof(cstate_lines));
topcstate = -4;
if (totalevents == 0 && maxcstate <= 1) {
sprintf(cstate_lines[5],_("< Detailed C-state information is not available.>\n"));
} else {
double sleept, percentage;;
c0 = sysconf(_SC_NPROCESSORS_ONLN) * ticktime * 1000 * FREQ - totalticks;
if (c0 < 0)
c0 = 0; /* rounding errors in measurement might make c0 go slightly negative.. this is confusing */
sprintf(cstate_lines[0], _("Cn\t Avg residency\n"));
percentage = c0 * 100.0 / (sysconf(_SC_NPROCESSORS_ONLN) * ticktime * 1000 * FREQ);
sprintf(cstate_lines[1], _("C0 (cpu running) (%4.1f%%)\n"), percentage);
if (percentage > 50)
topcstate = 0;
for (i = 0; i < 8; i++)
if (cur_usage[i]) {
sleept = (cur_duration[i] - last_duration[i]) / (cur_usage[i] - last_usage[i]
+ 0.1) / FREQ;
percentage = (cur_duration[i] -
last_duration[i]) * 100 /
(sysconf(_SC_NPROCESSORS_ONLN) * ticktime * 1000 * FREQ);
if (cnames[i][0]==0)
sprintf(cnames[i],"C%i",i+1);
sprintf
(cstate_lines[2+i], _("%s\t%5.1fms (%4.1f%%)\n"),
cnames[i], sleept, percentage);
if (maxsleep < sleept)
maxsleep = sleept;
if (percentage > 50)
topcstate = i+1;
}
}
do_cpufreq_stats();
show_cstates();
/* now the timer_stats info */
memset(line, 0, sizeof(line));
totalticks = 0;
file = NULL;
if (!nostats)
file = fopen("/proc/timer_stats", "r");
while (file && !feof(file)) {