Run clang-format on the whole tree
[lttng-tools.git] / src / bin / lttng-sessiond / fd-limit.cpp
CommitLineData
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
c9e313bc 9#include "fd-limit.hpp"
28ab034a 10
c9e313bc 11#include <common/compat/errno.hpp>
28ab034a
JG
12#include <common/error.hpp>
13
14#include <stdio.h>
15#include <sys/resource.h>
16#include <sys/time.h>
17#include <urcu/uatomic.h>
4063050c
MD
18
19/* total count of fd. */
20static long fd_count;
21
22/*
23 * threshold in % of number of fd allowed.
24 */
25static long fd_threshold[LTTNG_FD_NR_TYPES] = {
7966af57 26 75, /* LTTNG_FD_APPS */
4063050c
MD
27};
28
29static rlim_t max_nr_fd;
30
31int lttng_fd_get(enum lttng_fd_type type, unsigned int nr)
32{
33 long newval;
34
35 if (type >= LTTNG_FD_NR_TYPES) {
36 return -EINVAL;
37 }
38
39 newval = uatomic_add_return(&fd_count, (long) nr);
28ab034a 40 if ((long) (newval * 100) - (long) (max_nr_fd * fd_threshold[type]) > 0) {
4063050c
MD
41 uatomic_sub(&fd_count, (long) nr);
42 return -EPERM;
43 }
44 return 0;
45}
46
28ab034a 47void lttng_fd_put(enum lttng_fd_type type __attribute__((unused)), unsigned int nr)
4063050c
MD
48{
49 uatomic_sub(&fd_count, (long) nr);
50}
51
52void 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}
This page took 0.069663 seconds and 4 git commands to generate.