Simo 0.0.1
Loading...
Searching...
No Matches
Collector.h
1/*
2 * Copyright 2026 Matteo Fusi and Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SIMO_COLLECTOR_HH
18#define SIMO_COLLECTOR_HH
19#include <regex>
20#include <vector>
21
22#include "Simo/core/Context.h"
23#include "Simo/core/Time.h"
24#include "Simo/module/Module.h"
25#include "Simo/statistics/StatOutStream.h"
26
27namespace Simo::Modules::Core {
28class Collector : public Module {
29 public:
30 class Parameters : public Simo::Parameters {
31 public:
32 Parameters() {
33 trie.add_unset<Time>("start_time");
34 trie.add_unset<Time>("end_time").validator([this](const Time& end) {
35 const auto start_time_ptr = trie.find<Time>("start_time");
36 return start_time_ptr != nullptr && end > start_time_ptr->value();
37 });
38 trie.add<std::string>("module_match_regex", ".*");
39 trie.add<std::string>("dump_path", "statistics.yaml");
40 }
41 };
42
44 const Simo::Parameters& parameters) override {
45 if (const auto status = Module::initialize(sim_ctx_v, parameters);
46 !status.success()) {
47 return status;
48 }
49 const auto start_time = parameters.get<Time>("start_time")->value();
50 const auto end_time = parameters.get<Time>("end_time")->value();
51 dump_path = parameters.get<std::string>("dump_path")->value();
52 sim_ctx().schedule_at(start_time, [this] { open_window(); });
53 sim_ctx().schedule_at(end_time, [this] { close_window(); });
54 const auto module_regex =
55 parameters.get<std::string>("module_match_regex")->value();
56 sim_ctx().foreach_module([this, &module_regex](const Module& m) {
57 if (std::regex_match(std::string(m.name()), std::regex(module_regex))) {
58 add_module(const_cast<Module*>(&m));
59 }
60 });
61 return InitializationStatus::ok(this);
62 }
63
64 void add_module(Module* module) { modules.push_back(module); }
65
66 void collect() {
67 if (!searched_statistics) {
68 for (const auto& module : modules) {
69 mapper.emplace_back(*module);
70 }
71 searched_statistics = true;
72 }
73 }
74
75 void open_window() {
76 collect();
77 for (const auto& mod_mapper : mapper) {
78 mod_mapper.assign();
79 }
80 }
81
82 void close_window() {
83 collect();
84 auto out_stream = Statistics::StatOutStream();
85 out_stream.output_path(dump_path);
86 for (auto& mod_mapper : mapper) {
87 for (const auto& stat_ptr : mod_mapper.compute_diff()) {
88 out_stream << *stat_ptr;
89 }
90 }
91 out_stream.generate();
92 }
93
94 protected:
95 std::vector<Module*> modules;
96 bool searched_statistics = false;
97 std::vector<Statistics::ModuleStatMapper> mapper;
98 std::filesystem::path dump_path;
99};
100} // namespace Simo::Modules::Core
101
102#endif // SIMO_COLLECTOR_HH
Definition Context.h:54
Definition InitializationStatus.h:30
Definition Module.h:83
virtual InitializationStatus initialize(Context &sim_ctx_v, const Parameters &parameters)
Definition Module.cc:49
Definition Collector.h:28
InitializationStatus initialize(Context &sim_ctx_v, const Simo::Parameters &parameters) override
Definition Collector.h:43
Collects parameters that are then passed to a Module instance.
Definition Module.h:30
Parameter::ParameterTyped< T > * get(const std::string &name) const
Definition Module.h:57
Definition Time.h:40