Skip to content

Commit

Permalink
metadata modules is now a table
Browse files Browse the repository at this point in the history
- symbols and source_files are now up for migration to tables;
- sdb::scontext::indexed_by is now capable of inner context passing.
  • Loading branch information
tyoma committed Jul 31, 2022
1 parent 854f397 commit 227ed54
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 146 deletions.
4 changes: 2 additions & 2 deletions frontend/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ namespace micro_profiler
typedef sdb::table<module_mapping> module_mappings;


struct modules : containers::unordered_map<unsigned int /*persistent_id*/, module_info_metadata>
typedef record<module_info_metadata> module;
struct modules : sdb::table<module>
{
typedef containers::unordered_map<unsigned int /*persistent_id*/, module_info_metadata> base_t;
typedef std::shared_ptr<void> handle_t;

typedef std::function<void (const module_info_metadata &metadata)> metadata_ready_cb;
Expand Down
9 changes: 6 additions & 3 deletions frontend/persistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace strmd
{
template <> struct version<micro_profiler::profiling_session> { enum { value = 6 }; };
template <> struct version<micro_profiler::profiling_session> { enum { value = 7 }; };
}

namespace micro_profiler
Expand All @@ -47,7 +47,7 @@ namespace micro_profiler
template <typename ArchiveT>
inline void serialize(ArchiveT &archive, profiling_session &data, unsigned int ver)
{
sdb::scontext::indexed_by<keyer::external_id> as_map;
sdb::scontext::indexed_by<keyer::external_id, void> as_map;

archive(data.process_info);

Expand All @@ -56,7 +56,10 @@ namespace micro_profiler
else if (ver >= 3)
archive(data.mappings, as_map);

archive(data.modules);
if (ver >= 7)
archive(data.modules);
else
archive(data.modules, as_map);

if (ver >= 5)
archive(data.statistics);
Expand Down
11 changes: 2 additions & 9 deletions frontend/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,10 @@ namespace micro_profiler
archive(static_cast<T &>(data));
}

template <typename ArchiveT, typename T>
inline void serialize(ArchiveT &archive, record<T> &data, sdb::scontext::indexed_by<keyer::external_id> &, int ver)
template <typename ArchiveT, typename T, typename I>
inline void serialize(ArchiveT &archive, record<T> &data, sdb::scontext::indexed_by<keyer::external_id, I> &, int ver)
{ serialize(archive, static_cast<T &>(data), ver); }

template <typename ArchiveT>
inline void serialize(ArchiveT &archive, modules &data)
{
archive(static_cast<modules::base_t &>(data));
data.invalidate();
}

template <typename ArchiveT, typename ContextT>
inline void serialize(ArchiveT &archive, statistics &data, ContextT &/*context*/)
{ sdb::serialize(archive, data); }
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace micro_profiler
return;

auto modules_callback = [this] (ipc::deserializer &d) {
sdb::scontext::indexed_by<keyer::external_id> as_map;
sdb::scontext::indexed_by<keyer::external_id, void> as_map;

d(_db->mappings, as_map);
};
Expand Down Expand Up @@ -149,7 +149,7 @@ namespace micro_profiler
}

request(*req, request_threads_info, thread_ids, response_threads_info, [this, req] (ipc::deserializer &d) {
sdb::scontext::indexed_by<keyer::external_id> as_map;
sdb::scontext::indexed_by<keyer::external_id, void> as_map;

d(_db->threads, as_map);
_requests.erase(req);
Expand Down
25 changes: 18 additions & 7 deletions frontend/src/frontend_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ namespace micro_profiler
const tables::modules::metadata_ready_cb &ready)
{
const auto init = mx_metadata_requests_t::create(request_, _mx_metadata_requests, persistent_id, ready);
const auto m = _db->modules.find(persistent_id);
const auto m = sdb::unique_index(_db->modules, keyer::external_id()).find(persistent_id);

if (m != _db->modules.end())
if (m)
{
ready(m->second);
ready(*m);
}
else if (init.underlying)
{
Expand Down Expand Up @@ -81,9 +81,18 @@ namespace micro_profiler
d(m);
}, [this, persistent_id, ready, &rreq, cached_invoke] (shared_ptr<module_info_metadata> m) {
if (m)
ready((_db->modules)[persistent_id] = *m);
{
auto rec = sdb::unique_index(_db->modules, keyer::external_id())[persistent_id];
auto &m_ = *rec;

static_cast<module_info_metadata &>(m_) = *m;
rec.commit();
ready(m_);
}
else
{
request_metadata_nw(rreq, persistent_id, cached_invoke);
}
});
}

