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