-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOptionParser.cc
233 lines (188 loc) · 5.67 KB
/
OptionParser.cc
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <[email protected]>
* Yao Yuan <[email protected]>
*/
#include <errno.h>
#include "OptionParser.h"
OptionParser::OptionParser()
: Option()
{
}
OptionParser::~OptionParser()
{
}
int OptionParser::parse_file(std::string filename)
{
config_file = filename;
return reparse();
}
int OptionParser::reparse()
{
int ret_val;
YAML::Node config;
try {
config = YAML::LoadFile(config_file);
} catch (...) {
printf("ERROR: exception on loading YAML file %s\n", config_file.c_str());
return -1;
}
try {
ret_val = parse_option(config["options"]);
if (ret_val < 0) {
printf("ERROR: failed to parse options\n");
return ret_val;
}
ret_val = parse_policies(config["policies"]);
if (dump_options)
dump();
} catch (...) {
ret_val = -1;
printf("ERROR: exception on parsing options/policies\n");
}
return ret_val;
}
template<typename Tval>
int OptionParser::get_value(const YAML::const_iterator &iter,
const char* key_name, Tval &value)
{
std::string key = iter->first.as<std::string>();
if (!key.compare(key_name))
{
value = iter->second.as<Tval>();
return 1;
}
return 0;
}
int OptionParser::get_value(const YAML::const_iterator &iter,
const char* key_name, YAML::Node &value)
{
std::string key = iter->first.as<std::string>();
if (!key.compare(key_name))
{
value = iter->second;
return 1;
}
return 0;
}
int OptionParser::parse_option(YAML::Node &&option_node)
{
if (!option_node)
return -1;
if (!option_node.IsMap())
return -1;
for (YAML::const_iterator iter = option_node.begin();
iter != option_node.end();
++iter) {
#define OP_GET_VALUE(name, member) if (get_value(iter, name, member)) continue;
OP_GET_VALUE("max_walks", max_walks);
OP_GET_VALUE("interval", interval);
OP_GET_VALUE("initial_interval", initial_interval);
OP_GET_VALUE("sleep", sleep_secs);
OP_GET_VALUE("loop", nr_loops);
OP_GET_VALUE("max_threads", max_threads);
OP_GET_VALUE("split_rss_size", split_rss_size);
OP_GET_VALUE("bandwidth_mbps", bandwidth_mbps);
OP_GET_VALUE("dram_percent", dram_percent);
OP_GET_VALUE("output", output_file);
OP_GET_VALUE("hugetlb", hugetlb);
OP_GET_VALUE("thp", thp);
OP_GET_VALUE("exit_on_stabilized", exit_on_stabilized);
OP_GET_VALUE("numa_dram_nodes", numa_hw_config.numa_dram_list);
OP_GET_VALUE("numa_pmem_nodes", numa_hw_config.numa_pmem_list);
OP_GET_VALUE("numa_peer_nodes", numa_hw_config.pmem_dram_map);
OP_GET_VALUE("debug_move_pages", debug_move_pages);
#undef OP_GET_VALUE
std::string str_val;
#define OP_GET_BOOL_VALUE(name, member, max_val) \
if (get_value(iter, name, str_val)) { \
Option::parse_name_map(bool_name_map, str_val, member, max_val); \
continue; \
}
OP_GET_BOOL_VALUE("dump_options", dump_options, 2);
OP_GET_BOOL_VALUE("dump_processes", dump_processes, 2);
OP_GET_BOOL_VALUE("exit_on_exceeded", exit_on_exceeded, 2);
OP_GET_BOOL_VALUE("daemon", daemon, 2);
OP_GET_BOOL_VALUE("show_numa_stats", show_numa_stats, 2);
#undef OP_GET_BOOL_VALUE
YAML::Node sub_node;
if (get_value(iter, "numa_nodes", sub_node)) {
parse_numa_nodes(sub_node);
continue;
}
// parse_common_policy(iter, default_policy);
}
return 0;
}
void OptionParser::parse_numa_nodes(YAML::Node &numa_nodes)
{
std::string id;
YAML::Node node_entry;
NumaHWConfigEntry new_config_entry;
for (auto iter = numa_nodes.begin();
iter != numa_nodes.end();
++iter) {
new_config_entry.clear();
id = iter->first.as<std::string>();
node_entry = iter->second;
parse_one_numa_node(node_entry, new_config_entry);
new_config_entry["id"] = id;
numa_hw_config_v2.push_back(new_config_entry);
}
}
void OptionParser::parse_one_numa_node(YAML::Node &one_numa_node,
NumaHWConfigEntry &one_entry)
{
for (auto iter = one_numa_node.begin();
iter != one_numa_node.end();
++iter)
one_entry[iter->first.as<std::string>()]
= iter->second.as<std::string>();
}
int OptionParser::parse_policies(YAML::Node &&policies_node)
{
if (!policies_node)
return -1;
if (!policies_node.IsSequence())
return -1;
for (std::size_t i = 0; i < policies_node.size(); ++i) {
if (!policies_node[i].IsMap())
continue;
parse_one_policy(policies_node[i]);
}
return 0;
}
void OptionParser::parse_common_policy(const YAML::const_iterator& iter, Policy& policy)
{
std::string str_val;
if (get_value(iter, "migration", str_val)) {
policy.migrate_what
= Option::parse_migrate_name(str_val);
return;
}
if (get_value(iter, "placement", str_val)) {
Option::parse_name_map(placement_name_map, str_val, policy.placement, PLACEMENT_END);
return;
}
if (get_value(iter, "dump_distribution", str_val)) {
Option::parse_name_map(bool_name_map, str_val, policy.dump_distribution, 2);
return;
}
}
void OptionParser::parse_one_policy(YAML::Node &&policy_node)
{
struct Policy new_policy;
for (auto iter = policy_node.begin();
iter != policy_node.end();
++iter) {
if (get_value(iter, "pid", new_policy.pid))
continue;
if (get_value(iter, "name", new_policy.name))
continue;
parse_common_policy(iter, new_policy);
}
add_policy(new_policy);
}