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

perf record: Support synthesizing cgroup events

Synthesize cgroup events by iterating cgroup filesystem directories.
The cgroup event only saves the portion of cgroup path after the mount
point and the cgroup id (which actually is a file handle).
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Tested-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20200325124536.2800725-7-namhyung@kernel.org
Link: http://lore.kernel.org/lkml/20200402015249.3800462-1-namhyung@kernel.org
[ Extracted the HAVE_FILE_HANDLE from the followup patch, added missing __maybe_unused ]
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent b629f3e9
...@@ -1397,6 +1397,11 @@ static int record__synthesize(struct record *rec, bool tail) ...@@ -1397,6 +1397,11 @@ static int record__synthesize(struct record *rec, bool tail)
if (err < 0) if (err < 0)
pr_warning("Couldn't synthesize bpf events.\n"); pr_warning("Couldn't synthesize bpf events.\n");
err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
machine);
if (err < 0)
pr_warning("Couldn't synthesize cgroup events.\n");
err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads, err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
process_synthesized_event, opts->sample_address, process_synthesized_event, opts->sample_address,
1); 1);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "util/synthetic-events.h" #include "util/synthetic-events.h"
#include "util/target.h" #include "util/target.h"
#include "util/time-utils.h" #include "util/time-utils.h"
#include "util/cgroup.h"
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/string.h> #include <linux/string.h>
...@@ -414,6 +415,127 @@ int perf_event__synthesize_mmap_events(struct perf_tool *tool, ...@@ -414,6 +415,127 @@ int perf_event__synthesize_mmap_events(struct perf_tool *tool,
return rc; return rc;
} }
#ifdef HAVE_FILE_HANDLE
static int perf_event__synthesize_cgroup(struct perf_tool *tool,
union perf_event *event,
char *path, size_t mount_len,
perf_event__handler_t process,
struct machine *machine)
{
size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
size_t path_len = strlen(path) - mount_len + 1;
struct {
struct file_handle fh;
uint64_t cgroup_id;
} handle;
int mount_id;
while (path_len % sizeof(u64))
path[mount_len + path_len++] = '\0';
memset(&event->cgroup, 0, event_size);
event->cgroup.header.type = PERF_RECORD_CGROUP;
event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
handle.fh.handle_bytes = sizeof(handle.cgroup_id);
if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
pr_debug("stat failed: %s\n", path);
return -1;
}
event->cgroup.id = handle.cgroup_id;
strncpy(event->cgroup.path, path + mount_len, path_len);
memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
pr_debug("process synth event failed\n");
return -1;
}
return 0;
}
static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
union perf_event *event,
char *path, size_t mount_len,
perf_event__handler_t process,
struct machine *machine)
{
size_t pos = strlen(path);
DIR *d;
struct dirent *dent;
int ret = 0;
if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
process, machine) < 0)
return -1;
d = opendir(path);
if (d == NULL) {
pr_debug("failed to open directory: %s\n", path);
return -1;
}
while ((dent = readdir(d)) != NULL) {
if (dent->d_type != DT_DIR)
continue;
if (!strcmp(dent->d_name, ".") ||
!strcmp(dent->d_name, ".."))
continue;
/* any sane path should be less than PATH_MAX */
if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
continue;
if (path[pos - 1] != '/')
strcat(path, "/");
strcat(path, dent->d_name);
ret = perf_event__walk_cgroup_tree(tool, event, path,
mount_len, process, machine);
if (ret < 0)
break;
path[pos] = '\0';
}
closedir(d);
return ret;
}
int perf_event__synthesize_cgroups(struct perf_tool *tool,
perf_event__handler_t process,
struct machine *machine)
{
union perf_event event;
char cgrp_root[PATH_MAX];
size_t mount_len; /* length of mount point in the path */
if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
pr_debug("cannot find cgroup mount point\n");
return -1;
}
mount_len = strlen(cgrp_root);
/* make sure the path starts with a slash (after mount point) */
strcat(cgrp_root, "/");
if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
process, machine) < 0)
return -1;
return 0;
}
#else
int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
perf_event__handler_t process __maybe_unused,
struct machine *machine __maybe_unused)
{
return -1;
}
#endif
int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process, int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
struct machine *machine) struct machine *machine)
{ {
......
...@@ -45,6 +45,7 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, perf_event__handl ...@@ -45,6 +45,7 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, perf_event__handl
int perf_event__synthesize_mmap_events(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid, perf_event__handler_t process, struct machine *machine, bool mmap_data); int perf_event__synthesize_mmap_events(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid, perf_event__handler_t process, struct machine *machine, bool mmap_data);
int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process, struct machine *machine); int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process, struct machine *machine);
int perf_event__synthesize_namespaces(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid, perf_event__handler_t process, struct machine *machine); int perf_event__synthesize_namespaces(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid, perf_event__handler_t process, struct machine *machine);
int perf_event__synthesize_cgroups(struct perf_tool *tool, perf_event__handler_t process, struct machine *machine);
int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format, const struct perf_sample *sample); int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format, const struct perf_sample *sample);
int perf_event__synthesize_stat_config(struct perf_tool *tool, struct perf_stat_config *config, perf_event__handler_t process, struct machine *machine); int perf_event__synthesize_stat_config(struct perf_tool *tool, struct perf_stat_config *config, perf_event__handler_t process, struct machine *machine);
int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool, struct evlist *evlist, perf_event__handler_t process, bool attrs); int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool, struct evlist *evlist, perf_event__handler_t process, bool attrs);
......
...@@ -79,6 +79,7 @@ struct perf_tool { ...@@ -79,6 +79,7 @@ struct perf_tool {
bool ordered_events; bool ordered_events;
bool ordering_requires_timestamps; bool ordering_requires_timestamps;
bool namespace_events; bool namespace_events;
bool cgroup_events;
bool no_warn; bool no_warn;
enum show_feature_header show_feat_hdr; enum show_feature_header show_feat_hdr;
}; };
......
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