From 11373faf2326c34b15037c55e799b284cb32d373 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 8 Oct 2023 14:25:22 +0000 Subject: [PATCH] Log during -t=bench every database access from the main thread. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4498 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 19 +++++++++++++++++++ src/ssb.h | 2 ++ src/ssb.tests.c | 3 +++ 3 files changed, 24 insertions(+) diff --git a/src/ssb.c b/src/ssb.c index 425c7363..7d7c79fe 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -234,6 +234,8 @@ typedef struct _tf_ssb_t tf_ssb_store_queue_t store_queue; int ref_count; + + uv_thread_t thread_self; } tf_ssb_t; typedef struct _tf_ssb_connection_message_request_t @@ -2155,9 +2157,20 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path return ssb; } +static void _tf_ssb_assert_not_main_thread(tf_ssb_t* ssb) +{ + if (uv_thread_self() == ssb->thread_self) + { + const char* bt = tf_util_backtrace_string(); + tf_printf("Acquiring DB from the main thread:\n%s\n", bt); + tf_free((void*)bt); + } +} + sqlite3* tf_ssb_acquire_db_reader(tf_ssb_t* ssb) { tf_trace_begin(ssb->trace, "db_reader"); + _tf_ssb_assert_not_main_thread(ssb); sqlite3* db = NULL; uv_mutex_lock(&ssb->db_readers_lock); if (ssb->db_readers_count) @@ -2194,6 +2207,7 @@ void tf_ssb_release_db_reader(tf_ssb_t* ssb, sqlite3* db) sqlite3* tf_ssb_acquire_db_writer(tf_ssb_t* ssb) { tf_trace_begin(ssb->trace, "db_writer"); + _tf_ssb_assert_not_main_thread(ssb); uv_mutex_lock(&ssb->db_writer_lock); sqlite3* writer = ssb->db_writer; assert(writer); @@ -3627,3 +3641,8 @@ void tf_ssb_unref(tf_ssb_t* ssb) { ssb->ref_count--; } + +void tf_ssb_set_main_thread(tf_ssb_t* ssb) +{ + ssb->thread_self = uv_thread_self(); +} diff --git a/src/ssb.h b/src/ssb.h index ef2d6631..87603ad2 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -210,3 +210,5 @@ tf_ssb_store_queue_t* tf_ssb_get_store_queue(tf_ssb_t* ssb); void tf_ssb_ref(tf_ssb_t* ssb); void tf_ssb_unref(tf_ssb_t* ssb); + +void tf_ssb_set_main_thread(tf_ssb_t* ssb); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 5aaab141..d5b3b07e 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -638,6 +638,9 @@ void tf_ssb_test_bench(const tf_test_options_t* options) uint8_t id0bin[k_id_bin_len]; tf_ssb_id_str_to_bin(id0bin, id0); + tf_ssb_set_main_thread(ssb0); + tf_ssb_set_main_thread(ssb1); + uv_idle_t idle0 = { .data = ssb0 }; uv_idle_init(&loop, &idle0); uv_idle_start(&idle0, _ssb_test_idle);