~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/util.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include "../perf.h"
  3 #include "util.h"
  4 #include "debug.h"
  5 #include "namespaces.h"
  6 #include <api/fs/fs.h>
  7 #include <sys/mman.h>
  8 #include <sys/stat.h>
  9 #include <sys/utsname.h>
 10 #include <dirent.h>
 11 #include <fcntl.h>
 12 #include <inttypes.h>
 13 #include <signal.h>
 14 #include <stdio.h>
 15 #include <stdlib.h>
 16 #include <string.h>
 17 #include <errno.h>
 18 #include <limits.h>
 19 #include <linux/kernel.h>
 20 #include <linux/log2.h>
 21 #include <linux/time64.h>
 22 #include <unistd.h>
 23 #include "strlist.h"
 24 #include "string2.h"
 25 
 26 /*
 27  * XXX We need to find a better place for these things...
 28  */
 29 
 30 bool perf_singlethreaded = true;
 31 
 32 void perf_set_singlethreaded(void)
 33 {
 34         perf_singlethreaded = true;
 35 }
 36 
 37 void perf_set_multithreaded(void)
 38 {
 39         perf_singlethreaded = false;
 40 }
 41 
 42 unsigned int page_size;
 43 
 44 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
 45 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
 46 #else
 47 static void cache_line_size(int *cacheline_sizep)
 48 {
 49         if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
 50                 pr_debug("cannot determine cache line size");
 51 }
 52 #endif
 53 
 54 int cacheline_size(void)
 55 {
 56         static int size;
 57 
 58         if (!size)
 59                 cache_line_size(&size);
 60 
 61         return size;
 62 }
 63 
 64 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
 65 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
 66 
 67 int sysctl__max_stack(void)
 68 {
 69         int value;
 70 
 71         if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
 72                 sysctl_perf_event_max_stack = value;
 73 
 74         if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
 75                 sysctl_perf_event_max_contexts_per_stack = value;
 76 
 77         return sysctl_perf_event_max_stack;
 78 }
 79 
 80 bool test_attr__enabled;
 81 
 82 bool perf_host  = true;
 83 bool perf_guest = false;
 84 
 85 void event_attr_init(struct perf_event_attr *attr)
 86 {
 87         if (!perf_host)
 88                 attr->exclude_host  = 1;
 89         if (!perf_guest)
 90                 attr->exclude_guest = 1;
 91         /* to capture ABI version */
 92         attr->size = sizeof(*attr);
 93 }
 94 
 95 int mkdir_p(char *path, mode_t mode)
 96 {
 97         struct stat st;
 98         int err;
 99         char *d = path;
100 
101         if (*d != '/')
102                 return -1;
103 
104         if (stat(path, &st) == 0)
105                 return 0;
106 
107         while (*++d == '/');
108 
109         while ((d = strchr(d, '/'))) {
110                 *d = '\0';
111                 err = stat(path, &st) && mkdir(path, mode);
112                 *d++ = '/';
113                 if (err)
114                         return -1;
115                 while (*d == '/')
116                         ++d;
117         }
118         return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
119 }
120 
121 static bool match_pat(char *file, const char **pat)
122 {
123         int i = 0;
124 
125         if (!pat)
126                 return true;
127 
128         while (pat[i]) {
129                 if (strglobmatch(file, pat[i]))
130                         return true;
131 
132                 i++;
133         }
134 
135         return false;
136 }
137 
138 /*
139  * The depth specify how deep the removal will go.
140  * 0       - will remove only files under the 'path' directory
141  * 1 .. x  - will dive in x-level deep under the 'path' directory
142  *
143  * If specified the pat is array of string patterns ended with NULL,
144  * which are checked upon every file/directory found. Only matching
145  * ones are removed.
146  *
147  * The function returns:
148  *    0 on success
149  *   -1 on removal failure with errno set
150  *   -2 on pattern failure
151  */
152 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
153 {
154         DIR *dir;
155         int ret;
156         struct dirent *d;
157         char namebuf[PATH_MAX];
158         struct stat statbuf;
159 
160         /* Do not fail if there's no file. */
161         ret = lstat(path, &statbuf);
162         if (ret)
163                 return 0;
164 
165         /* Try to remove any file we get. */
166         if (!(statbuf.st_mode & S_IFDIR))
167                 return unlink(path);
168 
169         /* We have directory in path. */
170         dir = opendir(path);
171         if (dir == NULL)
172                 return -1;
173 
174         while ((d = readdir(dir)) != NULL && !ret) {
175 
176                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
177                         continue;
178 
179                 if (!match_pat(d->d_name, pat))
180                         return -2;
181 
182                 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
183                           path, d->d_name);
184 
185                 /* We have to check symbolic link itself */
186                 ret = lstat(namebuf, &statbuf);
187                 if (ret < 0) {
188                         pr_debug("stat failed: %s\n", namebuf);
189                         break;
190                 }
191 
192                 if (S_ISDIR(statbuf.st_mode))
193                         ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
194                 else
195                         ret = unlink(namebuf);
196         }
197         closedir(dir);
198 
199         if (ret < 0)
200                 return ret;
201 
202         return rmdir(path);
203 }
204 
205 int rm_rf_perf_data(const char *path)
206 {
207         const char *pat[] = {
208                 "header",
209                 "data.*",
210                 NULL,
211         };
212 
213         return rm_rf_depth_pat(path, 0, pat);
214 }
215 
216 int rm_rf(const char *path)
217 {
218         return rm_rf_depth_pat(path, INT_MAX, NULL);
219 }
220