-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBandwidthLimit.cc
52 lines (41 loc) · 1.06 KB
/
BandwidthLimit.cc
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
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Yao Yuan <yuan.yao@intel.com>
*/
#include <unistd.h>
#include <string.h>
#include <vector>
#include <thread>
#include <iostream>
#include "BandwidthLimit.h"
#include "lib/stats.h"
const float BandwidthLimit::MAX_TIME_HISTORY = 3;
void BandwidthLimit::add_and_sleep(unsigned long bytes)
{
float time_delta;
timeval cur_time;
if (0 == bwlimit_byteps)
return;
if (!last_time.tv_sec && !last_time.tv_usec) {
allow_bytes = 0;
gettimeofday(&last_time, NULL);
return;
}
std::unique_lock<std::mutex> lock(mlock);
gettimeofday(&cur_time, NULL);
time_delta = tv_secs(last_time, cur_time);
if (time_delta > MAX_TIME_HISTORY)
time_delta = MAX_TIME_HISTORY;
last_time = cur_time;
allow_bytes += time_delta * bwlimit_byteps;
allow_bytes -= bytes;
if (allow_bytes < 0) {
float sleep_time;
sleep_time = ((-allow_bytes) / bwlimit_byteps) * 1000000;
//printf("thread will sleep %f microseconds\n", sleep_time);
usleep(sleep_time);
}
}