Skip to content

Commit 6b02e0d

Browse files
committed
Simplify ownership of ApplyMetricsBatch, fix small nits
1 parent ccd4a96 commit 6b02e0d

4 files changed

Lines changed: 23 additions & 26 deletions

File tree

‎src/ledger/SorobanMetrics.cpp‎

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,39 +211,31 @@ SorobanMetrics::accumulateLedgerWriteByte(uint64_t writeByte)
211211
SorobanMetrics::ApplyMetricsBatch&
212212
SorobanMetrics::getApplyThreadBatch()
213213
{
214-
// One entry per (thread, SorobanMetrics instance): tests can run several
215-
// applications in one process and threads outlive applications, so the
216-
// weak_ptr is re-validated on every first-use (the registry owns the
217-
// batches; a stale entry from a destroyed instance whose address got
218-
// reused expires together with its registry).
219-
static thread_local std::unordered_map<SorobanMetrics const*,
220-
std::weak_ptr<ApplyMetricsBatch>>
221-
tlBatches;
222-
auto& weak = tlBatches[this];
223-
if (auto existing = weak.lock())
214+
std::lock_guard<std::mutex> lock(mApplyBatchesMutex);
215+
auto [it, inserted] = mApplyBatches.try_emplace(std::this_thread::get_id());
216+
if (inserted)
224217
{
225-
return *existing;
218+
it->second = std::make_unique<ApplyMetricsBatch>();
226219
}
227-
auto batch = std::make_shared<ApplyMetricsBatch>();
228-
{
229-
std::lock_guard<std::mutex> lock(mApplyBatchesMutex);
230-
mApplyBatches.push_back(batch);
231-
}
232-
weak = batch;
233-
return *batch;
220+
return *it->second;
234221
}
235222

236223
void
237224
SorobanMetrics::flushApplyMetricsBatches()
238225
{
239-
std::vector<std::shared_ptr<ApplyMetricsBatch>> batches;
226+
std::vector<std::unique_ptr<ApplyMetricsBatch>> batches;
240227
{
241228
std::lock_guard<std::mutex> lock(mApplyBatchesMutex);
242-
batches = mApplyBatches;
243-
}
244-
if (batches.empty())
245-
{
246-
return;
229+
if (mApplyBatches.empty())
230+
{
231+
return;
232+
}
233+
batches.reserve(mApplyBatches.size());
234+
for (auto& [_, v] : mApplyBatches)
235+
{
236+
batches.emplace_back(std::move(v));
237+
}
238+
mApplyBatches.clear();
247239
}
248240

249241
ApplyMetricsBatch total;

‎src/ledger/SorobanMetrics.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <cstdint>
1313
#include <memory>
1414
#include <mutex>
15+
#include <thread>
16+
#include <unordered_map>
1517
#include <vector>
1618

1719
namespace medida
@@ -97,7 +99,8 @@ class SorobanMetrics
9799
// All per-thread batches handed out by getApplyThreadBatch(), drained on
98100
// each publishAndResetLedgerWideMetrics() call.
99101
std::mutex mApplyBatchesMutex;
100-
std::vector<std::shared_ptr<ApplyMetricsBatch>> mApplyBatches;
102+
std::unordered_map<std::thread::id, std::unique_ptr<ApplyMetricsBatch>>
103+
mApplyBatches;
101104

102105
void flushApplyMetricsBatches();
103106

‎src/transactions/test/InvokeHostFunctionTests.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7641,7 +7641,7 @@ TEST_CASE("module cache rebuild on incremental wasm uploads",
76417641
static_cast<uint64_t>(rebuildBytesAtStartup));
76427642
}
76437643

7644-
TEST_CASE("soroban metrics published at ledger close", "[tx][soroban]")
7644+
TEST_CASE("soroban metrics published at ledger close", "[soroban]")
76457645
{
76467646
SorobanTest test;
76477647
auto const& contract =

‎src/util/SimpleTimer.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
#include <atomic>
44
#include <chrono>
5+
#include <cstdint>
56
#include <medida/counter.h>
67
#include <medida/metric_name.h>
8+
#include <string>
79

810
namespace stellar
911
{

0 commit comments

Comments
 (0)