File: rate.cpp

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (334 lines) | stat: -rw-r--r-- 8,860 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
249
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "rate.h"  // def.h

#include <sys/time.h>

#include "btconfig.h"
#include "bufio.h" // for BUF_DEF_SIZ
#include "bttime.h"
#include "console.h"

#ifndef HAVE_CLOCK_GETTIME
#include "compat.h"
#endif

#define RATE_INTERVAL 20
#define SHORT_INTERVAL 5

Rate::Rate()
{
  m_last_timestamp = m_total_timeused = m_nom_time = (time_t)0;
  m_count_bytes = 0;
  m_history = m_history_last = (BWSAMPLE *)0;
  m_last_realtime = m_recent_realtime = m_prev_realtime = 0;
  m_last_size = m_recent_size = m_prev_size = 0;
  m_selfrate = (Rate *)0;
  m_late = 0;
  m_ontime = m_update_nominal = 0;
  m_lastrate.lasttime = (time_t)0;
  m_nominal = DEFAULT_SLICE_SIZE / 8;  // minimum "acceptable" rate
}

void Rate::Reset()
{
  m_last_timestamp = m_total_timeused = (time_t)0;
  ClearHistory();
  m_last_realtime = 0;
  m_last_size = 0;
}

void Rate::StartTimer()
{
  if( !m_last_timestamp ) m_last_timestamp = now;
}

void Rate::StopTimer()
{
  if( m_last_timestamp ){
    m_total_timeused += (now - m_last_timestamp);
    if( m_history ){
      if( m_history == m_history_last )
        m_nominal = (size_t)(m_history->bytes /
                             (m_history->timestamp - m_last_timestamp));
      else (void)RateMeasure();  // updates nominal
    }
    m_last_timestamp = 0;
    ClearHistory();
  }
  m_update_nominal = 0;
}

BWSAMPLE *Rate::NewSample()
{
  BWSAMPLE *sample = new BWSAMPLE;
  if( sample ){
    sample->timestamp = 0;
    sample->bytes = 0;
    sample->next = (BWSAMPLE *)0;
  }else
    CONSOLE.Warning(2, "warn, failed to allocate memory for bandwidth sample.");

  return sample;
}

void Rate::ClearHistory()
{
  if( m_history ){
    BWSAMPLE *pnext;
    for( BWSAMPLE *p=m_history; p; p=pnext ){
      pnext = p->next;
      delete p;
    }
    m_history = m_history_last = (BWSAMPLE *)0;
  }
}

void Rate::Cleanup()
{
  BWSAMPLE *p = m_history;

  while( p && RATE_INTERVAL <= now - (time_t)(p->timestamp) ){
    int nzero = 0;
    if( !p->next ){
      if( BWSAMPLE *q = NewSample() ){
        q->timestamp = (double)now - 1;
        p->next = q;
        m_history_last = q;
        nzero++;
      }else{
        p->bytes = p->bytes * RATE_INTERVAL / (now - (time_t)(p->timestamp));
        p->timestamp = (double)(now - RATE_INTERVAL + 1);
      }
    }
    if( p->next ){
      if( RATE_INTERVAL > now - (time_t)(p->next->timestamp) ){
        time_t reftime = nzero ? now : (time_t)(p->next->timestamp);
        while( (time_t)(p->next->timestamp) > (time_t)(p->timestamp) + 1 ){
          // fill holes
          if( BWSAMPLE *q = NewSample() ){
            q->timestamp = (double)((time_t)(p->next->timestamp) - 1);
            q->next = p->next;
            p->next = q;
            nzero++;
          }else break;
        }
        if( nzero ){
          size_t bytes = (size_t)( p->bytes / (reftime - p->timestamp) );
          BWSAMPLE *q = p->next;
          for( ; nzero; nzero-- ){
            q->bytes += bytes;  // distribute over the following empty samples
            q = q->next;
          }
        }
      }
      m_history = p->next;
      delete p;
      p = m_history;
    }
  }
}

void Rate::CountAdd(size_t nbytes)
{
  m_count_bytes += nbytes;
  if( m_selfrate ) m_selfrate->CountAdd(nbytes);
}

void Rate::UnCount(size_t nbytes)
{
  m_count_bytes -= nbytes;
  if( m_selfrate ) m_selfrate->UnCount(nbytes);
}

void Rate::RateAdd(size_t nbytes, size_t bwlimit)
{
  struct timespec nowspec;

  clock_gettime(CLOCK_REALTIME, &nowspec);

  RateAdd(nbytes, bwlimit,
    nowspec.tv_sec + (double)(nowspec.tv_nsec)/1000000000);
}

