summaryrefslogtreecommitdiff
path: root/procps/powertop.c
blob: 250da817b669a7cada9bfaa61e8aeb34718b360b (plain)
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
/* vi: set sw=4 ts=4: */
/*
 * A mini 'powertop' utility:
 *   Analyze power consumption on Intel-based laptops.
 * Based on powertop 1.11.
 *
 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
 *
 * Licensed under GPLv2, see file LICENSE in this source tree.
 */

//applet:IF_POWERTOP(APPLET(powertop, _BB_DIR_BIN, _BB_SUID_DROP))

//kbuild:lib-$(CONFIG_POWERTOP) += powertop.o

//config:config POWERTOP
//config:	bool "powertop"
//config:	default y
//config:	help
//config:	  Analyze power consumption on Intel-based laptops

// XXX This should be configurable
#define ENABLE_FEATURE_POWERTOP_PROCIRQ 1

#include "libbb.h"


//#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
#define debug(fmt, ...) ((void)0)


#define BLOATY_HPET_IRQ_NUM_DETECTION 0
#define MAX_CSTATE_COUNT   8
#define IRQCOUNT           40


#define DEFAULT_SLEEP      10
#define DEFAULT_SLEEP_STR "10"

/* Frequency of the ACPI timer */
#define FREQ_ACPI          3579.545
#define FREQ_ACPI_1000     3579545

/* Max filename length of entry in /sys/devices subsystem */
#define BIG_SYSNAME_LEN    16

typedef unsigned long long ullong;

struct line {
	char *string;
	int count;
	/*int disk_count;*/
};

#if ENABLE_FEATURE_POWERTOP_PROCIRQ
struct irqdata {
	smallint active;
	int number;
	ullong count;
	char irq_desc[32];
};
#endif

struct globals {
	struct line *lines; /* the most often used member */
	int lines_cnt;
	int lines_cumulative_count;
	int maxcstate;
	unsigned total_cpus;
	smallint cant_enable_timer_stats;
#if ENABLE_FEATURE_POWERTOP_PROCIRQ
# if BLOATY_HPET_IRQ_NUM_DETECTION
	smallint scanned_timer_list;
	int percpu_hpet_start;
	int percpu_hpet_end;
# endif
	int interrupt_0;
	int total_interrupt;
	struct irqdata interrupts[IRQCOUNT];
#endif
	ullong start_usage[MAX_CSTATE_COUNT];
	ullong last_usage[MAX_CSTATE_COUNT];
	ullong start_duration[MAX_CSTATE_COUNT];
	ullong last_duration[MAX_CSTATE_COUNT];
#if ENABLE_FEATURE_USE_TERMIOS
	struct termios init_settings;
#endif
};
#define G (*ptr_to_globals)
#define INIT_G() do { \
	SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
} while (0)

#if ENABLE_FEATURE_USE_TERMIOS
static void reset_term(void)
{
	tcsetattr_stdin_TCSANOW(&G.init_settings);
}

static void sig_handler(int signo UNUSED_PARAM)
{
	reset_term();
	_exit(EXIT_FAILURE);
}
#endif

static int write_str_to_file(const char *fname, const char *str)
{
	FILE *fp = fopen_for_write(fname);
	if (!fp)
		return 1;
	fputs(str, fp);
	fclose(fp);
	return 0;
}

/* Make it more readable */
#define start_timer() write_str_to_file("/proc/timer_stats", "1\n")
#define stop_timer()  write_str_to_file("/proc/timer_stats", "0\n")

static NOINLINE void clear_lines(void)
{
	int i;
	if (G.lines) {
		for (i = 0; i < G.lines_cnt; i++)
			free(G.lines[i].string);
		free(G.lines);
		G.lines_cnt = 0;
		G.lines = NULL;
	}
}

static void update_lines_cumulative_count(void)
{
	int i;
	for (i = 0; i < G.lines_cnt; i++)
		G.lines_cumulative_count += G.lines[i].count;
}

