cpupowerutils - cpufrequtils extended with quite some features
[linux-2.6.git] / tools / power / cpupower / utils / idle_monitor / nhm_idle.c
1 /*
2  *  (C) 2010,2011       Thomas Renninger <trenn@suse.de>, Novell Inc.
3  *
4  *  Licensed under the terms of the GNU GPL License version 2.
5  *
6  *  Based on Len Brown's <lenb@kernel.org> turbostat tool.
7  */
8
9 #if defined(__i386__) || defined(__x86_64__)
10
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "helpers/helpers.h"
17 #include "idle_monitor/cpupower-monitor.h"
18
19 #define MSR_PKG_C3_RESIDENCY    0x3F8
20 #define MSR_PKG_C6_RESIDENCY    0x3F9
21 #define MSR_CORE_C3_RESIDENCY   0x3FC
22 #define MSR_CORE_C6_RESIDENCY   0x3FD
23
24 #define MSR_TSC 0x10
25
26 #define NHM_CSTATE_COUNT 4
27
28 enum intel_nhm_id { C3 = 0, C6, PC3, PC6, TSC = 0xFFFF };
29
30 static int nhm_get_count_percent(unsigned int self_id, double *percent,
31                                  unsigned int cpu);
32
33 static cstate_t nhm_cstates[NHM_CSTATE_COUNT] = {
34         {
35                 .name                   = "C3",
36                 .desc                   = N_("Processor Core C3"),
37                 .id                     = C3,
38                 .range                  = RANGE_CORE,
39                 .get_count_percent      = nhm_get_count_percent,
40         },
41         {
42                 .name                   = "C6",
43                 .desc                   = N_("Processor Core C6"),
44                 .id                     = C6,
45                 .range                  = RANGE_CORE,
46                 .get_count_percent      = nhm_get_count_percent,
47         },
48
49         {
50                 .name                   = "PC3",
51                 .desc                   = N_("Processor Package C3"),
52                 .id                     = PC3,
53                 .range                  = RANGE_PACKAGE,
54                 .get_count_percent      = nhm_get_count_percent,
55         },
56         {
57                 .name                   = "PC6",
58                 .desc                   = N_("Processor Package C6"),
59                 .id                     = PC6,
60                 .range                  = RANGE_PACKAGE,
61                 .get_count_percent      = nhm_get_count_percent,
62         },
63 };
64
65 static unsigned long long tsc_at_measure_start;
66 static unsigned long long tsc_at_measure_end;
67 static unsigned long long *previous_count[NHM_CSTATE_COUNT];
68 static unsigned long long *current_count[NHM_CSTATE_COUNT];
69 /* valid flag for all CPUs. If a MSR read failed it will be zero */
70 static int *is_valid;
71
72 static int nhm_get_count(enum intel_nhm_id id, unsigned long long *val, unsigned int cpu)
73 {
74         int msr;
75
76         switch(id) {
77         case C3:
78                 msr = MSR_CORE_C3_RESIDENCY;
79                 break;
80         case C6:
81                 msr = MSR_CORE_C6_RESIDENCY;
82                 break;
83         case PC3:
84                 msr = MSR_PKG_C3_RESIDENCY;
85                 break;
86         case PC6:
87                 msr = MSR_PKG_C6_RESIDENCY;
88                 break;
89         case TSC:
90                 msr = MSR_TSC;
91                 break;
92         default:
93                 return -1;
94         };
95         if (read_msr(cpu, msr, val))
96                 return -1;
97
98         return 0;
99 }
100
101 static int nhm_get_count_percent(unsigned int id, double *percent,
102                                  unsigned int cpu)
103 {
104         *percent = 0.0;
105
106         if (!is_valid[cpu])
107                 return -1;
108
109         *percent = (100.0 * (current_count[id][cpu] - previous_count[id][cpu])) /
110                 (tsc_at_measure_end - tsc_at_measure_start);
111
112         dprint("%s: previous: %llu - current: %llu - (%u)\n", nhm_cstates[id].name,
113                previous_count[id][cpu], current_count[id][cpu],
114                cpu);
115
116         dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
117                nhm_cstates[id].name,
118                (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
119                current_count[id][cpu] - previous_count[id][cpu],
120                *percent, cpu);
121
122         return 0;
123 }
124
125 static int nhm_start(void)
126 {
127         int num, cpu;
128         unsigned long long dbg, val;
129
130         nhm_get_count(TSC, &tsc_at_measure_start, 0);
131
132         for (num = 0; num < NHM_CSTATE_COUNT; num++) {
133                 for (cpu = 0; cpu < cpu_count; cpu++) {
134                         is_valid[cpu] = !nhm_get_count(num, &val, cpu);
135                         previous_count[num][cpu] = val;
136                 }
137         }
138         nhm_get_count(TSC, &dbg, 0);
139         dprint("TSC diff: %llu\n", dbg - tsc_at_measure_start);
140         return 0;
141 }
142
143 static int nhm_stop(void)
144 {
145         unsigned long long val;
146         unsigned long long dbg;
147         int num, cpu;
148
149         nhm_get_count(TSC, &tsc_at_measure_end, 0);
150
151         for (num = 0; num < NHM_CSTATE_COUNT; num++) {
152                 for (cpu = 0; cpu < cpu_count; cpu++) {
153                         is_valid[cpu] = !nhm_get_count(num, &val, cpu);
154                         current_count[num][cpu] = val;
155                 }
156         }
157         nhm_get_count(TSC, &dbg, 0);
158         dprint("TSC diff: %llu\n", dbg - tsc_at_measure_end);
159
160         return 0;
161 }
162
163 struct cpuidle_monitor intel_nhm_monitor;
164
165 struct cpuidle_monitor* intel_nhm_register(void) {
166         int num;
167
168         if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
169                 return NULL;
170
171         if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_INV_TSC))
172                 return NULL;
173
174         if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_APERF))
175                 return NULL;
176
177         /* Free this at program termination */
178         is_valid = calloc(cpu_count, sizeof (int));
179         for (num = 0; num < NHM_CSTATE_COUNT; num++) {
180                 previous_count[num] = calloc (cpu_count,
181                                               sizeof(unsigned long long));
182                 current_count[num]  = calloc (cpu_count,
183                                               sizeof(unsigned long long));
184         }
185
186         intel_nhm_monitor.name_len = strlen(intel_nhm_monitor.name);
187         return &intel_nhm_monitor;
188 }
189
190 void intel_nhm_unregister(void) {
191         int num;
192
193         for (num = 0; num < NHM_CSTATE_COUNT; num++) {
194                 free(previous_count[num]);
195                 free(current_count[num]);
196         }
197         free(is_valid);
198 }
199
200 struct cpuidle_monitor intel_nhm_monitor = {
201         .name                   = "Nehalem",
202         .hw_states_num          = NHM_CSTATE_COUNT,
203         .hw_states              = nhm_cstates,
204         .start                  = nhm_start,
205         .stop                   = nhm_stop,
206         .do_register            = intel_nhm_register,
207         .unregister             = intel_nhm_unregister,
208         .needs_root             = 1,
209         .overflow_s             = 922000000 /* 922337203 seconds TSC overflow
210                                                at 20GHz */
211 };
212 #endif