Platform-specific code for macOS. Everything in this module, including the headers, is wrapped in the following preprocessor macro
#if defined(__APPLE__) || defined(__MACH__)
/*
... code ...
*/
#endif /* defined(__APPLE__) || defined(__MACH__) */
Structs
ProcessData
ProcessData is a struct that tracks cpu and memory usage of a process. It needs to be regularly updated with calls to update_process() in order to provide accurate information
typedef struct {
double total_user_time;
double total_kernel_time;
double last_total_consumed;
double percent_cpu;
uint64_t virtual_memory;
uint64_t resident_memory;
time_t timestamp;
time_t last_timestamp;
} ProcessData;
Functions
new_ProcessData
Returns a pointer to a zero-initialized ProcessData struct. Returns NULL on allocation failure.
ProcessData *new_ProcessData();
destroy_ProcessData
Frees a ProcessData struct. Safe to call with NULL.
void destroy_ProcessData(ProcessData *pd);
update_process
Updates a ProcessData struct. This should be called in a loop or at specific intervals to measure proper usage data. Returns 0 on success, -1 on error (e.g. invalid PID). An example is below
int update_process(pid_t pid, ProcessData *proc);
/* Example */
pid_t pid = getpid();
ProcessData *pd = new_ProcessData();
for (int i = 0; i < 10; i++) {
update_process(pid, pd);
printf("CPU: %.2f\n", pd->percent_cpu);
sleep(1);
}
destroy_ProcessData(pd);
reallocarray
Reallocates memory for an array of nmemb elements of size bytes each. Checks for multiplication overflow before calling realloc. Returns NULL on overflow or allocation failure. This is a compatibility shim for platforms that don't provide reallocarray natively.
void *reallocarray(void *optr, size_t nmemb, size_t size);