mercredi 8 janvier 2020

std::chrono behaves different in arm

So the following code I am using for a hacky production fix due to time constraints. Basically I have a static function that is called from many places, much more than intended and it is causing another section of the application to choke. So I thought I would come up with a quick fix to limit the calls to the overworked function to once every two seconds. This works just fine in x86 using clang or gcc.

#include <chrono>
#include <iostream>
#include <unistd.h>

static void staticfunction()
{
    static auto t0 = std::chrono::high_resolution_clock::now();
    auto t1 = std::chrono::high_resolution_clock::now();
    if( 2000 <= std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count() )
    {
        // Make a check in other section of application
        std::cout << "Check true after " << std::dec
                  << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count()
                  << " ms.\n";
        t0 = std::chrono::high_resolution_clock::now();
    }
}

int main() {
    while(true) {
        staticfunction();
    }
    return 0;
}

Prints Check true after 2000 ms. Check true after 2000 ms. Check true after 2002 ms. Check true after 2005 ms. ....

However, for our ARM controller I cross compiled using Linaro 7.1 and now the condition for the if stmt isn't satisfied until 10 seconds has passed. I was curious and compared against 1 second instead of two (duration_cast of ms vs seconds doesn't change anything) and if(1 <= ....count()) was true after half a second.

Is this a bug in the Linaro compiler? Or are clocks for our ARM controller off? Cross compile flags are -mcpu=cortex-a7 -mfloat-abi=hard -marm -march=armv7ve if that makes a difference

Aucun commentaire:

Enregistrer un commentaire