Linux Audio

Check our new training course

Loading...
v4.17
 
 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 <sys/resource.h>
16
17#include "libbpf.h"
18#include "bpf_load.h"
19
20struct pair {
21	long long val;
22	__u64 ip;
23};
24
25static __u64 time_get_ns(void)
26{
27	struct timespec ts;
28
29	clock_gettime(CLOCK_MONOTONIC, &ts);
30	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
31}
32
33static void print_old_objects(int fd)
34{
35	long long val = time_get_ns();
36	__u64 key, next_key;
37	struct pair v;
38
39	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
40
41	key = -1;
42	while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
43		bpf_map_lookup_elem(map_fd[0], &next_key, &v);
44		key = next_key;
45		if (val - v.val < 1000000000ll)
46			/* object was allocated more then 1 sec ago */
47			continue;
48		printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
49		       next_key, (val - v.val) / 1000000000ll, v.ip);
50	}
51}
52
53int main(int ac, char **argv)
54{
55	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
56	char filename[256];
57	int i;
58
59	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
60
61	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
62		perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
63		return 1;
64	}
65
66	if (load_bpf_file(filename)) {
67		printf("%s", bpf_log_buf);
68		return 1;
69	}
70
71	for (i = 0; ; i++) {
72		print_old_objects(map_fd[1]);
73		sleep(1);
74	}
75
76	return 0;
77}
v5.4
 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 <linux/bpf.h>
12#include <sys/resource.h>
13
14#include <bpf/bpf.h>
15#include "bpf_load.h"
16
17struct pair {
18	long long val;
19	__u64 ip;
20};
21
22static __u64 time_get_ns(void)
23{
24	struct timespec ts;
25
26	clock_gettime(CLOCK_MONOTONIC, &ts);
27	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
28}
29
30static void print_old_objects(int fd)
31{
32	long long val = time_get_ns();
33	__u64 key, next_key;
34	struct pair v;
35
36	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
37
38	key = -1;
39	while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
40		bpf_map_lookup_elem(map_fd[0], &next_key, &v);
41		key = next_key;
42		if (val - v.val < 1000000000ll)
43			/* object was allocated more then 1 sec ago */
44			continue;
45		printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
46		       next_key, (val - v.val) / 1000000000ll, v.ip);
47	}
48}
49
50int main(int ac, char **argv)
51{
52	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
53	char filename[256];
54	int i;
55
56	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
57
58	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
59		perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
60		return 1;
61	}
62
63	if (load_bpf_file(filename)) {
64		printf("%s", bpf_log_buf);
65		return 1;
66	}
67
68	for (i = 0; ; i++) {
69		print_old_objects(map_fd[1]);
70		sleep(1);
71	}
72
73	return 0;
74}