From: Damjan Jovanovic Subject: [PATCH 1/7] ntdll: implement setting SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION.IdleTime on FreeBSD Message-Id: Date: Sun, 31 Oct 2021 17:47:26 +0200 Signed-off-by: Damjan Jovanovic --- dlls/ntdll/unix/system.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/dlls/ntdll/unix/system.c b/dlls/ntdll/unix/system.c index c37fc360f3e..6640a66597b 100644 --- a/dlls/ntdll/unix/system.c +++ b/dlls/ntdll/unix/system.c @@ -52,6 +52,9 @@ #ifdef HAVE_SYS_RANDOM_H # include #endif +#ifdef HAVE_SYS_RESOURCE_H +# include +#endif #ifdef HAVE_IOKIT_IOKITLIB_H # include # include @@ -2611,7 +2614,7 @@ NTSTATUS WINAPI NtQuerySystemInformation( SYSTEM_INFORMATION_CLASS class, vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count * sizeof(natural_t)); } } -#else +#elif defined(linux) { FILE *cpuinfo = fopen("/proc/stat", "r"); if (cpuinfo) @@ -2644,6 +2647,33 @@ NTSTATUS WINAPI NtQuerySystemInformation( SYSTEM_INFORMATION_CLASS class, fclose(cpuinfo); } } +#elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) + { + static int clockrate_name[] = { CTL_KERN, KERN_CLOCKRATE }; + size_t size = 0; + struct clockinfo clockrate; + int have_clockrate; + long *ptimes; + + size = sizeof(clockrate); + have_clockrate = !sysctl(clockrate_name, 2, &clockrate, &size, NULL, 0); + size = out_cpus * CPUSTATES * sizeof(long); + ptimes = malloc(size + 1); + if (ptimes) + { + if (have_clockrate && (!sysctlbyname("kern.cp_times", ptimes, &size, NULL, 0) || errno == ENOMEM)) + { + for (cpus = 0; cpus < out_cpus; cpus++) + { + if (cpus * CPUSTATES * sizeof(long) >= size) break; + sppi[cpus].IdleTime.QuadPart = (ULONGLONG)ptimes[cpus*CPUSTATES + CP_IDLE] * 10000000 / clockrate.stathz; + sppi[cpus].KernelTime.QuadPart = (ULONGLONG)ptimes[cpus*CPUSTATES + CP_SYS] * 10000000 / clockrate.stathz; + sppi[cpus].UserTime.QuadPart = (ULONGLONG)ptimes[cpus*CPUSTATES + CP_USER] * 10000000 / clockrate.stathz; + } + } + free(ptimes); + } + } #endif if (cpus == 0) {