Support LTTNG_KERNEL_SESSION_SET_CREATION_DATETIME of lttng-modules
[lttng-tools.git] / src / bin / lttng-sessiond / fd-limit.c
1 /*
2 * Copyright (C) 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <urcu/uatomic.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include "fd-limit.h"
25 #include <common/error.h>
26
27 /* total count of fd. */
28 static long fd_count;
29
30 /*
31 * threshold in % of number of fd allowed.
32 */
33 static long fd_threshold[LTTNG_FD_NR_TYPES] = {
34 [LTTNG_FD_APPS] = 75,
35 };
36
37 static rlim_t max_nr_fd;
38
39 int lttng_fd_get(enum lttng_fd_type type, unsigned int nr)
40 {
41 long newval;
42
43 if (type >= LTTNG_FD_NR_TYPES) {
44 return -EINVAL;
45 }
46
47 newval = uatomic_add_return(&fd_count, (long) nr);
48 if ((long) (newval * 100)
49 - (long) (max_nr_fd * fd_threshold[type]) > 0) {
50 uatomic_sub(&fd_count, (long) nr);
51 return -EPERM;
52 }
53 return 0;
54 }
55
56 void lttng_fd_put(enum lttng_fd_type type, unsigned int nr)
57 {
58 uatomic_sub(&fd_count, (long) nr);
59 }
60
61 void lttng_fd_init(void)
62 {
63 struct rlimit rlim;
64 int ret;
65
66 ret = getrlimit(RLIMIT_NOFILE, &rlim);
67 if (ret < 0) {
68 PERROR("getrlimit");
69 }
70 max_nr_fd = rlim.rlim_cur;
71 }
This page took 0.032816 seconds and 5 git commands to generate.