Debian Codegraph @

src/common/timestamp.cpp

raw
1 /*
2 * Component of the D-ITG 2.8.1 (r1023) platform (http://traffic.comics.unina.it/software/ITG)
3 *
4 * Copyright : (C) 2004-2013 by Alessio Botta, Walter de Donato, Alberto Dainotti,
5 * Stefano Avallone, Antonio Pescape' (PI)
6 * COMICS (COMputer for Interaction and CommunicationS) Group
7 * Department of Electrical Engineering and Information Technologies
8 * University of Napoli "Federico II".
9 * email : [email protected], [email protected], [email protected],
10 * [email protected], [email protected]
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * For commercial use please refer to D-ITG Professional.
26 */
27
28
29
30
31 #ifdef UNIX
32 #include <sys/wait.h>
33 #endif
34 #ifdef WIN32
35 #include <time.h>
36 #include <math.h>
37 #endif
38
39 #include "ITG.h"
40 #include "timestamp.h"
41 #include <cmath>
42
43 #ifdef WIN32
44
45 LARGE_INTEGER freq;
46 #endif
47
48
49
50 void setSeedRandom()
51 {
52 #ifdef UNIX
53
54 struct timeval tv;
55
56 gettimeofday(&tv, NULL);
57
58 srand(tv.tv_usec);
59 #endif
60 #ifdef WIN32
61
62 SYSTEMTIME lpSystemTime;
63
64 GetSystemTime(&lpSystemTime);
65
66 srand(lpSystemTime.wSecond);
67 #endif
68
69 }
70
71 #ifdef WIN32
72 void updateTicker(struct TTicker *Ticker, LARGE_INTEGER & _tend, LARGE_INTEGER & _tprec, LARGE_INTEGER & _tstart, int &first)
73 {
74 if (first) {
75 _tprec = _tstart;
76 first = 0;
77 }
78 QueryPerformanceCounter(&_tend);
79 Ticker->count += (((((double) _tend.QuadPart - (double) _tprec.QuadPart)) / (double) freq.QuadPart) * 1e3);
80 _tprec = _tend;
81 }
82
83
84 int tstart(LARGE_INTEGER & _tstart, unsigned long &secs, unsigned long &msecs, int &first, BYTE meter, int flag)
85 {
86
87 SYSTEMTIME lpSystemTime;
88
89 BOOL ret;
90
91 if (first) {
92
93
94 ret = QueryPerformanceFrequency(&freq);
95 if (ret == 0)
96 return -1;
97 first = 0;
98 }
99
100 GetSystemTime(&lpSystemTime);
101 ret = QueryPerformanceCounter(&_tstart);
102 if (ret == 0)
103 return -1;
104
105
106
107
108 secs = lpSystemTime.wHour * 3600 + lpSystemTime.wMinute * 60 + lpSystemTime.wSecond;
109
110 PRINTD(3,"tstart: GetSytemTime.second %lu \n",(unsigned long)secs);
111
112
113 msecs = lpSystemTime.wMilliseconds;
114
115 if ((meter == METER_OWDM) || (flag == RECEIVER)) {
116
117 msecs = lpSystemTime.wMilliseconds;
118 } else {
119
120 msecs = lpSystemTime.wMilliseconds * 1000;
121 }
122
123 return 0;
124 }
125
126 int gettimeofday(struct timeval *thisTime, LARGE_INTEGER & _tend, LARGE_INTEGER & _tstart,
127 unsigned long &secs, unsigned long &msecs, BYTE meter, int flag)
128 {
129
130 double secAndUsec = 0;
131
132 long micro = 0;
133
134 BOOL ret;
135
136 ret = QueryPerformanceCounter(&_tend);
137 if (ret == 0)
138 return -1;
139
140 secAndUsec = ((((double) _tend.QuadPart - (double) _tstart.QuadPart)) / (double) freq.QuadPart);
141 PRINTD(3,"gettimeofday: QueryPerformanceCounter:secAndUsec %lf \n", secAndUsec);
142
143 thisTime->tv_sec = ((unsigned long) secAndUsec) + secs;
144
145 if ((meter == METER_OWDM) || (flag == RECEIVER)) {
146
147 micro = (long) ceil(((secAndUsec - floor(secAndUsec)) * 1e6) / 1000);
148
149 if ((thisTime->tv_usec = (micro + msecs)) >= 1e3) {
150
151 thisTime->tv_sec++;
152
153 thisTime->tv_usec = (long int)(((double) (thisTime->tv_usec * 1e-3) - 1) * 1e3);
154 }
155
156 thisTime->tv_usec = (long int)(thisTime->tv_usec * 1e3);
157 } else {
158
159 micro = (long) ((secAndUsec - floor(secAndUsec)) * 1e6);
160
161 if ((thisTime->tv_usec = (micro + msecs)) >= 1e6) {
162
163 thisTime->tv_sec++;
164
165 thisTime->tv_usec = (long int)(((double) (thisTime->tv_usec * 1e-6) - 1) * 1e6);
166 }
167 }
168 return 0;
169 }
170 #endif
171
172 #ifdef UNIX
173 void updateTicker(struct TTicker *Ticker)
174 {
175 struct timeval thisTime, *lastTime = &Ticker->lastTime;
176
177 gettimeofday(&thisTime, NULL);
178 Ticker->count += ((double) (thisTime.tv_sec - lastTime->tv_sec)) * 1000.0 +
179 ((double) (thisTime.tv_usec - lastTime->tv_usec)) / 1000.0;
180 *lastTime = thisTime;
181
182 }
183 #endif