xref: /openbmc/linux/tools/perf/arch/s390/util/auxtrace.c (revision b08918fb)
1 #include <stdbool.h>
2 #include <linux/kernel.h>
3 #include <linux/types.h>
4 #include <linux/bitops.h>
5 #include <linux/log2.h>
6 #include <linux/zalloc.h>
7 
8 #include "../../util/evlist.h"
9 #include "../../util/auxtrace.h"
10 #include "../../util/evsel.h"
11 #include "../../util/record.h"
12 
13 #define PERF_EVENT_CPUM_SF		0xB0000 /* Event: Basic-sampling */
14 #define PERF_EVENT_CPUM_SF_DIAG		0xBD000 /* Event: Combined-sampling */
15 #define DEFAULT_AUX_PAGES		128
16 #define DEFAULT_FREQ			4000
17 
18 static void cpumsf_free(struct auxtrace_record *itr)
19 {
20 	free(itr);
21 }
22 
23 static size_t cpumsf_info_priv_size(struct auxtrace_record *itr __maybe_unused,
24 				    struct evlist *evlist __maybe_unused)
25 {
26 	return 0;
27 }
28 
29 static int
30 cpumsf_info_fill(struct auxtrace_record *itr __maybe_unused,
31 		 struct perf_session *session __maybe_unused,
32 		 struct perf_record_auxtrace_info *auxtrace_info __maybe_unused,
33 		 size_t priv_size __maybe_unused)
34 {
35 	auxtrace_info->type = PERF_AUXTRACE_S390_CPUMSF;
36 	return 0;
37 }
38 
39 static unsigned long
40 cpumsf_reference(struct auxtrace_record *itr __maybe_unused)
41 {
42 	return 0;
43 }
44 
45 static int
46 cpumsf_recording_options(struct auxtrace_record *ar __maybe_unused,
47 			 struct evlist *evlist __maybe_unused,
48 			 struct record_opts *opts)
49 {
50 	unsigned int factor = 1;
51 	unsigned int pages;
52 
53 	opts->full_auxtrace = true;
54 
55 	/*
56 	 * The AUX buffer size should be set properly to avoid
57 	 * overflow of samples if it is not set explicitly.
58 	 * DEFAULT_AUX_PAGES is an proper size when sampling frequency
59 	 * is DEFAULT_FREQ. It is expected to hold about 1/2 second
60 	 * of sampling data. The size used for AUX buffer will scale
61 	 * according to the specified frequency and DEFAULT_FREQ.
62 	 */
63 	if (!opts->auxtrace_mmap_pages) {
64 		if (opts->user_freq != UINT_MAX)
65 			factor = (opts->user_freq + DEFAULT_FREQ
66 				  - 1) / DEFAULT_FREQ;
67 		pages = DEFAULT_AUX_PAGES * factor;
68 		opts->auxtrace_mmap_pages = roundup_pow_of_two(pages);
69 	}
70 
71 	return 0;
72 }
73 
74 static int
75 cpumsf_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
76 			      struct record_opts *opts __maybe_unused,
77 			      const char *str __maybe_unused)
78 {
79 	return 0;
80 }
81 
82 /*
83  * auxtrace_record__init is called when perf record
84  * check if the event really need auxtrace
85  */
86 struct auxtrace_record *auxtrace_record__init(struct evlist *evlist,
87 					      int *err)
88 {
89 	struct auxtrace_record *aux;
90 	struct evsel *pos;
91 	int diagnose = 0;
92 
93 	*err = 0;
94 	if (evlist->core.nr_entries == 0)
95 		return NULL;
96 
97 	evlist__for_each_entry(evlist, pos) {
98 		if (pos->core.attr.config == PERF_EVENT_CPUM_SF_DIAG) {
99 			diagnose = 1;
100 			break;
101 		}
102 	}
103 
104 	if (!diagnose)
105 		return NULL;
106 
107 	/* sampling in diagnose mode. alloc aux buffer */
108 	aux = zalloc(sizeof(*aux));
109 	if (aux == NULL) {
110 		*err = -ENOMEM;
111 		return NULL;
112 	}
113 
114 	aux->parse_snapshot_options = cpumsf_parse_snapshot_options;
115 	aux->recording_options = cpumsf_recording_options;
116 	aux->info_priv_size = cpumsf_info_priv_size;
117 	aux->info_fill = cpumsf_info_fill;
118 	aux->free = cpumsf_free;
119 	aux->reference = cpumsf_reference;
120 
121 	return aux;
122 }
123