util.c 2.3 KB

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