Equivalent to Gettickcount() on Linux

Equivalent to GetTickCount() on Linux

You can use CLOCK_MONOTONIC e.g. in C:

struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC,&ts) != 0) {
//error
}

See this question for a Python way - How do I get monotonic time durations in python?

GetTickCount equivalent in kernel module

There is the jiffies variable in the Linux kernel that holds the number of ticks since system start. Look for example here.

c++ Linux TickCount

clock_gettime with CLOCK_MONOTONIC is the magic incantation you seem to be looking for. Sample code, untested:

struct timespec *t;
t = (struct timespec *)malloc(sizeof(t));
clock_gettime(CLOCK_MONOTONIC, t);

Let me know how you get on. I'm not on Linux at the moment, hence this is just conjecture from the man page I pointed to.

Convert GetTickCount() vxWorks to Linux

GetTickCount is a windows API described thus:

Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days

Yes, CLOCK_MONOTONIC is the correct POSIX clock to use. Here is untested code for you:

double GetTickCount(void) 
{
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now))
return 0;
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}

Is there any C++ standard class/function which is similar to GetTickCount() on Windows?

You could build a custom chrono clock on top of Windows' GetTickCount(). Then use that clock. In porting, all you would have to do is port the clock. For example, I am not on Windows, but here is what such a port might look like:

#include <chrono>

// simulation of Windows GetTickCount()
unsigned long long
GetTickCount()
{
using namespace std::chrono;
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}

// Clock built upon Windows GetTickCount()
struct TickCountClock
{
typedef unsigned long long rep;
typedef std::milli period;
typedef std::chrono::duration<rep, period> duration;
typedef std::chrono::time_point<TickCountClock> time_point;
static const bool is_steady = true;

static time_point now() noexcept
{
return time_point(duration(GetTickCount()));
}
};

// Test TickCountClock

#include <thread>
#include <iostream>

int
main()
{
auto t0 = TickCountClock::now();
std::this_thread::sleep_until(t0 + std::chrono::seconds(1));
auto t1 = TickCountClock::now();
std::cout << (t1-t0).count() << "ms\n";
}

On my system, steady_clock happens to return nanoseconds since boot. You may find other non-portable ways of emulating GetTickCount() on other platforms. But once that detail is done, your clock is solid, and the clock's clients don't need to be any wiser about it.

For me this test reliably outputs:

1000ms

Where is GetTickCount declared in Firemonkey?

GetTickCount is a Windows function and so does not exist on other platforms. For your cross platform needs you should use TStopWatch from System.Diagnostics.

Getting the System tick count with basic C++?

On Android NDK you can use the POSIX clock_gettime() call, which is part of libc. This function is where various Android timer calls end up.

For example, java.lang.System.nanoTime() is implemented with:

struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (u8)now.tv_sec*1000000000LL + now.tv_nsec;

This example uses the monotonic clock, which is what you want when computing durations. Unlike the wall clock (available through gettimeofday()), it won't skip forward or backward when the device's clock is changed by the network provider.

The Linux man page for clock_gettime() describes the other clocks that may be available, such as the per-thread elapsed CPU time.

Time measure without using system clock

Use monotonic time, which represents time since some point: http://linux.die.net/man/3/clock_gettime

int64_t get_monotonic_timestamp(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}


Related Topics



Leave a reply



Submit