static int line_compare(const void *p1, const void *p2)
{
	const struct line *a = p1;
	const struct line *b = p2;
	return (b->count /*+ 50 * b->disk_count*/) - (a->count /*+ 50 * a->disk_count*/);
}

static void sort_lines(void)
{
	qsort(G.lines, G.lines_cnt, sizeof(G.lines[0]), line_compare);
}

/* Save C-state usage and duration. Also update maxcstate. */
static void read_cstate_counts(ullong *usage, ullong *duration)
{
	DIR *dir;
	struct dirent *d;

	dir = opendir("/proc/acpi/processor");
	if (!dir)
		return;

	while ((d = readdir(dir)) != NULL) {
		FILE *fp;
		char buf[192];
		int level;
		int len;

		len = strlen(d->d_name); /* "CPUnn" */
		if (len < 3 || len > BIG_SYSNAME_LEN)
			continue;

		sprintf(buf, "/proc/acpi/processor/%s/power", d->d_name);
		fp = fopen_for_read(buf);
		if (!fp)
			continue;

// Example file contents:
// active state:            C0
// max_cstate:              C8
// maximum allowed latency: 2000000000 usec
// states:
//     C1:                  type[C1] promotion[--] demotion[--] latency[001] usage[00006173] duration[00000000000000000000]
//     C2:                  type[C2] promotion[--] demotion[--] latency[001] usage[00085191] duration[00000000000083024907]
//     C3:                  type[C3] promotion[--] demotion[--] latency[017] usage[01017622] duration[00000000017921327182]
		level = 0;
		while (fgets(buf, sizeof(buf), fp)) {
			char *p = strstr(buf, "age[");
			if (!p)
				continue;
			p += 4;
			usage[level] += bb_strtoull(p, NULL, 10) + 1;
			p = strstr(buf, "ation[");
			if (!p)
				continue;
			p += 6;
			duration[level] += bb_strtoull(p, NULL, 10);

			if (level >= MAX_CSTATE_COUNT-1)
				break;
			level++;
			if (level > G.maxcstate)  /* update maxcstate */
				G.maxcstate = level;
		}
		fclose(fp);
	}
	closedir(dir);
}

/* Add line and/or update count */
static void save_line(const char *string, int count)
{
	int i;
	for (i = 0; i < G.lines_cnt; i++) {
		if (strcmp(string, G.lines[i].string) == 0) {
			/* It's already there, only update count */
			G.lines[i].count += count;
			return;
		}
	}

	/* Add new line */
	G.lines = xrealloc_vector(G.lines, 4, G.lines_cnt);
	G.lines[G.lines_cnt].string = xstrdup(string);
	G.lines[G.lines_cnt].count = count;
	/*G.lines[G.lines_cnt].disk_count = 0;*/
	G.lines_cnt++;
}

#if ENABLE_FEATURE_POWERTOP_PROCIRQ
static int is_hpet_irq(const char *name)
{
	char *p;
# if BLOATY_HPET_IRQ_NUM_DETECTION
	long hpet_chan;

	/* Learn the range of existing hpet timers. This is done once */
	if (!G.scanned_timer_list) {
		FILE *fp;
		char buf[80];

		G.scanned_timer_list = true;
		fp = fopen_for_read("/proc/timer_list");
		if (!fp)
			return 0;

		while (fgets(buf, sizeof(buf), fp)) {
			p = strstr(buf, "Clock Event Device: hpet");
			if (!p)
				continue;
			p += sizeof("Clock Event Device: hpet")-1;
			if (!isdigit(*p))
				continue;
			hpet_chan = xatoi_positive(p);
			if (hpet_chan < G.percpu_hpet_start)
				G.percpu_hpet_start = hpet_chan;
			if (hpet_chan > G.percpu_hpet_end)
				G.percpu_hpet_end = hpet_chan;
		}
		fclose(fp);
	}
# endif
//TODO: optimize
	p = strstr(name, "hpet");
	if (!p)
		return 0;
	p += 4;
	if (!isdigit(*p))
		return 0;
# if BLOATY_HPET_IRQ_NUM_DETECTION
	hpet_chan = xatoi_positive(p);
	if (hpet_chan < G.percpu_hpet_start || hpet_chan > G.percpu_hpet_end)
		return 0;
# endif
	return 1;
}

