Skip to content

Commit

Permalink
Abstractize time getting
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jun 3, 2024
1 parent 64daf02 commit 304233c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
5 changes: 5 additions & 0 deletions include/ie_time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdint.h>

uint64_t microseconds_now();
16 changes: 16 additions & 0 deletions src/ie_time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "ie_time.h"

#ifdef __EMSCRIPTEN__
extern uint64_t microseconds_now();
#else
#include <stddef.h>
#include <sys/time.h>

_Thread_local struct timeval tv;

uint64_t microseconds_now()
{
gettimeofday(&tv, NULL);
return tv.tv_sec * 1e6 + tv.tv_usec;
}
#endif
11 changes: 6 additions & 5 deletions src/infiniemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "config.h"
#include "pinetime.h"
#include "gdb.h"
#include "ie_time.h"

int main(int argc, char **argv)
{
Expand Down Expand Up @@ -101,8 +102,8 @@ int main(int argc, char **argv)
else
{
#if ENABLE_MEASUREMENT
struct timeval tv_start, tv_now;
gettimeofday(&tv_start, NULL);
uint64_t start, now;
start = microseconds_now();

size_t inst_counter = 0;
#endif
Expand All @@ -114,11 +115,11 @@ int main(int argc, char **argv)
#ifdef ENABLE_MEASUREMENT
if (++inst_counter == 1000000)
{
gettimeofday(&tv_now, NULL);
now = microseconds_now();

long elapsed = (tv_now.tv_sec - tv_start.tv_sec) * 1000000 + (tv_now.tv_usec - tv_start.tv_usec);
uint64_t elapsed = now - start;

tv_start = tv_now;
start = now;

printf("Elapsed: %lu us\n", elapsed);
printf("Instructions ran: %lu\n", inst_counter);
Expand Down
9 changes: 3 additions & 6 deletions src/peripherals/nrf52832/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

#include "ie_time.h"
#include "peripherals/nrf52832/ppi.h"

#define TICK_INTERVAL 100
Expand Down Expand Up @@ -36,7 +36,6 @@ struct RTC_inst_t

size_t tick_interval_us;
size_t last_check_us;
struct timeval timeval;

inten_t inten, evten;
uint32_t prescaler, counter, prescaler_counter;
Expand All @@ -46,8 +45,7 @@ void rtc_tick(void *userdata)
{
RTC_t *rtc = userdata;

gettimeofday(&rtc->timeval, NULL);
size_t now = rtc->timeval.tv_sec * 1e6 + rtc->timeval.tv_usec;
size_t now = microseconds_now();

size_t elapsed = now - rtc->last_check_us;
size_t elapsed_ticks = elapsed / rtc->tick_interval_us;
Expand Down Expand Up @@ -158,8 +156,7 @@ PPI_TASK_HANDLER(rtc_task_handler)
{
ticker_add(rtc->ticker, rtc_tick, rtc, TICK_INTERVAL, true);

gettimeofday(&rtc->timeval, NULL);
rtc->last_check_us = rtc->timeval.tv_sec * 1e6 + rtc->timeval.tv_usec;
rtc->last_check_us = microseconds_now();

rtc->running = true;
}
Expand Down
9 changes: 4 additions & 5 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <unistd.h>
#include <sys/time.h>

#include "ie_time.h"

#define SCHEDULER_HZ 50

struct scheduler_t
Expand Down Expand Up @@ -35,7 +37,6 @@ scheduler_t *scheduler_new(scheduler_cb_t cb, void *userdata, size_t target_hz)

void scheduler_run(scheduler_t *sched)
{
struct timeval tv;
struct timespec ts_rem, ts_req = {0};

sched->stop = false;
Expand All @@ -46,17 +47,15 @@ void scheduler_run(scheduler_t *sched)
size_t iteration_count = sched->iteration_count;
size_t should_take_ns = sched->should_take_ns;

gettimeofday(&tv, NULL);
size_t start = tv.tv_sec * 1e6 + tv.tv_usec;
uint64_t start = microseconds_now();

for (size_t i = 0; i < iteration_count; i++)
{
sched->cb(sched->userdata);
}
sched->counter += iteration_count;

gettimeofday(&tv, NULL);
size_t end = tv.tv_sec * 1e6 + tv.tv_usec;
size_t end = microseconds_now();
size_t elapsed_ns = (end - start) * 1e3;

if (elapsed_ns < should_take_ns)
Expand Down

0 comments on commit 304233c

Please sign in to comment.