powertop.c revision 92ad8f28957c83dbe1243c7427186f176c7c4c36
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, 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"
/*
* Global variables, see powertop.h for comments and extern declarations.
* These are ordered by type, grouped by usage.
*/
double g_ticktime, g_ticktime_usr;
double g_interval;
double g_displaytime;
int g_bit_depth;
int g_total_events, g_tog_p_events;
char **g_argv;
char *optarg;
static const int true = 1;
int
{
double last_time;
char *endptr;
{ 0, 0, NULL, 0 }
};
(void) textdomain("powertop");
pt_set_progname(argv[0]);
/*
* Enumerate the system's CPUs
* Populate cpu_table, g_ncpus
*/
if ((g_bit_depth = get_bit_depth()) < 0)
g_displaytime = 0.0;
g_max_cstate = 0;
g_argc = 0;
g_observed_cpu = 0;
!= EOF) {
if (c == -1)
break;
switch (c) {
case 'd':
if (PTOP_ON_DUMP)
usage();
usage();
break;
case 't':
if (user_interval)
usage();
user_interval = 1;
&endptr);
usage();
break;
case 'v':
if (PTOP_ON_CPU || PTOP_ON_VERBOSE)
usage();
break;
case 'c':
if (PTOP_ON_CPU || PTOP_ON_VERBOSE)
usage();
if (g_observed_cpu >= g_ncpus)
usage();
g_argc = 1;
g_ncpus_observed = 1;
return (EXIT_FAILURE);
return (EXIT_FAILURE);
break;
case 'h':
default:
usage();
return (EXIT_USAGE);
}
}
usage();
}
/*
* 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)
/* Prepare turbo statistics */
if (pt_turbo_stat_prepare() == 0) {
}
(void) printf(_("Collecting data for %.2f second(s) \n"),
(float)g_ticktime);
if (!PTOP_ON_DUMP)
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 (!PTOP_ON_DUMP)
else
g_tog_p_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 (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;
/* Collect turbo statistics */
if (features & FEATURE_TURBO &&
pt_turbo_stat_collect() < 0) {
}
/*
* Initialize curses if we're not dumping and
* haven't already done it
*/
if (!PTOP_ON_DUMP) {
if (!ncursesinited) {
}
}
/* Show CPU power states */
if (features & FEATURE_CSTATE)
show_cstates();
/* Show wakeups events affecting PM */
if (features & FEATURE_EVENTS) {
}
if (key && !PTOP_ON_DUMP) {
switch (keychar) {
case 'Q':
break;
case 'R':
g_ticktime = 3;
break;
}
if (keychar == g_suggestion_key &&
g_displaytime = -1.0;
}
}
/* suggests PM */
if (geteuid() == 0) {
} else {
}
if (dump_count)
dump_count--;
/* Exits if user requested a dump */
if (PTOP_ON_DUMP && !dump_count) {
}
/* No key pressed, will suggest something */
if (!key && !dump_count)
/* Refresh display */
if (!PTOP_ON_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);
}