Linux Audio

Check our new training course

Loading...
v4.6
 
 1/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
 2 *
 3 * This program is free software; you can redistribute it and/or
 4 * modify it under the terms of version 2 of the GNU General Public
 5 * License as published by the Free Software Foundation.
 6 */
 7#include <stdio.h>
 8#include <stdlib.h>
 9#include <signal.h>
10#include <unistd.h>
11#include <stdbool.h>
12#include <string.h>
13#include <time.h>
14#include <linux/bpf.h>
15#include "libbpf.h"
16#include "bpf_load.h"
 
17
18struct pair {
19	long long val;
20	__u64 ip;
21};
22
23static __u64 time_get_ns(void)
24{
25	struct timespec ts;
26
27	clock_gettime(CLOCK_MONOTONIC, &ts);
28	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
29}
30
31static void print_old_objects(int fd)
32{
33	long long val = time_get_ns();
34	__u64 key, next_key;
35	struct pair v;
36
37	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
38
39	key = -1;
40	while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
41		bpf_lookup_elem(map_fd[0], &next_key, &v);
42		key = next_key;
43		if (val - v.val < 1000000000ll)
44			/* object was allocated more then 1 sec ago */
45			continue;
46		printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
47		       next_key, (val - v.val) / 1000000000ll, v.ip);
48	}
49}
50
51int main(int ac, char **argv)
52{
 
 
 
 
53	char filename[256];
54	int i;
 
 
 
 
 
55
56	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
 
 
 
 
57
58	if (load_bpf_file(filename)) {
59		printf("%s", bpf_log_buf);
60		return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61	}
62
63	for (i = 0; ; i++) {
64		print_old_objects(map_fd[1]);
65		sleep(1);
66	}
67
 
 
 
 
 
68	return 0;
69}
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
 
 
 
 
  3 */
  4#include <stdio.h>
  5#include <stdlib.h>
  6#include <signal.h>
  7#include <unistd.h>
  8#include <stdbool.h>
  9#include <string.h>
 10#include <time.h>
 11#include <sys/resource.h>
 12
 13#include <bpf/bpf.h>
 14#include <bpf/libbpf.h>
 15
 16struct pair {
 17	long long val;
 18	__u64 ip;
 19};
 20
 21static __u64 time_get_ns(void)
 22{
 23	struct timespec ts;
 24
 25	clock_gettime(CLOCK_MONOTONIC, &ts);
 26	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
 27}
 28
 29static void print_old_objects(int fd)
 30{
 31	long long val = time_get_ns();
 32	__u64 key, next_key;
 33	struct pair v;
 34
 35	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
 36
 37	key = -1;
 38	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
 39		bpf_map_lookup_elem(fd, &next_key, &v);
 40		key = next_key;
 41		if (val - v.val < 1000000000ll)
 42			/* object was allocated more then 1 sec ago */
 43			continue;
 44		printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
 45		       next_key, (val - v.val) / 1000000000ll, v.ip);
 46	}
 47}
 48
 49int main(int ac, char **argv)
 50{
 51	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
 52	struct bpf_link *links[2];
 53	struct bpf_program *prog;
 54	struct bpf_object *obj;
 55	char filename[256];
 56	int map_fd, i, j = 0;
 57
 58	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
 59		perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
 60		return 1;
 61	}
 62
 63	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 64	obj = bpf_object__open_file(filename, NULL);
 65	if (libbpf_get_error(obj)) {
 66		fprintf(stderr, "ERROR: opening BPF object file failed\n");
 67		return 0;
 68	}
 69
 70	/* load BPF program */
 71	if (bpf_object__load(obj)) {
 72		fprintf(stderr, "ERROR: loading BPF object file failed\n");
 73		goto cleanup;
 74	}
 75
 76	map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
 77	if (map_fd < 0) {
 78		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
 79		goto cleanup;
 80	}
 81
 82	bpf_object__for_each_program(prog, obj) {
 83		links[j] = bpf_program__attach(prog);
 84		if (libbpf_get_error(links[j])) {
 85			fprintf(stderr, "ERROR: bpf_program__attach failed\n");
 86			links[j] = NULL;
 87			goto cleanup;
 88		}
 89		j++;
 90	}
 91
 92	for (i = 0; ; i++) {
 93		print_old_objects(map_fd);
 94		sleep(1);
 95	}
 96
 97cleanup:
 98	for (j--; j >= 0; j--)
 99		bpf_link__destroy(links[j]);
100
101	bpf_object__close(obj);
102	return 0;
103}