util.c 6.8 KB

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