1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2019
4 * Author(s): Thomas Richter <tmricht@linux.ibm.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Architecture specific trace_event function. Save event's bc000 raw data
11 * to file. File name is aux.ctr.## where ## stands for the CPU number the
12 * sample was taken from.
13 */
14
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <inttypes.h>
19
20 #include <sys/stat.h>
21 #include <linux/compiler.h>
22 #include <asm/byteorder.h>
23
24 #include "debug.h"
25 #include "session.h"
26 #include "evlist.h"
27 #include "color.h"
28 #include "sample-raw.h"
29 #include "s390-cpumcf-kernel.h"
30 #include "util/pmu.h"
31 #include "util/sample.h"
32
ctrset_size(struct cf_ctrset_entry * set)33 static size_t ctrset_size(struct cf_ctrset_entry *set)
34 {
35 return sizeof(*set) + set->ctr * sizeof(u64);
36 }
37
ctrset_valid(struct cf_ctrset_entry * set)38 static bool ctrset_valid(struct cf_ctrset_entry *set)
39 {
40 return set->def == S390_CPUMCF_DIAG_DEF;
41 }
42
43 /* CPU Measurement Counter Facility raw data is a byte stream. It is 8 byte
44 * aligned and might have trailing padding bytes.
45 * Display the raw data on screen.
46 */
s390_cpumcfdg_testctr(struct perf_sample * sample)47 static bool s390_cpumcfdg_testctr(struct perf_sample *sample)
48 {
49 size_t len = sample->raw_size, offset = 0;
50 unsigned char *buf = sample->raw_data;
51 struct cf_trailer_entry *te;
52 struct cf_ctrset_entry *cep, ce;
53
54 if (!len)
55 return false;
56 while (offset < len) {
57 cep = (struct cf_ctrset_entry *)(buf + offset);
58 ce.def = be16_to_cpu(cep->def);
59 ce.set = be16_to_cpu(cep->set);
60 ce.ctr = be16_to_cpu(cep->ctr);
61 ce.res1 = be16_to_cpu(cep->res1);
62
63 if (!ctrset_valid(&ce) || offset + ctrset_size(&ce) > len) {
64 /* Raw data for counter sets are always multiple of 8
65 * bytes. Prepending a 4 bytes size field to the
66 * raw data block in the sample causes the perf tool
67 * to append 4 padding bytes to make the raw data part
68 * of the sample a multiple of eight bytes again.
69 *
70 * If the last entry (trailer) is 4 bytes off the raw
71 * area data end, all is good.
72 */
73 if (len - offset - sizeof(*te) == 4)
74 break;
75 pr_err("Invalid counter set entry at %zd\n", offset);
76 return false;
77 }
78 offset += ctrset_size(&ce);
79 }
80 return true;
81 }
82
83 /* Dump event bc000 on screen, already tested on correctness. */
s390_cpumcfdg_dumptrail(const char * color,size_t offset,struct cf_trailer_entry * tep)84 static void s390_cpumcfdg_dumptrail(const char *color, size_t offset,
85 struct cf_trailer_entry *tep)
86 {
87 struct cf_trailer_entry te;
88
89 te.flags = be64_to_cpu(tep->flags);
90 te.cfvn = be16_to_cpu(tep->cfvn);
91 te.csvn = be16_to_cpu(tep->csvn);
92 te.cpu_speed = be32_to_cpu(tep->cpu_speed);
93 te.timestamp = be64_to_cpu(tep->timestamp);
94 te.progusage1 = be64_to_cpu(tep->progusage1);
95 te.progusage2 = be64_to_cpu(tep->progusage2);
96 te.progusage3 = be64_to_cpu(tep->progusage3);
97 te.tod_base = be64_to_cpu(tep->tod_base);
98 te.mach_type = be16_to_cpu(tep->mach_type);
99 te.res1 = be16_to_cpu(tep->res1);
100 te.res2 = be32_to_cpu(tep->res2);
101
102 color_fprintf(stdout, color, " [%#08zx] Trailer:%c%c%c%c%c"
103 " Cfvn:%d Csvn:%d Speed:%d TOD:%#llx\n",
104 offset, te.clock_base ? 'T' : ' ',
105 te.speed ? 'S' : ' ', te.mtda ? 'M' : ' ',
106 te.caca ? 'C' : ' ', te.lcda ? 'L' : ' ',
107 te.cfvn, te.csvn, te.cpu_speed, te.timestamp);
108 color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#llx"
109 " Type:%x\n\n",
110 te.progusage1, te.progusage2, te.progusage3,
111 te.tod_base, te.mach_type);
112 }
113
114 /* Return starting number of a counter set */
get_counterset_start(int setnr)115 static int get_counterset_start(int setnr)
116 {
117 switch (setnr) {
118 case CPUMF_CTR_SET_BASIC: /* Basic counter set */
119 return 0;
120 case CPUMF_CTR_SET_USER: /* Problem state counter set */
121 return 32;
122 case CPUMF_CTR_SET_CRYPTO: /* Crypto counter set */
123 return 64;
124 case CPUMF_CTR_SET_EXT: /* Extended counter set */
125 return 128;
126 case CPUMF_CTR_SET_MT_DIAG: /* Diagnostic counter set */
127 return 448;
128 default:
129 return -1;
130 }
131 }
132
133 struct get_counter_name_data {
134 int wanted;
135 char *result;
136 };
137
get_counter_name_callback(void * vdata,struct pmu_event_info * info)138 static int get_counter_name_callback(void *vdata, struct pmu_event_info *info)
139 {
140 struct get_counter_name_data *data = vdata;
141 int rc, event_nr;
142 const char *event_str;
143
144 if (info->str == NULL)
145 return 0;
146
147 event_str = strstr(info->str, "event=");
148 if (!event_str)
149 return 0;
150
151 rc = sscanf(event_str, "event=%x", &event_nr);
152 if (rc == 1 && event_nr == data->wanted) {
153 data->result = strdup(info->name);
154 return 1; /* Terminate the search. */
155 }
156 return 0;
157 }
158
159 /* Scan the PMU and extract the logical name of a counter from the event. Input
160 * is the counter set and counter number with in the set. Construct the event
161 * number and use this as key. If they match return the name of this counter.
162 * If no match is found a NULL pointer is returned.
163 */
get_counter_name(int set,int nr,struct perf_pmu * pmu)164 static char *get_counter_name(int set, int nr, struct perf_pmu *pmu)
165 {
166 struct get_counter_name_data data = {
167 .wanted = get_counterset_start(set) + nr,
168 .result = NULL,
169 };
170
171 if (!pmu)
172 return NULL;
173
174 perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ true,
175 &data, get_counter_name_callback);
176 return data.result;
177 }
178
s390_cpumcfdg_dump(struct perf_pmu * pmu,struct perf_sample * sample)179 static void s390_cpumcfdg_dump(struct perf_pmu *pmu, struct perf_sample *sample)
180 {
181 size_t i, len = sample->raw_size, offset = 0;
182 unsigned char *buf = sample->raw_data;
183 const char *color = PERF_COLOR_BLUE;
184 struct cf_ctrset_entry *cep, ce;
185 u64 *p;
186
187 while (offset < len) {
188 cep = (struct cf_ctrset_entry *)(buf + offset);
189
190 ce.def = be16_to_cpu(cep->def);
191 ce.set = be16_to_cpu(cep->set);
192 ce.ctr = be16_to_cpu(cep->ctr);
193 ce.res1 = be16_to_cpu(cep->res1);
194
195 if (!ctrset_valid(&ce)) { /* Print trailer */
196 s390_cpumcfdg_dumptrail(color, offset,
197 (struct cf_trailer_entry *)cep);
198 return;
199 }
200
201 color_fprintf(stdout, color, " [%#08zx] Counterset:%d"
202 " Counters:%d\n", offset, ce.set, ce.ctr);
203 for (i = 0, p = (u64 *)(cep + 1); i < ce.ctr; ++i, ++p) {
204 char *ev_name = get_counter_name(ce.set, i, pmu);
205
206 color_fprintf(stdout, color,
207 "\tCounter:%03d %s Value:%#018lx\n", i,
208 ev_name ?: "<unknown>", be64_to_cpu(*p));
209 free(ev_name);
210 }
211 offset += ctrset_size(&ce);
212 }
213 }
214
215 /* S390 specific trace event function. Check for PERF_RECORD_SAMPLE events
216 * and if the event was triggered by a counter set diagnostic event display
217 * its raw data.
218 * The function is only invoked when the dump flag -D is set.
219 */
evlist__s390_sample_raw(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)220 void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
221 {
222 struct evsel *evsel;
223
224 if (event->header.type != PERF_RECORD_SAMPLE)
225 return;
226
227 evsel = evlist__event2evsel(evlist, event);
228 if (evsel == NULL ||
229 evsel->core.attr.config != PERF_EVENT_CPUM_CF_DIAG)
230 return;
231
232 /* Display raw data on screen */
233 if (!s390_cpumcfdg_testctr(sample)) {
234 pr_err("Invalid counter set data encountered\n");
235 return;
236 }
237 s390_cpumcfdg_dump(evsel->pmu, sample);
238 }
239