Skip to content

Commit b4dc782

Browse files
committed
fix: fix memory tracker on macOS
1 parent 58388ce commit b4dc782

File tree

8 files changed

+14
-75
lines changed

8 files changed

+14
-75
lines changed

programs/local/EmbeddedServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
# include <azure/storage/common/internal/xml_wrapper.hpp>
7575
#endif
7676

77+
bool chdb_embedded_server_initialized = false;
78+
7779
namespace fs = std::filesystem;
7880

7981
namespace CurrentMetrics
@@ -511,6 +513,8 @@ try
511513
global_register_once_flag,
512514
[]()
513515
{
516+
chdb_embedded_server_initialized = true;
517+
514518
registerInterpreters();
515519
/// Don't initialize DateLUT
516520
registerFunctions();

programs/local/LocalServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
# include <azure/storage/common/internal/xml_wrapper.hpp>
7474
#endif
7575

76+
extern bool chdb_embedded_server_initialized;
77+
7678
namespace fs = std::filesystem;
7779

7880
namespace CurrentMetrics
@@ -648,6 +650,8 @@ try
648650
global_register_once_flag,
649651
[]()
650652
{
653+
chdb_embedded_server_initialized = true;
654+
651655
registerInterpreters();
652656
/// Don't initialize DateLUT
653657
registerFunctions();

programs/local/chdb-arrow.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,13 @@ chdb_state chdb_arrow_scan(
134134
chdb_connection conn, const char * table_name,
135135
chdb_arrow_stream arrow_stream)
136136
{
137-
CHDB::ChdbMemoryTrackingGuard guard;
138-
139137
return chdb_inner_arrow_scan(conn, table_name, arrow_stream, false);
140138
}
141139

142140
chdb_state chdb_arrow_array_scan(
143141
chdb_connection conn, const char * table_name,
144142
chdb_arrow_schema arrow_schema, chdb_arrow_array arrow_array)
145143
{
146-
CHDB::ChdbMemoryTrackingGuard guard;
147-
148144
auto * private_data = new CHDB::PrivateData();
149145
private_data->schema = reinterpret_cast<ArrowSchema *>(arrow_schema);
150146
private_data->array = reinterpret_cast<ArrowArray *>(arrow_array);
@@ -162,8 +158,6 @@ chdb_state chdb_arrow_array_scan(
162158

163159
chdb_state chdb_arrow_unregister_table(chdb_connection conn, const char * table_name)
164160
{
165-
CHDB::ChdbMemoryTrackingGuard guard;
166-
167161
if (!table_name)
168162
return CHDBError;
169163

programs/local/chdb-internal.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,9 @@ inline bool checkConnectionValidity(chdb_conn * connection)
1818
return connection && connection->connected;
1919
}
2020

21-
extern thread_local bool chdb_memory_tracking;
22-
2321
namespace CHDB
2422
{
2523

26-
class ChdbMemoryTrackingGuard
27-
{
28-
public:
29-
ChdbMemoryTrackingGuard()
30-
: previous_value(chdb_memory_tracking)
31-
{
32-
chdb_memory_tracking = true;
33-
}
34-
35-
~ChdbMemoryTrackingGuard()
36-
{
37-
chdb_memory_tracking = previous_value;
38-
}
39-
40-
private:
41-
bool previous_value;
42-
};
43-
4424
std::unique_ptr<MaterializedQueryResult> pyEntryClickHouseLocal(int argc, char ** argv);
4525

4626
const std::string & chdb_result_error_string(chdb_result * result);

programs/local/chdb.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ const std::string & chdb_streaming_result_error_string(chdb_streaming_result * r
154154

155155
chdb_connection * connect_chdb_with_exception(int argc, char ** argv)
156156
{
157-
CHDB::ChdbMemoryTrackingGuard guard;
158-
159157
try
160158
{
161159
DB::ThreadStatus thread_status;
@@ -189,8 +187,6 @@ chdb_connection * connect_chdb_with_exception(int argc, char ** argv)
189187
#if USE_PYTHON
190188
void cachePythonTablesFromQuery(chdb_conn * conn, const std::string & query_str)
191189
{
192-
CHDB::ChdbMemoryTrackingGuard guard;
193-
194190
if (!conn || !conn->server || !conn->connected)
195191
return;
196192
auto * client = reinterpret_cast<DB::ChdbClient *>(conn->server);
@@ -204,8 +200,6 @@ using namespace CHDB;
204200

205201
local_result * query_stable(int argc, char ** argv)
206202
{
207-
CHDB::ChdbMemoryTrackingGuard guard;
208-
209203
auto query_result = pyEntryClickHouseLocal(argc, argv);
210204
if (!query_result->getError().empty() || query_result->result_buffer == nullptr)
211205
return nullptr;
@@ -222,8 +216,6 @@ local_result * query_stable(int argc, char ** argv)
222216

223217
void free_result(local_result * result)
224218
{
225-
CHDB::ChdbMemoryTrackingGuard guard;
226-
227219
if (!result)
228220
{
229221
return;
@@ -239,8 +231,6 @@ void free_result(local_result * result)
239231

240232
local_result_v2 * query_stable_v2(int argc, char ** argv)
241233
{
242-
CHDB::ChdbMemoryTrackingGuard guard;
243-
244234
// pyEntryClickHouseLocal may throw some serious exceptions, although it's not likely
245235
// to happen in the context of clickhouse-local. we catch them here and return an error
246236
local_result_v2 * res = nullptr;
@@ -269,8 +259,6 @@ local_result_v2 * query_stable_v2(int argc, char ** argv)
269259

270260
void free_result_v2(local_result_v2 * result)
271261
{
272-
CHDB::ChdbMemoryTrackingGuard guard;
273-
274262
if (!result)
275263
return;
276264

@@ -281,8 +269,6 @@ void free_result_v2(local_result_v2 * result)
281269

282270
chdb_conn ** connect_chdb(int argc, char ** argv)
283271
{
284-
CHDB::ChdbMemoryTrackingGuard guard;
285-
286272
auto * connection = chdb_connect(argc, argv);
287273
if (!connection)
288274
{
@@ -293,8 +279,6 @@ chdb_conn ** connect_chdb(int argc, char ** argv)
293279

294280
void close_conn(chdb_conn ** conn)
295281
{
296-
CHDB::ChdbMemoryTrackingGuard guard;
297-
298282
if (!conn || !*conn)
299283
return;
300284

@@ -324,8 +308,6 @@ struct local_result_v2 * query_conn(chdb_conn * conn, const char * query, const
324308

325309
struct local_result_v2 * query_conn_n(struct chdb_conn * conn, const char * query, size_t query_len, const char * format, size_t format_len)
326310
{
327-
CHDB::ChdbMemoryTrackingGuard guard;
328-
329311
if (!checkConnectionValidity(conn))
330312
return createErrorLocalResultV2("Invalid or closed connection");
331313

@@ -347,16 +329,12 @@ struct local_result_v2 * query_conn_n(struct chdb_conn * conn, const char * quer
347329

348330
chdb_streaming_result * query_conn_streaming(chdb_conn * conn, const char * query, const char * format)
349331
{
350-
CHDB::ChdbMemoryTrackingGuard guard;
351-
352332
return query_conn_streaming_n(conn, query, query ? std::strlen(query) : 0, format, format ? std::strlen(format) : 0);
353333
}
354334

355335
chdb_streaming_result *
356336
query_conn_streaming_n(struct chdb_conn * conn, const char * query, size_t query_len, const char * format, size_t format_len)
357337
{
358-
CHDB::ChdbMemoryTrackingGuard guard;
359-
360338
if (!checkConnectionValidity(conn))
361339
{
362340
auto * result = new StreamQueryResult("Invalid or closed connection");
@@ -391,8 +369,6 @@ query_conn_streaming_n(struct chdb_conn * conn, const char * query, size_t query
391369

392370
const char * chdb_streaming_result_error(chdb_streaming_result * result)
393371
{
394-
CHDB::ChdbMemoryTrackingGuard guard;
395-
396372
if (!result)
397373
return nullptr;
398374

@@ -407,8 +383,6 @@ const char * chdb_streaming_result_error(chdb_streaming_result * result)
407383

408384
local_result_v2 * chdb_streaming_fetch_result(chdb_conn * conn, chdb_streaming_result * result)
409385
{
410-
CHDB::ChdbMemoryTrackingGuard guard;
411-
412386
if (!checkConnectionValidity(conn))
413387
return createErrorLocalResultV2("Invalid or closed connection");
414388

@@ -438,8 +412,6 @@ local_result_v2 * chdb_streaming_fetch_result(chdb_conn * conn, chdb_streaming_r
438412

439413
void chdb_streaming_cancel_query(chdb_conn * conn, chdb_streaming_result * result)
440414
{
441-
CHDB::ChdbMemoryTrackingGuard guard;
442-
443415
if (!checkConnectionValidity(conn))
444416
return;
445417

@@ -460,8 +432,6 @@ void chdb_streaming_cancel_query(chdb_conn * conn, chdb_streaming_result * resul
460432

461433
void chdb_destroy_result(chdb_streaming_result * result)
462434
{
463-
CHDB::ChdbMemoryTrackingGuard guard;
464-
465435
if (!result)
466436
return;
467437

@@ -474,8 +444,6 @@ void chdb_destroy_result(chdb_streaming_result * result)
474444

475445
chdb_connection * chdb_connect(int argc, char ** argv)
476446
{
477-
CHDB::ChdbMemoryTrackingGuard guard;
478-
479447
try
480448
{
481449
return connect_chdb_with_exception(argc, argv);
@@ -505,8 +473,6 @@ chdb_connection * chdb_connect(int argc, char ** argv)
505473

506474
void chdb_close_conn(chdb_connection * conn)
507475
{
508-
CHDB::ChdbMemoryTrackingGuard guard;
509-
510476
if (!conn || !*conn)
511477
return;
512478

@@ -522,8 +488,6 @@ chdb_result * chdb_query(chdb_connection conn, const char * query, const char *
522488

523489
chdb_result * chdb_query_n(chdb_connection conn, const char * query, size_t query_len, const char * format, size_t format_len)
524490
{
525-
CHDB::ChdbMemoryTrackingGuard guard;
526-
527491
if (!conn)
528492
{
529493
auto * result = new MaterializedQueryResult("Unexpected null connection");
@@ -559,8 +523,6 @@ chdb_result * chdb_query_n(chdb_connection conn, const char * query, size_t quer
559523

560524
chdb_result * chdb_query_cmdline(int argc, char ** argv)
561525
{
562-
CHDB::ChdbMemoryTrackingGuard guard;
563-
564526
MaterializedQueryResult * result = nullptr;
565527
try
566528
{
@@ -587,8 +549,6 @@ chdb_result * chdb_stream_query(chdb_connection conn, const char * query, const
587549

588550
chdb_result * chdb_stream_query_n(chdb_connection conn, const char * query, size_t query_len, const char * format, size_t format_len)
589551
{
590-
CHDB::ChdbMemoryTrackingGuard guard;
591-
592552
if (!conn)
593553
{
594554
auto * result = new StreamQueryResult("Unexpected null connection");
@@ -629,8 +589,6 @@ chdb_result * chdb_stream_query_n(chdb_connection conn, const char * query, size
629589

630590
chdb_result * chdb_stream_fetch_result(chdb_connection conn, chdb_result * result)
631591
{
632-
CHDB::ChdbMemoryTrackingGuard guard;
633-
634592
if (!conn)
635593
{
636594
auto * query_result = new MaterializedQueryResult("Unexpected null connection");
@@ -676,8 +634,6 @@ chdb_result * chdb_stream_fetch_result(chdb_connection conn, chdb_result * resul
676634

677635
void chdb_stream_cancel_query(chdb_connection conn, chdb_result * result)
678636
{
679-
CHDB::ChdbMemoryTrackingGuard guard;
680-
681637
if (!result || !conn)
682638
return;
683639

@@ -700,8 +656,6 @@ void chdb_stream_cancel_query(chdb_connection conn, chdb_result * result)
700656

701657
void chdb_destroy_query_result(chdb_result * result)
702658
{
703-
CHDB::ChdbMemoryTrackingGuard guard;
704-
705659
if (!result)
706660
return;
707661

src/Common/CurrentMemoryTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
thread_local bool memory_tracker_always_throw_logical_error_on_allocation = false;
1010
#endif
1111

12-
thread_local bool chdb_memory_tracking = false;
12+
extern bool chdb_embedded_server_initialized;
1313

1414
namespace DB
1515
{
@@ -27,7 +27,7 @@ MemoryTracker * getMemoryTracker()
2727
if (auto * thread_memory_tracker = DB::CurrentThread::getMemoryTracker())
2828
return thread_memory_tracker;
2929

30-
if (chdb_memory_tracking)
30+
if (likely(chdb_embedded_server_initialized))
3131
return &total_memory_tracker;
3232

3333
/// Note, we cannot use total_memory_tracker earlier (i.e. just after static variable initialized without this check),

src/Common/memory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ inline ALWAYS_INLINE size_t untrackMemory(void * ptr [[maybe_unused]], Allocatio
221221
/// It's innaccurate resource free for sanitizers. malloc_usable_size() result is greater or equal to allocated size.
222222
else
223223
actual_size = malloc_usable_size(ptr);
224+
# elif defined(OS_DARWIN)
225+
else
226+
actual_size = malloc_size(ptr);
224227
# endif
225228
#endif
226229
trace = CurrentMemoryTracker::free(actual_size);

tests/test_streaming_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def test_multi_thread_streaming_query(self):
228228
for t in threads:
229229
t.start()
230230

231-
time.sleep(3)
231+
time.sleep(10)
232232
stop_event.set()
233233

234234
for t in threads:

0 commit comments

Comments
 (0)