mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Ingo Molnar <mingo@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Michael Petlan <mpetlan@redhat.com>,
	Ian Rogers <irogers@google.com>,
	Stephane Eranian <eranian@google.com>,
	Alexei Budankov <abudankov@huawei.com>
Subject: [PATCH 5/8] perf daemon: Add signal command
Date: Sat, 12 Dec 2020 11:43:55 +0100	[thread overview]
Message-ID: <20201212104358.412065-6-jolsa@kernel.org> (raw)
In-Reply-To: <20201212104358.412065-1-jolsa@kernel.org>

Allow perf daemon to send SIGUSR2 to all running sessions:

  # perf daemon
  [1:364758] perf record -m 10M -e cycles -o /opt/perfdata/1/perf.data --overwrite --switch-output -a
  [2:364759] perf record -m 10M -e sched:* -o /opt/perfdata/2/perf.data --overwrite --switch-output -a

  # perf daemon -s
  signal 12 sent to session '1 [92187]'
  signal 12 sent to session '2 [92188]'

Or to specific one:

  # perf daemon --signal=1
  signal 12 sent to session '1 [364758]'

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/perf/Documentation/perf-daemon.txt | 24 +++++++++++
 tools/perf/builtin-daemon.c              | 51 +++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/tools/perf/Documentation/perf-daemon.txt b/tools/perf/Documentation/perf-daemon.txt
index dee39be110ba..203ec4bf704c 100644
--- a/tools/perf/Documentation/perf-daemon.txt
+++ b/tools/perf/Documentation/perf-daemon.txt
@@ -30,6 +30,11 @@ OPTIONS
 --foreground::
 	Do not put the process in background.
 
+-s::
+--signal[=session]::
+	Send SIGUSR2 to specific session, if session is not specified,
+	send SIGUSR2 to all sessions.
+
 -v::
 --verbose::
 	Be more verbose.
@@ -92,6 +97,25 @@ Check sessions with more info:
 The 'output' file is perf record output for specific session.
 
 
+Send SIGUSR2 signal to all sessions:
+
+  # perf daemon -s
+  signal 12 sent to session '1 [92187]'
+  signal 12 sent to session '2 [92188]'
+
+Send SIGUSR2 signal to session '1':
+
+  # perf daemon --signal=1
+  signal 12 sent to session '1 [364758]'
+
+And check that the perf data dump was trigered:
+
+  # cat /opt/perfdata/2/output
+  rounding mmap pages size to 32M (8192 pages)
+  [ perf record: dump data: Woken up 1 times ]
+  [ perf record: Dump /opt/perfdata/2/perf.data.2020120715220385 ]
+
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-config[1]
diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c
index 7f455837d58a..c53d4ddc2b49 100644
--- a/tools/perf/builtin-daemon.c
+++ b/tools/perf/builtin-daemon.c
@@ -450,9 +450,15 @@ static int setup_server_socket(struct daemon *daemon)
 enum cmd {
 	CMD_LIST         = 0,
 	CMD_LIST_VERBOSE = 1,
+	CMD_SIGNAL       = 2,
 	CMD_MAX,
 };
 
+struct cmd_signal {
+	int	sig;
+	char	name[16];
+};
+
 static int cmd_session_list(struct daemon *daemon, FILE *out, bool simple)
 {
 	struct session *session;
@@ -469,6 +475,28 @@ static int cmd_session_list(struct daemon *daemon, FILE *out, bool simple)
 	return 0;
 }
 
+static int cmd_session_kill(struct daemon *daemon, FILE *out, int fd)
+{
+	struct session *session;
+	struct cmd_signal data;
+	bool all = false;
+
+	if (sizeof(data) != read(fd, &data, sizeof(data)))
+		return -1;
+
+	all = !strcmp(data.name, "all");
+
+	list_for_each_entry(session, &daemon->sessions, list) {
+		if (all || !strcmp(data.name, session->name)) {
+			session__signal(session, data.sig);
+			fprintf(out, "signal %d sent to session '%s [%d]'\n",
+				data.sig, session->name, session->pid);
+		}
+	}
+
+	return 0;
+}
+
 static int handle_server_socket(struct daemon *daemon, int sock_fd)
 {
 	int ret = -EINVAL, fd;
@@ -497,6 +525,9 @@ static int handle_server_socket(struct daemon *daemon, int sock_fd)
 	case CMD_LIST_VERBOSE:
 		ret = cmd_session_list(daemon, out, cmd == CMD_LIST);
 		break;
+	case CMD_SIGNAL:
+		ret = cmd_session_kill(daemon, out, fd);
+		break;
 	default:
 		break;
 	}
