turbostat.c (a02001086bbfb4da35d1228bebc2f1b442db455f) | turbostat.c (98481e79b60a50d699b79292ff1b7e56e7fa8425) |
---|---|
1/* 2 * turbostat -- show CPU frequency and C-state residency 3 * on modern Intel turbo-capable processors. 4 * 5 * Copyright (c) 2013 Intel Corporation. 6 * Len Brown <len.brown@intel.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it --- 24 unchanged lines hidden (view full) --- 33#include <signal.h> 34#include <sys/time.h> 35#include <stdlib.h> 36#include <dirent.h> 37#include <string.h> 38#include <ctype.h> 39#include <sched.h> 40#include <cpuid.h> | 1/* 2 * turbostat -- show CPU frequency and C-state residency 3 * on modern Intel turbo-capable processors. 4 * 5 * Copyright (c) 2013 Intel Corporation. 6 * Len Brown <len.brown@intel.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it --- 24 unchanged lines hidden (view full) --- 33#include <signal.h> 34#include <sys/time.h> 35#include <stdlib.h> 36#include <dirent.h> 37#include <string.h> 38#include <ctype.h> 39#include <sched.h> 40#include <cpuid.h> |
41#include <linux/capability.h> 42#include <errno.h> |
|
41 42char *proc_stat = "/proc/stat"; 43unsigned int interval_sec = 5; /* set with -i interval_sec */ 44unsigned int verbose; /* set with -v */ 45unsigned int rapl_verbose; /* set with -R */ 46unsigned int rapl_joules; /* set with -J */ 47unsigned int thermal_verbose; /* set with -T */ 48unsigned int summary_only; /* set with -S */ --- 197 unchanged lines hidden (view full) --- 246{ 247 ssize_t retval; 248 char pathname[32]; 249 int fd; 250 251 sprintf(pathname, "/dev/cpu/%d/msr", cpu); 252 fd = open(pathname, O_RDONLY); 253 if (fd < 0) | 43 44char *proc_stat = "/proc/stat"; 45unsigned int interval_sec = 5; /* set with -i interval_sec */ 46unsigned int verbose; /* set with -v */ 47unsigned int rapl_verbose; /* set with -R */ 48unsigned int rapl_joules; /* set with -J */ 49unsigned int thermal_verbose; /* set with -T */ 50unsigned int summary_only; /* set with -S */ --- 197 unchanged lines hidden (view full) --- 248{ 249 ssize_t retval; 250 char pathname[32]; 251 int fd; 252 253 sprintf(pathname, "/dev/cpu/%d/msr", cpu); 254 fd = open(pathname, O_RDONLY); 255 if (fd < 0) |
254 return -1; | 256 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); |
255 256 retval = pread(fd, msr, sizeof *msr, offset); 257 close(fd); 258 | 257 258 retval = pread(fd, msr, sizeof *msr, offset); 259 close(fd); 260 |
259 if (retval != sizeof *msr) { 260 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset); 261 return -1; 262 } | 261 if (retval != sizeof *msr) 262 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset); |
263 264 return 0; 265} 266 267/* 268 * Example Format w/ field column widths: 269 * 270 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt --- 1186 unchanged lines hidden (view full) --- 1457{ 1458 struct stat sb; 1459 1460 if (stat("/dev/cpu/0/msr", &sb)) 1461 err(-5, "no /dev/cpu/0/msr\n" 1462 "Try \"# modprobe msr\""); 1463} 1464 | 263 264 return 0; 265} 266 267/* 268 * Example Format w/ field column widths: 269 * 270 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt --- 1186 unchanged lines hidden (view full) --- 1457{ 1458 struct stat sb; 1459 1460 if (stat("/dev/cpu/0/msr", &sb)) 1461 err(-5, "no /dev/cpu/0/msr\n" 1462 "Try \"# modprobe msr\""); 1463} 1464 |
1465void check_super_user() | 1465void check_permissions() |
1466{ | 1466{ |
1467 if (getuid() != 0) 1468 errx(-6, "must be root"); | 1467 struct __user_cap_header_struct cap_header_data; 1468 cap_user_header_t cap_header = &cap_header_data; 1469 struct __user_cap_data_struct cap_data_data; 1470 cap_user_data_t cap_data = &cap_data_data; 1471 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 1472 int do_exit = 0; 1473 1474 /* check for CAP_SYS_RAWIO */ 1475 cap_header->pid = getpid(); 1476 cap_header->version = _LINUX_CAPABILITY_VERSION; 1477 if (capget(cap_header, cap_data) < 0) 1478 err(-6, "capget(2) failed"); 1479 1480 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 1481 do_exit++; 1482 warnx("capget(CAP_SYS_RAWIO) failed," 1483 " try \"# setcap cap_sys_rawio=ep %s\"", progname); 1484 } 1485 1486 /* test file permissions */ 1487 if (euidaccess("/dev/cpu/0/msr", R_OK)) { 1488 do_exit++; 1489 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 1490 } 1491 1492 /* if all else fails, thell them to be root */ 1493 if (do_exit) 1494 if (getuid() != 0) 1495 warnx("Or simply run as root"); 1496 1497 if (do_exit) 1498 exit(-6); |
1469} 1470 1471int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1472{ 1473 if (!genuine_intel) 1474 return 0; 1475 1476 if (family != 6) --- 817 unchanged lines hidden (view full) --- 2294 allocate_counters(&thread_even, &core_even, &package_even); 2295 allocate_counters(&thread_odd, &core_odd, &package_odd); 2296 allocate_output_buffer(); 2297 for_all_proc_cpus(initialize_counters); 2298} 2299 2300void turbostat_init() 2301{ | 1499} 1500 1501int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) 1502{ 1503 if (!genuine_intel) 1504 return 0; 1505 1506 if (family != 6) --- 817 unchanged lines hidden (view full) --- 2324 allocate_counters(&thread_even, &core_even, &package_even); 2325 allocate_counters(&thread_odd, &core_odd, &package_odd); 2326 allocate_output_buffer(); 2327 for_all_proc_cpus(initialize_counters); 2328} 2329 2330void turbostat_init() 2331{ |
2332 check_dev_msr(); 2333 check_permissions(); |
|
2302 check_cpuid(); 2303 | 2334 check_cpuid(); 2335 |
2304 check_dev_msr(); 2305 check_super_user(); 2306 | |
2307 setup_all_buffers(); 2308 2309 if (verbose) 2310 print_verbose_header(); 2311 2312 if (verbose) 2313 for_all_cpus(print_epb, ODD_COUNTERS); 2314 --- 121 unchanged lines hidden (view full) --- 2436 } 2437} 2438 2439int main(int argc, char **argv) 2440{ 2441 cmdline(argc, argv); 2442 2443 if (verbose) | 2336 setup_all_buffers(); 2337 2338 if (verbose) 2339 print_verbose_header(); 2340 2341 if (verbose) 2342 for_all_cpus(print_epb, ODD_COUNTERS); 2343 --- 121 unchanged lines hidden (view full) --- 2465 } 2466} 2467 2468int main(int argc, char **argv) 2469{ 2470 cmdline(argc, argv); 2471 2472 if (verbose) |
2444 fprintf(stderr, "turbostat v3.7 Feb 6, 2014" | 2473 fprintf(stderr, "turbostat v3.8 14-Aug 2014" |
2445 " - Len Brown <lenb@kernel.org>\n"); 2446 2447 turbostat_init(); 2448 2449 /* dump counters and exit */ 2450 if (dump_only) 2451 return get_and_dump_counters(); 2452 2453 /* 2454 * if any params left, it must be a command to fork 2455 */ 2456 if (argc - optind) 2457 return fork_it(argv + optind); 2458 else 2459 turbostat_loop(); 2460 2461 return 0; 2462} | 2474 " - Len Brown <lenb@kernel.org>\n"); 2475 2476 turbostat_init(); 2477 2478 /* dump counters and exit */ 2479 if (dump_only) 2480 return get_and_dump_counters(); 2481 2482 /* 2483 * if any params left, it must be a command to fork 2484 */ 2485 if (argc - optind) 2486 return fork_it(argv + optind); 2487 else 2488 turbostat_loop(); 2489 2490 return 0; 2491} |