By the following code:
#include "../common/common.dep"
// I expect rte_rdtsc() may be used for accurate time calculations, but it seems that it has incremental differentiation.
int main(int argc, char* argv[]) {
struct timeval tv, tv0;
long tsc, tsc0;
int rc;
rc = rte_eal_init(argc, argv);
if (rc < 0) return rc;
const long hz = rte_get_tsc_hz();
gettimeofday(&tv0, NULL);
tsc0 = rte_rdtsc();
long us1, us2;
while (true) {
for (int i = 0; i < 1e8; i++) {
gettimeofday(&tv, NULL);
tsc = rte_rdtsc();
us1 = (tv.tv_sec - tv0.tv_sec) * 1e6 + tv.tv_usec - tv0.tv_usec;
us2 = (tsc - tsc0) / (hz / 1e6);
}
if (us1 != us2) printf("%ld != %ld diff: %ld\n", us1, us2, us1 - us2);
}
return 0;
}
I get such an output:
1608543 != 1604721 diff: 3822
3217990 != 3210345 diff: 7645
4826270 != 4814805 diff: 11465
6433788 != 6418505 diff: 15283
8041399 != 8022297 diff: 19102
9648509 != 9625590 diff: 22919
11258183 != 11231440 diff: 26743
12867572 != 12837006 diff: 30566
14476237 != 14441851 diff: 34386
16084626 != 16046420 diff: 38206
17691614 != 17649591 diff: 42023
19302386 != 19256537 diff: 45849
20909053 != 20859388 diff: 49665
So, either rte_rdtsc() is inaccurate or gettimeofday().
Any idea?