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