1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | #include "libtrace.h" |
---|
32 | #include "libtrace_int.h" |
---|
33 | #include "config.h" |
---|
34 | |
---|
35 | #include <stdlib.h> |
---|
36 | #include <stdio.h> |
---|
37 | #include <string.h> |
---|
38 | #ifdef HAVE_INTTYPES_H |
---|
39 | # include <inttypes.h> |
---|
40 | #else |
---|
41 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
42 | #endif |
---|
43 | #include "format_helper.h" |
---|
44 | |
---|
45 | #include <sys/ioctl.h> |
---|
46 | |
---|
47 | struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
48 | struct libtrace_eventobj_t event; |
---|
49 | int data; |
---|
50 | |
---|
51 | if (packet->trace->format->get_fd) { |
---|
52 | event.fd = packet->trace->format->get_fd(packet); |
---|
53 | } else { |
---|
54 | event.fd = 0; |
---|
55 | } |
---|
56 | if (ioctl(event.fd,FIONREAD,&data)==-1) { |
---|
57 | perror("ioctl(FIONREAD)"); |
---|
58 | } |
---|
59 | if (data>0) { |
---|
60 | event.size = trace_read_packet(trace,packet); |
---|
61 | event.type = TRACE_EVENT_PACKET; |
---|
62 | return event; |
---|
63 | } |
---|
64 | event.type= TRACE_EVENT_IOWAIT; |
---|
65 | return event; |
---|
66 | } |
---|
67 | |
---|
68 | struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
69 | struct libtrace_eventobj_t event; |
---|
70 | double ts; |
---|
71 | double now; |
---|
72 | struct timeval stv; |
---|
73 | |
---|
74 | if (!trace->event.packet.buffer) { |
---|
75 | trace->event.packet.buffer = (void *)malloc(4096); |
---|
76 | trace->event.packet.size= |
---|
77 | trace_read_packet(trace,packet); |
---|
78 | event.size = trace->event.packet.size; |
---|
79 | if (trace->event.packet.size > 0 ) { |
---|
80 | memcpy(trace->event.packet.buffer, |
---|
81 | packet->buffer, |
---|
82 | trace->event.packet.size); |
---|
83 | } else { |
---|
84 | // return here, the test for |
---|
85 | // event.size will sort out the error |
---|
86 | event.type = TRACE_EVENT_PACKET; |
---|
87 | return event; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | ts=trace_get_seconds(packet); |
---|
92 | if (trace->event.tdelta!=0) { |
---|
93 | // Get the adjusted current time |
---|
94 | gettimeofday(&stv, NULL); |
---|
95 | now = stv.tv_sec + |
---|
96 | ((double)stv.tv_usec / 1000000.0); |
---|
97 | // adjust for trace delta |
---|
98 | now -= trace->event.tdelta; |
---|
99 | |
---|
100 | //if the trace timestamp is still in the |
---|
101 | //future, return a SLEEP event, |
---|
102 | //otherwise fire the packet |
---|
103 | if (ts > now) { |
---|
104 | event.seconds = ts - |
---|
105 | trace->event.trace_last_ts; |
---|
106 | event.type = TRACE_EVENT_SLEEP; |
---|
107 | return event; |
---|
108 | } |
---|
109 | } else { |
---|
110 | gettimeofday(&stv, NULL); |
---|
111 | // work out the difference between the |
---|
112 | // start of trace replay, and the first |
---|
113 | // packet in the trace |
---|
114 | trace->event.tdelta = stv.tv_sec + |
---|
115 | ((double)stv.tv_usec / 1000000.0); |
---|
116 | trace->event.tdelta -= ts; |
---|
117 | } |
---|
118 | |
---|
119 | // This is the first packet, so just fire away. |
---|
120 | packet->size = trace->event.packet.size; |
---|
121 | memcpy(packet->buffer, |
---|
122 | trace->event.packet.buffer, |
---|
123 | trace->event.packet.size); |
---|
124 | free(trace->event.packet.buffer); |
---|
125 | trace->event.packet.buffer = 0; |
---|
126 | event.type = TRACE_EVENT_PACKET; |
---|
127 | |
---|
128 | trace->event.trace_last_ts = ts; |
---|
129 | |
---|
130 | return event; |
---|
131 | |
---|
132 | } |
---|