File size: 1,357 Bytes
4b23c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
"""space.limits: the cgroup CPU quota and the thread cap derived from it."""
import os

import pytest

from space import limits as L


def test_the_quota_is_the_ceiling_of_quota_over_period():
    assert L.cpu_quota_from("100000 100000") == 1
    assert L.cpu_quota_from("150000 100000") == 2
    assert L.cpu_quota_from("800000 100000\n") == 8
    assert L.cpu_quota_from("50000 100000") == 1
    assert L.cpu_quota_from("max 100000") is None
    with pytest.raises(ValueError):
        L.cpu_quota_from("")


def test_the_cap_sets_only_the_unset_variables(monkeypatch):
    for var in L.THREAD_VARS:
        monkeypatch.delenv(var, raising=False)
    monkeypatch.setenv("MKL_NUM_THREADS", "3")
    assert L.cap_threads(quota=6) == 6
    assert os.environ["OMP_NUM_THREADS"] == "6" and os.environ["OPENBLAS_NUM_THREADS"] == "6"
    assert os.environ["MKL_NUM_THREADS"] == "3"


def test_the_affinity_cap_keeps_the_first_cpus_of_the_allowed_set():
    before = os.sched_getaffinity(0)
    try:
        assert L.cap_affinity(quota=len(before)) is None                        # no more than the quota: untouched
        assert os.sched_getaffinity(0) == before
        if len(before) > 1:
            got = L.cap_affinity(quota=1)
            assert got == {min(before)} and os.sched_getaffinity(0) == got
    finally:
        os.sched_setaffinity(0, before)