util.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include "../perf.h"
  2. #include "util.h"
  3. #include "debug.h"
  4. #include <api/fs/fs.h>
  5. #include <sys/mman.h>
  6. #ifdef HAVE_BACKTRACE_SUPPORT
  7. #include <execinfo.h>
  8. #endif
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <limits.h>
  14. #include <byteswap.h>
  15. #include <linux/kernel.h>
  16. /*
  17. * XXX We need to find a better place for these things...
  18. */
  19. unsigned int page_size;
  20. int cacheline_size;
  21. bool test_attr__enabled;
  22. bool perf_host = true;
  23. bool perf_guest = false;
  24. char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
  25. void event_attr_init(struct perf_event_attr *attr)
  26. {
  27. if (!perf_host)
  28. attr->exclude_host = 1;
  29. if (!perf_guest)
  30. attr->exclude_guest = 1;
  31. /* to capture ABI version */
  32. attr->size = sizeof(*attr);
  33. }
  34. int mkdir_p(char *path, mode_t mode)
  35. {
  36. struct stat st;
  37. int err;
  38. char *d = path;
  39. if (*d != '/')
  40. return -1;
  41. if (stat(path, &st) == 0)
  42. return 0;
  43. while (*++d == '/');
  44. while ((d = strchr(d, '/'))) {
  45. *d = '\0';
  46. err = stat(path, &st) && mkdir(path, mode);
  47. *d++ = '/';
  48. if (err)
  49. return -1;
  50. while (*d == '/')
  51. ++d;
  52. }
  53. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  54. }
  55. static int slow_copyfile(const char *from, const char *to, mode_t mode)
  56. {
  57. int err = -1;
  58. char *line = NULL;
  59. size_t n;
  60. FILE *from_fp = fopen(from, "r"), *to_fp;
  61. mode_t old_umask;
  62. if (from_fp == NULL)
  63. goto out;
  64. old_umask = umask(mode ^ 0777);
  65. to_fp = fopen(to, "w");
  66. umask(old_umask);
  67. if (to_fp == NULL)
  68. goto out_fclose_from;
  69. while (getline(&line, &n, from_fp) > 0)
  70. if (fputs(line, to_fp) == EOF)
  71. goto out_fclose_to;
  72. err = 0;
  73. out_fclose_to:
  74. fclose(to_fp);
  75. free(line);
  76. out_fclose_from:
  77. fclose(from_fp);
  78. out:
  79. return err;
  80. }
  81. int copyfile_mode(const char *from, const char *to, mode_t mode)
  82. {
  83. int fromfd, tofd;
  84. struct stat st;
  85. void *addr;
  86. int err = -1;
  87. if (stat(from, &st))
  88. goto out;
  89. if (st.st_size == 0) /* /proc? do it slowly... */
  90. return slow_copyfile(from, to, mode);
  91. fromfd = open(from, O_RDONLY);
  92. if (fromfd < 0)
  93. goto out;
  94. tofd = creat(to, mode);
  95. if (tofd < 0)
  96. goto out_close_from;
  97. addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
  98. if (addr == MAP_FAILED)
  99. goto out_close_to;
  100. if (write(tofd, addr, st.st_size) == st.st_size)
  101. err = 0;
  102. munmap(addr, st.st_size);
  103. out_close_to:
  104. close(tofd);
  105. if (err)
  106. unlink(to);
  107. out_close_from:
  108. close(fromfd);
  109. out:
  110. return err;
  111. }
  112. int copyfile(const char *from, const char *to)
  113. {
  114. return copyfile_mode(from, to, 0755);
  115. }
  116. unsigned long convert_unit(unsigned long value, char *unit)
  117. {
  118. *unit = ' ';
  119. if (value > 1000) {
  120. value /= 1000;
  121. *unit = 'K';
  122. }
  123. if (value > 1000) {
  124. value /= 1000;
  125. *unit = 'M';
  126. }
  127. if (value > 1000) {
  128. value /= 1000;
  129. *unit = 'G';
  130. }
  131. return value;
  132. }
  133. static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
  134. {
  135. void *buf_start = buf;
  136. size_t left = n;
  137. while (left) {
  138. ssize_t ret = is_read ? read(fd, buf, left) :
  139. write(fd, buf, left);
  140. if (ret < 0 && errno == EINTR)
  141. continue;
  142. if (ret <= 0)
  143. return ret;
  144. left -= ret;
  145. buf += ret;
  146. }
  147. BUG_ON((size_t)(buf - buf_start) != n);
  148. return n;
  149. }
  150. /*
  151. * Read exactly 'n' bytes or return an error.
  152. */
  153. ssize_t readn(int fd, void *buf, size_t n)
  154. {
  155. return ion(true, fd, buf, n);
  156. }
  157. /*
  158. * Write exactly 'n' bytes or return an error.
  159. */
  160. ssize_t writen(int fd, void *buf, size_t n)
  161. {
  162. return ion(false, fd, buf, n);
  163. }
  164. size_t hex_width(u64 v)
  165. {
  166. size_t n = 1;
  167. while ((v >>= 4))
  168. ++n;
  169. return n;
  170. }
  171. static int hex(char ch)
  172. {
  173. if ((ch >= '0') && (ch <= '9'))
  174. return ch - '0';
  175. if ((ch >= 'a') && (ch <= 'f'))
  176. return ch - 'a' + 10;
  177. if ((ch >= 'A') && (ch <= 'F'))
  178. return ch - 'A' + 10;
  179. return -1;
  180. }
  181. /*
  182. * While we find nice hex chars, build a long_val.
  183. * Return number of chars processed.
  184. */
  185. int hex2u64(const char *ptr, u64 *long_val)
  186. {
  187. const char *p = ptr;
  188. *long_val = 0;
  189. while (*p) {
  190. const int hex_val = hex(*p);
  191. if (hex_val < 0)
  192. break;
  193. *long_val = (*long_val << 4) | hex_val;
  194. p++;
  195. }
  196. return p - ptr;
  197. }
  198. /* Obtain a backtrace and print it to stdout. */
  199. #ifdef HAVE_BACKTRACE_SUPPORT
  200. void dump_stack(void)
  201. {
  202. void *array[16];
  203. size_t size = backtrace(array, ARRAY_SIZE(array));
  204. char **strings = backtrace_symbols(array, size);
  205. size_t i;
  206. printf("Obtained %zd stack frames.\n", size);
  207. for (i = 0; i < size; i++)
  208. printf("%s\n", strings[i]);
  209. free(strings);
  210. }
  211. #else
  212. void dump_stack(void) {}
  213. #endif
  214. void get_term_dimensions(struct winsize *ws)
  215. {
  216. char *s = getenv("LINES");
  217. if (s != NULL) {
  218. ws->ws_row = atoi(s);
  219. s = getenv("COLUMNS");
  220. if (s != NULL) {
  221. ws->ws_col = atoi(s);
  222. if (ws->ws_row && ws->ws_col)
  223. return;
  224. }
  225. }
  226. #ifdef TIOCGWINSZ
  227. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  228. ws->ws_row && ws->ws_col)
  229. return;
  230. #endif
  231. ws->ws_row = 25;
  232. ws->ws_col = 80;
  233. }
  234. static void set_tracing_events_path(const char *mountpoint)
  235. {
  236. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
  237. mountpoint, "tracing/events");
  238. }
  239. const char *perf_debugfs_mount(const char *mountpoint)
  240. {
  241. const char *mnt;
  242. mnt = debugfs_mount(mountpoint);
  243. if (!mnt)
  244. return NULL;
  245. set_tracing_events_path(mnt);
  246. return mnt;
  247. }
  248. void perf_debugfs_set_path(const char *mntpt)
  249. {
  250. snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
  251. set_tracing_events_path(mntpt);
  252. }
  253. static const char *find_debugfs(void)
  254. {
  255. const char *path = perf_debugfs_mount(NULL);
  256. if (!path)
  257. fprintf(stderr, "Your kernel does not support the debugfs filesystem");
  258. return path;
  259. }
  260. /*
  261. * Finds the path to the debugfs/tracing
  262. * Allocates the string and stores it.
  263. */
  264. const char *find_tracing_dir(void)
  265. {
  266. static char *tracing;
  267. static int tracing_found;
  268. const char *debugfs;
  269. if (tracing_found)
  270. return tracing;
  271. debugfs = find_debugfs();
  272. if (!debugfs)
  273. return NULL;
  274. if (asprintf(&tracing, "%s/tracing", debugfs) < 0)
  275. return NULL;
  276. tracing_found = 1;
  277. return tracing;
  278. }
  279. char *get_tracing_file(const char *name)
  280. {
  281. const char *tracing;
  282. char *file;
  283. tracing = find_tracing_dir();
  284. if (!tracing)
  285. return NULL;
  286. if (asprintf(&file, "%s/%s", tracing, name) < 0)
  287. return NULL;
  288. return file;
  289. }
  290. void put_tracing_file(char *file)
  291. {
  292. free(file);
  293. }
  294. int parse_nsec_time(const char *str, u64 *ptime)
  295. {
  296. u64 time_sec, time_nsec;
  297. char *end;
  298. time_sec = strtoul(str, &end, 10);
  299. if (*end != '.' && *end != '\0')
  300. return -1;
  301. if (*end == '.') {
  302. int i;
  303. char nsec_buf[10];
  304. if (strlen(++end) > 9)
  305. return -1;
  306. strncpy(nsec_buf, end, 9);
  307. nsec_buf[9] = '\0';
  308. /* make it nsec precision */
  309. for (i = strlen(nsec_buf); i < 9; i++)
  310. nsec_buf[i] = '0';
  311. time_nsec = strtoul(nsec_buf, &end, 10);
  312. if (*end != '\0')
  313. return -1;
  314. } else
  315. time_nsec = 0;
  316. *ptime = time_sec * NSEC_PER_SEC + time_nsec;
  317. return 0;
  318. }
  319. unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
  320. {
  321. struct parse_tag *i = tags;
  322. while (i->tag) {
  323. char *s;
  324. s = strchr(str, i->tag);
  325. if (s) {
  326. unsigned long int value;
  327. char *endptr;
  328. value = strtoul(str, &endptr, 10);
  329. if (s != endptr)
  330. break;
  331. if (value > ULONG_MAX / i->mult)
  332. break;
  333. value *= i->mult;
  334. return value;
  335. }
  336. i++;
  337. }
  338. return (unsigned long) -1;
  339. }
  340. int filename__read_int(const char *filename, int *value)
  341. {
  342. char line[64];
  343. int fd = open(filename, O_RDONLY), err = -1;
  344. if (fd < 0)
  345. return -1;
  346. if (read(fd, line, sizeof(line)) > 0) {
  347. *value = atoi(line);
  348. err = 0;
  349. }
  350. close(fd);
  351. return err;
  352. }
  353. int filename__read_str(const char *filename, char **buf, size_t *sizep)
  354. {
  355. size_t size = 0, alloc_size = 0;
  356. void *bf = NULL, *nbf;
  357. int fd, n, err = 0;
  358. fd = open(filename, O_RDONLY);
  359. if (fd < 0)
  360. return -errno;
  361. do {
  362. if (size == alloc_size) {
  363. alloc_size += BUFSIZ;
  364. nbf = realloc(bf, alloc_size);
  365. if (!nbf) {
  366. err = -ENOMEM;
  367. break;
  368. }
  369. bf = nbf;
  370. }
  371. n = read(fd, bf + size, alloc_size - size);
  372. if (n < 0) {
  373. if (size) {
  374. pr_warning("read failed %d: %s\n",
  375. errno, strerror(errno));
  376. err = 0;
  377. } else
  378. err = -errno;
  379. break;
  380. }
  381. size += n;
  382. } while (n > 0);
  383. if (!err) {
  384. *sizep = size;
  385. *buf = bf;
  386. } else
  387. free(bf);
  388. close(fd);
  389. return err;
  390. }
  391. const char *get_filename_for_perf_kvm(void)
  392. {
  393. const char *filename;
  394. if (perf_host && !perf_guest)
  395. filename = strdup("perf.data.host");
  396. else if (!perf_host && perf_guest)
  397. filename = strdup("perf.data.guest");
  398. else
  399. filename = strdup("perf.data.kvm");
  400. return filename;
  401. }
  402. int perf_event_paranoid(void)
  403. {
  404. char path[PATH_MAX];
  405. const char *procfs = procfs__mountpoint();
  406. int value;
  407. if (!procfs)
  408. return INT_MAX;
  409. scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
  410. if (filename__read_int(path, &value))
  411. return INT_MAX;
  412. return value;
  413. }
  414. void mem_bswap_32(void *src, int byte_size)
  415. {
  416. u32 *m = src;
  417. while (byte_size > 0) {
  418. *m = bswap_32(*m);
  419. byte_size -= sizeof(u32);
  420. ++m;
  421. }
  422. }
  423. void mem_bswap_64(void *src, int byte_size)
  424. {
  425. u64 *m = src;
  426. while (byte_size > 0) {
  427. *m = bswap_64(*m);
  428. byte_size -= sizeof(u64);
  429. ++m;
  430. }
  431. }