18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __PERF_HEADER_H
38c2ecf20Sopenharmony_ci#define __PERF_HEADER_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/stddef.h>
68c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
78c2ecf20Sopenharmony_ci#include <sys/types.h>
88c2ecf20Sopenharmony_ci#include <stdio.h> // FILE
98c2ecf20Sopenharmony_ci#include <stdbool.h>
108c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include "env.h"
138c2ecf20Sopenharmony_ci#include "pmu.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cienum {
168c2ecf20Sopenharmony_ci	HEADER_RESERVED		= 0,	/* always cleared */
178c2ecf20Sopenharmony_ci	HEADER_FIRST_FEATURE	= 1,
188c2ecf20Sopenharmony_ci	HEADER_TRACING_DATA	= 1,
198c2ecf20Sopenharmony_ci	HEADER_BUILD_ID,
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	HEADER_HOSTNAME,
228c2ecf20Sopenharmony_ci	HEADER_OSRELEASE,
238c2ecf20Sopenharmony_ci	HEADER_VERSION,
248c2ecf20Sopenharmony_ci	HEADER_ARCH,
258c2ecf20Sopenharmony_ci	HEADER_NRCPUS,
268c2ecf20Sopenharmony_ci	HEADER_CPUDESC,
278c2ecf20Sopenharmony_ci	HEADER_CPUID,
288c2ecf20Sopenharmony_ci	HEADER_TOTAL_MEM,
298c2ecf20Sopenharmony_ci	HEADER_CMDLINE,
308c2ecf20Sopenharmony_ci	HEADER_EVENT_DESC,
318c2ecf20Sopenharmony_ci	HEADER_CPU_TOPOLOGY,
328c2ecf20Sopenharmony_ci	HEADER_NUMA_TOPOLOGY,
338c2ecf20Sopenharmony_ci	HEADER_BRANCH_STACK,
348c2ecf20Sopenharmony_ci	HEADER_PMU_MAPPINGS,
358c2ecf20Sopenharmony_ci	HEADER_GROUP_DESC,
368c2ecf20Sopenharmony_ci	HEADER_AUXTRACE,
378c2ecf20Sopenharmony_ci	HEADER_STAT,
388c2ecf20Sopenharmony_ci	HEADER_CACHE,
398c2ecf20Sopenharmony_ci	HEADER_SAMPLE_TIME,
408c2ecf20Sopenharmony_ci	HEADER_MEM_TOPOLOGY,
418c2ecf20Sopenharmony_ci	HEADER_CLOCKID,
428c2ecf20Sopenharmony_ci	HEADER_DIR_FORMAT,
438c2ecf20Sopenharmony_ci	HEADER_BPF_PROG_INFO,
448c2ecf20Sopenharmony_ci	HEADER_BPF_BTF,
458c2ecf20Sopenharmony_ci	HEADER_COMPRESSED,
468c2ecf20Sopenharmony_ci	HEADER_CPU_PMU_CAPS,
478c2ecf20Sopenharmony_ci	HEADER_CLOCK_DATA,
488c2ecf20Sopenharmony_ci	HEADER_LAST_FEATURE,
498c2ecf20Sopenharmony_ci	HEADER_FEAT_BITS	= 256,
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cienum perf_header_version {
538c2ecf20Sopenharmony_ci	PERF_HEADER_VERSION_1,
548c2ecf20Sopenharmony_ci	PERF_HEADER_VERSION_2,
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct perf_file_section {
588c2ecf20Sopenharmony_ci	u64 offset;
598c2ecf20Sopenharmony_ci	u64 size;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct perf_file_header {
638c2ecf20Sopenharmony_ci	u64				magic;
648c2ecf20Sopenharmony_ci	u64				size;
658c2ecf20Sopenharmony_ci	u64				attr_size;
668c2ecf20Sopenharmony_ci	struct perf_file_section	attrs;
678c2ecf20Sopenharmony_ci	struct perf_file_section	data;
688c2ecf20Sopenharmony_ci	/* event_types is ignored */
698c2ecf20Sopenharmony_ci	struct perf_file_section	event_types;
708c2ecf20Sopenharmony_ci	DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct perf_pipe_file_header {
748c2ecf20Sopenharmony_ci	u64				magic;
758c2ecf20Sopenharmony_ci	u64				size;
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct perf_header;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciint perf_file_header__read(struct perf_file_header *header,
818c2ecf20Sopenharmony_ci			   struct perf_header *ph, int fd);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistruct perf_header {
848c2ecf20Sopenharmony_ci	enum perf_header_version	version;
858c2ecf20Sopenharmony_ci	bool				needs_swap;
868c2ecf20Sopenharmony_ci	u64				data_offset;
878c2ecf20Sopenharmony_ci	u64				data_size;
888c2ecf20Sopenharmony_ci	u64				feat_offset;
898c2ecf20Sopenharmony_ci	DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
908c2ecf20Sopenharmony_ci	struct perf_env 	env;
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct feat_fd {
948c2ecf20Sopenharmony_ci	struct perf_header *ph;
958c2ecf20Sopenharmony_ci	int		   fd;
968c2ecf20Sopenharmony_ci	void		   *buf;	/* Either buf != NULL or fd >= 0 */
978c2ecf20Sopenharmony_ci	ssize_t		   offset;
988c2ecf20Sopenharmony_ci	size_t		   size;
998c2ecf20Sopenharmony_ci	struct evsel	   *events;
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct perf_header_feature_ops {
1038c2ecf20Sopenharmony_ci	int	   (*write)(struct feat_fd *ff, struct evlist *evlist);
1048c2ecf20Sopenharmony_ci	void	   (*print)(struct feat_fd *ff, FILE *fp);
1058c2ecf20Sopenharmony_ci	int	   (*process)(struct feat_fd *ff, void *data);
1068c2ecf20Sopenharmony_ci	const char *name;
1078c2ecf20Sopenharmony_ci	bool	   full_only;
1088c2ecf20Sopenharmony_ci	bool	   synthesize;
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistruct evlist;
1128c2ecf20Sopenharmony_cistruct perf_session;
1138c2ecf20Sopenharmony_cistruct perf_tool;
1148c2ecf20Sopenharmony_ciunion perf_event;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciint perf_session__read_header(struct perf_session *session);
1178c2ecf20Sopenharmony_ciint perf_session__write_header(struct perf_session *session,
1188c2ecf20Sopenharmony_ci			       struct evlist *evlist,
1198c2ecf20Sopenharmony_ci			       int fd, bool at_exit);
1208c2ecf20Sopenharmony_ciint perf_header__write_pipe(int fd);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_civoid perf_header__set_feat(struct perf_header *header, int feat);
1238c2ecf20Sopenharmony_civoid perf_header__clear_feat(struct perf_header *header, int feat);
1248c2ecf20Sopenharmony_cibool perf_header__has_feat(const struct perf_header *header, int feat);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciint perf_header__set_cmdline(int argc, const char **argv);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciint perf_header__process_sections(struct perf_header *header, int fd,
1298c2ecf20Sopenharmony_ci				  void *data,
1308c2ecf20Sopenharmony_ci				  int (*process)(struct perf_file_section *section,
1318c2ecf20Sopenharmony_ci				  struct perf_header *ph,
1328c2ecf20Sopenharmony_ci				  int feat, int fd, void *data));
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciint perf_header__fprintf_info(struct perf_session *s, FILE *fp, bool full);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciint perf_event__process_feature(struct perf_session *session,
1378c2ecf20Sopenharmony_ci				union perf_event *event);
1388c2ecf20Sopenharmony_ciint perf_event__process_attr(struct perf_tool *tool, union perf_event *event,
1398c2ecf20Sopenharmony_ci			     struct evlist **pevlist);
1408c2ecf20Sopenharmony_ciint perf_event__process_event_update(struct perf_tool *tool,
1418c2ecf20Sopenharmony_ci				     union perf_event *event,
1428c2ecf20Sopenharmony_ci				     struct evlist **pevlist);
1438c2ecf20Sopenharmony_cisize_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp);
1448c2ecf20Sopenharmony_ciint perf_event__process_tracing_data(struct perf_session *session,
1458c2ecf20Sopenharmony_ci				     union perf_event *event);
1468c2ecf20Sopenharmony_ciint perf_event__process_build_id(struct perf_session *session,
1478c2ecf20Sopenharmony_ci				 union perf_event *event);
1488c2ecf20Sopenharmony_cibool is_perf_magic(u64 magic);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#define NAME_ALIGN 64
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistruct feat_fd;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciint do_write(struct feat_fd *fd, const void *buf, size_t size);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciint write_padded(struct feat_fd *fd, const void *bf,
1578c2ecf20Sopenharmony_ci		 size_t count, size_t count_aligned);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * arch specific callback
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ciint get_cpuid(char *buffer, size_t sz);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cichar *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
1658c2ecf20Sopenharmony_ciint strcmp_cpuid_str(const char *s1, const char *s2);
1668c2ecf20Sopenharmony_ci#endif /* __PERF_HEADER_H */
167