#include #include #include #include #include #include static int u64_to_string(uint64_t n, char* buf) { int i = 0; if (n == 0) { buf[i++] = '0'; buf[i] = '\0'; return i; } char rev[24]; int r_idx = 0; while (n > 0) { rev[r_idx++] = (char)('0' + (n % 10)); n /= 10; } while (r_idx > 0) { buf[i++] = rev[--r_idx]; } buf[i] = '\0'; return i; } int main(void) { NTL_CONSOLE_INFO console; SYS_MEMORY_STATUS mem; char metricsBuf[128]; size_t previousLength = 0; while (true) { if (NtlConsoleQueryInfo(SYS_HANDLE_INVALID, &console) == NTLSTATUS_SUCCESS && SysGetMemoryStatus(&mem) == NTLSTATUS_SUCCESS) { // If the system isn't reporting columns correctly, default to a safe standard width int columns = (console.Columns > 0) ? (int)console.Columns : 80; char freeStr[24], poolStr[24], heapStr[24]; u64_to_string(mem.FreePhysicalBytes / 1024u, freeStr); u64_to_string(mem.KernelPoolBytes / 1024u, poolStr); u64_to_string(mem.UserHeapBytes / 1024u, heapStr); metricsBuf[0] = '\0'; strcat(metricsBuf, "[ Free: "); strcat(metricsBuf, freeStr); strcat(metricsBuf, "K | Pool: "); strcat(metricsBuf, poolStr); strcat(metricsBuf, "K | Heap: "); strcat(metricsBuf, heapStr); strcat(metricsBuf, "K ]"); size_t textLen = strlen(metricsBuf); // Erase any trailing characters left by a longer previous sample. if (previousLength > textLen && previousLength < sizeof(metricsBuf)) { memset(metricsBuf + textLen, ' ', previousLength - textLen); textLen = previousLength; } int targetX = columns - (int)textLen; if (targetX >= 0) { size_t written = 0; if (NtlConsoleWriteAt(SYS_HANDLE_INVALID, targetX, 0, metricsBuf, textLen, &written) == NTLSTATUS_SUCCESS && written == textLen) previousLength = textLen; } } SysSleep(2000); } return 0; }