extern int LINUX_KERNEL_VERSION __kconfig;
+const volatile bool filter_cg = false;
const volatile bool targ_per_disk = false;
const volatile bool targ_per_flag = false;
const volatile bool targ_queued = false;
const volatile bool targ_ms = false;
const volatile dev_t targ_dev = -1;
+struct {
+ __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
+ __type(key, u32);
+ __type(value, u32);
+ __uint(max_entries, 1);
+} cgroup_map SEC(".maps");
+
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, MAX_ENTRIES);
SEC("tp_btf/block_rq_insert")
int block_rq_insert(u64 *ctx)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
/**
* commit a54895fa (v5.11-rc1) changed tracepoint argument list
* from TP_PROTO(struct request_queue *q, struct request *rq)
SEC("tp_btf/block_rq_issue")
int block_rq_issue(u64 *ctx)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
/**
* commit a54895fa (v5.11-rc1) changed tracepoint argument list
* from TP_PROTO(struct request_queue *q, struct request *rq)
int BPF_PROG(block_rq_complete, struct request *rq, int error,
unsigned int nr_bytes)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
u64 slot, *tsp, ts = bpf_ktime_get_ns();
struct hist_key hkey = {};
struct hist *histp;
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
+#include <fcntl.h>
#include <time.h>
#include <bpf/libbpf.h>
#include <sys/resource.h>
bool per_flag;
bool milliseconds;
bool verbose;
+ char *cgroupspath;
+ bool cg;
} env = {
.interval = 99999999,
.times = 99999999,
const char argp_program_doc[] =
"Summarize block device I/O latency as a histogram.\n"
"\n"
-"USAGE: biolatency [--help] [-T] [-m] [-Q] [-D] [-F] [-d DISK] [interval] [count]\n"
+"USAGE: biolatency [--help] [-T] [-m] [-Q] [-D] [-F] [-d DISK] [-c CG] [interval] [count]\n"
"\n"
"EXAMPLES:\n"
" biolatency # summarize block I/O latency as a histogram\n"
" biolatency -Q # include OS queued time in I/O time\n"
" biolatency -D # show each disk device separately\n"
" biolatency -F # show I/O flags separately\n"
-" biolatency -d sdc # Trace sdc only\n";
+" biolatency -d sdc # Trace sdc only\n"
+" biolatency -c CG # Trace process under cgroupsPath CG\n";
static const struct argp_option opts[] = {
{ "timestamp", 'T', NULL, 0, "Include timestamp on output" },
{ "flag", 'F', NULL, 0, "Print a histogram per set of I/O flags" },
{ "disk", 'd', "DISK", 0, "Trace this disk only" },
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
+ { "cgroup", 'c', "/sys/fs/cgroup/unified", 0, "Trace process in cgroup path"},
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
{},
};
case 'T':
env.timestamp = true;
break;
+ case 'c':
+ env.cgroupspath = arg;
+ env.cg = true;
+ break;
case 'd':
env.disk = arg;
if (strlen(arg) + 1 > DISK_NAME_LEN) {
char ts[32];
time_t t;
int err;
+ int idx, cg_map_fd;
+ int cgfd = -1;
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
obj->rodata->targ_per_flag = env.per_flag;
obj->rodata->targ_ms = env.milliseconds;
obj->rodata->targ_queued = env.queued;
+ obj->rodata->filter_cg = env.cg;
err = biolatency_bpf__load(obj);
if (err) {
goto cleanup;
}
+ /* update cgroup path fd to map */
+ if (env.cg) {
+ idx = 0;
+ cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);
+ cgfd = open(env.cgroupspath, O_RDONLY);
+ if (cgfd < 0) {
+ fprintf(stderr, "Failed opening Cgroup path: %s", env.cgroupspath);
+ goto cleanup;
+ }
+ if (bpf_map_update_elem(cg_map_fd, &idx, &cgfd, BPF_ANY)) {
+ fprintf(stderr, "Failed adding target cgroup to map");
+ goto cleanup;
+ }
+ }
+
if (env.queued) {
obj->links.block_rq_insert =
bpf_program__attach(obj->progs.block_rq_insert);
cleanup:
biolatency_bpf__destroy(obj);
partitions__free(partitions);
+ if (cgfd > 0)
+ close(cgfd);
return err != 0;
}
#define MAX_ENTRIES 10240
+const volatile bool filter_cg = false;
const volatile bool targ_queued = false;
const volatile dev_t targ_dev = -1;
extern __u32 LINUX_KERNEL_VERSION __kconfig;
+struct {
+ __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
+ __type(key, u32);
+ __type(value, u32);
+ __uint(max_entries, 1);
+} cgroup_map SEC(".maps");
+
struct piddata {
char comm[TASK_COMM_LEN];
u32 pid;
SEC("fentry/blk_account_io_start")
int BPF_PROG(blk_account_io_start, struct request *rq)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
return trace_pid(rq);
}
SEC("kprobe/blk_account_io_merge_bio")
int BPF_KPROBE(blk_account_io_merge_bio, struct request *rq)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
return trace_pid(rq);
}
SEC("tp_btf/block_rq_insert")
int BPF_PROG(block_rq_insert)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
/**
* commit a54895fa (v5.11-rc1) changed tracepoint argument list
* from TP_PROTO(struct request_queue *q, struct request *rq)
SEC("tp_btf/block_rq_issue")
int BPF_PROG(block_rq_issue)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
/**
* commit a54895fa (v5.11-rc1) changed tracepoint argument list
* from TP_PROTO(struct request_queue *q, struct request *rq)
int BPF_PROG(block_rq_complete, struct request *rq, int error,
unsigned int nr_bytes)
{
+ if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
+ return 0;
+
u64 ts = bpf_ktime_get_ns();
struct piddata *piddatap;
struct event event = {};
#include <bpf/libbpf.h>
#include <sys/resource.h>
#include <bpf/bpf.h>
+#include <fcntl.h>
#include "blk_types.h"
#include "biosnoop.h"
#include "biosnoop.skel.h"
bool timestamp;
bool queued;
bool verbose;
+ char *cgroupspath;
+ bool cg;
} env = {};
static volatile __u64 start_ts;
const char argp_program_doc[] =
"Trace block I/O.\n"
"\n"
-"USAGE: biosnoop [--help] [-d DISK] [-Q]\n"
+"USAGE: biosnoop [--help] [-d DISK] [-c CG] [-Q]\n"
"\n"
"EXAMPLES:\n"
" biosnoop # trace all block I/O\n"
" biosnoop -Q # include OS queued time in I/O time\n"
" biosnoop 10 # trace for 10 seconds only\n"
-" biosnoop -d sdc # trace sdc only\n";
+" biosnoop -d sdc # trace sdc only\n"
+" biosnoop -c CG # Trace process under cgroupsPath CG\n";
static const struct argp_option opts[] = {
{ "queued", 'Q', NULL, 0, "Include OS queued time in I/O time" },
{ "disk", 'd', "DISK", 0, "Trace this disk only" },
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
+ { "cgroup", 'c', "/sys/fs/cgroup/unified/CG", 0, "Trace process in cgroup path"},
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
{},
};
case 'Q':
env.queued = true;
break;
+ case 'c':
+ env.cg = true;
+ env.cgroupspath = arg;
+ break;
case 'd':
env.disk = arg;
if (strlen(arg) + 1 > DISK_NAME_LEN) {
struct biosnoop_bpf *obj;
__u64 time_end = 0;
int err;
+ int idx, cg_map_fd;
+ int cgfd = -1;
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
}
}
obj->rodata->targ_queued = env.queued;
+ obj->rodata->filter_cg = env.cg;
err = biosnoop_bpf__load(obj);
if (err) {
goto cleanup;
}
+ /* update cgroup path fd to map */
+ if (env.cg) {
+ idx = 0;
+ cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);
+ cgfd = open(env.cgroupspath, O_RDONLY);
+ if (cgfd < 0) {
+ fprintf(stderr, "Failed opening Cgroup path: %s\n", env.cgroupspath);
+ goto cleanup;
+ }
+ if (bpf_map_update_elem(cg_map_fd, &idx, &cgfd, BPF_ANY)) {
+ fprintf(stderr, "Failed adding target cgroup to map\n");
+ goto cleanup;
+ }
+ }
+
obj->links.blk_account_io_start =
bpf_program__attach(obj->progs.blk_account_io_start);
err = libbpf_get_error(obj->links.blk_account_io_start);
biosnoop_bpf__destroy(obj);
ksyms__free(ksyms);
partitions__free(partitions);
+ if (cgfd > 0)
+ close(cgfd);
return err != 0;
}