Skip to content

Instantly share code, notes, and snippets.

@machinaut
Created February 7, 2011 23:34
Show Gist options
  • Save machinaut/815525 to your computer and use it in GitHub Desktop.
Save machinaut/815525 to your computer and use it in GitHub Desktop.
Tests timers in Linux (trying to debug nasty problem in industrial line using linux pc's for control)
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
double timeval_subtract( struct timeval *endTime, struct timeval *beginTime)
{
double result;
result = (double) (endTime->tv_sec - beginTime->tv_sec)*1000000. + (endTime->tv_usec - beginTime->tv_usec);
return (result /= 1000000.);
}
int main()
{
static struct timeval tps;
static struct timeval tpe;
static struct timespec tp_delay;
int i, j;
double deltaTime;
int period = 8000; // time to sleep in usec;
int nAverage = 50;
int nSamples = 10;
char ans[80];
// Set up parameters for nanosleep
tp_delay.tv_sec = 0;
tp_delay.tv_nsec = 8000000;
printf("Sampling interval = %f sec\n\n", (nAverage * period) / 1000000.);
while(1) {
for (i = 0; i < nSamples; i++) {
gettimeofday(&tps, NULL);
for (j = 0; j < nAverage; j++) {
usleep(period);
}
gettimeofday(&tpe, NULL);
deltaTime = timeval_subtract(&tpe, &tps);
printf("Sample %d: Time between samples = %.3f seconds\n", i, deltaTime);
}
printf("Continue? ");
scanf("%s", ans);
if (ans[0] != 'y' && ans[0] != 'Y') break;
}
return 0;
}
/* compile with -lrt */
#include <stdio.h>
#include <time.h>
/* Subtract the `struct timespec' values X and Y,
storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0. */
int
timespec_subtract (result, x, y)
struct timespec *result, *x, *y;
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_nsec < y->tv_nsec) {
int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
y->tv_nsec -= 1000000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_nsec - y->tv_nsec > 1000000000) {
int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
y->tv_nsec += 1000000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_nsec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_nsec = x->tv_nsec - y->tv_nsec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
// WARNING: (XXX) assumes waits are less than one second
// this ignores the tv_sec part of the timespec struct
int main()
{
static struct timespec tpstart; //sample start
static struct timespec tpend; //sample end
static struct timespec res; //result
static struct timespec wait = {0,8000000-80000}; //result, adjusted for average overshoot
static int nSamples = 10;
static int nAverages= 50;
printf("Sampling interval = %ld nsec\n\n", (nAverages * wait.tv_nsec));
int i,j;
for (i = 0; i < nSamples; i++) {
clock_gettime(CLOCK_REALTIME,&tpstart);
for (j = 0; j < nAverages; j++) {
clock_nanosleep(CLOCK_REALTIME,0,&wait,NULL);
}
clock_gettime(CLOCK_REALTIME,&tpend);
timespec_subtract(&res,&tpend,&tpstart);
printf("Sample %2d: Time between samples = %10lu nanoseconds\n", i, res.tv_nsec);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment