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
|
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* 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>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include <poll.h>
#include "powertop.h"
/*
* Global variables, see powertop.h for comments and extern declarations.
* These are ordered by type, grouped by usage.
*/
int g_bit_depth;
int g_total_events, g_top_events;
int g_npstates, g_max_cstate, g_longest_cstate;
uint_t g_features;
uint_t g_ncpus;
uint_t g_ncpus_observed;
processorid_t *g_cpu_table;
double g_interval_length;
hrtime_t g_total_c_time;
uchar_t g_op_mode;
boolean_t g_gui;
uint_t g_observed_cpu;
event_info_t g_event_info[EVENT_NUM_MAX];
state_info_t g_cstate_info[NSTATES];
freq_state_info_t g_pstate_info[NSTATES];
cpu_power_info_t *g_cpu_power_states;
boolean_t g_turbo_supported;
boolean_t g_sig_resize;
uint_t g_argc;
char **g_argv;
static const int true = 1;
void
pt_sig_handler(int sig)
{
switch (sig) {
case SIGWINCH:
g_sig_resize = B_TRUE;
break;
}
}
int
main(int argc, char **argv)
{
double interval, interval_usr;
hrtime_t interval_start;
int index2 = 0, c, dump_count = 0;
char *endptr, key;
boolean_t root_user = B_FALSE;
struct pollfd pollset;
static struct option opts[] = {
{ "dump", 1, NULL, 'd' },
{ "time", 1, NULL, 't' },
{ "help", 0, NULL, 'h' },
{ "cpu", 1, NULL, 'c' },
{ "verbose", 0, NULL, 'v' },
{ 0, 0, NULL, 0 }
};
pt_set_progname(argv[0]);
/*
* Enumerate the system's CPUs, populate cpu_table, g_ncpus
*/
if ((g_ncpus = g_ncpus_observed = pt_enumerate_cpus()) == 0)
exit(EXIT_FAILURE);
if ((g_bit_depth = pt_get_bit_depth()) < 0)
exit(EXIT_FAILURE);
g_features = 0;
interval = interval_usr = INTERVAL_DEFAULT;
g_op_mode = PT_MODE_DEFAULT;
g_max_cstate = 0;
g_argv = NULL;
g_argc = 0;
g_observed_cpu = 0;
g_turbo_supported = B_FALSE;
g_sig_resize = B_FALSE;
g_curr_sugg = NULL;
while ((c = getopt_long(argc, argv, "d:t:hvc:", opts, &index2))
!= EOF) {
if (c == -1)
break;
switch (c) {
case 'd':
if (PT_ON_DUMP) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_DUMP;
g_gui = B_FALSE;
dump_count = (int)strtod(optarg, &endptr);
if (dump_count <= 0 || *endptr != '\0') {
pt_usage();
exit(EXIT_USAGE);
}
break;
case 't':
if (PT_ON_TIME) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_TIME;
interval = interval_usr = (double)strtod(optarg,
&endptr);
if (*endptr != '\0' || interval < 1 ||
interval > INTERVAL_MAX) {
pt_usage();
exit(EXIT_USAGE);
}
break;
case 'v':
if (PT_ON_CPU || PT_ON_VERBOSE) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_VERBOSE;
break;
case 'c':
if (PT_ON_CPU || PT_ON_VERBOSE) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_CPU;
g_observed_cpu = (uint_t)strtod(optarg, &endptr);
if (g_observed_cpu >= g_ncpus) {
pt_usage();
exit(EXIT_USAGE);
}
g_argc = 1;
g_ncpus_observed = 1;
if ((g_argv = malloc(sizeof (char *))) == NULL)
return (EXIT_FAILURE);
if ((*g_argv = malloc(sizeof (char) * 5)) == NULL)
return (EXIT_FAILURE);
(void) snprintf(*g_argv, 5, "%d\0", g_observed_cpu);
break;
case 'h':
pt_usage();
exit(EXIT_SUCCESS);
default:
pt_usage();
exit(EXIT_USAGE);
}
}
if (optind < argc) {
pt_usage();
exit(EXIT_USAGE);
}
(void) printf("%s %s\n\n", TITLE, COPYRIGHT_INTEL);
(void) printf("Collecting data for %.2f second(s) \n",
(float)interval);
/* Prepare P-state statistics */
if (pt_cpufreq_stat_prepare() == 0)
g_features |= FEATURE_PSTATE;
/* Prepare C-state statistics */
if (pt_cpuidle_stat_prepare() == 0)
g_features |= FEATURE_CSTATE;
else
/*
* PowerTop was unable to run a DTrace program,
* most likely for lack of permissions.
*/
exit(EXIT_FAILURE);
/* Prepare event statistics */
if (pt_events_stat_prepare() != -1)
g_features |= FEATURE_EVENTS;
/*
* If the system is running on battery, find out what's
* the kstat module for it
*/
pt_battery_mod_lookup();
/* Prepare turbo statistics */
if (pt_turbo_stat_prepare() == 0)
g_features |= FEATURE_TURBO;
/*
* Initialize the display.
*/
if (!PT_ON_DUMP) {
pt_display_init_curses();
pt_display_setup(B_FALSE);
(void) signal(SIGWINCH, pt_sig_handler);
pt_display_title_bar();
pt_display_status_bar();
g_gui = B_TRUE;
pollset.fd = STDIN_FILENO;
pollset.events = POLLIN;
}
/*
* Installs the initial suggestions, running as root and turning CPU
* power management ON.
*/
if (geteuid() != 0) {
pt_sugg_as_root();
} else {
root_user = B_TRUE;
pt_cpufreq_suggest();
}
while (true) {
key = 0;
if (g_sig_resize)
pt_display_resize();
interval_start = gethrtime();
if (!PT_ON_DUMP) {
if (poll(&pollset, (nfds_t)1,
(int)(interval * MILLISEC)) > 0)
(void) read(STDIN_FILENO, &key, 1);
} else {
(void) sleep((int)interval);
}
g_interval_length = (double)(gethrtime() - interval_start)
/NANOSEC;
g_top_events = 0;
g_total_events = 0;
(void) memset(g_event_info, 0,
EVENT_NUM_MAX * sizeof (event_info_t));
(void) memset(g_cstate_info, 0,
NSTATES * sizeof (state_info_t));
/* Collect idle state transition stats */
if (g_features & FEATURE_CSTATE &&
pt_cpuidle_stat_collect(g_interval_length) < 0) {
/* Reinitialize C-state statistics */
if (pt_cpuidle_stat_prepare() != 0)
exit(EXIT_FAILURE);
continue;
}
/* Collect frequency change stats */
if (g_features & FEATURE_PSTATE &&
pt_cpufreq_stat_collect(g_interval_length) < 0) {
/* Reinitialize P-state statistics */
if (pt_cpufreq_stat_prepare() != 0)
exit(EXIT_FAILURE);
continue;
}
/* Collect event statistics */
if (g_features & FEATURE_EVENTS &&
pt_events_stat_collect() < 0) {
/* Reinitialize event statistics */
if (pt_events_stat_prepare() != 0)
exit(EXIT_FAILURE);
continue;
}
/* Collect turbo statistics */
if (g_features & FEATURE_TURBO &&
pt_turbo_stat_collect() < 0)
exit(EXIT_FAILURE);
/* Show CPU power states */
pt_display_states();
/* Show wakeups events affecting PM */
if (g_features & FEATURE_EVENTS) {
pt_display_wakeups(g_interval_length);
pt_display_events(g_interval_length);
}
pt_battery_print();
if (key && !PT_ON_DUMP) {
switch (toupper(key)) {
case 'Q':
exit(EXIT_SUCCESS);
break;
case 'R':
interval = 3;
break;
}
/*
* Check if the user has activated the current
* suggestion.
*/
if (g_curr_sugg != NULL &&
toupper(key) == g_curr_sugg->key &&
g_curr_sugg->func)
g_curr_sugg->func();
}
if (dump_count)
dump_count--;
/* Exits if user requested a dump */
if (PT_ON_DUMP && !dump_count)
exit(EXIT_SUCCESS);
/* No key pressed, will suggest something */
if (!key && !dump_count)
pt_sugg_pick();
/* Refresh display */
if (!PT_ON_DUMP)
pt_display_update();
if (root_user)
pt_cpufreq_suggest();
/*
* Update the interval based on how long the CPU was in the
* longest c-state during the last snapshot. If the user
* specified an interval we skip this bit and keep it fixed.
*/
if (g_features & FEATURE_CSTATE && !PT_ON_TIME &&
g_longest_cstate > 0 &&
g_cstate_info[g_longest_cstate].events > 0) {
double deep_idle_res = (((double)
g_cstate_info[g_longest_cstate].total_time/MICROSEC
/g_ncpus)/g_cstate_info[g_longest_cstate].events);
if (deep_idle_res < INTERVAL_DEFAULT ||
(g_total_events/interval) < 1)
interval = INTERVAL_DEFAULT;
else
interval = INTERVAL_UPDATE(deep_idle_res);
} else {
/*
* Restore interval after a refresh.
*/
if (key)
interval = interval_usr;
}
}
return (EXIT_SUCCESS);
}
|