forked from venkatarun95/genericCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.hh
61 lines (49 loc) · 1.48 KB
/
utility.hh
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
#ifndef UTILITY_HH
#define UTILITY_HH
#include <cmath>
// Tracks the utility variables that we are interested in
class Utility
{
private:
// Total time for which sending was taking place. Used to calculate throughput.
double _tick_share_sending;
unsigned int _packets_received;
double _total_delay;
public:
Utility( void ) : _tick_share_sending( 0 ), _packets_received( 0 ), _total_delay( 0 ) {}
void sending_duration( const double & duration, const unsigned int num_sending ) { _tick_share_sending += duration / double( num_sending ); }
void packets_received( const std::vector< Packet > & packets ) {
_packets_received += packets.size();
for ( auto &x : packets ) {
assert( x.tick_received >= x.tick_sent );
_total_delay += x.tick_received - x.tick_sent;
}
}
double average_throughput( void ) const
{
if ( _tick_share_sending == 0 ) {
return 0.0;
}
return double( _packets_received ) / _tick_share_sending;
}
double average_delay( void ) const
{
if ( _packets_received == 0 ) {
return 0.0;
}
return double( _total_delay ) / double( _packets_received );
}
double utility( void ) const
{
if ( _tick_share_sending == 0 ) {
return 0.0;
}
if ( _packets_received == 0 ) {
return -INT_MAX;
}
const double throughput_utility = log2( average_throughput() );
const double delay_penalty = log2( average_delay() / 100.0 );
return throughput_utility - delay_penalty;
}
};
#endif