Skip to content

Commit 7c0894d

Browse files
committed
Implement more feedback on naming, and finally add some multithreaded unit tests
1 parent 72c6f2f commit 7c0894d

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

lldb/include/lldb/Target/Statistics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class SummaryStatisticsCache {
233233
/// Get the SummaryStatistics object for a given provider name, or insert
234234
/// if statistics for that provider is not in the map.
235235
SummaryStatisticsSP
236-
GetSummaryStatisticsForProviderName(lldb_private::TypeSummaryImpl &provider) {
236+
GetSummaryStatisticsForProvider(lldb_private::TypeSummaryImpl &provider) {
237237
std::lock_guard<std::mutex> guard(m_map_mutex);
238238
if (auto iterator = m_summary_stats_map.find(provider.GetName());
239239
iterator != m_summary_stats_map.end())

lldb/source/Core/ValueObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
620620
// Get Shared pointer to the summary statistics container
621621
SummaryStatisticsSP stats_sp =
622622
target_sp->GetSummaryStatisticsCache()
623-
.GetSummaryStatisticsForProviderName(*summary_ptr);
623+
.GetSummaryStatisticsForProvider(*summary_ptr);
624624

625625
// Construct RAII types to time and collect data on summary creation.
626626
SummaryStatistics::SummaryInvocation invocation(stats_sp);

lldb/source/Target/Target.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3196,7 +3196,7 @@ void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); }
31963196

31973197
lldb_private::SummaryStatisticsSP Target::GetSummaryStatisticsSPForProviderName(
31983198
lldb_private::TypeSummaryImpl &summary_provider) {
3199-
return m_summary_statistics_cache.GetSummaryStatisticsForProviderName(
3199+
return m_summary_statistics_cache.GetSummaryStatisticsForProvider(
32003200
summary_provider);
32013201
}
32023202

lldb/unittests/Target/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_lldb_unittest(TargetTests
1111
RegisterFlagsTest.cpp
1212
RemoteAwarePlatformTest.cpp
1313
StackFrameRecognizerTest.cpp
14+
SummaryStatisticsTest.cpp
1415
FindFileTest.cpp
1516

1617
LINK_LIBS
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "lldb/DataFormatters/TypeSummary.h"
2+
#include "lldb/Target/Statistics.h"
3+
#include "lldb/lldb-forward.h"
4+
#include "lldb/lldb-private-enumerations.h"
5+
#include "lldb/lldb-private.h"
6+
#include "lldb/Utility/Stream.h"
7+
#include "llvm/Testing/Support/Error.h"
8+
#include "gtest/gtest.h"
9+
#include <thread>
10+
11+
using namespace lldb_private;
12+
using Duration = std::chrono::duration<double>;
13+
14+
class DummySummaryImpl : public lldb_private::TypeSummaryImpl {
15+
public:
16+
DummySummaryImpl(Duration sleepTime):
17+
TypeSummaryImpl(TypeSummaryImpl::Kind::eSummaryString, TypeSummaryImpl::Flags()),
18+
m_sleepTime(sleepTime) {}
19+
20+
std::string GetName() override {
21+
return "DummySummary";
22+
}
23+
24+
std::string GetSummaryKindName() override {
25+
return "dummy";
26+
}
27+
28+
std::string GetDescription() override {
29+
return "";
30+
}
31+
32+
bool FormatObject(ValueObject *valobj, std::string &dest,
33+
const TypeSummaryOptions &options) override {
34+
return false;
35+
}
36+
37+
void FakeFormat() {
38+
std::this_thread::sleep_for(m_sleepTime);
39+
}
40+
41+
private:
42+
Duration m_sleepTime;
43+
};
44+
45+
TEST(MultithreadFormatting, Multithread) {
46+
SummaryStatisticsCache statistics_cache;
47+
DummySummaryImpl summary(Duration(1));
48+
std::vector<std::thread> threads;
49+
for (int i = 0; i < 10; ++i) {
50+
threads.emplace_back(std::thread([&statistics_cache, &summary]() {
51+
auto sp = statistics_cache.GetSummaryStatisticsForProvider(summary);
52+
{
53+
SummaryStatistics::SummaryInvocation invocation(sp);
54+
summary.FakeFormat();
55+
}
56+
}));
57+
}
58+
59+
for (auto &thread : threads)
60+
thread.join();
61+
62+
auto sp = statistics_cache.GetSummaryStatisticsForProvider(summary);
63+
ASSERT_TRUE(sp->GetDurationReference().get().count() > 10);
64+
ASSERT_TRUE(sp->GetSummaryCount() == 10);
65+
66+
std::string stats_as_json;
67+
llvm::raw_string_ostream ss(stats_as_json);
68+
ss << sp->ToJSON();
69+
ASSERT_THAT(stats_as_json, ::testing::HasSubstr("\"name\":\"DummySummary\""));
70+
ASSERT_THAT(stats_as_json, ::testing::HasSubstr("\"type\":\"dummy\""));
71+
}

0 commit comments

Comments
 (0)