aboutsummaryrefslogtreecommitdiffstats
path: root/timing.c
blob: d4baa1594fc4234a553039b51a8a3284301d5331 (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
/*  
   Copyright (C)  2006 Renaissance Technologies Corp. 
                  main developer: HP Wei <hp@rentec.com>
   Copyright (C)  2005 Renaissance Technologies Corp.
                  main developer: HP Wei <hp@rentec.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   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.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
*/

#include <sys/time.h>
#include <stdio.h>

#define N 501       /* effectively set the maximum time in rtt_hist to be 500 msec */

extern int verbose;

/* for timing */
struct timeval tv0, tv1;
unsigned long  usec_acc, sec_acc;   /* accumulator of timing */
unsigned int   rtt_hist[N];         /* rtt_hist[i] = count of rtt within (i, i+1) */

void refresh_timer() 
{
  usec_acc = 0;
  sec_acc  = 0;
}

void start_timer()
{
  struct timezone tz;
  gettimeofday(&tv0, &tz);
}

void end_timer()
{
  struct timezone tz;
  gettimeofday(&tv1, &tz); /* end timer -------- */
}

void update_time_accumulator()
{
  if (tv1.tv_usec<tv0.tv_usec) {
    sec_acc  += (tv1.tv_sec - tv0.tv_sec - 1);
    usec_acc += (1000000 + tv1.tv_usec - tv0.tv_usec);
  } else {
    sec_acc  += (tv1.tv_sec - tv0.tv_sec);
    usec_acc += (tv1.tv_usec - tv0.tv_usec);
  }    
}

double get_accumulated_time()
{
  double sec = sec_acc;
  sec += (usec_acc / 1e6);
  return sec;
}

double get_accumulated_usec()
{
  double usec = usec_acc;
  usec += (sec_acc*1e6);
  return usec;
}

void init_rtt_hist()
{
  int i;
  for(i=0; i<N; ++i) rtt_hist[i] = 0;
}

void update_rtt_hist(unsigned int rtt)
{
  unsigned int index;
  index = rtt / 1000;
  if (index>(N-2)) index = N-1;
  rtt_hist[index]++;
}

void pr_rtt_hist()
{
  int i;
  fprintf(stderr, "rtt histogram\n");
  fprintf(stderr, "msec counts\n");
  fprintf(stderr, "---- --------\n");
  for(i=0; i<N; ++i) {
    if (verbose<=1 && i>10) continue;
    if (rtt_hist[i] != 0) {
      fprintf(stderr, "%4d %u\n", i, rtt_hist[i]);
    }
  }
}

unsigned int pages_wo_ack()
{
  return rtt_hist[N-1];
}