Expand All @@ -94,11 +103,13 @@ namespace micro_profiler
request(request_, request_module_metadata, persistent_id, response_module_metadata,
[this, persistent_id, ready] (ipc::deserializer &d) {

auto &m = (_db->modules)[persistent_id];
auto rec = sdb::unique_index(_db->modules, keyer::external_id())[persistent_id];
auto &m = *rec;

d(m);
d(static_cast<module_info_metadata &>(m));
rec.commit();

LOG(PREAMBLE "received...") % A(persistent_id) % A(m.symbols.size()) % A(m.source_files.size());
LOG(PREAMBLE "received...") % A(m.id) % A(m.symbols.size()) % A(m.source_files.size());
ready(m);
});
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/image_patch_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ namespace micro_profiler

pair<image_patch_model::flattener::const_iterator, image_patch_model::flattener::const_iterator>
image_patch_model::flattener::equal_range(const tables::modules::value_type &from)
{ return make_pair(from.second.symbols.begin(), from.second.symbols.end()); }
{ return make_pair(from.symbols.begin(), from.symbols.end()); }

image_patch_model::flattener::const_reference image_patch_model::flattener::get(const tables::modules::value_type &l1, const symbol_info &l2)
{
record_type r = { make_tuple(l1.first, l2.rva), &l2 };
record_type r = { make_tuple(l1.id, l2.rva), &l2 };
return r;
}

Expand Down
109 changes: 88 additions & 21 deletions frontend/tests/FilePersistenceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ namespace micro_profiler
< make_pair(rhs.name, make_pair(rhs.rva, make_pair(rhs.size, make_pair(rhs.file_id, rhs.line))));
}

inline bool operator <(const mapped_module_ex &lhs, const mapped_module_ex &rhs)
{
return make_pair(lhs.persistent_id, make_pair(lhs.path, lhs.base))
< make_pair(rhs.persistent_id, make_pair(rhs.path, rhs.base));
}

inline bool operator <(const module_info_metadata &lhs, const module_info_metadata &rhs)
{ return make_pair(lhs.path, lhs.symbols) < make_pair(rhs.path, rhs.symbols); }

Expand Down Expand Up @@ -74,6 +68,19 @@ namespace micro_profiler
r.commit();
}
}

template <typename ContainerT>
void append(sdb::table<tables::module> &modules, const ContainerT &items)
{
for (auto i = begin(items); i != end(items); ++i)
{
auto r = modules.create();

(*r).id = i->first;
static_cast<module_info_metadata &>(*r) = i->second;
r.commit();
}
}
}

begin_test_suite( FilePersistenceTests )
Expand Down Expand Up @@ -170,13 +177,11 @@ namespace micro_profiler
{
// INIT
profiling_session ctx;
auto modules1 = plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]);

ctx.process_info.executable = "kjsdhgkjsdwwp.exe";
ctx.process_info.ticks_per_second = 0xF00000000ull;
append(ctx.statistics, statistics[0]);
add_records(ctx.mappings, plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]));
static_cast<tables::modules::base_t &>(ctx.modules) = tables::modules::base_t(modules1.begin(), modules1.end());
append(ctx.modules, plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]));
add_records(ctx.threads, threads[0]);

// ACT
Expand All @@ -197,13 +202,12 @@ namespace micro_profiler

// INIT
profiling_session ctx2;
auto modules2 = plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]);

ctx2.process_info.executable = "/usr/bin/grep";
ctx2.process_info.ticks_per_second = 0x1000ull;
append(ctx2.statistics, statistics[1]);
add_records(ctx2.mappings, plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]));
static_cast<tables::modules::base_t &>(ctx2.modules) = tables::modules::base_t(modules2.begin(), modules2.end());
append(ctx2.modules, plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]));
add_records(ctx2.threads, threads[1]);

// ACT
Expand Down Expand Up @@ -233,7 +237,7 @@ namespace micro_profiler
components1.process_info.ticks_per_second = 0xF00000000ull;
append(components1.statistics, statistics[0]);
assign_basic(components1.mappings, plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]));
assign_basic(components1.modules, plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]));
assign_basic(components1.modules, plural + make_module(10u, modules[0]) + make_module(4u, modules[1]));
assign_basic(components1.threads, plural
+ make_thread_info(1u, 111, "#1")
+ make_thread_info(3u, 112, "#2")
Expand All @@ -251,7 +255,7 @@ namespace micro_profiler
+ make_call_statistics(0, 4, 0, 0xF00115, 127, 0, 0, 0, 0)
+ make_call_statistics(0, 3, 0, 0xF00133, 12000, 0, 250, 0, 0), ctx1.statistics);
assert_equivalent(plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]), ctx1.mappings);
assert_equivalent(plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx1.modules);
assert_equivalent(plural + make_module(10u, modules[0]) + make_module(4u, modules[1]), ctx1.modules);
assert_equivalent(plural + make_thread_info(1u, 111, "#1") + make_thread_info(3u, 112, "#2") + make_thread_info(4u, 113, "#3"), ctx1.threads);

