Skip to content

Commit

Permalink
blkalgn: add support
Browse files Browse the repository at this point in the history
Add blkalgn support to trace block I/Os and report Granularity and
Alignment.

Signed-off-by: Daniel Gomez <[email protected]>
  • Loading branch information
dkruces committed Oct 29, 2024
1 parent 0d5dcf4 commit 0fb3ebe
Show file tree
Hide file tree
Showing 4 changed files with 649 additions and 3 deletions.
18 changes: 15 additions & 3 deletions libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ $(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(APPS) $(APP_ALIASES)
all: $(APPS) $(APP_ALIASES) blkalgn

ifeq ($(V),1)
Q =
Expand Down Expand Up @@ -186,12 +186,18 @@ $(APPS): %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) -lelf -lz -o $@

blkalgn: %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) -lelf -lz -ljson-c -lm -o $@

ifeq ($(USE_BLAZESYM),1)
$(patsubst %,$(OUTPUT)/%.o,$(BZ_APPS)): $(OUTPUT)/blazesym.h
endif

$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h

$(patsubst %,$(OUTPUT)/%.o,blkalgn): %.o: %.skel.h

$(OUTPUT)/%.o: %.c $(wildcard %.h) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
Expand All @@ -200,6 +206,10 @@ $(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

$(OUTPUT)/blkalgn.skel.h: $(OUTPUT)/blkalgn.bpf.o | $(OUTPUT) $(BPFTOOL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

$(OUTPUT)/softirqs.bpf.o: BPFCFLAGS = $(BPFCFLAGS_softirqs)

$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(ARCH)/vmlinux.h | $(OUTPUT)
Expand Down Expand Up @@ -238,11 +248,13 @@ $(SIGSNOOP_ALIAS): sigsnoop
$(call msg,SYMLINK,$@)
$(Q)ln -f -s $(APP_PREFIX)$^ $@

install: $(APPS) $(APP_ALIASES)
install: $(APPS) $(APP_ALIASES) install-blkalgn
$(call msg, INSTALL libbpf-tools)
$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bindir)
$(Q)$(foreach app,$(APPS),$(INSTALL) $(app) $(DESTDIR)$(bindir)/$(APP_PREFIX)$(app);)
$(Q)$(foreach alias,$(APP_ALIASES),cp -a $(alias) $(DESTDIR)$(bindir)/$(APP_PREFIX)$(alias);)

install-blkalgn: blkalgn
$(Q)$(INSTALL) blkalgn $(DESTDIR)$(bindir)/$(APP_PREFIX)blkalgn

.PHONY: force
force:
Expand Down
86 changes: 86 additions & 0 deletions libbpf-tools/blkalgn.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Samsung */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "blkalgn.h"
#include "bits.bpf.h"
#include "core_fixes.bpf.h"

const volatile bool filter_dev = false;
const volatile __u32 targ_dev = 0;
const volatile bool filter_ops = false;
const volatile __u32 targ_ops = 0;

extern __u32 LINUX_KERNEL_VERSION __kconfig;

char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 2097152);
} rb SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, struct hkey);
__type(value, struct hval);
} halgn_map SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, struct hkey);
__type(value, struct hval);
} hgran_map SEC(".maps");

static int __always_inline trace_rq_issue(struct request *rq)
{
struct event *e;
u32 dev;

struct gendisk *disk = get_disk(rq);

dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) :
0;

if (filter_dev && targ_dev != dev)
return 0;

if (filter_ops && targ_ops != (rq->cmd_flags & 0xff))
return 0;

e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
if (!e)
return 0;

bpf_get_current_comm(&e->comm, sizeof(e->comm));
bpf_probe_read_kernel(&e->disk, sizeof(e->disk),
rq->q->disk->disk_name);

e->flags = rq->cmd_flags;
e->lbs = rq->q->limits.logical_block_size;
e->len = rq->__data_len;
e->sector = rq->__sector;

bpf_ringbuf_submit(e, 0);

return 0;
}

SEC("tp_btf/block_rq_issue")
int BPF_PROG(block_rq_issue)
{
/*
* Commit a54895fa (v5.11-rc1) changed tracepoint argument list
* from TP_PROTO(struct request_queue *q, struct request *rq)
* to TP_PROTO(struct request *rq)
*/
if (LINUX_KERNEL_VERSION >= KERNEL_VERSION(5, 11, 0))
return trace_rq_issue((void *)ctx[0]);
else
return trace_rq_issue((void *)ctx[1]);
}
Loading

0 comments on commit 0fb3ebe

Please sign in to comment.