GET /api/processes — list codebase-memory-mcp processes via ps */
| 556 | |
| 557 | /* GET /api/processes — list codebase-memory-mcp processes via ps */ |
| 558 | static void handle_processes(cbm_http_conn_t *c) { |
| 559 | char buf[8192]; |
| 560 | int pos = 0; |
| 561 | |
| 562 | #ifdef _WIN32 |
| 563 | /* Windows: GetProcessMemoryInfo + GetProcessTimes */ |
| 564 | PROCESS_MEMORY_COUNTERS pmc; |
| 565 | FILETIME ft_create, ft_exit, ft_kernel, ft_user; |
| 566 | double user_s = 0, sys_s = 0; |
| 567 | size_t rss_bytes = 0; |
| 568 | if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) |
| 569 | rss_bytes = pmc.WorkingSetSize; |
| 570 | if (GetProcessTimes(GetCurrentProcess(), &ft_create, &ft_exit, &ft_kernel, &ft_user)) { |
| 571 | ULARGE_INTEGER u, k; |
| 572 | u.LowPart = ft_user.dwLowDateTime; |
| 573 | u.HighPart = ft_user.dwHighDateTime; |
| 574 | k.LowPart = ft_kernel.dwLowDateTime; |
| 575 | k.HighPart = ft_kernel.dwHighDateTime; |
| 576 | user_s = (double)u.QuadPart / 1e7; |
| 577 | sys_s = (double)k.QuadPart / 1e7; |
| 578 | } |
| 579 | http_appendf(buf, sizeof(buf), &pos, |
| 580 | "{\"self_pid\":%d,\"self_rss_mb\":%.1f," |
| 581 | "\"self_user_cpu_s\":%.1f,\"self_sys_cpu_s\":%.1f,\"processes\":[]}", |
| 582 | (int)_getpid(), (double)rss_bytes / (1024.0 * 1024.0), user_s, sys_s); |
| 583 | #else |
| 584 | struct rusage ru; |
| 585 | getrusage(RUSAGE_SELF, &ru); |
| 586 | long rss_kb = ru.ru_maxrss; |
| 587 | #ifdef __APPLE__ |
| 588 | rss_kb /= 1024; |
| 589 | #endif |
| 590 | http_appendf(buf, sizeof(buf), &pos, |
| 591 | "{\"self_pid\":%d,\"self_rss_mb\":%.1f," |
| 592 | "\"self_user_cpu_s\":%.1f,\"self_sys_cpu_s\":%.1f,\"processes\":[", |
| 593 | (int)getpid(), (double)rss_kb / 1024.0, |
| 594 | (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1e6, |
| 595 | (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec / 1e6); |
| 596 | |
| 597 | FILE *fp = popen("LC_ALL=C ps -eo pid,pcpu,rss,etime,comm 2>/dev/null" |
| 598 | " | grep '[c]odebase-memory-mcp'", |
| 599 | "r"); |
| 600 | int proc_count = 0; |
| 601 | if (fp) { |
| 602 | char line[1024]; |
| 603 | while (fgets(line, sizeof(line), fp)) { |
| 604 | int pid = 0; |
| 605 | float cpu = 0; |
| 606 | long rss = 0; |
| 607 | char elapsed[64] = {0}; |
| 608 | char comm[256] = {0}; |
| 609 | |
| 610 | if (sscanf(line, "%d %f %ld %63s %255s", &pid, &cpu, &rss, elapsed, comm) >= 4) { |
| 611 | if (proc_count > 0) |
| 612 | buf[pos++] = ','; |
| 613 | http_appendf(buf, sizeof(buf), &pos, |
| 614 | "{\"pid\":%d,\"cpu\":%.1f,\"rss_mb\":%.1f," |
| 615 | "\"elapsed\":\"%s\",\"command\":\"%s\",\"is_self\":%s}", |
no test coverage detected