void Rate::RateAdd(size_t nbytes, size_t bwlimit, double timestamp)
{
  int update_nominal = 0;

  if( m_history_last && timestamp < m_history_last->timestamp ){
    // time went backward
    ClearHistory();
  }else Cleanup();
  if( timestamp <= m_last_realtime ){  // time went backward
    m_ontime = 0;
    m_last_size = 0;
    m_last_realtime = 0;
  }

  if( m_history_last &&
      (time_t)timestamp == (time_t)(m_history_last->timestamp) )
    m_history_last->bytes += nbytes;
  else{
    if( BWSAMPLE *p = NewSample() ){
      p->timestamp = timestamp;
      p->bytes = nbytes;
      if( m_history_last ){
        m_history_last->next = p;
        update_nominal = 1;
      }
      else m_history = p;
      m_history_last = p;
    }
  }

  if( !m_selfrate && m_ontime ){
    double late=timestamp - (m_last_realtime + (double)m_last_size / bwlimit);
//  double tmplate = late;
    // keep the change under control in case the system gets weird on us
    if( late < 0 ) late /= 2;
    else if( m_late && late > m_late ) late = m_late / 2;
    m_late += late;
//  CONSOLE.Debug("%p late %f->%f: %f", this, tmplate, late, m_late);
    m_ontime = 0;
  }

  if( m_selfrate && bwlimit && m_last_realtime && m_selfrate->LastSize() /
      (timestamp - m_selfrate->LastRealtime()) > bwlimit )
    m_last_size += nbytes;
  else if( !m_selfrate && bwlimit && m_last_realtime &&
           m_last_size / (timestamp - m_last_realtime) > bwlimit )
    m_last_size += nbytes;
  else{
    m_last_realtime = timestamp;
    m_last_size = nbytes;
  }

  if( nbytes > BUF_DEF_SIZ ){
    m_prev_realtime = m_recent_realtime;
    m_prev_size = m_recent_size;
    m_recent_realtime = timestamp;
    m_recent_size = nbytes;
  }else m_recent_size += nbytes;

  if( m_selfrate ){
    if( update_nominal ){
      m_update_nominal = 1;
      (void)RateMeasure();  // updates nominal
    }
    m_selfrate->RateAdd(nbytes, bwlimit, timestamp);
  }

//if(!m_selfrate) CONSOLE.Debug("%p RateAdd %u @ %f next=%f", this,
//  nbytes, timestamp, m_last_realtime + (double)m_last_size / bwlimit);
}

void Rate::operator=(const Rate &ra)
{
  m_last_timestamp = now;
  m_count_bytes = ra.m_count_bytes;
}

size_t Rate::CurrentRate()
{
  // We can't make up for past slowness by overloading the line now/future.
  // Look at only the most recent data sent/received.
  if( !m_last_timestamp || !m_history ) return 0; // no current rate

  struct timespec timestamp;
  clock_gettime(CLOCK_REALTIME, &timestamp);

  double timeused = timestamp.tv_sec + (double)(timestamp.tv_nsec)/1000000000 -
    m_last_realtime;
  if( timeused <= 0 ) return 0;

  return (size_t)( m_last_size / timeused );
}

size_t Rate::NominalRate()
{
  if( !m_history && m_last_timestamp && TimeUsed() > 10 ){
    // sent a request over 10 sec ago but have received nothing
    if( !m_nom_time || now >= m_nom_time + 10 ){
      m_nominal /= 10;
      m_nom_time = now;
    }
  }
  return m_nominal;
}

size_t Rate::RateMeasure()
{
  // calculate rate based on bandwidth history data
  time_t timestamp = now;
  unsigned long countbytes = 0;
  double timeused = 0;
  BWSAMPLE *p;

  if( m_history && now == m_lastrate.lasttime &&
      m_recent_realtime == m_lastrate.recent ){
    if( m_update_nominal ) m_nominal = m_lastrate.value;
    return m_lastrate.value;
  }

  m_lastrate.lasttime = now;
  if( !m_last_timestamp || !m_history ){
    m_lastrate.value = 0;
    return 0; // no current rate
  }

  Cleanup();
  for( p=m_history; p; p=p->next ){
    countbytes += p->bytes;
  }
  timeused = (double)(now - (time_t)(m_history->timestamp));
  if( timeused == 0 ) timeused = 1;
  else if( timeused < 0 ) ClearHistory();  // time went backward
  else m_update_nominal = 1;
  if( now < (time_t)m_recent_realtime ){
    if( m_history ){
      m_recent_realtime = (double)now;
      m_prev_realtime = (double)(now - 1);
      m_recent_size = m_prev_size = 0;
    }else{
      m_recent_realtime = m_prev_realtime = 0;
      m_recent_size = m_prev_size = 0;
    }
  }
  if( !m_history ){
    m_lastrate.value = 0;
    return 0;
  }

  // Don't let the most recent addition inflate the rate measurement.
  if( now == (time_t)m_recent_realtime ){
    // don't count the most recent addition
    countbytes -= m_recent_size;
    timeused = m_recent_realtime - m_history->timestamp;
  }else if( m_recent_realtime &&
            RATE_INTERVAL > now - (time_t)m_recent_realtime &&
            m_recent_size / (now - (time_t)m_recent_realtime) >
            m_prev_size / (m_recent_realtime - m_prev_realtime) ){
    // "tone down" the most recent to match the previous addition's rate
    countbytes -= m_recent_size;
    countbytes += (unsigned long)(
      m_prev_size / (m_recent_realtime - m_prev_realtime) *
      (now - (time_t)m_recent_realtime) );
  }

  m_lastrate.value = (size_t)(countbytes / timeused);
  m_lastrate.recent = m_recent_realtime;
  if( m_update_nominal ) m_nominal = m_lastrate.value;
  return m_lastrate.value;
}

size_t Rate::RateMeasure(const Rate &ra_to)
{
  time_t timeused = TimeUsed();
  int tmp = ra_to.m_count_bytes - m_count_bytes;
  return (size_t)( (tmp>0) ? (tmp/(timeused ? timeused : 1)) : 0 );
}

time_t Rate::TimeUsed()
{
  if( now < m_last_timestamp ) m_last_timestamp = now;
  return now - m_last_timestamp;
}