util.c 2.9 KB

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