Skip to content

libbpf-tools/syscount,memleak: Add metric output support #5281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 79 additions & 12 deletions libbpf-tools/biotop.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define warn(...) fprintf(stderr, __VA_ARGS__)
#define OUTPUT_ROWS_LIMIT 10240

#define OPT_OUTPUT 1 /* --output */

enum SORT {
ALL,
IO,
Expand All @@ -50,6 +52,11 @@ struct vector {
void **elems;
};

struct data_t {
struct info_t key;
struct val_t value;
};

int grow_vector(struct vector *vector) {
if (vector->nr >= vector->capacity) {
void **reallocated;
Expand Down Expand Up @@ -87,6 +94,8 @@ static int interval = 1;
static int count = 99999999;
static pid_t target_pid = 0;
static bool verbose = false;
enum output_format output = 0;
static struct data_t datas[OUTPUT_ROWS_LIMIT];

const char *argp_program_version = "biotop 0.1";
const char *argp_program_bug_address =
Expand All @@ -107,6 +116,7 @@ static const struct argp_option opts[] = {
{ "rows", 'r', "ROWS", 0, "Maximum rows to print, default 20", 0 },
{ "pid", 'p', "PID", 0, "Process ID to trace", 0 },
{ "verbose", 'v', NULL, 0, "Verbose debug output", 0 },
{ "output", OPT_OUTPUT, "FORMAT", OPTION_ARG_OPTIONAL, "Output metrics in specified format (currently only 'line' supported)", 0 },
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help", 0 },
{},
};
Expand Down Expand Up @@ -160,6 +170,16 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
case 'h':
argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
break;
case OPT_OUTPUT:
output = FORMAT_LINE_PROTOCOL;
if (arg) {
if (strcmp(arg, "line") == 0)
output = FORMAT_LINE_PROTOCOL;
else
argp_error(state, "Invalid output format: %s. "
"Only 'line' is supported.", arg);
}
break;
case ARGP_KEY_ARG:
errno = 0;
if (pos_args == 0) {
Expand Down Expand Up @@ -198,11 +218,6 @@ static void sig_int(int signo)
exiting = 1;
}

struct data_t {
struct info_t key;
struct val_t value;
};

static int sort_column(const void *obj1, const void *obj2)
{
struct data_t *d1 = (struct data_t *) obj1;
Expand Down Expand Up @@ -299,13 +314,58 @@ static int read_stat(struct biotop_bpf *obj, struct data_t *datas, __u32 *count)
return 0;
}

static int print_metrics(struct biotop_bpf *obj)
{
int i, err = 0, rows = OUTPUT_ROWS_LIMIT;
time_t ts = time(NULL);
struct metric m = {
.name = "biotop",
.tags = {{ "pid", "" }},
.nr_tags = 1,
.fields = {
{ "I/O", 0 },
{ "Kbytes", 0},
{ "AVGms", 0}
},
.nr_fields = 3,
.ts = ts
};

err = read_stat(obj, datas, (__u32*) &rows);
if (err) {
fprintf(stderr, "read stat failed: %s\n", strerror(errno));
return err;
}

for (i = 0; i < rows; i++) {
struct info_t *key = &datas[i].key;
struct val_t *value = &datas[i].value;
float avg_ms = 0;

/* Tag */
snprintf(m.tags[0].value, sizeof(m.tags[0].value), "%u", key->pid);

/* To avoid floating point exception. */
if (value->io)
avg_ms = ((float) value->us) / 1000 / value->io;

/* Fields */
m.fields[0].value = value->io;
m.fields[1].value = value->bytes;
m.fields[2].value = avg_ms;

print_metric(&m, output);
}

return 0;
}

static int print_stat(struct biotop_bpf *obj)
{
FILE *f;
time_t t;
struct tm *tm;
char ts[16], buf[256];
static struct data_t datas[OUTPUT_ROWS_LIMIT];
int n, i, err = 0, rows = OUTPUT_ROWS_LIMIT;

f = fopen("/proc/loadavg", "r");
Expand All @@ -319,6 +379,7 @@ static int print_stat(struct biotop_bpf *obj)
printf("%8s loadavg: %s\n", ts, buf);
fclose(f);
}

printf("%-7s %-16s %1s %-3s %-3s %-8s %5s %7s %6s\n",
"PID", "COMM", "D", "MAJ", "MIN", "DISK", "I/O", "Kbytes", "AVGms");

Expand Down Expand Up @@ -448,15 +509,21 @@ int main(int argc, char **argv)
while (1) {
sleep(interval);

if (clear_screen) {
err = system("clear");
if (output) {
err = print_metrics(obj);
if (err)
goto cleanup;
}
} else {
if (clear_screen) {
err = system("clear");
if (err)
goto cleanup;
}

err = print_stat(obj);
if (err)
goto cleanup;
err = print_stat(obj);
if (err)
goto cleanup;
}

count--;
if (exiting || !count)
Expand Down
49 changes: 48 additions & 1 deletion libbpf-tools/memleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static struct env {
bool verbose;
char command[32];
char symbols_prefix[16];
enum output_format output;
} env = {
.interval = 5, // posarg 1
.nr_intervals = -1, // posarg 2
Expand Down Expand Up @@ -90,6 +91,7 @@ struct allocation {

#define OPT_PERF_MAX_STACK_DEPTH 1 /* --perf-max-stack-depth */
#define OPT_STACK_STORAGE_SIZE 2 /* --stack-storage-size */
#define OPT_OUTPUT 3 /* --output */

#define __ATTACH_UPROBE(skel, sym_name, prog_name, is_retprobe) \
do { \
Expand Down Expand Up @@ -223,6 +225,7 @@ static const struct argp_option argp_options[] = {
"the number of unique stack traces that can be stored and displayed (default 10240)", 0 },
{"perf-max-stack-depth", OPT_PERF_MAX_STACK_DEPTH,
"PERF-MAX-STACK-DEPTH", 0, "the limit for both kernel and user stack traces (default 127)", 0 },
{"output", OPT_OUTPUT, "FORMAT", OPTION_ARG_OPTIONAL, "Output metrics in specified format (currently only 'line' supported)", 0 },
{"verbose", 'v', NULL, 0, "verbose debug output", 0 },
{},
};
Expand Down Expand Up @@ -467,7 +470,8 @@ int main(int argc, char *argv[])
}
#endif

print_headers();
if (!env.output)
print_headers();

// main loop
while (!exiting && env.nr_intervals) {
Expand Down Expand Up @@ -603,6 +607,16 @@ error_t argp_parse_arg(int key, char *arg, struct argp_state *state)
argp_usage(state);
}
break;
case OPT_OUTPUT:
env.output = FORMAT_LINE_PROTOCOL;
if (arg) {
if (strcmp(arg, "line") == 0)
env.output = FORMAT_LINE_PROTOCOL;
else
argp_error(state, "Invalid output format: %s. "
"Only 'line' is supported.", arg);
}
break;
case ARGP_KEY_ARG:
pos_args++;

Expand Down Expand Up @@ -828,6 +842,27 @@ void print_stack_frames_by_syms_cache()
}
#endif

static void print_metrics(struct allocation *allocs, size_t nr_allocs, time_t ts)
{
for (size_t i = 0; i < nr_allocs; ++i) {
const struct allocation *alloc = &allocs[i];
struct metric m = {
.name = "memleak",
.tags = {{ "stackid", "" }},
.nr_tags = 1,
.fields = {
{ "size", alloc->size },
{ "count", alloc->count }
},
.nr_fields = 2,
.ts = ts
};
snprintf(m.tags[0].value, sizeof(m.tags[0].value), "%lu", alloc->stack_id);

print_metric(&m, env.output);
}
}

int print_stack_frames(struct allocation *allocs, size_t nr_allocs, int stack_traces_fd)
{
for (size_t i = 0; i < nr_allocs; ++i) {
Expand Down Expand Up @@ -977,6 +1012,11 @@ int print_outstanding_allocs(int allocs_fd, int stack_traces_fd)
nr_allocs++;
}

if (env.output) {
print_metrics(allocs, nr_allocs, t);
goto cleanup;
}

// sort the allocs array in descending order
qsort(allocs, nr_allocs, sizeof(allocs[0]), alloc_size_compare);

Expand All @@ -988,6 +1028,7 @@ int print_outstanding_allocs(int allocs_fd, int stack_traces_fd)

print_stack_frames(allocs, nr_allocs_to_show, stack_traces_fd);

cleanup:
// Reset allocs list so that we dont accidentaly reuse data the next time we call this function
for (size_t i = 0; i < nr_allocs; i++) {
allocs[i].stack_id = 0;
Expand Down Expand Up @@ -1067,6 +1108,11 @@ int print_outstanding_combined_allocs(int combined_allocs_fd, int stack_traces_f
nr_allocs++;
}

if (env.output) {
print_metrics(allocs, nr_allocs, t);
goto cleanup;
}

qsort(allocs, nr_allocs, sizeof(allocs[0]), alloc_size_compare);

// get min of allocs we stored vs the top N requested stacks
Expand All @@ -1077,6 +1123,7 @@ int print_outstanding_combined_allocs(int combined_allocs_fd, int stack_traces_f

print_stack_frames(allocs, nr_allocs, stack_traces_fd);

cleanup:
if (nr_missing_stacks > 0) {
fprintf(stderr, "WARNING: %zu stack traces could not be displayed"
" due to memory shortage, including %zu caused by hash collisions."
Expand Down
9 changes: 6 additions & 3 deletions libbpf-tools/syscall_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ static const char *syscall_names_generic[] = {
size_t syscall_names_generic_size = sizeof(syscall_names_generic)/sizeof(char*);
#endif

void syscall_name(unsigned n, char *buf, size_t size)
int syscall_name(unsigned n, char *buf, size_t size)
{
const char *name = NULL;

Expand All @@ -824,10 +824,13 @@ void syscall_name(unsigned n, char *buf, size_t size)
name = syscall_names_generic[n];
#endif

if (name)
if (name) {
strncpy(buf, name, size-1);
else
return 0;
} else {
snprintf(buf, size, "[unknown: %u]", n);
return -EINVAL;
}
}

int list_syscalls(void)
Expand Down
2 changes: 1 addition & 1 deletion libbpf-tools/syscall_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
void init_syscall_names(void);
void free_syscall_names(void);
void list_syscalls(void);
void syscall_name(unsigned n, char *buf, size_t size);
int syscall_name(unsigned n, char *buf, size_t size);

#endif /* __SYSCALL_HELPERS_H */
Loading
Loading