1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2018
4 * Auxtrace support for s390 CPU-Measurement Sampling Facility
6 * Author(s): Thomas Richter <tmricht@linux.ibm.com>
8 * Auxiliary traces are collected during 'perf record' using rbd000 event.
9 * Several PERF_RECORD_XXX are generated during recording:
12 * Records that new data landed in the AUX buffer part.
13 * PERF_RECORD_AUXTRACE:
14 * Defines auxtrace data. Followed by the actual data. The contents of
15 * the auxtrace data is dependent on the event and the CPU.
16 * This record is generated by perf record command. For details
17 * see Documentation/perf.data-file-format.txt.
18 * PERF_RECORD_AUXTRACE_INFO:
19 * Defines a table of contains for PERF_RECORD_AUXTRACE records. This
20 * record is generated during 'perf record' command. Each record contains up
21 * to 256 entries describing offset and size of the AUXTRACE data in the
23 * PERF_RECORD_AUXTRACE_ERROR:
24 * Indicates an error during AUXTRACE collection such as buffer overflow.
25 * PERF_RECORD_FINISHED_ROUND:
26 * Perf events are not necessarily in time stamp order, as they can be
27 * collected in parallel on different CPUs. If the events should be
28 * processed in time order they need to be sorted first.
29 * Perf report guarantees that there is no reordering over a
30 * PERF_RECORD_FINISHED_ROUND boundary event. All perf records with a
31 * time stamp lower than this record are processed (and displayed) before
32 * the succeeding perf record are processed.
34 * These records are evaluated during perf report command.
36 * 1. PERF_RECORD_AUXTRACE_INFO is used to set up the infrastructure for
37 * auxiliary trace data processing. See s390_cpumsf_process_auxtrace_info()
39 * Auxiliary trace data is collected per CPU. To merge the data into the report
40 * an auxtrace_queue is created for each CPU. It is assumed that the auxtrace
41 * data is in ascending order.
43 * Each queue has a double linked list of auxtrace_buffers. This list contains
44 * the offset and size of a CPU's auxtrace data. During auxtrace processing
45 * the data portion is mmap()'ed.
47 * To sort the queues in chronological order, all queue access is controlled
48 * by the auxtrace_heap. This is basicly a stack, each stack element has two
49 * entries, the queue number and a time stamp. However the stack is sorted by
50 * the time stamps. The highest time stamp is at the bottom the lowest
51 * (nearest) time stamp is at the top. That sort order is maintained at all
54 * After the auxtrace infrastructure has been setup, the auxtrace queues are
55 * filled with data (offset/size pairs) and the auxtrace_heap is populated.
57 * 2. PERF_RECORD_XXX processing triggers access to the auxtrace_queues.
58 * Each record is handled by s390_cpumsf_process_event(). The time stamp of
59 * the perf record is compared with the time stamp located on the auxtrace_heap
60 * top element. If that time stamp is lower than the time stamp from the
61 * record sample, the auxtrace queues will be processed. As auxtrace queues
62 * control many auxtrace_buffers and each buffer can be quite large, the
63 * auxtrace buffer might be processed only partially. In this case the
64 * position in the auxtrace_buffer of that queue is remembered and the time
65 * stamp of the last processed entry of the auxtrace_buffer replaces the
66 * current auxtrace_heap top.
68 * 3. Auxtrace_queues might run of out data and are feeded by the
69 * PERF_RECORD_AUXTRACE handling, see s390_cpumsf_process_auxtrace_event().
72 * Each sampling-data entry in the auxilary trace data generates a perf sample.
73 * This sample is filled
74 * with data from the auxtrace such as PID/TID, instruction address, CPU state,
75 * etc. This sample is processed with perf_session__deliver_synth_event() to
76 * be included into the GUI.
78 * 4. PERF_RECORD_FINISHED_ROUND event is used to process all the remaining
79 * auxiliary traces entries until the time stamp of this record is reached
80 * auxtrace_heap top. This is triggered by ordered_event->deliver().
83 * Perf event processing.
84 * Event processing of PERF_RECORD_XXX entries relies on time stamp entries.
85 * This is the function call sequence:
89 * perf_session__process_events()
91 * __perf_session__process_events()
93 * perf_session__process_event()
94 * | This functions splits the PERF_RECORD_XXX records.
95 * | - Those generated by perf record command (type number equal or higher
96 * | than PERF_RECORD_USER_TYPE_START) are handled by
97 * | perf_session__process_user_event(see below)
98 * | - Those generated by the kernel are handled by
99 * | perf_evlist__parse_sample_timestamp()
101 * perf_evlist__parse_sample_timestamp()
102 * | Extract time stamp from sample data.
104 * perf_session__queue_event()
105 * | If timestamp is positive the sample is entered into an ordered_event
106 * | list, sort order is the timestamp. The event processing is deferred until
107 * | later (see perf_session__process_user_event()).
108 * | Other timestamps (0 or -1) are handled immediately by
109 * | perf_session__deliver_event(). These are events generated at start up
110 * | of command perf record. They create PERF_RECORD_COMM and PERF_RECORD_MMAP*
111 * | records. They are needed to create a list of running processes and its
112 * | memory mappings and layout. They are needed at the beginning to enable
113 * | command perf report to create process trees and memory mappings.
115 * perf_session__deliver_event()
116 * | Delivers a PERF_RECORD_XXX entry for handling.
118 * auxtrace__process_event()
119 * | The timestamp of the PERF_RECORD_XXX entry is taken to correlate with
120 * | time stamps from the auxiliary trace buffers. This enables
121 * | synchronization between auxiliary trace data and the events on the
124 * machine__deliver_event()
125 * | Handles the PERF_RECORD_XXX event. This depends on the record type.
126 * It might update the process tree, update a process memory map or enter
127 * a sample with IP and call back chain data into GUI data pool.
130 * Deferred processing determined by perf_session__process_user_event() is
131 * finally processed when a PERF_RECORD_FINISHED_ROUND is encountered. These
132 * are generated during command perf record.
133 * The timestamp of PERF_RECORD_FINISHED_ROUND event is taken to process all
134 * PERF_RECORD_XXX entries stored in the ordered_event list. This list was
135 * built up while reading the perf.data file.
136 * Each event is now processed by calling perf_session__deliver_event().
137 * This enables time synchronization between the data in the perf.data file and
138 * the data in the auxiliary trace buffers.
143 #include <byteswap.h>
144 #include <inttypes.h>
145 #include <linux/kernel.h>
146 #include <linux/types.h>
147 #include <linux/bitops.h>
148 #include <linux/log2.h>
150 #include <sys/stat.h>
151 #include <sys/types.h>
162 #include "auxtrace.h"
163 #include "s390-cpumsf.h"
164 #include "s390-cpumsf-kernel.h"
165 #include "s390-cpumcf-kernel.h"
169 struct auxtrace auxtrace
;
170 struct auxtrace_queues queues
;
171 struct auxtrace_heap heap
;
172 struct perf_session
*session
;
173 struct machine
*machine
;
182 struct s390_cpumsf_queue
{
183 struct s390_cpumsf
*sf
;
184 unsigned int queue_nr
;
185 struct auxtrace_buffer
*buffer
;
191 /* Check if the raw data should be dumped to file. If this is the case and
192 * the file to dump to has not been opened for writing, do so.
194 * Return 0 on success and greater zero on error so processing continues.
196 static int s390_cpumcf_dumpctr(struct s390_cpumsf
*sf
,
197 struct perf_sample
*sample
)
199 struct s390_cpumsf_queue
*sfq
;
200 struct auxtrace_queue
*q
;
203 if (!sf
->use_logfile
|| sf
->queues
.nr_queues
<= sample
->cpu
)
206 q
= &sf
->queues
.queue_array
[sample
->cpu
];
208 if (!sfq
) /* Queue not yet allocated */
211 if (!sfq
->logfile_ctr
) {
215 ? asprintf(&name
, "%s/aux.ctr.%02x",
216 sf
->logdir
, sample
->cpu
)
217 : asprintf(&name
, "aux.ctr.%02x", sample
->cpu
);
219 sfq
->logfile_ctr
= fopen(name
, "w");
220 if (sfq
->logfile_ctr
== NULL
) {
221 pr_err("Failed to open counter set log file %s, "
222 "continue...\n", name
);
228 if (sfq
->logfile_ctr
) {
229 /* See comment above for -4 */
230 size_t n
= fwrite(sample
->raw_data
, sample
->raw_size
- 4, 1,
233 pr_err("Failed to write counter set data\n");
240 /* Display s390 CPU measurement facility basic-sampling data entry */
241 static bool s390_cpumsf_basic_show(const char *color
, size_t pos
,
242 struct hws_basic_entry
*basic
)
244 if (basic
->def
!= 1) {
245 pr_err("Invalid AUX trace basic entry [%#08zx]\n", pos
);
248 color_fprintf(stdout
, color
, " [%#08zx] Basic Def:%04x Inst:%#04x"
249 " %c%c%c%c AS:%d ASN:%#04x IA:%#018llx\n"
250 "\t\tCL:%d HPP:%#018llx GPP:%#018llx\n",
251 pos
, basic
->def
, basic
->U
,
252 basic
->T
? 'T' : ' ',
253 basic
->W
? 'W' : ' ',
254 basic
->P
? 'P' : ' ',
255 basic
->I
? 'I' : ' ',
256 basic
->AS
, basic
->prim_asn
, basic
->ia
, basic
->CL
,
257 basic
->hpp
, basic
->gpp
);
261 /* Display s390 CPU measurement facility diagnostic-sampling data entry */
262 static bool s390_cpumsf_diag_show(const char *color
, size_t pos
,
263 struct hws_diag_entry
*diag
)
265 if (diag
->def
< S390_CPUMSF_DIAG_DEF_FIRST
) {
266 pr_err("Invalid AUX trace diagnostic entry [%#08zx]\n", pos
);
269 color_fprintf(stdout
, color
, " [%#08zx] Diag Def:%04x %c\n",
270 pos
, diag
->def
, diag
->I
? 'I' : ' ');
274 /* Return TOD timestamp contained in an trailer entry */
275 static unsigned long long trailer_timestamp(struct hws_trailer_entry
*te
)
277 /* te->t set: TOD in STCKE format, bytes 8-15
278 * to->t not set: TOD in STCK format, bytes 0-7
280 unsigned long long ts
;
282 memcpy(&ts
, &te
->timestamp
[te
->t
], sizeof(ts
));
286 /* Display s390 CPU measurement facility trailer entry */
287 static bool s390_cpumsf_trailer_show(const char *color
, size_t pos
,
288 struct hws_trailer_entry
*te
)
290 if (te
->bsdes
!= sizeof(struct hws_basic_entry
)) {
291 pr_err("Invalid AUX trace trailer entry [%#08zx]\n", pos
);
294 color_fprintf(stdout
, color
, " [%#08zx] Trailer %c%c%c bsdes:%d"
295 " dsdes:%d Overflow:%lld Time:%#llx\n"
296 "\t\tC:%d TOD:%#lx 1:%#llx 2:%#llx\n",
301 te
->bsdes
, te
->dsdes
, te
->overflow
,
302 trailer_timestamp(te
), te
->clock_base
, te
->progusage2
,
303 te
->progusage
[0], te
->progusage
[1]);
307 /* Test a sample data block. It must be 4KB or a multiple thereof in size and
308 * 4KB page aligned. Each sample data page has a trailer entry at the
309 * end which contains the sample entry data sizes.
311 * Return true if the sample data block passes the checks and set the
312 * basic set entry size and diagnostic set entry size.
314 * Return false on failure.
316 * Note: Old hardware does not set the basic or diagnostic entry sizes
317 * in the trailer entry. Use the type number instead.
319 static bool s390_cpumsf_validate(int machine_type
,
320 unsigned char *buf
, size_t len
,
321 unsigned short *bsdes
,
322 unsigned short *dsdes
)
324 struct hws_basic_entry
*basic
= (struct hws_basic_entry
*)buf
;
325 struct hws_trailer_entry
*te
;
328 if (len
& (S390_CPUMSF_PAGESZ
- 1)) /* Illegal size */
330 if (basic
->def
!= 1) /* No basic set entry, must be first */
332 /* Check for trailer entry at end of SDB */
333 te
= (struct hws_trailer_entry
*)(buf
+ S390_CPUMSF_PAGESZ
337 if (!te
->bsdes
&& !te
->dsdes
) {
338 /* Very old hardware, use CPUID */
339 switch (machine_type
) {
361 /* Illegal trailer entry */
368 /* Return true if there is room for another entry */
369 static bool s390_cpumsf_reached_trailer(size_t entry_sz
, size_t pos
)
371 size_t payload
= S390_CPUMSF_PAGESZ
- sizeof(struct hws_trailer_entry
);
373 if (payload
- (pos
& (S390_CPUMSF_PAGESZ
- 1)) < entry_sz
)
378 /* Dump an auxiliary buffer. These buffers are multiple of
381 static void s390_cpumsf_dump(struct s390_cpumsf
*sf
,
382 unsigned char *buf
, size_t len
)
384 const char *color
= PERF_COLOR_BLUE
;
385 struct hws_basic_entry
*basic
;
386 struct hws_diag_entry
*diag
;
387 unsigned short bsdes
, dsdes
;
390 color_fprintf(stdout
, color
,
391 ". ... s390 AUX data: size %zu bytes\n",
394 if (!s390_cpumsf_validate(sf
->machine_type
, buf
, len
, &bsdes
,
396 pr_err("Invalid AUX trace data block size:%zu"
397 " (type:%d bsdes:%hd dsdes:%hd)\n",
398 len
, sf
->machine_type
, bsdes
, dsdes
);
402 /* s390 kernel always returns 4KB blocks fully occupied,
403 * no partially filled SDBs.
406 /* Handle Basic entry */
407 basic
= (struct hws_basic_entry
*)(buf
+ pos
);
408 if (s390_cpumsf_basic_show(color
, pos
, basic
))
413 /* Handle Diagnostic entry */
414 diag
= (struct hws_diag_entry
*)(buf
+ pos
);
415 if (s390_cpumsf_diag_show(color
, pos
, diag
))
420 /* Check for trailer entry */
421 if (!s390_cpumsf_reached_trailer(bsdes
+ dsdes
, pos
)) {
422 /* Show trailer entry */
423 struct hws_trailer_entry te
;
425 pos
= (pos
+ S390_CPUMSF_PAGESZ
)
426 & ~(S390_CPUMSF_PAGESZ
- 1);
428 memcpy(&te
, buf
+ pos
, sizeof(te
));
429 /* Set descriptor sizes in case of old hardware
430 * where these values are not set.
434 if (s390_cpumsf_trailer_show(color
, pos
, &te
))
442 static void s390_cpumsf_dump_event(struct s390_cpumsf
*sf
, unsigned char *buf
,
446 s390_cpumsf_dump(sf
, buf
, len
);
449 #define S390_LPP_PID_MASK 0xffffffff
451 static bool s390_cpumsf_make_event(size_t pos
,
452 struct hws_basic_entry
*basic
,
453 struct s390_cpumsf_queue
*sfq
)
455 struct perf_sample sample
= {
457 .pid
= basic
->hpp
& S390_LPP_PID_MASK
,
458 .tid
= basic
->hpp
& S390_LPP_PID_MASK
,
459 .cpumode
= PERF_RECORD_MISC_CPUMODE_UNKNOWN
,
463 union perf_event event
;
465 memset(&event
, 0, sizeof(event
));
466 if (basic
->CL
== 1) /* Native LPAR mode */
467 sample
.cpumode
= basic
->P
? PERF_RECORD_MISC_USER
468 : PERF_RECORD_MISC_KERNEL
;
469 else if (basic
->CL
== 2) /* Guest kernel/user space */
470 sample
.cpumode
= basic
->P
? PERF_RECORD_MISC_GUEST_USER
471 : PERF_RECORD_MISC_GUEST_KERNEL
;
472 else if (basic
->gpp
|| basic
->prim_asn
!= 0xffff)
473 /* Use heuristics on old hardware */
474 sample
.cpumode
= basic
->P
? PERF_RECORD_MISC_GUEST_USER
475 : PERF_RECORD_MISC_GUEST_KERNEL
;
477 sample
.cpumode
= basic
->P
? PERF_RECORD_MISC_USER
478 : PERF_RECORD_MISC_KERNEL
;
480 event
.sample
.header
.type
= PERF_RECORD_SAMPLE
;
481 event
.sample
.header
.misc
= sample
.cpumode
;
482 event
.sample
.header
.size
= sizeof(struct perf_event_header
);
484 pr_debug4("%s pos:%#zx ip:%#" PRIx64
" P:%d CL:%d pid:%d.%d cpumode:%d cpu:%d\n",
485 __func__
, pos
, sample
.ip
, basic
->P
, basic
->CL
, sample
.pid
,
486 sample
.tid
, sample
.cpumode
, sample
.cpu
);
487 if (perf_session__deliver_synth_event(sfq
->sf
->session
, &event
,
489 pr_err("s390 Auxiliary Trace: failed to deliver event\n");
495 static unsigned long long get_trailer_time(const unsigned char *buf
)
497 struct hws_trailer_entry
*te
;
498 unsigned long long aux_time
;
500 te
= (struct hws_trailer_entry
*)(buf
+ S390_CPUMSF_PAGESZ
503 if (!te
->clock_base
) /* TOD_CLOCK_BASE value missing */
506 /* Correct calculation to convert time stamp in trailer entry to
507 * nano seconds (taken from arch/s390 function tod_to_ns()).
508 * TOD_CLOCK_BASE is stored in trailer entry member progusage2.
510 aux_time
= trailer_timestamp(te
) - te
->progusage2
;
511 aux_time
= (aux_time
>> 9) * 125 + (((aux_time
& 0x1ff) * 125) >> 9);
515 /* Process the data samples of a single queue. The first parameter is a
516 * pointer to the queue, the second parameter is the time stamp. This
518 * - of the event that triggered this processing.
519 * - or the time stamp when the last proccesing of this queue stopped.
520 * In this case it stopped at a 4KB page boundary and record the
521 * position on where to continue processing on the next invocation
522 * (see buffer->use_data and buffer->use_size).
524 * When this function returns the second parameter is updated to
525 * reflect the time stamp of the last processed auxiliary data entry
526 * (taken from the trailer entry of that page). The caller uses this
527 * returned time stamp to record the last processed entry in this
530 * The function returns:
531 * 0: Processing successful. The second parameter returns the
532 * time stamp from the trailer entry until which position
533 * processing took place. Subsequent calls resume from this
535 * <0: An error occurred during processing. The second parameter
536 * returns the maximum time stamp.
537 * >0: Done on this queue. The second parameter returns the
538 * maximum time stamp.
540 static int s390_cpumsf_samples(struct s390_cpumsf_queue
*sfq
, u64
*ts
)
542 struct s390_cpumsf
*sf
= sfq
->sf
;
543 unsigned char *buf
= sfq
->buffer
->use_data
;
544 size_t len
= sfq
->buffer
->use_size
;
545 struct hws_basic_entry
*basic
;
546 unsigned short bsdes
, dsdes
;
551 if (!s390_cpumsf_validate(sf
->machine_type
, buf
, len
, &bsdes
,
557 /* Get trailer entry time stamp and check if entries in
558 * this auxiliary page are ready for processing. If the
559 * time stamp of the first entry is too high, whole buffer
560 * can be skipped. In this case return time stamp.
562 aux_ts
= get_trailer_time(buf
);
564 pr_err("[%#08" PRIx64
"] Invalid AUX trailer entry TOD clock base\n",
565 (s64
)sfq
->buffer
->data_offset
);
575 /* Handle Basic entry */
576 basic
= (struct hws_basic_entry
*)(buf
+ pos
);
577 if (s390_cpumsf_make_event(pos
, basic
, sfq
))
584 pos
+= dsdes
; /* Skip diagnositic entry */
586 /* Check for trailer entry */
587 if (!s390_cpumsf_reached_trailer(bsdes
+ dsdes
, pos
)) {
588 pos
= (pos
+ S390_CPUMSF_PAGESZ
)
589 & ~(S390_CPUMSF_PAGESZ
- 1);
590 /* Check existence of next page */
593 aux_ts
= get_trailer_time(buf
+ pos
);
600 sfq
->buffer
->use_data
+= pos
;
601 sfq
->buffer
->use_size
-= pos
;
608 sfq
->buffer
->use_size
= 0;
609 sfq
->buffer
->use_data
= NULL
;
610 return err
; /* Buffer completely scanned or error */
613 /* Run the s390 auxiliary trace decoder.
614 * Select the queue buffer to operate on, the caller already selected
615 * the proper queue, depending on second parameter 'ts'.
616 * This is the time stamp until which the auxiliary entries should
617 * be processed. This value is updated by called functions and
618 * returned to the caller.
620 * Resume processing in the current buffer. If there is no buffer
621 * get a new buffer from the queue and setup start position for
623 * When a buffer is completely processed remove it from the queue
626 * This function returns
627 * 1: When the queue is empty. Second parameter will be set to
628 * maximum time stamp.
629 * 0: Normal processing done.
630 * <0: Error during queue buffer setup. This causes the caller
631 * to stop processing completely.
633 static int s390_cpumsf_run_decoder(struct s390_cpumsf_queue
*sfq
,
637 struct auxtrace_buffer
*buffer
;
638 struct auxtrace_queue
*queue
;
641 queue
= &sfq
->sf
->queues
.queue_array
[sfq
->queue_nr
];
643 /* Get buffer and last position in buffer to resume
644 * decoding the auxiliary entries. One buffer might be large
645 * and decoding might stop in between. This depends on the time
646 * stamp of the trailer entry in each page of the auxiliary
647 * data and the time stamp of the event triggering the decoding.
649 if (sfq
->buffer
== NULL
) {
650 sfq
->buffer
= buffer
= auxtrace_buffer__next(queue
,
654 return 1; /* Processing done on this queue */
656 /* Start with a new buffer on this queue */
658 buffer
->use_size
= buffer
->size
;
659 buffer
->use_data
= buffer
->data
;
661 if (sfq
->logfile
) { /* Write into log file */
662 size_t rc
= fwrite(buffer
->data
, buffer
->size
, 1,
665 pr_err("Failed to write auxiliary data\n");
668 buffer
= sfq
->buffer
;
671 int fd
= perf_data__fd(sfq
->sf
->session
->data
);
673 buffer
->data
= auxtrace_buffer__get_data(buffer
, fd
);
676 buffer
->use_size
= buffer
->size
;
677 buffer
->use_data
= buffer
->data
;
679 if (sfq
->logfile
) { /* Write into log file */
680 size_t rc
= fwrite(buffer
->data
, buffer
->size
, 1,
683 pr_err("Failed to write auxiliary data\n");
686 pr_debug4("%s queue_nr:%d buffer:%" PRId64
" offset:%#" PRIx64
" size:%#zx rest:%#zx\n",
687 __func__
, sfq
->queue_nr
, buffer
->buffer_nr
, buffer
->offset
,
688 buffer
->size
, buffer
->use_size
);
689 err
= s390_cpumsf_samples(sfq
, ts
);
691 /* If non-zero, there is either an error (err < 0) or the buffer is
692 * completely done (err > 0). The error is unrecoverable, usually
693 * some descriptors could not be read successfully, so continue with
695 * In both cases the parameter 'ts' has been updated.
699 list_del(&buffer
->list
);
700 auxtrace_buffer__free(buffer
);
701 if (err
> 0) /* Buffer done, no error */
707 static struct s390_cpumsf_queue
*
708 s390_cpumsf_alloc_queue(struct s390_cpumsf
*sf
, unsigned int queue_nr
)
710 struct s390_cpumsf_queue
*sfq
;
712 sfq
= zalloc(sizeof(struct s390_cpumsf_queue
));
717 sfq
->queue_nr
= queue_nr
;
719 if (sf
->use_logfile
) {
724 ? asprintf(&name
, "%s/aux.smp.%02x",
725 sf
->logdir
, queue_nr
)
726 : asprintf(&name
, "aux.smp.%02x", queue_nr
);
728 sfq
->logfile
= fopen(name
, "w");
729 if (sfq
->logfile
== NULL
) {
730 pr_err("Failed to open auxiliary log file %s,"
731 "continue...\n", name
);
732 sf
->use_logfile
= false;
739 static int s390_cpumsf_setup_queue(struct s390_cpumsf
*sf
,
740 struct auxtrace_queue
*queue
,
741 unsigned int queue_nr
, u64 ts
)
743 struct s390_cpumsf_queue
*sfq
= queue
->priv
;
745 if (list_empty(&queue
->head
))
749 sfq
= s390_cpumsf_alloc_queue(sf
, queue_nr
);
754 if (queue
->cpu
!= -1)
755 sfq
->cpu
= queue
->cpu
;
757 return auxtrace_heap__add(&sf
->heap
, queue_nr
, ts
);
760 static int s390_cpumsf_setup_queues(struct s390_cpumsf
*sf
, u64 ts
)
765 for (i
= 0; i
< sf
->queues
.nr_queues
; i
++) {
766 ret
= s390_cpumsf_setup_queue(sf
, &sf
->queues
.queue_array
[i
],
774 static int s390_cpumsf_update_queues(struct s390_cpumsf
*sf
, u64 ts
)
776 if (!sf
->queues
.new_data
)
779 sf
->queues
.new_data
= false;
780 return s390_cpumsf_setup_queues(sf
, ts
);
783 static int s390_cpumsf_process_queues(struct s390_cpumsf
*sf
, u64 timestamp
)
785 unsigned int queue_nr
;
790 struct auxtrace_queue
*queue
;
791 struct s390_cpumsf_queue
*sfq
;
793 if (!sf
->heap
.heap_cnt
)
796 if (sf
->heap
.heap_array
[0].ordinal
>= timestamp
)
799 queue_nr
= sf
->heap
.heap_array
[0].queue_nr
;
800 queue
= &sf
->queues
.queue_array
[queue_nr
];
803 auxtrace_heap__pop(&sf
->heap
);
804 if (sf
->heap
.heap_cnt
) {
805 ts
= sf
->heap
.heap_array
[0].ordinal
+ 1;
812 ret
= s390_cpumsf_run_decoder(sfq
, &ts
);
814 auxtrace_heap__add(&sf
->heap
, queue_nr
, ts
);
818 ret
= auxtrace_heap__add(&sf
->heap
, queue_nr
, ts
);
826 static int s390_cpumsf_synth_error(struct s390_cpumsf
*sf
, int code
, int cpu
,
827 pid_t pid
, pid_t tid
, u64 ip
, u64 timestamp
)
829 char msg
[MAX_AUXTRACE_ERROR_MSG
];
830 union perf_event event
;
833 strncpy(msg
, "Lost Auxiliary Trace Buffer", sizeof(msg
) - 1);
834 auxtrace_synth_error(&event
.auxtrace_error
, PERF_AUXTRACE_ERROR_ITRACE
,
835 code
, cpu
, pid
, tid
, ip
, msg
, timestamp
);
837 err
= perf_session__deliver_synth_event(sf
->session
, &event
, NULL
);
839 pr_err("s390 Auxiliary Trace: failed to deliver error event,"
844 static int s390_cpumsf_lost(struct s390_cpumsf
*sf
, struct perf_sample
*sample
)
846 return s390_cpumsf_synth_error(sf
, 1, sample
->cpu
,
847 sample
->pid
, sample
->tid
, 0,
852 s390_cpumsf_process_event(struct perf_session
*session
,
853 union perf_event
*event
,
854 struct perf_sample
*sample
,
855 struct perf_tool
*tool
)
857 struct s390_cpumsf
*sf
= container_of(session
->auxtrace
,
860 u64 timestamp
= sample
->time
;
861 struct perf_evsel
*ev_bc000
;
868 if (!tool
->ordered_events
) {
869 pr_err("s390 Auxiliary Trace requires ordered events\n");
873 if (event
->header
.type
== PERF_RECORD_SAMPLE
&&
875 /* Handle event with raw data */
876 ev_bc000
= perf_evlist__event2evsel(session
->evlist
, event
);
878 ev_bc000
->attr
.config
== PERF_EVENT_CPUM_CF_DIAG
)
879 err
= s390_cpumcf_dumpctr(sf
, sample
);
883 if (event
->header
.type
== PERF_RECORD_AUX
&&
884 event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
)
885 return s390_cpumsf_lost(sf
, sample
);
888 err
= s390_cpumsf_update_queues(sf
, timestamp
);
890 err
= s390_cpumsf_process_queues(sf
, timestamp
);
895 struct s390_cpumsf_synth
{
896 struct perf_tool cpumsf_tool
;
897 struct perf_session
*session
;
901 s390_cpumsf_process_auxtrace_event(struct perf_session
*session
,
902 union perf_event
*event __maybe_unused
,
903 struct perf_tool
*tool __maybe_unused
)
905 struct s390_cpumsf
*sf
= container_of(session
->auxtrace
,
909 int fd
= perf_data__fd(session
->data
);
910 struct auxtrace_buffer
*buffer
;
917 if (perf_data__is_pipe(session
->data
)) {
920 data_offset
= lseek(fd
, 0, SEEK_CUR
);
921 if (data_offset
== -1)
925 err
= auxtrace_queues__add_event(&sf
->queues
, session
, event
,
926 data_offset
, &buffer
);
930 /* Dump here after copying piped trace out of the pipe */
932 if (auxtrace_buffer__get_data(buffer
, fd
)) {
933 s390_cpumsf_dump_event(sf
, buffer
->data
,
935 auxtrace_buffer__put_data(buffer
);
941 static void s390_cpumsf_free_events(struct perf_session
*session __maybe_unused
)
945 static int s390_cpumsf_flush(struct perf_session
*session __maybe_unused
,
946 struct perf_tool
*tool __maybe_unused
)
951 static void s390_cpumsf_free_queues(struct perf_session
*session
)
953 struct s390_cpumsf
*sf
= container_of(session
->auxtrace
,
956 struct auxtrace_queues
*queues
= &sf
->queues
;
959 for (i
= 0; i
< queues
->nr_queues
; i
++) {
960 struct s390_cpumsf_queue
*sfq
= (struct s390_cpumsf_queue
*)
961 queues
->queue_array
[i
].priv
;
965 fclose(sfq
->logfile
);
968 if (sfq
->logfile_ctr
) {
969 fclose(sfq
->logfile_ctr
);
970 sfq
->logfile_ctr
= NULL
;
973 zfree(&queues
->queue_array
[i
].priv
);
975 auxtrace_queues__free(queues
);
978 static void s390_cpumsf_free(struct perf_session
*session
)
980 struct s390_cpumsf
*sf
= container_of(session
->auxtrace
,
984 auxtrace_heap__free(&sf
->heap
);
985 s390_cpumsf_free_queues(session
);
986 session
->auxtrace
= NULL
;
991 static int s390_cpumsf_get_type(const char *cpuid
)
995 ret
= sscanf(cpuid
, "%*[^,],%u", &family
);
996 return (ret
== 1) ? family
: 0;
999 /* Check itrace options set on perf report command.
1000 * Return true, if none are set or all options specified can be
1001 * handled on s390 (currently only option 'd' for logging.
1002 * Return false otherwise.
1004 static bool check_auxtrace_itrace(struct itrace_synth_opts
*itops
)
1008 if (!itops
|| !itops
->set
)
1010 ison
= itops
->inject
|| itops
->instructions
|| itops
->branches
||
1011 itops
->transactions
|| itops
->ptwrites
||
1012 itops
->pwr_events
|| itops
->errors
||
1013 itops
->dont_decode
|| itops
->calls
|| itops
->returns
||
1014 itops
->callchain
|| itops
->thread_stack
||
1018 pr_err("Unsupported --itrace options specified\n");
1022 /* Check for AUXTRACE dump directory if it is needed.
1023 * On failure print an error message but continue.
1024 * Return 0 on wrong keyword in config file and 1 otherwise.
1026 static int s390_cpumsf__config(const char *var
, const char *value
, void *cb
)
1028 struct s390_cpumsf
*sf
= cb
;
1032 if (strcmp(var
, "auxtrace.dumpdir"))
1034 sf
->logdir
= strdup(value
);
1035 if (sf
->logdir
== NULL
) {
1036 pr_err("Failed to find auxtrace log directory %s,"
1037 " continue with current directory...\n", value
);
1040 rc
= stat(sf
->logdir
, &stbuf
);
1041 if (rc
== -1 || !S_ISDIR(stbuf
.st_mode
)) {
1042 pr_err("Missing auxtrace log directory %s,"
1043 " continue with current directory...\n", value
);
1050 int s390_cpumsf_process_auxtrace_info(union perf_event
*event
,
1051 struct perf_session
*session
)
1053 struct auxtrace_info_event
*auxtrace_info
= &event
->auxtrace_info
;
1054 struct s390_cpumsf
*sf
;
1057 if (auxtrace_info
->header
.size
< sizeof(struct auxtrace_info_event
))
1060 sf
= zalloc(sizeof(struct s390_cpumsf
));
1064 if (!check_auxtrace_itrace(session
->itrace_synth_opts
)) {
1068 sf
->use_logfile
= session
->itrace_synth_opts
->log
;
1069 if (sf
->use_logfile
)
1070 perf_config(s390_cpumsf__config
, sf
);
1072 err
= auxtrace_queues__init(&sf
->queues
);
1076 sf
->session
= session
;
1077 sf
->machine
= &session
->machines
.host
; /* No kvm support */
1078 sf
->auxtrace_type
= auxtrace_info
->type
;
1079 sf
->pmu_type
= PERF_TYPE_RAW
;
1080 sf
->machine_type
= s390_cpumsf_get_type(session
->evlist
->env
->cpuid
);
1082 sf
->auxtrace
.process_event
= s390_cpumsf_process_event
;
1083 sf
->auxtrace
.process_auxtrace_event
= s390_cpumsf_process_auxtrace_event
;
1084 sf
->auxtrace
.flush_events
= s390_cpumsf_flush
;
1085 sf
->auxtrace
.free_events
= s390_cpumsf_free_events
;
1086 sf
->auxtrace
.free
= s390_cpumsf_free
;
1087 session
->auxtrace
= &sf
->auxtrace
;
1092 err
= auxtrace_queues__process_index(&sf
->queues
, session
);
1094 goto err_free_queues
;
1096 if (sf
->queues
.populated
)
1097 sf
->data_queued
= true;
1102 auxtrace_queues__free(&sf
->queues
);
1103 session
->auxtrace
= NULL
;