util.c 2.8 KB

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