/* Save new IRQ count, return delta from old one */
static int save_irq_count(int irq, ullong count)
{
	int unused = IRQCOUNT;
	int i;
	for (i = 0; i < IRQCOUNT; i++) {
		if (G.interrupts[i].active && G.interrupts[i].number == irq) {
			ullong old = G.interrupts[i].count;
			G.interrupts[i].count = count;
			return count - old;
		}
		if (!G.interrupts[i].active && unused > i)
			unused = i;
	}
	if (unused < IRQCOUNT) {
		G.interrupts[unused].active = 1;
		G.interrupts[unused].count = count;
		G.interrupts[unused].number = irq;
	}
	return count;
}

/* Read /proc/interrupts, save IRQ counts and IRQ description */
static void process_irq_counts(void)
{
	FILE *fp;
	char buf[128];

	/* Reset values */
	G.interrupt_0 = 0;
	G.total_interrupt = 0;

	fp = xfopen_for_read("/proc/interrupts");
	while (fgets(buf, sizeof(buf), fp)) {
		char irq_desc[sizeof("   <kernel IPI> : ") + sizeof(buf)];
		char *p;
		const char *name;
		int nr;
		ullong count;
		ullong delta;

		p = strchr(buf, ':');
		if (!p)
			continue;
		/*  0:  143646045  153901007   IO-APIC-edge      timer
		 *   ^
		 */
		/* Deal with non-maskable interrupts -- make up fake numbers */
		nr = -1;
		if (buf[0] != ' ' && !isdigit(buf[0])) {
//TODO: optimize
			if (strncmp(buf, "NMI:", 4) == 0)
				nr = 20000;
			if (strncmp(buf, "RES:", 4) == 0)
				nr = 20001;
			if (strncmp(buf, "CAL:", 4) == 0)
				nr = 20002;
			if (strncmp(buf, "TLB:", 4) == 0)
				nr = 20003;
			if (strncmp(buf, "TRM:", 4) == 0)
				nr = 20004;
			if (strncmp(buf, "THR:", 4) == 0)
				nr = 20005;
			if (strncmp(buf, "SPU:", 4) == 0)
				nr = 20006;
		} else {
			/* bb_strtou doesn't eat leading spaces, using strtoul */
			nr = strtoul(buf, NULL, 10);
		}
		if (nr == -1)
			continue;

		p++;
		/*  0:  143646045  153901007   IO-APIC-edge      timer
		 *    ^
		 */
		/* Sum counts for this IRQ */
		count = 0;
		while (1) {
			char *tmp;
			p = skip_whitespace(p);
			if (!isdigit(*p))
				break;
			count += bb_strtoull(p, &tmp, 10);
			p = tmp;
		}
		/*   0:  143646045  153901007   IO-APIC-edge      timer
		 * NMI:          1          2   Non-maskable interrupts
		 *                              ^
		 */
		if (nr < 20000) {
			/* Skip to the interrupt name, e.g. 'timer' */
			p = strchr(p, ' ');
			if (!p)
				continue;
			p = skip_whitespace(p);
		}

		name = p;
		strchrnul(name, '\n')[0] = '\0';
		/* Save description of the interrupt */
		if (nr >= 20000)
			sprintf(irq_desc, "   <kernel IPI> : %s", name);
		else
			sprintf(irq_desc, "    <interrupt> : %s", name);

		delta = save_irq_count(nr, count);

		/* Skip per CPU timer interrupts */
		if (is_hpet_irq(name))
			continue;

		if (nr != 0 && delta != 0)
			save_line(irq_desc, delta);

		if (nr == 0)
			G.interrupt_0 = delta;
		else
			G.total_interrupt += delta;
	}

	fclose(fp);
}
#else /* !ENABLE_FEATURE_POWERTOP_PROCIRQ */
# define process_irq_counts()  ((void)0)
#endif

