util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 perf_host = true;
  14. bool perf_guest = false;
  15. void event_attr_init(struct perf_event_attr *attr)
  16. {
  17. if (!perf_host)
  18. attr->exclude_host = 1;
  19. if (!perf_guest)
  20. attr->exclude_guest = 1;
  21. /* to capture ABI version */
  22. attr->size = sizeof(*attr);
  23. }
  24. int mkdir_p(char *path, mode_t mode)
  25. {
  26. struct stat st;
  27. int err;
  28. char *d = path;
  29. if (*d != '/')
  30. return -1;
  31. if (stat(path, &st) == 0)
  32. return 0;
  33. while (*++d == '/');
  34. while ((d = strchr(d, '/'))) {
  35. *d = '\0';
  36. err = stat(path, &st) && mkdir(path, mode);
  37. *d++ = '/';
  38. if (err)
  39. return -1;
  40. while (*d == '/')
  41. ++d;
  42. }
  43. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  44. }
  45. static int slow_copyfile(const char *from, const char *to)
  46. {
  47. int err = 0;
  48. char *line = NULL;
  49. size_t n;
  50. FILE *from_fp = fopen(from, "r"), *to_fp;
  51. if (from_fp == NULL)
  52. goto out;
  53. to_fp = fopen(to, "w");
  54. if (to_fp == NULL)
  55. goto out_fclose_from;
  56. while (getline(&line, &n, from_fp) > 0)
  57. if (fputs(line, to_fp) == EOF)
  58. goto out_fclose_to;
  59. err = 0;
  60. out_fclose_to:
  61. fclose(to_fp);
  62. free(line);
  63. out_fclose_from:
  64. fclose(from_fp);
  65. out:
  66. return err;
  67. }
  68. int copyfile(const char *from, const char *to)
  69. {
  70. int fromfd, tofd;
  71. struct stat st;
  72. void *addr;
  73. int err = -1;
  74. if (stat(from, &st))
  75. goto out;
  76. if (st.st_size == 0) /* /proc? do it slowly... */
  77. return slow_copyfile(from, to);
  78. fromfd = open(from, O_RDONLY);
  79. if (fromfd < 0)
  80. goto out;
  81. tofd = creat(to, 0755);
  82. if (tofd < 0)
  83. goto out_close_from;
  84. addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
  85. if (addr == MAP_FAILED)
  86. goto out_close_to;
  87. if (write(tofd, addr, st.st_size) == st.st_size)
  88. err = 0;
  89. munmap(addr, st.st_size);
  90. out_close_to:
  91. close(tofd);
  92. if (err)
  93. unlink(to);
  94. out_close_from:
  95. close(fromfd);
  96. out:
  97. return err;
  98. }
  99. unsigned long convert_unit(unsigned long value, char *unit)
  100. {
  101. *unit = ' ';
  102. if (value > 1000) {
  103. value /= 1000;
  104. *unit = 'K';
  105. }
  106. if (value > 1000) {
  107. value /= 1000;
  108. *unit = 'M';
  109. }
  110. if (value > 1000) {
  111. value /= 1000;
  112. *unit = 'G';
  113. }
  114. return value;
  115. }
  116. int readn(int fd, void *buf, size_t n)
  117. {
  118. void *buf_start = buf;
  119. while (n) {
  120. int ret = read(fd, buf, n);
  121. if (ret <= 0)
  122. return ret;
  123. n -= ret;
  124. buf += ret;
  125. }
  126. return buf - buf_start;
  127. }
  128. size_t hex_width(u64 v)
  129. {
  130. size_t n = 1;
  131. while ((v >>= 4))
  132. ++n;
  133. return n;
  134. }
  135. /* Obtain a backtrace and print it to stdout. */
  136. #ifdef BACKTRACE_SUPPORT
  137. void dump_stack(void)
  138. {
  139. void *array[16];
  140. size_t size = backtrace(array, ARRAY_SIZE(array));
  141. char **strings = backtrace_symbols(array, size);
  142. size_t i;
  143. printf("Obtained %zd stack frames.\n", size);
  144. for (i = 0; i < size; i++)
  145. printf("%s\n", strings[i]);
  146. free(strings);
  147. }
  148. #else
  149. void dump_stack(void) {}
  150. #endif