validator.c 14.8 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

25
/**@internal @IFILE validator.c
Pekka Pessi's avatar
Pekka Pessi committed
26
 *
Pekka Pessi's avatar
Pekka Pessi committed
27 28
 * SIP parser tester. This uses output from tport dump where messages are
 * separated with Control-K ('\v') from each other.
Pekka Pessi's avatar
Pekka Pessi committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Wed Mar 21 19:12:13 2001 ppessi
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/stat.h>
Martti Mela's avatar
Martti Mela committed
46
#ifndef _WIN32
Pekka Pessi's avatar
Pekka Pessi committed
47
#include <sys/mman.h>
Martti Mela's avatar
Martti Mela committed
48
#endif
Pekka Pessi's avatar
Pekka Pessi committed
49 50
#include <fcntl.h>
#include <unistd.h>
Martti Mela's avatar
Martti Mela committed
51
#include <errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
52

53 54
#include <sofia-sip/su_types.h>
#include <sofia-sip/su_alloc_stat.h>
Pekka Pessi's avatar
Pekka Pessi committed
55

56
#include <sofia-sip/su_time.h>
Pekka Pessi's avatar
Pekka Pessi committed
57

58 59 60
#include <sofia-sip/su_tag.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_io.h>
Pekka Pessi's avatar
Pekka Pessi committed
61

62 63
#include <sofia-sip/sip_tag.h>
#include <sofia-sip/url_tag.h>
Pekka Pessi's avatar
Pekka Pessi committed
64

65 66
#include <sofia-sip/sip.h>
#include <sofia-sip/sip_header.h>
Pekka Pessi's avatar
Pekka Pessi committed
67

68
#include <sofia-sip/msg_buffer.h>
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72 73 74 75 76

char const *name = "validator";

typedef struct {
  unsigned o_verbose : 1;	/**< Be verbose */
  unsigned o_very_verbose : 1;	/**< Be very verbose */
  unsigned o_requests : 1;	/**< Only requests */
  unsigned o_responses : 1;	/**< Only responses */
77
  unsigned o_decode : 1;	/**< Only try to decode,
Pekka Pessi's avatar
Pekka Pessi committed
78 79 80 81 82 83 84 85 86 87 88 89
				   print error if unknown headers */
  unsigned o_print : 1;		/**< Print whole message */
  unsigned o_times : 1;		/**< Generate timing information */
  unsigned o_memstats : 1;	/**< Generate memory statistics */
  unsigned o_histogram : 1;	/**< Generate histograms */
  unsigned o_sipstats : 1;	/**< Generate header statistics  */
  unsigned o_vsipstats : 1;	/**< Generate verbatim header statistics */
  unsigned : 0;
  unsigned o_flags;		/**< Message flags */
} options_t;

typedef struct {
90
  size_t N;
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96 97
  uint32_t bsize;
  double buckets[32768];
} histogram_t;

static
histogram_t *histogram_create(uint64_t max, uint32_t bsize)
{
98
  size_t N = (max + bsize - 1) / bsize;
Pekka Pessi's avatar
Pekka Pessi committed
99 100 101 102 103 104 105 106 107 108 109
  histogram_t *h = calloc(1, offsetof(histogram_t, buckets[N + 1]));
  if (!h) { perror("calloc"); exit(1); }
  h->N = N, h->bsize = bsize;
  return h;
}

static
double *histogram_update(histogram_t *h, uint32_t n)
{
  if (h->bsize > 1)
    n /= h->bsize;
110

Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118 119
  if (n < h->N)
    return &h->buckets[n];
  else
    return &h->buckets[h->N];
}

static void
histogram_div(histogram_t *h, histogram_t const *n)
{
120
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
121
  assert(h->N == n->N); assert(h->bsize == n->bsize);
122

Pekka Pessi's avatar
Pekka Pessi committed
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
  for (i = 0; i <= h->N; i++) {
    if (n->buckets[i]) {
      h->buckets[i] /= n->buckets[i];
    }
    else {
      assert(h->buckets[i] == 0);
    }
  }
}

