tools/perf/util/cgroup.c
Source file repositories/reference/linux-study-clean/tools/perf/util/cgroup.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/cgroup.c- Extension
.c- Size
- 13479 bytes
- Lines
- 647
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
subcmd/parse-options.hevsel.hcgroup.hevlist.hrblist.hmetricgroup.hstat.hlinux/zalloc.hsys/types.hsys/stat.hsys/statfs.herrno.hfcntl.hstdlib.hstring.hapi/fs/fs.hftw.hregex.h
Detected Declarations
struct cgroup_namefunction open_cgroupfunction __read_cgroup_idfunction read_cgroup_idfunction __read_cgroup_idfunction cgroup_is_v2function add_cgroupfunction cgroup__deletefunction cgroup__putfunction evsel__set_default_cgroupfunction evlist__set_default_cgroupfunction add_cgroup_namefunction check_and_add_cgroup_namefunction list_for_each_entryfunction release_cgroup_listfunction list_cgroupsfunction match_cgroupsfunction list_for_each_entryfunction parse_cgroupsfunction evlist__for_each_entryfunction has_pattern_stringfunction evlist__expand_cgroupfunction list_for_each_entryfunction evlist__for_each_entryfunction evlist__for_each_entryfunction perf_env__purge_cgroupsfunction read_all_cgroupsfunction list_for_each_entry
Annotated Snippet
struct cgroup_name {
struct list_head list;
bool used;
char name[];
};
static LIST_HEAD(cgroup_list);
static int open_cgroup(const char *name)
{
char path[PATH_MAX + 1];
char mnt[PATH_MAX + 1];
int fd;
if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
return -1;
scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
fd = open(path, O_RDONLY);
if (fd == -1)
fprintf(stderr, "no access to cgroup %s\n", path);
return fd;
}
#ifdef HAVE_FILE_HANDLE
static u64 __read_cgroup_id(const char *path)
{
struct {
struct file_handle fh;
uint64_t cgroup_id;
} handle;
int mount_id;
handle.fh.handle_bytes = sizeof(handle.cgroup_id);
if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0)
return -1ULL;
return handle.cgroup_id;
}
int read_cgroup_id(struct cgroup *cgrp)
{
char path[PATH_MAX + 1];
char mnt[PATH_MAX + 1];
if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
return -1;
scnprintf(path, PATH_MAX, "%s/%s", mnt, cgrp->name);
cgrp->id = __read_cgroup_id(path);
return 0;
}
#else
static inline u64 __read_cgroup_id(const char *path __maybe_unused) { return -1ULL; }
#endif /* HAVE_FILE_HANDLE */
#ifndef CGROUP2_SUPER_MAGIC
#define CGROUP2_SUPER_MAGIC 0x63677270
#endif
int cgroup_is_v2(const char *subsys)
{
char mnt[PATH_MAX + 1];
struct statfs stbuf;
if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys))
return -1;
if (statfs(mnt, &stbuf) < 0)
return -1;
return (stbuf.f_type == CGROUP2_SUPER_MAGIC);
}
static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
{
struct evsel *counter;
/*
* check if cgrp is already defined, if so we reuse it
*/
evlist__for_each_entry(evlist, counter) {
if (!counter->cgrp)
continue;
if (!strcmp(counter->cgrp->name, str))
return cgroup__get(counter->cgrp);
}
Annotation
- Immediate include surface: `subcmd/parse-options.h`, `evsel.h`, `cgroup.h`, `evlist.h`, `rblist.h`, `metricgroup.h`, `stat.h`, `linux/zalloc.h`.
- Detected declarations: `struct cgroup_name`, `function open_cgroup`, `function __read_cgroup_id`, `function read_cgroup_id`, `function __read_cgroup_id`, `function cgroup_is_v2`, `function add_cgroup`, `function cgroup__delete`, `function cgroup__put`, `function evsel__set_default_cgroup`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.