util.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #include "../perf.h"
  2. #include "util.h"
  3. #include <sys/mman.h>
  4. #ifdef 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)
  48. {
  49. int err = 0;
  50. char *line = NULL;
  51. size_t n;
  52. FILE *from_fp = fopen(from, "r"), *to_fp;
  53. if (from_fp == NULL)
  54. goto out;
  55. to_fp = fopen(to, "w");
  56. if (to_fp == NULL)
  57. goto out_fclose_from;
  58. while (getline(&line, &n, from_fp) > 0)
  59. if (fputs(line, to_fp) == EOF)
  60. goto out_fclose_to;
  61. err = 0;
  62. out_fclose_to:
  63. fclose(to_fp);
  64. free(line);
  65. out_fclose_from:
  66. fclose(from_fp);
  67. out:
  68. return err;
  69. }
  70. int copyfile(const char *from, const char *to)
  71. {
  72. int fromfd, tofd;
  73. struct stat st;
  74. void *addr;
  75. int err = -1;
  76. if (stat(from, &st))
  77. goto out;
  78. if (st.st_size == 0) /* /proc? do it slowly... */
  79. return slow_copyfile(from, to);
  80. fromfd = open(from, O_RDONLY);
  81. if (fromfd < 0)
  82. goto out;
  83. tofd = creat(to, 0755);
  84. if (tofd < 0)
  85. goto out_close_from;
  86. addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
  87. if (addr == MAP_FAILED)
  88. goto out_close_to;
  89. if (write(tofd, addr, st.st_size) == st.st_size)
  90. err = 0;
  91. munmap(addr, st.st_size);
  92. out_close_to:
  93. close(tofd);
  94. if (err)
  95. unlink(to);
  96. out_close_from:
  97. close(fromfd);
  98. out:
  99. return err;
  100. }
  101. unsigned long convert_unit(unsigned long value, char *unit)
  102. {
  103. *unit = ' ';
  104. if (value > 1000) {
  105. value /= 1000;
  106. *unit = 'K';
  107. }
  108. if (value > 1000) {
  109. value /= 1000;
  110. *unit = 'M';
  111. }
  112. if (value > 1000) {
  113. value /= 1000;
  114. *unit = 'G';
  115. }
  116. return value;
  117. }
  118. int readn(int fd, void *buf, size_t n)
  119. {
  120. void *buf_start = buf;
  121. while (n) {
  122. int ret = read(fd, buf, n);
  123. if (ret <= 0)
  124. return ret;
  125. n -= ret;
  126. buf += ret;
  127. }
  128. return buf - buf_start;
  129. }
  130. size_t hex_width(u64 v)
  131. {
  132. size_t n = 1;
  133. while ((v >>= 4))
  134. ++n;
  135. return n;
  136. }
  137. static int hex(char ch)
  138. {
  139. if ((ch >= '0') && (ch <= '9'))
  140. return ch - '0';
  141. if ((ch >= 'a') && (ch <= 'f'))
  142. return ch - 'a' + 10;
  143. if ((ch >= 'A') && (ch <= 'F'))
  144. return ch - 'A' + 10;
  145. return -1;
  146. }
  147. /*
  148. * While we find nice hex chars, build a long_val.
  149. * Return number of chars processed.
  150. */
  151. int hex2u64(const char *ptr, u64 *long_val)
  152. {
  153. const char *p = ptr;
  154. *long_val = 0;
  155. while (*p) {
  156. const int hex_val = hex(*p);
  157. if (hex_val < 0)
  158. break;
  159. *long_val = (*long_val << 4) | hex_val;
  160. p++;
  161. }
  162. return p - ptr;
  163. }
  164. /* Obtain a backtrace and print it to stdout. */
  165. #ifdef BACKTRACE_SUPPORT
  166. void dump_stack(void)
  167. {
  168. void *array[16];
  169. size_t size = backtrace(array, ARRAY_SIZE(array));
  170. char **strings = backtrace_symbols(array, size);
  171. size_t i;
  172. printf("Obtained %zd stack frames.\n", size);
  173. for (i = 0; i < size; i++)
  174. printf("%s\n", strings[i]);
  175. free(strings);
  176. }
  177. #else
  178. void dump_stack(void) {}
  179. #endif
  180. void get_term_dimensions(struct winsize *ws)
  181. {
  182. char *s = getenv("LINES");
  183. if (s != NULL) {
  184. ws->ws_row = atoi(s);
  185. s = getenv("COLUMNS");
  186. if (s != NULL) {
  187. ws->ws_col = atoi(s);
  188. if (ws->ws_row && ws->ws_col)
  189. return;
  190. }
  191. }
  192. #ifdef TIOCGWINSZ
  193. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  194. ws->ws_row && ws->ws_col)
  195. return;
  196. #endif
  197. ws->ws_row = 25;
  198. ws->ws_col = 80;
  199. }
  200. static void set_tracing_events_path(const char *mountpoint)
  201. {
  202. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
  203. mountpoint, "tracing/events");
  204. }
  205. const char *perf_debugfs_mount(const char *mountpoint)
  206. {
  207. const char *mnt;
  208. mnt = debugfs_mount(mountpoint);
  209. if (!mnt)
  210. return NULL;
  211. set_tracing_events_path(mnt);
  212. return mnt;
  213. }
  214. void perf_debugfs_set_path(const char *mntpt)
  215. {
  216. snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
  217. set_tracing_events_path(mntpt);
  218. }
  219. static const char *find_debugfs(void)
  220. {
  221. const char *path = perf_debugfs_mount(NULL);
  222. if (!path)
  223. fprintf(stderr, "Your kernel does not support the debugfs filesystem");
  224. return path;
  225. }
  226. /*
  227. * Finds the path to the debugfs/tracing
  228. * Allocates the string and stores it.
  229. */
  230. const char *find_tracing_dir(void)
  231. {
  232. static char *tracing;
  233. static int tracing_found;
  234. const char *debugfs;
  235. if (tracing_found)
  236. return tracing;
  237. debugfs = find_debugfs();
  238. if (!debugfs)
  239. return NULL;
  240. tracing = malloc(strlen(debugfs) + 9);
  241. if (!tracing)
  242. return NULL;
  243. sprintf(tracing, "%s/tracing", debugfs);
  244. tracing_found = 1;
  245. return tracing;
  246. }
  247. char *get_tracing_file(const char *name)
  248. {
  249. const char *tracing;
  250. char *file;
  251. tracing = find_tracing_dir();
  252. if (!tracing)
  253. return NULL;
  254. file = malloc(strlen(tracing) + strlen(name) + 2);
  255. if (!file)
  256. return NULL;
  257. sprintf(file, "%s/%s", tracing, name);
  258. return file;
  259. }
  260. void put_tracing_file(char *file)
  261. {
  262. free(file);
  263. }
  264. int parse_nsec_time(const char *str, u64 *ptime)
  265. {
  266. u64 time_sec, time_nsec;
  267. char *end;
  268. time_sec = strtoul(str, &end, 10);
  269. if (*end != '.' && *end != '\0')
  270. return -1;
  271. if (*end == '.') {
  272. int i;
  273. char nsec_buf[10];
  274. if (strlen(++end) > 9)
  275. return -1;
  276. strncpy(nsec_buf, end, 9);
  277. nsec_buf[9] = '\0';
  278. /* make it nsec precision */
  279. for (i = strlen(nsec_buf); i < 9; i++)
  280. nsec_buf[i] = '0';
  281. time_nsec = strtoul(nsec_buf, &end, 10);
  282. if (*end != '\0')
  283. return -1;
  284. } else
  285. time_nsec = 0;
  286. *ptime = time_sec * NSEC_PER_SEC + time_nsec;
  287. return 0;
  288. }