From 1aa4b0e590752412d64c525cdb00a5e38b33c415 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 5 Feb 2022 20:18:58 +0000 Subject: [PATCH] Hook up some stats from the SSB side. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3822 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 44 +++++++++++++++++++++++++++++++++++++++----- src/ssb.h | 19 +++++++++++++++++++ src/task.c | 11 +++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index 0b90dec5..c7420dea 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -146,6 +146,10 @@ typedef struct _tf_ssb_t { uint8_t pub[crypto_sign_PUBLICKEYBYTES]; uint8_t priv[crypto_sign_SECRETKEYBYTES]; + int messages_stored; + int rpc_in; + int rpc_out; + tf_ssb_connection_t* connections; int connections_count; @@ -470,6 +474,7 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, _tf_ssb_connection_box_stream_send(connection, combined, 1 + 2 * sizeof(uint32_t) + size); free(combined); printf("RPC SEND flags=%x RN=%d: %.*s\n", flags, request_number, (int)size, message); + connection->ssb->rpc_out++; } void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_id, size_t out_id_size) @@ -996,6 +1001,7 @@ static bool _tf_ssb_name_equals(JSContext* context, JSValue object, const char** static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const uint8_t* message, size_t size) { + connection->ssb->rpc_in++; if (flags & k_ssb_rpc_flag_json) { printf("RPC RECV flags=%x RN=%d: %.*s\n", flags, request_number, (int)size, message); @@ -1220,10 +1226,16 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message) char id[sodium_base64_ENCODED_LEN(crypto_hash_sha256_BYTES, sodium_base64_VARIANT_ORIGINAL) + 7 + 1]; tf_ssb_calculate_message_id(ssb->context, root, id, sizeof(id)); - if (valid && - !tf_ssb_db_store_message(ssb, ssb->context, id, root, signature_base64, false)) + if (valid) { - printf("message not stored.\n"); + if (tf_ssb_db_store_message(ssb, ssb->context, id, root, signature_base64, false)) + { + tf_ssb_notify_message_added(ssb, id); + } + else + { + printf("message not stored.\n"); + } } if (!tf_ssb_verify_and_strip_signature(ssb->context, root, NULL, 0, NULL)) @@ -1231,8 +1243,6 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message) printf("Failed to verify message signature.\n"); } - tf_ssb_notify_message_added(ssb, id); - JS_FreeValue(context, root); } @@ -1603,6 +1613,29 @@ static void _tf_ssb_trace_timer(uv_timer_t* timer) tf_trace_counter(ssb->trace, "ssb", _countof(values), names, values); } +void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats) +{ + *out_stats = (tf_ssb_stats_t) + { + .connections = ssb->connections_count, + .broadcasts = ssb->broadcasts_count, + .messages_stored = ssb->messages_stored, + .rpc_in = ssb->rpc_in, + .rpc_out = ssb->rpc_out, + .callbacks = + { + .rpc = ssb->rpc_count, + .connections_changed = ssb->connections_changed_count, + .message_added = ssb->message_added_count, + .blob_want_added = ssb->blob_want_added_count, + .broadcasts_changed = ssb->broadcasts_changed_count, + }, + }; + ssb->messages_stored = 0; + ssb->rpc_in = 0; + ssb->rpc_out = 0; +} + tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db, const char* secrets_path) { tf_ssb_t* ssb = malloc(sizeof(tf_ssb_t)); @@ -2577,6 +2610,7 @@ void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_ca void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id) { tf_ssb_message_added_callback_node_t* next = NULL; + ssb->messages_stored++; for (tf_ssb_message_added_callback_node_t* node = ssb->message_added; node; node = next) { next = node->next; diff --git a/src/ssb.h b/src/ssb.h index f860cb4c..e60b0bc6 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -39,6 +39,23 @@ enum { k_blob_id_len = 53, }; +typedef struct _tf_ssb_stats_t +{ + int connections; + int broadcasts; + int messages_stored; + int rpc_in; + int rpc_out; + struct + { + int rpc; + int connections_changed; + int message_added; + int blob_want_added; + int broadcasts_changed; + } callbacks; +} tf_ssb_stats_t; + tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db, const char* secrets_path); void tf_ssb_destroy(tf_ssb_t* ssb); @@ -114,3 +131,5 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t* callback, void* user_data); JSClassID tf_ssb_get_connection_class_id(); + +void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats); diff --git a/src/task.c b/src/task.c index 25766651..29c44c4b 100644 --- a/src/task.c +++ b/src/task.c @@ -730,6 +730,17 @@ static JSValue _tf_task_getStats(JSContext* context, JSValueConst this_val, int { JS_SetPropertyStr(context, result, "rss", JS_NewInt64(context, rss)); } + + if (task->_ssb) + { + tf_ssb_stats_t ssb_stats = { 0 }; + tf_ssb_get_stats(task->_ssb, &ssb_stats); + + JS_SetPropertyStr(context, result, "messages_stored", JS_NewInt32(context, ssb_stats.messages_stored)); + JS_SetPropertyStr(context, result, "rpc_in", JS_NewInt32(context, ssb_stats.rpc_in)); + JS_SetPropertyStr(context, result, "rpc_out", JS_NewInt32(context, ssb_stats.rpc_out)); + } + return result; }