Statistics
| Branch: | Revision:

streamers / measures.c @ 14e5c21e

History | View | Annotate | Download (3.9 KB)

1
/*
2
 *  Copyright (c) 2010 Csaba Kiraly
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
#include <stdio.h>
7
#include <stdint.h>
8
#include <math.h>
9
#ifndef NAN        //NAN is missing in some old math.h versions
10
#define NAN            (0.0/0.0)
11
#endif
12
#ifndef INFINITY
13
#define INFINITY       (1.0/0.0)
14
#endif
15
#include <sys/time.h>
16

    
17
#include "measures.h"
18

    
19
static struct timeval print_tdiff = {1, 0};
20
static struct timeval print_tstartdiff = {20, 0};
21
static struct timeval print_tstart;
22

    
23
static int duplicates = 0;
24
static int chunks = 0;
25
static int played = 0;
26
static uint64_t sum_reorder_delay = 0;
27

    
28
static int chunks_received = 0;
29
static int sum_hopcount = 0;
30
static uint64_t sum_receive_delay = 0;
31

    
32
static int chunks_sent = 0;
33

    
34
static int neighsize = 0;
35

    
36
double tdiff_sec(const struct timeval *a, const struct timeval *b)
37
{
38
  struct timeval tdiff;
39
  timersub(a, b, &tdiff);
40
  return tdiff.tv_sec + tdiff.tv_usec / 1000000.0;
41
}
42

    
43
void print_measure(const char *name, double value)
44
{
45
  fprintf(stderr,"abouttopublish,,,,%s,%f,,,\n", name, value);
46
}
47

    
48
void print_measures()
49
{
50
  struct timeval tnow;
51

    
52
  if (chunks) print_measure("ChunksPlayed", (double)chunks);
53
  if (chunks) print_measure("DuplicateRatio", (double)duplicates / chunks);
54
  if (chunks) print_measure("PlayoutRatio", (double)played / chunks);
55
  if (chunks) print_measure("ReorderDelay", (double)sum_reorder_delay / 1e6 / chunks);
56
  print_measure("NeighSize", (double)neighsize);
57
  if (chunks_received) print_measure("OverlayDistance", (double)sum_hopcount / chunks_received);
58
  if (chunks_received) print_measure("ReceiveDelay", (double)sum_receive_delay / 1e6 / chunks_received);
59

    
60
  gettimeofday(&tnow, NULL);
61
  if (timerisset(&print_tstart)) print_measure("ChunkReceiveRate", (double) chunks_received / tdiff_sec(&tnow, &print_tstart));
62
  if (timerisset(&print_tstart)) print_measure("ChunkSendRate", (double) chunks_sent / tdiff_sec(&tnow, &print_tstart));
63
}
64

    
65
bool print_every()
66
{
67
  static struct timeval tnext;
68
  static bool startup = true;
69
  struct timeval tnow;
70

    
71
  gettimeofday(&tnow, NULL);
72
  if (startup) {
73
    if (!timerisset(&print_tstart)) {
74
      timeradd(&tnow, &print_tstartdiff, &print_tstart);
75
    }
76
    if (timercmp(&tnow, &print_tstart, <)) {
77
      return false;
78
    } else {
79
      startup = false;
80
    }
81
  }
82

    
83
  if (!timerisset(&tnext)) {
84
    timeradd(&print_tstart, &print_tdiff, &tnext);
85
  }
86
  if (!timercmp(&tnow, &tnext, <)) {
87
    print_measures();
88
    timeradd(&tnext, &print_tdiff, &tnext);
89
  }
90
  return true;
91
}
92

    
93
/*
94
 * Register duplicate arrival
95
*/
96
void reg_chunk_duplicate()
97
{
98
  if (!print_every()) return;
99

    
100
  duplicates++;
101
}
102

    
103
/*
104
 * Register playout/loss of a chunk before playout
105
*/
106
void reg_chunk_playout(int id, bool b, uint64_t timestamp)
107
{
108
  struct timeval tnow;
109

    
110
  if (!print_every()) return;
111

    
112
  played += b ? 1 : 0;
113
  chunks++;
114
  gettimeofday(&tnow, NULL);
115
  sum_reorder_delay += (tnow.tv_usec + tnow.tv_sec * 1000000ULL) - timestamp;
116
}
117

    
118
/*
119
 * Register actual neghbourhood size
120
*/
121
void reg_neigh_size(int s)
122
{
123
  if (!print_every()) return;
124

    
125
  neighsize = s;
126
}
127

    
128
/*
129
 * Register chunk receive event
130
*/
131
void reg_chunk_receive(int id, uint64_t timestamp, int hopcount, bool old, bool dup)
132
{
133
  struct timeval tnow;
134

    
135
  if (!print_every()) return;
136

    
137
  chunks_received++;
138
  sum_hopcount += hopcount;
139
  gettimeofday(&tnow, NULL);
140
  sum_receive_delay += (tnow.tv_usec + tnow.tv_sec * 1000000ULL) - timestamp;
141
}
142

    
143
/*
144
 * Register chunk send event
145
*/
146
void reg_chunk_send(int id)
147
{
148
  if (!print_every()) return;
149

    
150
  chunks_sent++;
151
}
152

    
153
/*
154
 * Register chunk accept evemt
155
*/
156
void reg_offer_accept(bool b)
157
{
158
  static int offers = 0;
159
  static int accepts = 0;
160

    
161
  offers++;
162
  if (b) accepts++;
163
}
164

    
165
/*
166
 * Initialize peer level measurements
167
*/
168
void init_measures()
169
{
170
}
171

    
172
/*
173
 * End peer level measurements
174
*/
175
void end_measures()
176
{
177
  print_measures();
178
}
179

    
180
/*
181
 * Initialize p2p measurements towards a peer
182
*/
183
void add_measures(struct nodeID *id)
184
{
185
}
186

    
187
/*
188
 * Delete p2p measurements towards a peer
189
*/
190
void delete_measures(struct nodeID *id)
191
{
192
}