Statistics
| Branch: | Revision:

streamers / measures.c @ a8a31ce0

History | View | Annotate | Download (3.36 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

    
27
static int chunks_received = 0;
28
static int sum_hopcount = 0;
29

    
30
static int chunks_sent = 0;
31

    
32
static int neighsize = 0;
33

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

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

    
46
void print_measures()
47
{
48
  struct timeval tnow;
49

    
50
  if (chunks) print_measure("ChunksPlayed", (double)chunks);
51
  if (chunks) print_measure("DuplicateRatio", (double)duplicates / chunks);
52
  if (chunks) print_measure("PlayoutRatio", (double)played / chunks);
53
  print_measure("NeighSize", (double)neighsize);
54
  if (chunks_received) print_measure("OverlayDistance", (double)sum_hopcount / chunks_received);
55

    
56
  gettimeofday(&tnow, NULL);
57
  if (timerisset(&print_tstart)) print_measure("ChunkReceiveRate", (double) chunks_received / tdiff_sec(&tnow, &print_tstart));
58
  if (timerisset(&print_tstart)) print_measure("ChunkSendRate", (double) chunks_sent / tdiff_sec(&tnow, &print_tstart));
59
}
60

    
61
bool print_every()
62
{
63
  static struct timeval tnext;
64
  static bool startup = true;
65
  struct timeval tnow;
66

    
67
  gettimeofday(&tnow, NULL);
68
  if (startup) {
69
    if (!timerisset(&print_tstart)) {
70
      timeradd(&tnow, &print_tstartdiff, &print_tstart);
71
    }
72
    if (timercmp(&tnow, &print_tstart, <)) {
73
      return false;
74
    } else {
75
      startup = false;
76
    }
77
  }
78

    
79
  if (!timerisset(&tnext)) {
80
    timeradd(&print_tstart, &print_tdiff, &tnext);
81
  }
82
  if (!timercmp(&tnow, &tnext, <)) {
83
    print_measures();
84
    timeradd(&tnext, &print_tdiff, &tnext);
85
  }
86
  return true;
87
}
88

    
89
/*
90
 * Register duplicate arrival
91
*/
92
void reg_chunk_duplicate()
93
{
94
  if (!print_every()) return;
95

    
96
  duplicates++;
97
}
98

    
99
/*
100
 * Register playout/loss of a chunk before playout
101
*/
102
void reg_chunk_playout(int id, bool b, uint64_t timestamp)
103
{
104
  if (!print_every()) return;
105

    
106
  played += b ? 1 : 0;
107
  chunks++;
108
}
109

    
110
/*
111
 * Register actual neghbourhood size
112
*/
113
void reg_neigh_size(int s)
114
{
115
  if (!print_every()) return;
116

    
117
  neighsize = s;
118
}
119

    
120
/*
121
 * Register chunk receive event
122
*/
123
void reg_chunk_receive(int id, uint64_t timestamp, int hopcount)
124
{
125
  if (!print_every()) return;
126

    
127
  chunks_received++;
128
  sum_hopcount += hopcount;
129
}
130

    
131
/*
132
 * Register chunk send event
133
*/
134
void reg_chunk_send(int id)
135
{
136
  if (!print_every()) return;
137

    
138
  chunks_sent++;
139
}
140

    
141
/*
142
 * Register chunk accept evemt
143
*/
144
void reg_offer_accept(bool b)
145
{
146
  static int offers = 0;
147
  static int accepts = 0;
148

    
149
  offers++;
150
  if (b) accepts++;
151
}
152

    
153
/*
154
 * Initialize peer level measurements
155
*/
156
void init_measures()
157
{
158
}
159

    
160
/*
161
 * End peer level measurements
162
*/
163
void end_measures()
164
{
165
  print_measures();
166
}
167

    
168
/*
169
 * Initialize p2p measurements towards a peer
170
*/
171
void add_measures(struct nodeID *id)
172
{
173
}
174

    
175
/*
176
 * Delete p2p measurements towards a peer
177
*/
178
void delete_measures(struct nodeID *id)
179
{
180
}