SST 16.0.0
Structural Simulation Toolkit
perfReporter.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_PERF_REPORTER_H
13#define SST_CORE_PERF_REPORTER_H
14
15#include "sst/core/output.h"
16#include "sst/core/unitAlgebra.h"
17#include "sst/core/util/filesystem.h"
18
19#include "nlohmann/json.hpp"
20
21#include <cstddef>
22#include <cstdint>
23#include <fstream>
24#include <iostream>
25#include <map>
26#include <mutex>
27#include <sstream>
28#include <stack>
29#include <string>
30#include <utility>
31#include <variant>
32#include <vector>
33
34namespace SST::Util {
35
36class DataRecord;
37class PerfReporter;
38
39class PerfData
40{
41 friend DataRecord;
42
43public:
44 // Create (begin) a new record level
45 PerfData(std::string name, PerfData* parent);
46
47 template <typename T>
48 T getData(const std::string& key)
49 {
50 const auto& val = data_.at(key);
51 return std::get<T>(val);
52 }
53
54 std::string name_ = "";
55 std::map<std::string, std::variant<uint64_t, int64_t, double, UnitAlgebra, std::string>> data_;
56 std::map<std::string, std::pair<std::string, std::string>>
57 keys_; // Keys optionally map to a <extended_key, unit> pair for pretty print
58 PerfData* parent_ = nullptr;
59 std::vector<PerfData*> children_;
60};
61
62// Stores a hierarchy of named key/val pairs that should be output
63class DataRecord
64{
65public:
66 friend PerfReporter;
67 enum class TextFormat { plain, tree, list };
68 DataRecord(std::string name, TextFormat format);
69
70 // API for inserting data into records
71 // Map of key str to pair of (pretty print string, units). Units may be "" if not applicable.
72 // Keys are *not* required to be entered in this structure if no pretty print string/units are required
73 // Extended string for the name of the record can also be entered
74 void setKeys(std::map<std::string, std::pair<std::string, std::string>>& key_map);
75
76 void setFormat(TextFormat format) { format_ = format; }
77
78 // API for traversing
79 void changeLevelUp();
80 bool changeLevelDown(std::string name); // Return whether level was changed
81 void addChild(std::string name);
82 void endChild();
83 void addData(std::string key, double value);
84 void addData(std::string key, uint64_t value);
85 void addData(std::string key, int64_t value);
86 void addData(std::string key, UnitAlgebra value);
87 void addData(std::string key, std::string value);
88 void addData(std::map<std::string, std::variant<uint64_t, int64_t, double, UnitAlgebra, std::string>> data);
89
90private:
91 PerfData* root_;
92 PerfData* current_record_;
93 TextFormat format_;
94};
95
96class PerfReporter
97{
98public:
99 PerfReporter() { output_.init("", 0, 0, Output::STDOUT); }
100
101 /*
102 * Thread-safe function to return the data record with the given name
103 * If record does not already exist, it will be created
104 */
105 DataRecord* createDataRecord(std::string name, DataRecord::TextFormat format = DataRecord::TextFormat::plain);
106
107 void output(int rank, int num_ranks); // Output all registered records
108
109 void configureOutput(std::string output_str);
110 void outputRecordToTextTree(const PerfData* node, std::stringstream* sstr);
111
112 void outputNodeToTextTree(
113 const PerfData* node, std::stringstream* sstr, bool last_child, std::vector<int> levels_has_more_siblings = {});
114 void printTreeIndent(
115 std::stringstream* sstr, const std::vector<int>& levels_has_more_siblings, size_t indent_level);
116 void outputValueToText(
117 const std::variant<uint64_t, int64_t, double, UnitAlgebra, std::string>& v, std::stringstream* sstr);
118 std::string convertValueToString(const std::variant<uint64_t, int64_t, double, UnitAlgebra, std::string>& v);
119 void outputRecordToText(const PerfData* node, std::stringstream* sstr, int indent = 0, bool print_name = true);
120 void outputRecordToTextList(const PerfData* node, std::stringstream* sstr, bool header);
121 void outputRecordToJSON(const PerfData* node, nlohmann::ordered_json* obj);
122 size_t recordCount();
123
124private:
125 // Can output to console and/or a file (json or text)
126 Output output_;
127 bool output_console_ = false; // output to console or not
128 std::string filename_; // File to output to (or "" if console only; extension determines format)
129 std::map<std::string, DataRecord*>
130 records_; // Data stored at the base level - no indent (txt) or highest-level object (JSON)
131 mutable std::mutex mtx; // mutex for record creation
132};
133
134} // namespace SST::Util
135
136#endif // SST_CORE_PERF_REPORTER_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:58
@ STDOUT
Definition output.h:64
Performs Unit math in full precision.
Definition unitAlgebra.h:107
Definition perfReporter.h:64
Definition perfReporter.h:40
Definition perfReporter.h:97