Fix oversized ETCP monitor snapshots
This commit is contained in:
1 parent
25ae211584
commit
5def1ebca1
4 files changed
+183
-67
No files matched your search
@@ -244,7 +244,9 @@ struct voice_played_req {
|
||||
uint64_t author_node_id;
|
||||
};
|
||||
|
||||
// Сбор статуса (NTP + connections) и отправка в GUI. arg = struct UTUN_INSTANCE* (inst)
|
||||
/* Сбор статуса (NTP + connections) в uasync. arg — заимствованный inst.
|
||||
* Ответ CHAT_EVT_STATUS_REFRESH: UTF-8 без завершающего NUL в длине; размер растёт по данным.
|
||||
* Текст заимствован только на время синхронного event callback; при ошибке приходит короткое описание. */
|
||||
void chat_core_collect_status_trampoline(void* arg);
|
||||
|
||||
// Лёгкий запрос нагрузки uasync для GUI. arg = inst, ответ CHAT_EVT_CPU_LOAD.
|
||||
@@ -255,7 +257,10 @@ void chat_core_collect_conn_list_trampoline(void* arg);
|
||||
|
||||
struct chat_node_arg { struct UTUN_INSTANCE* inst; uint64_t node_id; };
|
||||
|
||||
// Сбор полных метрик одного соединения (arg = u_malloc struct chat_node_arg)
|
||||
/* Полные метрики одного соединения, вызывается в uasync. arg — u_malloc struct chat_node_arg,
|
||||
* trampoline освобождает arg; inst остаётся заимствованным. CHAT_EVT_CONN_METRICS содержит UTF-8
|
||||
* без завершающего NUL в длине; буфер растёт по числу линков, при ошибке — короткое описание.
|
||||
* Данные ответа заимствованы только на время синхронного event callback. */
|
||||
void chat_core_collect_conn_metrics_trampoline(void* arg);
|
||||
|
||||
/* Диагностика карточки участника: запрос/codec находятся в chat_member.h. */
|
||||
|
||||
+86
-60
@@ -14,6 +14,7 @@
|
||||
#include "../../lib/ll_queue.h"
|
||||
#include "../../lib/mem.h"
|
||||
#include "../../lib/platform_compat.h"
|
||||
#include "../../lib/strbuf.h"
|
||||
#include "../transport_layer/etcp.h"
|
||||
#include "../transport_layer/etcp_connections.h"
|
||||
#include "../transport_layer/stcp_link.h"
|
||||
@@ -73,42 +74,40 @@ static void get_node_name(struct UTUN_INSTANCE* inst, uint64_t node_id, char* ou
|
||||
}
|
||||
|
||||
static void chat_core_collect_status(struct UTUN_INSTANCE* inst) {
|
||||
char buf[8192]; int off = 0;
|
||||
struct strbuf text = strbuf_new();
|
||||
|
||||
if (!CC(inst) || !CC(inst)->initialized || !inst) {
|
||||
off = snprintf(buf, sizeof(buf), "uTun not initialized\n");
|
||||
chat_event_post(inst, CHAT_EVT_STATUS_REFRESH, (const uint8_t*)buf, off);
|
||||
return;
|
||||
if (!inst || !CC(inst) || !CC(inst)->initialized) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CHAT, "status snapshot: chat unavailable");
|
||||
if (strbuf_addf(&text, "uTun not initialized\n") < 0) goto failed;
|
||||
goto post;
|
||||
}
|
||||
|
||||
struct uasync_load load;
|
||||
if (uasync_get_load(inst->ua, &load) == ERR_OK && load.valid) {
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "uasync CPU: %.1f%%, average %.1f%% (window %.0f ms, age %.0f ms)\n",
|
||||
if (strbuf_addf(&text, "uasync CPU: %.1f%%, average %.1f%% (window %.0f ms, age %.0f ms)\n",
|
||||
load.current_percent, load.average_percent, load.window_us / 1000.0,
|
||||
(get_time_us() - load.sample_time_us) / 1000.0);
|
||||
} else off += snprintf(buf + off, sizeof(buf) - off, "uasync CPU: unavailable\n");
|
||||
(get_time_us() - load.sample_time_us) / 1000.0) < 0) goto failed;
|
||||
} else if (strbuf_addf(&text, "uasync CPU: unavailable\n") < 0) goto failed;
|
||||
|
||||
struct NTP_TIME* ntp = &inst->ntp;
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "=== NTP ===\n");
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "Enabled: %s\n", ntp->enabled ? "yes" : "no");
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "Synced: %s\n", ntp->synced ? "yes" : "no");
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "Offset: %.1f s\n", ntp->offset_us / 1000000.0);
|
||||
if (strbuf_addf(&text, "=== NTP ===\nEnabled: %s\nSynced: %s\nOffset: %.1f s\n",
|
||||
ntp->enabled ? "yes" : "no", ntp->synced ? "yes" : "no", ntp->offset_us / 1000000.0) < 0) goto failed;
|
||||
if (ntp->server_count > 0 && ntp->servers) {
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "Servers: %d\n", ntp->server_count);
|
||||
if (strbuf_addf(&text, "Servers: %d\n", ntp->server_count) < 0) goto failed;
|
||||
for (int i = 0; i < ntp->server_count && ntp->servers[i]; i++)
|
||||
off += snprintf(buf + off, sizeof(buf) - off, " [%d] %s%s\n", i, ntp->servers[i],
|
||||
i == ntp->server_current ? " (current)" : "");
|
||||
if (strbuf_addf(&text, " [%d] %s%s\n", i, ntp->servers[i],
|
||||
i == ntp->server_current ? " (current)" : "") < 0) goto failed;
|
||||
}
|
||||
if (ntp->synced && ntp->last_sync_tb > 0) {
|
||||
uint64_t now_tb = get_time_tb();
|
||||
uint64_t ago_sec = (now_tb - ntp->last_sync_tb) / 10000;
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "Last sync: %llu sec ago\n", (unsigned long long)ago_sec);
|
||||
if (strbuf_addf(&text, "Last sync: %llu sec ago\n", (unsigned long long)ago_sec) < 0) goto failed;
|
||||
}
|
||||
|
||||
int conn_count = 0;
|
||||
struct ll_entry* entry = inst->connections ? inst->connections->head : NULL;
|
||||
while (entry) { conn_count++; entry = entry->next; }
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "=== Connections (%d) ===\n", conn_count);
|
||||
if (strbuf_addf(&text, "=== Connections (%d) ===\n", conn_count) < 0) goto failed;
|
||||
|
||||
uint64_t my_id = inst->node_id;
|
||||
entry = inst->connections ? inst->connections->head : NULL;
|
||||
@@ -132,34 +131,44 @@ static void chat_core_collect_status(struct UTUN_INSTANCE* inst) {
|
||||
char peername[64];
|
||||
get_node_name(inst, pid, peername, sizeof(peername));
|
||||
|
||||
if (peername[0])
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "[%s]→[%s] %s ETCP:%s%s(%dL)\n",
|
||||
if (peername[0]) {
|
||||
if (strbuf_addf(&text, "[%s]→[%s] %s ETCP:%s%s(%dL)\n",
|
||||
myhex, peerhex, peername,
|
||||
conn->links_up ? "UP" : "DOWN", init_str, link_count);
|
||||
else
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "[%s]→[%s] ETCP:%s%s(%dL)\n",
|
||||
conn->links_up ? "UP" : "DOWN", init_str, link_count) < 0) goto failed;
|
||||
} else {
|
||||
if (strbuf_addf(&text, "[%s]→[%s] ETCP:%s%s(%dL)\n",
|
||||
myhex, peerhex,
|
||||
conn->links_up ? "UP" : "DOWN", init_str, link_count);
|
||||
conn->links_up ? "UP" : "DOWN", init_str, link_count) < 0) goto failed;
|
||||
}
|
||||
|
||||
int link_idx = 0;
|
||||
struct ETCP_LINK* link = conn->links;
|
||||
while (link) {
|
||||
char rtt_str[32]; rtt_str[0] = '\0';
|
||||
if (link->rtt_last > 0) snprintf(rtt_str, sizeof(rtt_str), " / rtt=%ums", link->rtt_last / 10);
|
||||
off += snprintf(buf + off, sizeof(buf) - off, " LINK#%d: %s %s %s NAT=%s%s\n",
|
||||
if (strbuf_addf(&text, " LINK#%d: %s %s %s NAT=%s%s\n",
|
||||
link_idx,
|
||||
link->is_tcp ? "TCP" : "UDP",
|
||||
link->conn ? link->conn->name : "?",
|
||||
link->link_status ? "UP" : "DOWN",
|
||||
link_address_profile_text(link).text,
|
||||
rtt_str);
|
||||
rtt_str) < 0) goto failed;
|
||||
link = link->next; link_idx++;
|
||||
}
|
||||
|
||||
entry = entry->next;
|
||||
}
|
||||
|
||||
chat_event_post(inst, CHAT_EVT_STATUS_REFRESH, (const uint8_t*)buf, off);
|
||||
post:
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CHAT, "status snapshot: bytes=%zu", text.len);
|
||||
chat_event_post(inst, CHAT_EVT_STATUS_REFRESH, (const uint8_t*)strbuf_str(&text), (int)text.len);
|
||||
strbuf_free(&text);
|
||||
return;
|
||||
failed:
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "status snapshot: formatting/allocation failed after %zu bytes", text.len);
|
||||
strbuf_free(&text);
|
||||
static const uint8_t error[] = "Unable to collect status: formatting/allocation failed\n";
|
||||
chat_event_post(inst, CHAT_EVT_STATUS_REFRESH, error, sizeof(error) - 1);
|
||||
}
|
||||
|
||||
/* ── Бинарный список соединений: [count:2][entry:46B]* ── */
|
||||
@@ -167,16 +176,17 @@ static void chat_core_collect_status(struct UTUN_INSTANCE* inst) {
|
||||
#define CONN_LIST_ENTRY_SIZE 46
|
||||
|
||||
static void collect_conn_list(struct UTUN_INSTANCE* inst) {
|
||||
if (!CC(inst) || !CC(inst)->initialized || !inst || !inst->connections) return;
|
||||
if (!inst || !CC(inst) || !CC(inst)->initialized || !inst->connections) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CHAT, "connection list: chat/connections unavailable"); return;
|
||||
}
|
||||
|
||||
uint16_t count = 0;
|
||||
struct ll_entry* entry = inst->connections->head;
|
||||
while (entry) { count++; entry = entry->next; }
|
||||
if (count > 250) count = 250;
|
||||
while (entry && count < 250) { count++; entry = entry->next; }
|
||||
|
||||
size_t buf_sz = 2 + (size_t)count * CONN_LIST_ENTRY_SIZE;
|
||||
uint8_t* buf = (uint8_t*)u_malloc(buf_sz);
|
||||
if (!buf) return;
|
||||
if (!buf) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "connection list: cannot allocate %zu bytes", buf_sz); return; }
|
||||
|
||||
uint16_t* hdr = (uint16_t*)buf;
|
||||
*hdr = count;
|
||||
@@ -222,25 +232,26 @@ static void collect_conn_list(struct UTUN_INSTANCE* inst) {
|
||||
|
||||
/* ── Полный текстовый дамп одного соединения ── */
|
||||
static void collect_conn_metrics(struct UTUN_INSTANCE* inst, uint64_t peer_node_id) {
|
||||
char buf[8192]; int off = 0;
|
||||
struct strbuf text = strbuf_new();
|
||||
|
||||
if (!CC(inst) || !CC(inst)->initialized || !inst || !inst->connections) {
|
||||
off = snprintf(buf, sizeof(buf), "uTun not initialized\n");
|
||||
chat_event_post(inst, CHAT_EVT_CONN_METRICS, (const uint8_t*)buf, off);
|
||||
return;
|
||||
if (!inst || !CC(inst) || !CC(inst)->initialized || !inst->connections) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CHAT, "connection metrics: chat/connections unavailable peer=%016llX",
|
||||
(unsigned long long)peer_node_id);
|
||||
if (strbuf_addf(&text, "uTun not initialized\n") < 0) goto failed;
|
||||
goto post;
|
||||
}
|
||||
|
||||
struct ll_entry* e = queue_find_data_by_index(inst->connections, (const uint8_t*)&peer_node_id);
|
||||
if (!e) {
|
||||
off = snprintf(buf, sizeof(buf), "Connection %016llX not found\n", (unsigned long long)peer_node_id);
|
||||
chat_event_post(inst, CHAT_EVT_CONN_METRICS, (const uint8_t*)buf, off);
|
||||
return;
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CHAT, "connection metrics: peer=%016llX not found", (unsigned long long)peer_node_id);
|
||||
if (strbuf_addf(&text, "Connection %016llX not found\n", (unsigned long long)peer_node_id) < 0) goto failed;
|
||||
goto post;
|
||||
}
|
||||
struct conn_queue_entry* ce = (struct conn_queue_entry*)e->data;
|
||||
if (!ce || !ce->conn) {
|
||||
off = snprintf(buf, sizeof(buf), "Connection %016llX has null data\n", (unsigned long long)peer_node_id);
|
||||
chat_event_post(inst, CHAT_EVT_CONN_METRICS, (const uint8_t*)buf, off);
|
||||
return;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "connection metrics: peer=%016llX has null data", (unsigned long long)peer_node_id);
|
||||
if (strbuf_addf(&text, "Connection %016llX has null data\n", (unsigned long long)peer_node_id) < 0) goto failed;
|
||||
goto post;
|
||||
}
|
||||
struct ETCP_CONN* conn = ce->conn;
|
||||
uint64_t my_id = inst->node_id;
|
||||
@@ -255,7 +266,7 @@ static void collect_conn_metrics(struct UTUN_INSTANCE* inst, uint64_t peer_node_
|
||||
int link_count = 0, links_up_count = 0;
|
||||
struct ETCP_LINK* tl = conn->links; while (tl) { if (tl->link_status) links_up_count++; link_count++; tl = tl->next; }
|
||||
|
||||
off += snprintf(buf + off, sizeof(buf) - off,
|
||||
if (strbuf_addf(&text,
|
||||
"=== PEER: %04llX\u2192%04llX [%s] ===\n"
|
||||
"Role: %s\n"
|
||||
"Status: %s Links: %d/%d Initialized: %s MTU: %d\n"
|
||||
@@ -276,33 +287,38 @@ static void collect_conn_metrics(struct UTUN_INSTANCE* inst, uint64_t peer_node_
|
||||
conn->bytes_sent_total, conn->retransmissions_count, conn->ack_packets_count,
|
||||
conn->unacked_bytes, conn->max_inflight,
|
||||
conn->rx_dup_count, conn->tx_dup_count,
|
||||
conn->next_tx_id, conn->last_rx_id, conn->last_delivered_id, conn->rx_ack_till);
|
||||
conn->next_tx_id, conn->last_rx_id, conn->last_delivered_id, conn->rx_ack_till) < 0) goto failed;
|
||||
|
||||
if (conn->input_queue) off += snprintf(buf + off, sizeof(buf) - off, "input_q: %uB/%dp ", (unsigned)queue_total_bytes(conn->input_queue), queue_entry_count(conn->input_queue));
|
||||
if (conn->input_send_q) off += snprintf(buf + off, sizeof(buf) - off, "send_q: %uB/%dp ", (unsigned)queue_total_bytes(conn->input_send_q), queue_entry_count(conn->input_send_q));
|
||||
if (conn->input_wait_ack) off += snprintf(buf + off, sizeof(buf) - off, "wait_ack: %uB/%dp\n", (unsigned)queue_total_bytes(conn->input_wait_ack), queue_entry_count(conn->input_wait_ack));
|
||||
if (conn->input_queue && strbuf_addf(&text, "input_q: %uB/%dp ",
|
||||
(unsigned)queue_total_bytes(conn->input_queue), queue_entry_count(conn->input_queue)) < 0) goto failed;
|
||||
if (conn->input_send_q && strbuf_addf(&text, "send_q: %uB/%dp ",
|
||||
(unsigned)queue_total_bytes(conn->input_send_q), queue_entry_count(conn->input_send_q)) < 0) goto failed;
|
||||
if (conn->input_wait_ack && strbuf_addf(&text, "wait_ack: %uB/%dp\n",
|
||||
(unsigned)queue_total_bytes(conn->input_wait_ack), queue_entry_count(conn->input_wait_ack)) < 0) goto failed;
|
||||
unsigned ack_bytes = 0, ack_packets = 0;
|
||||
for (struct ETCP_LINK* link = conn->links; link; link = link->next) {
|
||||
ack_bytes += (unsigned)queue_total_bytes(link->ack_q); ack_packets += queue_entry_count(link->ack_q);
|
||||
}
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "ack_q: %uB/%up ", ack_bytes, ack_packets);
|
||||
if (conn->recv_q) off += snprintf(buf + off, sizeof(buf) - off, "recv_q: %uB/%dp ", (unsigned)queue_total_bytes(conn->recv_q), queue_entry_count(conn->recv_q));
|
||||
if (conn->output_queue) off += snprintf(buf + off, sizeof(buf) - off, "out_q: %uB/%dp\n", (unsigned)queue_total_bytes(conn->output_queue), queue_entry_count(conn->output_queue));
|
||||
if (strbuf_addf(&text, "ack_q: %uB/%up ", ack_bytes, ack_packets) < 0) goto failed;
|
||||
if (conn->recv_q && strbuf_addf(&text, "recv_q: %uB/%dp ",
|
||||
(unsigned)queue_total_bytes(conn->recv_q), queue_entry_count(conn->recv_q)) < 0) goto failed;
|
||||
if (conn->output_queue && strbuf_addf(&text, "out_q: %uB/%dp\n",
|
||||
(unsigned)queue_total_bytes(conn->output_queue), queue_entry_count(conn->output_queue)) < 0) goto failed;
|
||||
|
||||
off += snprintf(buf + off, sizeof(buf) - off, "\n--- ACK Debug ---\n"
|
||||
if (strbuf_addf(&text, "\n--- ACK Debug ---\n"
|
||||
"hit_inf=%u hit_sndq=%u miss=%u link_wait=%u\n",
|
||||
conn->cnt_ack_hit_inf, conn->cnt_ack_hit_sndq, conn->cnt_ack_miss, conn->cnt_link_wait);
|
||||
conn->cnt_ack_hit_inf, conn->cnt_ack_hit_sndq, conn->cnt_ack_miss, conn->cnt_link_wait) < 0) goto failed;
|
||||
|
||||
/* Normalizer */
|
||||
if (conn->normalizer) {
|
||||
struct PKTNORM* pn = conn->normalizer;
|
||||
off += snprintf(buf + off, sizeof(buf) - off,
|
||||
if (strbuf_addf(&text,
|
||||
"\n--- Normalizer ---\n"
|
||||
"in: %llu pkts / %llu B out: %llu pkts / %llu B\n"
|
||||
"frag_size=%u data_ptr=%u data_size=%u alloc_err=%u logic_err=%u\n",
|
||||
(unsigned long long)pn->in_total_pkts, (unsigned long long)pn->in_total_bytes,
|
||||
(unsigned long long)pn->out_total_pkts, (unsigned long long)pn->out_total_bytes,
|
||||
pn->frag_size, pn->data_ptr, pn->data_size, pn->alloc_errors, pn->logic_errors);
|
||||
pn->frag_size, pn->data_ptr, pn->data_size, pn->alloc_errors, pn->logic_errors) < 0) goto failed;
|
||||
}
|
||||
|
||||
/* Links */
|
||||
@@ -310,10 +326,10 @@ static void collect_conn_metrics(struct UTUN_INSTANCE* inst, uint64_t peer_node_
|
||||
struct ETCP_LINK* link = conn->links;
|
||||
while (link) {
|
||||
const char* is_tcp_str = link->is_tcp ? "TCP" : "UDP";
|
||||
char local_str[54], remote_str[54];
|
||||
strncpy(local_str, link->conn ? (link->conn->interface_addr.ss_family ? sockaddr_storage_to_str(&link->conn->interface_addr).str : sockaddr_storage_to_str(&link->conn->local_addr).str) : "stcp", sizeof(local_str) - 1);
|
||||
strncpy(remote_str, sockaddr_storage_to_str(&link->remote_addr).str, sizeof(remote_str) - 1);
|
||||
off += snprintf(buf + off, sizeof(buf) - off,
|
||||
ip_str_t local = {.str = "stcp"}, remote = sockaddr_storage_to_str(&link->remote_addr);
|
||||
if (link->conn) local = sockaddr_storage_to_str(link->conn->interface_addr.ss_family ?
|
||||
&link->conn->interface_addr : &link->conn->local_addr);
|
||||
if (strbuf_addf(&text,
|
||||
"\n--- LINK#%d: %s %s NAT=%s ---\n"
|
||||
"addr: %s -> %s\n"
|
||||
"RTT: %.1f ms TT: %.1f ms\n"
|
||||
@@ -321,16 +337,26 @@ static void collect_conn_metrics(struct UTUN_INSTANCE* inst, uint64_t peer_node_
|
||||
"Inflight: %u / %u B (%u pkts)\n"
|
||||
"Encrypt: sent=%zu err=%zu Decrypt: rcvd=%zu err=%zu\n",
|
||||
link_idx, link->link_status ? "UP" : "DOWN", is_tcp_str, link_address_profile_text(link).text,
|
||||
local_str, remote_str,
|
||||
local.str, remote.str,
|
||||
link->rtt_last / 10.0f, link->tt_last / 10.0f,
|
||||
link->keepalive_sent_count, link->keepalive_recv_count, link->keepalive_interval,
|
||||
link->inflight_bytes, link->inflight_lim_bytes, link->inflight_packets,
|
||||
link->total_encrypted, link->encrypt_errors, link->total_decrypted, link->decrypt_errors);
|
||||
link->total_encrypted, link->encrypt_errors, link->total_decrypted, link->decrypt_errors) < 0) goto failed;
|
||||
|
||||
link = link->next; link_idx++;
|
||||
}
|
||||
|
||||
chat_event_post(inst, CHAT_EVT_CONN_METRICS, (const uint8_t*)buf, off);
|
||||
post:
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CHAT, "connection metrics: peer=%016llX bytes=%zu", (unsigned long long)peer_node_id, text.len);
|
||||
chat_event_post(inst, CHAT_EVT_CONN_METRICS, (const uint8_t*)strbuf_str(&text), (int)text.len);
|
||||
strbuf_free(&text);
|
||||
return;
|
||||
failed:
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "connection metrics: peer=%016llX formatting/allocation failed after %zu bytes",
|
||||
(unsigned long long)peer_node_id, text.len);
|
||||
strbuf_free(&text);
|
||||
static const uint8_t error[] = "Unable to collect connection metrics: formatting/allocation failed\n";
|
||||
chat_event_post(inst, CHAT_EVT_CONN_METRICS, error, sizeof(error) - 1);
|
||||
}
|
||||
|
||||
void chat_core_collect_status_trampoline(void* arg) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "topo_node_sqlite.h"
|
||||
#include "route_lib.h"
|
||||
#include "etcp.h"
|
||||
#include "pkt_normalizer.h"
|
||||
#include "etcp_router.h"
|
||||
#include "media_delivery/media_index.h"
|
||||
#include "media_delivery/media_delivery_proto.h"
|
||||
@@ -28,6 +29,80 @@ static void work(void* arg) { ((struct job*)arg)->worked = 1; }
|
||||
static void done(void* arg, int err) { struct job* job = arg; job->done++; job->error = err; }
|
||||
static void ready(struct ll_queue* queue, void* arg) { (void)queue; (*(int*)arg)++; }
|
||||
|
||||
static char monitor_text[256 * 1024];
|
||||
static int monitor_event, monitor_length;
|
||||
|
||||
/* Копируем заимствованный текст: снимок освобождается сразу после callback. */
|
||||
static void capture_monitor(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int len) {
|
||||
(void)inst;
|
||||
assert(type == CHAT_EVT_STATUS_REFRESH || type == CHAT_EVT_CONN_METRICS);
|
||||
assert(data && len > 0 && len < (int)sizeof(monitor_text) && !memchr(data, 0, (size_t)len));
|
||||
memcpy(monitor_text, data, (size_t)len); monitor_text[len] = '\0';
|
||||
monitor_event = type; monitor_length = len;
|
||||
}
|
||||
|
||||
/* Публичные сборщики проходят прежнюю границу 8 КБ и сохраняют последний линк без утечек. */
|
||||
static void check_monitor_snapshots(struct UTUN_INSTANCE* inst) {
|
||||
const size_t before = u_get_allocated_count();
|
||||
struct UTUN_INSTANCE fixture = *inst;
|
||||
fixture.connections = queue_new(inst->ua, 16, 0, 8, "monitor test"); assert(fixture.connections);
|
||||
fixture.chat_event_handler = capture_monitor;
|
||||
struct PKTNORM normalizer = { .frag_size = 1400 };
|
||||
struct ETCP_CONN conn = { .instance = &fixture, .peer_node_id = 7, .normalizer = &normalizer };
|
||||
struct ll_entry* entry = queue_entry_new(sizeof(struct conn_queue_entry)); assert(entry);
|
||||
struct conn_queue_entry* ce = (struct conn_queue_entry*)entry->data; ce->peer_node_id = 7; ce->conn = &conn;
|
||||
assert(!queue_data_put_with_index(fixture.connections, entry));
|
||||
struct ETCP_LINK* links = u_calloc(255, sizeof(*links)); assert(links);
|
||||
struct ll_queue* ack = queue_new(inst->ua, 0, 0, 0, "monitor ACK"); assert(ack);
|
||||
struct ETCP_SOCKET socket = { .name = "monitor IPv6" };
|
||||
struct sockaddr_in6* addr = (struct sockaddr_in6*)&socket.interface_addr;
|
||||
addr->sin6_family = AF_INET6; addr->sin6_port = htons(65535);
|
||||
assert(inet_pton(AF_INET6, "1234:5678:9abc:def0:1234:5678:9abc:def0", &addr->sin6_addr) == 1);
|
||||
const ip_str_t address = sockaddr_storage_to_str(&socket.interface_addr);
|
||||
for (int i = 0; i < 255; i++) {
|
||||
links[i].etcp = &conn; links[i].conn = &socket; links[i].ack_q = ack;
|
||||
links[i].remote_addr = socket.interface_addr;
|
||||
}
|
||||
debug_level_t saved = g_debug_config.category_levels[DEBUG_CATEGORY_CHAT];
|
||||
debug_set_category_level(DEBUG_CATEGORY_CHAT, DEBUG_LEVEL_TRACE);
|
||||
const int counts[] = {0, 1, 35, 36, 40, 255};
|
||||
for (unsigned c = 0; c < sizeof(counts) / sizeof(counts[0]); c++) {
|
||||
const int count = counts[c];
|
||||
for (int i = 0; i < 255; i++) links[i].next = i + 1 < count ? &links[i + 1] : NULL;
|
||||
conn.links = count ? links : NULL;
|
||||
char last_link[40]; snprintf(last_link, sizeof(last_link), "LINK#%d:", count - 1);
|
||||
const size_t allocated = u_get_allocated_count();
|
||||
struct chat_node_arg* req = u_malloc(sizeof(*req)); assert(req); req->inst = &fixture; req->node_id = 7;
|
||||
monitor_event = 0; chat_core_collect_conn_metrics_trampoline(req);
|
||||
assert(monitor_event == CHAT_EVT_CONN_METRICS && strstr(monitor_text, "--- Normalizer ---"));
|
||||
assert(!count || (strstr(monitor_text, last_link) && strstr(monitor_text, address.str)));
|
||||
if (count >= 35) assert(monitor_length > 8192);
|
||||
assert(u_get_allocated_count() == allocated);
|
||||
monitor_event = 0; chat_core_collect_status_trampoline(&fixture);
|
||||
assert(monitor_event == CHAT_EVT_STATUS_REFRESH && strstr(monitor_text, "=== Connections (1) ==="));
|
||||
assert(!count || strstr(monitor_text, last_link));
|
||||
if (count == 255) assert(monitor_length > 8192);
|
||||
assert(u_get_allocated_count() == allocated);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CHAT, "[PASS] monitor snapshots: links=%d, full metrics/status, allocations unchanged", count);
|
||||
}
|
||||
ce->conn = NULL;
|
||||
struct chat_node_arg* req = u_malloc(sizeof(*req)); assert(req); req->inst = &fixture; req->node_id = 7;
|
||||
chat_core_collect_conn_metrics_trampoline(req); assert(strstr(monitor_text, "has null data"));
|
||||
req = u_malloc(sizeof(*req)); assert(req); req->inst = &fixture; req->node_id = 8;
|
||||
chat_core_collect_conn_metrics_trampoline(req); assert(strstr(monitor_text, "not found"));
|
||||
fixture.chat_core = NULL;
|
||||
req = u_malloc(sizeof(*req)); assert(req); req->inst = &fixture; req->node_id = 7;
|
||||
chat_core_collect_conn_metrics_trampoline(req); assert(strstr(monitor_text, "not initialized"));
|
||||
chat_core_collect_status_trampoline(&fixture); assert(strstr(monitor_text, "not initialized"));
|
||||
chat_core_collect_conn_list_trampoline(NULL);
|
||||
chat_core_collect_status_trampoline(NULL);
|
||||
req = u_calloc(1, sizeof(*req)); assert(req); chat_core_collect_conn_metrics_trampoline(req);
|
||||
debug_set_category_level(DEBUG_CATEGORY_CHAT, saved);
|
||||
queue_entry_free(queue_data_get(fixture.connections)); queue_free(fixture.connections); queue_free(ack); u_free(links);
|
||||
assert(u_get_allocated_count() == before);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CHAT, "[PASS] monitor snapshots: unavailable/missing/null connection and NULL instance, no leaks");
|
||||
}
|
||||
|
||||
/* Активность звонка не создаёт таймер, который сам переведёт мобильный узел в standby. */
|
||||
static void check_client_activity(struct UASYNC* ua) {
|
||||
struct UTUN_INSTANCE mobile = { .ua = ua, .client_type = CLIENT_TYPE_MOBILE, .client_activity = CLIENT_ACTIVITY_STANDBY };
|
||||
@@ -251,6 +326,7 @@ int main(void) {
|
||||
struct TOPO_NODE* identity = topo_node_registry_find(inst->topo_groups, inst->node_id);
|
||||
assert(identity && identity->v4_addrs);
|
||||
assert(chat_service_start(inst) == 0 && chat_service_start(inst) == 0);
|
||||
check_monitor_snapshots(inst);
|
||||
assert(!topo_groups_get_default(inst->topo_groups));
|
||||
chat_core_create_channel_auto(inst, "lifecycle");
|
||||
struct TOPO_GROUP* chat = chat_group(inst); assert(chat);
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
// connmonitorwindow.h — ETCP connection monitor (standalone, non-modal window)
|
||||
/* Немодальное окно диагностики ETCP: список соединений и текстовые метрики выбранного узла.
|
||||
* Создавать в куче, открывать через show(): закрытие удаляет окно (WA_DeleteOnClose).
|
||||
* Одновременно используется один экземпляр: он получает глобальные callbacks gui_bridge.
|
||||
* Все методы работают в GUI-потоке; ядро собирает снимки асинхронно в uasync.
|
||||
* Таймер 100 мс запускается при создании/show и останавливается при hide. Выделенный текст
|
||||
* или нажатая кнопка мыши приостанавливают новые запросы, но не применение готовых ответов. */
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
@@ -10,10 +15,14 @@
|
||||
class ConnMonitorWindow : public QDialog {
|
||||
Q_OBJECT
|
||||
public:
|
||||
// Создать окно и зарегистрировать callbacks; parent задаёт обычное Qt-владение.
|
||||
explicit ConnMonitorWindow(QWidget* parent = nullptr);
|
||||
~ConnMonitorWindow() override;
|
||||
|
||||
// Применить список [count:uint16][entry:46B]* в native endian. data заимствован на время вызова.
|
||||
void onConnList(const uint8_t* data, int len);
|
||||
// Показать len байт UTF-8 без требования завершающего NUL, сохранив позицию прокрутки.
|
||||
// data заимствован на время вызова; текст копируется в документ Qt.
|
||||
void onConnMetrics(const uint8_t* data, int len);
|
||||
|
||||
protected:
|
||||
@@ -27,8 +36,8 @@ private slots:
|
||||
private:
|
||||
void requestMetricsForSelected();
|
||||
|
||||
QListWidget* m_connList;
|
||||
QPlainTextEdit* m_details;
|
||||
QTimer* m_timer;
|
||||
uint64_t m_selectedNodeId = 0;
|
||||
QListWidget* m_connList; // Список узлов, Qt::UserRole содержит полный node_id; принадлежит окну.
|
||||
QPlainTextEdit* m_details; // Только чтение; выделение текста приостанавливает опрос.
|
||||
QTimer* m_timer; // Опрос каждые 100 мс; принадлежит окну.
|
||||
uint64_t m_selectedNodeId = 0; // Узел для запросов метрик; 0 — выбор отсутствует.
|
||||
};
|
||||
Reference in new issue
Block a user