1b47b5b34SRafael Vanoni /* 2b47b5b34SRafael Vanoni * Copyright 2009, Intel Corporation 3b47b5b34SRafael Vanoni * Copyright 2009, Sun Microsystems, Inc 4b47b5b34SRafael Vanoni * 5b47b5b34SRafael Vanoni * This file is part of PowerTOP 6b47b5b34SRafael Vanoni * 7b47b5b34SRafael Vanoni * This program file is free software; you can redistribute it and/or modify it 8b47b5b34SRafael Vanoni * under the terms of the GNU General Public License as published by the 9b47b5b34SRafael Vanoni * Free Software Foundation; version 2 of the License. 10b47b5b34SRafael Vanoni * 11b47b5b34SRafael Vanoni * This program is distributed in the hope that it will be useful, but WITHOUT 12b47b5b34SRafael Vanoni * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13b47b5b34SRafael Vanoni * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14b47b5b34SRafael Vanoni * for more details. 15b47b5b34SRafael Vanoni * 16b47b5b34SRafael Vanoni * You should have received a copy of the GNU General Public License 17b47b5b34SRafael Vanoni * along with this program in a file named COPYING; if not, write to the 18b47b5b34SRafael Vanoni * Free Software Foundation, Inc., 19b47b5b34SRafael Vanoni * 51 Franklin Street, Fifth Floor, 20b47b5b34SRafael Vanoni * Boston, MA 02110-1301 USA 21b47b5b34SRafael Vanoni * 22b47b5b34SRafael Vanoni * Authors: 23b47b5b34SRafael Vanoni * Arjan van de Ven <arjan@linux.intel.com> 24b47b5b34SRafael Vanoni * Eric C Saxe <eric.saxe@sun.com> 25b47b5b34SRafael Vanoni * Aubrey Li <aubrey.li@intel.com> 26b47b5b34SRafael Vanoni */ 27b47b5b34SRafael Vanoni 28b47b5b34SRafael Vanoni /* 29b47b5b34SRafael Vanoni * GPL Disclaimer 30b47b5b34SRafael Vanoni * 31b47b5b34SRafael Vanoni * For the avoidance of doubt, except that if any license choice other 32b47b5b34SRafael Vanoni * than GPL or LGPL is available it will apply instead, Sun elects to 33b47b5b34SRafael Vanoni * use only the General Public License version 2 (GPLv2) at this time 34b47b5b34SRafael Vanoni * for any software where a choice of GPL license versions is made 35b47b5b34SRafael Vanoni * available with the language indicating that GPLv2 or any later 36b47b5b34SRafael Vanoni * version may be used, or where a choice of which version of the GPL 37b47b5b34SRafael Vanoni * is applied is otherwise unspecified. 38b47b5b34SRafael Vanoni */ 39b47b5b34SRafael Vanoni 40b47b5b34SRafael Vanoni #include <string.h> 41b47b5b34SRafael Vanoni #include <dtrace.h> 42b47b5b34SRafael Vanoni #include "powertop.h" 43b47b5b34SRafael Vanoni 44*636423dbSRafael Vanoni #define S2NS(x) ((x) * (NANOSEC)) 45*636423dbSRafael Vanoni 46b47b5b34SRafael Vanoni static dtrace_hdl_t *dtp; 47b47b5b34SRafael Vanoni 48b47b5b34SRafael Vanoni /* 49b47b5b34SRafael Vanoni * Buffer containing DTrace program to track CPU idle state transitions 50b47b5b34SRafael Vanoni */ 51b47b5b34SRafael Vanoni static const char *dtp_cpuidle = 52b47b5b34SRafael Vanoni ":::idle-state-transition" 53b47b5b34SRafael Vanoni "/arg0 != 0/" 54b47b5b34SRafael Vanoni "{" 55b47b5b34SRafael Vanoni " self->start = timestamp;" 56b47b5b34SRafael Vanoni " self->state = arg0;" 57b47b5b34SRafael Vanoni "}" 58b47b5b34SRafael Vanoni "" 59b47b5b34SRafael Vanoni ":::idle-state-transition" 60b47b5b34SRafael Vanoni "/arg0 == 0 && self->start/" 61b47b5b34SRafael Vanoni "{" 62b47b5b34SRafael Vanoni " @number[self->state] = count();" 63*636423dbSRafael Vanoni " @times[self->state] = sum(timestamp - self->start);" 64b47b5b34SRafael Vanoni " self->start = 0;" 65b47b5b34SRafael Vanoni " self->state = 0;" 66b47b5b34SRafael Vanoni "}"; 67b47b5b34SRafael Vanoni 68b47b5b34SRafael Vanoni /* 69b47b5b34SRafael Vanoni * Same as above but only for a specific CPU 70b47b5b34SRafael Vanoni */ 71b47b5b34SRafael Vanoni static const char *dtp_cpuidle_c = 72b47b5b34SRafael Vanoni ":::idle-state-transition" 73b47b5b34SRafael Vanoni "/cpu == $0 &&" 74b47b5b34SRafael Vanoni " arg0 != 0/" 75b47b5b34SRafael Vanoni "{" 76b47b5b34SRafael Vanoni " self->start = timestamp;" 77b47b5b34SRafael Vanoni " self->state = arg0;" 78b47b5b34SRafael Vanoni "}" 79b47b5b34SRafael Vanoni "" 80b47b5b34SRafael Vanoni ":::idle-state-transition" 81b47b5b34SRafael Vanoni "/cpu == $0 &&" 82b47b5b34SRafael Vanoni " arg0 == 0 && self->start/" 83b47b5b34SRafael Vanoni "{" 84b47b5b34SRafael Vanoni " @number[self->state] = count();" 85*636423dbSRafael Vanoni " @times[self->state] = sum(timestamp - self->start);" 86b47b5b34SRafael Vanoni " self->start = 0;" 87b47b5b34SRafael Vanoni " self->state = 0;" 88b47b5b34SRafael Vanoni "}"; 89b47b5b34SRafael Vanoni 90b47b5b34SRafael Vanoni static int pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *, void *); 91b47b5b34SRafael Vanoni 92b47b5b34SRafael Vanoni /* 93b47b5b34SRafael Vanoni * Perform setup necessary to track CPU idle state transitions 94b47b5b34SRafael Vanoni */ 95b47b5b34SRafael Vanoni int 96b47b5b34SRafael Vanoni pt_cpuidle_stat_prepare(void) 97b47b5b34SRafael Vanoni { 98b47b5b34SRafael Vanoni dtrace_prog_t *prog; 99b47b5b34SRafael Vanoni dtrace_proginfo_t info; 100b47b5b34SRafael Vanoni dtrace_optval_t statustime; 101b47b5b34SRafael Vanoni int err; 102b47b5b34SRafael Vanoni char *prog_ptr; 103b47b5b34SRafael Vanoni 104b47b5b34SRafael Vanoni if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) { 105b47b5b34SRafael Vanoni pt_error("%s : cannot open dtrace library: %s\n", __FILE__, 106b47b5b34SRafael Vanoni dtrace_errmsg(NULL, err)); 107b47b5b34SRafael Vanoni return (-1); 108b47b5b34SRafael Vanoni } 109b47b5b34SRafael Vanoni 110b47b5b34SRafael Vanoni /* 111b47b5b34SRafael Vanoni * Execute different scripts (defined above) depending on 112b47b5b34SRafael Vanoni * user specified options. 113b47b5b34SRafael Vanoni */ 114*636423dbSRafael Vanoni if (PT_ON_CPU) 115b47b5b34SRafael Vanoni prog_ptr = (char *)dtp_cpuidle_c; 116b47b5b34SRafael Vanoni else 117b47b5b34SRafael Vanoni prog_ptr = (char *)dtp_cpuidle; 118b47b5b34SRafael Vanoni 119b47b5b34SRafael Vanoni if ((prog = dtrace_program_strcompile(dtp, prog_ptr, 120b47b5b34SRafael Vanoni DTRACE_PROBESPEC_NAME, 0, g_argc, g_argv)) == NULL) { 121b47b5b34SRafael Vanoni pt_error("%s : C-State DTrace probes unavailable\n", __FILE__); 122b47b5b34SRafael Vanoni return (dtrace_errno(dtp)); 123b47b5b34SRafael Vanoni } 124b47b5b34SRafael Vanoni 125b47b5b34SRafael Vanoni if (dtrace_program_exec(dtp, prog, &info) == -1) { 126b47b5b34SRafael Vanoni pt_error("%s : failed to enable C State probes\n", __FILE__); 127b47b5b34SRafael Vanoni return (dtrace_errno(dtp)); 128b47b5b34SRafael Vanoni } 129b47b5b34SRafael Vanoni 130b47b5b34SRafael Vanoni if (dtrace_setopt(dtp, "aggsize", "128k") == -1) { 131b47b5b34SRafael Vanoni pt_error("%s : failed to set C-state 'aggsize'\n", __FILE__); 132b47b5b34SRafael Vanoni } 133b47b5b34SRafael Vanoni 134b47b5b34SRafael Vanoni if (dtrace_setopt(dtp, "aggrate", "0") == -1) { 135b47b5b34SRafael Vanoni pt_error("%s : failed to set C-state'aggrate'\n", __FILE__); 136b47b5b34SRafael Vanoni } 137b47b5b34SRafael Vanoni 138b47b5b34SRafael Vanoni if (dtrace_setopt(dtp, "aggpercpu", 0) == -1) { 139b47b5b34SRafael Vanoni pt_error("%s : failed to set C-state 'aggpercpu'\n", __FILE__); 140b47b5b34SRafael Vanoni } 141b47b5b34SRafael Vanoni 142b47b5b34SRafael Vanoni if (dtrace_go(dtp) != 0) { 143b47b5b34SRafael Vanoni pt_error("%s : failed to start C-state observation", __FILE__); 144b47b5b34SRafael Vanoni return (dtrace_errno(dtp)); 145b47b5b34SRafael Vanoni } 146b47b5b34SRafael Vanoni 147b47b5b34SRafael Vanoni if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) { 148b47b5b34SRafael Vanoni pt_error("%s : failed to get C-state 'statusrate'\n", __FILE__); 149b47b5b34SRafael Vanoni return (dtrace_errno(dtp)); 150b47b5b34SRafael Vanoni } 151b47b5b34SRafael Vanoni 152b47b5b34SRafael Vanoni return (0); 153b47b5b34SRafael Vanoni } 154b47b5b34SRafael Vanoni 155b47b5b34SRafael Vanoni /* 156b47b5b34SRafael Vanoni * The DTrace probes have been enabled, and are tracking CPU idle state 157b47b5b34SRafael Vanoni * transitions. Take a snapshot of the aggregations, and invoke the aggregation 158b47b5b34SRafael Vanoni * walker to process any records. The walker does most of the accounting work 159b47b5b34SRafael Vanoni * chalking up time spent into the g_cstate_info structure. 160b47b5b34SRafael Vanoni */ 161b47b5b34SRafael Vanoni int 162b47b5b34SRafael Vanoni pt_cpuidle_stat_collect(double interval) 163b47b5b34SRafael Vanoni { 164b47b5b34SRafael Vanoni int i; 165b47b5b34SRafael Vanoni hrtime_t t = 0; 166b47b5b34SRafael Vanoni 167b47b5b34SRafael Vanoni /* 168b47b5b34SRafael Vanoni * Assume that all the time spent in this interval will 169b47b5b34SRafael Vanoni * be the default "0" state. The DTrace walker will reallocate 170b47b5b34SRafael Vanoni * time out of the default bucket as it processes aggregation 171b47b5b34SRafael Vanoni * records for time spent in other states. 172b47b5b34SRafael Vanoni */ 173*636423dbSRafael Vanoni g_cstate_info[0].total_time = (uint64_t)S2NS(interval * 174*636423dbSRafael Vanoni g_ncpus_observed); 175b47b5b34SRafael Vanoni 176b47b5b34SRafael Vanoni if (dtrace_status(dtp) == -1) 177b47b5b34SRafael Vanoni return (-1); 178b47b5b34SRafael Vanoni 179b47b5b34SRafael Vanoni if (dtrace_aggregate_snap(dtp) != 0) 180b47b5b34SRafael Vanoni pt_error("%s : failed to add to aggregation", __FILE__); 181b47b5b34SRafael Vanoni 182b47b5b34SRafael Vanoni if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_cpuidle_dtrace_walk, 183b47b5b34SRafael Vanoni NULL) != 0) 184b47b5b34SRafael Vanoni pt_error("%s : failed to sort aggregation", __FILE__); 185b47b5b34SRafael Vanoni 186b47b5b34SRafael Vanoni dtrace_aggregate_clear(dtp); 187b47b5b34SRafael Vanoni 188b47b5b34SRafael Vanoni /* 189b47b5b34SRafael Vanoni * Populate g_cstate_info with the correct amount of time spent 190b47b5b34SRafael Vanoni * in each C state and update the number of C states in g_max_cstate 191b47b5b34SRafael Vanoni */ 192b47b5b34SRafael Vanoni g_total_c_time = 0; 193b47b5b34SRafael Vanoni for (i = 0; i < NSTATES; i++) { 194b47b5b34SRafael Vanoni if (g_cstate_info[i].total_time > 0) { 195b47b5b34SRafael Vanoni g_total_c_time += g_cstate_info[i].total_time; 196b47b5b34SRafael Vanoni if (i > g_max_cstate) 197b47b5b34SRafael Vanoni g_max_cstate = i; 198b47b5b34SRafael Vanoni if (g_cstate_info[i].last_time > t) { 199b47b5b34SRafael Vanoni t = g_cstate_info[i].last_time; 200b47b5b34SRafael Vanoni g_longest_cstate = i; 201b47b5b34SRafael Vanoni } 202b47b5b34SRafael Vanoni } 203b47b5b34SRafael Vanoni } 204b47b5b34SRafael Vanoni 205b47b5b34SRafael Vanoni return (0); 206b47b5b34SRafael Vanoni } 207b47b5b34SRafael Vanoni 208b47b5b34SRafael Vanoni /* 209b47b5b34SRafael Vanoni * DTrace aggregation walker that sorts through a snapshot of data records 210b47b5b34SRafael Vanoni * collected during firings of the idle-state-transition probe. 211b47b5b34SRafael Vanoni * 212b47b5b34SRafael Vanoni * XXX A way of querying the current idle state for a CPU is needed in addition 213b47b5b34SRafael Vanoni * to logic similar to that in cpufreq.c 214b47b5b34SRafael Vanoni */ 215b47b5b34SRafael Vanoni /*ARGSUSED*/ 216b47b5b34SRafael Vanoni static int 217b47b5b34SRafael Vanoni pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *data, void *arg) 218b47b5b34SRafael Vanoni { 219b47b5b34SRafael Vanoni dtrace_aggdesc_t *aggdesc = data->dtada_desc; 220b47b5b34SRafael Vanoni dtrace_recdesc_t *rec; 221b47b5b34SRafael Vanoni uint64_t n = 0; 222b47b5b34SRafael Vanoni int32_t state; 223b47b5b34SRafael Vanoni int i; 224b47b5b34SRafael Vanoni 225b47b5b34SRafael Vanoni rec = &aggdesc->dtagd_rec[1]; 226b47b5b34SRafael Vanoni /* LINTED - alignment */ 227b47b5b34SRafael Vanoni state = *(int32_t *)(data->dtada_data + rec->dtrd_offset); 228b47b5b34SRafael Vanoni 229b47b5b34SRafael Vanoni if (strcmp(aggdesc->dtagd_name, "number") == 0) { 230b47b5b34SRafael Vanoni for (i = 0; i < g_ncpus; i++) { 231b47b5b34SRafael Vanoni /* LINTED - alignment */ 232b47b5b34SRafael Vanoni n += *((uint64_t *)(data->dtada_percpu[i])); 233b47b5b34SRafael Vanoni } 234b47b5b34SRafael Vanoni g_total_events += n; 235b47b5b34SRafael Vanoni g_cstate_info[state].events += n; 236b47b5b34SRafael Vanoni } 237b47b5b34SRafael Vanoni else 238b47b5b34SRafael Vanoni if (strcmp(aggdesc->dtagd_name, "times") == 0) { 239b47b5b34SRafael Vanoni for (i = 0; i < g_ncpus; i++) { 240b47b5b34SRafael Vanoni /* LINTED - alignment */ 241b47b5b34SRafael Vanoni n += *((uint64_t *)(data->dtada_percpu[i])); 242b47b5b34SRafael Vanoni } 243b47b5b34SRafael Vanoni g_cstate_info[state].last_time = n; 244b47b5b34SRafael Vanoni g_cstate_info[state].total_time += n; 245b47b5b34SRafael Vanoni if (g_cstate_info[0].total_time >= n) 246b47b5b34SRafael Vanoni g_cstate_info[0].total_time -= n; 247*636423dbSRafael Vanoni else 248*636423dbSRafael Vanoni g_cstate_info[0].total_time = 0; 249b47b5b34SRafael Vanoni } 250b47b5b34SRafael Vanoni 251b47b5b34SRafael Vanoni return (DTRACE_AGGWALK_NEXT); 252b47b5b34SRafael Vanoni } 253