Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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 | * Output format inspired by Len Brown's <lenb@kernel.org> turbostat tool. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <unistd.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <time.h> |
| 16 | #include <signal.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/wait.h> |
| 19 | #include <libgen.h> |
| 20 | |
| 21 | #include "idle_monitor/cpupower-monitor.h" |
| 22 | #include "idle_monitor/idle_monitors.h" |
| 23 | #include "helpers/helpers.h" |
| 24 | |
| 25 | /* Define pointers to all monitors. */ |
| 26 | #define DEF(x) & x ## _monitor , |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 27 | struct cpuidle_monitor *all_monitors[] = { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 28 | #include "idle_monitors.def" |
| 29 | 0 |
| 30 | }; |
| 31 | |
| 32 | static struct cpuidle_monitor *monitors[MONITORS_MAX]; |
| 33 | static unsigned int avail_monitors; |
| 34 | |
| 35 | static char *progname; |
| 36 | |
| 37 | enum operation_mode_e { list = 1, show, show_all }; |
| 38 | static int mode; |
| 39 | static int interval = 1; |
| 40 | static char *show_monitors_param; |
| 41 | static struct cpupower_topology cpu_top; |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 42 | static unsigned int wake_cpus; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 43 | |
| 44 | /* ToDo: Document this in the manpage */ |
| 45 | static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', }; |
| 46 | |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 47 | static void print_wrong_arg_exit(void) |
| 48 | { |
| 49 | printf(_("invalid or unknown argument\n")); |
| 50 | exit(EXIT_FAILURE); |
| 51 | } |
| 52 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 53 | long long timespec_diff_us(struct timespec start, struct timespec end) |
| 54 | { |
| 55 | struct timespec temp; |
| 56 | if ((end.tv_nsec - start.tv_nsec) < 0) { |
| 57 | temp.tv_sec = end.tv_sec - start.tv_sec - 1; |
| 58 | temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; |
| 59 | } else { |
| 60 | temp.tv_sec = end.tv_sec - start.tv_sec; |
| 61 | temp.tv_nsec = end.tv_nsec - start.tv_nsec; |
| 62 | } |
| 63 | return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000); |
| 64 | } |
| 65 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 66 | void print_n_spaces(int n) |
| 67 | { |
| 68 | int x; |
| 69 | for (x = 0; x < n; x++) |
| 70 | printf(" "); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 71 | } |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 72 | |
| 73 | /* size of s must be at least n + 1 */ |
| 74 | int fill_string_with_spaces(char *s, int n) |
| 75 | { |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 76 | int len = strlen(s); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 77 | if (len > n) |
| 78 | return -1; |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 79 | for (; len < n; len++) |
| 80 | s[len] = ' '; |
| 81 | s[len] = '\0'; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 82 | return 0; |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 83 | } |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 84 | |
| 85 | void print_header(int topology_depth) |
| 86 | { |
| 87 | int unsigned mon; |
Palmer Cox | fb8eaeb | 2012-11-27 13:17:44 +0100 | [diff] [blame] | 88 | int state, need_len; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 89 | cstate_t s; |
| 90 | char buf[128] = ""; |
| 91 | int percent_width = 4; |
| 92 | |
| 93 | fill_string_with_spaces(buf, topology_depth * 5 - 1); |
| 94 | printf("%s|", buf); |
| 95 | |
| 96 | for (mon = 0; mon < avail_monitors; mon++) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 97 | need_len = monitors[mon]->hw_states_num * (percent_width + 3) |
| 98 | - 1; |
| 99 | if (mon != 0) { |
| 100 | printf("|| "); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 101 | need_len--; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 102 | } |
| 103 | sprintf(buf, "%s", monitors[mon]->name); |
| 104 | fill_string_with_spaces(buf, need_len); |
| 105 | printf("%s", buf); |
| 106 | } |
| 107 | printf("\n"); |
| 108 | |
| 109 | if (topology_depth > 2) |
| 110 | printf("PKG |"); |
| 111 | if (topology_depth > 1) |
| 112 | printf("CORE|"); |
| 113 | if (topology_depth > 0) |
| 114 | printf("CPU |"); |
| 115 | |
| 116 | for (mon = 0; mon < avail_monitors; mon++) { |
| 117 | if (mon != 0) |
| 118 | printf("|| "); |
| 119 | else |
| 120 | printf(" "); |
| 121 | for (state = 0; state < monitors[mon]->hw_states_num; state++) { |
| 122 | if (state != 0) |
| 123 | printf(" | "); |
| 124 | s = monitors[mon]->hw_states[state]; |
| 125 | sprintf(buf, "%s", s.name); |
| 126 | fill_string_with_spaces(buf, percent_width); |
| 127 | printf("%s", buf); |
| 128 | } |
| 129 | printf(" "); |
| 130 | } |
| 131 | printf("\n"); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void print_results(int topology_depth, int cpu) |
| 136 | { |
| 137 | unsigned int mon; |
| 138 | int state, ret; |
| 139 | double percent; |
| 140 | unsigned long long result; |
| 141 | cstate_t s; |
| 142 | |
Thomas Renninger | 9ee31f6 | 2011-08-12 01:11:38 +0200 | [diff] [blame] | 143 | /* Be careful CPUs may got resorted for pkg value do not just use cpu */ |
| 144 | if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu)) |
| 145 | return; |
Jacob Tanenbaum | 20102ac | 2015-10-22 17:17:05 +0200 | [diff] [blame] | 146 | if (!cpu_top.core_info[cpu].is_online && |
| 147 | cpu_top.core_info[cpu].pkg == -1) |
| 148 | return; |
Thomas Renninger | 9ee31f6 | 2011-08-12 01:11:38 +0200 | [diff] [blame] | 149 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 150 | if (topology_depth > 2) |
| 151 | printf("%4d|", cpu_top.core_info[cpu].pkg); |
| 152 | if (topology_depth > 1) |
| 153 | printf("%4d|", cpu_top.core_info[cpu].core); |
| 154 | if (topology_depth > 0) |
| 155 | printf("%4d|", cpu_top.core_info[cpu].cpu); |
| 156 | |
| 157 | for (mon = 0; mon < avail_monitors; mon++) { |
| 158 | if (mon != 0) |
| 159 | printf("||"); |
| 160 | |
| 161 | for (state = 0; state < monitors[mon]->hw_states_num; state++) { |
| 162 | if (state != 0) |
| 163 | printf("|"); |
| 164 | |
| 165 | s = monitors[mon]->hw_states[state]; |
| 166 | |
| 167 | if (s.get_count_percent) { |
| 168 | ret = s.get_count_percent(s.id, &percent, |
| 169 | cpu_top.core_info[cpu].cpu); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 170 | if (ret) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 171 | printf("******"); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 172 | else if (percent >= 100.0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 173 | printf("%6.1f", percent); |
| 174 | else |
| 175 | printf("%6.2f", percent); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 176 | } else if (s.get_count) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 177 | ret = s.get_count(s.id, &result, |
| 178 | cpu_top.core_info[cpu].cpu); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 179 | if (ret) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 180 | printf("******"); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 181 | else |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 182 | printf("%6llu", result); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 183 | } else { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 184 | printf(_("Monitor %s, Counter %s has no count " |
| 185 | "function. Implementation error\n"), |
| 186 | monitors[mon]->name, s.name); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 187 | exit(EXIT_FAILURE); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
Thomas Renninger | 7c74d2b | 2011-08-12 01:11:37 +0200 | [diff] [blame] | 191 | /* |
| 192 | * The monitor could still provide useful data, for example |
| 193 | * AMD HW counters partly sit in PCI config space. |
| 194 | * It's up to the monitor plug-in to check .is_online, this one |
| 195 | * is just for additional info. |
| 196 | */ |
Jacob Tanenbaum | 20102ac | 2015-10-22 17:17:05 +0200 | [diff] [blame] | 197 | if (!cpu_top.core_info[cpu].is_online && |
| 198 | cpu_top.core_info[cpu].pkg != -1) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 199 | printf(_(" *is offline\n")); |
| 200 | return; |
| 201 | } else |
| 202 | printf("\n"); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /* param: string passed by -m param (The list of monitors to show) |
| 207 | * |
| 208 | * Monitors must have been registered already, matching monitors |
| 209 | * are picked out and available monitors array is overridden |
| 210 | * with matching ones |
| 211 | * |
| 212 | * Monitors get sorted in the same order the user passes them |
| 213 | */ |
| 214 | |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 215 | static void parse_monitor_param(char *param) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 216 | { |
| 217 | unsigned int num; |
| 218 | int mon, hits = 0; |
| 219 | char *tmp = param, *token; |
| 220 | struct cpuidle_monitor *tmp_mons[MONITORS_MAX]; |
| 221 | |
| 222 | |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 223 | for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 224 | token = strtok(tmp, ","); |
| 225 | if (token == NULL) |
| 226 | break; |
| 227 | if (strlen(token) >= MONITOR_NAME_LEN) { |
| 228 | printf(_("%s: max monitor name length" |
| 229 | " (%d) exceeded\n"), token, MONITOR_NAME_LEN); |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | for (num = 0; num < avail_monitors; num++) { |
| 234 | if (!strcmp(monitors[num]->name, token)) { |
| 235 | dprint("Found requested monitor: %s\n", token); |
| 236 | tmp_mons[hits] = monitors[num]; |
| 237 | hits++; |
| 238 | } |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 239 | } |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 240 | } |
| 241 | if (hits == 0) { |
| 242 | printf(_("No matching monitor found in %s, " |
| 243 | "try -l option\n"), param); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 244 | exit(EXIT_FAILURE); |
| 245 | } |
| 246 | /* Override detected/registerd monitors array with requested one */ |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 247 | memcpy(monitors, tmp_mons, |
| 248 | sizeof(struct cpuidle_monitor *) * MONITORS_MAX); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 249 | avail_monitors = hits; |
| 250 | } |
| 251 | |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 252 | void list_monitors(void) |
| 253 | { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 254 | unsigned int mon; |
| 255 | int state; |
| 256 | cstate_t s; |
| 257 | |
| 258 | for (mon = 0; mon < avail_monitors; mon++) { |
| 259 | printf(_("Monitor \"%s\" (%d states) - Might overflow after %u " |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 260 | "s\n"), |
| 261 | monitors[mon]->name, monitors[mon]->hw_states_num, |
| 262 | monitors[mon]->overflow_s); |
| 263 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 264 | for (state = 0; state < monitors[mon]->hw_states_num; state++) { |
| 265 | s = monitors[mon]->hw_states[state]; |
| 266 | /* |
| 267 | * ToDo show more state capabilities: |
| 268 | * percent, time (granlarity) |
| 269 | */ |
| 270 | printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range], |
| 271 | gettext(s.desc)); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | int fork_it(char **argv) |
| 277 | { |
| 278 | int status; |
| 279 | unsigned int num; |
| 280 | unsigned long long timediff; |
| 281 | pid_t child_pid; |
| 282 | struct timespec start, end; |
| 283 | |
| 284 | child_pid = fork(); |
| 285 | clock_gettime(CLOCK_REALTIME, &start); |
| 286 | |
| 287 | for (num = 0; num < avail_monitors; num++) |
| 288 | monitors[num]->start(); |
| 289 | |
| 290 | if (!child_pid) { |
| 291 | /* child */ |
| 292 | execvp(argv[0], argv); |
| 293 | } else { |
| 294 | /* parent */ |
| 295 | if (child_pid == -1) { |
| 296 | perror("fork"); |
| 297 | exit(1); |
| 298 | } |
| 299 | |
| 300 | signal(SIGINT, SIG_IGN); |
| 301 | signal(SIGQUIT, SIG_IGN); |
| 302 | if (waitpid(child_pid, &status, 0) == -1) { |
| 303 | perror("wait"); |
| 304 | exit(1); |
| 305 | } |
| 306 | } |
| 307 | clock_gettime(CLOCK_REALTIME, &end); |
| 308 | for (num = 0; num < avail_monitors; num++) |
| 309 | monitors[num]->stop(); |
| 310 | |
| 311 | timediff = timespec_diff_us(start, end); |
| 312 | if (WIFEXITED(status)) |
| 313 | printf(_("%s took %.5f seconds and exited with status %d\n"), |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 314 | argv[0], timediff / (1000.0 * 1000), |
| 315 | WEXITSTATUS(status)); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int do_interval_measure(int i) |
| 320 | { |
| 321 | unsigned int num; |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 322 | int cpu; |
| 323 | |
| 324 | if (wake_cpus) |
| 325 | for (cpu = 0; cpu < cpu_count; cpu++) |
| 326 | bind_cpu(cpu); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 327 | |
| 328 | for (num = 0; num < avail_monitors; num++) { |
| 329 | dprint("HW C-state residency monitor: %s - States: %d\n", |
| 330 | monitors[num]->name, monitors[num]->hw_states_num); |
| 331 | monitors[num]->start(); |
| 332 | } |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 333 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 334 | sleep(i); |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 335 | |
| 336 | if (wake_cpus) |
| 337 | for (cpu = 0; cpu < cpu_count; cpu++) |
| 338 | bind_cpu(cpu); |
| 339 | |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 340 | for (num = 0; num < avail_monitors; num++) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 341 | monitors[num]->stop(); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 342 | |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 343 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static void cmdline(int argc, char *argv[]) |
| 348 | { |
| 349 | int opt; |
| 350 | progname = basename(argv[0]); |
| 351 | |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 352 | while ((opt = getopt(argc, argv, "+lci:m:")) != -1) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 353 | switch (opt) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 354 | case 'l': |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 355 | if (mode) |
| 356 | print_wrong_arg_exit(); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 357 | mode = list; |
| 358 | break; |
| 359 | case 'i': |
| 360 | /* only allow -i with -m or no option */ |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 361 | if (mode && mode != show) |
| 362 | print_wrong_arg_exit(); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 363 | interval = atoi(optarg); |
| 364 | break; |
| 365 | case 'm': |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 366 | if (mode) |
| 367 | print_wrong_arg_exit(); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 368 | mode = show; |
| 369 | show_monitors_param = optarg; |
| 370 | break; |
Thomas Renninger | c8cfc3c | 2012-11-27 13:17:48 +0100 | [diff] [blame] | 371 | case 'c': |
| 372 | wake_cpus = 1; |
| 373 | break; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 374 | default: |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 375 | print_wrong_arg_exit(); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | if (!mode) |
| 379 | mode = show_all; |
| 380 | } |
| 381 | |
| 382 | int cmd_monitor(int argc, char **argv) |
| 383 | { |
| 384 | unsigned int num; |
| 385 | struct cpuidle_monitor *test_mon; |
| 386 | int cpu; |
| 387 | |
| 388 | cmdline(argc, argv); |
| 389 | cpu_count = get_cpu_topology(&cpu_top); |
| 390 | if (cpu_count < 0) { |
| 391 | printf(_("Cannot read number of available processors\n")); |
| 392 | return EXIT_FAILURE; |
| 393 | } |
| 394 | |
Jacob Tanenbaum | 20102ac | 2015-10-22 17:17:05 +0200 | [diff] [blame] | 395 | if (!cpu_top.core_info[0].is_online) |
| 396 | printf("WARNING: at least one cpu is offline\n"); |
| 397 | |
Thomas Renninger | 9ee31f6 | 2011-08-12 01:11:38 +0200 | [diff] [blame] | 398 | /* Default is: monitor all CPUs */ |
| 399 | if (bitmask_isallclear(cpus_chosen)) |
| 400 | bitmask_setall(cpus_chosen); |
| 401 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 402 | dprint("System has up to %d CPU cores\n", cpu_count); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 403 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 404 | for (num = 0; all_monitors[num]; num++) { |
| 405 | dprint("Try to register: %s\n", all_monitors[num]->name); |
| 406 | test_mon = all_monitors[num]->do_register(); |
| 407 | if (test_mon) { |
| 408 | if (test_mon->needs_root && !run_as_root) { |
| 409 | fprintf(stderr, _("Available monitor %s needs " |
| 410 | "root access\n"), test_mon->name); |
| 411 | continue; |
| 412 | } |
| 413 | monitors[avail_monitors] = test_mon; |
| 414 | dprint("%s registered\n", all_monitors[num]->name); |
| 415 | avail_monitors++; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (avail_monitors == 0) { |
| 420 | printf(_("No HW Cstate monitors found\n")); |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | if (mode == list) { |
| 425 | list_monitors(); |
| 426 | exit(EXIT_SUCCESS); |
| 427 | } |
| 428 | |
| 429 | if (mode == show) |
| 430 | parse_monitor_param(show_monitors_param); |
| 431 | |
| 432 | dprint("Packages: %d - Cores: %d - CPUs: %d\n", |
| 433 | cpu_top.pkgs, cpu_top.cores, cpu_count); |
| 434 | |
| 435 | /* |
| 436 | * if any params left, it must be a command to fork |
| 437 | */ |
| 438 | if (argc - optind) |
| 439 | fork_it(argv + optind); |
| 440 | else |
| 441 | do_interval_measure(interval); |
| 442 | |
| 443 | /* ToDo: Topology parsing needs fixing first to do |
| 444 | this more generically */ |
| 445 | if (cpu_top.pkgs > 1) |
| 446 | print_header(3); |
| 447 | else |
| 448 | print_header(1); |
| 449 | |
| 450 | for (cpu = 0; cpu < cpu_count; cpu++) { |
| 451 | if (cpu_top.pkgs > 1) |
| 452 | print_results(3, cpu); |
| 453 | else |
| 454 | print_results(1, cpu); |
| 455 | } |
| 456 | |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 457 | for (num = 0; num < avail_monitors; num++) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 458 | monitors[num]->unregister(); |
Dominik Brodowski | b510b54 | 2011-04-19 19:58:59 +0200 | [diff] [blame] | 459 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 460 | cpu_topology_release(cpu_top); |
| 461 | return 0; |
| 462 | } |