static NOINLINE int process_timer_stats(void)
{
	char buf[128];
	char line[15 + 3 + 128];
	int n;
	ullong totalticks;
	FILE *fp;

	buf[0] = '\0';
	totalticks = 0;

	fp = NULL;
	if (!G.cant_enable_timer_stats)
		fp = fopen_for_read("/proc/timer_stats");
	if (fp) {
// Example file contents:
// Timer Stats Version: v0.2
// Sample period: 1.329 s
//    76,     0 swapper          hrtimer_start_range_ns (tick_sched_timer)
//    88,     0 swapper          hrtimer_start_range_ns (tick_sched_timer)
//    24,  3787 firefox          hrtimer_start_range_ns (hrtimer_wakeup)
//   46D,  1136 kondemand/1      do_dbs_timer (delayed_work_timer_fn)
// ...
//     1,  1656 Xorg             hrtimer_start_range_ns (hrtimer_wakeup)
//     1,  2159 udisks-daemon    hrtimer_start_range_ns (hrtimer_wakeup)
// 331 total events, 249.059 events/sec
		while (fgets(buf, sizeof(buf), fp)) {
			const char *count, *process, *func;
			char *p;
			int cnt;

			count = skip_whitespace(buf);
			p = strchr(count, ',');
			if (!p)
				continue;
			*p++ = '\0';
			if (strcmp(strchrnul(count, ' '), " total events") == 0)
				break;
			p = skip_whitespace(p); /* points to pid */

/* Find char ' ', then eat remaining spaces */
#define ADVANCE(p) do {           \
	(p) = strchr((p), ' ');   \
	if (!(p))                 \
		continue;         \
	*(p) = '\0';              \
	(p)++;                    \
	(p) = skip_whitespace(p); \
} while (0)
			/* Get process name */
			ADVANCE(p);
			process = p;
			/* Get function */
			ADVANCE(p);
			func = p;
#undef ADVANCE
			//if (strcmp(process, "swapper") == 0
			// && strcmp(func, "hrtimer_start_range_ns (tick_sched_timer)\n") == 0
			//) {
			//	process = "[kernel scheduler]";
			//	func = "Load balancing tick";
			//}

			if (strncmp(func, "tick_nohz_", 10) == 0)
				continue;
			if (strncmp(func, "tick_setup_sched_timer", 20) == 0)
				continue;
			//if (strcmp(process, "powertop") == 0)
			//	continue;

			if (strcmp(process, "insmod") == 0)
				process = "[kernel module]";
			if (strcmp(process, "modprobe") == 0)
				process = "[kernel module]";
			if (strcmp(process, "swapper") == 0)
				process = "<kernel core>";

			strchrnul(p, '\n')[0] = '\0';

			{
				char *tmp;
				cnt = bb_strtoull(count, &tmp, 10);
				p = tmp;
			}
			while (*p != '\0') {
				if (*p++ == 'D') /* deferred */
					goto skip;
			}

			//if (strchr(process, '['))
				sprintf(line, "%15.15s : %s", process, func);
			//else
			//	sprintf(line, "%s", process);
			save_line(line, cnt);
 skip: ;
		}
		fclose(fp);
	}

	n = 0;
#if ENABLE_FEATURE_POWERTOP_PROCIRQ
	if (strstr(buf, "total events")) {
		n = bb_strtoull(buf, NULL, 10) / G.total_cpus;
		if (n > 0 && n < G.interrupt_0) {
			sprintf(line, "    <interrupt> : %s", "extra timer interrupt");
			save_line(line, G.interrupt_0 - n);
		}
	}
#endif
	return n;
}

#ifdef __i386__
/*
 * Get information about CPU using CPUID opcode.
 */
