powertop.c revision bcde4861cca9caf5cab2b710a3241b038fec477c
/*
* Copyright 2008, Intel Corporation
* Copyright 2008, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* 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 <string.h>
#include <ctype.h>
#include <locale.h>
#include "powertop.h"
int g_ncpus;
const int true = 1;
int
{
double last_time;
char *endptr;
{ 0, 0, NULL, 0 }
};
(void) textdomain("powertop");
pt_set_progname(argv[0]);
if ((bit_depth = get_bit_depth()) < 0)
displaytime = 0.0;
dump = 0;
event_mode = ' ';
max_cstate = 0;
if (c == -1)
break;
switch (c) {
case 'd':
if (dump)
usage();
dump = 1;
usage();
break;
case 't':
if (user_interval)
usage();
user_interval = 1;
&endptr);
usage();
break;
case 'v':
if (event_mode == 'v')
usage();
event_mode = 'v';
break;
case 'h':
default:
usage();
return (EXIT_USAGE);
}
}
usage();
}
/*
* Enumerate the system's CPUs
* Populate cpu_table, g_ncpus
*/
/*
* If the system is running on battery, find out what's
* the kstat module for it
*/
/* Prepare C-state statistics */
if (ret == 0)
else
/*
* PowerTop was unable to run a DTrace program,
* most likely for lack of permissions.
*/
/* Prepare P-state statistics */
if (pt_cpufreq_stat_prepare() == 0)
/* Prepare event statistics */
if (pt_events_stat_prepare() != -1)
(void) printf(_("Collecting data for %.2f second(s) \n"),
(float)ticktime);
while (true) {
char keychar;
/*
* Sleep for a while waiting either for input (if we're not
* in dump mode) or for the timeout to elapse
*/
if (!dump)
else
top_events = 0;
total_events = 0;
0);
/* Collect idle state transition stats */
if (features & FEATURE_CSTATE &&
pt_cpuidle_stat_collect(g_interval) < 0) {
/* Reinitialize C-state statistics */
if (pt_cpuidle_stat_prepare() != 0)
reinit = 1;
}
/* Collect frequency change stats */
if (features & FEATURE_PSTATE &&
pt_cpufreq_stat_collect(g_interval) < 0) {
/* Reinitialize P-state statistics */
if (pt_cpufreq_stat_prepare() != 0)
reinit = 1;
}
/* Collect event statistics */
if (features & FEATURE_EVENTS &&
pt_events_stat_collect() < 0) {
/* Reinitialize event statistics */
if (pt_events_stat_prepare() != 0)
reinit = 1;
}
if (reinit)
continue;
/*
* Initialize curses if we're not dumping and
* haven't already done it
*/
if (!dump) {
if (!ncursesinited) {
}
}
/* Show CPU power states */
if (features & FEATURE_CSTATE)
show_cstates();
/* Show wakeups events affecting PM */
if (features & FEATURE_EVENTS) {
}
switch (keychar) {
case 'Q':
break;
case 'R':
ticktime = 3;
break;
}
displaytime = -1.0;
}
}
/* suggests PM */
if (geteuid() == 0) {
} else {
}
if (dump_count)
dump_count--;
/* Exits if user requested a dump */
if (dump && !dump_count) {
}
/* No key pressed, will suggest something */
if (!key && !dump_count)
/* Refresh display */
if (!dump) {
}
/*
* 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 (!user_interval)
if (last_time < INTERVAL_DEFAULT ||
else
/*
* Restore user specified interval after a refresh
*/
}
return (EXIT_SUCCESS);
}
void
suggest_as_root(void)
{
add_suggestion("Suggestion: run as root to get suggestions"
NULL);
}