xref: /linux/tools/perf/util/data.h (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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 <unistd.h>
8 #include <linux/types.h>
9 
10 enum perf_data_mode {
11 	PERF_DATA_MODE_WRITE,
12 	PERF_DATA_MODE_READ,
13 };
14 
15 enum perf_dir_version {
16 	PERF_DIR_SINGLE_FILE	= 0,
17 	PERF_DIR_VERSION	= 1,
18 };
19 
20 /**
21  * struct perf_data_file: A wrapper around a file used for perf.data reading or writing. Generally
22  * part of struct perf_data.
23  */
24 struct perf_data_file {
25 	/**
26 	 * @path: Path of file. Generally a copy of perf_data.path but for a
27 	 * directory it is the file within the directory.
28 	 */
29 	char		*path;
30 	union {
31 		/** @fd: File descriptor for read/writes. Valid if use_stdio is false. */
32 		int	 fd;
33 		/**
34 		 * @fptr: Stdio FILE. Valid if use_stdio is true, currently just
35 		 * pipes in perf inject.
36 		 */
37 		FILE	*fptr;
38 	};
39 	/** @size: Size of file when opened. */
40 	unsigned long	 size;
41 	/** @use_stdio: Use buffered stdio operations. */
42 	bool		 use_stdio;
43 };
44 
45 /**
46  * struct perf_data: A wrapper around a file used for perf.data reading or writing.
47  */
48 struct perf_data {
49 	/** @path: Path to open and of the file. NULL implies 'perf.data' will be used. */
50 	const char		*path;
51 	/** @file: Underlying file to be used. */
52 	struct perf_data_file	 file;
53 	/** @is_pipe: Underlying file is a pipe. */
54 	bool			 is_pipe;
55 	/** @is_dir: Underlying file is a directory. */
56 	bool			 is_dir;
57 	/** @force: Ignore opening a file creating created by a different user. */
58 	bool			 force;
59 	/** @in_place_update: A file opened for reading but will be written to. */
60 	bool			 in_place_update;
61 	/** @mode: Read or write mode. */
62 	enum perf_data_mode	 mode;
63 
64 	struct {
65 		/** @version: perf_dir_version. */
66 		u64			 version;
67 		/** @files: perf data files for the directory. */
68 		struct perf_data_file	*files;
69 		/** @nr: Number of perf data files for the directory. */
70 		int			 nr;
71 	} dir;
72 };
73 
74 static inline int perf_data_file__fd(struct perf_data_file *file)
75 {
76 	return file->use_stdio ? fileno(file->fptr) : file->fd;
77 }
78 
79 ssize_t perf_data_file__write(struct perf_data_file *file,
80 			      void *buf, size_t size);
81 off_t perf_data_file__seek(struct perf_data_file *file, off_t offset, int whence);
82 
83 
84 static inline bool perf_data__is_read(struct perf_data *data)
85 {
86 	return data->mode == PERF_DATA_MODE_READ;
87 }
88 
89 static inline bool perf_data__is_write(struct perf_data *data)
90 {
91 	return data->mode == PERF_DATA_MODE_WRITE;
92 }
93 
94 static inline int perf_data__is_pipe(struct perf_data *data)
95 {
96 	return data->is_pipe;
97 }
98 
99 static inline bool perf_data__is_dir(struct perf_data *data)
100 {
101 	return data->is_dir;
102 }
103 
104 static inline bool perf_data__is_single_file(struct perf_data *data)
105 {
106 	return data->dir.version == PERF_DIR_SINGLE_FILE;
107 }
108 
109 static inline int perf_data__fd(struct perf_data *data)
110 {
111 	return perf_data_file__fd(&data->file);
112 }
113 
114 int perf_data__open(struct perf_data *data);
115 void perf_data__close(struct perf_data *data);
116 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size);
117 ssize_t perf_data__write(struct perf_data *data,
118 			 void *buf, size_t size);
119 off_t perf_data__seek(struct perf_data *data, off_t offset, int whence);
120 /*
121  * If at_exit is set, only rename current perf.data to
122  * perf.data.<postfix>, continue write on original data.
123  * Set at_exit when flushing the last output.
124  *
125  * Return value is fd of new output.
126  */
127 int perf_data__switch(struct perf_data *data,
128 		      const char *postfix,
129 		      size_t pos, bool at_exit, char **new_filepath);
130 
131 int perf_data__create_dir(struct perf_data *data, int nr);
132 int perf_data__open_dir(struct perf_data *data);
133 void perf_data__close_dir(struct perf_data *data);
134 unsigned long perf_data__size(struct perf_data *data);
135 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz);
136 char *perf_data__kallsyms_name(struct perf_data *data);
137 char *perf_data__guest_kallsyms_name(struct perf_data *data, pid_t machine_pid);
138 
139 bool has_kcore_dir(const char *path);
140 bool is_perf_data(const char *path);
141 
142 #endif /* __PERF_DATA_H */
143