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
This commit is contained in:
Cory McWilliams 2023-10-08 14:25:22 +00:00
parent 0473eec0a2
commit 11373faf23
3 changed files with 24 additions and 0 deletions

View File

@ -234,6 +234,8 @@ typedef struct _tf_ssb_t
tf_ssb_store_queue_t store_queue; tf_ssb_store_queue_t store_queue;
int ref_count; int ref_count;
uv_thread_t thread_self;
} tf_ssb_t; } tf_ssb_t;
typedef struct _tf_ssb_connection_message_request_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; 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) sqlite3* tf_ssb_acquire_db_reader(tf_ssb_t* ssb)
{ {
tf_trace_begin(ssb->trace, "db_reader"); tf_trace_begin(ssb->trace, "db_reader");
_tf_ssb_assert_not_main_thread(ssb);
sqlite3* db = NULL; sqlite3* db = NULL;
uv_mutex_lock(&ssb->db_readers_lock); uv_mutex_lock(&ssb->db_readers_lock);
if (ssb->db_readers_count) 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) sqlite3* tf_ssb_acquire_db_writer(tf_ssb_t* ssb)
{ {
tf_trace_begin(ssb->trace, "db_writer"); tf_trace_begin(ssb->trace, "db_writer");
_tf_ssb_assert_not_main_thread(ssb);
uv_mutex_lock(&ssb->db_writer_lock); uv_mutex_lock(&ssb->db_writer_lock);
sqlite3* writer = ssb->db_writer; sqlite3* writer = ssb->db_writer;
assert(writer); assert(writer);
@ -3627,3 +3641,8 @@ void tf_ssb_unref(tf_ssb_t* ssb)
{ {
ssb->ref_count--; ssb->ref_count--;
} }
void tf_ssb_set_main_thread(tf_ssb_t* ssb)
{
ssb->thread_self = uv_thread_self();
}

View File

@ -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_ref(tf_ssb_t* ssb);
void tf_ssb_unref(tf_ssb_t* ssb); void tf_ssb_unref(tf_ssb_t* ssb);
void tf_ssb_set_main_thread(tf_ssb_t* ssb);

View File

@ -638,6 +638,9 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
uint8_t id0bin[k_id_bin_len]; uint8_t id0bin[k_id_bin_len];
tf_ssb_id_str_to_bin(id0bin, id0); 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_t idle0 = { .data = ssb0 };
uv_idle_init(&loop, &idle0); uv_idle_init(&loop, &idle0);
uv_idle_start(&idle0, _ssb_test_idle); uv_idle_start(&idle0, _ssb_test_idle);