static void cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx,
				  unsigned int *edx)
{
	/* EAX value specifies what information to return */
	__asm__(
		"	pushl %%ebx\n"     /* Save EBX */
		"	cpuid\n"
		"	movl %%ebx, %1\n"  /* Save content of EBX */
		"	popl %%ebx\n"      /* Restore EBX */
		: "=a"(*eax), /* Output */
		  "=r"(*ebx),
		  "=c"(*ecx),
		  "=d"(*edx)
		: "0"(*eax),  /* Input */
		  "1"(*ebx),
		  "2"(*ecx),
		  "3"(*edx)
		/* No clobbered registers */
	);
}
#endif

#ifdef __i386__
static NOINLINE void print_intel_cstates(void)
{
	int bios_table[8] = { 0 };
	int nbios = 0;
	DIR *cpudir;
	struct dirent *d;
	int i;
	unsigned eax, ebx, ecx, edx;

	cpudir = opendir("/sys/devices/system/cpu");
	if (!cpudir)
		return;

	/* Loop over cpuN entries */
	while ((d = readdir(cpudir)) != NULL) {
		DIR *dir;
		int len;
		char fname[sizeof("/sys/devices/system/cpu//cpuidle//desc") + 2*BIG_SYSNAME_LEN];

		len = strlen(d->d_name);
		if (len < 3 || len > BIG_SYSNAME_LEN)
			continue;

		if (!isdigit(d->d_name[3]))
			continue;

		len = sprintf(fname, "/sys/devices/system/cpu/%s/cpuidle", d->d_name);
		dir = opendir(fname);
		if (!dir)
			continue;

		/*
		 * Every C-state has its own stateN directory, that
		 * contains a 'time' and a 'usage' file.
		 */
		while ((d = readdir(dir)) != NULL) {
			FILE *fp;
			char buf[64];
			int n;

			n = strlen(d->d_name);
			if (n < 3 || n > BIG_SYSNAME_LEN)
				continue;

			sprintf(fname + len, "/%s/desc", d->d_name);
			fp = fopen_for_read(fname);
			if (fp) {
				char *p = fgets(buf, sizeof(buf), fp);
				fclose(fp);
				if (!p)
					break;
				p = strstr(p, "MWAIT ");
				if (p) {
					int pos;
					p += sizeof("MWAIT ") - 1;
					pos = (bb_strtoull(p, NULL, 16) >> 4) + 1;
					if (pos >= ARRAY_SIZE(bios_table))
						continue;
					bios_table[pos]++;
					nbios++;
				}
			}
		}
		closedir(dir);
	}
	closedir(cpudir);

	if (!nbios)
		return;

	eax = 5;
	ebx = ecx = edx = 0;
	cpuid(&eax, &ebx, &ecx, &edx);
	if (!edx || !(ecx & 1))
		return;

	printf("Your CPU supports the following C-states: ");
	i = 0;
	while (edx) {
		if (edx & 7)
			printf("C%u ", i);
		edx >>= 4;
		i++;
	}
	bb_putchar('\n');

	/* Print BIOS C-States */
	printf("Your BIOS reports the following C-states: ");
	for (i = 0; i < ARRAY_SIZE(bios_table); i++)
		if (bios_table[i])
			printf("C%u ", i);

	bb_putchar('\n');
}
#else
# define print_intel_cstates(void) ((void)0)
#endif

static void show_timerstats(void)
{
	unsigned lines;

	/* Get terminal height */
	get_terminal_width_height(STDOUT_FILENO, NULL, &lines);

	/* We don't have whole terminal just for timerstats */
	lines -= 12;

	if (!G.cant_enable_timer_stats) {
		int i, n = 0;
		char strbuf6[6];

		strbuf6[5] = '\0';
		puts("\nTop causes for wakeups:");
		for (i = 0; i < G.lines_cnt; i++) {
			if ((G.lines[i].count > 0 /*|| G.lines[i].disk_count > 0*/)
			 && n++ < lines
			) {
				/* NB: upstream powertop prints "(wakeups/sec)",
				 * we print just "(wakeup counts)".
				 */
				/*char c = ' ';
				if (G.lines[i].disk_count)
					c = 'D';*/
				smart_ulltoa5(G.lines[i].count, strbuf6, " KMGTPEZY");
				printf(/*" %5.1f%% (%s)%c  %s\n"*/
					" %5.1f%% (%s)   %s\n",
					G.lines[i].count * 100.0 / G.lines_cumulative_count,
					strbuf6, /*c,*/
					G.lines[i].string);
			}
		}
	} else {
		bb_putchar('\n');
		bb_error_msg("no stats available; run as root or"
				" enable the cpufreq_stats module");
	}
}

