Skip to content

Commit 5db41c3

Browse files
committed
Implemented copy tracker for debugging
1 parent 7a2d21c commit 5db41c3

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ OPTION(ENABLE_INTEGRATION_TEST "Enable integration tests" OFF)
102102
MESSAGE(STATUS "🔧 Enable integration tests: ${ENABLE_INTEGRATION_TEST}")
103103
OPTION(ENABLE_INTEGRATION_TEST "Creates json_reader target and enable integration tests" OFF)
104104
OPTION(CREATE_JSON_READER_TARGET "Create json_reader target, automatically set when ENABLE_INTEGRATION_TEST is ON" OFF)
105+
OPTION(TRACK_COPIES, "Track copies in tests" OFF)
105106

106107
if(ENABLE_INTEGRATION_TEST)
107108
set(CREATE_JSON_READER_TARGET ON)
@@ -181,6 +182,11 @@ if(SPARROW_CONTRACTS_THROW_ON_FAILURE)
181182
list(APPEND SPARROW_COMPILE_DEFINITIONS SPARROW_CONTRACTS_THROW_ON_FAILURE=1)
182183
endif()
183184

185+
if(TRACK_COPIES)
186+
message(STATUS "Tracking copies in tests")
187+
list(APPEND SPARROW_COMPILE_DEFINITIONS SPARROW_TRACK_COPIES)
188+
endif()
189+
184190
# Build
185191
# =====
186192
set(BINARY_BUILD_DIR "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}")
@@ -227,6 +233,9 @@ set(SPARROW_HEADERS
227233
${SPARROW_INCLUDE_DIR}/sparrow/config/config.hpp
228234
${SPARROW_INCLUDE_DIR}/sparrow/config/sparrow_version.hpp
229235

236+
# debug
237+
${SPARROW_INCLUDE_DIR}/sparrow/debug/copy_tracker.hpp
238+
230239
# detail
231240
${SPARROW_INCLUDE_DIR}/sparrow/details/3rdparty/float16_t.hpp
232241

@@ -323,6 +332,7 @@ set(SPARROW_SRC
323332
${SPARROW_SOURCE_DIR}/arrow_interface/arrow_array_schema_proxy.cpp
324333
${SPARROW_SOURCE_DIR}/arrow_interface/arrow_schema.cpp
325334
${SPARROW_SOURCE_DIR}/arrow_interface/private_data_ownership.cpp
335+
${SPARROW_SOURCE_DIR}/debug/copy_tracker.cpp
326336
${SPARROW_SOURCE_DIR}/layout/array_factory.cpp
327337
${SPARROW_SOURCE_DIR}/layout/array_helper.cpp
328338
${SPARROW_SOURCE_DIR}/layout/fixed_width_binary_array_utils.cpp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
#include "sparrow/config/config.hpp"
7+
8+
namespace sparrow::copy_tracker
9+
{
10+
template <class T>
11+
std::string key();
12+
13+
constexpr bool is_enabled()
14+
{
15+
#ifdef SPARROW_TRACK_COPIES
16+
return true;
17+
#else
18+
return false;
19+
#endif
20+
}
21+
22+
SPARROW_API void increase(const std::string& key);
23+
SPARROW_API void reset(const std::string& key);
24+
SPARROW_API void reset_all();
25+
SPARROW_API int count(const std::string& key, int disabled_value = 0);
26+
SPARROW_API std::vector<std::string> key_list();
27+
28+
}
29+

src/debug/copy_tracker.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "sparrow/debug/copy_tracker.hpp"
2+
3+
#include <map>
4+
#include <ranges>
5+
#include <string>
6+
7+
namespace sparrow::copy_tracker
8+
{
9+
#ifdef SPARROW_TRACK_COPIES
10+
11+
class copy_tracker_impl
12+
{
13+
public:
14+
15+
static copy_tracker_impl& instance()
16+
{
17+
static copy_tracker_impl inst;
18+
return inst;
19+
}
20+
21+
void increase(const std::string& key)
22+
{
23+
m_copy_count[key] += 1;
24+
}
25+
26+
void reset(const std::string& key)
27+
{
28+
m_copy_count[key] = 0;
29+
}
30+
31+
void reset_all()
32+
{
33+
m_copy_count.clear();
34+
}
35+
36+
int count(const std::string& key)
37+
{
38+
return m_copy_count[key];
39+
}
40+
41+
auto key_list()
42+
{
43+
return std::views::keys(m_copy_count);
44+
}
45+
46+
private:
47+
48+
copy_tracker_impl() = default;
49+
50+
std::map<std::string, int> m_copy_count = {};
51+
};
52+
53+
void increase(const std::string& key)
54+
{
55+
return copy_tracker_impl::instance().increase(key);
56+
}
57+
58+
void reset(const std::string& key)
59+
{
60+
return copy_tracker_impl::instance().reset(key);
61+
}
62+
63+
void reset_all()
64+
{
65+
return copy_tracker_impl::instance().reset_all();
66+
}
67+
68+
int count(const std::string& key, int /*disabled_value*/)
69+
{
70+
return copy_tracker_impl::instance().count(key);
71+
}
72+
73+
std::vector<std::string> key_list()
74+
{
75+
auto keys = copy_tracker_impl::instance().key_list();
76+
return { keys.begin(), keys.end() };
77+
}
78+
79+
#else
80+
81+
void increase(const std::string&)
82+
{
83+
}
84+
85+
void reset(const std::string&)
86+
{
87+
}
88+
89+
void reset_all()
90+
{
91+
}
92+
93+
int count(const std::string&, int disabled_value)
94+
{
95+
return disabled_value;
96+
}
97+
98+
std::vector<std::string> key_list()
99+
{
100+
return {};
101+
}
102+
103+
#endif
104+
105+
}

src/record_batch.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616

1717
#include <unordered_set>
1818

19+
#include "sparrow/debug/copy_tracker.hpp"
1920
#include "sparrow/utils/contracts.hpp"
2021

2122
namespace sparrow
2223
{
24+
namespace copy_tracker
25+
{
26+
template <>
27+
SPARROW_API std::string key<record_batch>()
28+
{
29+
return "record_batch";
30+
}
31+
}
32+
2333
record_batch::record_batch(initializer_type init)
2434
{
2535
m_name_list.reserve(init.size());
@@ -97,6 +107,7 @@ namespace sparrow
97107
, m_array_list(rhs.m_array_list)
98108
{
99109
update_array_map_cache();
110+
copy_tracker::increase(copy_tracker::key<record_batch>());
100111
}
101112

102113
record_batch& record_batch::operator=(const record_batch& rhs)

test/test_record_batch.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <string_view>
1616

17+
#include "sparrow/debug/copy_tracker.hpp"
1718
#include "sparrow/primitive_array.hpp"
1819
#include "sparrow/record_batch.hpp"
1920

@@ -199,9 +200,16 @@ namespace sparrow
199200

200201
TEST_CASE("copy semantic")
201202
{
203+
#ifdef SPARROW_TRACK_COPIES
204+
auto key = std::string("record_batch");//copy_tracker::key<record_batch>();
205+
copy_tracker::reset(key);
206+
#endif
202207
auto record1 = make_record_batch(col_size);
203208
auto record2(record1);
204209
CHECK_EQ(record1, record2);
210+
#ifdef SPARROW_TRACK_COPIES
211+
CHECK_EQ(copy_tracker::count(key), 1);
212+
#endif
205213

206214
auto record3 = make_record_batch(col_size + 2u);
207215
CHECK_NE(record1, record3);
@@ -330,6 +338,17 @@ namespace sparrow
330338
);
331339
CHECK(res);
332340
}
341+
#ifdef SPARROW_TRACK_COPIES
342+
TEST_CASE("emplace_back")
343+
{
344+
std::vector<record_batch> vec(3, make_record_batch(col_size));
345+
std::cout << "capacity: " << vec.capacity() << std::endl;
346+
auto key = std::string("record_batch");//copy_tracker::key<record_batch>();
347+
copy_tracker::reset(key);
348+
vec.emplace_back(make_record_batch(col_size));
349+
CHECK_EQ(copy_tracker::count(key), 0);
350+
}
351+
#endif
333352

334353
TEST_CASE("extract_arrow_structures")
335354
{

0 commit comments

Comments
 (0)