util.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include "../perf.h"
  2. #include "util.h"
  3. #include "debug.h"
  4. #include <api/fs/fs.h>
  5. #include <sys/mman.h>
  6. #include <sys/stat.h>
  7. #include <sys/utsname.h>
  8. #include <dirent.h>
  9. #include <fcntl.h>
  10. #include <inttypes.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include <linux/kernel.h>
  18. #include <linux/log2.h>
  19. #include <linux/time64.h>
  20. #include <unistd.h>
  21. #include "strlist.h"
  22. /*
  23. * XXX We need to find a better place for these things...
  24. */
  25. unsigned int page_size;
  26. int cacheline_size;
  27. int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
  28. int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
  29. bool test_attr__enabled;
  30. bool perf_host = true;
  31. bool perf_guest = false;
  32. void event_attr_init(struct perf_event_attr *attr)
  33. {
  34. if (!perf_host)
  35. attr->exclude_host = 1;
  36. if (!perf_guest)
  37. attr->exclude_guest = 1;
  38. /* to capture ABI version */
  39. attr->size = sizeof(*attr);
  40. }
  41. int mkdir_p(char *path, mode_t mode)
  42. {
  43. struct stat st;
  44. int err;
  45. char *d = path;
  46. if (*d != '/')
  47. return -1;
  48. if (stat(path, &st) == 0)
  49. return 0;
  50. while (*++d == '/');
  51. while ((d = strchr(d, '/'))) {
  52. *d = '\0';
  53. err = stat(path, &st) && mkdir(path, mode);
  54. *d++ = '/';
  55. if (err)
  56. return -1;
  57. while (*d == '/')
  58. ++d;
  59. }
  60. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  61. }
  62. int rm_rf(const char *path)
  63. {
  64. DIR *dir;
  65. int ret = 0;
  66. struct dirent *d;
  67. char namebuf[PATH_MAX];
  68. dir = opendir(path);
  69. if (dir == NULL)
  70. return 0;
  71. while ((d = readdir(dir)) != NULL && !ret) {
  72. struct stat statbuf;
  73. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  74. continue;
  75. scnprintf(namebuf, sizeof(namebuf), "%s/%s",
  76. path, d->d_name);
  77. /* We have to check symbolic link itself */
  78. ret = lstat(namebuf, &statbuf);
  79. if (ret < 0) {
  80. pr_debug("stat failed: %s\n", namebuf);
  81. break;
  82. }
  83. if (S_ISDIR(statbuf.st_mode))
  84. ret = rm_rf(namebuf);
  85. else
  86. ret = unlink(namebuf);
  87. }
  88. closedir(dir);
  89. if (ret < 0)
  90. return ret;
  91. return rmdir(path);
  92. }
  93. /* A filter which removes dot files */
  94. bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
  95. {
  96. return d->d_name[0] != '.';
  97. }
  98. /* lsdir reads a directory and store it in strlist */
  99. struct strlist *lsdir(const char *name,
  100. bool (*filter)(const char *, struct dirent *))
  101. {
  102. struct strlist *list = NULL;
  103. DIR *dir;
  104. struct dirent *d;
  105. dir = opendir(name);
  106. if (!dir)
  107. return NULL;
  108. list = strlist__new(NULL, NULL);
  109. if (!list) {
  110. errno = ENOMEM;
  111. goto out;
  112. }
  113. while ((d = readdir(dir)) != NULL) {
  114. if (!filter || filter(name, d))
  115. strlist__add(list, d->d_name);
  116. }
  117. out:
  118. closedir(dir);
  119. return list;
  120. }
  121. static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
  122. {
  123. int err = -1;
  124. char *line = NULL;
  125. size_t n;
  126. FILE *from_fp, *to_fp;
  127. struct nscookie nsc;
  128. nsinfo__mountns_enter(nsi, &nsc);
  129. from_fp = fopen(from, "r");
  130. nsinfo__mountns_exit(&nsc);
  131. if (from_fp == NULL)
  132. goto out;
  133. to_fp = fopen(to, "w");
  134. if (to_fp == NULL)
  135. goto out_fclose_from;
  136. while (getline(&line, &n, from_fp) > 0)
  137. if (fputs(line, to_fp) == EOF)
  138. goto out_fclose_to;
  139. err = 0;
  140. out_fclose_to:
  141. fclose(to_fp);
  142. free(line);
  143. out_fclose_from:
  144. fclose(from_fp);
  145. out:
  146. return err;
  147. }
  148. static int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
  149. {
  150. void *ptr;
  151. loff_t pgoff;
  152. pgoff = off_in & ~(page_size - 1);
  153. off_in -= pgoff;
  154. ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
  155. if (ptr == MAP_FAILED)
  156. return -1;
  157. while (size) {
  158. ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
  159. if (ret < 0 && errno == EINTR)
  160. continue;
  161. if (ret <= 0)
  162. break;
  163. size -= ret;
  164. off_in += ret;
  165. off_out -= ret;
  166. }
  167. munmap(ptr, off_in + size);
  168. return size ? -1 : 0;
  169. }
  170. static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
  171. struct nsinfo *nsi)
  172. {
  173. int fromfd, tofd;
  174. struct stat st;
  175. int err;
  176. char *tmp = NULL, *ptr = NULL;
  177. struct nscookie nsc;
  178. nsinfo__mountns_enter(nsi, &nsc);
  179. err = stat(from, &st);
  180. nsinfo__mountns_exit(&nsc);
  181. if (err)
  182. goto out;
  183. err = -1;
  184. /* extra 'x' at the end is to reserve space for '.' */
  185. if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
  186. tmp = NULL;
  187. goto out;
  188. }
  189. ptr = strrchr(tmp, '/');
  190. if (!ptr)
  191. goto out;
  192. ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
  193. *ptr = '.';
  194. tofd = mkstemp(tmp);
  195. if (tofd < 0)
  196. goto out;
  197. if (fchmod(tofd, mode))
  198. goto out_close_to;
  199. if (st.st_size == 0) { /* /proc? do it slowly... */
  200. err = slow_copyfile(from, tmp, nsi);
  201. goto out_close_to;
  202. }
  203. nsinfo__mountns_enter(nsi, &nsc);
  204. fromfd = open(from, O_RDONLY);
  205. nsinfo__mountns_exit(&nsc);
  206. if (fromfd < 0)
  207. goto out_close_to;
  208. err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
  209. close(fromfd);
  210. out_close_to:
  211. close(tofd);
  212. if (!err)
  213. err = link(tmp, to);
  214. unlink(tmp);
  215. out:
  216. free(tmp);
  217. return err;
  218. }
  219. int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
  220. {
  221. return copyfile_mode_ns(from, to, 0755, nsi);
  222. }
  223. int copyfile_mode(const char *from, const char *to, mode_t mode)
  224. {
  225. return copyfile_mode_ns(from, to, mode, NULL);
  226. }
  227. int copyfile(const char *from, const char *to)
  228. {
  229. return copyfile_mode(from, to, 0755);
  230. }
  231. static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
  232. {
  233. void *buf_start = buf;
  234. size_t left = n;
  235. while (left) {
  236. /* buf must be treated as const if !is_read. */
  237. ssize_t ret = is_read ? read(fd, buf, left) :
  238. write(fd, buf, left);
  239. if (ret < 0 && errno == EINTR)
  240. continue;
  241. if (ret <= 0)
  242. return ret;
  243. left -= ret;
  244. buf += ret;
  245. }
  246. BUG_ON((size_t)(buf - buf_start) != n);
  247. return n;
  248. }
  249. /*
  250. * Read exactly 'n' bytes or return an error.
  251. */
  252. ssize_t readn(int fd, void *buf, size_t n)
  253. {
  254. return ion(true, fd, buf, n);
  255. }
  256. /*
  257. * Write exactly 'n' bytes or return an error.
  258. */
  259. ssize_t writen(int fd, const void *buf, size_t n)
  260. {
  261. /* ion does not modify buf. */
  262. return ion(false, fd, (void *)buf, n);
  263. }
  264. size_t hex_width(u64 v)
  265. {
  266. size_t n = 1;
  267. while ((v >>= 4))
  268. ++n;
  269. return n;
  270. }
  271. static int hex(char ch)
  272. {
  273. if ((ch >= '0') && (ch <= '9'))
  274. return ch - '0';
  275. if ((ch >= 'a') && (ch <= 'f'))
  276. return ch - 'a' + 10;
  277. if ((ch >= 'A') && (ch <= 'F'))
  278. return ch - 'A' + 10;
  279. return -1;
  280. }
  281. /*
  282. * While we find nice hex chars, build a long_val.
  283. * Return number of chars processed.
  284. */
  285. int hex2u64(const char *ptr, u64 *long_val)
  286. {
  287. const char *p = ptr;
  288. *long_val = 0;
  289. while (*p) {
  290. const int hex_val = hex(*p);
  291. if (hex_val < 0)
  292. break;
  293. *long_val = (*long_val << 4) | hex_val;
  294. p++;
  295. }
  296. return p - ptr;
  297. }
  298. int perf_event_paranoid(void)
  299. {
  300. int value;
  301. if (sysctl__read_int("kernel/perf_event_paranoid", &value))
  302. return INT_MAX;
  303. return value;
  304. }
  305. static int
  306. fetch_ubuntu_kernel_version(unsigned int *puint)
  307. {
  308. ssize_t len;
  309. size_t line_len = 0;
  310. char *ptr, *line = NULL;
  311. int version, patchlevel, sublevel, err;
  312. FILE *vsig;
  313. if (!puint)
  314. return 0;
  315. vsig = fopen("/proc/version_signature", "r");
  316. if (!vsig) {
  317. pr_debug("Open /proc/version_signature failed: %s\n",
  318. strerror(errno));
  319. return -1;
  320. }
  321. len = getline(&line, &line_len, vsig);
  322. fclose(vsig);
  323. err = -1;
  324. if (len <= 0) {
  325. pr_debug("Reading from /proc/version_signature failed: %s\n",
  326. strerror(errno));
  327. goto errout;
  328. }
  329. ptr = strrchr(line, ' ');
  330. if (!ptr) {
  331. pr_debug("Parsing /proc/version_signature failed: %s\n", line);
  332. goto errout;
  333. }
  334. err = sscanf(ptr + 1, "%d.%d.%d",
  335. &version, &patchlevel, &sublevel);
  336. if (err != 3) {
  337. pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
  338. line);
  339. goto errout;
  340. }
  341. *puint = (version << 16) + (patchlevel << 8) + sublevel;
  342. err = 0;
  343. errout:
  344. free(line);
  345. return err;
  346. }
  347. int
  348. fetch_kernel_version(unsigned int *puint, char *str,
  349. size_t str_size)
  350. {
  351. struct utsname utsname;
  352. int version, patchlevel, sublevel, err;
  353. bool int_ver_ready = false;
  354. if (access("/proc/version_signature", R_OK) == 0)
  355. if (!fetch_ubuntu_kernel_version(puint))
  356. int_ver_ready = true;
  357. if (uname(&utsname))
  358. return -1;
  359. if (str && str_size) {
  360. strncpy(str, utsname.release, str_size);
  361. str[str_size - 1] = '\0';
  362. }
  363. if (!puint || int_ver_ready)
  364. return 0;
  365. err = sscanf(utsname.release, "%d.%d.%d",
  366. &version, &patchlevel, &sublevel);
  367. if (err != 3) {
  368. pr_debug("Unable to get kernel version from uname '%s'\n",
  369. utsname.release);
  370. return -1;
  371. }
  372. *puint = (version << 16) + (patchlevel << 8) + sublevel;
  373. return 0;
  374. }
  375. const char *perf_tip(const char *dirpath)
  376. {
  377. struct strlist *tips;
  378. struct str_node *node;
  379. char *tip = NULL;
  380. struct strlist_config conf = {
  381. .dirname = dirpath,
  382. .file_only = true,
  383. };
  384. tips = strlist__new("tips.txt", &conf);
  385. if (tips == NULL)
  386. return errno == ENOENT ? NULL :
  387. "Tip: check path of tips.txt or get more memory! ;-p";
  388. if (strlist__nr_entries(tips) == 0)
  389. goto out;
  390. node = strlist__entry(tips, random() % strlist__nr_entries(tips));
  391. if (asprintf(&tip, "Tip: %s", node->s) < 0)
  392. tip = (char *)"Tip: get more memory! ;-)";
  393. out:
  394. strlist__delete(tips);
  395. return tip;
  396. }