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