xref: /openbmc/linux/tools/perf/util/rlimit.c (revision 8795a739)
1 /* SPDX-License-Identifier: LGPL-2.1 */
2 
3 #include "util/debug.h"
4 #include "util/rlimit.h"
5 #include <sys/time.h>
6 #include <sys/resource.h>
7 
8 /*
9  * Bump the memlock so that we can get bpf maps of a reasonable size,
10  * like the ones used with 'perf trace' and with 'perf test bpf',
11  * improve this to some specific request if needed.
12  */
13 void rlimit__bump_memlock(void)
14 {
15 	struct rlimit rlim;
16 
17 	if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
18 		rlim.rlim_cur *= 4;
19 		rlim.rlim_max *= 4;
20 
21 		if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
22 			rlim.rlim_cur /= 2;
23 			rlim.rlim_max /= 2;
24 
25 			if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
26 				pr_debug("Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc\n");
27 		}
28 	}
29 }
30