Commit | Line | Data |
---|---|---|
4063050c | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4063050c | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
4063050c | 5 | * |
4063050c MD |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
4063050c MD |
9 | #include <urcu/uatomic.h> |
10 | #include <sys/time.h> | |
11 | #include <sys/resource.h> | |
4063050c | 12 | #include <stdio.h> |
c9e313bc SM |
13 | #include "fd-limit.hpp" |
14 | #include <common/error.hpp> | |
15 | #include <common/compat/errno.hpp> | |
4063050c MD |
16 | |
17 | /* total count of fd. */ | |
18 | static long fd_count; | |
19 | ||
20 | /* | |
21 | * threshold in % of number of fd allowed. | |
22 | */ | |
23 | static long fd_threshold[LTTNG_FD_NR_TYPES] = { | |
7966af57 | 24 | 75, /* LTTNG_FD_APPS */ |
4063050c MD |
25 | }; |
26 | ||
27 | static rlim_t max_nr_fd; | |
28 | ||
29 | int lttng_fd_get(enum lttng_fd_type type, unsigned int nr) | |
30 | { | |
31 | long newval; | |
32 | ||
33 | if (type >= LTTNG_FD_NR_TYPES) { | |
34 | return -EINVAL; | |
35 | } | |
36 | ||
37 | newval = uatomic_add_return(&fd_count, (long) nr); | |
38 | if ((long) (newval * 100) | |
39 | - (long) (max_nr_fd * fd_threshold[type]) > 0) { | |
40 | uatomic_sub(&fd_count, (long) nr); | |
41 | return -EPERM; | |
42 | } | |
43 | return 0; | |
44 | } | |
45 | ||
f46376a1 MJ |
46 | void lttng_fd_put(enum lttng_fd_type type __attribute__((unused)), |
47 | unsigned int nr) | |
4063050c MD |
48 | { |
49 | uatomic_sub(&fd_count, (long) nr); | |
50 | } | |
51 | ||
52 | void lttng_fd_init(void) | |
53 | { | |
54 | struct rlimit rlim; | |
55 | int ret; | |
56 | ||
57 | ret = getrlimit(RLIMIT_NOFILE, &rlim); | |
58 | if (ret < 0) { | |
6f04ed72 | 59 | PERROR("getrlimit"); |
4063050c MD |
60 | } |
61 | max_nr_fd = rlim.rlim_cur; | |
62 | } |