-
Notifications
You must be signed in to change notification settings - Fork 35
/
settings.cpp
2423 lines (2199 loc) · 101 KB
/
settings.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
// MIT License
//
// Copyright (c) 2020, The Regents of the University of California,
// through Lawrence Berkeley National Laboratory (subject to receipt of any
// required approvals from the U.S. Dept. of Energy). All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef TIMEMORY_SETTINGS_SETTINGS_CPP_
#define TIMEMORY_SETTINGS_SETTINGS_CPP_ 1
#include "timemory/settings/settings.hpp"
#include "timemory/backends/dmp.hpp"
#include "timemory/backends/process.hpp"
#include "timemory/defines.h"
#include "timemory/mpl/policy.hpp"
#include "timemory/settings/macros.hpp"
#include "timemory/settings/types.hpp"
#include "timemory/tpls/cereal/archives.hpp"
#include "timemory/utility/argparse.hpp"
#include "timemory/utility/bits/signals.hpp"
#include "timemory/utility/declaration.hpp"
#include "timemory/utility/filepath.hpp"
#include "timemory/utility/md5.hpp"
#include "timemory/utility/types.hpp"
#include "timemory/variadic/macros.hpp"
#include <cctype>
#include <exception>
#include <fstream>
#include <initializer_list>
#include <iterator>
#include <locale>
#include <regex>
#include <string>
namespace tim
{
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::shared_ptr<settings>
settings::shared_instance()
{
// do not take reference to ensure push/pop w/o template parameters do not change
// the settings
static auto _instance = shared_instance<TIMEMORY_API>();
return _instance;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings*
settings::instance()
{
// do not take reference to ensure push/pop w/o template parameters do not change
// the settings
static auto _instance = shared_instance();
return _instance.get();
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings::strvector_t&
settings::command_line()
{
return instance()->get_command_line();
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings::strvector_t&
settings::environment()
{
return instance()->get_environment();
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings::strvector_t
settings::get_global_environment()
{
#if defined(TIMEMORY_UNIX)
strvector_t _environ;
if(environ != nullptr)
{
int idx = 0;
while(environ[idx] != nullptr)
_environ.push_back(environ[idx++]);
}
return _environ;
#else
return std::vector<std::string>();
#endif
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
get_local_datetime(const char* dt_format, std::time_t* dt_curr)
{
char mbstr[512];
if(!dt_curr)
dt_curr = settings::get_launch_time(TIMEMORY_API{});
if(std::strftime(mbstr, sizeof(mbstr), dt_format, std::localtime(dt_curr)) != 0)
return std::string{ mbstr };
return std::string{};
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::tolower(std::string str)
{
for(auto& itr : str)
itr = ::tolower(itr);
return str;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::toupper(std::string str)
{
for(auto& itr : str)
itr = ::toupper(itr);
return str;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::get_global_input_prefix()
{
auto _dir = input_path();
auto _prefix = input_prefix();
return filepath::osrepr(_dir + std::string("/") + _prefix);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::get_global_output_prefix(bool _make_dir)
{
using str_t = std::string;
auto _out_path =
get_with_env_fallback<str_t>(TIMEMORY_SETTINGS_KEY("OUTPUT_PATH"), ".");
auto _out_prefix =
get_with_env_fallback<str_t>(TIMEMORY_SETTINGS_KEY("OUTPUT_PREFIX"), "");
auto _time_format =
get_with_env_fallback<str_t>(TIMEMORY_SETTINGS_KEY("TIME_FORMAT"), "%F_%I.%M_%p");
auto _time_output =
get_with_env_fallback<bool>(TIMEMORY_SETTINGS_KEY("TIME_OUTPUT"), false);
if(_time_output)
{
// get the statically stored launch time
auto* _launch_time = get_launch_time(TIMEMORY_API{});
auto _local_datetime = get_local_datetime(_time_format.c_str(), _launch_time);
if(_out_path.find(_local_datetime) == std::string::npos)
{
if(_out_path.length() > 0 && _out_path[_out_path.length() - 1] != '/')
_out_path += "/";
_out_path += _local_datetime;
}
}
// always return zero if not making dir. if makedir failed, don't prefix with
// directory
auto ret = (_make_dir) ? makedir(_out_path) : 0;
return (ret == 0) ? filepath::osrepr(_out_path + std::string("/") + _out_prefix)
: filepath::osrepr(std::string("./") + _out_prefix);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
void
settings::store_command_line(int argc, char** argv)
{
auto& _v = command_line();
_v.clear();
for(int i = 0; i < argc; ++i)
{
_v.emplace_back(std::string(argv[i]));
}
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE std::vector<std::pair<std::string, std::string>>
settings::output_keys(const std::string& _tag)
{
using strpair_t = std::pair<std::string, std::string>;
auto _cmdline = command_line();
std::string _arg0_string = {}; // only the first cmdline arg
std::string _argv_string = {}; // entire argv cmd
std::string _args_string = {}; // cmdline args
std::string _argt_string = _tag; // prefix + cmdline args
std::string _tag0_string = _tag; // only the basic prefix
auto _options = std::vector<strpair_t>{};
auto _replace = [](auto& _v, const strpair_t& itr) {
auto pos = std::string::npos;
while((pos = _v.find(itr.first)) != std::string::npos)
_v.replace(pos, itr.first.length(), itr.second);
};
if(_cmdline.size() > 1 && _cmdline.at(1) == "--")
_cmdline.erase(_cmdline.begin() + 1);
for(auto& itr : _cmdline)
{
itr = argparse::helpers::trim(itr);
_replace(itr, { "/", "_" });
while(!itr.empty() && itr.at(0) == '.')
itr = itr.substr(1);
while(!itr.empty() && itr.at(0) == '_')
itr = itr.substr(1);
}
if(!_cmdline.empty())
{
_arg0_string += _cmdline.at(0);
_argv_string += _cmdline.at(0);
for(size_t i = 1; i < _cmdline.size(); ++i)
{
const auto _l = std::string{ "_" };
auto _v = _cmdline.at(i);
_argv_string += _l + _v;
_argt_string += _l + _v;
_args_string += _l + _v;
_options.emplace_back(TIMEMORY_JOIN("", "%arg", i, "%"), _v);
_options.emplace_back(TIMEMORY_JOIN("", "%arg", i, "_hash%"),
md5::compute_md5(_v));
}
}
auto* _launch_time = get_launch_time(TIMEMORY_API{});
auto _time_format = get_with_env_fallback<std::string>(
TIMEMORY_SETTINGS_KEY("TIME_FORMAT"), "%F_%I.%M_%p");
auto _dmp_size = TIMEMORY_JOIN("", dmp::size());
auto _dmp_rank = TIMEMORY_JOIN("", dmp::rank());
auto _proc_id = TIMEMORY_JOIN("", process::get_id());
auto _pwd_string = get_env<std::string>("PWD", ".", false);
auto _slurm_job_id = get_env<std::string>("SLURM_JOB_ID", "0", false);
auto _slurm_proc_id = get_env<std::string>("SLURM_PROCID", _dmp_rank, false);
auto _launch_string = get_local_datetime(_time_format.c_str(), _launch_time);
#if defined(TIMEMORY_WINDOWS)
auto _parent_id = TIMEMORY_JOIN("", process::get_id());
#else
auto _parent_id = TIMEMORY_JOIN("", getppid());
#endif
using strpairinit_t = std::initializer_list<std::pair<std::string, std::string>>;
for(auto&& itr : strpairinit_t{
{ "%arg0%", _arg0_string },
{ "%arg0_hash%", md5::compute_md5(_arg0_string) },
{ "%argv%", _argv_string },
{ "%argv_hash%", md5::compute_md5(_argv_string) },
{ "%argt%", _argt_string },
{ "%argt_hash%", md5::compute_md5(_argt_string) },
{ "%args%", _args_string },
{ "%args_hash%", md5::compute_md5(_args_string) },
{ "%tag%", _tag0_string },
{ "%tag_hash%", md5::compute_md5(_tag0_string) },
{ "%pid%", _proc_id },
{ "%ppid%", _parent_id },
{ "%job%", _slurm_job_id },
{ "%rank%", _slurm_proc_id },
{ "%size%", _dmp_size },
{ "%launch_time%", _launch_string },
{ "%m", md5::compute_md5(_argt_string) },
{ "%p", _proc_id },
{ "%j", _slurm_job_id },
{ "%r", _slurm_proc_id },
{ "%s", _dmp_size },
})
{
_options.emplace_back(itr);
}
return _options;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE std::string
settings::format(std::string _fpath, const std::string& _tag)
{
using strpair_t = std::pair<std::string, std::string>;
auto _replace = [](auto& _v, const strpair_t& itr) {
auto pos = std::string::npos;
while((pos = _v.find(itr.first)) != std::string::npos)
_v.replace(pos, itr.first.length(), itr.second);
};
_fpath = filepath::canonical(_fpath);
if(_fpath.find('%') == std::string::npos)
return _fpath;
for(auto&& itr : output_keys(_tag))
_replace(_fpath, itr);
auto _verbose =
get_with_env_fallback(TIMEMORY_SETTINGS_KEY("VERBOSE"), 0) +
(get_with_env_fallback(TIMEMORY_SETTINGS_KEY("DEBUG"), false) ? 16 : 0);
// environment and configuration variables
try
{
for(auto _expr : { std::string{ "(.*)%(env|ENV)\\{([A-Z0-9_]+)\\}%(.*)" },
std::string{ "(.*)\\$(env|ENV)\\{([A-Z0-9_]+)\\}(.*)" },
std::string{ "(.*)%(cfg|CFG)\\{([A-Z0-9_]+)\\}%(.*)" },
std::string{ "(.*)\\$(cfg|CFG)\\{([A-Z0-9_]+)\\}(.*)" } })
{
std::regex _re{ _expr };
std::string _cbeg = (_expr.find("(.*)%") == 0) ? "%" : "$";
std::string _cend = (_expr.find("(.*)%") == 0) ? "}%" : "}";
bool _is_env = (_expr.find("(env|ENV)")) ? true : false;
_cbeg += (_is_env) ? "env{" : "cfg{";
while(std::regex_search(_fpath, _re))
{
auto _var = std::regex_replace(_fpath, _re, "$3");
std::string _val = {};
if(_is_env)
{
_val = get_env<std::string>(_var, "");
}
else
{
auto _settings = shared_instance();
if(_settings)
{
auto _cfg = _settings->find(_var);
if(_cfg != _settings->end())
{
_val = _cfg->second->as_string();
_replace(_val, strpair_t{ ",", "-" });
}
}
}
if(_verbose >= 1 && _val.empty())
fprintf(stderr,
"[%s][settings][%s] '%s' not found! Removing '%s%s%s' from "
"'%s'...\n",
TIMEMORY_PROJECT_NAME, __FUNCTION__, _var.c_str(),
_cbeg.c_str(), _var.c_str(), _cend.c_str(), _fpath.c_str());
else if(_verbose >= 4 && !_val.empty())
fprintf(
stderr,
"[%s][settings][%s] replacing '%s%s%s' in '%s' with '%s'...\n",
TIMEMORY_PROJECT_NAME, __FUNCTION__, _cbeg.c_str(), _var.c_str(),
_cend.c_str(), _fpath.c_str(), _val.c_str());
auto _beg = std::regex_replace(_fpath, _re, "$1");
auto _end = std::regex_replace(_fpath, _re, "$4");
_fpath = _beg + _val + _end;
if(_verbose >= 3)
fprintf(stderr,
"[%s][settings][%s] replacing '%s%s%s' resulted in '%s'...\n",
TIMEMORY_PROJECT_NAME, __FUNCTION__, _cbeg.c_str(),
_var.c_str(), _cend.c_str(), _fpath.c_str());
}
}
} catch(std::exception& _e)
{
TIMEMORY_PRINT_HERE("Warning! settings::%s throw exception :: %s", __FUNCTION__,
_e.what());
}
// remove %arg<N>% and %arg<N>_hash% where N >= argc
try
{
std::regex _re{ "(.*)%(arg[0-9]+|arg[0-9]+_hash)%([-/_]*)(.*)" };
while(std::regex_search(_fpath, _re))
_fpath = std::regex_replace(_fpath, _re, "$1$4");
} catch(std::exception& _e)
{
TIMEMORY_PRINT_HERE("Warning! settings::%s threw exception :: %s", __FUNCTION__,
_e.what());
}
return _fpath;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::format(std::string _prefix, std::string _tag, std::string _suffix,
std::string _ext)
{
// add period before extension
if(_ext.find('.') != 0)
_ext = std::string(".") + _ext;
// if the tag contains the extension, remove it
auto _ext_pos = _tag.length() - _ext.length();
if(_tag.find(_ext) == _ext_pos)
_tag = _tag.substr(0, _ext_pos);
auto plast = static_cast<intmax_t>(_prefix.length()) - 1;
// add dash if not empty, not ends in '/', and last char is alphanumeric
if(!_prefix.empty() && _prefix[plast] != '/' && isalnum(_prefix[plast]) != 0)
_prefix += "-";
settings* _instance = instance();
std::string _global_tag = {};
if(_instance)
_global_tag = _instance->get_tag();
else
_global_tag = get_fallback_tag();
return format(_prefix + _tag + std::move(_suffix) + _ext, _global_tag);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::compose_output_filename(std::string _tag, std::string _ext, bool _use_suffix,
int32_t _output_suffix, bool _make_dir,
std::string _explicit)
{
bool _is_explicit = (!_explicit.empty());
// if there isn't an explicit prefix, get the <OUTPUT_PATH>/<OUTPUT_PREFIX>
auto _prefix =
(!_explicit.empty()) ? std::move(_explicit) : get_global_output_prefix(_make_dir);
// return on empty
if(_prefix.empty())
return "";
auto only_ascii = [](char c) { return isascii(c) == 0; };
_prefix.erase(std::remove_if(_prefix.begin(), _prefix.end(), only_ascii),
_prefix.end());
// if explicit prefix is provided, then make the directory
if(_is_explicit && _make_dir)
{
auto ret = makedir(_prefix);
if(ret != 0)
_prefix = filepath::osrepr(std::string("./"));
}
// add the mpi rank if not root
auto _suffix = (_use_suffix && _output_suffix >= 0)
? (std::string("-") + std::to_string(_output_suffix))
: std::string("");
// create the path
std::string _fpath = format(_prefix, std::move(_tag), _suffix, std::move(_ext));
return filepath::osrepr(_fpath);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::compose_input_filename(std::string _tag, std::string _ext, bool _use_suffix,
int32_t _output_suffix, std::string _explicit)
{
if(settings::input_path().empty())
settings::input_path() = settings::output_path();
if(settings::input_prefix().empty())
settings::input_prefix() = settings::output_prefix();
auto _prefix =
(_explicit.length() > 0) ? std::move(_explicit) : get_global_input_prefix();
auto only_ascii = [](char c) { return isascii(c) == 0; };
_prefix.erase(std::remove_if(_prefix.begin(), _prefix.end(), only_ascii),
_prefix.end());
auto _suffix = (_use_suffix && _output_suffix >= 0)
? (std::string("-") + std::to_string(_output_suffix))
: std::string("");
// create the path
std::string _fpath = format(_prefix, std::move(_tag), _suffix, std::move(_ext));
return filepath::osrepr(_fpath);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
void
settings::parse(const std::shared_ptr<settings>& _settings)
{
if(_settings)
parse(_settings.get());
}
//
//--------------------------------------------------------------------------------------//
//
// function to parse the environment for settings
//
// Nearly all variables will parse env when first access but this allows provides a
// way to reparse the environment so that default settings (possibly from previous
// invocation) can be overwritten
//
TIMEMORY_SETTINGS_INLINE
void
settings::parse(settings* _settings)
{
if(!_settings)
{
TIMEMORY_PRINT_HERE("%s", "nullptr to tim::settings");
return;
}
if(_settings->get_suppress_parsing())
{
static auto _once = false;
if(!_once)
{
TIMEMORY_PRINT_HERE("%s", "settings parsing has been suppressed");
_once = true;
}
return;
}
for(const auto& itr : *_settings)
{
itr.second->parse();
}
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings::settings()
: m_data(data_type{})
{
initialize();
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings::settings(const settings& rhs)
: m_data(data_type{})
, m_order(rhs.m_order)
, m_command_line(rhs.m_command_line)
, m_environment(rhs.m_environment)
{
for(const auto& itr : rhs.m_data)
m_data.emplace(itr.first, itr.second->clone());
for(auto& itr : m_order)
{
if(m_data.find(itr) == m_data.end())
{
auto ritr = rhs.m_data.find(itr);
if(ritr == rhs.m_data.end())
{
TIMEMORY_EXCEPTION(string_t("Error! Missing ordered entry: ") + itr)
}
else
{
m_data.emplace(itr, ritr->second->clone());
}
}
}
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
settings&
settings::operator=(const settings& rhs)
{
if(this == &rhs)
return *this;
for(const auto& itr : rhs.m_data)
m_data[itr.first] = itr.second->clone();
m_order = rhs.m_order;
m_command_line = rhs.m_command_line;
m_environment = rhs.m_environment;
for(auto& itr : m_order)
{
if(m_data.find(itr) == m_data.end())
{
auto ritr = rhs.m_data.find(itr);
if(ritr == rhs.m_data.end())
{
TIMEMORY_EXCEPTION(string_t("Error! Missing ordered entry: ") + itr)
}
else
{
m_data.emplace(itr, ritr->second->clone());
}
}
}
return *this;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::get_fallback_tag()
{
std::string _tag = {};
if(command_line().empty())
command_line() = read_command_line(process::get_id());
if(command_line().empty())
{
_tag = std::string{ TIMEMORY_SETTINGS_PREFIX };
auto _pos = std::string::npos;
while((_pos = _tag.find_last_of('_')) == _tag.length() - 1)
_tag = _tag.substr(0, _tag.length() - 1);
return _tag;
}
_tag = command_line().front();
while(_tag.find('\\') != std::string::npos)
_tag = _tag.substr(_tag.find_last_of('\\') + 1);
while(_tag.find('/') != std::string::npos)
_tag = _tag.substr(_tag.find_last_of('/') + 1);
for(auto&& itr : { std::string{ ".py" }, std::string{ ".exe" } })
{
if(_tag.find(itr) != std::string::npos)
_tag.erase(_tag.find(itr), itr.length() + 1);
}
return _tag;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::string
settings::get_tag() const
{
if(m_tag.empty())
{
if(command_line().empty() && !m_command_line.empty())
command_line() = m_command_line;
const_cast<settings*>(this)->m_tag = get_fallback_tag();
}
return m_tag;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
bool
settings::disable(string_view_cref_t _key, bool _exact)
{
auto itr = find(_key.data(), _exact);
if(itr != m_data.end() && itr->second)
{
itr->second->set_enabled(false);
return true;
}
return false;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::set<std::string>
settings::disable_category(string_view_cref_t _category)
{
std::set<std::string> _v{};
for(auto&& itr : m_data)
{
if(itr.second->matches(".*", _category.data()))
{
itr.second->set_enabled(false);
_v.emplace(itr.first);
}
}
return _v;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
bool
settings::enable(string_view_cref_t _key, bool _exact)
{
auto itr = find(_key.data(), _exact);
if(itr != m_data.end() && itr->second)
{
itr->second->set_enabled(true);
return true;
}
return false;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
std::set<std::string>
settings::enable_category(string_view_cref_t _category)
{
std::set<std::string> _v{};
for(auto&& itr : m_data)
{
if(itr.second->matches(".*", _category.data()))
{
itr.second->set_enabled(true);
_v.emplace(itr.first);
}
}
return _v;
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
void
settings::initialize_core()
{
const auto* homedir = "%env{HOME}%";
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, config_file, TIMEMORY_SETTINGS_KEY("CONFIG_FILE"),
"Configuration file for " TIMEMORY_PROJECT_NAME,
TIMEMORY_JOIN(';', TIMEMORY_JOIN('/', homedir, "." TIMEMORY_PROJECT_NAME ".cfg"),
TIMEMORY_JOIN('/', homedir, "." TIMEMORY_PROJECT_NAME ".json")),
TIMEMORY_ESC(strset_t{ "native", "core", "config" }),
strvector_t({ "-C", "--" TIMEMORY_PROJECT_NAME "-config" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, suppress_config, TIMEMORY_SETTINGS_KEY("SUPPRESS_CONFIG"),
"Disable processing of setting configuration files", false,
TIMEMORY_ESC(strset_t{ "native", "core", "config" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-suppress-config",
"--" TIMEMORY_PROJECT_NAME "-no-config" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, strict_config, TIMEMORY_SETTINGS_KEY("STRICT_CONFIG"),
"Throw errors for unknown setting names in configuration files instead of "
"emitting a warning",
true, TIMEMORY_ESC(strset_t{ "native", "core", "config" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-strict-config" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, suppress_parsing, TIMEMORY_SETTINGS_KEY("SUPPRESS_PARSING"),
"Disable parsing environment", false,
TIMEMORY_ESC(strset_t{ "native", "core", "config" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-suppress-parsing" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, enabled, TIMEMORY_SETTINGS_KEY("ENABLED"), "Activation state of timemory",
TIMEMORY_DEFAULT_ENABLED, TIMEMORY_ESC(strset_t{ "native", "core" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-enabled" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
int, verbose, TIMEMORY_SETTINGS_KEY("VERBOSE"), "Verbosity level", 0,
TIMEMORY_ESC(strset_t{ "native", "core", "debugging" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-verbose" }), 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, debug, TIMEMORY_SETTINGS_KEY("DEBUG"), "Enable debug output", false,
TIMEMORY_ESC(strset_t{ "native", "core", "debugging" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-debug" }), -1, 1);
TIMEMORY_SETTINGS_REFERENCE_ARG_IMPL(
bool, flat_profile, TIMEMORY_SETTINGS_KEY("FLAT_PROFILE"),
"Set the label hierarchy mode to default to flat",
scope::get_fields()[scope::flat::value],
TIMEMORY_ESC(strset_t{ "native", "core", "data", "data_layout" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-flat-profile" }), -1, 1);
TIMEMORY_SETTINGS_REFERENCE_ARG_IMPL(
bool, timeline_profile, TIMEMORY_SETTINGS_KEY("TIMELINE_PROFILE"),
"Set the label hierarchy mode to default to timeline",
scope::get_fields()[scope::timeline::value],
TIMEMORY_ESC(strset_t{ "native", "core", "data", "data_layout" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-timeline-profile" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
uint16_t, max_depth, TIMEMORY_SETTINGS_KEY("MAX_DEPTH"),
"Set the maximum depth of label hierarchy reporting",
std::numeric_limits<uint16_t>::max(),
TIMEMORY_ESC(strset_t{ "native", "core", "data" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-max-depth" }), 1);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
void
settings::initialize_components()
{
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, global_components, TIMEMORY_SETTINGS_KEY("GLOBAL_COMPONENTS"),
"A specification of components which is used by multiple variadic bundlers and "
"user_bundles as the fall-back set of components if their specific variable is "
"not set. E.g. user_mpip_bundle will use this if MPIP_COMPONENTS is not "
"specified",
"", TIMEMORY_ESC(strset_t{ "native", "component" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-global-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, ompt_components, TIMEMORY_SETTINGS_KEY("OMPT_COMPONENTS"),
"A specification of components which will be added to structures containing "
"the 'user_ompt_bundle'. Priority: TRACE_COMPONENTS -> PROFILER_COMPONENTS -> "
"COMPONENTS -> GLOBAL_COMPONENTS",
"", TIMEMORY_ESC(strset_t{ "native", "component", "ompt", "gotcha" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-ompt-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, mpip_components, TIMEMORY_SETTINGS_KEY("MPIP_COMPONENTS"),
"A specification of components which will be added to structures containing "
"the 'user_mpip_bundle'. Priority: TRACE_COMPONENTS -> PROFILER_COMPONENTS -> "
"COMPONENTS -> GLOBAL_COMPONENTS",
"", TIMEMORY_ESC(strset_t{ "native", "component", "mpip", "gotcha" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-mpip-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, ncclp_components, TIMEMORY_SETTINGS_KEY("NCCLP_COMPONENTS"),
"A specification of components which will be added to structures containing "
"the 'user_ncclp_bundle'. Priority: MPIP_COMPONENTS -> TRACE_COMPONENTS -> "
"PROFILER_COMPONENTS -> COMPONENTS -> GLOBAL_COMPONENTS",
"", TIMEMORY_ESC(strset_t{ "native", "component", "ncclp", "gotcha" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-ncclp-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, trace_components, TIMEMORY_SETTINGS_KEY("TRACE_COMPONENTS"),
"A specification of components which will be used by the interfaces which "
"are designed for full profiling. These components will be subjected to "
"throttling. "
"Priority: COMPONENTS -> GLOBAL_COMPONENTS",
"", TIMEMORY_ESC(strset_t{ "native", "component" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-trace-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, profiler_components, TIMEMORY_SETTINGS_KEY("PROFILER_COMPONENTS"),
"A specification of components which will be used by the interfaces which "
"are designed for full python profiling. This specification will be overridden "
"by a trace_components specification. Priority: COMPONENTS -> GLOBAL_COMPONENTS",
"", TIMEMORY_ESC(strset_t{ "native", "component" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-profiler-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, kokkos_components, TIMEMORY_SETTINGS_KEY("KOKKOS_COMPONENTS"),
"A specification of components which will be used by the interfaces which "
"are designed for kokkos profiling. Priority: TRACE_COMPONENTS -> "
"PROFILER_COMPONENTS -> COMPONENTS -> GLOBAL_COMPONENTS",
"", TIMEMORY_ESC(strset_t{ "native", "component" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-kokkos-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, components, TIMEMORY_SETTINGS_KEY("COMPONENTS"),
"A specification of components which is used by the library interface. This "
"falls back to GLOBAL_COMPONENTS.",
"", TIMEMORY_ESC(strset_t{ "native", "component" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-components" }));
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
std::string, network_interface, TIMEMORY_SETTINGS_KEY("NETWORK_INTERFACE"),
"Default network interface", std::string{},
TIMEMORY_ESC(strset_t{ "native", "component" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-network-interface" }), -1, 1);
}
//
//--------------------------------------------------------------------------------------//
//
TIMEMORY_SETTINGS_INLINE
void
settings::initialize_io()
{
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, auto_output, TIMEMORY_SETTINGS_KEY("AUTO_OUTPUT"),
"Generate output at application termination", true,
TIMEMORY_ESC(strset_t{ "native", "io" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-auto-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, cout_output, TIMEMORY_SETTINGS_KEY("COUT_OUTPUT"), "Write output to stdout",
true, TIMEMORY_ESC(strset_t{ "native", "io", "console" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-cout-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, file_output, TIMEMORY_SETTINGS_KEY("FILE_OUTPUT"), "Write output to files",
true, TIMEMORY_ESC(strset_t{ "native", "io" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-file-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, text_output, TIMEMORY_SETTINGS_KEY("TEXT_OUTPUT"),
"Write text output files", true, TIMEMORY_ESC(strset_t{ "native", "io", "text" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-text-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, json_output, TIMEMORY_SETTINGS_KEY("JSON_OUTPUT"),
"Write json output files", true, TIMEMORY_ESC(strset_t{ "native", "io", "json" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-json-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, tree_output, TIMEMORY_SETTINGS_KEY("TREE_OUTPUT"),
"Write hierarchical json output files", true,
TIMEMORY_ESC(strset_t{ "native", "io", "json" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-tree-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, dart_output, TIMEMORY_SETTINGS_KEY("DART_OUTPUT"),
"Write dart measurements for CDash", false,
TIMEMORY_ESC(strset_t{ "native", "io", "dart", "cdash", "console" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-dart-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, time_output, TIMEMORY_SETTINGS_KEY("TIME_OUTPUT"),
"Output data to subfolder w/ a timestamp (see also: TIME_FORMAT)", false,
TIMEMORY_ESC(strset_t{ "native", "io", "filename" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-time-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, plot_output, TIMEMORY_SETTINGS_KEY("PLOT_OUTPUT"),
"Generate plot outputs from json outputs", TIMEMORY_DEFAULT_PLOTTING,
TIMEMORY_ESC(strset_t{ "native", "io", "plotting" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-plot-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, diff_output, TIMEMORY_SETTINGS_KEY("DIFF_OUTPUT"),
"Generate a difference output vs. a pre-existing output (see also: "
"INPUT_PATH and INPUT_PREFIX)",
false, TIMEMORY_ESC(strset_t{ "native", "io" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-diff-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, flamegraph_output, TIMEMORY_SETTINGS_KEY("FLAMEGRAPH_OUTPUT"),
"Write a json output for flamegraph visualization (use chrome://tracing)", true,
TIMEMORY_ESC(strset_t{ "native", "io", "flamegraph", "json" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-flamegraph-output" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
bool, ctest_notes, TIMEMORY_SETTINGS_KEY("CTEST_NOTES"),
"Write a CTestNotes.txt for each text output", false,
TIMEMORY_ESC(strset_t{ "native", "io", "ctest" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-ctest-notes" }), -1, 1);
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, output_path, TIMEMORY_SETTINGS_KEY("OUTPUT_PATH"),
"Explicitly specify the output folder for results",
TIMEMORY_PROJECT_NAME "-%tag%-output",
TIMEMORY_ESC(strset_t{ "native", "io", "filename" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-output-path" }),
1); // folder
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, output_prefix, TIMEMORY_SETTINGS_KEY("OUTPUT_PREFIX"),
"Explicitly specify a prefix for all output files", "",
TIMEMORY_ESC(strset_t{ "native", "io", "filename" }),
strvector_t({ "--" TIMEMORY_PROJECT_NAME "-output-prefix" }),
1); // file prefix
TIMEMORY_SETTINGS_MEMBER_ARG_IMPL(
string_t, input_path, TIMEMORY_SETTINGS_KEY("INPUT_PATH"),
"Explicitly specify the input folder for difference "