Catch exceptions during file read. (#10623)

This commit is contained in:
Jiaming Yuan 2024-07-23 03:48:19 +08:00 committed by GitHub
parent a19bbc9be5
commit 485d90218c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -74,18 +74,27 @@ std::int32_t GetCGroupV2Count(std::filesystem::path const& bandwidth_path) noexc
std::int32_t GetCfsCPUCount() noexcept {
namespace fs = std::filesystem;
try {
fs::path const bandwidth_path{"/sys/fs/cgroup/cpu.max"};
auto has_v2 = fs::exists(bandwidth_path);
if (has_v2) {
return GetCGroupV2Count(bandwidth_path);
}
} catch (std::exception const&) {
return -1;
}
try {
fs::path const quota_path{"/sys/fs/cgroup/cpu/cpu.cfs_quota_us"};
fs::path const peroid_path{"/sys/fs/cgroup/cpu/cpu.cfs_period_us"};
auto has_v1 = fs::exists(quota_path) && fs::exists(peroid_path);
if (has_v1) {
return GetCGroupV1Count(quota_path, peroid_path);
}
} catch (std::exception const&) {
return -1;
}
return -1;
}