util.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. }