Mixe for Privacy and Anonymity in the Internet
CALogPacketStats.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 
29 #include "StdAfx.h"
30 #include "CALogPacketStats.hpp"
31 #include "CAUtil.hpp"
32 #include "CAMuxSocket.hpp"
33 #include "CAMsg.hpp"
34 
35 #ifdef LOG_PACKET_TIMES
40 THREAD_RETURN loopLogPacketStats(void* param)
41  {
42  CALogPacketStats* pLog=(CALogPacketStats*)param;
43  UINT32 countLog=0;
44  while(pLog->m_bRunLog)
45  {
46  if(countLog==0)
47  {
48  pLog->logTimeingStats();
49  pLog->resetTimeingStats();
50  countLog=pLog->m_iLogIntervallInHalfMinutes;
51  }
52  sSleep(30);
53  countLog--;
54  }
56  }
57 
58 SINT32 CALogPacketStats::start()
59  {
60  resetTimeingStats();
61  m_pthreadLog=new CAThread((UINT8*)"Packet Stats Logging Thread");
62  m_pthreadLog->setMainLoop(loopLogPacketStats);
63  m_bRunLog=true;
64  return m_pthreadLog->start(this);
65  }
66 
67 SINT32 CALogPacketStats::stop()
68  {
69  m_bRunLog=false;
70  if(m_pthreadLog!=NULL)
71  {
72  m_pthreadLog->join();
73  delete m_pthreadLog;
74  m_pthreadLog=NULL;
75  }
76 
77  return E_SUCCESS;
78  }
79 
80 SINT32 CALogPacketStats::addToTimeingStats(const tQueueEntry& oQueueEntry,UINT32 uType,bool bUpstream)
81 {
82  m_csTimeingStats.lock();
83  UINT32 proccessingTime=diff64(oQueueEntry.timestamp_proccessing_end,oQueueEntry.timestamp_proccessing_start);
84  UINT32 proccessingTimeOP=diff64(oQueueEntry.timestamp_proccessing_end_OP,oQueueEntry.timestamp_proccessing_start_OP);
85  if(bUpstream)
86  {
87  if(uType==CHANNEL_DATA)
88  {
89  m_timingCountDataPacketsUpstream++;
90  add64(m_timingSumDataPacketUpstream,proccessingTime);
91  if(proccessingTime>m_timingMaxDataPacketUpstream)
92  m_timingMaxDataPacketUpstream=proccessingTime;
93  else if(m_timingMinDataPacketUpstream>proccessingTime)
94  m_timingMinDataPacketUpstream=proccessingTime;
95  add64(m_timingSumDataPacketUpstreamOP,proccessingTimeOP);
96  if(proccessingTimeOP>m_timingMaxDataPacketUpstreamOP)
97  m_timingMaxDataPacketUpstreamOP=proccessingTimeOP;
98  else if(m_timingMinDataPacketUpstreamOP>proccessingTimeOP)
99  m_timingMinDataPacketUpstreamOP=proccessingTimeOP;
100  }
101  else if(uType==CHANNEL_CLOSE)
102  {
103  m_timingCountClosePacketsUpstream++;
104  add64(m_timingSumClosePacketUpstream,proccessingTime);
105  if(proccessingTime>m_timingMaxClosePacketUpstream)
106  m_timingMaxClosePacketUpstream=proccessingTime;
107  else if(m_timingMinClosePacketUpstream>proccessingTime)
108  m_timingMinClosePacketUpstream=proccessingTime;
109  add64(m_timingSumClosePacketUpstreamOP,proccessingTimeOP);
110  if(proccessingTimeOP>m_timingMaxClosePacketUpstreamOP)
111  m_timingMaxClosePacketUpstreamOP=proccessingTimeOP;
112  else if(m_timingMinClosePacketUpstreamOP>proccessingTimeOP)
113  m_timingMinClosePacketUpstreamOP=proccessingTimeOP;
114  }
115  else
116  {//open
117  m_timingCountOpenPacketsUpstream++;
118  add64(m_timingSumOpenPacketUpstream,proccessingTime);
119  if(proccessingTime>m_timingMaxOpenPacketUpstream)
120  m_timingMaxOpenPacketUpstream=proccessingTime;
121  else if(m_timingMinOpenPacketUpstream>proccessingTime)
122  m_timingMinOpenPacketUpstream=proccessingTime;
123  add64(m_timingSumOpenPacketUpstreamOP,proccessingTimeOP);
124  if(proccessingTimeOP>m_timingMaxOpenPacketUpstreamOP)
125  m_timingMaxOpenPacketUpstreamOP=proccessingTimeOP;
126  else if(m_timingMinOpenPacketUpstreamOP>proccessingTimeOP)
127  m_timingMinOpenPacketUpstreamOP=proccessingTimeOP;
128  }
129  #ifdef USE_POOL
130  UINT32 poolTime=diff64(oQueueEntry.pool_timestamp_out,oQueueEntry.pool_timestamp_in);
131  m_timingCountPoolPacketsUpstream++;
132  add64(m_timingSumPoolPacketUpstream,poolTime);
133  if(poolTime>m_timingMaxPoolPacketUpstream)
134  m_timingMaxPoolPacketUpstream=poolTime;
135  else if(m_timingMinPoolPacketUpstream>poolTime)
136  m_timingMinPoolPacketUpstream=poolTime;
137  #endif
138  #ifdef _DEBUG
139  #ifndef USE_POOL
140  CAMsg::printMsg(LOG_CRIT,"Upload Packet processing time (arrival --> send): %u µs -- Queue out --> Queue in %u µs\n",
141  proccessingTime,proccessingTimeOP);
142  #else
143  CAMsg::printMsg(LOG_CRIT,"Upload Packet processing time (arrival --> send): %u µs -- Queue out --> Queue in %u µs -- Pool Time: %u µs\n",
144  proccessingTime,proccessingTimeOP,poolTime);
145  #endif
146  #endif
147  }
148  else //downstream
149  {
150  //always data packets
151  m_timingCountDataPacketsDownStream++;
152  add64(m_timingSumDataPacketDownStream,proccessingTime);
153  if(proccessingTime>m_timingMaxDataPacketDownStream)
154  m_timingMaxDataPacketDownStream=proccessingTime;
155  else if(m_timingMinDataPacketDownStream>proccessingTime)
156  m_timingMinDataPacketDownStream=proccessingTime;
157  add64(m_timingSumDataPacketDownStreamOP,proccessingTimeOP);
158  if(proccessingTimeOP>m_timingMaxDataPacketDownStreamOP)
159  m_timingMaxDataPacketDownStreamOP=proccessingTimeOP;
160  else if(m_timingMinDataPacketDownStreamOP>proccessingTimeOP)
161  m_timingMinDataPacketDownStreamOP=proccessingTimeOP;
162  #ifdef USE_POOL
163  UINT32 poolTime=diff64(oQueueEntry.pool_timestamp_out,oQueueEntry.pool_timestamp_in);
164  m_timingCountPoolPacketsDownStream++;
165  add64(m_timingSumPoolPacketDownStream,poolTime);
166  if(poolTime>m_timingMaxPoolPacketDownStream)
167  m_timingMaxPoolPacketDownStream=poolTime;
168  else if(m_timingMinPoolPacketDownStream>poolTime)
169  m_timingMinPoolPacketDownStream=poolTime;
170  #endif
171  #ifdef _DEBUG
172  #ifndef USE_POOL
173  CAMsg::printMsg(LOG_CRIT,"Download Packet processing time (arrival --> send): %u µs -- Queue out --> Queue in %u µs\n",
174  proccessingTime,proccessingTimeOP);
175  #else
176  CAMsg::printMsg(LOG_CRIT,"Download Packet processing time (arrival --> send): %u µs -- Queue out --> Queue in %u µs -- Pool Time: %u µs\n",
177  proccessingTime,proccessingTimeOP,poolTime);
178  #endif
179  #endif
180  }
181 
182  m_csTimeingStats.unlock();
183  return E_SUCCESS;
184 }
185 
186 SINT32 CALogPacketStats::logTimeingStats()
187 {
188  m_csTimeingStats.lock();
189  UINT32 aveDataUpstream=0;
190  UINT32 aveCloseUpstream=0;
191  UINT32 aveOpenUpstream=0;
192  UINT32 aveDataDownStream=0;
193  UINT32 aveDataUpstreamOP=0;
194  UINT32 aveCloseUpstreamOP=0;
195  UINT32 aveOpenUpstreamOP=0;
196  UINT32 aveDataDownStreamOP=0;
197  if(m_timingCountOpenPacketsUpstream>0)
198  {
199  aveOpenUpstream=div64(m_timingSumOpenPacketUpstream,m_timingCountOpenPacketsUpstream);
200  aveOpenUpstreamOP=div64(m_timingSumOpenPacketUpstreamOP,m_timingCountOpenPacketsUpstream);
201  }
202  if(m_timingCountDataPacketsUpstream>0)
203  {
204  aveDataUpstream=div64(m_timingSumDataPacketUpstream,m_timingCountDataPacketsUpstream);
205  aveDataUpstreamOP=div64(m_timingSumDataPacketUpstreamOP,m_timingCountDataPacketsUpstream);
206  }
207  if(m_timingCountClosePacketsUpstream>0)
208  {
209  aveCloseUpstream=div64(m_timingSumClosePacketUpstream,m_timingCountClosePacketsUpstream);
210  aveCloseUpstreamOP=div64(m_timingSumClosePacketUpstreamOP,m_timingCountClosePacketsUpstream);
211  }
212  if(m_timingCountDataPacketsDownStream>0)
213  {
214  aveDataDownStream=div64(m_timingSumDataPacketDownStream,m_timingCountDataPacketsDownStream);
215  aveDataDownStreamOP=div64(m_timingSumDataPacketDownStreamOP,m_timingCountDataPacketsDownStream);
216  }
217 
218  #ifdef USE_POOL
219  UINT32 avePoolUpstream=0;
220  UINT32 avePoolDownStream=0;
221  if(m_timingCountPoolPacketsUpstream>0)
222  avePoolUpstream=div64(m_timingSumPoolPacketUpstream,m_timingCountPoolPacketsUpstream);
223  if(m_timingCountPoolPacketsDownStream>0)
224  aveDataDownStream=div64(m_timingSumPoolPacketDownStream,m_timingCountPoolPacketsDownStream);
225  #endif
226  CAMsg::printMsg(LOG_DEBUG,"Packet timeing stats [µs] -- Data Packets Upstream [%u] (Min/Max/Ave): %u/%u/%u -- Open Packets Upstream [%u]: %u/%u/%u Close Packets Upstream [%u] %u/%u/%u -- Data Packets Downstream [%u]: %u/%u/%u \n",
227  m_timingCountDataPacketsUpstream,m_timingMinDataPacketUpstream,m_timingMaxDataPacketUpstream,aveDataUpstream,
228  m_timingCountOpenPacketsUpstream,m_timingMinOpenPacketUpstream,m_timingMaxOpenPacketUpstream,aveOpenUpstream,
229  m_timingCountClosePacketsUpstream,m_timingMinClosePacketUpstream,m_timingMaxClosePacketUpstream,aveCloseUpstream,
230  m_timingCountDataPacketsDownStream,m_timingMinDataPacketDownStream,m_timingMaxDataPacketDownStream,aveDataDownStream);
231 
232  CAMsg::printMsg(LOG_DEBUG,"Packet timeing stats (only Queue out --> Queue in)[µs] -- Data Packets Upstream [%u] (Min/Max/Ave): %u/%u/%u -- Open Packets Upstream [%u]: %u/%u/%u Close Packets Upstream [%u] %u/%u/%u -- Data Packets Downstream [%u]: %u/%u/%u \n",
233  m_timingCountDataPacketsUpstream,m_timingMinDataPacketUpstreamOP,m_timingMaxDataPacketUpstreamOP,aveDataUpstreamOP,
234  m_timingCountOpenPacketsUpstream,m_timingMinOpenPacketUpstreamOP,m_timingMaxOpenPacketUpstreamOP,aveOpenUpstreamOP,
235  m_timingCountClosePacketsUpstream,m_timingMinClosePacketUpstreamOP,m_timingMaxClosePacketUpstreamOP,aveCloseUpstreamOP,
236  m_timingCountDataPacketsDownStream,m_timingMinDataPacketDownStreamOP,m_timingMaxDataPacketDownStreamOP,aveDataDownStreamOP);
237  #ifdef USE_POOL
238  CAMsg::printMsg(LOG_DEBUG,"Pool timeing stats [µs] -- Upstream [%u] (Min/Max/Ave): %u/%u/%u -- Downstream [%u]: %u/%u/%u\n",
239  m_timingCountPoolPacketsUpstream,m_timingMinPoolPacketUpstream,m_timingMaxPoolPacketUpstream,avePoolUpstream,
240  m_timingCountPoolPacketsDownStream,m_timingMinPoolPacketDownStream,m_timingMaxPoolPacketDownStream,avePoolDownStream);
241  #endif
242  m_csTimeingStats.unlock();
243  return E_SUCCESS;
244 }
245 
246 SINT32 CALogPacketStats::resetTimeingStats()
247  {
248  m_csTimeingStats.lock();
249  m_timingMaxDataPacketUpstream=0;
250  m_timingMaxDataPacketDownStream=0;
251  m_timingMaxClosePacketUpstream=0;
252  m_timingMinDataPacketUpstream=0xFFFFFFFF;
253  m_timingMinDataPacketDownStream=0xFFFFFFFF;
254  m_timingMinClosePacketUpstream=0xFFFFFFFF;
255  m_timingCountDataPacketsUpstream=m_timingCountDataPacketsDownStream=0;
256  m_timingCountClosePacketsUpstream=0;
257  setZero64(m_timingSumDataPacketUpstream);
258  setZero64(m_timingSumDataPacketDownStream);
259  setZero64(m_timingSumClosePacketUpstream);
260  m_timingMaxOpenPacketUpstream=0;
261  m_timingMinOpenPacketUpstream=0xFFFFFFFF;
262  m_timingCountOpenPacketsUpstream=0;
263  setZero64(m_timingSumOpenPacketUpstream);
264 
265  m_timingMaxDataPacketUpstreamOP=0;
266  m_timingMaxDataPacketDownStreamOP=0;
267  m_timingMaxClosePacketUpstreamOP=0;
268  m_timingMinDataPacketUpstreamOP=0xFFFFFFFF;
269  m_timingMinDataPacketDownStreamOP=0xFFFFFFFF;
270  m_timingMinClosePacketUpstreamOP=0xFFFFFFFF;
271  setZero64(m_timingSumDataPacketUpstreamOP);
272  setZero64(m_timingSumDataPacketDownStreamOP);
273  setZero64(m_timingSumClosePacketUpstreamOP);
274  m_timingMaxOpenPacketUpstreamOP=0;
275  m_timingMinOpenPacketUpstreamOP=0xFFFFFFFF;
276  setZero64(m_timingSumOpenPacketUpstreamOP);
277 
278  #ifdef USE_POOL
279  m_timingMaxPoolPacketUpstream=0;
280  m_timingMaxPoolPacketDownStream=0;
281  m_timingMinPoolPacketUpstream=m_timingMinPoolPacketDownStream=0xFFFFFFFF;
282  m_timingCountPoolPacketsUpstream=0;
283  setZero64(m_timingSumPoolPacketUpstream);
284  m_timingCountPoolPacketsDownStream=0;
285  setZero64(m_timingSumPoolPacketDownStream);
286  #endif
287  m_csTimeingStats.unlock();
288  return E_SUCCESS;
289  }
290 #endif
SINT32 sSleep(UINT32 sec)
Sleeps sec Seconds.
Definition: CAUtil.cpp:425
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
Definition: CAUtil.hpp:398
void setZero64(UINT64 &op1)
Definition: CAUtil.hpp:355
UINT32 div64(UINT64 &op1, UINT32 op2)
Definition: CAUtil.hpp:407
void add64(UINT64 &op1, UINT32 op2)
Definition: CAUtil.hpp:375
#define THREAD_RETURN
Definition: StdAfx.h:540
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
Definition: typedefs.hpp:169
#define CHANNEL_DATA
Definition: typedefs.hpp:42
#define CHANNEL_CLOSE
Definition: typedefs.hpp:47