1
0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-09-26 05:30:46 +02:00

Rewrite example code with tsc timers

This commit is contained in:
Pavel Odintsov 2015-08-10 17:10:39 +02:00
parent 27a396fbc5
commit d6044d92aa

View File

@ -1,15 +1,11 @@
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
/* The frequency of the RDTSC timer resolution */
static uint64_t eal_tsc_resolution_hz = 0;
inline uint64_t rte_rdtsc(void) {
inline uint64_t read_tsc_cpu_register(void) {
union {
uint64_t tsc_64;
struct {
@ -24,18 +20,16 @@ inline uint64_t rte_rdtsc(void) {
return tsc.tsc_64;
}
uint64_t rte_get_tsc_hz(void) {
return eal_tsc_resolution_hz;
}
void set_tsc_freq_fallback() {
uint64_t start = rte_rdtsc();
uint64_t get_tsc_freq_with_sleep() {
uint64_t start = read_tsc_cpu_register();
sleep(1);
eal_tsc_resolution_hz = rte_rdtsc() - start;
return read_tsc_cpu_register() - start;
}
int set_tsc_freq_from_clock(void) {
#ifdef CLOCK_MONOTONIC_RAW
uint64_t get_tsc_freq_from_clock(void) {
//#ifdef CLOCK_MONOTONIC_RAW
#define NS_PER_SEC 1E9
struct timespec sleeptime;
@ -47,30 +41,31 @@ int set_tsc_freq_from_clock(void) {
if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
uint64_t ns, end, start;
start = rte_rdtsc();
start = read_tsc_cpu_register();
nanosleep(&sleeptime,NULL);
clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
end = rte_rdtsc();
end = read_tsc_cpu_register();
ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
ns += (t_end.tv_nsec - t_start.tv_nsec);
double secs = (double)ns/NS_PER_SEC;
eal_tsc_resolution_hz = (uint64_t)((end - start)/secs);
return 0;
return (uint64_t)((end - start)/secs);
}
#endif
return -1;
//#endif
}
int main() {
/* The frequency of the RDTSC timer resolution */
uint64_t fastnetmon_tsc_resolution_hz = 0;
printf("Determine TSC freq with sleep\n");
set_tsc_freq_fallback();
printf("TSC freq is %llu\n", eal_tsc_resolution_hz);
fastnetmon_tsc_resolution_hz = get_tsc_freq_with_sleep();
printf("TSC freq is %llu\n", fastnetmon_tsc_resolution_hz);
printf("Determing TSC freq with CLOCK_MONOTONIC_RAW\n");
set_tsc_freq_from_clock();
printf("TSC freq is %llu\n", eal_tsc_resolution_hz);
fastnetmon_tsc_resolution_hz = get_tsc_freq_from_clock();
printf("TSC freq is %llu\n", fastnetmon_tsc_resolution_hz);
printf("Current TSC value %llu\n", rte_rdtsc());
printf("Current TSC value %llu\n", read_tsc_cpu_register());
}