GNU Linux-libre 5.19-rc6-gnu
[releases.git] / tools / perf / util / data.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_DATA_H
3 #define __PERF_DATA_H
4
5 #include <stdio.h>
6 #include <stdbool.h>
7 #include <linux/types.h>
8
9 enum perf_data_mode {
10         PERF_DATA_MODE_WRITE,
11         PERF_DATA_MODE_READ,
12 };
13
14 enum perf_dir_version {
15         PERF_DIR_SINGLE_FILE    = 0,
16         PERF_DIR_VERSION        = 1,
17 };
18
19 struct perf_data_file {
20         char            *path;
21         union {
22                 int      fd;
23                 FILE    *fptr;
24         };
25         unsigned long    size;
26 };
27
28 struct perf_data {
29         const char              *path;
30         struct perf_data_file    file;
31         bool                     is_pipe;
32         bool                     is_dir;
33         bool                     force;
34         bool                     use_stdio;
35         bool                     in_place_update;
36         enum perf_data_mode      mode;
37
38         struct {
39                 u64                      version;
40                 struct perf_data_file   *files;
41                 int                      nr;
42         } dir;
43 };
44
45 static inline bool perf_data__is_read(struct perf_data *data)
46 {
47         return data->mode == PERF_DATA_MODE_READ;
48 }
49
50 static inline bool perf_data__is_write(struct perf_data *data)
51 {
52         return data->mode == PERF_DATA_MODE_WRITE;
53 }
54
55 static inline int perf_data__is_pipe(struct perf_data *data)
56 {
57         return data->is_pipe;
58 }
59
60 static inline bool perf_data__is_dir(struct perf_data *data)
61 {
62         return data->is_dir;
63 }
64
65 static inline bool perf_data__is_single_file(struct perf_data *data)
66 {
67         return data->dir.version == PERF_DIR_SINGLE_FILE;
68 }
69
70 static inline int perf_data__fd(struct perf_data *data)
71 {
72         if (data->use_stdio)
73                 return fileno(data->file.fptr);
74
75         return data->file.fd;
76 }
77
78 int perf_data__open(struct perf_data *data);
79 void perf_data__close(struct perf_data *data);
80 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size);
81 ssize_t perf_data__write(struct perf_data *data,
82                               void *buf, size_t size);
83 ssize_t perf_data_file__write(struct perf_data_file *file,
84                               void *buf, size_t size);
85 /*
86  * If at_exit is set, only rename current perf.data to
87  * perf.data.<postfix>, continue write on original data.
88  * Set at_exit when flushing the last output.
89  *
90  * Return value is fd of new output.
91  */
92 int perf_data__switch(struct perf_data *data,
93                            const char *postfix,
94                            size_t pos, bool at_exit, char **new_filepath);
95
96 int perf_data__create_dir(struct perf_data *data, int nr);
97 int perf_data__open_dir(struct perf_data *data);
98 void perf_data__close_dir(struct perf_data *data);
99 int perf_data__update_dir(struct perf_data *data);
100 unsigned long perf_data__size(struct perf_data *data);
101 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz);
102 bool has_kcore_dir(const char *path);
103 char *perf_data__kallsyms_name(struct perf_data *data);
104 bool is_perf_data(const char *path);
105 #endif /* __PERF_DATA_H */