// Example display from powertop version 1.11
// Cn                Avg residency       P-states (frequencies)
// C0 (cpu running)        ( 0.5%)         2.00 Ghz     0.0%
// polling           0.0ms ( 0.0%)         1.67 Ghz     0.0%
// C1 mwait          0.0ms ( 0.0%)         1333 Mhz     0.1%
// C2 mwait          0.1ms ( 0.1%)         1000 Mhz    99.9%
// C3 mwait         12.1ms (99.4%)
//
// Wakeups-from-idle per second : 93.6     interval: 15.0s
// no ACPI power usage estimate available
//
// Top causes for wakeups:
//   32.4% ( 26.7)       <interrupt> : extra timer interrupt
//   29.0% ( 23.9)     <kernel core> : hrtimer_start_range_ns (tick_sched_timer)
//    9.0% (  7.5)     <kernel core> : hrtimer_start (tick_sched_timer)
//    6.5% (  5.3)       <interrupt> : ata_piix
//    5.0% (  4.1)             inetd : hrtimer_start_range_ns (hrtimer_wakeup)

//usage:#define powertop_trivial_usage
//usage:       ""
//usage:#define powertop_full_usage "\n\n"
//usage:       "Analyze power consumption on Intel-based laptops\n"

int powertop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
{
	ullong cur_usage[MAX_CSTATE_COUNT];
	ullong cur_duration[MAX_CSTATE_COUNT];
	char cstate_lines[MAX_CSTATE_COUNT + 2][64];
#if ENABLE_FEATURE_USE_TERMIOS
	struct termios new_settings;
	struct pollfd pfd[1];

	pfd[0].fd = 0;
	pfd[0].events = POLLIN;
#endif

	INIT_G();

#if ENABLE_FEATURE_POWERTOP_PROCIRQ && BLOATY_HPET_IRQ_NUM_DETECTION
	G.percpu_hpet_start = INT_MAX;
	G.percpu_hpet_end = INT_MIN;
#endif

	/* Print warning when we don't have superuser privileges */
	if (geteuid() != 0)
		bb_error_msg("run as root to collect enough information");

	/* Get number of CPUs */
	G.total_cpus = get_cpu_count();

	printf("Collecting data for "DEFAULT_SLEEP_STR" seconds\n");

#if ENABLE_FEATURE_USE_TERMIOS
	tcgetattr(0, (void *)&G.init_settings);
	memcpy(&new_settings, &G.init_settings, sizeof(new_settings));
	/* Turn on unbuffered input, turn off echoing */
	new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
	/* So we don't forget to reset term settings */
	atexit(reset_term);
	bb_signals(BB_FATAL_SIGS, sig_handler);
	tcsetattr_stdin_TCSANOW(&new_settings);
#endif

	/* Collect initial data */
	process_irq_counts();

	/* Read initial usage and duration */
	read_cstate_counts(G.start_usage, G.start_duration);

	/* Copy them to "last" */
	memcpy(G.last_usage, G.start_usage, sizeof(G.last_usage));
	memcpy(G.last_duration, G.start_duration, sizeof(G.last_duration));

	/* Display C-states */
	print_intel_cstates();

	G.cant_enable_timer_stats |= stop_timer(); /* 1 on error */

	/* The main loop */
	for (;;) {
		//double maxsleep = 0.0;
		ullong totalticks, totalevents;
		int i;

		G.cant_enable_timer_stats |= start_timer(); /* 1 on error */
#if !ENABLE_FEATURE_USE_TERMIOS
		sleep(DEFAULT_SLEEP);
#else
		if (safe_poll(pfd, 1, DEFAULT_SLEEP * 1000) > 0) {
			unsigned char c;
			if (safe_read(STDIN_FILENO, &c, 1) != 1)
				break; /* EOF/error */
			if (c == G.init_settings.c_cc[VINTR])
				break; /* ^C */
			if ((c | 0x20) == 'q')
				break;
		}
#endif
		G.cant_enable_timer_stats |= stop_timer(); /* 1 on error */

		clear_lines();
		process_irq_counts();

		/* Clear the stats */
		memset(cur_duration, 0, sizeof(cur_duration));
		memset(cur_usage, 0, sizeof(cur_usage));

		/* Read them */
		read_cstate_counts(cur_usage, cur_duration);

		/* Count totalticks and totalevents */
		totalticks = totalevents = 0;
		for (i = 0; i < MAX_CSTATE_COUNT; i++) {
			if (cur_usage[i] != 0) {
				totalticks += cur_duration[i] - G.last_duration[i];
				totalevents += cur_usage[i] - G.last_usage[i];
			}
		}

		/* Clear the screen */
		printf("\033[H\033[J");

		/* Clear C-state lines */
		memset(&cstate_lines, 0, sizeof(cstate_lines));

		if (totalevents == 0 && G.maxcstate <= 1) {
			/* This should not happen */
			strcpy(cstate_lines[0], "C-state information is not available\n");
		} else {
			double percentage;
			unsigned newticks;

			newticks = G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000 - totalticks;
			/* Handle rounding errors: do not display negative values */
			if ((int)newticks < 0)
				newticks = 0;

			sprintf(cstate_lines[0], "Cn\t\t  Avg residency\n");
			percentage = newticks * 100.0 / (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000);
			sprintf(cstate_lines[1], "C0 (cpu running)        (%4.1f%%)\n", percentage);

			/* Compute values for individual C-states */
			for (i = 0; i < MAX_CSTATE_COUNT; i++) {
				if (cur_usage[i] != 0) {
					double slept;
					slept = (cur_duration[i] - G.last_duration[i])
						/ (cur_usage[i] - G.last_usage[i] + 0.1) / FREQ_ACPI;
					percentage = (cur_duration[i] - G.last_duration[i]) * 100
						/ (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000);
					sprintf(cstate_lines[i + 2], "C%u\t\t%5.1fms (%4.1f%%)\n",
						i + 1, slept, percentage);
					//if (maxsleep < slept)
					//	maxsleep = slept;
				}
			}
		}

		for (i = 0; i < MAX_CSTATE_COUNT + 2; i++)
			if (cstate_lines[i][0])
				fputs(cstate_lines[i], stdout);

		i = process_timer_stats();
#if ENABLE_FEATURE_POWERTOP_PROCIRQ
		if (totalevents == 0) {
			/* No C-state info available, use timerstats */
			totalevents = i * G.total_cpus + G.total_interrupt;
			if (i < 0)
				totalevents += G.interrupt_0 - i;
		}
#endif
		/* Upstream powertop prints wakeups per sec per CPU,
		 * we print just raw wakeup counts.
		 */
//TODO: show real seconds (think about manual refresh)
		printf("\nWakeups-from-idle in %u seconds: %llu\n",
			DEFAULT_SLEEP,
			totalevents
		);

		update_lines_cumulative_count();
		sort_lines();
		show_timerstats();
		fflush(stdout);

		/* Clear the stats */
		memset(cur_duration, 0, sizeof(cur_duration));
		memset(cur_usage, 0, sizeof(cur_usage));

		/* Get new values */
		read_cstate_counts(cur_usage, cur_duration);

		/* Save them */
		memcpy(G.last_usage, cur_usage, sizeof(G.last_usage));
		memcpy(G.last_duration, cur_duration, sizeof(G.last_duration));
	} /* for (;;) */

	bb_putchar('\n');

	return EXIT_SUCCESS;
}