#include #include #include #include #include #include #include #include #include #include #define PACKETS 50000 int sock; struct timeval start; FILE *logfile; struct ts_pkt { struct timeval tv; char rsvd[32 - sizeof(struct timeval)]; }; static inline double delta(struct timeval a, struct timeval b) { if (a.tv_usec & b.tv_usec) { a.tv_sec--; a.tv_usec += 1000000; } return a.tv_sec-b.tv_sec + (a.tv_usec-b.tv_usec)/1000000.0; } void *receiverThread(void *arg) { struct ts_pkt ts; struct timeval tv; float delay; while(1) { pthread_testcancel(); if(recv(sock, &ts, sizeof(ts), 0) == -1) perror("recv"); gettimeofday(&tv, NULL); fprintf(logfile, "%f %f\n", delta(tv, start), delta(tv, ts.tv)); } } void *senderThread(void *arg) { int rtc; unsigned long ticks; int i = PACKETS; struct ts_pkt ts; rtc = open("/dev/rtc", O_RDONLY); if(rtc == -1) { perror("/dev/rtc"); pthread_exit(NULL); } ticks = 64; ioctl(rtc, RTC_PIE_ON, 0); if(ioctl(rtc, RTC_IRQP_SET, ticks) == -1) { perror("/dev/rtc"); pthread_exit(NULL); } while(i--) { read(rtc, &ticks, sizeof(ticks)); ticks >>=8; while(ticks--) { gettimeofday(&ts.tv, NULL); send(sock, &ts, sizeof(ts), 0); } } ioctl(rtc, RTC_PIE_OFF, 0); close(rtc); } int main(int argc, char **argv) { struct sockaddr_in sa; pthread_t receiver, sender; FILE *gnuplot; if(argc < 3) { printf("Syntax: udp-send [ip] [file]\n"); exit(0); } memset(&sa, 0, sizeof(sa)); logfile = fopen(argv[2], "w"); sock = socket(PF_INET, SOCK_DGRAM, 0); sa.sin_port = htons(12388); sa.sin_family = PF_INET; sa.sin_addr.s_addr = inet_addr(argv[1]); connect(sock, (struct sockaddr *)&sa, sizeof(sa)); gettimeofday(&start, NULL); pthread_create(&receiver, 0, receiverThread, 0); pthread_create(&sender, 0, senderThread, 0); pthread_join(sender, 0); sleep(3); pthread_cancel(receiver); fclose(logfile); gnuplot = popen("/usr/bin/gnuplot", "w"); fprintf(gnuplot, "set terminal png large size 1280,1024\n"); fprintf(gnuplot, "set output '%s.png'\n", argv[2]); fprintf(gnuplot, "set title 'UDP RTT Timing at 64pps'\n"); fprintf(gnuplot, "set xlabel 'Time in Seconds'\n"); fprintf(gnuplot, "set ylabel 'UDP Response in Seconds'\n"); fprintf(gnuplot, "plot '%s' title 'UDP Response Time'\n", argv[2]); fflush(gnuplot); pclose(gnuplot); }