10#include <boost/algorithm/string/replace.hpp>
11#include <boost/any.hpp>
12#include <boost/iterator/iterator_traits.hpp>
13#include <boost/type_index/type_index_facade.hpp>
22#include <unordered_map>
23#include <unordered_set>
28#include "sparta/report/Report.hpp"
29#include "sparta/utils/Utils.hpp"
30#include "sparta/report/format/BaseFormatter.hpp"
35#include "sparta/log/NotificationSource.hpp"
36#include "sparta/report/RoiManager.hpp"
39 class SimulationConfiguration;
40 class ReportStatsCollector;
43namespace sparta::trigger {
44 class SkippedAnnotatorBase;
52 class FormatterFactory;
61 namespace statistics {
70 typedef std::unordered_map<std::string, boost::any> NamedExtensions;
71 typedef std::unordered_map<std::string, std::string> TriggerKeyValues;
72 typedef std::unordered_map<std::string, std::string> MetaDataKeyValues;
80 typedef std::pair<Report*, report::format::BaseFormatter*> inst_t;
87 std::set<const Report*> triggered_reports_;
97 std::set<const Report*> idle_reports_;
102 std::map<std::string, std::shared_ptr<report::format::BaseFormatter>> formatters_;
108 std::shared_ptr<statistics::ReportStatisticsArchive> report_archive_;
114 std::shared_ptr<statistics::StreamNode> streaming_stats_root_;
122 bool updateReportActiveState_(
const Report * r);
128 std::vector<inst_t> instantiations_;
135 const sparta::report::format::FormatterFactory* fact_;
152 class DescUpdateTracker {
154 void enable(
const Scheduler * scheduler);
155 bool checkIfDuplicateUpdate();
166 DescUpdateTracker update_tracker_;
172 std::shared_ptr<sparta::trigger::SkippedAnnotatorBase> skipped_annotator_;
177 bool report_stopped_ =
false;
183 bool enabled_ =
true;
192 std::string orig_dest_file_;
198 size_t roi_region_ = 0;
214 bool legacy_reports_enabled_ =
true;
221 bool uses_sprintf_notif_trigger_ =
false;
227 std::vector<std::string> sprintf_notif_trigger_dest_files_;
235 void sweepSimDbStats_();
243 void skipSimDbStats_();
347 const std::string& _def_file,
348 const std::string& _dest_file,
349 const std::string& _format=
"text",
350 const size_t roi_region = 0);
392 return orig_dest_file_;
408 const std::string & dir);
425 report_stopped_ =
true;
432 auto comma_sep_files = [](
const std::vector<std::string>& dest_files)
434 std::ostringstream oss;
436 for (
const auto& s : dest_files)
448 auto _dest_files = sprintf_notif_trigger_dest_files_.empty() ?
449 (orig_dest_file_.empty() ?
dest_file : orig_dest_file_) :
450 comma_sep_files(sprintf_notif_trigger_dest_files_);
452 std::stringstream ss;
454 <<
"\" -> \"" << _dest_files <<
"\"";
456 ss <<
" (format=" <<
format <<
")";
484 std::ostream* out=
nullptr);
491 return instantiations_;
501 const std::vector<Report*> & pending_reports =
502 boost::any_cast<const std::vector<Report*>&>(iter->second);
504 return pending_reports;
518 std::unordered_set<Report*> all(pending.begin(), pending.end());
519 for (
const auto & cur : current) {
520 all.insert(cur.first);
522 return std::vector<Report*>(all.begin(), all.end());
540 legacy_reports_enabled_ =
false;
586 uses_sprintf_notif_trigger_ =
true;
594 return uses_sprintf_notif_trigger_;
647 const std::string& sim_name,
651 typedef std::vector<ReportDescriptor> ReportDescVec;
652 typedef std::vector<std::pair<std::string, std::string>> ReportYamlReplacements;
687 const std::string desc_name = getDescriptorName_(rd);
688 auto iter = indices_by_descriptor_name_.find(desc_name);
689 if (iter != indices_by_descriptor_name_.end()) {
692 if (rep_descs_.at(iter->second).isEnabled()) {
694 desc_name <<
"' already exists in this configuration";
697 indices_by_descriptor_name_[desc_name] = rep_descs_.size();
708 rep_descs_.push_back(rd);
709 rep_descs_.back().enabled_ =
true;
713 template <
class... Args>
721 for (
auto &rd : rep_descs_) {
729 for (
const auto & rep_desc : rep_descs_) {
730 if (rep_desc.isEnabled()) {
744 bool contains(
const std::string & desc_name)
const {
745 auto iter = indices_by_descriptor_name_.find(desc_name);
746 if (iter == indices_by_descriptor_name_.end()) {
749 const auto replaced_desc_name = replaceDotsWithUnderscores_(desc_name);
750 iter = indices_by_descriptor_name_.find(replaced_desc_name);
755 if (iter == indices_by_descriptor_name_.end()) {
761 return rep_descs_.at(iter->second).isEnabled();
766 auto idx = getDescriptorIndexByName_(desc_name);
767 return rep_descs_.at(idx);
779 std::vector<std::string> names;
780 for (
const auto & rd : indices_by_descriptor_name_) {
781 if (rep_descs_.at(rd.second).isEnabled()) {
782 names.push_back(rd.first);
789 std::deque<ReportDescriptor>::iterator
begin() {
790 return rep_descs_.begin();
794 std::deque<ReportDescriptor>::const_iterator
begin()
const {
795 return rep_descs_.cbegin();
799 std::deque<ReportDescriptor>::iterator
end() {
800 return rep_descs_.end();
804 std::deque<ReportDescriptor>::const_iterator
end()
const {
805 return rep_descs_.cend();
810 return replaceDotsWithUnderscores_(rd.
dest_file);
813 std::string replaceDotsWithUnderscores_(
const std::string & str)
const {
814 std::string replaced = str;
815 boost::replace_all(replaced,
".",
"_");
819 size_t getDescriptorIndexByName_(
const std::string & desc_name)
const {
820 auto iter = indices_by_descriptor_name_.find(desc_name);
821 if (iter == indices_by_descriptor_name_.end()) {
824 const auto replaced_desc_name = replaceDotsWithUnderscores_(desc_name);
825 iter = indices_by_descriptor_name_.find(replaced_desc_name);
828 if (iter == indices_by_descriptor_name_.end()) {
829 throw SpartaException(
830 "No descriptor named '") << desc_name <<
"' exists";
837 if (!rep_descs_.at(iter->second).isEnabled()) {
838 throw SpartaException(
"The descriptor named '") << desc_name
839 <<
"' has already been disabled";
844 std::deque<ReportDescriptor> rep_descs_;
845 std::unordered_map<std::string, size_t> indices_by_descriptor_name_;
910 void republishReportCollection_();
913 void finishPythonInteraction_();
916 void disallowChangesToDescriptors_();
929 bool allow_descriptor_changes_ =
true;
930 RoiManager roi_manager_{root_};
938 const std::string & replacements_yaml);
946 const std::string & def_file,
972 const std::string & def_file,
974 const ReportYamlReplacements & placeholder_key_value_pairs);
981 const std::string & def_string,
File that defines the Clock class.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
File that defines a ValidValue.
TreeNode which represents the root ("top") of a device tree.
A class that lets you schedule events now and in the future.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Accepts a sprintf-style format string like "%s_%i.json" and uses the arguments given to postNotificat...
Node in a composite tree representing a sparta Tree item.
Configuration applicator class that is used for configuring a simulator's reports....
void addMemoryReportsFromYaml(const std::string &yaml_file)
const ReportDescriptorCollection * getDescriptors() const
Access the underlying report descriptors.
RoiManager * getROIManager()
Access the underlying ROI manager.
ReportDescriptorCollection * getDescriptors()
Access the underlying report descriptors.
void showAllReportDescriptorInfo()
Pretty print information about all app::ReportDescriptor's.
void serializeAllDescriptorsToYaml()
void addReportsFromYaml(const std::string &yaml_file)
void removeReportByName(const std::string &rd_name)
void addReport(const ReportDescriptor &rd)
Add one report descriptor to this collection.
This collection of ReportDescriptors is designed to never deallocate memory once it has been allocate...
std::vector< std::string > getAllDescriptorNames() const
bool contains(const std::string &desc_name) const
void removeDescriptorByName(const std::string &desc_name)
std::deque< ReportDescriptor >::const_iterator begin() const
Iterator access.
size_t size() const
Get the number of enabled descriptors in this collection.
ReportDescriptor & getDescriptorByName(const std::string &desc_name)
Access a descriptor by the app::ReportDescriptor's "dest_file".
std::deque< ReportDescriptor >::iterator end()
Iterator access.
std::deque< ReportDescriptor >::iterator begin()
Iterator access.
void push_back(const ReportDescriptor &rd)
Add one report descriptor to this collection.
std::deque< ReportDescriptor >::const_iterator end() const
Iterator access.
void emplace_back(Args &&... args)
Add one report descriptor to this collection.
bool empty() const
See if there are any enabled descriptors in this collection.
void clear()
Remove (disable) all descriptors from this collection.
Describes one or more report to instantiate.
~ReportDescriptor()
Destructor (note that triggered reports are automatically flushed)
MetaDataKeyValues header_metadata_
Metadata to include in report headers.
uint32_t getUsageCount() const
Returns the usage count (incremented by addInstantiation)
const std::string & getDescriptorDefFile() const
Getter for this descriptor's def_file, e.g. "simple_stats.yaml".
std::string format
Optional formatting string (how to write to the file). This is converted to lower-case at constructio...
std::vector< Report * > getAllInstantiations() const
Returns all report instantiations, including those already instantiated (no report start trigger) and...
std::string computeFilename(const Report *r, const std::string &sim_name, uint32_t idx) const
Computes the filename to which this reportdescriptor will be saved using any necessary variables in t...
uint32_t writeOutput(std::ostream *out=nullptr)
Saves all of the instantiations whose formatters do not support 'update' to their respective destinat...
bool configSimDbReports(app::ReportStatsCollector *collector)
Get ready for SimDB report collection.
void disableLegacyReports()
In MAP v2.1, we provide report systems for both legacy reports and SimDB-exported reports....
void capUpdatesToOncePerTick(const Scheduler *scheduler)
Instruct this descriptor to automatically ignore any "duplicate" updates that occur at the exact same...
report::format::BaseFormatter * addInstantiation(Report *r, Simulation *sim, std::ostream *out=nullptr)
Tracks a report instantiated based on this descriptor and allocates a new formatter for it....
size_t getRegionOfInterest() const
Get the specific Region of Interest (ROI) for this descriptor.
const std::string & getDescriptorFormat() const
Getter for this descriptor's format, e.g. "json_reduced".
std::string dest_file
Destination filename to which report will be written. Later this will represent other types of destin...
uint32_t getNumWrites() const
Returns the number of writes done on this report descriptor's instantiated report formatters.
uint32_t getNumUpdates() const
Returns the number of updates done on this report descriptor's instantiated report formatters.
void teardown()
Called when the ReportRepository is shutting down.
std::vector< Report * > getPendingInstantiations() const
Returns a vector of reports that have not been instantiated yet, but will be when this report descrip...
static const char * GLOBAL_KEYWORD
Global search scope node keyword for report locations.
std::string name
The name of this report.
bool usesSprintfNotifTrigger() const
Returns whether this descriptor uses a 'sprintf-notif' trigger.
ReportDescriptor & operator=(const ReportDescriptor &)=default
Allow assignment.
ReportDescriptor(const std::string &_loc_pattern, const std::string &_def_file, const std::string &_dest_file, const std::string &_format="text", const size_t roi_region=0)
Construct a report decsriptor.
std::string loc_pattern
Node location string (pattern) on which report should be generated. Typically "" or top.
void clearDestinationFiles(const Simulation &sim)
Clears all destination files that will be filled with instances of this report descriptor.
ReportDescriptor(const ReportDescriptor &)=default
Allow construction.
void disable()
Calling this method causes the simulation to skip this descriptor when it is setting up its reports....
const std::string & getDescriptorDestFile() const
Getter for this descriptor's dest_file, e.g. "out.json".
const std::string & getDescriptorOrigDestFile() const
This descriptor may have had its dest_file changed when the the Simulation::setupReports() method was...
bool isEnabled() const
See if this descriptor is enabled or not. Disabling a descriptor means that it will be filtered from ...
std::vector< inst_t > getInstantiations() const
Returns the vector of instantiated reports based on this descriptor.
NamedExtensions extensions_
Key-value extensions used by parsers to bind opaque report configurations to descriptors.
void setUsesSprintfNotifTrigger(SprintfNotificationSource *notif_src)
Marks this descriptor as using a 'sprintf-notif' trigger, meaning dest_file is a sprintf-style format...
static bool isValidFormatName(const std::string &format)
Determines if format is a valid name for formatter. This allows the command line parser to disregard ...
void skipOutput()
Let the descriptor know to skip over one update of data.
std::shared_ptr< statistics::ReportStatisticsArchive > logOutputValuesToArchive(const std::string &dir)
Tell the descriptor to send all of its writeOutput / updateOutput statistics values to a binary archi...
std::string def_file
Filename of report definition. If '@', auto generates reports with all counters and stats of loc_patt...
uint32_t writeOutput(const std::string &filename)
Overload used only by descriptors bound to a 'sprintf-notif' trigger. Writes all non-updatable instan...
std::shared_ptr< statistics::StreamNode > createRootStatisticsStream()
Create and return a StreamNode object that sits at the top of a tree hierarchy that describes the Rep...
const std::string & getDescriptorName() const
Getter for this report's name.
void ignoreFurtherUpdates()
Report descriptors may be triggered to stop early - ensure no further updates are written to disk.
std::string stringize() const
Represents this descriptor as a string.
const std::string & getDescriptorPattern() const
Getter for this descriptor's pattern, e.g. "_global".
void setSkippedAnnotator(std::shared_ptr< sparta::trigger::SkippedAnnotatorBase > annotator)
Give this descriptor a specific annotator subclass for printing skipped update information to reports...
uint32_t updateOutput(std::ostream *out=nullptr)
Updates all of the instantiations whose formatters support 'update', possibly by writing to the desti...
Configuration applicator class that is used for configuring a simulator. Works in conjunction with sp...
Simulator which builds a sparta DeviceTree.
This class coordinates live SPARTA simulations (source) with binary output archives (sink).
When a simulation is configured to stream its statistics values for asynchronous processing,...
Provides a wrapper around a value to ensure that the value is assigned.
Sparta Application framework.
ReportDescVec createDescriptorsFromFile(const std::string &def_file, TreeNode *context)
Given a multi-report definition YAML file, parse it out into individual descriptors,...
ReportYamlReplacements createReplacementsFromYaml(const std::string &replacements_yaml)
Parse a YAML file containing key-value pairs into a single ReportYamlReplacements data structure.
ReportDescVec createDescriptorsFromDefinitionString(const std::string &def_string, TreeNode *context)
Given a multi-report definition string, parse it out into individual descriptors.
ReportDescVec createDescriptorsFromFileWithPlaceholderReplacements(const std::string &def_file, TreeNode *context, const ReportYamlReplacements &placeholder_key_value_pairs)
This method is similar to "createDescriptorsFromFile()", except that it can be used for report yaml f...
Macros for handling exponential backoff.