aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/cpumask.h
blob: 29741aa1d643141681b0c3501e7174a5158cc6ea (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma once

#include "defs.h"

#include <library/cpp/containers/stack_vector/stack_vec.h>

#include <util/string/split.h>
#include <util/generic/yexception.h>

using TCpuId = ui32;

// Simple data structure to operate with set of cpus
struct TCpuMask {
    TStackVec<bool, 1024> Cpus;

    // Creates empty mask
    TCpuMask() {}

    // Creates mask with single cpu set
    explicit TCpuMask(TCpuId cpuId) {
        Set(cpuId);
    }

    // Initialize mask from raw boolean array
    template <class T>
    TCpuMask(const T* cpus, TCpuId size) {
        Cpus.reserve(size);
        for (TCpuId i = 0; i != size; ++i) {
            Cpus.emplace_back(bool(cpus[i]));
        }
    }

    // Parse a numerical list of processors. The numbers are separated by commas and may include ranges. For example: 0,5,7,9-11
    explicit TCpuMask(const TString& cpuList) {
        try {
            for (TStringBuf s : StringSplitter(cpuList).Split(',')) {
                TCpuId l, r;
                if (s.find('-') != TString::npos) {
                    StringSplitter(s).Split('-').CollectInto(&l, &r);
                } else {
                    l = r = FromString<TCpuId>(s);
                }
                if (r >= Cpus.size()) {
                    Cpus.resize(r + 1, false);
                }
                for (TCpuId cpu = l; cpu <= r; cpu++) {
                    Cpus[cpu] = true;
                }
            }
        } catch (...) {
            ythrow TWithBackTrace<yexception>() << "Exception occured while parsing cpu list '" << cpuList << "': " << CurrentExceptionMessage();
        }
    }

    // Returns size of underlying vector
    TCpuId Size() const {
        return Cpus.size();
    }

    // Returns number of set bits in mask
    TCpuId CpuCount() const {
        TCpuId result = 0;
        for (bool value : Cpus) {
            result += value;
        }
        return result;
    }

    bool IsEmpty() const {
        for (bool value : Cpus) {
            if (value) {
                return false;
            }
        }
        return true;
    }

    bool IsSet(TCpuId cpu) const {
        return cpu < Cpus.size() && Cpus[cpu];
    }

    void Set(TCpuId cpu) {
        if (cpu >= Cpus.size()) {
            Cpus.resize(cpu + 1, false);
        }
        Cpus[cpu] = true;
    }

    void Reset(TCpuId cpu) {
        if (cpu < Cpus.size()) {
            Cpus[cpu] = false;
        }
    }

    void RemoveTrailingZeros() {
        while (!Cpus.empty() && !Cpus.back()) {
            Cpus.pop_back();
        }
    }

    explicit operator bool() const {
        return !IsEmpty();
    }

    TCpuMask operator &(const TCpuMask& rhs) const {
        TCpuMask result;
        TCpuId size = Max(Size(), rhs.Size());
        result.Cpus.reserve(size);
        for (TCpuId cpu = 0; cpu < size; cpu++) {
            result.Cpus.emplace_back(IsSet(cpu) && rhs.IsSet(cpu));
        }
        return result;
    }

    TCpuMask operator |(const TCpuMask& rhs) const {
        TCpuMask result;
        TCpuId size = Max(Size(), rhs.Size());
        result.Cpus.reserve(size);
        for (TCpuId cpu = 0; cpu < size; cpu++) {
            result.Cpus.emplace_back(IsSet(cpu) || rhs.IsSet(cpu));
        }
        return result;
    }

    TCpuMask operator -(const TCpuMask& rhs) const {
        TCpuMask result;
        result.Cpus.reserve(Size());
        for (TCpuId cpu = 0; cpu < Size(); cpu++) {
            result.Cpus.emplace_back(IsSet(cpu) && !rhs.IsSet(cpu));
        }
        return result;
    }
};