// INIT
Expand All @@ -262,7 +266,7 @@ namespace micro_profiler
components2.process_info.ticks_per_second = 0x1000ull;
append(components2.statistics, statistics[1]);
assign_basic(components2.mappings, plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]));
assign_basic(components2.modules, plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]));
assign_basic(components2.modules, plural + make_module(4u, modules[1]) + make_module(2u, modules[2]));
assign_basic(components2.threads, plural
+ make_thread_info(1u, 1211, "thread A")
+ make_thread_info(17u, 1212, "thread ABC"));
Expand All @@ -281,7 +285,7 @@ namespace micro_profiler
+ make_call_statistics(0, 1, 0, 0x9000FFF, 12000, 0, 250, 0, 0)
+ make_call_statistics(0, 17, 0, 0x9000FFF, 12000, 0, 250, 0, 0), ctx2.statistics);
assert_equivalent(plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]), ctx2.mappings);
assert_equivalent(plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx2.modules);
assert_equivalent(plural + make_module(4u, modules[1]) + make_module(2u, modules[2]), ctx2.modules);
assert_equivalent(plural + make_thread_info(1u, 1211, "thread A") + make_thread_info(17u, 1212, "thread ABC"), ctx2.threads);
}

Expand Down Expand Up @@ -311,7 +315,7 @@ namespace micro_profiler
+ make_call_statistics(0, 0, 0, 0xF00115, 127, 0, 0, 0, 0)
+ make_call_statistics(0, 0, 0, 0xF00133, 12000, 0, 250, 0, 0), ctx1.statistics);
assert_equivalent(plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]), ctx1.mappings);
assert_equivalent(plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx1.modules);
assert_equivalent(plural + make_module(10u, modules[0]) + make_module(4u, modules[1]), ctx1.modules);
assert_equal(ctx1.threads.end(), ctx1.threads.begin());

// INIT
Expand All @@ -337,7 +341,7 @@ namespace micro_profiler
+ make_call_statistics(0, 0, 0, 0xF00133, 127, 0, 8, 0, 0)
+ make_call_statistics(0, 0, 0, 0x9000FFF, 12000, 0, 250, 0, 0), ctx2.statistics);
assert_equivalent(plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]), ctx2.mappings);
assert_equivalent(plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx2.modules);
assert_equivalent(plural + make_module(4u, modules[1]) + make_module(2u, modules[2]), ctx2.modules);
assert_equal(ctx2.threads.end(), ctx2.threads.begin());
}

Expand Down Expand Up @@ -371,7 +375,7 @@ namespace micro_profiler
+ make_call_statistics(0, 4, 0, 0xF00115, 127, 0, 0, 0, 0)
+ make_call_statistics(0, 3, 0, 0xF00133, 12000, 0, 250, 0, 0), ctx1.statistics);
assert_equivalent(plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]), ctx1.mappings);
assert_equivalent(plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx1.modules);
assert_equivalent(plural + make_module(10u, modules[0]) + make_module(4u, modules[1]), ctx1.modules);
assert_equivalent(plural + make_thread_info(1u, 111, "#1") + make_thread_info(3u, 112, "#2") + make_thread_info(4u, 113, "#3"), ctx1.threads);

// INIT
Expand Down Expand Up @@ -401,7 +405,7 @@ namespace micro_profiler
+ make_call_statistics(0, 1, 0, 0x9000FFF, 12000, 0, 250, 0, 0)
+ make_call_statistics(0, 17, 0, 0x9000FFF, 12000, 0, 250, 0, 0), ctx2.statistics);
assert_equivalent(plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]), ctx2.mappings);
assert_equivalent(plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx2.modules);
assert_equivalent(plural + make_module(4u, modules[1]) + make_module(2u, modules[2]), ctx2.modules);
assert_equivalent(plural + make_thread_info(1u, 1211, "thread A") + make_thread_info(17u, 1212, "thread ABC"), ctx2.threads);
}

Expand Down Expand Up @@ -434,7 +438,7 @@ namespace micro_profiler
+ make_call_statistics(0, 4, 0, 0xF00115, 127, 0, 0, 0, 0)
+ make_call_statistics(0, 3, 0, 0xF00133, 12000, 0, 250, 0, 0), ctx1.statistics);
assert_equivalent(plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]), ctx1.mappings);
assert_equivalent(plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx1.modules);
assert_equivalent(plural + make_module(10u, modules[0]) + make_module(4u, modules[1]), ctx1.modules);
assert_equivalent(plural + make_thread_info(1u, 111, "#1") + make_thread_info(3u, 112, "#2") + make_thread_info(4u, 113, "#3"), ctx1.threads);

