MCPcopy Create free account
hub / github.com/catboost/catboost / NumberOfCpus

Method NumberOfCpus

util/system/info.cpp:134–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

132}
133
134size_t NSystemInfo::NumberOfCpus() {
135#if defined(_linux_)
136 if (auto res = CgroupCpus(); res) {
137 return Max<ssize_t>(1, std::llround(res));
138 }
139#endif
140
141#if defined(_win_)
142 SYSTEM_INFO info;
143
144 GetSystemInfo(&info);
145
146 return info.dwNumberOfProcessors;
147#elif defined(_SC_NPROCESSORS_ONLN)
148 return sysconf(_SC_NPROCESSORS_ONLN);
149#elif defined(_linux_)
150 unsigned ret;
151 int fd, nread, column;
152 char buf[512];
153 static const char matchstr[] = "processor\t:";
154
155 fd = open("/proc/cpuinfo", O_RDONLY);
156
157 if (fd == -1) {
158 abort();
159 }
160
161 column = 0;
162 ret = 0;
163
164 while (true) {
165 nread = read(fd, buf, sizeof(buf));
166
167 if (nread <= 0) {
168 break;
169 }
170
171 for (int i = 0; i < nread; ++i) {
172 const char ch = buf[i];
173
174 if (ch == '\n') {
175 column = 0;
176 } else if (column != -1) {
177 if (AsciiToLower(ch) == matchstr[column]) {
178 ++column;
179
180 if (column == sizeof(matchstr) - 1) {
181 column = -1;
182 ++ret;
183 }
184 } else {
185 column = -1;
186 }
187 }
188 }
189 }
190
191 if (ret == 0) {

Callers

nothing calls this directly

Calls 6

CgroupCpusFunction · 0.85
AsciiToLowerFunction · 0.85
llroundFunction · 0.50
openFunction · 0.50
readFunction · 0.50
closeFunction · 0.50

Tested by

no test coverage detected