-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.c
More file actions
96 lines (81 loc) · 2.84 KB
/
Copy pathtest.c
File metadata and controls
96 lines (81 loc) · 2.84 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
// 直接包含 bbr.c 实现文件,省略头文件
#include "bbrlite/bbrlite.c"
#define HISTORY_SIZE 100
#define NUM_BBRS 10
#define MSS 1500
/**
* The following are simulated attribute values
*/
// 产生约10%概率的bool
static bool rand_bool_10pct() {
return (rand() % 10) == 0;
}
// 产生约20%概率的bool
static bool rand_bool_20pct() {
return (rand() % 5) == 0;
}
// 模拟带宽(单位:scaled 带宽,1<<24是单位),范围 1~10Mbps
static uint32_t rand_bw() {
return ((uint32_t)(1000 + rand() % 9000)) << BW_SCALE;
}
// 模拟RTT,单位微秒,10~100ms
static uint32_t rand_rtt() {
return 10000 + rand() % 90000;
}
// 模拟acked样本,单位任意,1~20000
static uint32_t rand_acked() {
return 1 + rand() % 20000;
}
int main(void) {
srand((unsigned int)time(NULL));
// 预分配每个BBR实例的缓冲区
// Pre allocate buffer for each BBR instance
uint32_t bw_bufs[NUM_BBRS][HISTORY_SIZE];
uint32_t rtt_bufs[NUM_BBRS][HISTORY_SIZE];
uint32_t acked_bufs[NUM_BBRS][HISTORY_SIZE];
bool loss_bufs[NUM_BBRS][HISTORY_SIZE];
bool app_limited_bufs[NUM_BBRS][HISTORY_SIZE];
bbr_t bbrs[NUM_BBRS];
// 初始化每个BBR实例
// Init
for (int i = 0; i < NUM_BBRS; i++) {
bbr_init(&bbrs[i], bw_bufs[i], rtt_bufs[i], acked_bufs[i], loss_bufs[i], app_limited_bufs[i], HISTORY_SIZE);
}
// 模拟10秒,每秒生成随机样本,调用bbr_update并打印状态
// Simulate for 10 seconds, generate random samples per second, call `bbr_update` and print status
for (int sec = 0; sec < 10; sec++) {
printf("== Time %d s ==\n", sec);
for (int i = 0; i < NUM_BBRS; i++) {
// 追加随机样本
// Add random samples
bbr_append_bw(&bbrs[i], rand_bw());
bbr_append_rtt(&bbrs[i], rand_rtt());
bbr_append_acked(&bbrs[i], rand_acked());
bbr_append_loss(&bbrs[i], rand_bool_10pct());
bbr_append_app_limited(&bbrs[i], rand_bool_20pct());
// 更新内部状态
// Update internal status
bbr_update(&bbrs[i]);
// 获取建议速率和窗口
// Get suggested speed and window
uint64_t pacing_rate = bbr_pacing_rate(&bbrs[i], MSS);
uint32_t cwnd = bbr_cwnd(&bbrs[i]);
// 输出
// Output
printf("BBR %d: mode=%d, bw_hi=%u, min_rtt=%u, pacing_rate=%llu, cwnd=%u\n",
i,
(int)bbrs[i].mode,
bbrs[i].bw_hi >> BW_SCALE, // 以Mbps简单表示
bbrs[i].min_rtt_us,
pacing_rate,
cwnd);
}
printf("\n");
sleep(1);
}
return 0;
}