Skip to content

Commit

Permalink
add cpu_time (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
catfact authored Oct 21, 2023
1 parent 00056f1 commit 8cd7aa2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
29 changes: 29 additions & 0 deletions matron/src/hardware/cpu_time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "cpu_time.h"

#include <time.h>

struct timespec t0;

static struct timespec diff(const struct timespec *start, const struct timespec *end)
{
struct timespec temp;
if ((end->tv_nsec - start->tv_nsec)<0) {
temp.tv_sec = end->tv_sec - start->tv_sec - 1;
temp.tv_nsec = 1000000000 + end->tv_nsec - start->tv_nsec;
} else {
temp.tv_sec = end->tv_sec - start->tv_sec;
temp.tv_nsec = end->tv_nsec - start->tv_nsec;
}
return temp;
}

void cpu_time_start() {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);
}

unsigned long int cpu_time_get_delta_ns() {
struct timespec t1;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
struct timespec delta = diff(&t0, &t1);
return (delta.tv_sec * 1000000000) + delta.tv_nsec;
}
8 changes: 8 additions & 0 deletions matron/src/hardware/cpu_time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// extremely simple CPU time measurement module, for benchmarking
// sets a single reference point, and computes time deltas since that point

#pragma once

extern void cpu_time_start();

extern unsigned long int cpu_time_get_delta_ns();
11 changes: 0 additions & 11 deletions matron/src/system_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,8 @@ void *run_cmd(void *cmd) {
strncat(capture, line, capacity);
}
capacity -= len;

#if 1 // test..
fprintf(stderr, "last line: \n\t%s\n", line);
// fprintf(stderr, "current buffer: \n\t %s\n", line);
fprintf(stderr, "remaining capacity: %d bytes\n", capacity);
#endif

} while (capacity > 0); // ... or, stop if buffer is full

#if 0 // test...
fprintf(stderr, "finished line loop; full buffer: \n");
fprintf(stderr, "%s\n", capture);
#endif

// just use memcpy and include the null terminator
size_t len = strlen(capture) + 1;
Expand Down
18 changes: 18 additions & 0 deletions matron/src/weaver.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "clocks/clock_internal.h"
#include "clocks/clock_link.h"
#include "clocks/clock_scheduler.h"
#include "cpu_time.h"
#include "device_crow.h"
#include "device_hid.h"
#include "device_midi.h"
Expand Down Expand Up @@ -288,6 +289,10 @@ static int _clock_get_tempo(lua_State *l);
static int _audio_get_cpu_load(lua_State *l);
static int _audio_get_xrun_count(lua_State *l);

// CPU time
static int _cpu_time_start_timer(lua_State *l);
static int _cpu_time_get_delta(lua_State *l);

// platform detection (CM3 vs PI3 vs OTHER)
static int _platform(lua_State *l);

Expand Down Expand Up @@ -548,6 +553,9 @@ void w_init(void) {
lua_register_norns("audio_get_cpu_load", &_audio_get_cpu_load);
lua_register_norns("audio_get_xrun_count", &_audio_get_xrun_count);

lua_register_norns("cpu_time_start_timer", &_cpu_time_start_timer);
lua_register_norns("cpu_time_get_delta", &_cpu_time_get_delta);

// platform
lua_register_norns("platform", &_platform);

Expand Down Expand Up @@ -1973,6 +1981,16 @@ int _audio_get_xrun_count(lua_State *l) {
return 1;
}

int _cpu_time_start_timer(lua_State *l) {
cpu_time_start();
return 0;
}

int _cpu_time_get_delta(lua_State *l) {
lua_pushnumber(l, cpu_time_get_delta_ns());
return 1;
}

//--------------------------------------------------
//--- define lua handlers for system callbacks

Expand Down
1 change: 1 addition & 0 deletions matron/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def build(bld):
'src/device/device_crow.c',
'src/osc.c',
'src/hardware/battery.c',
'src/hardware/cpu_time.c',
'src/hardware/i2c.c',
'src/hardware/input.c',
'src/hardware/io.c',
Expand Down

0 comments on commit 8cd7aa2

Please sign in to comment.