util.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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)
  139. return ret;
  140. left -= ret;
  141. buf += ret;
  142. }
  143. BUG_ON((size_t)(buf - buf_start) != n);
  144. return n;
  145. }
  146. /*
  147. * Read exactly 'n' bytes or return an error.
  148. */
  149. ssize_t readn(int fd, void *buf, size_t n)
  150. {
  151. return ion(true, fd, buf, n);
  152. }
  153. /*
  154. * Write exactly 'n' bytes or return an error.
  155. */
  156. ssize_t writen(int fd, void *buf, size_t n)
  157. {
  158. return ion(false, fd, buf, n);
  159. }
  160. size_t hex_width(u64 v)
  161. {
  162. size_t n = 1;
  163. while ((v >>= 4))
  164. ++n;
  165. return n;
  166. }
  167. static int hex(char ch)
  168. {
  169. if ((ch >= '0') && (ch <= '9'))
  170. return ch - '0';
  171. if ((ch >= 'a') && (ch <= 'f'))
  172. return ch - 'a' + 10;
  173. if ((ch >= 'A') && (ch <= 'F'))
  174. return ch - 'A' + 10;
  175. return -1;
  176. }
  177. /*
  178. * While we find nice hex chars, build a long_val.
  179. * Return number of chars processed.
  180. */
  181. int hex2u64(const char *ptr, u64 *long_val)
  182. {
  183. const char *p = ptr;
  184. *long_val = 0;
  185. while (*p) {
  186. const int hex_val = hex(*p);
  187. if (hex_val < 0)
  188. break;
  189. *long_val = (*long_val << 4) | hex_val;
  190. p++;
  191. }
  192. return p - ptr;
  193. }
  194. /* Obtain a backtrace and print it to stdout. */
  195. #ifdef HAVE_BACKTRACE_SUPPORT
  196. void dump_stack(void)
  197. {
  198. void *array[16];
  199. size_t size = backtrace(array, ARRAY_SIZE(array));
  200. char **strings = backtrace_symbols(array, size);
  201. size_t i;
  202. printf("Obtained %zd stack frames.\n", size);
  203. for (i = 0; i < size; i++)
  204. printf("%s\n", strings[i]);
  205. free(strings);
  206. }
  207. #else
  208. void dump_stack(void) {}
  209. #endif
  210. void get_term_dimensions(struct winsize *ws)
  211. {
  212. char *s = getenv("LINES");
  213. if (s != NULL) {
  214. ws->ws_row = atoi(s);
  215. s = getenv("COLUMNS");
  216. if (s != NULL) {
  217. ws->ws_col = atoi(s);
  218. if (ws->ws_row && ws->ws_col)
  219. return;
  220. }
  221. }
  222. #ifdef TIOCGWINSZ
  223. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  224. ws->ws_row && ws->ws_col)
  225. return;
  226. #endif
  227. ws->ws_row = 25;
  228. ws->ws_col = 80;
  229. }
  230. static void set_tracing_events_path(const char *mountpoint)
  231. {
  232. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
  233. mountpoint, "tracing/events");
  234. }
  235. const char *perf_debugfs_mount(const char *mountpoint)
  236. {
  237. const char *mnt;
  238. mnt = debugfs_mount(mountpoint);
  239. if (!mnt)
  240. return NULL;
  241. set_tracing_events_path(mnt);
  242. return mnt;
  243. }
  244. void perf_debugfs_set_path(const char *mntpt)
  245. {
  246. snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
  247. set_tracing_events_path(mntpt);
  248. }
  249. static const char *find_debugfs(void)
  250. {
  251. const char *path = perf_debugfs_mount(NULL);
  252. if (!path)
  253. fprintf(stderr, "Your kernel does not support the debugfs filesystem");
  254. return path;
  255. }
  256. /*
  257. * Finds the path to the debugfs/tracing
  258. * Allocates the string and stores it.
  259. */
  260. const char *find_tracing_dir(void)
  261. {
  262. static char *tracing;
  263. static int tracing_found;
  264. const char *debugfs;
  265. if (tracing_found)
  266. return tracing;
  267. debugfs = find_debugfs();
  268. if (!debugfs)
  269. return NULL;
  270. tracing = malloc(strlen(debugfs) + 9);
  271. if (!tracing)
  272. return NULL;
  273. sprintf(tracing, "%s/tracing", debugfs);
  274. tracing_found = 1;
  275. return tracing;
  276. }
  277. char *get_tracing_file(const char *name)
  278. {
  279. const char *tracing;
  280. char *file;
  281. tracing = find_tracing_dir();
  282. if (!tracing)
  283. return NULL;
  284. file = malloc(strlen(tracing) + strlen(name) + 2);
  285. if (!file)
  286. return NULL;
  287. sprintf(file, "%s/%s", tracing, name);
  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. }