1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_tpebs.c: Intel TPEBS support
8 #include <subcmd/run-command.h>
10 #include "intel-tpebs.h"
11 #include <linux/list.h>
12 #include <linux/zalloc.h>
13 #include <linux/err.h>
21 #include "metricgroup.h"
30 static pid_t tpebs_pid
= -1;
31 static size_t tpebs_event_size
;
32 static LIST_HEAD(tpebs_results
);
33 static pthread_t tpebs_reader_thread
;
34 static struct child_process
*tpebs_cmd
;
36 struct tpebs_retire_lat
{
40 /* Event name with the TPEBS modifier R */
41 const char *tpebs_name
;
42 /* Count of retire_latency values found in sample data */
44 /* Sum of all the retire_latency values in sample data */
46 /* Average of retire_latency, val = sum / count */
50 static int get_perf_record_args(const char **record_argv
, char buf
[],
51 const char *cpumap_buf
)
53 struct tpebs_retire_lat
*e
;
56 pr_debug("tpebs: Prepare perf record for retire_latency\n");
58 record_argv
[i
++] = "perf";
59 record_argv
[i
++] = "record";
60 record_argv
[i
++] = "-W";
61 record_argv
[i
++] = "--synth=no";
62 record_argv
[i
++] = buf
;
65 pr_err("tpebs: Require cpumap list to run sampling\n");
68 /* Use -C when cpumap_buf is not "-1" */
69 if (strcmp(cpumap_buf
, "-1")) {
70 record_argv
[i
++] = "-C";
71 record_argv
[i
++] = cpumap_buf
;
74 list_for_each_entry(e
, &tpebs_results
, nd
) {
75 record_argv
[i
++] = "-e";
76 record_argv
[i
++] = e
->name
;
79 record_argv
[i
++] = "-o";
80 record_argv
[i
++] = PERF_DATA
;
85 static int prepare_run_command(const char **argv
)
87 tpebs_cmd
= zalloc(sizeof(struct child_process
));
90 tpebs_cmd
->argv
= argv
;
95 static int start_perf_record(int control_fd
[], int ack_fd
[],
96 const char *cpumap_buf
)
98 const char **record_argv
;
102 scnprintf(buf
, sizeof(buf
), "--control=fd:%d,%d", control_fd
[0], ack_fd
[1]);
104 record_argv
= calloc(12 + 2 * tpebs_event_size
, sizeof(char *));
108 ret
= get_perf_record_args(record_argv
, buf
, cpumap_buf
);
112 ret
= prepare_run_command(record_argv
);
115 ret
= start_command(tpebs_cmd
);
121 static int process_sample_event(const struct perf_tool
*tool __maybe_unused
,
122 union perf_event
*event __maybe_unused
,
123 struct perf_sample
*sample
,
125 struct machine
*machine __maybe_unused
)
129 struct tpebs_retire_lat
*t
;
131 evname
= evsel__name(evsel
);
134 * Need to handle per core results? We are assuming average retire
135 * latency value will be used. Save the number of samples and the sum of
136 * retire latency value for each event.
138 list_for_each_entry(t
, &tpebs_results
, nd
) {
139 if (!strcmp(evname
, t
->name
)) {
141 t
->sum
+= sample
->retire_lat
;
142 t
->val
= (double) t
->sum
/ t
->count
;
150 static int process_feature_event(struct perf_session
*session
,
151 union perf_event
*event
)
153 if (event
->feat
.feat_id
< HEADER_LAST_FEATURE
)
154 return perf_event__process_feature(session
, event
);
158 static void *__sample_reader(void *arg
)
160 struct child_process
*child
= arg
;
161 struct perf_session
*session
;
162 struct perf_data data
= {
163 .mode
= PERF_DATA_MODE_READ
,
165 .file
.fd
= child
->out
,
167 struct perf_tool tool
;
169 perf_tool__init(&tool
, /*ordered_events=*/false);
170 tool
.sample
= process_sample_event
;
171 tool
.feature
= process_feature_event
;
172 tool
.attr
= perf_event__process_attr
;
174 session
= perf_session__new(&data
, &tool
);
177 perf_session__process_events(session
);
178 perf_session__delete(session
);
184 * tpebs_stop - stop the sample data read thread and the perf record process.
186 static int tpebs_stop(void)
190 /* Like tpebs_start, we should only run tpebs_end once. */
191 if (tpebs_pid
!= -1) {
192 kill(tpebs_cmd
->pid
, SIGTERM
);
194 pthread_join(tpebs_reader_thread
, NULL
);
195 close(tpebs_cmd
->out
);
196 ret
= finish_command(tpebs_cmd
);
197 if (ret
== -ERR_RUN_COMMAND_WAITPID_SIGNAL
)
204 * tpebs_start - start tpebs execution.
205 * @evsel_list: retire_latency evsels in this list will be selected and sampled
206 * to get the average retire_latency value.
208 * This function will be called from evlist level later when evlist__open() is
209 * called consistently.
211 int tpebs_start(struct evlist
*evsel_list
)
218 * We should only run tpebs_start when tpebs_recording is enabled.
219 * And we should only run it once with all the required events.
221 if (tpebs_pid
!= -1 || !tpebs_recording
)
224 cpu_map__snprint(evsel_list
->core
.user_requested_cpus
, cpumap_buf
, sizeof(cpumap_buf
));
226 * Prepare perf record for sampling event retire_latency before fork and
229 evlist__for_each_entry(evsel_list
, evsel
) {
232 struct tpebs_retire_lat
*new;
234 if (!evsel
->retire_lat
)
237 pr_debug("tpebs: Retire_latency of event %s is required\n", evsel
->name
);
238 for (i
= strlen(evsel
->name
) - 1; i
> 0; i
--) {
239 if (evsel
->name
[i
] == 'R')
242 if (i
<= 0 || evsel
->name
[i
] != 'R') {
247 name
= strdup(evsel
->name
);
254 new = zalloc(sizeof(*new));
261 new->tpebs_name
= evsel
->name
;
262 list_add_tail(&new->nd
, &tpebs_results
);
263 tpebs_event_size
+= 1;
266 if (tpebs_event_size
> 0) {
267 struct pollfd pollfd
= { .events
= POLLIN
, };
268 int control_fd
[2], ack_fd
[2], len
;
271 /*Create control and ack fd for --control*/
272 if (pipe(control_fd
) < 0) {
273 pr_err("tpebs: Failed to create control fifo");
277 if (pipe(ack_fd
) < 0) {
278 pr_err("tpebs: Failed to create control fifo");
283 ret
= start_perf_record(control_fd
, ack_fd
, cpumap_buf
);
286 tpebs_pid
= tpebs_cmd
->pid
;
287 if (pthread_create(&tpebs_reader_thread
, NULL
, __sample_reader
, tpebs_cmd
)) {
288 kill(tpebs_cmd
->pid
, SIGTERM
);
289 close(tpebs_cmd
->out
);
290 pr_err("Could not create thread to process sample data.\n");
294 /* Wait for perf record initialization.*/
295 len
= strlen(EVLIST_CTL_CMD_ENABLE_TAG
);
296 ret
= write(control_fd
[1], EVLIST_CTL_CMD_ENABLE_TAG
, len
);
298 pr_err("perf record control write control message failed\n");
302 /* wait for an ack */
303 pollfd
.fd
= ack_fd
[0];
306 * We need this poll to ensure the ack_fd PIPE will not hang
307 * when perf record failed for any reason. The timeout value
308 * 3000ms is an empirical selection.
310 if (!poll(&pollfd
, 1, 3000)) {
311 pr_err("tpebs failed: perf record ack timeout\n");
316 if (!(pollfd
.revents
& POLLIN
)) {
317 pr_err("tpebs failed: did not received an ack\n");
322 ret
= read(ack_fd
[0], ack_buf
, sizeof(ack_buf
));
324 ret
= strcmp(ack_buf
, EVLIST_CTL_CMD_ACK_TAG
);
326 pr_err("tpebs: perf record control ack failed\n");
330 close(control_fd
[0]);
331 close(control_fd
[1]);
342 int tpebs_set_evsel(struct evsel
*evsel
, int cpu_map_idx
, int thread
)
346 struct tpebs_retire_lat
*t
;
347 struct perf_counts_values
*count
;
349 /* Non reitre_latency evsel should never enter this function. */
350 if (!evsel__is_retire_lat(evsel
))
354 * Need to stop the forked record to ensure get sampled data from the
355 * PIPE to process and get non-zero retire_lat value for hybrid.
358 count
= perf_counts(evsel
->counts
, cpu_map_idx
, thread
);
360 list_for_each_entry(t
, &tpebs_results
, nd
) {
361 if (t
->tpebs_name
== evsel
->name
||
362 (evsel
->metric_id
&& !strcmp(t
->tpebs_name
, evsel
->metric_id
))) {
368 /* Set ena and run to non-zero */
369 count
->ena
= count
->run
= 1;
374 * Set default value or 0 when retire_latency for this event is
375 * not found from sampling data (record_tpebs not set or 0
383 * Only set retire_latency value to the first CPU and thread.
385 if (cpu_map_idx
== 0 && thread
== 0)
394 static void tpebs_retire_lat__delete(struct tpebs_retire_lat
*r
)
402 * tpebs_delete - delete tpebs related data and stop the created thread and
403 * process by calling tpebs_stop().
405 * This function is called from evlist_delete() and also from builtin-stat
406 * stat_handle_error(). If tpebs_start() is called from places other then perf
407 * stat, need to ensure tpebs_delete() is also called to safely free mem and
408 * close the data read thread and the forked perf record process.
410 * This function is also called in evsel__close() to be symmetric with
411 * tpebs_start() being called in evsel__open(). We will update this call site
412 * when move tpebs_start() to evlist level.
414 void tpebs_delete(void)
416 struct tpebs_retire_lat
*r
, *rtmp
;
423 list_for_each_entry_safe(r
, rtmp
, &tpebs_results
, nd
) {
424 list_del_init(&r
->nd
);
425 tpebs_retire_lat__delete(r
);