X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Ffd-limit.cpp;fp=src%2Fbin%2Flttng-sessiond%2Ffd-limit.cpp;h=833b64714b2aa1e9b1920c953962bf787076ee79;hb=7966af5763c4aaca39df9bbfa9277ff15715c720;hp=0000000000000000000000000000000000000000;hpb=3a5f70173aa04d11ccb22694d5d31a702cad33ab;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/fd-limit.cpp b/src/bin/lttng-sessiond/fd-limit.cpp new file mode 100644 index 000000000..833b64714 --- /dev/null +++ b/src/bin/lttng-sessiond/fd-limit.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2012 Mathieu Desnoyers + * + * SPDX-License-Identifier: GPL-2.0-only + * + */ + +#define _LGPL_SOURCE +#include +#include +#include +#include +#include "fd-limit.h" +#include +#include + +/* total count of fd. */ +static long fd_count; + +/* + * threshold in % of number of fd allowed. + */ +static long fd_threshold[LTTNG_FD_NR_TYPES] = { + 75, /* LTTNG_FD_APPS */ +}; + +static rlim_t max_nr_fd; + +int lttng_fd_get(enum lttng_fd_type type, unsigned int nr) +{ + long newval; + + if (type >= LTTNG_FD_NR_TYPES) { + return -EINVAL; + } + + newval = uatomic_add_return(&fd_count, (long) nr); + if ((long) (newval * 100) + - (long) (max_nr_fd * fd_threshold[type]) > 0) { + uatomic_sub(&fd_count, (long) nr); + return -EPERM; + } + return 0; +} + +void lttng_fd_put(enum lttng_fd_type type, unsigned int nr) +{ + uatomic_sub(&fd_count, (long) nr); +} + +void lttng_fd_init(void) +{ + struct rlimit rlim; + int ret; + + ret = getrlimit(RLIMIT_NOFILE, &rlim); + if (ret < 0) { + PERROR("getrlimit"); + } + max_nr_fd = rlim.rlim_cur; +}