Skip to content

Commit

Permalink
Minor?
Browse files Browse the repository at this point in the history
  • Loading branch information
Sberm committed Mar 22, 2024
1 parent 28ae577 commit 7c2d199
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
# \_ sberf

# bpf.c文件
BPF_FILE_ := record stat
BPF_FILE_ := record stat mem
BPF_FILE := $(addsuffix .bpf.c, $(BPF_FILE_))
SKEL := $(patsubst %.bpf.c, %.skel.h,$(BPF_FILE))
SKEL_BUILT := $(addprefix $(SKEL_DIR)/,$(SKEL))
Expand Down
74 changes: 74 additions & 0 deletions src/mem.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*-*- coding:utf-8 -*-│
│vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2024 Howard Chu │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/

#include "vmlinux.h"
#include "mem.h"
#include "bpf_util.h"
#include "util.h"

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

// specific pid
volatile bool spec_pid = 0;
volatile pid_t pids[MAX_PID] = {0};

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
__type(key, u32);
} stack_map SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
__type(value, u32);
__uint(max_entries, MAX_ENTRIES);
} mem_usage SEC(".maps");

SEC("kprobe")
int mem_profile(void *ctx)
{
u64 id = bpf_get_current_pid_tgid();
u32 pid = id >> 32;

// if to trace only specific pids
if (spec_pid) {
int i;
for (i = 0;i < ARRAY_LEN(pids); i++) {
if (pids[i] == 0)
return 0;
if (pids[i] == pid)
break;
}
/* didn't match through the whole pids array */
if (i == ARRAY_LEN(pids))
return 0;
}

bpf_printk("mem triggered");
/*u64 zero = 0;*/
// u64* cnt = bpf_map_lookup_insert(&stat_cnt, &zero, &zero);

/*if (cnt)*/
/*__sync_fetch_and_add(cnt, 1);*/
/*else {*/
/*bpf_printk("Failed to look up stack sample");*/
/*return -1;*/
/*}*/
return 0;
}
26 changes: 26 additions & 0 deletions src/mem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*-*- coding:utf-8 -*-│
│vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2024 Howard Chu │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/

#ifndef MEM_H
#define MEM_H

#define MAX_ENTRIES 10240
#define MAX_PID 1024

#endif
65 changes: 56 additions & 9 deletions src/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "util.h"
#include "record.skel.h"
#include "stat.skel.h"
#include "mem.skel.h"
#include "record.h"
#include "stack.h"
#include "sym.h"
Expand All @@ -61,22 +62,19 @@ static struct {
char event_names_str[512];
} env = {
.freq = 1,
.sample_freq = 3999,
.sample_freq = 69,
.no_plot = 0,
.pids = "\0",
.all_p = 0,
.svg_file_name = "debug.svg",
.event_names_str = "\0",
};

int record_syscall(int argc, char** argv, int index);
int record_tracepoint(int argc, char** argv, int index);
int record_pid(int argc, char** argv, int index);

static struct func_struct record_func[] = {
{"-s", record_syscall},
{"-t", record_tracepoint},
{"-p", record_pid},
{"-m", record_mem},
};

static struct env_struct record_env[] = {
Expand All @@ -91,6 +89,15 @@ static struct env_struct event_env[] = {
{"-p", 1, &env.pids},
{"-s", 1, &env.event_names_str},
{"-t", 1, &env.event_names_str},
{"-rt", 1, &env.event_names_str},
};

static struct env_struct mem_env[] = {
{"-f", 0, &env.sample_freq},
{"-np", 4, &env.no_plot},
{"-a", 4, &env.all_p},
{"-p", 1, &env.pids},
{"-fn", 1, &env.svg_file_name},
};

static void signalHandler(int signum)
Expand Down Expand Up @@ -166,7 +173,7 @@ void print_stack(struct bpf_map *stack_map, struct bpf_map *sample)
printf("Collected %d samples\n", sample_num_total);
}

int split_env_str() {
int split_event_str() {
char *token;
size_t index = 0;
token = strtok(env.event_names_str, ",");
Expand Down Expand Up @@ -241,7 +248,7 @@ int record_syscall(int argc, char** argv, int cur)

parse_opts_env(argc, argv, cur, event_env, ARRAY_LEN(event_env));

int event_num = split_env_str();
int event_num = split_event_str();

printf("recording events: ");
for (int i = 0;i < event_num; i++)
Expand Down Expand Up @@ -289,7 +296,7 @@ int record_tracepoint(int argc, char** argv, int cur)

parse_opts_env(argc, argv, cur, event_env, ARRAY_LEN(event_env));

int event_num = split_env_str();
int event_num = split_event_str();

printf("recording events: ");
for (int i = 0;i < event_num; i++)
Expand Down Expand Up @@ -353,7 +360,6 @@ int record_pid(int argc, char** argv, int cur)
pid_t *pids = skel->bss->pids;
size_t num_of_pids = split(env.pids, pids);
skel->bss->spec_pid = !env.all_p; // if to record all process(all_p = 1), specific pid(spec_pid) is 0


/* sberf record 1001 is also legal */
if (!env.all_p && strlen(env.pids) == 0) {
Expand Down Expand Up @@ -458,3 +464,44 @@ int record_pid(int argc, char** argv, int cur)
return err;
}

int record_mem(int argc, char** argv, int cur)
{
struct mem_bpf *skel;
int err = 0;

parse_opts_env(argc, argv, cur, mem_env, ARRAY_LEN(record_env));

skel = mem_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open and load record's BPF skeleton\n");
return 1;
}

/* pids to trace */
pid_t *pids = skel->bss->pids;
size_t num_of_pids = split(env.pids, pids);
skel->bss->spec_pid = !env.all_p; // if to record all process(all_p = 1), specific pid(spec_pid) is 0

struct bpf_link *link = NULL;
link = bpf_program__attach_ksyscall(skel->progs.mem_profile, "mmap", NULL);

err = mem_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}

err = mem_bpf__attach(skel);
if (err) {
fprintf(stderr, "Failed to attach BPF skeleton\n");
goto cleanup;
}


sleep(100);

cleanup:
mem_bpf__destroy(skel);
return err;
return 0;
}
5 changes: 5 additions & 0 deletions src/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ struct key_t {
char comm[TASK_COMM_LEN];
};

int record_syscall(int argc, char** argv, int index);
int record_tracepoint(int argc, char** argv, int index);
int record_pid(int argc, char** argv, int index);
int record_mem(int argc, char** argv, int cur);

#endif
1 change: 1 addition & 0 deletions src/stat.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ int stat_tracepoint(void *ctx)
SEC("ksyscall")
int stat_ksyscall(void *ctx)
{
bpf_printk("triggered");
u64 zero = 0;
u64* cnt = bpf_map_lookup_insert(&stat_cnt, &zero, &zero);
if (cnt)
Expand Down

0 comments on commit 7c2d199

Please sign in to comment.