// INIT
Expand Down Expand Up @@ -464,7 +468,70 @@ namespace micro_profiler
+ make_call_statistics(0, 1, 0, 0x9000FFF, 12000, 0, 250, 0, 0)
+ make_call_statistics(0, 17, 0, 0x9000FFF, 12000, 0, 250, 0, 0), ctx2.statistics);
assert_equivalent(plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]), ctx2.mappings);
assert_equivalent(plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]), (containers::unordered_map<unsigned int, module_info_metadata> &)ctx2.modules);
assert_equivalent(plural + make_module(4u, modules[1]) + make_module(2u, modules[2]), ctx2.modules);
assert_equivalent(plural + make_thread_info(1u, 1211, "thread A") + make_thread_info(17u, 1212, "thread ABC"), ctx2.threads);
}


test( FileV6ContentIsFullyDeserialized )
{
// INIT
file_v6_components components1;
profiling_session ctx1;

components1.process_info.executable = "kjsdhgkjsdwwp.exe";
components1.process_info.ticks_per_second = 0xF00000000ull;
append(components1.statistics, statistics[0]);
assign_basic(components1.mappings, plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]));
assign_basic(components1.modules, plural + make_pair(10u, modules[0]) + make_pair(4u, modules[1]));
assign_basic(components1.threads, plural
+ make_thread_info(1u, 111, "#1")
+ make_thread_info(3u, 112, "#2")
+ make_thread_info(4u, 113, "#3"));
serialize_legacy(_buffer, components1);

// ACT
dser(ctx1);

// ASSERT
assert_equal(components1.process_info, ctx1.process_info);
assert_equivalent(plural
+ make_call_statistics(0, 1, 0, 0x100005, 123, 0, 1000, 0, 0)
+ make_call_statistics(0, 3, 0, 0x100017, 12, 0, 0, 0, 0)
+ make_call_statistics(0, 4, 0, 0xF00115, 127, 0, 0, 0, 0)
+ make_call_statistics(0, 3, 0, 0xF00133, 12000, 0, 250, 0, 0), ctx1.statistics);
assert_equivalent(plural + make_mapping(10u, mappings[0]) + make_mapping(11u, mappings[1]), ctx1.mappings);
assert_equivalent(plural + make_module(10u, modules[0]) + make_module(4u, modules[1]), ctx1.modules);
assert_equivalent(plural + make_thread_info(1u, 111, "#1") + make_thread_info(3u, 112, "#2") + make_thread_info(4u, 113, "#3"), ctx1.threads);

// INIT
file_v6_components components2;
profiling_session ctx2;

components2.process_info.executable = "/usr/bin/grep";
components2.process_info.ticks_per_second = 0x1000ull;
append(components2.statistics, statistics[1]);
assign_basic(components2.mappings, plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]));
assign_basic(components2.modules, plural + make_pair(4u, modules[1]) + make_pair(2u, modules[2]));
assign_basic(components2.threads, plural
+ make_thread_info(1u, 1211, "thread A")
+ make_thread_info(17u, 1212, "thread ABC"));
serialize_legacy(_buffer, components2);

// ACT
dser(ctx2);

// ASSERT
assert_equal(components2.process_info, ctx2.process_info);
assert_equivalent(plural
+ make_call_statistics(0, 1, 0, 0xF00115, 123, 0, 1000, 0, 0)
+ make_call_statistics(0, 1, 0, 0xF00023, 12, 0, 9, 0, 0)
+ make_call_statistics(0, 1, 0, 0xF00180, 127, 0, 10, 0, 0)
+ make_call_statistics(0, 1, 0, 0xF00133, 127, 0, 8, 0, 0)
+ make_call_statistics(0, 1, 0, 0x9000FFF, 12000, 0, 250, 0, 0)
+ make_call_statistics(0, 17, 0, 0x9000FFF, 12000, 0, 250, 0, 0), ctx2.statistics);
assert_equivalent(plural + make_mapping(0u, mappings[1]) + make_mapping(1u, mappings[2]), ctx2.mappings);
assert_equivalent(plural + make_module(4u, modules[1]) + make_module(2u, modules[2]), ctx2.modules);
assert_equivalent(plural + make_thread_info(1u, 1211, "thread A") + make_thread_info(17u, 1212, "thread ABC"), ctx2.threads);
}

Expand Down
Loading

0 comments on commit 227ed54

Please sign in to comment.