Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rachelbythebay
Created March 3, 2013 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelbythebay/5074299 to your computer and use it in GitHub Desktop.
Save rachelbythebay/5074299 to your computer and use it in GitHub Desktop.
Fix timing inaccuracies stemming from initial call to time() (no microseconds) and off-by-one time reporting error stemming from incorrect placement of subsequent calls to gettimeofday().
--- script.c.stock 2013-03-02 18:37:35.000000000 -0800
+++ script.c 2013-03-02 18:38:02.000000000 -0800
@@ -312,7 +312,7 @@
time_t tvec;
char obuf[BUFSIZ];
struct timeval tv;
- double oldtime=time(NULL), newtime;
+ double oldtime, newtime;
int flgs = 0;
ssize_t wrt;
size_t fwrt;
@@ -325,6 +325,11 @@
my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec));
fprintf(fscript, _("Script started on %s"), obuf);
+ if (tflg) {
+ gettimeofday(&tv, NULL);
+ oldtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
+ }
+
do {
if (die && flgs == 0) {
/* ..child is dead, but it doesn't mean that there is
@@ -334,8 +339,6 @@
if (fcntl(master, F_SETFL, (flgs | O_NONBLOCK)) == -1)
break;
}
- if (tflg)
- gettimeofday(&tv, NULL);
errno = 0;
cc = read(master, obuf, sizeof (obuf));
@@ -348,6 +351,7 @@
if (cc <= 0)
break;
if (tflg) {
+ gettimeofday(&tv, NULL);
newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
fprintf(stderr, "%f %zd\n", newtime - oldtime, cc);
oldtime = newtime;
--- scriptreplay.c.stock 2013-03-02 18:51:03.000000000 -0800
+++ scriptreplay.c 2013-03-02 18:51:19.000000000 -0800
@@ -121,7 +121,6 @@
double divi;
int c;
unsigned long line;
- size_t oldblk = 0;
/* Because we use space as a separator, we can't afford to use any
* locale which tolerates a space in a number. In any case, script.c
@@ -171,9 +170,7 @@
if (delay > SCRIPT_MIN_DELAY)
delay_for(delay);
- if (oldblk)
- emit(sfile, sname, oldblk);
- oldblk = blk;
+ emit(sfile, sname, blk);
}
fclose(sfile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment