Skip to content

Commit ca41210

Browse files
authored
Merge pull request #6353 from IntersectMBO/russoul/patch-up-cpu-time-metric-darwin
trace-resources: Patch up process CPU time metric on Darwin
2 parents 4f8d4ad + d2a84de commit ca41210

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

cardano-node/cardano-node.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ library
214214
, time
215215
, trace-dispatcher ^>= 2.10.0
216216
, trace-forward ^>= 2.3.0
217-
, trace-resources ^>= 0.2.3
217+
, trace-resources ^>= 0.2.4
218218
, tracer-transformers
219219
, transformers
220220
, transformers-except

cardano-tracer/cardano-tracer.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ library
199199
, time
200200
, trace-dispatcher ^>= 2.10.0
201201
, trace-forward ^>= 2.3.0
202-
, trace-resources ^>= 0.2.3
202+
, trace-resources ^>= 0.2.4
203203
, wai ^>= 3.2
204204
, warp ^>= 3.4
205205
, yaml

trace-resources/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Revision history for trace-resources
22

3+
## 0.2.4
4+
* Patch up CPU time metric on darwin
5+
36
## 0,2,3
47

58
* New names for metrics

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)

trace-resources/trace-resources.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cabal-version: 3.0
22

33
name: trace-resources
4-
version: 0.2.3
4+
version: 0.2.4
55
synopsis: Package for tracing resources for linux, mac and windows
66
description: Package for tracing resources for linux, mac and windows.
77
category: Cardano,

0 commit comments

Comments
 (0)