|
| 1 | +#ifndef _GNU_SOURCE |
| 2 | +#define _GNU_SOURCE |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <cstdio> |
| 6 | +#include <cstdlib> |
| 7 | +#include <cstring> |
| 8 | +#include <cctype> |
| 9 | + |
| 10 | +#include <vector> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +#include <inttypes.h> |
| 14 | +#include <sys/types.h> |
| 15 | + |
| 16 | +#include <unistd.h> |
| 17 | +#include <dirent.h> |
| 18 | +#include <getopt.h> |
| 19 | +#include <libgen.h> |
| 20 | + |
| 21 | +#include <errno.h> |
| 22 | +#include <fcntl.h> |
| 23 | +#include <err.h> |
| 24 | + |
| 25 | +#include "msr_version.hpp" |
| 26 | + |
| 27 | +#define program "msr_read" |
| 28 | + |
| 29 | +#define msr_warn(fmt, ...) fprintf(stderr, program ": " fmt, ##__VA_ARGS__) |
| 30 | + |
| 31 | +namespace msr { |
| 32 | + |
| 33 | +const struct option long_options[] = { |
| 34 | + {"help", no_argument, NULL, 'h'}, |
| 35 | + {"version", no_argument, NULL, 'V'}, |
| 36 | + {"all", no_argument, NULL, 'a'}, |
| 37 | + {"processor", required_argument, NULL, 'p'}, |
| 38 | + {"cpu", required_argument, NULL, 'p'}, |
| 39 | + {NULL, no_argument, NULL, 0 } |
| 40 | +}; |
| 41 | + |
| 42 | +const char shot_options[] = "hVap:"; |
| 43 | + |
| 44 | +typedef struct { |
| 45 | + int cpu; |
| 46 | + uint32_t reg; |
| 47 | +} options_t; |
| 48 | + |
| 49 | +options_t options; |
| 50 | + |
| 51 | + |
| 52 | +void usage() |
| 53 | +{ |
| 54 | + fprintf(stderr, |
| 55 | + "Usage:\n" |
| 56 | + " %s [options] <register-id> <register-value> [...]\n" |
| 57 | + "Options:\n" |
| 58 | + " --help -h print this help\n" |
| 59 | + " --version -V print current version\n" |
| 60 | + " --all -a all processors\n" |
| 61 | + " --processor # -p select processor number (default 0)\n", |
| 62 | + program |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +void version() |
| 67 | +{ |
| 68 | + fprintf(stderr, "%s: version %s\n", program, MSR_VERSION_STRING); |
| 69 | +} |
| 70 | + |
| 71 | +int is_cpu(const struct dirent *dirp) |
| 72 | +{ |
| 73 | + return std::isdigit(dirp->d_name[0]); /* /dev/cpu/<cpu> */ |
| 74 | +} |
| 75 | + |
| 76 | +bool is_onln(int cpu) |
| 77 | +{ |
| 78 | + if (cpu < 0) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // the boot cpu do *not* support hotplug (x86 platform, linux-2.6.x, linux-4.7.x) |
| 83 | + if (cpu == 0) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + // if online, the cache directory exist, otherwise not (x86 platform, linux-2.6.x, linux-4.7.x) |
| 88 | + std::string cpu_path = "/sys/devices/system/cpu/cpu" + std::to_string(cpu) + "/cache"; |
| 89 | + |
| 90 | + if (::access(cpu_path.c_str(), F_OK) != 0) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * msr_print - print value in the given format |
| 99 | + * |
| 100 | + * @val value to print |
| 101 | + * |
| 102 | + * FIXME: |
| 103 | + * |
| 104 | + */ |
| 105 | +void msr_print(uint64_t val) |
| 106 | +{ |
| 107 | + FILE *fp = stdout; |
| 108 | + |
| 109 | + fprintf(fp, "%zx\n", val); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * msr_read - read values from @cpu's msr register specified by @reg |
| 114 | + * |
| 115 | + * @cpu which processor |
| 116 | + * @reg the register id |
| 117 | + * |
| 118 | + * Return: |
| 119 | + * true - read succ |
| 120 | + * false - read fail |
| 121 | + */ |
| 122 | +bool msr_read(int cpu, uint32_t reg) |
| 123 | +{ |
| 124 | + if (!is_onln(cpu)) { |
| 125 | + msr_warn("cpu %2d does not online\n", cpu); |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + std::string msr_path = "/dev/cpu/" + std::to_string(cpu) + "/msr"; |
| 130 | + |
| 131 | + int fd = ::open(msr_path.c_str(), O_RDONLY); |
| 132 | + if (fd == -1) { |
| 133 | + switch(errno) { |
| 134 | + |
| 135 | + // the file is a device special file and no corresponding device exists |
| 136 | + case ENXIO: |
| 137 | + msr_warn("cpu %2d does not exist\n", cpu); |
| 138 | + return false; |
| 139 | + |
| 140 | + // |
| 141 | + case EIO: |
| 142 | + msr_warn("cpu %2d does not support MSR\n", cpu); |
| 143 | + return false; |
| 144 | + |
| 145 | + default: |
| 146 | + msr_warn("failed on open MSR file for cpu %d, %s\n", cpu, strerror_r(errno, NULL, 0)); |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + uint64_t val = 0; |
| 152 | + |
| 153 | + if (::pread(fd, &val, sizeof(val), reg) != sizeof(val)) { |
| 154 | + msr_warn("failed to read MSR file for cpu %d, %s\n", cpu, strerror_r(errno, NULL, 0)); |
| 155 | + ::close(fd); |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + ::close(fd); |
| 160 | + |
| 161 | + msr_print(val); |
| 162 | + |
| 163 | + return true; |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +/** |
| 168 | + * msr_read - read values from all (online) cpus' msr register specified by @reg |
| 169 | + * |
| 170 | + * @reg the register id |
| 171 | + * |
| 172 | + * Return: |
| 173 | + * true - read succ |
| 174 | + * false - read fail |
| 175 | + */ |
| 176 | +bool msr_read(uint32_t reg) |
| 177 | +{ |
| 178 | + struct dirent **namelist; |
| 179 | + size_t nr_dirent = 0; |
| 180 | + |
| 181 | + nr_dirent = ::scandir("/dev/cpu", &namelist, is_cpu, 0); |
| 182 | + |
| 183 | + for (size_t cpu = 0; cpu < nr_dirent; ++cpu) { |
| 184 | + msr_read(std::stoi(namelist[cpu]->d_name), reg); |
| 185 | + } |
| 186 | + |
| 187 | + for (size_t cpu = 0; cpu < nr_dirent; ++cpu) { |
| 188 | + free(namelist[cpu]); |
| 189 | + } |
| 190 | + |
| 191 | + free(namelist); |
| 192 | + |
| 193 | + return true; |
| 194 | +} |
| 195 | + |
| 196 | +} /* namespace msr */ |
| 197 | + |
| 198 | + |
| 199 | +int main(int argc, char **argv) |
| 200 | +{ |
| 201 | + char ch; |
| 202 | + while ((ch = getopt_long(argc, argv, msr::shot_options, msr::long_options, NULL)) != -1) { |
| 203 | + switch (ch) { |
| 204 | + case 'h': |
| 205 | + msr::usage(); |
| 206 | + exit(EXIT_SUCCESS); |
| 207 | + |
| 208 | + case 'V': |
| 209 | + msr::version(); |
| 210 | + exit(EXIT_SUCCESS); |
| 211 | + |
| 212 | + case 'a': |
| 213 | + msr::options.cpu = -1; |
| 214 | + break; |
| 215 | + |
| 216 | + case 'p': |
| 217 | + msr::options.cpu = std::stoi(optarg); |
| 218 | + break; |
| 219 | + |
| 220 | + default: |
| 221 | + msr::usage(); |
| 222 | + exit(EXIT_FAILURE); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if (optind + 1 > argc) { |
| 227 | + msr::usage(); |
| 228 | + exit(EXIT_FAILURE); |
| 229 | + } |
| 230 | + |
| 231 | + if (geteuid() != 0) { |
| 232 | + msr_warn("you need root privilege to run\n"); |
| 233 | + exit(EXIT_FAILURE); |
| 234 | + } |
| 235 | + |
| 236 | + msr::options.reg = std::stoul(argv[optind++], 0, 16); |
| 237 | + |
| 238 | + if (msr::options.cpu == -1) { |
| 239 | + msr::msr_read(msr::options.reg); |
| 240 | + } else { |
| 241 | + msr::msr_read(msr::options.cpu, msr::options.reg); |
| 242 | + } |
| 243 | +} |
0 commit comments