util.c 9.5 KB

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