Skip to content

Commit b3999ef

Browse files
committed
trace-resources: Patch up CPU time metric on darwin
1 parent 4f8d4ad commit b3999ef

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

trace-resources/cbits/os-support-darwin.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <mach/mach_host.h>
1313
#include <net/route.h>
1414
#include <net/if_dl.h>
15+
#include <libproc.h>
1516

1617
/*
1718
// includes for c_get_sys_disk_io_counters
@@ -270,3 +271,19 @@ int c_get_sys_network_io_counters2(NET_IO *counters) {
270271
free(msghdrbuf);
271272
return 1;
272273
}
274+
275+
uint64_t c_get_process_cpu_time_microseconds(pid_t pid) {
276+
struct proc_taskinfo ti;
277+
278+
int ret = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &ti, sizeof(ti));
279+
280+
if (ret <= 0) {
281+
perror("proc_pidinfo");
282+
return 0;
283+
}
284+
285+
uint64_t user_microsec = ti.pti_total_user / 1e3;
286+
uint64_t sys_microsec = ti.pti_total_system / 1e3;
287+
288+
return user_microsec + sys_microsec;
289+
}

trace-resources/include/os-support-darwin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ long c_get_boot_time2();
3535
int c_get_sys_cpu_times2(CPU_TIMES *counters);
3636
int c_get_sys_network_io_counters2(NET_IO *counters);
3737
int c_get_sys_disk_io_counters2(DISK_COUNTERS *counters);
38+
uint64_t c_get_process_cpu_time_microseconds(pid_t);

trace-resources/src/Cardano/Logging/Resources/Darwin.hsc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ instance Storable MachTaskBasicInfo where
7474

7575
foreign import ccall unsafe c_get_process_memory_info2 :: Ptr MachTaskBasicInfo -> CInt -> IO CInt
7676

77+
foreign import ccall unsafe c_get_process_cpu_time_microseconds :: CInt -> IO Word64
78+
7779

7880
getMemoryInfo :: ProcessID -> IO MachTaskBasicInfo
7981
getMemoryInfo pid =
@@ -84,13 +86,12 @@ getMemoryInfo pid =
8486

8587
readResourceStatsInternal :: IO (Maybe ResourceStats)
8688
readResourceStatsInternal = getProcessID >>= \pid -> do
87-
cpu <- getMemoryInfo pid
8889
rts <- GhcStats.getRTSStats
8990
mem <- getMemoryInfo pid
91+
cpuTimeMicro <- c_get_process_cpu_time_microseconds (fromIntegral pid)
9092
pure . Just $
9193
Resources
92-
{ rCentiCpu = timeValToCenti (_user_time cpu)
93-
+ timeValToCenti (_system_time cpu)
94+
{ rCentiCpu = usToCenti cpuTimeMicro
9495
, rCentiGC = nsToCenti $ GhcStats.gc_cpu_ns rts
9596
, rCentiMut = nsToCenti $ GhcStats.mutator_cpu_ns rts
9697
, rGcsMajor = fromIntegral $ GhcStats.major_gcs rts
@@ -109,8 +110,5 @@ readResourceStatsInternal = getProcessID >>= \pid -> do
109110
where
110111
nsToCenti :: GhcStats.RtsTime -> Word64
111112
nsToCenti = fromIntegral . (`div` 10000000)
112-
timeValToCenti :: TIME_VALUE_T -> Word64
113-
timeValToCenti tv = usFromTimeValue tv `div` 10000
114-
115-
usFromTimeValue :: TIME_VALUE_T -> Word64
116-
usFromTimeValue (TIME_VALUE_T s us) = s * 1000000 + us
113+
usToCenti :: Word64 -> Word64
114+
usToCenti = (`div` 10000)

0 commit comments

Comments
 (0)