util.c 8.2 KB

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