Skip to content

Commit

Permalink
Move Mach CPU usage functions to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandesign committed Mar 11, 2021
1 parent 20f8fa3 commit 00594d6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 67 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ endif()

set(SOURCE_FILES hwmond.c cpu_usage.c)

INCLUDE(CheckIncludeFiles)
CHECK_INCLUDE_FILES(mach/mach_host.h HAVE_MACH_MACH_HOST_H)

if(HAVE_MACH_MACH_HOST_H)
list(APPEND SOURCE_FILES cpu_usage_mach.c)
else()
message(FATAL_ERROR "Don't know how to get CPU usage on this system")
endif()

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_SOURCE_DIR}/modules")

find_package(LibUSB REQUIRED)
Expand Down
67 changes: 0 additions & 67 deletions cpu_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,13 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mach/mach_host.h>
#include <mach/vm_map.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>

#define CPU_UPDATE_INTERVAL ((useconds_t)(1e6 / 5))

static int get_num_packages(void) {
int num_packages;
size_t sizeof_num_packages = sizeof(num_packages);

if (sysctlbyname("hw.packages", &num_packages, &sizeof_num_packages, NULL, 0) != 0) {
printf("ERROR fetching hw.packages: %s", strerror(errno));
return -1;
}

if (num_packages <= 0) {
printf("WARNING: hw.packages gave invalid number %d, using 1", num_packages);
num_packages = 1;
}

return num_packages;
}

int fetch_ticks_by_core(unsigned int **ticks_by_core_busy_ptr, unsigned int **ticks_by_core_total_ptr, unsigned int *num_cores_ptr) {
// Fetch load info
natural_t num_cores;
processor_cpu_load_info_data_t* load_info;
mach_msg_type_number_t load_info_len;

kern_return_t err = host_processor_info(mach_host_self(),
PROCESSOR_CPU_LOAD_INFO,
&num_cores,
(processor_info_array_t *)&load_info,
&load_info_len);
if (err != KERN_SUCCESS) {
printf("ERROR fetching CPU load info: %d\n", err);
return -1;
}

// Aggregate per-core ticks into busy & total counts
unsigned int *ticks_by_core_busy = calloc(num_cores, sizeof(unsigned int));
unsigned int *ticks_by_core_total = calloc(num_cores, sizeof(unsigned int));

if (ticks_by_core_busy == NULL || ticks_by_core_total == NULL) {
printf("Failed to alloc\n");
return -1;
}

for (size_t core = 0; core < num_cores; core++) {
unsigned int *ticks_by_state = load_info[core].cpu_ticks;
for (int state = 0; state < CPU_STATE_MAX; state++) {
ticks_by_core_total[core] += ticks_by_state[state];
if (state != CPU_STATE_IDLE) {
ticks_by_core_busy[core] += ticks_by_state[state];
}

}
}

// Deallocate memory from the host_processor_info call
vm_deallocate(mach_task_self(), (vm_address_t)load_info, load_info_len);

// Output aggregated data
*ticks_by_core_busy_ptr = ticks_by_core_busy;
*ticks_by_core_total_ptr = ticks_by_core_total;
*num_cores_ptr = num_cores;

return 0;
}

static int num_packages;
static unsigned int *prev_ticks_by_core_busy = NULL;
static unsigned int *prev_ticks_by_core_total = NULL;
Expand Down
2 changes: 2 additions & 0 deletions cpu_usage.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

int get_num_packages(void);
int fetch_ticks_by_core(unsigned int **ticks_by_core_busy_ptr, unsigned int **ticks_by_core_total_ptr, unsigned int *num_cores_ptr);

int cpu_usage_setup(void);
void cpu_update_usage_loop(volatile float *usage_breakdowns, unsigned int num_breakdowns);
Expand Down
73 changes: 73 additions & 0 deletions cpu_usage_mach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <errno.h>
#include <mach/mach_host.h>
#include <mach/vm_map.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <unistd.h>

int get_num_packages(void) {
int num_packages;
size_t sizeof_num_packages = sizeof(num_packages);

if (sysctlbyname("hw.packages", &num_packages, &sizeof_num_packages, NULL, 0) != 0) {
printf("ERROR fetching hw.packages: %s", strerror(errno));
return -1;
}

if (num_packages <= 0) {
printf("WARNING: hw.packages gave invalid number %d, using 1", num_packages);
num_packages = 1;
}

return num_packages;
}

int fetch_ticks_by_core(unsigned int **ticks_by_core_busy_ptr, unsigned int **ticks_by_core_total_ptr, unsigned int *num_cores_ptr) {
// Fetch load info
natural_t num_cores;
processor_cpu_load_info_data_t* load_info;
mach_msg_type_number_t load_info_len;

kern_return_t err = host_processor_info(mach_host_self(),
PROCESSOR_CPU_LOAD_INFO,
&num_cores,
(processor_info_array_t *)&load_info,
&load_info_len);
if (err != KERN_SUCCESS) {
printf("ERROR fetching CPU load info: %d\n", err);
return -1;
}

// Aggregate per-core ticks into busy & total counts
unsigned int *ticks_by_core_busy = calloc(num_cores, sizeof(unsigned int));
unsigned int *ticks_by_core_total = calloc(num_cores, sizeof(unsigned int));

if (ticks_by_core_busy == NULL || ticks_by_core_total == NULL) {
printf("Failed to alloc\n");
return -1;
}

for (size_t core = 0; core < num_cores; core++) {
unsigned int *ticks_by_state = load_info[core].cpu_ticks;
for (int state = 0; state < CPU_STATE_MAX; state++) {
ticks_by_core_total[core] += ticks_by_state[state];
if (state != CPU_STATE_IDLE) {
ticks_by_core_busy[core] += ticks_by_state[state];
}

}
}

// Deallocate memory from the host_processor_info call
vm_deallocate(mach_task_self(), (vm_address_t)load_info, load_info_len);

// Output aggregated data
*ticks_by_core_busy_ptr = ticks_by_core_busy;
*ticks_by_core_total_ptr = ticks_by_core_total;
*num_cores_ptr = num_cores;

return 0;
}

0 comments on commit 00594d6

Please sign in to comment.