util.c 8.9 KB

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