rlimit.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* SPDX-License-Identifier: LGPL-2.1 */
  2. #include <errno.h>
  3. #include "util/debug.h"
  4. #include "util/rlimit.h"
  5. #include <sys/time.h>
  6. #include <sys/resource.h>
  7. /*
  8. * Bump the memlock so that we can get bpf maps of a reasonable size,
  9. * like the ones used with 'perf trace' and with 'perf test bpf',
  10. * improve this to some specific request if needed.
  11. */
  12. void rlimit__bump_memlock(void)
  13. {
  14. struct rlimit rlim;
  15. if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
  16. rlim.rlim_cur *= 4;
  17. rlim.rlim_max *= 4;
  18. if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
  19. rlim.rlim_cur /= 2;
  20. rlim.rlim_max /= 2;
  21. if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
  22. pr_debug("Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc\n");
  23. }
  24. }
  25. }
  26. bool rlimit__increase_nofile(enum rlimit_action *set_rlimit)
  27. {
  28. int old_errno;
  29. struct rlimit l;
  30. if (*set_rlimit < INCREASED_MAX) {
  31. old_errno = errno;
  32. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  33. if (*set_rlimit == NO_CHANGE) {
  34. l.rlim_cur = l.rlim_max;
  35. } else {
  36. l.rlim_cur = l.rlim_max + 1000;
  37. l.rlim_max = l.rlim_cur;
  38. }
  39. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  40. (*set_rlimit) += 1;
  41. errno = old_errno;
  42. return true;
  43. }
  44. }
  45. errno = old_errno;
  46. }
  47. return false;
  48. }