void prq_enqueue (char* buf, int blockNo) { spinlock_lock (&prq_mutex); struct PendingRead* pr; if (prq_free != NULL) { pr = prq_free; prq_free = prq_free->next; } else pr = malloc (sizeof (struct PendingRead)); pr->buf = buf; pr->blockNo = blockNo; gettimeofday (& pr ->completeTime, NULL); tm_add (& pr ->completeTime, READ_LATENCY_USEC); pr->prev = NULL; pr->next = prq_back; if (prq_back == NULL) prq_back = (prq_front = pr); else { prq_back->prev = pr; prq_back = pr; } spinlock_unlock (&prq_mutex); }
int main(int argc, char *argv[]) { int rv; struct pktbuf *p; cat_time_t pts, now, next, nnext, start_time, start_ts = { 0 }; struct xpkt_tag_ts *ts; int infd = 0; int outfd = 1; ulong bits; pkb_init_pools(1); parse_args(argc, argv, &infd, &outfd); now = tm_uget(); if (g_start_delay > 0) { next = tm_add(now, tm_dset(g_start_delay)); sleep_until(&next, &now); } start_time = now; next = now; while ((rv = pkb_fd_read_a(&p, infd, NULL, NULL)) > 0) { now = tm_uget(); ++g_npkts; if (g_interval > 0.0) { nnext = tm_add(next, tm_dset(g_interval)); sleep_until(&next, &now); next = nnext; } else if (g_bps > 1.0) { bits = pkb_get_len(p) * 8; nnext = tm_add(next, tm_dset(bits / g_bps)); sleep_until(&next, &now); next = nnext; } else { ts = (struct xpkt_tag_ts *) pkb_find_tag(p, XPKT_TAG_TIMESTAMP, 0); if ( ts == NULL ) { fprintf(stderr, "no timestamp on packet %lu: sending\n", g_npkts); goto send; } pts = tm_lset(ts->sec, ts->nsec); if (tm_ltz(pts)) { fprintf(stderr, "Invalid timestamp on packet %lu " "(%ld,%ld)\n", g_npkts, tm_sec(pts), tm_nsec(pts)); pkb_free(p); continue; } if (g_npkts == 1) start_ts = pts; next = tm_add(tm_sub(pts, start_ts), start_time); sleep_until(&next, &now); } send: rv = pkb_pack(p); abort_unless(rv == 0); if (pkb_fd_write(p, outfd) < 0) errsys("Error writing packet %lu", g_npkts); pkb_free(p); } if (rv < 0) errsys("Error reading packet %lu: ", g_npkts + 1); return 0; }