util.c 8.9 KB

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