forked from llnl/Caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonFormatter.cpp
More file actions
294 lines (226 loc) · 9.12 KB
/
JsonFormatter.cpp
File metadata and controls
294 lines (226 loc) · 9.12 KB
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
// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
// Print web-readable table in sparse format
#include "JsonFormatter.h"
#include "caliper/reader/QuerySpec.h"
#include "caliper/common/Attribute.h"
#include "caliper/common/CaliperMetadataAccessInterface.h"
#include "caliper/common/Node.h"
#include "caliper/common/OutputStream.h"
#include "../common/util/format_util.h"
#include "../common/util/split.hpp"
#include <algorithm>
#include <functional>
#include <iterator>
#include <mutex>
#include <set>
#include <sstream>
#include <iostream>
using namespace cali;
struct JsonFormatter::JsonFormatterImpl {
std::set<std::string> m_selected;
OutputStream m_os;
std::mutex m_os_lock;
unsigned m_num_recs = 0;
enum Layout { Records, Split, Object };
Layout m_layout = Records;
bool m_opt_split = false;
bool m_opt_pretty = false;
bool m_opt_quote_all = false;
bool m_opt_sep_nested = false;
std::map<std::string, std::string> m_aliases;
JsonFormatterImpl(OutputStream& os) : m_os(os) {}
void configure(const QuerySpec& spec)
{
for (auto p : spec.format.kwargs) {
if (p.first == "pretty")
m_opt_pretty = true;
else if (p.first == "quote-all")
m_opt_quote_all = true;
else if (p.first == "separate-nested")
m_opt_sep_nested = true;
else if (p.first == "records")
m_layout = Records;
else if (p.first == "split")
m_layout = Split;
else if (p.first == "object")
m_layout = Object;
}
switch (spec.select.selection) {
case QuerySpec::AttributeSelection::Default:
case QuerySpec::AttributeSelection::All:
// do nothing; default is all
break;
case QuerySpec::AttributeSelection::None:
// doesn't make much sense
break;
case QuerySpec::AttributeSelection::List:
m_selected = std::set<std::string>(spec.select.list.begin(), spec.select.list.end());
break;
}
m_aliases = spec.aliases;
}
inline std::string get_key(const Attribute& attr)
{
std::string name = attr.name();
bool selected = m_selected.count(name) > 0;
if (!selected && (!m_selected.empty() || attr.is_hidden() || attr.is_global()))
return "";
if (attr.is_nested() && !m_opt_sep_nested)
return "path";
auto it = m_aliases.find(name);
return (it == m_aliases.end() ? name : it->second);
}
void begin_records_section(std::ostream& os)
{
if (m_layout == Records)
os << "[\n";
else if (m_layout == Object)
os << "{\n\"records\": [\n";
}
void end_records_section(std::ostream& os)
{
if (m_layout == Records || m_layout == Object)
os << "\n]";
}
void print(CaliperMetadataAccessInterface& db, const EntryList& list)
{
std::map<std::string, std::string> noquote_kvs;
std::map<std::string, std::string> quote_kvs;
//
// collect json key-value pairs for this record
//
for (const Entry& e : list) {
if (e.is_reference()) {
for (const Node* node = e.node(); node && node->attribute() != CALI_INV_ID; node = node->parent()) {
std::string key = get_key(db.get_attribute(node->attribute()));
if (key.empty())
continue;
auto it = quote_kvs.find(key);
if (it == quote_kvs.end())
quote_kvs.emplace(std::make_pair(std::move(key), node->data().to_string()));
else
it->second = node->data().to_string().append("/").append(it->second);
}
} else if (e.is_immediate()) {
Attribute attr = db.get_attribute(e.attribute());
std::string key = get_key(attr);
if (key.empty())
continue;
cali_attr_type type = attr.type();
if (m_opt_quote_all || type == CALI_TYPE_STRING || type == CALI_TYPE_USR)
quote_kvs[std::move(key)] = e.value().to_string();
else
noquote_kvs[std::move(key)] = e.value().to_string();
}
} // for (Entry& ... )
//
// write the key-value pairs
//
std::lock_guard<std::mutex> g(m_os_lock);
std::ostream* real_os = m_os.stream();
if (noquote_kvs.size() + quote_kvs.size() > 0) {
if (m_num_recs == 0)
begin_records_section(*real_os);
*real_os << (m_num_recs > 0 ? ",\n" : "") << "{";
int count = 0;
for (auto& p : quote_kvs) {
*real_os << (count++ > 0 ? "," : "") << (m_opt_pretty ? "\n\t" : "");
util::write_json_esc_string(*real_os << "\"", p.first) << "\":";
util::write_json_esc_string(*real_os << "\"", p.second) << "\"";
}
for (auto& p : noquote_kvs) {
*real_os << (count++ > 0 ? "," : "") << (m_opt_pretty ? "\n\t" : "");
util::write_json_esc_string(*real_os << "\"", p.first) << "\":";
util::write_json_esc_string(*real_os, p.second);
}
*real_os << (m_opt_pretty ? "\n" : "") << "}";
++m_num_recs;
}
}
std::ostream& write_attributes(CaliperMetadataAccessInterface& db, std::ostream& os)
{
std::vector<Attribute> attrs;
if (m_selected.empty())
attrs = db.get_all_attributes();
else
for (const std::string& str : m_selected)
attrs.push_back(db.get_attribute(str));
// remove nested and hidden attributes
if (!m_opt_sep_nested)
attrs.erase(
std::remove_if(attrs.begin(), attrs.end(), [](const Attribute& a) { return a.is_nested(); }),
attrs.end()
);
attrs.erase(
std::remove_if(attrs.begin(), attrs.end(), [](const Attribute& a) { return a.is_hidden(); }),
attrs.end()
);
// write "attr1": { "prop": "value", ... } , "attr2" : ...
int count = 0;
for (const Attribute& a : attrs) {
os << (count++ > 0 ? ",\n" : "\n") << (m_opt_pretty ? "\t" : "") << '\"' << a.name() << "\": {";
// encode some properties
os << "\"is_global\": " << (a.is_global() ? "true" : "false")
<< ",\"is_nested\": " << (a.is_nested() ? "true" : "false");
// print meta-info
for (const Node* node = a.node(); node && node->attribute() != CALI_INV_ID; node = node->parent()) {
util::write_json_esc_string(os << ",\"", db.get_attribute(node->attribute()).name()) << "\": ";
util::write_json_esc_string(os << "\"", node->data().to_string()) << '\"';
}
os << "}";
}
return os;
}
std::ostream& write_globals(CaliperMetadataAccessInterface& db, std::ostream& os)
{
std::vector<Entry> globals = db.get_globals();
std::map<cali_id_t, std::string> global_vals;
for (const Entry& e : globals)
if (e.is_reference())
for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) {
std::string s = node->data().to_string();
if (global_vals[node->attribute()].size() > 0)
s.append("/").append(global_vals[node->attribute()]);
global_vals[node->attribute()] = s;
}
else
global_vals[e.attribute()] = e.value().to_string();
int count = 0;
for (auto& p : global_vals) {
if (count++ > 0)
os << ",\n";
if (m_opt_pretty)
os << '\t';
util::write_json_esc_string(os << '\"', db.get_attribute(p.first).name()) << "\": ";
util::write_json_esc_string(os << '\"', p.second) << '\"';
}
return os;
}
void flush(CaliperMetadataAccessInterface& db)
{
std::ostream* real_os = m_os.stream();
// close records section
if (m_num_recs == 0)
begin_records_section(*real_os);
end_records_section(*real_os);
if (m_layout == Object) {
write_globals(db, *real_os << ",\n\"globals\": {\n") << "\n}";
write_attributes(db, *real_os << ",\n\"attributes\": {") << "\n}";
*real_os << "\n}"; // close object opened in begin_records_section()
}
*real_os << std::endl;
}
};
JsonFormatter::JsonFormatter(OutputStream& os, const QuerySpec& spec) : mP { new JsonFormatterImpl(os) }
{
mP->configure(spec);
}
void JsonFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list)
{
mP->print(db, list);
}
void JsonFormatter::flush(CaliperMetadataAccessInterface& db, std::ostream&)
{
mP->flush(db);
}