-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathConfigManager.cpp
1610 lines (1285 loc) · 46.3 KB
/
ConfigManager.cpp
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
#include "caliper/caliper-config.h"
#include "caliper/ConfigManager.h"
#include "caliper/common/Log.h"
#include "caliper/common/StringConverter.h"
#include "../src/common/util/format_util.h"
#include "../src/common/util/parse_util.h"
#include "../services/Services.h"
#include <algorithm>
#include <fstream>
#include <sstream>
using namespace cali;
namespace cali
{
// defined in controllers/controllers.cpp
extern const ConfigManager::ConfigInfo* builtin_controllers_table[];
extern const char* builtin_base_option_specs;
extern const char* builtin_gotcha_option_specs;
extern const char* builtin_libdw_option_specs;
extern const char* builtin_mpi_option_specs;
extern const char* builtin_openmp_option_specs;
extern const char* builtin_cuda_option_specs;
extern const char* builtin_rocm_option_specs;
extern const char* builtin_pcp_option_specs;
extern const char* builtin_umpire_option_specs;
extern const char* builtin_kokkos_option_specs;
extern const char* builtin_papi_hsw_option_specs;
extern const char* builtin_papi_skl_option_specs;
extern const char* builtin_papi_spr_option_specs;
extern void add_submodule_controllers_and_services();
} // namespace cali
namespace
{
ChannelController* make_basic_channel_controller(
const char* name,
const config_map_t& initial_cfg,
const ConfigManager::Options& opts
)
{
class BasicController : public ChannelController
{
public:
BasicController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts)
: ChannelController(name, 0, initial_cfg)
{
// Hacky way to handle "output" option
if (opts.is_set("output")) {
std::string output = opts.get("output");
config()["CALI_RECORDER_FILENAME"] = output;
config()["CALI_REPORT_FILENAME"] = output;
config()["CALI_MPIREPORT_FILENAME"] = output;
}
opts.update_channel_config(config());
opts.update_channel_metadata(metadata());
}
};
return new BasicController(name, initial_cfg, opts);
}
class ConfigSpecManager
{
std::vector<ConfigManager::ConfigInfo> m_configs;
public:
void add_controller_specs(const ConfigManager::ConfigInfo** specs)
{
for (const ConfigManager::ConfigInfo** s = specs; s && *s; s++)
m_configs.push_back(**s);
}
std::vector<ConfigManager::ConfigInfo> get_config_specs() const { return m_configs; }
static ConfigSpecManager* instance()
{
static std::unique_ptr<ConfigSpecManager> mP { new ConfigSpecManager };
if (mP->m_configs.empty())
mP->add_controller_specs(builtin_controllers_table);
return mP.get();
}
};
ConfigManager::arglist_t merge_new_elements(ConfigManager::arglist_t& to, const ConfigManager::arglist_t& from)
{
for (auto& p : from) {
auto it = std::find_if(to.begin(), to.end(), [p](const std::pair<std::string, std::string>& v) {
return p.first == v.first;
});
if (it == to.end() || p.first == "metadata") // hacky but we want to allow multiple entries
// for metadata
to.push_back(p);
}
return to;
}
std::vector<std::string> to_stringlist(const std::vector<StringConverter>& list)
{
std::vector<std::string> ret;
ret.reserve(list.size());
for (const StringConverter& sc : list)
ret.push_back(sc.to_string());
return ret;
}
std::string join_stringlist(const std::vector<std::string>& list)
{
std::string ret;
int c = 0;
for (const std::string& s : list) {
if (c++ > 0)
ret.append(",");
ret.append(s);
}
return ret;
}
void join_stringlist(std::string& in, const std::vector<std::string>& list)
{
for (const auto& s : list) {
if (!in.empty())
in.append(",");
in.append(s);
}
}
std::string find_or(const std::map<std::string, std::string>& m, const std::string& k, const std::string v = "")
{
auto it = m.find(k);
return it != m.end() ? it->second : v;
}
std::string expand_variables(const std::string& in, const std::string& val)
{
std::string ret;
std::istringstream is(in);
bool esc = false;
while (is.good()) {
char c = is.get();
if (c == '\\') {
c = is.get();
if (is.good())
ret.push_back(c);
continue;
} else if (c == '"') {
esc = !esc;
continue;
} else if (c == '{') {
c = is.get();
if (c == '}') {
ret.append(val);
continue;
} else {
ret.push_back('{');
}
}
if (!is.good())
break;
ret.push_back(c);
}
return ret;
}
ConfigManager::arglist_t parse_keyval_list(std::istream& is)
{
ConfigManager::arglist_t ret;
char c = 0;
do {
std::string key = util::read_word(is, "=");
if (key.empty())
return ret;
c = util::read_char(is);
if (c != '=')
return ret;
std::string val = util::read_word(is, ",()");
if (!val.empty())
ret.push_back(std::make_pair(key, val));
c = util::read_char(is);
} while (is.good() && c == ',');
if (is.good())
is.unget();
return ret;
}
std::pair<bool, StringConverter> find_key_in_json(
const std::vector<std::string>& path,
const std::map<std::string, StringConverter>& dict
)
{
if (path.empty())
return std::make_pair(false, StringConverter());
auto it = dict.find(path.front());
if (it == dict.end())
return std::make_pair(false, StringConverter());
if (path.size() == 1)
return std::make_pair(true, it->second);
StringConverter ret = it->second;
for (auto path_it = path.begin() + 1; path_it != path.end(); ++path_it) {
auto sub_dict = ret.rec_dict();
it = sub_dict.find(*path_it);
if (it == sub_dict.end())
return std::make_pair(false, StringConverter());
ret = it->second;
}
return std::make_pair(true, ret);
}
unsigned add_metadata_entries(const std::string& key, const StringConverter& val, info_map_t& info)
{
unsigned num_entries = 0;
bool is_dict = false;
auto dict = val.rec_dict(&is_dict);
if (is_dict) {
std::string prefix = key + ".";
for (const auto& p : dict)
num_entries += add_metadata_entries(prefix + p.first, p.second, info);
} else {
info[key] = val.to_string();
++num_entries;
}
return num_entries;
}
void read_metadata_from_json_file(const std::string& filename, const std::string& keys, info_map_t& info)
{
std::ifstream is(filename.c_str(), std::ios::ate);
if (!is) {
Log(0).stream() << "read_metadata_from_json_file(): Cannot open file " << filename << ", quitting\n";
return;
}
auto size = is.tellg();
std::string str(size, '\0');
is.seekg(0);
if (!is.read(&str[0], size)) {
Log(0).stream() << "read_metadata_from_json_file(): Cannot read file " << filename << ", quitting\n";
return;
}
bool ok = false;
auto top = StringConverter(str).rec_dict(&ok);
if (!ok) {
Log(0).stream() << "read_metadata_from_json_file(): Cannot parse top-level dict in " << filename
<< ", quitting\n";
return;
}
auto keylist = StringConverter(keys).to_stringlist();
if (keylist.empty()) {
for (const auto& p : top)
keylist.push_back(p.first);
}
for (const std::string& key : keylist) {
std::vector<std::string> path = StringConverter(key).to_stringlist(".");
auto ret = find_key_in_json(path, top);
if (ret.first)
add_metadata_entries(key, ret.second, info);
else
Log(1).stream() << "read_metadata_from_json_file(): Key " << key << " not found\n";
}
}
void add_metadata(const std::string& args, info_map_t& info)
{
std::istringstream is(args);
auto arglist = parse_keyval_list(is);
auto it = std::find_if(arglist.begin(), arglist.end(), [](const std::pair<std::string, std::string>& p) {
return p.first == "file";
});
if (it != arglist.end()) {
std::string filename = it->second;
std::string keys;
it = std::find_if(arglist.begin(), arglist.end(), [](const std::pair<std::string, std::string>& p) {
return p.first == "keys";
});
if (it != arglist.end())
keys = it->second;
read_metadata_from_json_file(filename, keys, info);
} else {
for (const auto& p : arglist)
info[p.first] = p.second;
}
}
} // namespace
//
// --- OptionSpec
//
class ConfigManager::OptionSpec
{
struct query_arg_t {
std::vector<std::string> select;
std::vector<std::string> groupby;
std::vector<std::string> let;
std::vector<std::string> where;
std::vector<std::string> aggregate;
std::vector<std::string> orderby;
};
struct option_spec_t {
std::string type;
std::string description;
std::string category;
std::vector<std::string> services;
std::vector<std::string> inherited_specs;
std::map<std::string, query_arg_t> query_args;
std::map<std::string, std::string> config;
};
std::map<std::string, option_spec_t> data;
bool m_error;
std::string m_error_msg;
void set_error(const std::string& msg)
{
m_error = true;
m_error_msg = msg;
}
void parse_select(const std::vector<StringConverter>& list, query_arg_t& qarg)
{
for (const StringConverter& sc : list) {
bool is_a_dict = false;
std::map<std::string, StringConverter> dict = sc.rec_dict(&is_a_dict);
// The deprecated syntax for a select spec is a list of
// { "expr": "expression", "as": "alias", "unit": "unit" }
// dicts. The new syntax is just a list of
// "expression AS alias UNIT unit"
// strings. Determine which we have and parse accordingly.
if (is_a_dict) {
std::string str = dict["expr"].to_string();
auto it = dict.find("as");
if (it != dict.end()) {
str.append(" as \"");
str.append(it->second.to_string());
str.append("\"");
}
it = dict.find("unit");
if (it != dict.end()) {
str.append(" unit \"");
str.append(it->second.to_string());
str.append("\"");
}
qarg.select.push_back(str);
} else {
qarg.select.push_back(sc.to_string());
}
}
}
void parse_query_args(const std::vector<StringConverter>& list, option_spec_t& opt)
{
for (const StringConverter& sc : list) {
std::map<std::string, StringConverter> dict = sc.rec_dict();
query_arg_t qarg;
auto it = dict.find("group by");
if (it != dict.end())
qarg.groupby = ::to_stringlist(it->second.rec_list());
it = dict.find("let");
if (it != dict.end())
qarg.let = ::to_stringlist(it->second.rec_list());
it = dict.find("where");
if (it != dict.end())
qarg.where = ::to_stringlist(it->second.rec_list());
it = dict.find("aggregate");
if (it != dict.end())
qarg.aggregate = ::to_stringlist(it->second.rec_list());
it = dict.find("order by");
if (it != dict.end())
qarg.orderby = ::to_stringlist(it->second.rec_list());
it = dict.find("select");
if (it != dict.end())
parse_select(it->second.rec_list(), qarg);
it = dict.find("level");
if (it == dict.end()) {
set_error(": query arg: missing \"level\"");
continue;
}
opt.query_args[it->second.to_string()] = qarg;
}
}
void parse_config(const std::map<std::string, StringConverter>& dict, option_spec_t& opt)
{
for (auto p : dict)
opt.config[p.first] = p.second.to_string();
}
void parse_spec(const std::map<std::string, StringConverter>& dict)
{
option_spec_t opt;
bool ok = true;
auto it = dict.find("category");
if (it != dict.end())
opt.category = it->second.to_string();
it = dict.find("services");
if (it != dict.end())
opt.services = ::to_stringlist(it->second.rec_list(&ok));
it = dict.find("inherit");
if (ok && !m_error && it != dict.end())
opt.inherited_specs = ::to_stringlist(it->second.rec_list(&ok));
it = dict.find("config");
if (ok && !m_error && it != dict.end())
parse_config(it->second.rec_dict(&ok), opt);
it = dict.find("query");
if (ok && !m_error && it != dict.end())
parse_query_args(it->second.rec_list(&ok), opt);
it = dict.find("type");
if (ok && !m_error && it != dict.end())
opt.type = it->second.to_string();
it = dict.find("description");
if (ok && !m_error && it != dict.end())
opt.description = it->second.to_string();
it = dict.find("name");
if (it == dict.end()) {
set_error(": \"name\" missing");
return;
}
if (!ok)
set_error(": parse error");
if (!m_error)
data[it->second.to_string()] = opt;
}
std::vector<std::string> recursive_get_services_list(const std::string& cfg)
{
std::vector<std::string> ret;
auto it = data.find(cfg);
if (it == data.end())
return ret;
ret = it->second.services;
for (const std::string& s : it->second.inherited_specs) {
auto tmp = recursive_get_services_list(s);
ret.insert(ret.end(), tmp.begin(), tmp.end());
}
std::sort(ret.begin(), ret.end());
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
return ret;
}
public:
OptionSpec() : m_error(false) {}
OptionSpec(const OptionSpec&) = default;
OptionSpec(OptionSpec&&) = default;
OptionSpec& operator= (const OptionSpec&) = default;
OptionSpec& operator= (OptionSpec&&) = default;
~OptionSpec() {}
bool error() const { return m_error; }
std::string error_msg() const { return m_error_msg; }
void add(const OptionSpec& other, const std::vector<std::string>& categories)
{
for (const auto& p : other.data)
if (std::find(categories.begin(), categories.end(), p.second.category) != categories.end())
data.insert(p);
}
void add(const std::vector<StringConverter>& list)
{
if (m_error)
return;
for (auto& p : list) {
parse_spec(p.rec_dict());
if (m_error) {
m_error_msg = std::string("option spec: ") + util::clamp_string(p.to_string(), 32) + m_error_msg;
break;
}
}
}
bool contains(const std::string& name) const { return data.find(name) != data.end(); }
std::map<std::string, std::string> get_option_descriptions()
{
std::map<std::string, std::string> ret;
for (auto& p : data)
ret.insert(std::make_pair(p.first, p.second.description));
return ret;
}
// throw out options requiring unavailable services
void filter_unavailable_options(const std::vector<std::string>& in)
{
std::vector<std::string> available(in);
std::sort(available.begin(), available.end());
available.erase(std::unique(available.begin(), available.end()), available.end());
auto it = data.begin();
while (it != data.end()) {
auto required = recursive_get_services_list(it->first);
if (std::includes(available.begin(), available.end(), required.begin(), required.end()))
++it;
else
it = data.erase(it);
}
}
friend class ConfigManager::Options;
};
//
// --- Options
//
struct ConfigManager::Options::OptionsImpl {
OptionSpec spec;
arglist_t args;
std::vector<std::string> enabled_options;
std::string check() const
{
//
// Check if option value has the correct datatype
//
for (const auto& arg : args) {
auto it = spec.data.find(arg.first);
if (it == spec.data.end())
continue;
if (it->second.type == "bool") {
bool ok = false;
StringConverter(arg.second).to_bool(&ok);
if (!ok)
return std::string("Invalid value \"") + arg.second + std::string("\" for ") + arg.first;
}
}
//
// Check if the required services for all requested profiling options
// are there
//
add_submodule_controllers_and_services();
auto slist = services::get_available_services();
for (const std::string& opt : enabled_options) {
auto o_it = spec.data.find(opt);
if (o_it == spec.data.end())
continue;
for (const std::string& required_service : o_it->second.services)
if (std::find(slist.begin(), slist.end(), required_service) == slist.end())
return required_service + std::string(" service required for ") + o_it->first
+ std::string(" option is not available");
}
return "";
}
std::string services(const std::string& in)
{
std::vector<std::string> vec = StringConverter(in).to_stringlist();
for (const std::string& opt : enabled_options) {
auto it = spec.data.find(opt);
if (it == spec.data.end())
continue;
vec.insert(vec.end(), it->second.services.begin(), it->second.services.end());
}
// remove duplicates
std::sort(vec.begin(), vec.end());
auto last = std::unique(vec.begin(), vec.end());
vec.erase(last, vec.end());
return ::join_stringlist(vec);
}
void append_config(config_map_t& config)
{
for (const std::string& opt : enabled_options) {
auto spec_it = spec.data.find(opt);
if (spec_it == spec.data.end())
continue;
for (const auto& kv_p : spec_it->second.config) {
auto it = std::find_if(args.begin(), args.end(), [&opt](const std::pair<std::string, std::string>& p) {
return opt == p.first;
});
if (it != args.end())
// replace "{}" variable placeholders in spec with argument, if any
config[kv_p.first] = ::expand_variables(kv_p.second, it->second);
}
}
}
void update_channel_config(config_map_t& config)
{
config["CALI_SERVICES_ENABLE"] = services(config["CALI_SERVICES_ENABLE"]);
append_config(config);
}
void update_channel_metadata(info_map_t& info)
{
for (const auto& p : args) {
if (p.first == "metadata") {
::add_metadata(p.second, info);
} else {
std::string key = "opts:";
key.append(p.first);
info[key] = p.second;
}
}
}
std::string build_query(const char* level, const std::map<std::string, std::string>& in) const
{
std::string q_let = ::find_or(in, "let");
std::string q_select = ::find_or(in, "select");
std::string q_groupby = ::find_or(in, "group by");
std::string q_where = ::find_or(in, "where");
std::string q_aggregate = ::find_or(in, "aggregate");
std::string q_orderby = ::find_or(in, "order by");
std::string q_format = ::find_or(in, "format");
for (const std::string& opt : enabled_options) {
auto s_it = spec.data.find(opt);
if (s_it == spec.data.end())
continue;
auto l_it = s_it->second.query_args.find(level);
if (l_it != s_it->second.query_args.end()) {
const auto& q = l_it->second;
::join_stringlist(q_let, q.let);
::join_stringlist(q_select, q.select);
::join_stringlist(q_groupby, q.groupby);
::join_stringlist(q_where, q.where);
::join_stringlist(q_aggregate, q.aggregate);
::join_stringlist(q_orderby, q.orderby);
}
}
std::string ret;
if (!q_let.empty())
ret.append(" let ").append(q_let);
if (!q_select.empty())
ret.append(" select ").append(q_select);
if (!q_groupby.empty())
ret.append(" group by ").append(q_groupby);
if (!q_where.empty())
ret.append(" where ").append(q_where);
if (!q_aggregate.empty())
ret.append(" aggregate ").append(q_aggregate);
if (!q_orderby.empty())
ret.append(" order by ").append(q_orderby);
if (!q_format.empty())
ret.append(" format ").append(q_format);
return ret;
}
std::vector<std::string> get_inherited_specs(const std::string& name)
{
std::vector<std::string> ret;
auto it = spec.data.find(name);
if (it == spec.data.end())
return ret;
for (const std::string& inh : it->second.inherited_specs) {
auto tmp = get_inherited_specs(inh);
ret.insert(ret.end(), tmp.begin(), tmp.end());
ret.push_back(inh);
}
return ret;
}
void find_enabled_options()
{
std::vector<std::string> vec;
for (const auto& argp : args) {
auto s_it = spec.data.find(argp.first);
if (s_it == spec.data.end())
continue;
// Non-boolean options are enabled if they are present in args.
// For boolean options, check if they are set to false or true.
bool enabled = true;
if (s_it->second.type == "bool")
enabled = StringConverter(argp.second).to_bool();
if (enabled) {
vec.push_back(argp.first);
auto tmp = get_inherited_specs(argp.first);
vec.insert(vec.end(), tmp.begin(), tmp.end());
}
}
// make sure there are no duplicates, but keep entries in input order
std::vector<std::string> ret;
ret.reserve(vec.size());
for (auto it = vec.begin(); it != vec.end(); ++it) {
if (std::find(ret.begin(), ret.end(), *it) == ret.end())
ret.emplace_back(std::move(*it));
}
enabled_options = std::move(ret);
}
OptionsImpl(const OptionSpec& s, const arglist_t& a) : spec(s), args(a) { find_enabled_options(); }
};
ConfigManager::Options::Options(const OptionSpec& spec, const arglist_t& args) : mP(new OptionsImpl(spec, args))
{}
ConfigManager::Options::~Options()
{
mP.reset();
}
bool ConfigManager::Options::is_set(const char* option) const
{
std::string ostr(option);
return std::find_if(
mP->args.begin(),
mP->args.end(),
[&ostr](const std::pair<std::string, std::string>& p) { return ostr == p.first; }
)
!= mP->args.end();
}
bool ConfigManager::Options::is_enabled(const char* option) const
{
return std::find(mP->enabled_options.begin(), mP->enabled_options.end(), std::string(option))
!= mP->enabled_options.end();
}
std::vector<std::string> ConfigManager::Options::enabled_options() const
{
std::vector<std::string> ret;
for (const auto& s : mP->enabled_options) {
auto s_it = mP->spec.data.find(s);
if (s_it == mP->spec.data.end())
continue;
if (s_it->second.type == "bool")
ret.push_back(s);
}
return ret;
}
std::string ConfigManager::Options::get(const char* option, const char* default_val) const
{
std::string ostr(option);
auto it = std::find_if(mP->args.begin(), mP->args.end(), [&ostr](const std::pair<std::string, std::string>& p) {
return ostr == p.first;
});
if (it != mP->args.end())
return it->second;
return std::string(default_val);
}
std::string ConfigManager::Options::check() const
{
return mP->check();
}
void ConfigManager::Options::update_channel_config(config_map_t& config) const
{
mP->update_channel_config(config);
}
void ConfigManager::Options::update_channel_metadata(info_map_t& metadata) const
{
mP->update_channel_metadata(metadata);
}
std::string ConfigManager::Options::build_query(const char* level, const std::map<std::string, std::string>& in) const
{
return mP->build_query(level, in);
}
//
// --- ConfigManager
//
struct ConfigManager::ConfigManagerImpl {
ChannelList m_channels;
bool m_error = false;
std::string m_error_msg = "";
std::vector<const char*> builtin_option_specs_list;
std::map<std::string, arglist_t> m_default_parameters_for_spec;
arglist_t m_default_parameters;
argmap_t m_extra_vars;
OptionSpec m_global_opts;
struct config_spec_t {
std::string json; // the json spec
CreateConfigFn create;
CheckArgsFn check_args;
std::string name;
std::vector<std::string> categories;
std::string description;
config_map_t initial_cfg;
OptionSpec opts;
arglist_t defaults; // default options, if any
};
std::map<std::string, std::shared_ptr<config_spec_t>> m_spec;
void set_error(const std::string& msg)
{
m_error = true;
m_error_msg = msg;
}
void set_error(const std::string& msg, std::istream& is)
{
m_error = true;
m_error_msg = msg;
size_t maxctx = 16;
if (is.good()) {
is.unget();
m_error_msg.append(" at ");
for (char c = is.get(); is.good() && --maxctx > 0; c = is.get())
m_error_msg.push_back(c);
if (maxctx == 0)
m_error_msg.append("...");
}
}
// sets error if err is set
void check_error(const std::string& err)
{
if (err.empty())
return;
set_error(err);
}
void add_config_spec(const char* jsonspec, CreateConfigFn create, CheckArgsFn check, bool ignore_existing)
{
config_spec_t spec;
bool ok = false;
spec.json = jsonspec;
spec.create = (create == nullptr) ? ::make_basic_channel_controller : create;
spec.check_args = check;
auto dict = StringConverter(spec.json).rec_dict(&ok);
if (!ok) {
set_error(std::string("spec parse error: ") + util::clamp_string(spec.json, 48));
return;
}
auto it = dict.find("name");
if (it == dict.end()) {
set_error(std::string("'name' missing in spec: ") + util::clamp_string(spec.json, 48));
return;
}
spec.name = it->second.to_string();
// check if the spec already exists
if (m_spec.count(spec.name) > 0) {
if (!ignore_existing)
set_error(spec.name + std::string(" already exists"));
return;
}
std::vector<std::string> cfg_srvcs;
it = dict.find("services");
if (ok && !m_error && it != dict.end())
cfg_srvcs = ::to_stringlist(it->second.rec_list(&ok));
services::add_default_service_specs();
auto slist = services::get_available_services();
bool have_all_services = true;
for (const auto& s : cfg_srvcs)
if (std::find(slist.begin(), slist.end(), s) == slist.end())
have_all_services = false;
if (!have_all_services)
return;
it = dict.find("categories");
if (ok && !m_error && it != dict.end())
spec.categories = ::to_stringlist(it->second.rec_list(&ok));
it = dict.find("description");
if (ok && !m_error && it != dict.end())
spec.description = it->second.to_string();
it = dict.find("options");
if (ok && !m_error && it != dict.end())
spec.opts.add(it->second.rec_list(&ok));
it = dict.find("config");
if (ok && !m_error && it != dict.end())
for (const auto& p : it->second.rec_dict(&ok))
spec.initial_cfg[p.first] = p.second.to_string();
if (cfg_srvcs.size() > 0)
spec.initial_cfg["CALI_SERVICES_ENABLE"].append(::join_stringlist(cfg_srvcs));
it = dict.find("defaults");
if (ok && !m_error && it != dict.end())
for (const auto& p : it->second.rec_dict(&ok))