@@ -730,8 +761,9 @@ static int __cmd_daemon(struct daemon *daemon, bool foreground, const char *conf
 	return err;
 }
 
-static int send_cmd(struct daemon *daemon, u64 cmd)
+static int send_cmd(struct daemon *daemon, u64 cmd, const char *str)
 {
+	struct cmd_signal data;
 	char *line = NULL;
 	size_t len = 0;
 	ssize_t nread;
@@ -747,6 +779,14 @@ static int send_cmd(struct daemon *daemon, u64 cmd)
 	if (sizeof(cmd) != write(fd, &cmd, sizeof(cmd)))
 		return -1;
 
+	if (cmd == CMD_SIGNAL) {
+		data.sig = SIGUSR2;
+		strncpy(data.name, str, sizeof(data.name) - 1);
+
+		if (sizeof(data) != write(fd, &data, sizeof(data)))
+			return -1;
+	}
+
 	in = fdopen(fd, "r");
 	if (!in) {
 		perror("fopen");
@@ -770,7 +810,9 @@ static const char * const daemon_usage[] = {
 int cmd_daemon(int argc, const char **argv)
 {
 	bool foreground = false;
+	bool signal = false;
 	const char *config = NULL;
+	const char *signal_str = NULL;
 	struct daemon daemon = {
 		.sessions = LIST_HEAD_INIT(daemon.sessions),
 		.out	  = stdout,
@@ -780,6 +822,8 @@ int cmd_daemon(int argc, const char **argv)
 		OPT_STRING(0, "config", &config,
 			   "config file", "config file path"),
 		OPT_BOOLEAN('f', "foreground", &foreground, "stay on console"),
+		OPT_STRING_OPTARG_SET('s', "signal", &signal_str, &signal,
+				      "signal", "send signal to session", "all"),
 		OPT_END()
 	};
 
@@ -790,5 +834,8 @@ int cmd_daemon(int argc, const char **argv)
 	if (config)
 		return __cmd_daemon(&daemon, foreground, config);
 
-	return send_cmd(&daemon, verbose ? CMD_LIST_VERBOSE : CMD_LIST);
+	if (signal)
+		return send_cmd(&daemon, CMD_SIGNAL, signal_str);
+
+	return send_cmd(&daemon, verbose ? CMD_LIST_VERBOSE : CMD_LIST, NULL);
 }
-- 
2.26.2


  parent reply	other threads:[~2020-12-12 10:47 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-12 10:43 [RFC 0/8] perf tools: Add daemon command Jiri Olsa
2020-12-12 10:43 ` [PATCH 1/8] perf tools: Add debug_set_file function Jiri Olsa
2020-12-15 15:37   ` Arnaldo Carvalho de Melo
2020-12-12 10:43 ` [PATCH 2/8] perf tools: Add debug_set_display_time function Jiri Olsa
2020-12-15 15:37   ` Arnaldo Carvalho de Melo
2020-12-12 10:43 ` [PATCH 3/8] perf tools: Add config set interface Jiri Olsa
2020-12-15 15:41   ` Arnaldo Carvalho de Melo
2020-12-15 19:11     ` Jiri Olsa
2020-12-12 10:43 ` [PATCH 4/8] perf daemon: Add daemon command Jiri Olsa
2020-12-15 15:40   ` Alexei Budankov
2020-12-15 19:43     ` Jiri Olsa
2020-12-16  7:54       ` Alexei Budankov
2020-12-16  8:14         ` Jiri Olsa
2020-12-18 13:25       ` Namhyung Kim
2020-12-18 19:30         ` Jiri Olsa
2020-12-15 15:44   ` Arnaldo Carvalho de Melo
2020-12-15 19:20     ` Jiri Olsa
2020-12-12 10:43 ` Jiri Olsa [this message]
2020-12-15 15:45   ` [PATCH 5/8] perf daemon: Add signal command Arnaldo Carvalho de Melo
2020-12-15 19:14     ` Jiri Olsa
2020-12-12 10:43 ` [PATCH 6/8] perf daemon: Add stop command Jiri Olsa
2020-12-15 15:45   ` Arnaldo Carvalho de Melo
2020-12-12 10:43 ` [PATCH 7/8] perf daemon: Allow only one daemon over base directory Jiri Olsa
2020-12-15 15:46   ` Arnaldo Carvalho de Melo
2020-12-15 19:16     ` Jiri Olsa
2020-12-12 10:43 ` [PATCH 8/8] perf daemon: Set control fifo for session Jiri Olsa
2020-12-15 15:47   ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201212104358.412065-6-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=abudankov@huawei.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@kernel.org \
    --cc=mpetlan@redhat.com \
    --cc=namhyung@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome