drm/ast: Only warn about unsupported TX chips on Gen4 and later
[drm/drm-misc.git] / tools / perf / util / data.c
blob98661ede2a736320c690b3160cc350beefde368a
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <linux/err.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <asm/bug.h>
14 #include <dirent.h>
16 #include "data.h"
17 #include "util.h" // rm_rf_perf_data()
18 #include "debug.h"
19 #include "header.h"
20 #include "rlimit.h"
21 #include <internal/lib.h>
23 static void close_dir(struct perf_data_file *files, int nr)
25 while (--nr >= 0) {
26 close(files[nr].fd);
27 zfree(&files[nr].path);
29 free(files);
32 void perf_data__close_dir(struct perf_data *data)
34 close_dir(data->dir.files, data->dir.nr);
37 int perf_data__create_dir(struct perf_data *data, int nr)
39 enum rlimit_action set_rlimit = NO_CHANGE;
40 struct perf_data_file *files = NULL;
41 int i, ret;
43 if (WARN_ON(!data->is_dir))
44 return -EINVAL;
46 files = zalloc(nr * sizeof(*files));
47 if (!files)
48 return -ENOMEM;
50 for (i = 0; i < nr; i++) {
51 struct perf_data_file *file = &files[i];
53 ret = asprintf(&file->path, "%s/data.%d", data->path, i);
54 if (ret < 0) {
55 ret = -ENOMEM;
56 goto out_err;
59 retry_open:
60 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
61 if (ret < 0) {
63 * If using parallel threads to collect data,
64 * perf record needs at least 6 fds per CPU.
65 * When we run out of them try to increase the limits.
67 if (errno == EMFILE && rlimit__increase_nofile(&set_rlimit))
68 goto retry_open;
70 ret = -errno;
71 goto out_err;
73 set_rlimit = NO_CHANGE;
75 file->fd = ret;
78 data->dir.version = PERF_DIR_VERSION;
79 data->dir.files = files;
80 data->dir.nr = nr;
81 return 0;
83 out_err:
84 close_dir(files, i);
85 return ret;
88 int perf_data__open_dir(struct perf_data *data)
90 struct perf_data_file *files = NULL;
91 struct dirent *dent;
92 int ret = -1;
93 DIR *dir;
94 int nr = 0;
97 * Directory containing a single regular perf data file which is already
98 * open, means there is nothing more to do here.
100 if (perf_data__is_single_file(data))
101 return 0;
103 if (WARN_ON(!data->is_dir))
104 return -EINVAL;
106 /* The version is provided by DIR_FORMAT feature. */
107 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
108 return -1;
110 dir = opendir(data->path);
111 if (!dir)
112 return -EINVAL;
114 while ((dent = readdir(dir)) != NULL) {
115 struct perf_data_file *file;
116 char path[PATH_MAX];
117 struct stat st;
119 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
120 if (stat(path, &st))
121 continue;
123 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
124 continue;
126 ret = -ENOMEM;
128 file = realloc(files, (nr + 1) * sizeof(*files));
129 if (!file)
130 goto out_err;
132 files = file;
133 file = &files[nr++];
135 file->path = strdup(path);
136 if (!file->path)
137 goto out_err;
139 ret = open(file->path, O_RDONLY);
140 if (ret < 0)
141 goto out_err;
143 file->fd = ret;
144 file->size = st.st_size;
147 closedir(dir);
148 if (!files)
149 return -EINVAL;
151 data->dir.files = files;
152 data->dir.nr = nr;
153 return 0;
155 out_err:
156 closedir(dir);
157 close_dir(files, nr);
158 return ret;
161 int perf_data__update_dir(struct perf_data *data)
163 int i;
165 if (WARN_ON(!data->is_dir))
166 return -EINVAL;
168 for (i = 0; i < data->dir.nr; i++) {
169 struct perf_data_file *file = &data->dir.files[i];
170 struct stat st;
172 if (fstat(file->fd, &st))
173 return -1;
175 file->size = st.st_size;
178 return 0;
181 static bool check_pipe(struct perf_data *data)
183 struct stat st;
184 bool is_pipe = false;
185 int fd = perf_data__is_read(data) ?
186 STDIN_FILENO : STDOUT_FILENO;
188 if (!data->path) {
189 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
190 is_pipe = true;
191 } else {
192 if (!strcmp(data->path, "-"))
193 is_pipe = true;
196 if (is_pipe) {
197 if (data->use_stdio) {
198 const char *mode;
200 mode = perf_data__is_read(data) ? "r" : "w";
201 data->file.fptr = fdopen(fd, mode);
203 if (data->file.fptr == NULL) {
204 data->file.fd = fd;
205 data->use_stdio = false;
209 * When is_pipe and data->file.fd is given, use given fd
210 * instead of STDIN_FILENO or STDOUT_FILENO
212 } else if (data->file.fd <= 0) {
213 data->file.fd = fd;
217 return data->is_pipe = is_pipe;
220 static int check_backup(struct perf_data *data)
222 struct stat st;
224 if (perf_data__is_read(data))
225 return 0;
227 if (!stat(data->path, &st) && st.st_size) {
228 char oldname[PATH_MAX];
229 int ret;
231 snprintf(oldname, sizeof(oldname), "%s.old",
232 data->path);
234 ret = rm_rf_perf_data(oldname);
235 if (ret) {
236 pr_err("Can't remove old data: %s (%s)\n",
237 ret == -2 ?
238 "Unknown file found" : strerror(errno),
239 oldname);
240 return -1;
243 if (rename(data->path, oldname)) {
244 pr_err("Can't move data: %s (%s to %s)\n",
245 strerror(errno),
246 data->path, oldname);
247 return -1;
251 return 0;
254 static bool is_dir(struct perf_data *data)
256 struct stat st;
258 if (stat(data->path, &st))
259 return false;
261 return (st.st_mode & S_IFMT) == S_IFDIR;
264 static int open_file_read(struct perf_data *data)
266 int flags = data->in_place_update ? O_RDWR : O_RDONLY;
267 struct stat st;
268 int fd;
269 char sbuf[STRERR_BUFSIZE];
271 fd = open(data->file.path, flags);
272 if (fd < 0) {
273 int err = errno;
275 pr_err("failed to open %s: %s", data->file.path,
276 str_error_r(err, sbuf, sizeof(sbuf)));
277 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
278 pr_err(" (try 'perf record' first)");
279 pr_err("\n");
280 return -err;
283 if (fstat(fd, &st) < 0)
284 goto out_close;
286 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
287 pr_err("File %s not owned by current user or root (use -f to override)\n",
288 data->file.path);
289 goto out_close;
292 if (!st.st_size) {
293 pr_info("zero-sized data (%s), nothing to do!\n",
294 data->file.path);
295 goto out_close;
298 data->file.size = st.st_size;
299 return fd;
301 out_close:
302 close(fd);
303 return -1;
306 static int open_file_write(struct perf_data *data)
308 int fd;
309 char sbuf[STRERR_BUFSIZE];
311 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
312 S_IRUSR|S_IWUSR);
314 if (fd < 0)
315 pr_err("failed to open %s : %s\n", data->file.path,
316 str_error_r(errno, sbuf, sizeof(sbuf)));
318 return fd;
321 static int open_file(struct perf_data *data)
323 int fd;
325 fd = perf_data__is_read(data) ?
326 open_file_read(data) : open_file_write(data);
328 if (fd < 0) {
329 zfree(&data->file.path);
330 return -1;
333 data->file.fd = fd;
334 return 0;
337 static int open_file_dup(struct perf_data *data)
339 data->file.path = strdup(data->path);
340 if (!data->file.path)
341 return -ENOMEM;
343 return open_file(data);
346 static int open_dir(struct perf_data *data)
348 int ret;
351 * So far we open only the header, so we can read the data version and
352 * layout.
354 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
355 return -1;
357 if (perf_data__is_write(data) &&
358 mkdir(data->path, S_IRWXU) < 0)
359 return -1;
361 ret = open_file(data);
363 /* Cleanup whatever we managed to create so far. */
364 if (ret && perf_data__is_write(data))
365 rm_rf_perf_data(data->path);
367 return ret;
370 int perf_data__open(struct perf_data *data)
372 if (check_pipe(data))
373 return 0;
375 /* currently it allows stdio for pipe only */
376 data->use_stdio = false;
378 if (!data->path)
379 data->path = "perf.data";
381 if (check_backup(data))
382 return -1;
384 if (perf_data__is_read(data))
385 data->is_dir = is_dir(data);
387 return perf_data__is_dir(data) ?
388 open_dir(data) : open_file_dup(data);
391 void perf_data__close(struct perf_data *data)
393 if (perf_data__is_dir(data))
394 perf_data__close_dir(data);
396 zfree(&data->file.path);
398 if (data->use_stdio)
399 fclose(data->file.fptr);
400 else
401 close(data->file.fd);
404 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
406 if (data->use_stdio) {
407 if (fread(buf, size, 1, data->file.fptr) == 1)
408 return size;
409 return feof(data->file.fptr) ? 0 : -1;
411 return readn(data->file.fd, buf, size);
414 ssize_t perf_data_file__write(struct perf_data_file *file,
415 void *buf, size_t size)
417 return writen(file->fd, buf, size);
420 ssize_t perf_data__write(struct perf_data *data,
421 void *buf, size_t size)
423 if (data->use_stdio) {
424 if (fwrite(buf, size, 1, data->file.fptr) == 1)
425 return size;
426 return -1;
428 return perf_data_file__write(&data->file, buf, size);
431 int perf_data__switch(struct perf_data *data,
432 const char *postfix,
433 size_t pos, bool at_exit,
434 char **new_filepath)
436 int ret;
438 if (perf_data__is_read(data))
439 return -EINVAL;
441 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
442 return -ENOMEM;
445 * Only fire a warning, don't return error, continue fill
446 * original file.
448 if (rename(data->path, *new_filepath))
449 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
451 if (!at_exit) {
452 close(data->file.fd);
453 ret = perf_data__open(data);
454 if (ret < 0)
455 goto out;
457 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
458 ret = -errno;
459 pr_debug("Failed to lseek to %zu: %s",
460 pos, strerror(errno));
461 goto out;
464 ret = data->file.fd;
465 out:
466 return ret;
469 unsigned long perf_data__size(struct perf_data *data)
471 u64 size = data->file.size;
472 int i;
474 if (perf_data__is_single_file(data))
475 return size;
477 for (i = 0; i < data->dir.nr; i++) {
478 struct perf_data_file *file = &data->dir.files[i];
480 size += file->size;
483 return size;
486 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
488 int ret;
490 if (!data->is_dir)
491 return -1;
493 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
494 if (ret < 0 || (size_t)ret >= buf_sz)
495 return -1;
497 return mkdir(buf, S_IRWXU);
500 bool has_kcore_dir(const char *path)
502 struct dirent *d = ERR_PTR(-EINVAL);
503 const char *name = "kcore_dir";
504 DIR *dir = opendir(path);
505 size_t n = strlen(name);
506 bool result = false;
508 if (dir) {
509 while (d && !result) {
510 d = readdir(dir);
511 result = d ? strncmp(d->d_name, name, n) : false;
513 closedir(dir);
516 return result;
519 char *perf_data__kallsyms_name(struct perf_data *data)
521 char *kallsyms_name;
522 struct stat st;
524 if (!data->is_dir)
525 return NULL;
527 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
528 return NULL;
530 if (stat(kallsyms_name, &st)) {
531 free(kallsyms_name);
532 return NULL;
535 return kallsyms_name;
538 char *perf_data__guest_kallsyms_name(struct perf_data *data, pid_t machine_pid)
540 char *kallsyms_name;
541 struct stat st;
543 if (!data->is_dir)
544 return NULL;
546 if (asprintf(&kallsyms_name, "%s/kcore_dir__%d/kallsyms", data->path, machine_pid) < 0)
547 return NULL;
549 if (stat(kallsyms_name, &st)) {
550 free(kallsyms_name);
551 return NULL;
554 return kallsyms_name;
557 bool is_perf_data(const char *path)
559 bool ret = false;
560 FILE *file;
561 u64 magic;
563 file = fopen(path, "r");
564 if (!file)
565 return false;
567 if (fread(&magic, 1, 8, file) < 8)
568 goto out;
570 ret = is_perf_magic(magic);
571 out:
572 fclose(file);
573 return ret;