Measure uasync CPU load and display it in desktop and Android GUIs

Sample thread CPU usage without waking idle loops, expose a one-second moving average, and refresh compact GUI indicators at 3 Hz while visible on Android. Add load and bridge regression coverage, streaming notes, and task list cleanup.
This commit is contained in:
evgeny committed 2026-10-10 12:31:13 +02:00
1 parent bcfe7ca4c1
commit 71d19013c0
25 files changed
+692 -587

No files matched your search

+136 -13
View File
@@ -1012,11 +1012,123 @@ static void process_epoll_events(struct UASYNC* ua, struct epoll_event* events,
}
#endif
// Instance version
void uasync_poll(struct UASYNC* ua, int timeout_tb) {
if (!ua) return;
if (!ua->sockets || !ua->timeout_heap) return;
// CPU-часы читаются только в потоке владельца, включая системное время этого потока.
static int uasync_thread_cpu_ns(uint64_t* value) {
#ifdef _WIN32
FILETIME created, exited, kernel, user;
if (!GetThreadTimes(GetCurrentThread(), &created, &exited, &kernel, &user)) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "uasync CPU clock failed: error=%lu", (unsigned long)GetLastError());
return -1;
}
uint64_t kernel_ticks = ((uint64_t)kernel.dwHighDateTime << 32) | kernel.dwLowDateTime;
uint64_t user_ticks = ((uint64_t)user.dwHighDateTime << 32) | user.dwLowDateTime;
*value = (kernel_ticks + user_ticks) * 100;
#else
struct timespec ts;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "uasync CPU clock failed: %s", strerror(errno));
return -1;
}
*value = (uint64_t)ts.tv_sec * 1000000000ULL + ts.tv_nsec;
#endif
return 0;
}
#define UASYNC_LOAD_PERIOD_US 100000ULL
#define UASYNC_LOAD_WINDOW_US 1000000ULL
static void uasync_update_load(struct UASYNC* ua) {
if (ua->load_state < 0) return;
uint64_t now = get_time_us();
if (ua->load_state && now < ua->load_next_us) return;
uint64_t cpu_ns;
if (uasync_thread_cpu_ns(&cpu_ns) < 0) {
ua->load_state = -1; ua->load.valid = 0;
DEBUG_WARN(DEBUG_CATEGORY_SYS, "uasync CPU load disabled: ua=%p", (void*)ua);
return;
}
if (!ua->load_state) {
ua->load_state = 1; ua->load_previous_us = now; ua->load_previous_cpu_ns = cpu_ns;
ua->load_next_us = now + UASYNC_LOAD_PERIOD_US; ua->load_log_us = now + UASYNC_LOAD_WINDOW_US;
DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "uasync CPU load started: ua=%p period=100ms window=1000ms", (void*)ua);
return;
}
if (cpu_ns < ua->load_previous_cpu_ns || now <= ua->load_previous_us) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "uasync CPU load clock regression: ua=%p wall=%llu->%llu cpu=%llu->%llu",
(void*)ua, (unsigned long long)ua->load_previous_us, (unsigned long long)now,
(unsigned long long)ua->load_previous_cpu_ns, (unsigned long long)cpu_ns);
ua->load_state = -1; ua->load.valid = 0;
return;
}
uint64_t elapsed = now - ua->load_previous_us, delta_cpu = cpu_ns - ua->load_previous_cpu_ns;
uint64_t late = now - ua->load_next_us;
if (late >= UASYNC_LOAD_PERIOD_US && now - ua->last_poll_exit_us >= UASYNC_LOAD_PERIOD_US) {
DEBUG_WARN(DEBUG_CATEGORY_SYS, "uasync CPU sample delayed: ua=%p interval=%lluus late=%lluus missed=%llu",
(void*)ua, (unsigned long long)elapsed, (unsigned long long)late,
(unsigned long long)(late / UASYNC_LOAD_PERIOD_US));
}
unsigned int capacity = sizeof(ua->load_history) / sizeof(ua->load_history[0]);
ua->load_history[ua->load_head] = (struct uasync_load_interval){ua->load_previous_us, now, delta_cpu};
ua->load_head = (ua->load_head + 1) % capacity;
if (ua->load_count < capacity) ua->load_count++;
uint64_t cutoff = now > UASYNC_LOAD_WINDOW_US ? now - UASYNC_LOAD_WINDOW_US : 0;
uint64_t window_us = 0;
double window_cpu_ns = 0;
for (unsigned int i = 0; i < ua->load_count; i++) {
const struct uasync_load_interval* sample = &ua->load_history[i];
if (sample->end_us <= cutoff) continue;
uint64_t start = sample->start_us > cutoff ? sample->start_us : cutoff;
uint64_t overlap = sample->end_us - start;
window_us += overlap;
window_cpu_ns += (double)sample->cpu_ns * overlap / (sample->end_us - sample->start_us);
}
ua->load = (struct uasync_load){
.current_percent = (double)delta_cpu / elapsed / 10.0,
.average_percent = window_cpu_ns / window_us / 10.0,
.sample_time_us = now, .interval_us = elapsed, .window_us = window_us, .valid = 1
};
// Разница точности CPU/реальных часов может дать небольшое превышение 100%.
if (ua->load.current_percent > 100.0) ua->load.current_percent = 100.0;
if (ua->load.average_percent > 100.0) ua->load.average_percent = 100.0;
ua->load_previous_us = now; ua->load_previous_cpu_ns = cpu_ns;
ua->load_next_us += (late / UASYNC_LOAD_PERIOD_US + 1) * UASYNC_LOAD_PERIOD_US;
if (now >= ua->load_log_us) {
DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "uasync CPU load: ua=%p current=%.2f%% average=%.2f%% interval=%lluus window=%lluus late=%lluus",
(void*)ua, ua->load.current_percent, ua->load.average_percent, (unsigned long long)elapsed,
(unsigned long long)window_us, (unsigned long long)late);
ua->load_log_us = now + UASYNC_LOAD_WINDOW_US;
}
}
// Секунда непрерывного ожидания полностью вытесняет прежнюю нагрузку из окна.
static void uasync_load_after_wait(struct UASYNC* ua, uint64_t wait_start_us) {
uint64_t now = get_time_us();
ua->last_poll_exit_us = now;
if (ua->load_state <= 0 || now - wait_start_us < UASYNC_LOAD_WINDOW_US) return;
uint64_t cpu_ns;
if (uasync_thread_cpu_ns(&cpu_ns) < 0) { ua->load_state = -1; ua->load.valid = 0; return; }
ua->load_history[0] = (struct uasync_load_interval){now - UASYNC_LOAD_WINDOW_US, now, 0};
ua->load_head = 1; ua->load_count = 1;
ua->load_previous_us = now; ua->load_previous_cpu_ns = cpu_ns;
ua->load_next_us = now + UASYNC_LOAD_PERIOD_US;
ua->load = (struct uasync_load){.sample_time_us = now, .interval_us = now - wait_start_us,
.window_us = UASYNC_LOAD_WINDOW_US, .valid = 1};
DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "uasync CPU load: ua=%p current=0%% average=0%% idle=%lluus",
(void*)ua, (unsigned long long)(now - wait_start_us));
ua->load_log_us = now + UASYNC_LOAD_WINDOW_US;
}
err_t uasync_get_load(struct UASYNC* ua, struct uasync_load* load) {
if (!ua || !load) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "uasync_get_load: invalid argument ua=%p load=%p", (void*)ua, (void*)load);
return ERR_FAIL;
}
uasync_update_load(ua);
*load = ua->load;
return ERR_OK;
}
static void uasync_poll_once(struct UASYNC* ua, int timeout_tb) {
process_immediate_queue(ua);
uint64_t now_us_poll_entry = get_time_us();
@@ -1050,9 +1162,10 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
struct epoll_event events[64]; // Stack-allocated array for events
int max_events = 64;
uint64_t wait_start_us = get_time_us();
int ret = epoll_wait(ua->epoll_fd, events, max_events, timeout_ms);
int saved_errno = errno;
ua->last_poll_exit_us = get_time_us();
uasync_load_after_wait(ua, wait_start_us);
if (ret < 0) {
if (saved_errno == EINTR) {
DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "epoll_wait→EINTR");
@@ -1087,6 +1200,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
// If no sockets to poll, just wait and process timeouts
if (total_fds == 0) {
uint64_t wait_start_us = get_time_us();
if (timeout_ms > 0) {
#ifdef _WIN32
Sleep(timeout_ms);
@@ -1095,7 +1209,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
nanosleep(&ts, NULL);
#endif
}
ua->last_poll_exit_us = get_time_us();
uasync_load_after_wait(ua, wait_start_us);
process_timeouts(ua);
return;
}
@@ -1138,11 +1252,13 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
uint64_t wait_start_us = get_time_us();
int ret = select((int)max_fd + 1, &read_fds, &write_fds, &except_fds, timeout_ms < 0 ? NULL : &tv);
ua->last_poll_exit_us = get_time_us();
int saved_error = ret < 0 ? WSAGetLastError() : 0;
uasync_load_after_wait(ua, wait_start_us);
if (ret < 0) {
int err = WSAGetLastError();
int err = saved_error;
if (err != WSAEINTR) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "select failed: %d", err);
}
@@ -1229,9 +1345,10 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
}
}
uint64_t wait_start_us = get_time_us();
int ret = poll(ua->poll_fds, ua->poll_fds_count, timeout_ms);
int saved_errno = errno;
ua->last_poll_exit_us = get_time_us();
uasync_load_after_wait(ua, wait_start_us);
if (ret < 0) {
if (saved_errno == EINTR) {
return;
@@ -1344,7 +1461,12 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) {
}
// Put this near the top of u_async.c, after includes and before uasync_create
void uasync_poll(struct UASYNC* ua, int timeout_tb) {
if (!ua || !ua->sockets || !ua->timeout_heap) return;
uasync_update_load(ua);
uasync_poll_once(ua, timeout_tb);
uasync_update_load(ua);
}
#ifdef _WIN32
@@ -1793,7 +1915,8 @@ void uasync_stop(struct UASYNC* ua) {
}
void uasync_mainloop(struct UASYNC* ua) {
while (!__atomic_load_n(&ua->stop, __ATOMIC_ACQUIRE)) {
uasync_poll(ua, -1);
}
if (!ua) { DEBUG_ERROR(DEBUG_CATEGORY_SYS, "uasync_mainloop: null instance"); return; }
while (!__atomic_load_n(&ua->stop, __ATOMIC_ACQUIRE)) {
uasync_poll(ua, -1);
}
}