util.c 8.7 KB

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