Skip to content

Commit e731f39

Browse files
authored
Refactor snapshot record representation (LLNL#408)
* Refactor cali::Entry (wip) * Fix tests * Use constants instead of Attribute::s_keys * De-const node ptr in Attribute and Entry * Refactor blackboard * Rename Entry::is_empty() -> Entry::empty() * Add Entry::pack()/unpack() * Snapshot record refactor * CUDA fixes * RocTracer fixes * Fix warnings * Fix libpfm service * Remove dead code * Some cleanup
1 parent ca0fb49 commit e731f39

File tree

96 files changed

+1377
-1658
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1377
-1658
lines changed

include/caliper/Caliper.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "cali_definitions.h"
1010

11+
#include "SnapshotRecord.h"
12+
1113
#include "common/Attribute.h"
1214
#include "common/CaliperMetadataAccessInterface.h"
1315
#include "common/Entry.h"
@@ -27,7 +29,6 @@ class Caliper;
2729
struct CaliperService;
2830
class Node;
2931
class RuntimeConfig;
30-
class SnapshotRecord;
3132

3233
// --- Typedefs
3334

@@ -60,16 +61,16 @@ class Channel : public IdType
6061
typedef util::callback<void(Caliper*,Channel*)>
6162
caliper_cbvec;
6263

63-
typedef util::callback<void(Caliper*,Channel*,int,const SnapshotRecord*,SnapshotRecord*)>
64+
typedef util::callback<void(Caliper*,Channel*,int,SnapshotView,SnapshotBuilder&)>
6465
snapshot_cbvec;
65-
typedef util::callback<void(Caliper*,Channel*,const SnapshotRecord*,const SnapshotRecord*)>
66+
typedef util::callback<void(Caliper*,Channel*,SnapshotView,SnapshotView)>
6667
process_snapshot_cbvec;
6768
typedef util::callback<void(Caliper*,Channel*,std::vector<Entry>&)>
6869
edit_snapshot_cbvec;
6970

70-
typedef util::callback<void(Caliper*,Channel*,const SnapshotRecord*,SnapshotFlushFn)>
71+
typedef util::callback<void(Caliper*,Channel*,SnapshotView,SnapshotFlushFn)>
7172
flush_cbvec;
72-
typedef util::callback<void(Caliper*,Channel*,const SnapshotRecord*)>
73+
typedef util::callback<void(Caliper*,Channel*,SnapshotView)>
7374
write_cbvec;
7475

7576
typedef util::callback<void(Caliper*,Channel*,const void*, const char*, size_t, size_t, const size_t*,
@@ -313,7 +314,7 @@ class Caliper : public CaliperMetadataAccessInterface
313314
/// \param trigger_info A caller-provided list of attributes that is passed
314315
/// to the snapshot and process_snapshot callbacks, and added to the
315316
/// returned snapshot record.
316-
void push_snapshot(Channel* channel, const SnapshotRecord* trigger_info);
317+
void push_snapshot(Channel* channel, SnapshotView trigger_info);
317318

318319
/// \brief Trigger and return a snapshot.
319320
///
@@ -338,7 +339,7 @@ class Caliper : public CaliperMetadataAccessInterface
338339
/// \param sbuf A caller-provided snapshot record buffer in which the
339340
/// snapshot record is returned. Must have sufficient space for the
340341
/// snapshot contents.
341-
void pull_snapshot(Channel* channel, int scopes, const SnapshotRecord* trigger_info, SnapshotRecord* snapshot);
342+
void pull_snapshot(Channel* channel, int scopes, SnapshotView trigger_info, SnapshotBuilder& rec);
342343

343344
// --- Flush and I/O API
344345

@@ -348,7 +349,7 @@ class Caliper : public CaliperMetadataAccessInterface
348349

349350
/// \brief Flush aggregation/trace buffer contents into the \a proc_fn
350351
/// processing function.
351-
void flush(Channel* channel, const SnapshotRecord* flush_info, SnapshotFlushFn proc_fn);
352+
void flush(Channel* channel, SnapshotView flush_info, SnapshotFlushFn proc_fn);
352353

353354
/// \brief Flush snapshot buffer contents on \a channel into the registered
354355
/// output services.
@@ -359,7 +360,7 @@ class Caliper : public CaliperMetadataAccessInterface
359360
///
360361
/// \param channel The channel to flush
361362
/// \param input_flush_info User-provided flush context information
362-
void flush_and_write(Channel* channel, const SnapshotRecord* flush_info);
363+
void flush_and_write(Channel* channel, SnapshotView flush_info);
363364

364365
/// \brief Clear snapshot buffers on \a channel
365366
///
@@ -487,12 +488,12 @@ class Caliper : public CaliperMetadataAccessInterface
487488
/// \param n Number of elements in attribute/value lists
488489
/// \param attr Attribute list
489490
/// \param data Value list
490-
/// \param list Output record
491+
/// \param list Output record builder
491492
/// \param parent (Optional) parent node for any treee elements.
492493
void make_record(size_t n,
493494
const Attribute attr[],
494495
const Variant data[],
495-
SnapshotRecord& list,
496+
SnapshotBuilder& rec,
496497
cali::Node* parent = nullptr);
497498

498499
// --- Metadata Access Interface

include/caliper/SnapshotRecord.h

+97-109
Original file line numberDiff line numberDiff line change
@@ -6,136 +6,124 @@
66

77
#pragma once
88

9-
#include "common/Attribute.h"
109
#include "common/Entry.h"
1110

1211
#include <algorithm>
13-
#include <map>
14-
#include <vector>
1512

1613
namespace cali
1714
{
1815

1916
class CaliperMetadataAccessInterface;
2017

21-
// Snapshots are fixed-size, stack-allocated objects that can be used in
22-
// a signal handler
23-
24-
/// \brief Snapshot record representation.
18+
/// \brief Non-owning read-only view for snapshot record
19+
class SnapshotView
20+
{
21+
const Entry* m_data;
22+
size_t m_len;
2523

26-
class SnapshotRecord
27-
{
2824
public:
2925

30-
struct Data {
31-
const cali::Node* const* node_entries;
32-
const cali_id_t* immediate_attr;
33-
const cali::Variant* immediate_data;
34-
};
35-
36-
struct Sizes {
37-
std::size_t n_nodes;
38-
std::size_t n_immediate;
39-
};
40-
41-
template<std::size_t N>
42-
struct FixedSnapshotRecord {
43-
cali::Node* node_array[N];
44-
cali_id_t attr_array[N];
45-
cali::Variant data_array[N];
46-
47-
FixedSnapshotRecord() {
48-
std::fill_n(node_array, N, nullptr);
49-
std::fill_n(attr_array, N, CALI_INV_ID);
50-
std::fill_n(data_array, N, cali::Variant());
26+
SnapshotView()
27+
: m_data { nullptr }, m_len { 0 }
28+
{ }
29+
SnapshotView(size_t len, const Entry* data)
30+
: m_data { data }, m_len { len }
31+
{ }
32+
33+
using iterator = Entry*;
34+
using const_iterator = const Entry*;
35+
36+
const_iterator begin() const { return m_data; }
37+
const_iterator end() const { return m_data+m_len; }
38+
39+
size_t size() const { return m_len; }
40+
const Entry* data() const { return m_data; }
41+
bool empty() const { return m_len == 0; }
42+
43+
Entry get(const Attribute& attr) const {
44+
for (const Entry& e : *this) {
45+
Entry ret = e.get(attr);
46+
if (!ret.empty())
47+
return ret;
5148
}
52-
};
53-
54-
SnapshotRecord()
55-
: m_node_array { 0 },
56-
m_attr_array { 0 },
57-
m_data_array { 0 },
58-
m_sizes { 0, 0 },
59-
m_capacity { 0, 0 }
60-
{ }
61-
62-
template<std::size_t N>
63-
SnapshotRecord(FixedSnapshotRecord<N>& list)
64-
: m_node_array { list.node_array },
65-
m_attr_array { list.attr_array },
66-
m_data_array { list.data_array },
67-
m_sizes { 0, 0 },
68-
m_capacity { N, N }
69-
{ }
70-
71-
SnapshotRecord(size_t n_nodes, cali::Node **nodes, size_t n, cali_id_t* attr, Variant* data)
72-
: m_node_array { nodes },
73-
m_attr_array { attr },
74-
m_data_array { data },
75-
m_sizes { n_nodes, n },
76-
m_capacity { n_nodes, n }
77-
{ }
78-
79-
SnapshotRecord(size_t n, cali_id_t* attr, Variant* data)
80-
: m_node_array { 0 },
81-
m_attr_array { attr },
82-
m_data_array { data },
83-
m_sizes { 0, n },
84-
m_capacity { 0, n }
85-
{ }
86-
87-
void append(const SnapshotRecord& list);
88-
void append(size_t n, const cali_id_t*, const cali::Variant*);
89-
void append(size_t n, cali::Node* const*, size_t m, const cali_id_t*, const cali::Variant*);
90-
91-
void append(cali::Node* node) {
92-
if (m_sizes.n_nodes < m_capacity.n_nodes)
93-
m_node_array[m_sizes.n_nodes++] = node;
49+
50+
return Entry();
9451
}
95-
96-
void append(cali_id_t attr, const cali::Variant& data) {
97-
if (m_sizes.n_immediate < m_capacity.n_immediate) {
98-
m_attr_array[m_sizes.n_immediate] = attr;
99-
m_data_array[m_sizes.n_immediate] = data;
100-
++m_sizes.n_immediate;
101-
}
52+
};
53+
54+
/// \brief Non-owning writable view for snapshot record
55+
class SnapshotBuilder
56+
{
57+
Entry* m_data;
58+
size_t m_capacity;
59+
size_t m_len;
60+
size_t m_skipped;
61+
62+
public:
63+
64+
SnapshotBuilder()
65+
: m_data { nullptr}, m_capacity { 0 }, m_len { 0 }, m_skipped { 0 }
66+
{ }
67+
SnapshotBuilder(size_t capacity, Entry* data)
68+
: m_data { data }, m_capacity { capacity }, m_len { 0 }, m_skipped { 0 }
69+
{ }
70+
71+
SnapshotBuilder(SnapshotBuilder&&) = default;
72+
SnapshotBuilder(const SnapshotBuilder&) = delete;
73+
74+
SnapshotBuilder& operator = (SnapshotBuilder&&) = default;
75+
SnapshotBuilder& operator = (const SnapshotBuilder&) = delete;
76+
77+
size_t capacity() const { return m_capacity; }
78+
size_t size() const { return m_len; }
79+
size_t skipped() const { return m_skipped; }
80+
81+
void append(const Entry& e) {
82+
if (m_len < m_capacity)
83+
m_data[m_len++] = e;
84+
else
85+
++m_skipped;
10286
}
10387

104-
void append(const cali::Attribute& attr, const cali::Variant& data) {
105-
append(attr.id(), data);
88+
void append(size_t n, const Entry* entries) {
89+
size_t num_copied = std::min(n, m_capacity - m_len);
90+
std::copy_n(entries, num_copied, m_data+m_len);
91+
m_len += num_copied;
92+
m_skipped += n-num_copied;
10693
}
10794

108-
Sizes capacity() const {
109-
return { m_capacity.n_nodes - m_sizes.n_nodes,
110-
m_capacity.n_immediate - m_sizes.n_immediate };
95+
void append(const Attribute& attr, const Variant& val) {
96+
append(Entry(attr, val));
11197
}
11298

113-
Sizes size() const {
114-
return m_sizes;
99+
void append(SnapshotView view) {
100+
append(view.size(), view.data());
115101
}
116-
117-
Data data() const {
118-
Data addr = { m_node_array, m_attr_array, m_data_array };
119-
return addr;
120-
}
121-
122-
std::size_t num_nodes() const { return m_sizes.n_nodes; }
123-
std::size_t num_immediate() const { return m_sizes.n_immediate; }
124-
125-
Entry get(const Attribute&) const;
126-
127-
std::vector<Entry>
128-
to_entrylist() const;
129-
130-
private:
131-
132-
cali::Node** m_node_array;
133-
cali_id_t* m_attr_array;
134-
cali::Variant* m_data_array;
135-
136-
Sizes m_sizes;
137-
Sizes m_capacity;
138102

103+
SnapshotView view() const {
104+
return SnapshotView { m_len, m_data };
105+
}
106+
};
107+
108+
/// \brief A fixed-size snapshot record
109+
template <std::size_t N>
110+
class FixedSizeSnapshotRecord
111+
{
112+
Entry m_data[N];
113+
SnapshotBuilder m_builder;
114+
115+
public:
116+
117+
FixedSizeSnapshotRecord()
118+
: m_builder { N, m_data }
119+
{ }
120+
121+
SnapshotBuilder& builder() { return m_builder; }
122+
SnapshotView view() const { return m_builder.view(); }
123+
124+
void reset() {
125+
m_builder = SnapshotBuilder(N, m_data);
126+
}
139127
};
140128

141-
}
129+
} // namespace cali

include/caliper/cali-mpi.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#ifdef __cplusplus
1515

16+
#include "caliper/SnapshotRecord.h"
17+
1618
namespace cali
1719
{
1820

@@ -22,7 +24,6 @@ class Channel;
2224
class CaliperMetadataDB;
2325
class OutputStream;
2426
class QuerySpec;
25-
class SnapshotRecord;
2627

2728
/**
2829
* \brief Perform cross-process aggregation over MPI
@@ -54,7 +55,7 @@ void
5455
collective_flush(OutputStream& stream,
5556
Caliper& c,
5657
Channel& channel,
57-
const SnapshotRecord* flush_info,
58+
SnapshotView flush_info,
5859
const QuerySpec& local_query,
5960
const QuerySpec& cross_query,
6061
MPI_Comm comm);

0 commit comments

Comments
 (0)