Linux C ++ userpace app real time sleep function (POSIX, Raspberry Pi)

I need a function that pauses my program in μs, it should be in real time, so if I call it 50 μs, the flow should stop at exactly 50 μs. My C ++ program runs on a Raspberry Pi with regular Raspbian installed.

I wrote this example program which uses posix time functions to pause the pause time measurement.

#include <cstdlib>
#include "stdint-gcc.h"
#include "signal.h"
#include <time.h>
#include <cerrno>
#include <cstdio>
#include <iostream>
#include <cstring>

#define S_IN_NS 1000000000UL
#define MS_IN_NS 1000000UL
#define US_IN_NS 1000UL
#define GET_TIME_CLOCK CLOCK_MONOTONIC

using namespace std;

int main(int argc, char** argv) {
    struct timespec newTimeStamp;
    struct timespec oldTimeStamp;
    struct timespec sleeptime;
    sleeptime.tv_sec = 0;
    sleeptime.tv_nsec = 50000; //50us

    if (clock_gettime(GET_TIME_CLOCK, &oldTimeStamp) == -1)
        cout << "Could not get clock time! ERRNO: " << strerror(errno);

    if ((clock_nanosleep(CLOCK_MONOTONIC, 0, &sleeptime, NULL)) == -1)
        cout << "Sleep failed! ERRNO: " << strerror(errno);


    if (clock_gettime(GET_TIME_CLOCK, &newTimeStamp) == -1)
        cout << "Could not get clock time! ERRNO: " << strerror(errno);

    uint64_t measuredSec = (newTimeStamp.tv_sec - oldTimeStamp.tv_sec);
    int32_t measuredNs = (newTimeStamp.tv_nsec - oldTimeStamp.tv_nsec);

    uint64_t diffus = (((measuredSec * S_IN_NS) + measuredNs + 500) / 1000UL);
    uint64_t diffns = (((measuredSec * S_IN_NS) + measuredNs));

    cout << "Diffns:" << diffns << " Diffus:" << diffus << endl;
    return 0;
}

      

Build commands:

arm-bcm2708hardfp-linux-gnueabi-g++ -lrt   -c -g -MMD -MP -MF "build/Debug/GNU_ARM_HARDFP-Linux-x86/main.o.d" -o build/Debug/GNU_ARM_HARDFP-Linux-x86/main.o main.cpp

arm-bcm2708hardfp-linux-gnueabi-g++ -lrt    -o dist/Debug/GNU_ARM_HARDFP-Linux-x86/timetest build/Debug/GNU_ARM_HARDFP-Linux-x86/main.o

      

Result (chrt - manipulating process real-time attributes):

pi@raspberrypi ~ $ sudo chrt 99 ./timetest 
Diffns:130994 Diffus:131
pi@raspberrypi ~ $ sudo chrt 99 ./timetest 
Diffns:135994 Diffus:136
pi@raspberrypi ~ $ sudo chrt 99 ./timetest 
Diffns:138993 Diffus:139

      

The program should sleep exactly 50us, but I measured 130-139us. If I change the definition of GET_TIME_CLOCK to CLOCK_PROCESS_CPUTIME_ID, the CPU time is measured (excluding grease) (as I understand it).

Result:

pi@raspberrypi ~ $ sudo chrt 99 ./timetest 
Diffns:89000 Diffus:89
pi@raspberrypi ~ $ sudo chrt 99 ./timetest 
Diffns:86000 Diffus:86
pi@raspberrypi ~ $ sudo chrt 99 ./timetest 
Diffns:88000 Diffus:88

      

It takes about 80-90 μs to execute clock_nanosleep () even if I change the lubricant to 500 μs!

So, is there a way to pause a thread for a certain amount of time (μs) in a C ++ user-space application on Raspbian?

thank

+3


source to share


1 answer


If you need to sleep for that long, you will probably need to use a spin loop to check the current time. This will consume quite a bit of power (and generate heat), but it's a pretty reliable and portable way to do it. Another idea is to try the ideas on this page: http://blog.regehr.org/archives/794



0


source







All Articles