forked from sirius-db/sirius
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsirius_config.cpp
More file actions
410 lines (357 loc) · 15.7 KB
/
sirius_config.cpp
File metadata and controls
410 lines (357 loc) · 15.7 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright 2025, Sirius Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sirius_config.hpp"
#include "exec/config.hpp"
#include "yaml_reader.hpp"
#include <cucascade/memory/config.hpp>
#include <cucascade/memory/reservation_manager_configurator.hpp>
#include <yaml-cpp/yaml.h>
#include <exception>
#include <variant>
#include <vector>
namespace sirius {
// ================ from_yaml for external types ================= //
static void from_yaml(const YAML::Node& node, cucascade::memory::gpu_memory_space_config& opt)
{
opt.per_stream_reservation = false; // default to false for sirius
yaml::reader r(node, "gpu_memory_space");
r.optional("device_id", opt.device_id);
r.optional("per_stream_reservation", opt.per_stream_reservation);
r.optional(
"reservation_limit_fraction", opt.reservation_limit_fraction, yaml::fraction<double>{});
r.optional(
"downgrade_trigger_fraction", opt.downgrade_trigger_fraction, yaml::fraction<double>{});
r.optional("downgrade_stop_fraction", opt.downgrade_stop_fraction, yaml::fraction<double>{});
r.optional("memory_capacity", yaml::bytes(opt.memory_capacity));
r.reject_unknown();
}
static void from_yaml(const YAML::Node& node, cucascade::memory::host_memory_space_config& opt)
{
yaml::reader r(node, "host_memory_space");
r.optional("numa_id", opt.numa_id);
r.optional(
"reservation_limit_fraction", opt.reservation_limit_fraction, yaml::fraction<double>{});
r.optional(
"downgrade_trigger_fraction", opt.downgrade_trigger_fraction, yaml::fraction<double>{});
r.optional("downgrade_stop_fraction", opt.downgrade_stop_fraction, yaml::fraction<double>{});
r.optional("memory_capacity", yaml::bytes(opt.memory_capacity));
r.optional("block_size", yaml::bytes(opt.block_size));
r.optional("pool_size", opt.pool_size);
r.optional("initial_number_pools", opt.initial_number_pools);
r.reject_unknown();
}
static void from_yaml(const YAML::Node& node, cucascade::memory::disk_memory_space_config& opt)
{
yaml::reader r(node, "disk_memory_space");
r.optional("disk_id", opt.disk_id);
r.optional("mount_path", opt.mount_paths);
r.optional("memory_capacity", yaml::bytes(opt.memory_capacity));
r.reject_unknown();
}
static void from_yaml(const YAML::Node& node, exec::thread_pool_config& opt)
{
yaml::reader r(node, "thread_pool");
r.optional("num_threads", opt.num_threads, yaml::greater_than<int>{0});
r.optional("thread_name_prefix", opt.thread_name_prefix);
r.optional("cpu_affinity", opt.cpu_affinity_list);
r.reject_unknown();
}
static void from_yaml(const YAML::Node& node, operator_params& opt)
{
yaml::reader r(node, "operator_params");
r.optional("scan_task_batch_size", yaml::bytes(opt.scan_task_batch_size));
r.optional("default_scan_task_varchar_size", yaml::bytes(opt.default_scan_task_varchar_size));
r.optional("max_sort_partition_bytes", yaml::bytes(opt.max_sort_partition_bytes));
r.optional("hash_partition_bytes", yaml::bytes(opt.hash_partition_bytes));
r.optional("concat_batch_bytes", yaml::bytes(opt.concat_batch_bytes));
r.optional("max_build_hash_table_bytes", yaml::bytes(opt.max_build_hash_table_bytes));
r.reject_unknown();
}
static void from_yaml(const YAML::Node& node, op::scan::scan_executor_config& opt)
{
yaml::reader r(node, "duckdb_scan");
r.optional("cache", opt.cache);
r.optional("num_threads", opt.thread_pool_config.num_threads, yaml::greater_than<int>{0});
r.optional("thread_name_prefix", opt.thread_pool_config.thread_name_prefix);
r.optional("cpu_affinity", opt.thread_pool_config.cpu_affinity_list);
r.reject_unknown();
}
static void from_yaml(const YAML::Node& node, exec::downgrade_executor_config& opt)
{
yaml::reader r(node, "downgrade");
r.optional("num_threads", opt.thread_pool.num_threads, yaml::greater_than<int>{0});
r.optional("thread_name_prefix", opt.thread_pool.thread_name_prefix);
r.optional("cpu_affinity", opt.thread_pool.cpu_affinity_list);
r.optional("monitor_period_ms", opt.monitor_period_ms);
r.reject_unknown();
}
namespace {
struct topology {
std::variant<size_t, std::vector<int>> num_gpus_or_gpu_ids{size_t{1}};
static void from_yaml(const YAML::Node& node, topology& opt)
{
yaml::reader r(node, "topology");
// gpu_ids and num_gpus are mutually exclusive; try gpu_ids first
std::vector<int> ids;
r.optional("gpu_ids", ids);
if (!ids.empty()) {
opt.num_gpus_or_gpu_ids = std::move(ids);
} else {
size_t n = 1;
r.optional("num_gpus", n);
opt.num_gpus_or_gpu_ids = n;
}
r.reject_unknown();
}
};
struct gpu_mem_config {
std::variant<double, std::uint64_t> usage_limit{0.95};
std::variant<double, std::uint64_t> reservation_limit{0.9};
double downgrade_trigger_fraction{1.0};
double downgrade_stop_fraction{0.7};
bool track_per_stream_reservation{false};
static void from_yaml(const YAML::Node& node, gpu_mem_config& opt)
{
opt.track_per_stream_reservation = false;
yaml::reader r(node, "memory.gpu");
// usage_limit: fraction (double) or absolute bytes — mutually exclusive keys
std::optional<std::uint64_t> usage_bytes;
double usage_frac = 0.95;
r.optional("usage_limit_bytes", yaml::bytes(usage_bytes));
r.optional("usage_limit_fraction", usage_frac, yaml::fraction<double>{});
opt.usage_limit = usage_bytes ? std::variant<double, std::uint64_t>{*usage_bytes}
: std::variant<double, std::uint64_t>{usage_frac};
// reservation_limit: fraction or absolute bytes
std::optional<std::uint64_t> res_bytes;
double res_frac = 0.9;
r.optional("reservation_limit_bytes", yaml::bytes(res_bytes));
r.optional("reservation_limit_fraction", res_frac, yaml::fraction<double>{});
opt.reservation_limit = res_bytes ? std::variant<double, std::uint64_t>{*res_bytes}
: std::variant<double, std::uint64_t>{res_frac};
r.optional(
"downgrade_trigger_fraction", opt.downgrade_trigger_fraction, yaml::fraction<double>{});
r.optional("downgrade_stop_fraction", opt.downgrade_stop_fraction, yaml::fraction<double>{});
r.optional("track_per_stream_reservation", opt.track_per_stream_reservation);
r.reject_unknown();
}
void setup_configurator(cucascade::memory::reservation_manager_configurator& builder) const
{
if (std::holds_alternative<double>(usage_limit)) {
builder.set_usage_limit_ratio_per_gpu(std::get<double>(usage_limit));
} else {
builder.set_gpu_usage_limit(std::get<std::uint64_t>(usage_limit));
}
if (std::holds_alternative<double>(reservation_limit)) {
builder.set_reservation_fraction_per_gpu(std::get<double>(reservation_limit));
} else {
builder.set_reservation_fraction_per_gpu(std::get<std::uint64_t>(reservation_limit));
}
builder.set_downgrade_fractions_per_gpu(downgrade_trigger_fraction, downgrade_stop_fraction);
builder.track_reservation_per_stream(track_per_stream_reservation);
}
};
struct host_mem_config {
std::uint64_t numa_region_capacity_bytes = 8UL << 30; // 8GB per NUMA node
std::variant<double, std::uint64_t> reservation_limit{0.9};
double downgrade_trigger_fraction{0.8};
double downgrade_stop_fraction{0.7};
std::size_t block_size{cucascade::memory::default_block_size};
std::size_t pool_size{cucascade::memory::default_pool_size};
std::size_t initial_number_pools{cucascade::memory::default_initial_number_pools};
static void from_yaml(const YAML::Node& node, host_mem_config& opt)
{
yaml::reader r(node, "memory.host");
r.optional("capacity_bytes", yaml::bytes(opt.numa_region_capacity_bytes));
std::optional<std::uint64_t> res_bytes;
double res_frac = 0.9;
r.optional("reservation_limit_bytes", yaml::bytes(res_bytes));
r.optional("reservation_limit_fraction", res_frac, yaml::fraction<double>{});
opt.reservation_limit = res_bytes ? std::variant<double, std::uint64_t>{*res_bytes}
: std::variant<double, std::uint64_t>{res_frac};
r.optional(
"downgrade_trigger_fraction", opt.downgrade_trigger_fraction, yaml::fraction<double>{});
r.optional("downgrade_stop_fraction", opt.downgrade_stop_fraction, yaml::fraction<double>{});
r.optional("block_size", yaml::bytes(opt.block_size));
r.optional("pool_size", opt.pool_size);
r.optional("initial_number_pools", opt.initial_number_pools);
r.reject_unknown();
}
void setup_configurator(cucascade::memory::reservation_manager_configurator& builder) const
{
builder.use_host_per_numa();
if (std::holds_alternative<double>(reservation_limit)) {
builder.set_reservation_fraction_per_host(std::get<double>(reservation_limit));
} else {
builder.set_reservation_fraction_per_host(std::get<std::uint64_t>(reservation_limit));
}
builder.set_downgrade_fractions_per_host(downgrade_trigger_fraction, downgrade_stop_fraction);
builder.set_per_host_capacity(numa_region_capacity_bytes);
builder.set_host_pool_features(block_size, pool_size, initial_number_pools);
}
};
struct disk_mem_config {
int id{0};
std::size_t capacity_bytes{1024UL << 30}; // 1TB
std::string downgrade_root_dirs;
static void from_yaml(const YAML::Node& node, disk_mem_config& opt)
{
yaml::reader r(node, "memory.disk");
r.optional("disk_id", opt.id);
r.optional("capacity_bytes", yaml::bytes(opt.capacity_bytes));
r.optional("downgrade_root_dirs", opt.downgrade_root_dirs);
r.reject_unknown();
}
void setup_configurator(cucascade::memory::reservation_manager_configurator& builder) const
{
if (downgrade_root_dirs.empty() || capacity_bytes == 0) { return; }
builder.set_disk_mounting_point(id, capacity_bytes, downgrade_root_dirs);
}
};
// Helper: read a vector using file-local from_yaml overloads
template <typename T>
void read_yaml_vec(const YAML::Node& node, std::vector<T>& out)
{
if (!node.IsSequence()) { throw std::runtime_error("expected a sequence"); }
for (const auto& item : node) {
T val{};
from_yaml(item, val);
out.push_back(std::move(val));
}
}
} // namespace
// ================ sirius_config ================= //
sirius_config::sirius_config()
{
cucascade::memory::topology_discovery discovery;
if (discovery.discover()) { _hw_topology = discovery.get_topology(); }
}
void sirius_config::apply_defaults()
{
// Run the configurator with default values to populate memory space configs
topology topo;
gpu_mem_config gpu_cfg;
host_mem_config host_cfg;
disk_mem_config disk_cfg;
cucascade::memory::reservation_manager_configurator builder;
builder.set_number_of_gpus(std::get<size_t>(topo.num_gpus_or_gpu_ids));
gpu_cfg.setup_configurator(builder);
host_cfg.setup_configurator(builder);
disk_cfg.setup_configurator(builder);
_memory_space_configs = builder.build(_hw_topology);
}
void sirius_config::load_from_file(const std::filesystem::path& config_path)
{
try {
YAML::Node root;
try {
root = YAML::LoadFile(config_path.string());
} catch (const YAML::Exception& e) {
throw std::runtime_error("failed to parse YAML: " + std::string(e.what()));
}
yaml::reader top(root);
auto sirius_node = top.optional_node("sirius");
top.reject_unknown();
if (!sirius_node) { throw std::runtime_error("missing top-level 'sirius' key"); }
yaml::reader r(*sirius_node, "sirius");
// Topology
topology topo;
r.optional("topology", topo);
// High-level memory config (mutually exclusive with space config)
gpu_mem_config gpu_cfg;
host_mem_config host_cfg;
disk_mem_config disk_cfg;
if (auto mem_node = r.optional_node("memory")) {
yaml::reader mr(*mem_node, "sirius.memory");
if (auto n = mr.optional_node("gpu")) gpu_mem_config::from_yaml(*n, gpu_cfg);
if (auto n = mr.optional_node("host")) host_mem_config::from_yaml(*n, host_cfg);
if (auto n = mr.optional_node("disk")) disk_mem_config::from_yaml(*n, disk_cfg);
mr.reject_unknown();
}
// Executors
if (auto exec_node = r.optional_node("executor")) {
yaml::reader er(*exec_node, "sirius.executor");
if (auto n = er.optional_node("task_creator")) from_yaml(*n, _task_creator_config);
if (auto n = er.optional_node("pipeline")) from_yaml(*n, _gpu_pipeline_executor_config);
if (auto n = er.optional_node("downgrade")) from_yaml(*n, _downgrade_executor_config);
if (auto n = er.optional_node("duckdb_scan")) sirius::from_yaml(*n, _scan_executor_config);
er.reject_unknown();
}
// Operator params
if (auto n = r.optional_node("operator_params")) { sirius::from_yaml(*n, _operator_params); }
// Explicit space configs (low-level API)
std::vector<cucascade::memory::gpu_memory_space_config> gpu_space_configs;
std::vector<cucascade::memory::host_memory_space_config> host_space_configs;
std::vector<cucascade::memory::disk_memory_space_config> disk_space_configs;
if (auto space_node = r.optional_node("space")) {
yaml::reader sr(*space_node, "sirius.space");
if (auto n = sr.optional_node("gpu")) read_yaml_vec(*n, gpu_space_configs);
if (auto n = sr.optional_node("host")) read_yaml_vec(*n, host_space_configs);
if (auto n = sr.optional_node("disk")) read_yaml_vec(*n, disk_space_configs);
sr.reject_unknown();
}
r.reject_unknown();
// Build memory space configs
_memory_space_configs.clear();
std::copy(gpu_space_configs.begin(),
gpu_space_configs.end(),
std::back_inserter(_memory_space_configs));
std::copy(host_space_configs.begin(),
host_space_configs.end(),
std::back_inserter(_memory_space_configs));
std::copy(disk_space_configs.begin(),
disk_space_configs.end(),
std::back_inserter(_memory_space_configs));
bool using_configurator = _memory_space_configs.empty();
if (using_configurator) {
cucascade::memory::reservation_manager_configurator builder;
if (std::holds_alternative<size_t>(topo.num_gpus_or_gpu_ids)) {
builder.set_number_of_gpus(std::get<size_t>(topo.num_gpus_or_gpu_ids));
} else {
const auto& gpu_ids = std::get<std::vector<int>>(topo.num_gpus_or_gpu_ids);
builder.set_gpu_ids(gpu_ids);
}
gpu_cfg.setup_configurator(builder);
host_cfg.setup_configurator(builder);
disk_cfg.setup_configurator(builder);
_memory_space_configs = builder.build(_hw_topology);
}
} catch (const std::exception& e) {
throw std::runtime_error("Failed to load config from " + config_path.string() + ": " +
e.what());
}
}
const std::vector<cucascade::memory::memory_space_config>& sirius_config::get_memory_space_configs()
const noexcept
{
return _memory_space_configs;
}
const exec::thread_pool_config& sirius_config::get_gpu_pipeline_executor_config() const noexcept
{
return _gpu_pipeline_executor_config;
}
const exec::downgrade_executor_config& sirius_config::get_downgrade_executor_config() const noexcept
{
return _downgrade_executor_config;
}
const exec::thread_pool_config& sirius_config::get_task_creator_config() const noexcept
{
return _task_creator_config;
}
const exec::thread_pool_config& sirius_config::get_duckdb_scan_executor_config() const noexcept
{
return _scan_executor_config.thread_pool_config;
}
} // namespace sirius