Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong timeouts with Raspberry Pi port (ESF-5) #27

Open
tim-nordell-nimbelink opened this issue Jun 1, 2021 · 1 comment
Open

Wrong timeouts with Raspberry Pi port (ESF-5) #27

tim-nordell-nimbelink opened this issue Jun 1, 2021 · 1 comment

Comments

@tim-nordell-nimbelink
Copy link

tim-nordell-nimbelink commented Jun 1, 2021

These lines of code:

void loader_port_start_timer(uint32_t ms)
{
s_time_end = clock() + (ms * (CLOCKS_PER_SEC / 1000));
}
uint32_t loader_port_remaining_time(void)
{
int64_t remaining = (s_time_end - clock()) / 1000;
return (remaining > 0) ? (uint32_t)remaining : 0;
}

are incorrect. The clock() routine returns how much processor time is used by the application, not how much processor time has passed. An example of checking this is doing (by using a system call to sleep, plus spinning for some "user" time measurement, but measuring across the span of both with clock()):

#include <time.h>
#include <unistd.h>
#include <stdio.h>

double to_seconds(struct timespec *spec)
{
    return spec->tv_sec + (double)spec->tv_nsec / 1e9;
}

int main(int argc, char **argv)
{
    int i;
    clock_t clk_start, clk_end;
    struct timespec mono_start, mono_end;

    clk_start = clock();
    clock_gettime(CLOCK_MONOTONIC, &mono_start);
    for(i=0;i<100000000;++i);
    usleep(1000 * 1000 * 1);
    clk_end = clock();
    clock_gettime(CLOCK_MONOTONIC, &mono_end);

    printf("clock() duration: %f\n", (float)(clk_end - clk_start) / CLOCKS_PER_SEC);
    printf("clock_gettime() duration: %f\n", to_seconds(&mono_end) - to_seconds(&mono_start));

    return 0;
}

which outputs:

$ time ./test
clock() duration: 0.167709
clock_gettime() duration: 1.167781

real    0m1.169s
user    0m0.169s
sys     0m0.000s

You can see that clock() isn't suitable for measuring how much real time has elapsed (only "user" time). It'd be better to utilize the POSIX API clock_gettime(CLOCK_MONOTONIC, xxx) instead.

@dobairoland dobairoland changed the title Wrong timeouts with Raspberry Pi port Wrong timeouts with Raspberry Pi port Dec 7, 2022
@github-actions github-actions bot changed the title Wrong timeouts with Raspberry Pi port Wrong timeouts with Raspberry Pi port (ESF-5) Dec 7, 2022
@DNedic
Copy link
Collaborator

DNedic commented Apr 25, 2023

Thank you for the submission and sorry for the big delay in the reply, we've only recently added Github issue tracking for this project.

This does seem to be correct, I will investigate and test the changes suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants