added preload_usb hack
[tridge/junkcode.git] / lag.c
1 /* use TCP acks to measure lag. Call this after sending critcal data
2    and it will return the number of milliseconds it took for all the
3    data to be acknowledged by the clients TCP stack 
4
5    this is very CPU intensive, for a real application you would need
6    to do this in a timer or at least yield between ioctl() calls
7
8    -1 is returned on error
9 */
10 int measure_lag(int fd)
11 {
12         struct timeval tv1, tv2;
13         int outq;
14
15 #ifndef TIOCOUTQ
16 #define TIOCOUTQ 0x5411
17 #endif
18
19         if (gettimeofday(&tv1, NULL) != 0) {
20                 return -1;
21         }
22
23         while (ioctl(fd, TIOCOUTQ, &outq) == 0 && outq != 0) ;
24
25         if (gettimeofday(&tv2, NULL) != 0) {
26                 return -1;
27         }
28         
29         return (tv2.tv_sec - tv1.tv_sec) * 1000 +
30                 (tv2.tv_usec - tv1.tv_usec) / 1000;
31 }
32