aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/getMaxFileDescriptorCount.cpp
blob: 10142ef8445d40b9ba40275a8d295464cf8eec3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromString.h>
#include <IO/ReadBufferFromFile.h>
#include <Common/ShellCommand.h>
#include <Common/getMaxFileDescriptorCount.h>
#include <filesystem>

int getMaxFileDescriptorCount()
{
    namespace fs = std::filesystem;
    int result = -1;
#if defined(OS_LINUX) || defined(OS_DARWIN)
    using namespace DB;

    if (fs::exists("/proc/sys/fs/file-max"))
    {
        ReadBufferFromFile reader("/proc/sys/fs/file-max");
        readIntText(result, reader);
    }
    else
    {
        auto command = ShellCommand::execute("ulimit -n");
        try
        {
            readIntText(result, command->out);
            command->wait();
        }
        catch (...)
        {
        }
    }

#endif

    return result;
}