util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. #include <unistd.h>
  17. #include "callchain.h"
  18. struct callchain_param callchain_param = {
  19. .mode = CHAIN_GRAPH_REL,
  20. .min_percent = 0.5,
  21. .order = ORDER_CALLEE,
  22. .key = CCKEY_FUNCTION
  23. };
  24. /*
  25. * XXX We need to find a better place for these things...
  26. */
  27. unsigned int page_size;
  28. int cacheline_size;
  29. bool test_attr__enabled;
  30. bool perf_host = true;
  31. bool perf_guest = false;
  32. char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
  33. void event_attr_init(struct perf_event_attr *attr)
  34. {
  35. if (!perf_host)
  36. attr->exclude_host = 1;
  37. if (!perf_guest)
  38. attr->exclude_guest = 1;
  39. /* to capture ABI version */
  40. attr->size = sizeof(*attr);
  41. }
  42. int mkdir_p(char *path, mode_t mode)
  43. {
  44. struct stat st;
  45. int err;
  46. char *d = path;
  47. if (*d != '/')
  48. return -1;
  49. if (stat(path, &st) == 0)
  50. return 0;
  51. while (*++d == '/');
  52. while ((d = strchr(d, '/'))) {
  53. *d = '\0';
  54. err = stat(path, &st) && mkdir(path, mode);
  55. *d++ = '/';
  56. if (err)
  57. return -1;
  58. while (*d == '/')
  59. ++d;
  60. }
  61. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  62. }
  63. int rm_rf(char *path)
  64. {
  65. DIR *dir;
  66. int ret = 0;
  67. struct dirent *d;
  68. char namebuf[PATH_MAX];
  69. dir = opendir(path);
  70. if (dir == NULL)
  71. return 0;
  72. while ((d = readdir(dir)) != NULL && !ret) {
  73. struct stat statbuf;
  74. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  75. continue;
  76. scnprintf(namebuf, sizeof(namebuf), "%s/%s",
  77. path, d->d_name);
  78. ret = stat(namebuf, &statbuf);
  79. if (ret < 0) {
  80. pr_debug("stat failed: %s\n", namebuf);
  81. break;
  82. }
  83. if (S_ISREG(statbuf.st_mode))
  84. ret = unlink(namebuf);
  85. else if (S_ISDIR(statbuf.st_mode))
  86. ret = rm_rf(namebuf);
  87. else {
  88. pr_debug("unknown file: %s\n", namebuf);
  89. ret = -1;
  90. }
  91. }
  92. closedir(dir);
  93. if (ret < 0)
  94. return ret;
  95. return rmdir(path);
  96. }
  97. static int slow_copyfile(const char *from, const char *to, mode_t mode)
  98. {
  99. int err = -1;
  100. char *line = NULL;
  101. size_t n;
  102. FILE *from_fp = fopen(from, "r"), *to_fp;
  103. mode_t old_umask;
  104. if (from_fp == NULL)
  105. goto out;
  106. old_umask = umask(mode ^ 0777);
  107. to_fp = fopen(to, "w");
  108. umask(old_umask);
  109. if (to_fp == NULL)
  110. goto out_fclose_from;
  111. while (getline(&line, &n, from_fp) > 0)
  112. if (fputs(line, to_fp) == EOF)
  113. goto out_fclose_to;
  114. err = 0;
  115. out_fclose_to:
  116. fclose(to_fp);
  117. free(line);
  118. out_fclose_from:
  119. fclose(from_fp);
  120. out:
  121. return err;
  122. }
  123. int copyfile_mode(const char *from, const char *to, mode_t mode)
  124. {
  125. int fromfd, tofd;
  126. struct stat st;
  127. void *addr;
  128. int err = -1;
  129. if (stat(from, &st))
  130. goto out;
  131. if (st.st_size == 0) /* /proc? do it slowly... */
  132. return slow_copyfile(from, to, mode);
  133. fromfd = open(from, O_RDONLY);
  134. if (fromfd < 0)
  135. goto out;
  136. tofd = creat(to, mode);
  137. if (tofd < 0)
  138. goto out_close_from;
  139. addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
  140. if (addr == MAP_FAILED)
  141. goto out_close_to;
  142. if (write(tofd, addr, st.st_size) == st.st_size)
  143. err = 0;
  144. munmap(addr, st.st_size);
  145. out_close_to:
  146. close(tofd);
  147. if (err)
  148. unlink(to);
  149. out_close_from:
  150. close(fromfd);
  151. out:
  152. return err;
  153. }
  154. int copyfile(const char *from, const char *to)
  155. {
  156. return copyfile_mode(from, to, 0755);
  157. }
  158. unsigned long convert_unit(unsigned long value, char *unit)
  159. {
  160. *unit = ' ';
  161. if (value > 1000) {
  162. value /= 1000;
  163. *unit = 'K';
  164. }
  165. if (value > 1000) {
  166. value /= 1000;
  167. *unit = 'M';
  168. }
  169. if (value > 1000) {
  170. value /= 1000;
  171. *unit = 'G';
  172. }
  173. return value;
  174. }
  175. static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
  176. {
  177. void *buf_start = buf;
  178. size_t left = n;
  179. while (left) {
  180. ssize_t ret = is_read ? read(fd, buf, left) :
  181. write(fd, buf, left);
  182. if (ret < 0 && errno == EINTR)
  183. continue;
  184. if (ret <= 0)
  185. return ret;
  186. left -= ret;
  187. buf += ret;
  188. }
  189. BUG_ON((size_t)(buf - buf_start) != n);
  190. return n;
  191. }
  192. /*
  193. * Read exactly 'n' bytes or return an error.
  194. */
  195. ssize_t readn(int fd, void *buf, size_t n)
  196. {
  197. return ion(true, fd, buf, n);
  198. }
  199. /*
  200. * Write exactly 'n' bytes or return an error.
  201. */
  202. ssize_t writen(int fd, void *buf, size_t n)
  203. {
  204. return ion(false, fd, buf, n);
  205. }
  206. size_t hex_width(u64 v)
  207. {
  208. size_t n = 1;
  209. while ((v >>= 4))
  210. ++n;
  211. return n;
  212. }
  213. static int hex(char ch)
  214. {
  215. if ((ch >= '0') && (ch <= '9'))
  216. return ch - '0';
  217. if ((ch >= 'a') && (ch <= 'f'))
  218. return ch - 'a' + 10;
  219. if ((ch >= 'A') && (ch <= 'F'))
  220. return ch - 'A' + 10;
  221. return -1;
  222. }
  223. /*
  224. * While we find nice hex chars, build a long_val.
  225. * Return number of chars processed.
  226. */
  227. int hex2u64(const char *ptr, u64 *long_val)
  228. {
  229. const char *p = ptr;
  230. *long_val = 0;
  231. while (*p) {
  232. const int hex_val = hex(*p);
  233. if (hex_val < 0)
  234. break;
  235. *long_val = (*long_val << 4) | hex_val;
  236. p++;
  237. }
  238. return p - ptr;
  239. }
  240. /* Obtain a backtrace and print it to stdout. */
  241. #ifdef HAVE_BACKTRACE_SUPPORT
  242. void dump_stack(void)
  243. {
  244. void *array[16];
  245. size_t size = backtrace(array, ARRAY_SIZE(array));
  246. char **strings = backtrace_symbols(array, size);
  247. size_t i;
  248. printf("Obtained %zd stack frames.\n", size);
  249. for (i = 0; i < size; i++)
  250. printf("%s\n", strings[i]);
  251. free(strings);
  252. }
  253. #else
  254. void dump_stack(void) {}
  255. #endif
  256. void sighandler_dump_stack(int sig)
  257. {
  258. psignal(sig, "perf");
  259. dump_stack();
  260. exit(sig);
  261. }
  262. void get_term_dimensions(struct winsize *ws)
  263. {
  264. char *s = getenv("LINES");
  265. if (s != NULL) {
  266. ws->ws_row = atoi(s);
  267. s = getenv("COLUMNS");
  268. if (s != NULL) {
  269. ws->ws_col = atoi(s);
  270. if (ws->ws_row && ws->ws_col)
  271. return;
  272. }
  273. }
  274. #ifdef TIOCGWINSZ
  275. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  276. ws->ws_row && ws->ws_col)
  277. return;
  278. #endif
  279. ws->ws_row = 25;
  280. ws->ws_col = 80;
  281. }
  282. void set_term_quiet_input(struct termios *old)
  283. {
  284. struct termios tc;
  285. tcgetattr(0, old);
  286. tc = *old;
  287. tc.c_lflag &= ~(ICANON | ECHO);
  288. tc.c_cc[VMIN] = 0;
  289. tc.c_cc[VTIME] = 0;
  290. tcsetattr(0, TCSANOW, &tc);
  291. }
  292. static void set_tracing_events_path(const char *tracing, const char *mountpoint)
  293. {
  294. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
  295. mountpoint, tracing, "events");
  296. }
  297. static const char *__perf_tracefs_mount(const char *mountpoint)
  298. {
  299. const char *mnt;
  300. mnt = tracefs_mount(mountpoint);
  301. if (!mnt)
  302. return NULL;
  303. set_tracing_events_path("", mnt);
  304. return mnt;
  305. }
  306. static const char *__perf_debugfs_mount(const char *mountpoint)
  307. {
  308. const char *mnt;
  309. mnt = debugfs_mount(mountpoint);
  310. if (!mnt)
  311. return NULL;
  312. set_tracing_events_path("tracing/", mnt);
  313. return mnt;
  314. }
  315. const char *perf_debugfs_mount(const char *mountpoint)
  316. {
  317. const char *mnt;
  318. mnt = __perf_tracefs_mount(mountpoint);
  319. if (mnt)
  320. return mnt;
  321. mnt = __perf_debugfs_mount(mountpoint);
  322. return mnt;
  323. }
  324. void perf_debugfs_set_path(const char *mntpt)
  325. {
  326. snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
  327. set_tracing_events_path("tracing/", mntpt);
  328. }
  329. static const char *find_tracefs(void)
  330. {
  331. const char *path = __perf_tracefs_mount(NULL);
  332. return path;
  333. }
  334. static const char *find_debugfs(void)
  335. {
  336. const char *path = __perf_debugfs_mount(NULL);
  337. if (!path)
  338. fprintf(stderr, "Your kernel does not support the debugfs filesystem");
  339. return path;
  340. }
  341. /*
  342. * Finds the path to the debugfs/tracing
  343. * Allocates the string and stores it.
  344. */
  345. const char *find_tracing_dir(void)
  346. {
  347. const char *tracing_dir = "";
  348. static char *tracing;
  349. static int tracing_found;
  350. const char *debugfs;
  351. if (tracing_found)
  352. return tracing;
  353. debugfs = find_tracefs();
  354. if (!debugfs) {
  355. tracing_dir = "/tracing";
  356. debugfs = find_debugfs();
  357. if (!debugfs)
  358. return NULL;
  359. }
  360. if (asprintf(&tracing, "%s%s", debugfs, tracing_dir) < 0)
  361. return NULL;
  362. tracing_found = 1;
  363. return tracing;
  364. }
  365. char *get_tracing_file(const char *name)
  366. {
  367. const char *tracing;
  368. char *file;
  369. tracing = find_tracing_dir();
  370. if (!tracing)
  371. return NULL;
  372. if (asprintf(&file, "%s/%s", tracing, name) < 0)
  373. return NULL;
  374. return file;
  375. }
  376. void put_tracing_file(char *file)
  377. {
  378. free(file);
  379. }
  380. int parse_nsec_time(const char *str, u64 *ptime)
  381. {
  382. u64 time_sec, time_nsec;
  383. char *end;
  384. time_sec = strtoul(str, &end, 10);
  385. if (*end != '.' && *end != '\0')
  386. return -1;
  387. if (*end == '.') {
  388. int i;
  389. char nsec_buf[10];
  390. if (strlen(++end) > 9)
  391. return -1;
  392. strncpy(nsec_buf, end, 9);
  393. nsec_buf[9] = '\0';
  394. /* make it nsec precision */
  395. for (i = strlen(nsec_buf); i < 9; i++)
  396. nsec_buf[i] = '0';
  397. time_nsec = strtoul(nsec_buf, &end, 10);
  398. if (*end != '\0')
  399. return -1;
  400. } else
  401. time_nsec = 0;
  402. *ptime = time_sec * NSEC_PER_SEC + time_nsec;
  403. return 0;
  404. }
  405. unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
  406. {
  407. struct parse_tag *i = tags;
  408. while (i->tag) {
  409. char *s;
  410. s = strchr(str, i->tag);
  411. if (s) {
  412. unsigned long int value;
  413. char *endptr;
  414. value = strtoul(str, &endptr, 10);
  415. if (s != endptr)
  416. break;
  417. if (value > ULONG_MAX / i->mult)
  418. break;
  419. value *= i->mult;
  420. return value;
  421. }
  422. i++;
  423. }
  424. return (unsigned long) -1;
  425. }
  426. int filename__read_str(const char *filename, char **buf, size_t *sizep)
  427. {
  428. size_t size = 0, alloc_size = 0;
  429. void *bf = NULL, *nbf;
  430. int fd, n, err = 0;
  431. char sbuf[STRERR_BUFSIZE];
  432. fd = open(filename, O_RDONLY);
  433. if (fd < 0)
  434. return -errno;
  435. do {
  436. if (size == alloc_size) {
  437. alloc_size += BUFSIZ;
  438. nbf = realloc(bf, alloc_size);
  439. if (!nbf) {
  440. err = -ENOMEM;
  441. break;
  442. }
  443. bf = nbf;
  444. }
  445. n = read(fd, bf + size, alloc_size - size);
  446. if (n < 0) {
  447. if (size) {
  448. pr_warning("read failed %d: %s\n", errno,
  449. strerror_r(errno, sbuf, sizeof(sbuf)));
  450. err = 0;
  451. } else
  452. err = -errno;
  453. break;
  454. }
  455. size += n;
  456. } while (n > 0);
  457. if (!err) {
  458. *sizep = size;
  459. *buf = bf;
  460. } else
  461. free(bf);
  462. close(fd);
  463. return err;
  464. }
  465. const char *get_filename_for_perf_kvm(void)
  466. {
  467. const char *filename;
  468. if (perf_host && !perf_guest)
  469. filename = strdup("perf.data.host");
  470. else if (!perf_host && perf_guest)
  471. filename = strdup("perf.data.guest");
  472. else
  473. filename = strdup("perf.data.kvm");
  474. return filename;
  475. }
  476. int perf_event_paranoid(void)
  477. {
  478. int value;
  479. if (sysctl__read_int("kernel/perf_event_paranoid", &value))
  480. return INT_MAX;
  481. return value;
  482. }
  483. void mem_bswap_32(void *src, int byte_size)
  484. {
  485. u32 *m = src;
  486. while (byte_size > 0) {
  487. *m = bswap_32(*m);
  488. byte_size -= sizeof(u32);
  489. ++m;
  490. }
  491. }
  492. void mem_bswap_64(void *src, int byte_size)
  493. {
  494. u64 *m = src;
  495. while (byte_size > 0) {
  496. *m = bswap_64(*m);
  497. byte_size -= sizeof(u64);
  498. ++m;
  499. }
  500. }
  501. bool find_process(const char *name)
  502. {
  503. size_t len = strlen(name);
  504. DIR *dir;
  505. struct dirent *d;
  506. int ret = -1;
  507. dir = opendir(procfs__mountpoint());
  508. if (!dir)
  509. return -1;
  510. /* Walk through the directory. */
  511. while (ret && (d = readdir(dir)) != NULL) {
  512. char path[PATH_MAX];
  513. char *data;
  514. size_t size;
  515. if ((d->d_type != DT_DIR) ||
  516. !strcmp(".", d->d_name) ||
  517. !strcmp("..", d->d_name))
  518. continue;
  519. scnprintf(path, sizeof(path), "%s/%s/comm",
  520. procfs__mountpoint(), d->d_name);
  521. if (filename__read_str(path, &data, &size))
  522. continue;
  523. ret = strncmp(name, data, len);
  524. free(data);
  525. }
  526. closedir(dir);
  527. return ret ? false : true;
  528. }