Skip to content

Commit 2724412

Browse files
authored
Accumulated updates for heap profiling (pingcap#9240) (pingcap#247)
Signed-off-by: CalvinNeo <[email protected]>
1 parent 9395cec commit 2724412

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

dbms/src/Interpreters/AsynchronousMetrics.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ void AsynchronousMetrics::update()
311311
set("LogDiskBytes", usage.total_log_disk_size);
312312
set("PagesInMem", usage.num_pages);
313313
set("VersionedEntries", DB::PS::PageStorageMemorySummary::versioned_entry_or_delete_count.load());
314+
set("UniversalWrite", DB::PS::PageStorageMemorySummary::universal_write_count.load());
314315
}
315316

316317
if (context.getSharedContextDisagg()->isDisaggregatedStorageMode())

dbms/src/Storages/Page/PageStorageMemorySummary.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct PageStorageMemorySummary
2323
static inline std::atomic_int64_t uni_page_id_bytes{0};
2424
static inline std::atomic_int64_t versioned_entry_or_delete_bytes{0};
2525
static inline std::atomic_int64_t versioned_entry_or_delete_count{0};
26+
static inline std::atomic_int64_t universal_write_count{0};
2627
};
2728

28-
} // namespace DB::PS
29+
} // namespace DB::PS

dbms/src/Storages/Page/V3/Universal/UniversalWriteBatchImpl.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class UniversalWriteBatch : private boost::noncopyable
5353
: prefix(std::move(prefix_))
5454
{}
5555

56+
~UniversalWriteBatch() { PS::PageStorageMemorySummary::universal_write_count.fetch_sub(writes.size()); }
57+
5658
void putPage(
5759
PageIdU64 page_id,
5860
UInt64 tag,
@@ -126,6 +128,7 @@ class UniversalWriteBatch : private boost::noncopyable
126128
Write w{WriteBatchWriteType::PUT, page_id, tag, read_buffer, size, "", std::move(offsets)};
127129
total_data_size += size;
128130
writes.emplace_back(std::move(w));
131+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
129132
}
130133

131134
void putPage(
@@ -156,6 +159,7 @@ class UniversalWriteBatch : private boost::noncopyable
156159
data_location};
157160
writes.emplace_back(std::move(w));
158161
has_writes_from_remote = true;
162+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
159163
}
160164

161165
void updateRemotePage(const UniversalPageId & page_id, const ReadBufferPtr & read_buffer, PageSize size)
@@ -164,13 +168,15 @@ class UniversalWriteBatch : private boost::noncopyable
164168
total_data_size += size;
165169
writes.emplace_back(std::move(w));
166170
// This is use for update local page data from remote, don't need to set `has_writes_from_remote`
171+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
167172
}
168173

169174
void putExternal(const UniversalPageId & page_id, UInt64 tag)
170175
{
171176
// External page's data is not managed by PageStorage, which means data is empty.
172177
Write w{WriteBatchWriteType::PUT_EXTERNAL, page_id, tag, nullptr, 0, "", {}};
173178
writes.emplace_back(std::move(w));
179+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
174180
}
175181

176182
void putRemoteExternal(const UniversalPageId & page_id, const PS::V3::CheckpointLocation & data_location)
@@ -179,19 +185,22 @@ class UniversalWriteBatch : private boost::noncopyable
179185
Write w{WriteBatchWriteType::PUT_EXTERNAL, page_id, /*tag*/ 0, nullptr, 0, "", {}, data_location};
180186
writes.emplace_back(std::move(w));
181187
has_writes_from_remote = true;
188+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
182189
}
183190

184191
// Add RefPage{ref_id} -> Page{page_id}
185192
void putRefPage(const UniversalPageId & ref_id, const UniversalPageId & page_id)
186193
{
187194
Write w{WriteBatchWriteType::REF, ref_id, 0, nullptr, 0, page_id, {}};
188195
writes.emplace_back(std::move(w));
196+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
189197
}
190198

191199
void delPage(const UniversalPageId & page_id)
192200
{
193201
Write w{WriteBatchWriteType::DEL, page_id, 0, nullptr, 0, "", {}};
194202
writes.emplace_back(std::move(w));
203+
PS::PageStorageMemorySummary::universal_write_count.fetch_add(1);
195204
}
196205

197206
bool empty() const { return writes.empty(); }
@@ -276,11 +285,13 @@ class UniversalWriteBatch : private boost::noncopyable
276285

277286
UniversalWriteBatch(UniversalWriteBatch && rhs) noexcept
278287
: prefix(std::move(rhs.prefix))
279-
, writes(std::move(rhs.writes))
280288
, total_data_size(rhs.total_data_size)
281289
, has_writes_from_remote(rhs.has_writes_from_remote)
282290
, remote_lock_disabled(rhs.remote_lock_disabled)
283-
{}
291+
{
292+
PS::PageStorageMemorySummary::universal_write_count.fetch_sub(writes.size());
293+
writes = std::move(rhs.writes);
294+
}
284295

