Skip to content

Commit f33cbe7

Browse files
committed
perf evlist: Send the errno in the signal when workload fails
When a tool uses perf_evlist__start_workload and the supplied workload fails (e.g.: its binary wasn't found), perror was being used to print the error reason. This is undesirable, as the caller may be a GUI, when it wants to have total control of the error reporting process. So move to using sigaction(SA_SIGINFO) + siginfo_t->sa_value->sival_int to communicate to the caller the errno and let it print it using the UI of its choosing. Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/n/tip-epgcv7kjq8ll2udqfken92pz@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 6af206f commit f33cbe7

3 files changed

Lines changed: 60 additions & 11 deletions

File tree

‎tools/perf/builtin-record.c‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ static void record__init_features(struct record *rec)
341341
perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
342342
}
343343

344+
static volatile int workload_exec_errno;
345+
346+
/*
347+
* perf_evlist__prepare_workload will send a SIGUSR1
348+
* if the fork fails, since we asked by setting its
349+
* want_signal to true.
350+
*/
351+
static void workload_exec_failed_signal(int signo, siginfo_t *info,
352+
void *ucontext __maybe_unused)
353+
{
354+
workload_exec_errno = info->si_value.sival_int;
355+
done = 1;
356+
signr = signo;
357+
child_finished = 1;
358+
}
359+
344360
static int __cmd_record(struct record *rec, int argc, const char **argv)
345361
{
346362
int err;
@@ -359,7 +375,6 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
359375
on_exit(record__sig_exit, rec);
360376
signal(SIGCHLD, sig_handler);
361377
signal(SIGINT, sig_handler);
362-
signal(SIGUSR1, sig_handler);
363378
signal(SIGTERM, sig_handler);
364379

365380
session = perf_session__new(file, false, NULL);
@@ -492,8 +507,20 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
492507
/*
493508
* Let the child rip
494509
*/
495-
if (forks)
510+
if (forks) {
511+
struct sigaction act = {
512+
.sa_flags = SA_SIGINFO,
513+
.sa_sigaction = workload_exec_failed_signal,
514+
};
515+
/*
516+
* perf_evlist__prepare_workload will, after we call
517+
* perf_evlist__start_Workload, send a SIGUSR1 if the exec call
518+
* fails, that we will catch in workload_signal to flip
519+
* workload_exec_errno.
520+
*/
521+
sigaction(SIGUSR1, &act, NULL);
496522
perf_evlist__start_workload(evsel_list);
523+
}
497524

498525
for (;;) {
499526
int hits = rec->samples;
@@ -521,6 +548,14 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
521548
}
522549
}
523550

551+
if (forks && workload_exec_errno) {
552+
char msg[512];
553+
const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
554+
pr_err("Workload failed: %s\n", emsg);
555+
err = -1;
556+
goto out_delete_session;
557+
}
558+
524559
if (quiet || signr == SIGUSR1)
525560
return 0;
526561

‎tools/perf/builtin-stat.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "util/thread.h"
5959
#include "util/thread_map.h"
6060

61+
#include <signal.h>
6162
#include <stdlib.h>
6263
#include <sys/prctl.h>
6364
#include <locale.h>
@@ -509,16 +510,17 @@ static void handle_initial_delay(void)
509510
}
510511
}
511512

512-
static volatile bool workload_exec_failed;
513+
static volatile int workload_exec_errno;
513514

514515
/*
515516
* perf_evlist__prepare_workload will send a SIGUSR1
516517
* if the fork fails, since we asked by setting its
517518
* want_signal to true.
518519
*/
519-
static void workload_exec_failed_signal(int signo __maybe_unused)
520+
static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
521+
void *ucontext __maybe_unused)
520522
{
521-
workload_exec_failed = true;
523+
workload_exec_errno = info->si_value.sival_int;
522524
}
523525

524526
static int __run_perf_stat(int argc, const char **argv)
@@ -596,13 +598,17 @@ static int __run_perf_stat(int argc, const char **argv)
596598
clock_gettime(CLOCK_MONOTONIC, &ref_time);
597599

598600
if (forks) {
601+
struct sigaction act = {
602+
.sa_flags = SA_SIGINFO,
603+
.sa_sigaction = workload_exec_failed_signal,
604+
};
599605
/*
600606
* perf_evlist__prepare_workload will, after we call
601607
* perf_evlist__start_Workload, send a SIGUSR1 if the exec call
602608
* fails, that we will catch in workload_signal to flip
603-
* workload_exec_failed.
609+
* workload_exec_errno.
604610
*/
605-
signal(SIGUSR1, workload_exec_failed_signal);
611+
sigaction(SIGUSR1, &act, NULL);
606612

607613
perf_evlist__start_workload(evsel_list);
608614
handle_initial_delay();
@@ -615,8 +621,11 @@ static int __run_perf_stat(int argc, const char **argv)
615621
}
616622
wait(&status);
617623

618-
if (workload_exec_failed)
624+
if (workload_exec_errno) {
625+
const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
626+
pr_err("Workload failed: %s\n", emsg);
619627
return -1;
628+
}
620629

621630
if (WIFSIGNALED(status))
622631
psignal(WTERMSIG(status), argv[0]);

‎tools/perf/util/evlist.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,14 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *tar
10731073

10741074
execvp(argv[0], (char **)argv);
10751075

1076-
perror(argv[0]);
1077-
if (want_signal)
1078-
kill(getppid(), SIGUSR1);
1076+
if (want_signal) {
1077+
union sigval val;
1078+
1079+
val.sival_int = errno;
1080+
if (sigqueue(getppid(), SIGUSR1, val))
1081+
perror(argv[0]);
1082+
} else
1083+
perror(argv[0]);
10791084
exit(-1);
10801085
}
10811086

0 commit comments

Comments
 (0)