Commit 30626e08 authored by Namhyung Kim's avatar Namhyung Kim Committed by Arnaldo Carvalho de Melo

perf tools: Use /proc/<PID>/task/<TID>/status for PERF_RECORD_ event synthesis

To save memory usage, it needs to reduce the number of entries in the
proc filesystem.  It's using /proc/<PID>/task directory to traverse
threads in the process and then kernel creates /proc/<PID>/task/<TID>
entries.

After that it checks the thread info using the /proc/<TID>/status file
rather than /proc/<PID>/task/<TID>/status.  As far as I can see, they
are the same and contain all the info we need.

Using the latter eliminates the unnecessary /proc/<TID> entry.  This can
be useful especially a large number of threads are used in the system.
In my experiment around 1KB of memory on average was saved for each
thread (which is not a thread group leader).

To do this, pass both pid and tid to perf_event_prepare_comm() if it
knows them.  In case it doesn't know, passing 0 as pid will do the old
way.
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Acked-by: default avatarJiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20210202090118.2008551-2-namhyung@kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent c3a9cdef
...@@ -69,7 +69,7 @@ int perf_tool__process_synth_event(struct perf_tool *tool, ...@@ -69,7 +69,7 @@ int perf_tool__process_synth_event(struct perf_tool *tool,
* Assumes that the first 4095 bytes of /proc/pid/stat contains * Assumes that the first 4095 bytes of /proc/pid/stat contains
* the comm, tgid and ppid. * the comm, tgid and ppid.
*/ */
static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, static int perf_event__get_comm_ids(pid_t pid, pid_t tid, char *comm, size_t len,
pid_t *tgid, pid_t *ppid) pid_t *tgid, pid_t *ppid)
{ {
char bf[4096]; char bf[4096];
...@@ -81,7 +81,10 @@ static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, ...@@ -81,7 +81,10 @@ static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
*tgid = -1; *tgid = -1;
*ppid = -1; *ppid = -1;
snprintf(bf, sizeof(bf), "/proc/%d/status", pid); if (pid)
snprintf(bf, sizeof(bf), "/proc/%d/task/%d/status", pid, tid);
else
snprintf(bf, sizeof(bf), "/proc/%d/status", tid);
fd = open(bf, O_RDONLY); fd = open(bf, O_RDONLY);
if (fd < 0) { if (fd < 0) {
...@@ -93,7 +96,7 @@ static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, ...@@ -93,7 +96,7 @@ static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
close(fd); close(fd);
if (n <= 0) { if (n <= 0) {
pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n", pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
pid); tid);
return -1; return -1;
} }
bf[n] = '\0'; bf[n] = '\0';
...@@ -116,27 +119,27 @@ static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, ...@@ -116,27 +119,27 @@ static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
memcpy(comm, name, size); memcpy(comm, name, size);
comm[size] = '\0'; comm[size] = '\0';
} else { } else {
pr_debug("Name: string not found for pid %d\n", pid); pr_debug("Name: string not found for pid %d\n", tid);
} }
if (tgids) { if (tgids) {
tgids += 5; /* strlen("Tgid:") */ tgids += 5; /* strlen("Tgid:") */
*tgid = atoi(tgids); *tgid = atoi(tgids);
} else { } else {
pr_debug("Tgid: string not found for pid %d\n", pid); pr_debug("Tgid: string not found for pid %d\n", tid);
} }
if (ppids) { if (ppids) {
ppids += 5; /* strlen("PPid:") */ ppids += 5; /* strlen("PPid:") */
*ppid = atoi(ppids); *ppid = atoi(ppids);
} else { } else {
pr_debug("PPid: string not found for pid %d\n", pid); pr_debug("PPid: string not found for pid %d\n", tid);
} }
return 0; return 0;
} }
static int perf_event__prepare_comm(union perf_event *event, pid_t pid, static int perf_event__prepare_comm(union perf_event *event, pid_t pid, pid_t tid,
struct machine *machine, struct machine *machine,
pid_t *tgid, pid_t *ppid) pid_t *tgid, pid_t *ppid)
{ {
...@@ -147,7 +150,7 @@ static int perf_event__prepare_comm(union perf_event *event, pid_t pid, ...@@ -147,7 +150,7 @@ static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
memset(&event->comm, 0, sizeof(event->comm)); memset(&event->comm, 0, sizeof(event->comm));
if (machine__is_host(machine)) { if (machine__is_host(machine)) {
if (perf_event__get_comm_ids(pid, event->comm.comm, if (perf_event__get_comm_ids(pid, tid, event->comm.comm,
sizeof(event->comm.comm), sizeof(event->comm.comm),
tgid, ppid) != 0) { tgid, ppid) != 0) {
return -1; return -1;
...@@ -168,7 +171,7 @@ static int perf_event__prepare_comm(union perf_event *event, pid_t pid, ...@@ -168,7 +171,7 @@ static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
event->comm.header.size = (sizeof(event->comm) - event->comm.header.size = (sizeof(event->comm) -
(sizeof(event->comm.comm) - size) + (sizeof(event->comm.comm) - size) +
machine->id_hdr_size); machine->id_hdr_size);
event->comm.tid = pid; event->comm.tid = tid;
return 0; return 0;
} }
...@@ -180,7 +183,7 @@ pid_t perf_event__synthesize_comm(struct perf_tool *tool, ...@@ -180,7 +183,7 @@ pid_t perf_event__synthesize_comm(struct perf_tool *tool,
{ {
pid_t tgid, ppid; pid_t tgid, ppid;
if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0) if (perf_event__prepare_comm(event, 0, pid, machine, &tgid, &ppid) != 0)
return -1; return -1;
if (perf_tool__process_synth_event(tool, event, machine, process) != 0) if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
...@@ -746,7 +749,7 @@ static int __event__synthesize_thread(union perf_event *comm_event, ...@@ -746,7 +749,7 @@ static int __event__synthesize_thread(union perf_event *comm_event,
continue; continue;
rc = -1; rc = -1;
if (perf_event__prepare_comm(comm_event, _pid, machine, if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
&tgid, &ppid) != 0) &tgid, &ppid) != 0)
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment