/*
* 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 <string.h>
#include <dtrace.h>
#include "powertop.h"
/*
* Buffer containing DTrace program to track CPU idle state transitions
*/
static const char *dtp_cpuidle =
":::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);"
" self->start = 0;"
" self->state = 0;"
"}";
/*
* Same as above but only for a specific CPU
*/
static const char *dtp_cpuidle_c =
":::idle-state-transition"
"/cpu == $0 &&"
" arg0 != 0/"
"{"
" self->start = timestamp;"
" self->state = arg0;"
"}"
""
":::idle-state-transition"
"/cpu == $0 &&"
" arg0 == 0 && self->start/"
"{"
" @number[self->state] = count();"
" @times[self->state] = sum(timestamp - self->start);"
" 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;
char *prog_ptr;
pt_error("cannot open dtrace library for the %s report: %s\n",
return (-1);
}
/*
* Execute different scripts (defined above) depending on
* user specified options.
*/
if (PT_ON_CPU)
prog_ptr = (char *)dtp_cpuidle_c;
else
prog_ptr = (char *)dtp_cpuidle;
return (dtrace_errno(dtp));
}
return (dtrace_errno(dtp));
}
return (dtrace_errno(dtp));
}
return (dtrace_errno(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 g_cstate_info structure.
*/
int
{
int i;
hrtime_t t = 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(dtp) != 0)
NULL) != 0)
/*
* Populate g_cstate_info with the correct amount of time spent
* in each C state and update the number of C states in g_max_cstate
*/
g_total_c_time = 0;
for (i = 0; i < NSTATES; i++) {
if (g_cstate_info[i].total_time > 0) {
if (i > g_max_cstate)
g_max_cstate = i;
if (g_cstate_info[i].last_time > t) {
t = g_cstate_info[i].last_time;
g_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
{
int i;
switch (g_bit_depth) {
case 32:
/* LINTED - alignment */
rec->dtrd_offset);
break;
case 64:
/* LINTED - alignment */
rec->dtrd_offset);
break;
}
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
}
g_total_events += n;
}
else
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
}
if (g_cstate_info[0].total_time >= n)
g_cstate_info[0].total_time -= n;
else
g_cstate_info[0].total_time = 0;
}
return (DTRACE_AGGWALK_NEXT);
}