-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathresource_config.cpp
More file actions
86 lines (77 loc) · 2.32 KB
/
Copy pathresource_config.cpp
File metadata and controls
86 lines (77 loc) · 2.32 KB
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
#include <filesystem>
#include <fstream>
#include "log.h"
#include "register.h"
#include "resource_config.h"
using namespace std;
using namespace xpu;
int ResourceConfig::Initialize()
{
// check if client running in container
if (!filesystem::exists(xpu_.ConfigPath())) {
log_debug("{} no exist, client is running in host", xpu_.ConfigPath());
return RET_SUCC;
}
return LoadVxpuConfig();
}
int ResourceConfig::ParseLineByConfigName(const string& line, const string& configName,
unsigned long& value, unsigned int maxValue)
{
string valueStr;
string::size_type pos = line.npos;
pos = line.rfind(configName, 0);
if (pos == line.npos) {
log_err("not found {}", configName);
return RET_FAIL;
}
valueStr = line.substr(configName.size() + 1); // configName:
value = stoul(valueStr);
if (value > maxValue) {
log_err("parse {} failed, content {} overflow", configName, value);
return RET_FAIL;
}
return RET_SUCC;
}
/*
* Format in vgpu config:
* UsedMem:xxx
* UsedCores:yyy
*/
int ResourceConfig::LoadVxpuConfig()
{
const string configPath(xpu_.ConfigPath());
ifstream file(configPath);
if (!file.is_open()) {
FileOperateErrorHandler(file, configPath);
return RET_FAIL;
}
int ret;
string line;
unsigned long memoryValue;
unsigned long coresValue;
if (!getline(file, line)) {
log_err("getting line failed while parsing UsedMem");
return RET_FAIL;
}
ret = ParseLineByConfigName(line, "UsedMem", memoryValue, UINT_MAX);
if (ret) {
return ret;
}
memory_ = memoryValue * MEGABYTE;
limitMemory_ = true;
if (!getline(file, line)) {
log_err("getting line failed while parsing UsedCores");
return RET_FAIL;
}
ret = ParseLineByConfigName(line, "UsedCores", coresValue, PERCENT_MAX);
if (ret) {
return ret;
}
computingPower_ = static_cast<unsigned int>(coresValue);
// if computingPower is 0, don't limit computingPower
limitComputingPower_ = (computingPower_ != 0);
log_info("parse {} over, the configs are as follows: ", xpu_.ConfigPath());
log_info("limitMemory {}, limitComputingPower {}, memory {}, computingPower {}",
limitMemory_, limitComputingPower_, memory_, computingPower_);
return RET_SUCC;
}