forked from llnl/Caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpand.cpp
More file actions
132 lines (100 loc) · 3.84 KB
/
Expand.cpp
File metadata and controls
132 lines (100 loc) · 3.84 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
// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
// Print expanded records
#include "Expand.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/split.hpp"
#include <algorithm>
#include <functional>
#include <iterator>
#include <mutex>
#include <set>
#include <sstream>
using namespace cali;
struct Expand::ExpandImpl {
std::set<std::string> m_selected;
std::map<std::string, std::string> m_aliases;
OutputStream m_os;
std::mutex m_os_lock;
ExpandImpl(OutputStream& os) : m_os(os) {}
void configure(const QuerySpec& spec)
{
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;
}
void print(CaliperMetadataAccessInterface& db, const EntryList& list)
{
int nentry = 0;
std::ostringstream os;
for (const Entry& e : list) {
if (e.is_reference()) {
std::vector<const Node*> nodes;
for (const Node* node = e.node(); node && node->attribute() != CALI_INV_ID; node = node->parent()) {
std::string name = db.get_attribute(node->attribute()).name();
if ((!m_selected.empty() && m_selected.count(name) == 0))
continue;
nodes.push_back(node);
}
if (nodes.empty())
continue;
stable_sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
return a->attribute() < b->attribute();
});
cali_id_t prev_attr_id = CALI_INV_ID;
for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) {
if ((*it)->attribute() != prev_attr_id) {
std::string name = db.get_attribute((*it)->attribute()).name();
{
auto it = m_aliases.find(name);
if (it != m_aliases.end())
name = it->second;
}
os << (nentry++ ? "," : "") << name << '=';
prev_attr_id = (*it)->attribute();
} else {
os << '/';
}
os << (*it)->data().to_string();
}
} else if (e.is_immediate()) {
std::string name = db.get_attribute(e.attribute()).name();
if ((!m_selected.empty() && m_selected.count(name) == 0))
continue;
{
auto it = m_aliases.find(name);
if (it != m_aliases.end())
name = it->second;
}
os << (nentry++ ? "," : "") << name << '=' << e.value();
}
}
if (nentry > 0) {
std::lock_guard<std::mutex> g(m_os_lock);
std::ostream* real_os = m_os.stream();
*real_os << os.str() << std::endl;
}
}
};
Expand::Expand(OutputStream& os, const QuerySpec& spec) : mP { new ExpandImpl(os) }
{
mP->configure(spec);
}
void Expand::process_record(CaliperMetadataAccessInterface& db, const EntryList& list)
{
mP->print(db, list);
}