summaryrefslogtreecommitdiff
path: root/ics-openvpn-stripped/main/openvpn/src/openvpn/perf.c
blob: 910d171d896a64f3d287f5e9b2eca36914be4894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *  OpenVPN -- An application to securely tunnel IP networks
 *             over a single TCP/UDP port, with support for SSL/TLS-based
 *             session authentication and key exchange,
 *             packet encryption, packet authentication, and
 *             packet compression.
 *
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program (see the file COPYING included with this
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_MSC_VER)
#include "config-msvc.h"
#endif

#include "syshead.h"

#include "perf.h"

#ifdef ENABLE_PERFORMANCE_METRICS

#include "error.h"
#include "otime.h"

#include "memdbg.h"

static const char *metric_names[] = {
  "PERF_BIO_READ_PLAINTEXT",
  "PERF_BIO_WRITE_PLAINTEXT",
  "PERF_BIO_READ_CIPHERTEXT",
  "PERF_BIO_WRITE_CIPHERTEXT",
  "PERF_TLS_MULTI_PROCESS",
  "PERF_IO_WAIT",
  "PERF_EVENT_LOOP",
  "PERF_MULTI_CREATE_INSTANCE",
  "PERF_MULTI_CLOSE_INSTANCE",
  "PERF_MULTI_SHOW_STATS",
  "PERF_MULTI_BCAST",
  "PERF_MULTI_MCAST",
  "PERF_SCRIPT",
  "PERF_READ_IN_LINK",
  "PERF_PROC_IN_LINK",
  "PERF_READ_IN_TUN",
  "PERF_PROC_IN_TUN",
  "PERF_PROC_OUT_LINK",
  "PERF_PROC_OUT_TUN",
  "PERF_PROC_OUT_TUN_MTCP"
};

struct perf
{
# define PS_INITIAL            0
# define PS_METER_RUNNING      1
# define PS_METER_INTERRUPTED  2
  int state;

  struct timeval start;
  double sofar;
  double sum;
  double max;
  double count;
};

struct perf_set
{
  int stack_len;
  int stack[STACK_N];
  struct perf perf[PERF_N];
};

static struct perf_set perf_set;

static void perf_print_state (int lev);

static inline int
get_stack_index (int sdelta)
{
  const int sindex = perf_set.stack_len + sdelta;
  if (sindex >= 0 && sindex < STACK_N)
    return sindex;
  else
    return -1;
}

static int
get_perf_index (int sdelta)
{
  const int sindex = get_stack_index (sdelta);
  if (sindex >= 0)
    {
      const int pindex = perf_set.stack[sindex];
      if (pindex >= 0 && pindex < PERF_N)
	return pindex;
      else
	return -1;
    }
  else
    return -1;
}

static struct perf *
get_perf (int sdelta)
{
  const int pindex = get_perf_index (sdelta);
  if (pindex >= 0)
    return &perf_set.perf[pindex];
  else
    return NULL;
}

static void
push_perf_index (int pindex)
{
  const int sindex = get_stack_index (0);
  const int newlen = get_stack_index (1);
  if (sindex >= 0 && newlen >= 0
      && pindex >= 0 && pindex < PERF_N)
    {
      int i;
      for (i = 0; i < sindex; ++i)
	if (perf_set.stack[i] == pindex)
	  {
	    perf_print_state (M_INFO);
	    msg (M_FATAL, "PERF: push_perf_index %s failed",
		 metric_names [pindex]); 
	  }

      perf_set.stack[sindex] = pindex;
      perf_set.stack_len = newlen;
    }
  else
    msg (M_FATAL, "PERF: push_perf_index: stack push error"); 
}

static void
pop_perf_index (void)
{
  const int newlen = get_stack_index (-1);
  if (newlen >= 0)
    {
      perf_set.stack_len = newlen;
    }
  else
    msg (M_FATAL, "PERF: pop_perf_index: stack pop error"); 
}

static void
state_must_be (const struct perf *p, const int wanted)
{
  if (p->state != wanted)
    msg (M_FATAL, "PERF: bad state actual=%d wanted=%d",
	 p->state,
	 wanted);
}

static void
update_sofar (struct perf *p)
{
  struct timeval current;
  ASSERT (!gettimeofday (&current, NULL));
  p->sofar += (double) tv_subtract (&current, &p->start, 600) / 1000000.0;
  tv_clear (&p->start);
}

static void
perf_start (struct perf *p)
{
  state_must_be (p, PS_INITIAL);
  ASSERT (!gettimeofday (&p->start, NULL));
  p->sofar = 0.0;
  p->state = PS_METER_RUNNING;
}

static void
perf_stop (struct perf *p)
{
  state_must_be (p, PS_METER_RUNNING);
  update_sofar (p);
  p->sum += p->sofar;
  if (p->sofar > p->max)
    p->max = p->sofar;
  p->count += 1.0;
  p->sofar = 0.0;
  p->state = PS_INITIAL;
}

static void
perf_interrupt (struct perf *p)
{
  state_must_be (p, PS_METER_RUNNING);
  update_sofar (p);
  p->state = PS_METER_INTERRUPTED;
}

static void
perf_resume (struct perf *p)
{
  state_must_be (p, PS_METER_INTERRUPTED);
  ASSERT (!gettimeofday (&p->start, NULL));
  p->state = PS_METER_RUNNING;
}

void
perf_push (int type)
{
  struct perf *prev;
  struct perf *cur;

  ASSERT (SIZE(metric_names) == PERF_N);
  push_perf_index (type);

  prev = get_perf (-2);
  cur = get_perf (-1);

  ASSERT (cur);

  if (prev)
    perf_interrupt (prev);
  perf_start (cur);
}

void
perf_pop (void)
{
  struct perf *prev;
  struct perf *cur;

  prev = get_perf (-2);
  cur = get_perf (-1);

  ASSERT (cur);
  perf_stop (cur);

  if (prev)
    perf_resume (prev);

  pop_perf_index ();
}

void
perf_output_results (void)
{
  int i;
  msg (M_INFO, "LATENCY PROFILE (mean and max are in milliseconds)");
  for (i = 0; i < PERF_N; ++i)
    {
      struct perf *p = &perf_set.perf[i];
      if (p->count > 0.0)
	{
	  const double mean = p->sum / p->count;
	  msg (M_INFO, "%s n=%.0f mean=%.3f max=%.3f", metric_names[i], p->count, mean*1000.0, p->max*1000.0);
	}
    }
}

static void
perf_print_state (int lev)
{
  struct gc_arena gc = gc_new ();
  int i;
  msg (lev, "PERF STATE");
  msg (lev, "Stack:");
  for (i = 0; i < perf_set.stack_len; ++i)
    {
      const int j = perf_set.stack[i];
      const struct perf *p = &perf_set.perf[j];
      msg (lev, "[%d] %s state=%d start=%s sofar=%f sum=%f max=%f count=%f",
	   i,
	   metric_names[j],
	   p->state,
	   tv_string (&p->start, &gc),
	   p->sofar,
	   p->sum,
	   p->max,
	   p->count);
    }
  gc_free (&gc);
}

#else
#ifdef _MSC_VER  /* Dummy function needed to avoid empty file compiler warning in Microsoft VC */
static void dummy(void) {}
#endif
#endif