aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/resource_limits.cpp
blob: 070f8b40055b0643487b0e276291849ced1f9b6f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "resource_limits.h"
#include "cgroup_resource_limits.h"
#include <unistd.h>
#include <thread>

namespace vespalib {

ResourceLimits::ResourceLimits(uint64_t memory, uint32_t cpu)
    : _memory(memory),
      _cpu(cpu)
{
}

ResourceLimits
ResourceLimits::create()
{
    uint64_t memory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
    uint32_t cpu = std::thread::hardware_concurrency();
    CGroupResourceLimits cgroup_limits;
    auto& cg_memory = cgroup_limits.get_memory_limit();
    auto& cg_cpu = cgroup_limits.get_cpu_limit();
    if (cg_memory.has_value() && cg_memory.value() < memory) {
        memory = cg_memory.value();
    }
    if (cg_cpu.has_value() && cg_cpu.value() < cpu) {
        cpu = cg_cpu.value();
    }
    return ResourceLimits(memory, cpu);
}

}