typedef struct {
  uint64_t number;
  uint64_t headers;
  uint64_t payloads;
  uint64_t pl_bytes;
} sipstat_t;

typedef struct {
  sipstat_t req, resp;
  histogram_t *hist_headers;
} sipstats_t;

typedef struct {
  char const *name;
  char const *sep;
  uint64_t  messages;
  uint64_t  bytes;
  uint64_t  errors;
  uint32_t  files;
  double    time;
  options_t options[1];

  /* Statistics */
  histogram_t *hist_msgsize;
  histogram_t *hist_mallocs;
  histogram_t *hist_memsize;
  histogram_t *hist_nheaders;
  sipstats_t   sipstats[1];
  su_home_stat_t hs[1];

  uint64_t     est_fail, est_succ, est_slack;
} context_t;

void usage(void)
{
168 169
  fprintf(stderr,
	  "usage: %s [-vdp]\n",
Pekka Pessi's avatar
Pekka Pessi committed
170 171 172 173 174 175 176 177
	  name);
  exit(2);
}

char *lastpart(char *path)
{
  char *p = strrchr(path, '/');

178
  if (p)
Pekka Pessi's avatar
Pekka Pessi committed
179 180 181 182 183
    return p + 1;
  else
    return path;
}

184
msg_mclass_t const *mclass = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

int validate_file(int fd, char const *name, context_t *ctx);
int validate_dump(char *, off_t, context_t *ctx);
int report(context_t const *ctx);
static void memstats(msg_t *, uint32_t msize, context_t *ctx);
static void sipstats(msg_t *, uint32_t msize, sipstats_t *ss, context_t *ctx);

int main(int argc, char *argv[])
{
  context_t ctx[1] =  {{ 0 }};
  options_t *o = ctx->options;

  name = lastpart(argv[0]);  /* Set our name */

  for (; argv[1]; argv++) {
    if (argv[1][0] == 0)
      usage();
    else if (argv[1][0] != '-')
      break;
    else if (argv[1][1] == 0) {
      argv++; break;
    }
207
    else if (strcmp(argv[1], "-v") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
208
      o->o_very_verbose = o->o_verbose, o->o_verbose = 1;
209
    else if (strcmp(argv[1], "-d") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
210
      o->o_decode = 1;		/* Decode only */
211
    else if (strcmp(argv[1], "-p") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
212
      o->o_print = 1;
213
    else if (strcmp(argv[1], "-q") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
214
      o->o_requests = 1;
215
    else if (strcmp(argv[1], "-Q") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
216
      o->o_responses = 1;
217
    else if (strcmp(argv[1], "-t") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
218
      o->o_times = 1;
219
    else if (strcmp(argv[1], "-m") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
220
      o->o_memstats = 1;
221
    else if (strcmp(argv[1], "-s") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
222
      o->o_vsipstats = o->o_sipstats, o->o_sipstats = 1;
223
    else if (strcmp(argv[1], "-h") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
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
      o->o_histogram = 1;
    else
      usage();
  }

  if (o->o_requests && o->o_responses)
    usage();

  if (!mclass)
    mclass = sip_default_mclass();

  if (argv[1]) {
    for (; argv[1]; argv++) {
      int fd = open(argv[1], O_RDONLY, 000);
      if (fd == -1)
	perror(argv[1]), exit(1);
      if (validate_file(fd, argv[1], ctx))
	exit(1);
      close(fd);
    }
  }
  else
    validate_file(0, "", ctx);

  report(ctx);
249

Pekka Pessi's avatar
Pekka Pessi committed
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
  exit(0);
}


int validate_file(int fd, char const *name, context_t *ctx)
{
  void *p;
  off_t size;
  int retval;

  ctx->name = name;
  if (strlen(name))
    ctx->sep = ": ";
  else
    ctx->sep = "";

  ctx->files++;

  size = lseek(fd, 0, SEEK_END);

  if (size < 1)
    return 0;
  if (size > INT_MAX) {
    fprintf(stderr, "%s%stoo large file to map\n", ctx->name, ctx->sep);
    return -1;
  }

Martti Mela's avatar
Martti Mela committed
277
#ifndef _WIN32
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281 282
  p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0L);
  if (p == NULL) {
    perror("mmap");
    return -1;
  }
Martti Mela's avatar
Martti Mela committed
283

Pekka Pessi's avatar
Pekka Pessi committed
284 285 286
  retval = validate_dump(p, size, ctx);
  munmap(p, size);
  return retval;
Martti Mela's avatar
Martti Mela committed
287 288 289 290 291

#else
  errno = EINVAL;
  perror("mmap not implemented");
  return -1;
292
#endif
Martti Mela's avatar
Martti Mela committed
293

Pekka Pessi's avatar
Pekka Pessi committed
294 295
}

296
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302 303 304 305 306 307 308
void nul_terminate(char *b, off_t size)
{
  char *end;

  /* NUL-terminate */
  for (end = b + size - 1; end != b; end--)
    if (*end == '\v')
      break;

  *end = '\0';
}

309
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
310 311 312 313 314 315 316 317 318 319 320 321 322
int search_msg(char **bb, char const *protocol)
{
  int linelen, plen = strlen(protocol);
  char *b = *bb;

  for (;;) {
    if (!b[0])
      return 0;

    if (strncmp(b, protocol, plen) == 0 && b[plen] == ' ')
      return 1;			/* status */

    linelen = strcspn(b, "\r\n");
323 324 325

    if (linelen > plen + 1 &&
	b[linelen - plen - 1] == ' ' &&
Pekka Pessi's avatar
Pekka Pessi committed
326 327 328 329 330 331 332 333 334 335
	strncmp(b + linelen - plen, protocol, plen) == 0)
      return 1;			/* request */

    b += linelen + strspn(b + linelen, "\r\n");
    *bb = b;
  }
}

int validate_dump(char *b, off_t size, context_t *ctx)
{
336
  size_t n = 0, N = 0;
337
  struct message {
Pekka Pessi's avatar
Pekka Pessi committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    char *b;
    int   size;
  } *msgs = NULL;
  uint64_t time0, time1;
  options_t *o = ctx->options;
  int maxsize = 0;

  nul_terminate(b, size);

  /* Split dump file to messages */
  while (search_msg(&b, SIP_VERSION_CURRENT)) {
    int msize = strcspn(b, "\v");
    int linelen = strcspn(b, "\r\n");

    if (o->o_responses &&
	memcmp(b, SIP_VERSION_CURRENT, strlen(SIP_VERSION_CURRENT)) != 0)
      ;
355
    else if (o->o_requests &&
Pekka Pessi's avatar
Pekka Pessi committed
356 357 358 359
	memcmp(b, SIP_VERSION_CURRENT, strlen(SIP_VERSION_CURRENT)) == 0)
      ;
    else {
      if (o->o_very_verbose)
360
	printf("message "MOD_ZU": %.*s\n", n, linelen, b);
Pekka Pessi's avatar
Pekka Pessi committed
361 362 363

      if (n == N) {
	N *= 2; if (n == N) N = 16;
364

Pekka Pessi's avatar
Pekka Pessi committed
365 366 367 368 369 370 371 372
	msgs = realloc(msgs, sizeof(*msgs) * N);
	if (msgs == NULL) {
	  perror("realloc");
	  exit(1);
	}
      }
      msgs[n].b = b; msgs[n].size = msize;
      n++;
373

Pekka Pessi's avatar
Pekka Pessi committed
374
      ctx->bytes += msize;
375

Pekka Pessi's avatar
Pekka Pessi committed
376 377 378
      if (msize > maxsize)
	maxsize = msize;
    }
379

Pekka Pessi's avatar
Pekka Pessi committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    b += msize; if (*b) *b++ = '\0';
  }

  ctx->messages += N = n;

  if (o->o_histogram) {
    ctx->hist_msgsize = histogram_create(maxsize, 64);

    if (o->o_memstats) {
      ctx->hist_mallocs = histogram_create(maxsize, 64);
      ctx->hist_memsize = histogram_create(maxsize, 64);
    }

    if (o->o_sipstats) {
      ctx->sipstats->hist_headers = histogram_create(64, 1);
      ctx->hist_nheaders = histogram_create(maxsize, 64);
    }
  }

  time0 = su_nanocounter();

  for (n = 0; n < N; n++) {
    msg_t *msg = msg_create(mclass, o->o_flags);
    int m;

    if (msg == NULL) {
      perror("msg_create"); exit(1);
    }

    if (o->o_memstats)
      su_home_init_stats(msg_home(msg));
411

Pekka Pessi's avatar
Pekka Pessi committed
412 413
    msg_buf_set(msg, msgs[n].b, msgs[n].size + 1);
    msg_buf_commit(msg, msgs[n].size, 1);
414

Pekka Pessi's avatar
Pekka Pessi committed
415 416 417 418 419
    su_home_preload(msg_home(msg), 1, msgs[n].size + 384);

    m = msg_extract(msg);

    if (m < 0) {
420
      fprintf(stderr, "%s%sparsing error in message "MOD_ZU"\n",
Pekka Pessi's avatar
Pekka Pessi committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	      ctx->name, ctx->sep, n);
      ctx->errors++;
    }
    else {
      if (ctx->hist_msgsize)
	*histogram_update(ctx->hist_msgsize, msgs[n].size) += 1;

      if (o->o_sipstats)
	sipstats(msg, msgs[n].size, ctx->sipstats, ctx);

      if (o->o_memstats)
	memstats(msg, msgs[n].size, ctx);
    }

    msg_destroy(msg);
  }
437

Pekka Pessi's avatar
Pekka Pessi committed
438 439 440 441 442 443 444
  time1 = su_nanocounter();

  if (o->o_times) {
    double dur = (time1 - time0) * 1E-9;

    ctx->time += dur;

445
    printf("%s%s"MOD_ZU" messages in %g seconds (%g msg/sec)\n"
Pekka Pessi's avatar
Pekka Pessi committed
446 447 448 449 450 451 452 453 454 455 456
	   "      parse speed %.1f Mb/s (on Ethernet wire %.1f Mb/s)\n",
	   ctx->name, ctx->sep, N, dur, (double)N / dur,
	   (double)ctx->bytes * 8 / ctx->time / 1e6,
	   ((double)ctx->bytes + N * (16 + 20 + 8)) * 8 / ctx->time / 1e6);
  }

  free(msgs);

  return 0;
}

457
typedef unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
458 459 460 461 462 463

static
void report_memstats(char const *title, su_home_stat_t const hs[1])
{
  printf("%s%smemory statistics\n", title, strlen(title) ? " " : "");
  if (hs->hs_allocs.hsa_number)
464 465 466 467
    printf("\t"LLU" allocs, "LLU" bytes, "LLU" rounded,"
	   " "LLU" max\n",
	   (ull)hs->hs_allocs.hsa_number, (ull)hs->hs_allocs.hsa_bytes,
	   (ull)hs->hs_allocs.hsa_rbytes, (ull)hs->hs_allocs.hsa_maxrbytes);
Pekka Pessi's avatar
Pekka Pessi committed
468
  if (hs->hs_frees.hsf_number)
469 470 471
    printf("\t"LLU" frees, "LLU" bytes, rounded to "LLU" bytes\n",
	   (ull)hs->hs_frees.hsf_number, (ull)hs->hs_frees.hsf_bytes,
	   (ull)hs->hs_frees.hsf_rbytes);
Pekka Pessi's avatar
Pekka Pessi committed
472
  if (hs->hs_rehash || hs->hs_clones)
473
    printf("\t"LLU" rehashes, "LLU" clones\n",
474
	   (ull)hs->hs_rehash, (ull)hs->hs_clones);
Pekka Pessi's avatar
Pekka Pessi committed
475 476 477 478 479 480 481
}

void memstats(msg_t *msg, uint32_t msize, context_t *ctx)
{
  options_t *o = ctx->options;
  su_home_stat_t hs[1];

482
  su_home_get_stats(msg_home(msg), 1, hs, sizeof(hs));
Pekka Pessi's avatar
Pekka Pessi committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  su_home_stat_add(ctx->hs, hs);

  if (o->o_histogram) {
    *histogram_update(ctx->hist_mallocs, msize) += hs->hs_allocs.hsa_number;
    *histogram_update(ctx->hist_memsize, msize) += hs->hs_allocs.hsa_maxrbytes;
  }

  {
    int estimate = msize + 384;
    int slack = estimate - hs->hs_allocs.hsa_maxrbytes;

    if (slack < 0)
      ctx->est_fail++;
    else {
      ctx->est_succ++;
      ctx->est_slack += slack;
    }
  }

  if (o->o_very_verbose)
    report_memstats(ctx->name, hs);
}

void report_sipstat(char const *what, sipstat_t const *sss)
{
508
  printf("%s: "LLU" with %.1f headers (total "LLU")\n",
509
	 what, (ull)sss->number, (double)sss->headers / sss->number,
510
	 (ull)sss->headers);
Pekka Pessi's avatar
Pekka Pessi committed
511
  if (sss->payloads)
512
    printf("\t"LLU" with body of %.1f bytes (total "LLU")\n",
513
	   (ull)sss->payloads, (double)sss->pl_bytes / sss->payloads,
514
	   (ull)sss->payloads);
Pekka Pessi's avatar
Pekka Pessi committed
515 516 517 518 519 520 521 522 523
}

void sipstats(msg_t *msg, uint32_t msize, sipstats_t *ss, context_t *ctx)
{
  options_t *o = ctx->options;
  msg_pub_t *m = msg_object(msg);
  sip_t const *sip = sip_object(msg);
  msg_header_t *h;
  sipstat_t *sss;
524
  size_t n, bytes;
Pekka Pessi's avatar
Pekka Pessi committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

  if (!sip)
    return;

  if (m->msg_request) {
    sss = &ss->req;
    h = m->msg_request;
  }
  else if (m->msg_status) {
    sss = &ss->resp;
    h = m->msg_status;
  }
  else {
    return;
  }

  sss->number++;

  /* Count headers */
544
  for (n = 0, h = h->sh_succ; h && !sip_is_separator((sip_header_t *)h); h = h->sh_succ)
Pekka Pessi's avatar
Pekka Pessi committed
545 546 547 548
    n++;

  sss->headers += n;

549
  bytes = sip->sip_payload ? (size_t)sip->sip_payload->pl_len : 0;
Pekka Pessi's avatar
Pekka Pessi committed
550 551 552 553 554 555 556 557 558 559 560 561

  if (bytes) {
    sss->payloads++;
    sss->pl_bytes += bytes;
  }

  if (ctx->hist_nheaders) {
    *histogram_update(ctx->hist_nheaders, msize) += n;
    *histogram_update(ss->hist_headers, n) += 1;
  }

  if (o->o_very_verbose)
562
    printf("%s%s"MOD_ZU" headers, "MOD_ZU" bytes in payload\n",
Pekka Pessi's avatar
Pekka Pessi committed
563 564 565 566 567
	   ctx->name, ctx->sep, n, bytes);
}

void report_histogram(char const *title, histogram_t const *h)
{
568
  size_t i, min_i, max_i;
Pekka Pessi's avatar
Pekka Pessi committed
569

570
  for (i = 0; i < h->N && h->buckets[i] == 0.0; i++)
Pekka Pessi's avatar
Pekka Pessi committed
571 572 573
    ;
  min_i = i;

574
  for (i = h->N - 1; i >= 0 && h->buckets[i] == 0.0; i--)
Pekka Pessi's avatar
Pekka Pessi committed
575 576
    ;
  max_i = i;
577

Pekka Pessi's avatar
Pekka Pessi committed
578 579 580 581 582
  if (min_i >= max_i)
    return;

  printf("%s histogram\n", title);
  for (i = min_i; i < max_i; i++)
583
    printf("\t"MOD_ZU".."MOD_ZU": %.1f\n", i * h->bsize, (i + 1) * h->bsize, h->buckets[i]);
Pekka Pessi's avatar
Pekka Pessi committed
584 585

  if (h->buckets[h->N])
586
    printf("\t"MOD_ZU"..: %.1f\n", h->N * h->bsize, h->buckets[h->N]);
Pekka Pessi's avatar
Pekka Pessi committed
587 588 589 590
}

int report(context_t const *ctx)
{
591
  const options_t *o = ctx->options;
Pekka Pessi's avatar
Pekka Pessi committed
592 593 594 595 596
  uint64_t n = ctx->messages;

  if (!n)
    return -1;

597 598
  printf("total "LLU" messages with "LLU" bytes (mean size "LLU")\n",
	 (ull)n, (ull)ctx->bytes, (ull)(ctx->bytes / n));
Pekka Pessi's avatar
Pekka Pessi committed
599 600 601 602

  if (ctx->hist_msgsize)
    report_histogram("Message size", ctx->hist_msgsize);

603
  if (o->o_times && ctx->files > 1)
604 605
    printf("total "LLU" messages in %g seconds (%g msg/sec)\n",
	   (ull)n, ctx->time, (double)n / ctx->time);
Pekka Pessi's avatar
Pekka Pessi committed
606 607

  if (o->o_sipstats) {
608
    const sipstats_t *ss = ctx->sipstats;
Pekka Pessi's avatar
Pekka Pessi committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622
    report_sipstat("requests", &ss->req);
    report_sipstat("responses", &ss->resp);

    if (ctx->hist_nheaders) {
      histogram_div(ctx->hist_nheaders, ctx->hist_msgsize);
      report_histogram("Number of headers", ctx->hist_nheaders);
    }
  }

  if (o->o_memstats) {
    su_home_stat_t hs[1];

    *hs = *ctx->hs;
    report_memstats("total", hs);
623

Pekka Pessi's avatar
Pekka Pessi committed
624 625 626 627 628 629 630 631 632 633 634
    /* Calculate mean */
    hs->hs_clones /= n; hs->hs_rehash /= n;
    hs->hs_allocs.hsa_number /= n; hs->hs_allocs.hsa_bytes /= n;
    hs->hs_allocs.hsa_rbytes /= n; hs->hs_allocs.hsa_maxrbytes /= n;
    hs->hs_frees.hsf_number /= n; hs->hs_frees.hsf_bytes /= n;
    hs->hs_frees.hsf_rbytes /= n;
    hs->hs_blocks.hsb_number /= n; hs->hs_blocks.hsb_bytes /= n;
    hs->hs_blocks.hsb_rbytes /= n;

    report_memstats("mean", hs);

635
    printf("\testimator fails %.1f%% times (mean slack %.0f bytes)\n",
Pekka Pessi's avatar
Pekka Pessi committed
636 637 638 639 640 641 642 643 644 645 646
	   100 * (double)ctx->est_fail / (ctx->est_fail + ctx->est_succ),
	   (double)ctx->est_slack / ctx->est_succ);

    if (ctx->hist_memsize) {
      histogram_div(ctx->hist_memsize, ctx->hist_msgsize);
      report_histogram("Allocated memory", ctx->hist_memsize);
    }
  }

  return 0;
}