Skip to content

Commit

Permalink
trace-resources: update & make available
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaro authored and deepfire committed Jan 11, 2022
1 parent 54c3adf commit ad3a62c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 50 deletions.
3 changes: 3 additions & 0 deletions cabal.project
Expand Up @@ -84,6 +84,9 @@ package trace-dispatcher
package trace-forward
tests: True

package trace-resources
tests: True

-- The following is needed because Nix is doing something crazy.
package byron-spec-ledger
tests: False
Expand Down
12 changes: 6 additions & 6 deletions trace-resources/cbits/os-support-darwin.c
Expand Up @@ -32,7 +32,7 @@

/* c_get_process_memory_info */

int c_get_process_memory_info(struct mach_task_basic_info *counters, int pid)
int c_get_process_memory_info2(struct mach_task_basic_info *counters, int pid)
{
task_t task = MACH_PORT_NULL;
if (task_for_pid(current_task(), pid, &task) != KERN_SUCCESS) {
Expand All @@ -49,7 +49,7 @@ int c_get_process_memory_info(struct mach_task_basic_info *counters, int pid)

/* c_get_host_info */
/* currently this is not used
int c_get_host_info(struct host_basic_info *counters)
int c_get_host_info2(struct host_basic_info *counters)
{
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
mach_port_t host_port = mach_host_self();
Expand All @@ -63,7 +63,7 @@ int c_get_host_info(struct host_basic_info *counters)

/* c_get_boot_time */

long c_get_boot_time()
long c_get_boot_time2()
{
// copied from psutil
// fetch sysctl "kern.boottime"
Expand All @@ -79,7 +79,7 @@ long c_get_boot_time()
}

/* c_get_sys_cpu_times */
int c_get_sys_cpu_times(CPU_TIMES *counters)
int c_get_sys_cpu_times2(CPU_TIMES *counters)
{
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
host_cpu_load_info_data_t r_load;
Expand All @@ -103,7 +103,7 @@ int c_get_sys_cpu_times(CPU_TIMES *counters)

/* c_get_sys_disk_io_counters */
/* adapted from psutil */
int c_get_sys_disk_io_counters(DISK_COUNTERS *counters) {
int c_get_sys_disk_io_counters2(DISK_COUNTERS *counters) {
counters->ndsks = 0;
// uncomment the following to extract disk I/O metrics
// requires to include the right headers (see top of this file)
Expand Down Expand Up @@ -221,7 +221,7 @@ int c_get_sys_disk_io_counters(DISK_COUNTERS *counters) {

/* c_get_sys_network_io_counters */
/* adapted from psutil */
int c_get_sys_network_io_counters(NET_IO *counters) {
int c_get_sys_network_io_counters2(NET_IO *counters) {
counters->nifs = 0;
int noutput = 0;
char *msghdrbuf = NULL, *end_of_list, *next;
Expand Down
12 changes: 6 additions & 6 deletions trace-resources/cbits/os-support-win.c
Expand Up @@ -7,7 +7,7 @@

/* c_get_process_memory_info */

int c_get_process_memory_info (PROCESS_MEMORY_COUNTERS *counters, DWORD pid) {
int c_get_process_memory_info2 (PROCESS_MEMORY_COUNTERS *counters, DWORD pid) {
HANDLE hProc;
hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
Expand All @@ -23,7 +23,7 @@ int c_get_process_memory_info (PROCESS_MEMORY_COUNTERS *counters, DWORD pid) {

/* c_get_io_counters */

int c_get_io_counters (IO_COUNTERS *counters, DWORD pid) {
int c_get_io_counters2 (IO_COUNTERS *counters, DWORD pid) {
HANDLE hProc;
hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, pid );
Expand All @@ -39,7 +39,7 @@ int c_get_io_counters (IO_COUNTERS *counters, DWORD pid) {
// #define HI_T 429.4967296

/* c_get_sys_cpu_times */
int c_get_sys_cpu_times (CPU_TIMES *cputimes) {
int c_get_sys_cpu_times2 (CPU_TIMES *cputimes) {
FILETIME usert={0,0}, kernelt={0,0}, idlet={0,0};
if (! GetSystemTimes(&idlet, &kernelt, &usert) ) {
return -2;
Expand All @@ -52,7 +52,7 @@ int c_get_sys_cpu_times (CPU_TIMES *cputimes) {
}

/* c_get_proc_cpu_times */
int c_get_proc_cpu_times (CPU_TIMES *cputimes, DWORD pid) {
int c_get_proc_cpu_times2 (CPU_TIMES *cputimes, DWORD pid) {
HANDLE hProc;
hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, pid );
Expand All @@ -73,13 +73,13 @@ int c_get_proc_cpu_times (CPU_TIMES *cputimes, DWORD pid) {
}

/* c_get_system_info */
int c_get_system_info (SYSTEM_INFO *sysinfo) {
int c_get_system_info2 (SYSTEM_INFO *sysinfo) {
GetSystemInfo (sysinfo);
return 1;
}

/* c_get_win_bits */
int c_get_win_bits (DWORD pid) {
int c_get_win_bits2 (DWORD pid) {
BOOL res;
HANDLE hProc;
hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
Expand Down
10 changes: 5 additions & 5 deletions trace-resources/include/os-support-darwin.h
Expand Up @@ -29,9 +29,9 @@ typedef struct _DISK_COUNTERS {
DISK_INFO dsks[MAX_DISK_COUNTERS];
} DISK_COUNTERS;

int c_get_process_memory_info (struct mach_task_basic_info *counters, int pid);
int c_get_process_memory_info2 (struct mach_task_basic_info *counters, int pid);
//int c_get_host_info (struct host_basic_info *counters);
long c_get_boot_time();
int c_get_sys_cpu_times(CPU_TIMES *counters);
int c_get_sys_network_io_counters(NET_IO *counters);
int c_get_sys_disk_io_counters(DISK_COUNTERS *counters);
long c_get_boot_time2();
int c_get_sys_cpu_times2(CPU_TIMES *counters);
int c_get_sys_network_io_counters2(NET_IO *counters);
int c_get_sys_disk_io_counters2(DISK_COUNTERS *counters);
12 changes: 6 additions & 6 deletions trace-resources/include/os-support-win.h
Expand Up @@ -5,10 +5,10 @@ typedef struct _CPU_TIMES {
ULONGLONG idletime;
} CPU_TIMES;

int c_get_process_memory_info (PROCESS_MEMORY_COUNTERS *counters, DWORD pid);
int c_get_io_counters (IO_COUNTERS *counters, DWORD pid);
int c_get_sys_cpu_times (CPU_TIMES *cputimes);
int c_get_proc_cpu_times (CPU_TIMES *cputimes, DWORD pid);
int c_get_system_info (SYSTEM_INFO *sysinfo);
int c_get_win_bits (DWORD pid);
int c_get_process_memory_info2 (PROCESS_MEMORY_COUNTERS *counters, DWORD pid);
int c_get_io_counters2 (IO_COUNTERS *counters, DWORD pid);
int c_get_sys_cpu_times2 (CPU_TIMES *cputimes);
int c_get_proc_cpu_times2 (CPU_TIMES *cputimes, DWORD pid);
int c_get_system_info2 (SYSTEM_INFO *sysinfo);
int c_get_win_bits2 (DWORD pid);

6 changes: 3 additions & 3 deletions trace-resources/src/Cardano/Logging/Resources/Darwin.hsc
Expand Up @@ -72,14 +72,14 @@ instance Storable MachTaskBasicInfo where
<*> (#peek struct mach_task_basic_info, suspend_count) ptr
poke _ _ = pure ()

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


getMemoryInfo :: ProcessID -> IO MachTaskBasicInfo
getMemoryInfo pid =
allocaBytes 128 $ \ptr -> do
throwIfNeg_ (\res -> "c_get_process_memory_info: failure returned: " ++ show (pred res))
(succ <$> c_get_process_memory_info ptr (fromIntegral pid))
throwIfNeg_ (\res -> "c_get_process_memory_info2: failure returned: " ++ show (pred res))
(succ <$> c_get_process_memory_info2 ptr (fromIntegral pid))
peek ptr

readRessoureStatsInternal :: IO (Maybe ResourceStats)
Expand Down
36 changes: 18 additions & 18 deletions trace-resources/src/Cardano/Logging/Resources/Types.hs
Expand Up @@ -10,7 +10,7 @@ module Cardano.Logging.Resources.Types
) where


import Cardano.Logging.Types
import Cardano.Logging
import Data.Aeson
import Data.Text (pack)
import Data.Word
Expand Down Expand Up @@ -75,15 +75,15 @@ jsonEncodingOptions = defaultOptions
docResourceStats :: Documented ResourceStats
docResourceStats = Documented [
DocMsg
(pure 0)
[("Stat.Cputicks", "Reports the CPU ticks, sice the process was started")
,("Mem.Resident", "TODO JNF")
,("RTS.GcLiveBytes", "TODO JNF")
,("RTS.GcMajorNum", "TODO JNF")
,("RTS.GcMinorNum", "TODO JNF")
,("RTS.Gcticks", "TODO JNF")
,("RTS.Mutticks", "TODO JNF")
,("RTS.Threads","TODO JNF")
anyProto
[("stat.cputicks", "Reports the CPU ticks, sice the process was started")
,("mem.resident", "TODO JNF")
,("rts.gcLiveBytes", "TODO JNF")
,("rts.gcMajorNum", "TODO JNF")
,("rts.gcMinorNum", "TODO JNF")
,("rts.gcticks", "TODO JNF")
,("rts.mutticks", "TODO JNF")
,("rts.threads","TODO JNF")
]
"TODO JNF"
]
Expand Down Expand Up @@ -112,12 +112,12 @@ instance LogFormatting ResourceStats where
]

asMetrics rs =
[ IntM "Stat.Cputicks" (fromIntegral $ rCentiCpu rs)
, IntM "Mem.Resident" (fromIntegral $ rRSS rs)
, IntM "RTS.GcLiveBytes" (fromIntegral $ rLive rs)
, IntM "RTS.GcMajorNum" (fromIntegral $ rGcsMajor rs)
, IntM "RTS.GcMinorNum" (fromIntegral $ rGcsMinor rs)
, IntM "RTS.Gcticks" (fromIntegral $ rCentiGC rs)
, IntM "RTS.Mutticks" (fromIntegral $ rCentiMut rs)
, IntM "Stat.Threads" (fromIntegral $ rThreads rs)
[ IntM "stat.cputicks" (fromIntegral $ rCentiCpu rs)
, IntM "mem.resident" (fromIntegral $ rRSS rs)
, IntM "rts.gcLiveBytes" (fromIntegral $ rLive rs)
, IntM "rts.gcMajorNum" (fromIntegral $ rGcsMajor rs)
, IntM "rts.gcMinorNum" (fromIntegral $ rGcsMinor rs)
, IntM "rts.gcticks" (fromIntegral $ rCentiGC rs)
, IntM "rts.mutticks" (fromIntegral $ rCentiMut rs)
, IntM "rts.stat.threads" (fromIntegral $ rThreads rs)
]
12 changes: 6 additions & 6 deletions trace-resources/src/Cardano/Logging/Resources/Windows.hsc
Expand Up @@ -78,7 +78,7 @@ instance Storable ProcessMemoryCounters where
<*> (#peek PROCESS_MEMORY_COUNTERS, PeakPagefileUsage) ptr
poke _ _ = pure ()

foreign import ccall unsafe c_get_process_memory_info :: Ptr ProcessMemoryCounters -> CInt -> IO CInt
foreign import ccall unsafe c_get_process_memory_info2 :: Ptr ProcessMemoryCounters -> CInt -> IO CInt

{- I/O counters -}
{- https://docs.microsoft.com/de-de/windows/win32/api/winnt/ns-winnt-io_counters
Expand Down Expand Up @@ -127,14 +127,14 @@ instance Storable CpuTimes where
<*> (#peek CPU_TIMES, idletime) ptr
poke _ _ = pure ()

foreign import ccall unsafe c_get_proc_cpu_times :: Ptr CpuTimes -> CInt -> IO CInt
foreign import ccall unsafe c_get_proc_cpu_times2 :: Ptr CpuTimes -> CInt -> IO CInt


getMemoryInfo :: ProcessId -> IO ProcessMemoryCounters
getMemoryInfo pid =
allocaBytes 128 $ \ptr -> do
throwIfNeg_ (\res -> "c_get_process_memory_info: failure returned: " ++ show res)
(c_get_process_memory_info ptr (fromIntegral pid))
throwIfNeg_ (\res -> "c_get_process_memory_info2: failure returned: " ++ show res)
(c_get_process_memory_info2 ptr (fromIntegral pid))
peek ptr

readRessoureStatsInternal :: IO (Maybe ResourceStats)
Expand Down Expand Up @@ -166,10 +166,10 @@ readRessoureStatsInternal = getCurrentProcessId >>= \pid -> do
getCpuTimes :: ProcessId -> IO CpuTimes
getCpuTimes pid =
allocaBytes 128 $ \ptr -> do
res <- c_get_proc_cpu_times ptr (fromIntegral pid)
res <- c_get_proc_cpu_times2 ptr (fromIntegral pid)
if res <= 0
then do
putStrLn $ "c_get_proc_cpu_times: failure returned: " ++ (show res)
putStrLn $ "c_get_proc_cpu_times2: failure returned: " ++ (show res)
return $ CpuTimes 0 0 0
else
peek ptr

0 comments on commit ad3a62c

Please sign in to comment.