util.c 8.5 KB

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