util.c 9.5 KB

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