1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

introduced two functions for timespec handling

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@87 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
Steffen Vogel 2014-06-25 17:50:27 +00:00
parent e6f1f580b1
commit 0c1e40dfff
3 changed files with 39 additions and 7 deletions

View file

@ -57,6 +57,12 @@ int resolve_addr(const char *addr, struct sockaddr_in *sa, int flags);
*/
cpu_set_t to_cpu_set(int set);
/** Get delta between two timespec structs */
double timespec_delta(struct timespec *start, struct timespec *end);
/** Get period as timespec from rate */
struct timespec timespec_rate(double rate);
/** Append an element to a single linked list */
#define list_add(list, elm) do { \
elm->next = list; \

View file

@ -71,8 +71,8 @@ int main(int argc, char *argv[])
struct timespec *ts1 = (struct timespec *) &m.data;
struct timespec *ts2 = malloc(sizeof(struct timespec));
long long rtt, rtt_max = LLONG_MIN, rtt_min = LLONG_MAX;
long long run = 0, avg = 0;
double rtt, rtt_max = LLONG_MIN, rtt_min = LLONG_MAX, avg = 0;
int run = 0;
while (1) {
clock_gettime(CLOCK_REALTIME, ts1);
@ -80,20 +80,21 @@ int main(int argc, char *argv[])
msg_recv(&m, &n);
clock_gettime(CLOCK_REALTIME, ts2);
rtt = ts2->tv_nsec - ts1->tv_nsec;
rtt = timespec_delta(ts1, ts2);
if (rtt < 0) continue;
if (rtt > rtt_max) rtt_max = rtt;
if (rtt < rtt_min) rtt_min = rtt;
avg += rtt;
info("rtt %.3f min %.3f max %.3f avg %.3f uS", 1e-3 * rtt, 1e-3 * rtt_min, 1e-3 * rtt_max, 1e-3 * avg / run);
run++;
info("rtt %.3f min %.3f max %.3f avg %.3f", 1e3 * rtt, 1e3 * rtt_min, 1e3 * rtt_max, 1e3 * avg / run);
m.sequence++;
usleep(1000);
}
free(ts2);
}
return 0;

View file

@ -12,6 +12,8 @@
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
#include <math.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@ -94,3 +96,26 @@ cpu_set_t to_cpu_set(int set)
return cset;
}
double timespec_delta(struct timespec *start, struct timespec *end)
{
double sec = end->tv_sec - start->tv_sec;
double nsec = end->tv_nsec - start->tv_nsec;
if (nsec < 0) {
sec -= 1;
nsec += 1e9;
}
return sec + nsec * 1e-9;
}
struct timespec timespec_rate(double rate)
{
struct timespec ts;
ts.tv_sec = 1 / rate;
ts.tv_nsec = 1.0e9 * (1 / rate - ts.tv_sec);
return ts;
}