util.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. void event_attr_init(struct perf_event_attr *attr)
  17. {
  18. if (!perf_host)
  19. attr->exclude_host = 1;
  20. if (!perf_guest)
  21. attr->exclude_guest = 1;
  22. /* to capture ABI version */
  23. attr->size = sizeof(*attr);
  24. }
  25. int mkdir_p(char *path, mode_t mode)
  26. {
  27. struct stat st;
  28. int err;
  29. char *d = path;
  30. if (*d != '/')
  31. return -1;
  32. if (stat(path, &st) == 0)
  33. return 0;
  34. while (*++d == '/');
  35. while ((d = strchr(d, '/'))) {
  36. *d = '\0';
  37. err = stat(path, &st) && mkdir(path, mode);
  38. *d++ = '/';
  39. if (err)
  40. return -1;
  41. while (*d == '/')
  42. ++d;
  43. }
  44. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  45. }
  46. static int slow_copyfile(const char *from, const char *to)
  47. {
  48. int err = 0;
  49. char *line = NULL;
  50. size_t n;
  51. FILE *from_fp = fopen(from, "r"), *to_fp;
  52. if (from_fp == NULL)
  53. goto out;
  54. to_fp = fopen(to, "w");
  55. if (to_fp == NULL)
  56. goto out_fclose_from;
  57. while (getline(&line, &n, from_fp) > 0)
  58. if (fputs(line, to_fp) == EOF)
  59. goto out_fclose_to;
  60. err = 0;
  61. out_fclose_to:
  62. fclose(to_fp);
  63. free(line);
  64. out_fclose_from:
  65. fclose(from_fp);
  66. out:
  67. return err;
  68. }
  69. int copyfile(const char *from, const char *to)
  70. {
  71. int fromfd, tofd;
  72. struct stat st;
  73. void *addr;
  74. int err = -1;
  75. if (stat(from, &st))
  76. goto out;
  77. if (st.st_size == 0) /* /proc? do it slowly... */
  78. return slow_copyfile(from, to);
  79. fromfd = open(from, O_RDONLY);
  80. if (fromfd < 0)
  81. goto out;
  82. tofd = creat(to, 0755);
  83. if (tofd < 0)
  84. goto out_close_from;
  85. addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
  86. if (addr == MAP_FAILED)
  87. goto out_close_to;
  88. if (write(tofd, addr, st.st_size) == st.st_size)
  89. err = 0;
  90. munmap(addr, st.st_size);
  91. out_close_to:
  92. close(tofd);
  93. if (err)
  94. unlink(to);
  95. out_close_from:
  96. close(fromfd);
  97. out:
  98. return err;
  99. }
  100. unsigned long convert_unit(unsigned long value, char *unit)
  101. {
  102. *unit = ' ';
  103. if (value > 1000) {
  104. value /= 1000;
  105. *unit = 'K';
  106. }
  107. if (value > 1000) {
  108. value /= 1000;
  109. *unit = 'M';
  110. }
  111. if (value > 1000) {
  112. value /= 1000;
  113. *unit = 'G';
  114. }
  115. return value;
  116. }
  117. int readn(int fd, void *buf, size_t n)
  118. {
  119. void *buf_start = buf;
  120. while (n) {
  121. int ret = read(fd, buf, n);
  122. if (ret <= 0)
  123. return ret;
  124. n -= ret;
  125. buf += ret;
  126. }
  127. return buf - buf_start;
  128. }
  129. size_t hex_width(u64 v)
  130. {
  131. size_t n = 1;
  132. while ((v >>= 4))
  133. ++n;
  134. return n;
  135. }
  136. static int hex(char ch)
  137. {
  138. if ((ch >= '0') && (ch <= '9'))
  139. return ch - '0';
  140. if ((ch >= 'a') && (ch <= 'f'))
  141. return ch - 'a' + 10;
  142. if ((ch >= 'A') && (ch <= 'F'))
  143. return ch - 'A' + 10;
  144. return -1;
  145. }
  146. /*
  147. * While we find nice hex chars, build a long_val.
  148. * Return number of chars processed.
  149. */
  150. int hex2u64(const char *ptr, u64 *long_val)
  151. {
  152. const char *p = ptr;
  153. *long_val = 0;
  154. while (*p) {
  155. const int hex_val = hex(*p);
  156. if (hex_val < 0)
  157. break;
  158. *long_val = (*long_val << 4) | hex_val;
  159. p++;
  160. }
  161. return p - ptr;
  162. }
  163. /* Obtain a backtrace and print it to stdout. */
  164. #ifdef BACKTRACE_SUPPORT
  165. void dump_stack(void)
  166. {
  167. void *array[16];
  168. size_t size = backtrace(array, ARRAY_SIZE(array));
  169. char **strings = backtrace_symbols(array, size);
  170. size_t i;
  171. printf("Obtained %zd stack frames.\n", size);
  172. for (i = 0; i < size; i++)
  173. printf("%s\n", strings[i]);
  174. free(strings);
  175. }
  176. #else
  177. void dump_stack(void) {}
  178. #endif
  179. void get_term_dimensions(struct winsize *ws)
  180. {
  181. char *s = getenv("LINES");
  182. if (s != NULL) {
  183. ws->ws_row = atoi(s);
  184. s = getenv("COLUMNS");
  185. if (s != NULL) {
  186. ws->ws_col = atoi(s);
  187. if (ws->ws_row && ws->ws_col)
  188. return;
  189. }
  190. }
  191. #ifdef TIOCGWINSZ
  192. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  193. ws->ws_row && ws->ws_col)
  194. return;
  195. #endif
  196. ws->ws_row = 25;
  197. ws->ws_col = 80;
  198. }