285296
void swap(UniversalWriteBatch & o)
286297
{

dbms/src/Storages/Page/V3/Universal/tests/gtest_universal_page_storage.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -568,18 +568,58 @@ TEST(UniPageStorageIdTest, UniversalPageIdMemoryTrace)
568568
auto u_id = UniversalPageIdFormat::toFullPageId("aaa", 100);
569569
auto page1_mem = PS::PageStorageMemorySummary::uni_page_id_bytes.load();
570570
auto ps = page1_mem - prim_mem;
571+
// copy construct
571572
auto u_id_cpy = u_id;
572573
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 2);
574+
// move assignment
573575
UniversalPageId u_id_mv = UniversalPageIdFormat::toFullPageId("aaa", 100);
574576
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 3);
575577
u_id_mv = std::move(u_id_cpy);
576578
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 2);
579+
// copy assignment
577580
UniversalPageId u_id_cpy2 = UniversalPageIdFormat::toFullPageId("aaa", 100);
578581
u_id_cpy2 = u_id_mv;
579582
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 3);
583+
// move construct
584+
auto u_id_mv2 = std::move(u_id_cpy2);
585+
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 3);
580586
}
581587
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem);
582588
}
583589

590+
591+
TEST(UniPageStorageIdTest, UniversalWriteBatchMemory)
592+
{
593+
const String prefix = "aaa";
594+
const UInt64 tag = 0;
595+
static constexpr size_t buf_sz = 1024;
596+
char c_buff[buf_sz] = {};
597+
for (size_t i = 0; i < buf_sz; ++i)
598+
{
599+
c_buff[i] = i % 0xff;
600+
}
601+
{
602+
UniversalWriteBatch wb;
603+
wb.putPage(
604+
UniversalPageIdFormat::toFullPageId(prefix, 0),
605+
tag,
606+
std::make_shared<ReadBufferFromMemory>(c_buff, buf_sz),
607+
buf_sz);
608+
ASSERT_EQ(PageStorageMemorySummary::universal_write_count.load(), 1);
609+
}
610+
ASSERT_EQ(PageStorageMemorySummary::universal_write_count.load(), 0);
611+
{
612+
UniversalWriteBatch wb;
613+
wb.putPage(
614+
UniversalPageIdFormat::toFullPageId(prefix, 0),
615+
tag,
616+
std::make_shared<ReadBufferFromMemory>(c_buff, buf_sz),
617+
buf_sz);
618+
UniversalWriteBatch wb2 = std::move(wb);
619+
ASSERT_EQ(PageStorageMemorySummary::universal_write_count.load(), 1);
620+
}
621+
ASSERT_EQ(PageStorageMemorySummary::universal_write_count.load(), 0);
622+
}
623+
584624
} // namespace PS::universal::tests
585625
} // namespace DB

dbms/src/Storages/Page/V3/tests/gtest_wal_log.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,5 @@ TEST(LogFileRWTest2, ManuallySync)
867867
ASSERT_EQ(scratch, payload);
868868
}
869869
}
870+
870871
} // namespace DB::PS::V3::tests

libs/libcommon/cmake/find_jemalloc.cmake

+4-11
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Only enable under linux
16-
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
17-
set(ENABLE_JEMALLOC_DEFAULT 1)
18-
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
19-
set(ENABLE_JEMALLOC_DEFAULT 0)
20-
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
21-
set(ENABLE_JEMALLOC_DEFAULT 0)
22-
endif()
23-
24-
option (ENABLE_JEMALLOC "Set to TRUE to use jemalloc" ${ENABLE_JEMALLOC_DEFAULT})
25-
# TODO: Make ENABLE_JEMALLOC_PROF default value to ${ENABLE_JEMALLOC_DEFAULT} after https://github.com/pingcap/tics/issues/3236 get fixed.
15+
option (ENABLE_JEMALLOC "Set to TRUE to use jemalloc" ON)
16+
# 1. The deadlock mentioned in https://github.com/pingcap/tics/issues/3236 is not related to ENABLE_JEMALLOC_PROF.
17+
# 2. It is also expected to be eliminated even if the heap profiling is activated, with a newer version of pprof-rs.
18+
# TODO: Enable continuous heap profiling after we make sure statement 2.
2619
option (ENABLE_JEMALLOC_PROF "Set to ON to enable jemalloc profiling" ON)
2720
option (USE_INTERNAL_JEMALLOC_LIBRARY "Set to FALSE to use system jemalloc library instead of bundled" ${NOT_UNBUNDLED})
2821

0 commit comments

Comments
 (0)