Fix: build failure using disable-lttng-ust configure option
[lttng-tools.git] / src / bin / lttng-sessiond / fd-limit.c
CommitLineData
4063050c
MD
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 _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
4063050c
MD
20#include <urcu/uatomic.h>
21#include <sys/time.h>
22#include <sys/resource.h>
23#include <errno.h>
24#include <stdio.h>
25#include "fd-limit.h"
6f04ed72 26#include <common/error.h>
4063050c
MD
27
28/* total count of fd. */
29static long fd_count;
30
31/*
32 * threshold in % of number of fd allowed.
33 */
34static long fd_threshold[LTTNG_FD_NR_TYPES] = {
35 [LTTNG_FD_APPS] = 75,
36};
37
38static rlim_t max_nr_fd;
39
40int lttng_fd_get(enum lttng_fd_type type, unsigned int nr)
41{
42 long newval;
43
44 if (type >= LTTNG_FD_NR_TYPES) {
45 return -EINVAL;
46 }
47
48 newval = uatomic_add_return(&fd_count, (long) nr);
49 if ((long) (newval * 100)
50 - (long) (max_nr_fd * fd_threshold[type]) > 0) {
51 uatomic_sub(&fd_count, (long) nr);
52 return -EPERM;
53 }
54 return 0;
55}
56
57void lttng_fd_put(enum lttng_fd_type type, unsigned int nr)
58{
59 uatomic_sub(&fd_count, (long) nr);
60}
61
62void lttng_fd_init(void)
63{
64 struct rlimit rlim;
65 int ret;
66
67 ret = getrlimit(RLIMIT_NOFILE, &rlim);
68 if (ret < 0) {
6f04ed72 69 PERROR("getrlimit");
4063050c
MD
70 }
71 max_nr_fd = rlim.rlim_cur;
72}
This page took 0.036201 seconds and 4 git commands to generate.