Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

Idle CPU: --measure-idle reads real process CPU time (std::clock is wall time on Windows: idle read ~100%); fails above 2% of a core; nightly 30 s idle check

islandmedbd65d93cf4ca
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ce636505..262c92fd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -903,9 +903,13 @@ if(SCULPTFORGE_BUILD_TESTS)
# A crash leaves a note in the log file (#16). Needs no window.
add_test(NAME crash_log COMMAND ${CMAKE_COMMAND} -DAPP=$<TARGET_FILE:sculptforge>
-DDIR=${CMAKE_CURRENT_BINARY_DIR}/crash-log-test -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/crash_log.cmake)
- # Idle, the app draws nothing (#14): --measure-idle fails when it keeps drawing frames.
+ # Idle, the app draws nothing (#14) and uses next to no CPU: --measure-idle fails when it keeps drawing frames or
+ # its process CPU is over SF_IDLE_CPU_MAX % of one core (4 s here: a spinning loop's 100 % fails it; the 30 s
+ # nightly holds it to 2 %).
add_test(NAME ui_idle_redraw COMMAND sculptforge --measure-idle 4)
- set_tests_properties(ui_idle_redraw PROPERTIES LABELS ui TIMEOUT 60 ENVIRONMENT "SF_NO_UPDATE_CHECK=1")
+ set_tests_properties(ui_idle_redraw PROPERTIES LABELS ui TIMEOUT 60 ENVIRONMENT "SF_NO_UPDATE_CHECK=1;SF_IDLE_CPU_MAX=10")
+ add_test(NAME ui_idle_cpu_30s COMMAND sculptforge --measure-idle 30)
+ set_tests_properties(ui_idle_cpu_30s PROPERTIES LABELS "ui;nightly" TIMEOUT 120 RUN_SERIAL TRUE ENVIRONMENT "SF_NO_UPDATE_CHECK=1")
# sculptforge --calibrate on this machine's GPU: the JSON it prints passes the schema check (it exits 1 if not).
add_test(NAME ui_calibrate_json COMMAND sculptforge --calibrate-quick --calibrate-no-save)
set_tests_properties(ui_calibrate_json PROPERTIES LABELS ui TIMEOUT 60)
diff --git a/changes/fix-idle-cpu.md b/changes/fix-idle-cpu.md
new file mode 100644
index 00000000..a3a2dd47
--- /dev/null
+++ b/changes/fix-idle-cpu.md
@@ -0,0 +1,9 @@
+---
+fixed: `--measure-idle` reported about 100 % of one core on Windows while the app sat idle: it timed itself with the
+ C library's clock, which on Windows counts wall time, not processor time. It now reads the process's real CPU time
+ on every OS. Measured from outside on Windows (RTX 3060), idle 0.11.1 and 0.11.2 (a sphere open) use 0.00 % of a core, and Linux uses
+ 0.7 %: there was no busy loop. The idle tests now also fail if the app uses more than 2 % of a core over 30 s.
+---
+Reviewer notes: processCpuSeconds() in core/HardwareProfile (GetProcessTimes / getrusage) replaces std::clock() in
+App::run's --measure-idle; SF_IDLE_CPU_MAX (default 2) fails the run above it. ui_idle_redraw (4 s) at 10 % catches a
+spin; new nightly ui_idle_cpu_30s at 2 %. External check: Get-Process TotalProcessorTime per thread over 20 s idle.
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 4affd97b..38141738 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -834,7 +834,7 @@ int App::run() {
constexpr double kMeasureSettle = 3.0;
const double started = seconds();
uint64_t framesDrawn = 0, wakeUps = 0, framesAtMeasure = 0, wakesAtMeasure = 0;
- std::clock_t cpuAtMeasure = 0; // processor time of every thread (the driver's included)
+ double cpuAtMeasure = 0.0; // processor time of every thread (the driver's included)
bool measuring = false;
// Input while measuring (a pointer crossing the window on a shared desktop) restarts the count once it has
// passed: idle means no input. After kMeasureGiveUp seconds it counts whatever comes (the test's timeout).
@@ -887,17 +887,21 @@ int App::run() {
measuring = true;
framesAtMeasure = framesDrawn;
wakesAtMeasure = wakeUps;
- cpuAtMeasure = std::clock();
+ cpuAtMeasure = processCpuSeconds();
} else if (measuring && t0 >= measureFrom + options_.measureIdle) {
const double fps = double(framesDrawn - framesAtMeasure) / double(options_.measureIdle);
- const double cpu = 100.0 * double(std::clock() - cpuAtMeasure) / CLOCKS_PER_SEC / double(options_.measureIdle);
+ const double cpu = 100.0 * (processCpuSeconds() - cpuAtMeasure) / double(options_.measureIdle);
std::printf("Idle: %llu frames, %llu wake-ups in %.1f s: %.2f fps, CPU %.1f%% of one core (%s redraw)\n",
(unsigned long long)(framesDrawn - framesAtMeasure), (unsigned long long)(wakeUps - wakesAtMeasure),
double(options_.measureIdle), fps, cpu, continuous ? "continuous" : "on-demand");
if (measureRestarts > 0) std::printf(" (restarted %d times after input)\n", measureRestarts);
std::fflush(stdout);
// Idle is 0 fps; a pointer crossing the window draws about a second's worth. Every frame is 60+.
- return continuous || fps < 5.0 ? 0 : 1;
+ // And near 0 CPU (a laptop's battery): SF_IDLE_CPU_MAX percent of one core, 2 unless set.
+ const char* cpuMaxEnv = std::getenv("SF_IDLE_CPU_MAX");
+ const double cpuMax = cpuMaxEnv && *cpuMaxEnv ? std::atof(cpuMaxEnv) : 2.0;
+ if (!continuous && cpu > cpuMax) std::printf("Idle: CPU over %.1f%% of one core: something spins while idle\n", cpuMax);
+ return continuous || (fps < 5.0 && cpu <= cpuMax) ? 0 : 1;
}
}
// Nothing to draw: sleep until an event, or the next poller tick.
diff --git a/src/core/HardwareProfile.cpp b/src/core/HardwareProfile.cpp
index 1ba0ea40..549e1913 100644
--- a/src/core/HardwareProfile.cpp
+++ b/src/core/HardwareProfile.cpp
@@ -25,6 +25,7 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
+#include <sys/resource.h>
#include <sys/utsname.h>
#include <unistd.h>
#endif
@@ -230,6 +231,19 @@ uint64_t sysfsVramMB(const std::string& glVendor, const std::string& sysfsRoot)
return best;
}
+double processCpuSeconds() {
+#if defined(_WIN32)
+ FILETIME created, exited, kernel, user;
+ if (!GetProcessTimes(GetCurrentProcess(), &created, &exited, &kernel, &user)) return 0.0;
+ const auto ticks = [](const FILETIME& f) { return (uint64_t(f.dwHighDateTime) << 32) | f.dwLowDateTime; };
+ return double(ticks(kernel) + ticks(user)) * 1e-7; // 100 ns units
+#else
+ rusage u{};
+ if (getrusage(RUSAGE_SELF, &u) != 0) return 0.0;
+ return double(u.ru_utime.tv_sec + u.ru_stime.tv_sec) + double(u.ru_utime.tv_usec + u.ru_stime.tv_usec) * 1e-6;
+#endif
+}
+
void probeSystem(HardwareInfo& info) {
info.appVersion = SCULPTFORGE_VERSION;
info.cpuThreads = int(std::max(1u, std::thread::hardware_concurrency()));
diff --git a/src/core/HardwareProfile.h b/src/core/HardwareProfile.h
index f9292cfc..97259cab 100644
--- a/src/core/HardwareProfile.h
+++ b/src/core/HardwareProfile.h
@@ -66,6 +66,9 @@ struct HardwareInfo {
// Fills the machine fields (os, cpu, cpuThreads, ramMB, simd, simdBuild, appVersion).
void probeSystem(HardwareInfo& info);
+// Processor time this process has used so far, every thread's (user + system), in seconds. Not std::clock(): on
+// Windows that is wall time, so an idle app read as 100 % of a core.
+double processCpuSeconds();
// Classifies the GPU from its OpenGL strings: Software (llvmpipe, softpipe, SwiftShader, Microsoft Basic Render,
// GDI Generic, Apple Software Renderer), Unified (Apple GPUs, including Asahi Linux), Integrated (Intel UHD / Iris /
// Xe, AMD APUs whose name has no model number) or Discrete (NVIDIA GeForce / Quadro / RTX, AMD Radeon RX / Pro,