cpuidle.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 <string.h>
#include <dtrace.h>
#include "powertop.h"
static dtrace_hdl_t *g_dtp;
/*
* Buffer containing DTrace program to track CPU idle state transitions
*/
static const char *pt_cpuidle_dtrace_prog =
":::idle-state-transition"
"/arg0 != 0/"
"{"
" self->start = timestamp;"
" self->state = arg0;"
"}"
""
":::idle-state-transition"
"/arg0 == 0 && self->start/"
"{"
" @number[self->state] = count();"
" @times[self->state] = sum((timestamp - self->start)/1000000);"
" self->start = 0;"
" self->state = 0;"
"}";
static int pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *, void *);
/*
* Perform setup necessary to track CPU idle state transitions
*/
int
pt_cpuidle_stat_prepare(void)
{
int err;
return (-1);
}
return (dtrace_errno(g_dtp));
}
return (dtrace_errno(g_dtp));
}
}
}
}
return (dtrace_errno(g_dtp));
}
return (dtrace_errno(g_dtp));
}
return (0);
}
/*
* The DTrace probes have been enabled, and are tracking CPU idle state
* transitions. Take a snapshot of the aggregations, and invoke the aggregation
* walker to process any records. The walker does most of the accounting work
* chalking up time spent into the cstate_info structure.
*/
int
pt_cpuidle_stat_collect(double interval)
{
int i;
hrtime_t t = 0;
/*
* Zero out the interval time reported by DTrace for
* this interval
*/
for (i = 0; i < NSTATES; i++) {
cstate_info[i].total_time = 0;
cstate_info[i].events = 0;
}
/*
* Assume that all the time spent in this interval will
* be the default "0" state. The DTrace walker will reallocate
* time out of the default bucket as it processes aggregation
* records for time spent in other states.
*/
return (-1);
if (dtrace_aggregate_snap(g_dtp) != 0)
NULL) != 0)
/*
* Populate cstate_info with the correct amount of time spent
* in each C state and update the number of C states in max_cstate
*/
total_c_time = 0;
for (i = 0; i < NSTATES; i++) {
if (cstate_info[i].total_time > 0) {
if (i > max_cstate)
max_cstate = i;
if (cstate_info[i].last_time > t) {
t = cstate_info[i].last_time;
longest_cstate = i;
}
}
}
return (0);
}
/*
* DTrace aggregation walker that sorts through a snapshot of data records
* collected during firings of the idle-state-transition probe.
*
* XXX A way of querying the current idle state for a CPU is needed in addition
* to logic similar to that in cpufreq.c
*/
/*ARGSUSED*/
static int
{
uint64_t n = 0;
int i;
/* LINTED - alignment */
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
}
total_events += n;
}
else
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
}
if (cstate_info[0].total_time >= n)
cstate_info[0].total_time -= n;
}
return (DTRACE_AGGWALK_NEXT);
}