-
Notifications
You must be signed in to change notification settings - Fork 23
/
bthrottler.c
204 lines (187 loc) · 7.05 KB
/
bthrottler.c
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
/*
Copyright (c) 2017 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <libdillimpl.h>
#include <stdint.h>
#include <stdlib.h>
#include "dsock.h"
#include "iol.h"
#include "utils.h"
dsock_unique_id(bthrottler_type);
static void *bthrottler_hquery(struct hvfs *hvfs, const void *type);
static void bthrottler_hclose(struct hvfs *hvfs);
static int bthrottler_bsendl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline);
static int bthrottler_brecvl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline);
struct bthrottler_sock {
struct hvfs hvfs;
struct bsock_vfs bvfs;
int s;
size_t send_full;
size_t send_remaining;
int64_t send_interval;
int64_t send_last;
size_t recv_full;
size_t recv_remaining;
int64_t recv_interval;
int64_t recv_last;
};
static void *bthrottler_hquery(struct hvfs *hvfs, const void *type) {
struct bthrottler_sock *obj = (struct bthrottler_sock*)hvfs;
if(type == bsock_type) return &obj->bvfs;
if(type == bthrottler_type) return obj;
errno = ENOTSUP;
return NULL;
}
int bthrottler_attach(int s,
uint64_t send_throughput, int64_t send_interval,
uint64_t recv_throughput, int64_t recv_interval) {
int err;
if(dsock_slow(send_throughput != 0 && send_interval <= 0 )) {
err = EINVAL; goto error1;}
if(dsock_slow(recv_throughput != 0 && recv_interval <= 0 )) {
err = EINVAL; goto error1;}
/* Check whether underlying socket is a bytestream. */
if(dsock_slow(!hquery(s, bsock_type))) {err = errno; goto error1;}
/* Create the object. */
struct bthrottler_sock *obj = malloc(sizeof(struct bthrottler_sock));
if(dsock_slow(!obj)) {err = ENOMEM; goto error1;}
obj->hvfs.query = bthrottler_hquery;
obj->hvfs.close = bthrottler_hclose;
obj->bvfs.bsendl = bthrottler_bsendl;
obj->bvfs.brecvl = bthrottler_brecvl;
obj->s = -1;
obj->send_full = 0;
if(send_throughput > 0) {
obj->send_full = send_throughput * send_interval / 1000;
obj->send_remaining = obj->send_full;
obj->send_interval = send_interval;
obj->send_last = now();
}
obj->recv_full = 0;
if(recv_throughput > 0) {
obj->recv_full = recv_throughput * recv_interval / 1000;
obj->recv_remaining = obj->recv_full;
obj->recv_interval = recv_interval;
obj->recv_last = now();
}
/* Create the handle. */
int h = hmake(&obj->hvfs);
if(dsock_slow(h < 0)) {err = errno; goto error2;}
/* Make a private copy of the underlying socket. */
obj->s = hdup(s);
if(dsock_slow(obj->s < 0)) {err = errno; goto error3;}
int rc = hclose(s);
dsock_assert(rc == 0);
return h;
error3:
rc = hclose(h);
dsock_assert(rc == 0);
error2:
free(obj);
error1:
errno = err;
return -1;
}
int bthrottler_detach(int s) {
struct bthrottler_sock *obj = hquery(s, bthrottler_type);
if(dsock_slow(!obj)) return -1;
int u = obj->s;
free(obj);
return u;
}
static int bthrottler_bsendl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline) {
struct bthrottler_sock *obj =
dsock_cont(bvfs, struct bthrottler_sock, bvfs);
/* If send-throttling is off forward the call. */
if(obj->send_full == 0) return bsendl(obj->s, first, last, deadline);
/* Get rid of the corner case. */
size_t bytes;
int rc = iol_check(first, last, NULL, &bytes);
if(dsock_slow(rc < 0)) return -1;
if(dsock_slow(bytes == 0)) return 0;
size_t pos = 0;
while(1) {
/* If there's capacity send as much data as possible. */
if(obj->send_remaining) {
size_t tosend = bytes < obj->send_remaining ?
bytes : obj->send_remaining;
struct iol_slice slc;
iol_slice_init(&slc, first, last, pos, tosend);
int rc = bsendl(obj->s, &slc.first, slc.last, deadline);
iol_slice_term(&slc);
if(dsock_slow(rc < 0)) return -1;
obj->send_remaining -= tosend;
pos += tosend;
bytes -= tosend;
if(bytes == 0) return 0;
}
/* Wait till capacity can be renewed. */
int rc = msleep(obj->send_last + obj->send_interval);
if(dsock_slow(rc < 0)) return -1;
/* Renew the capacity. */
obj->send_remaining = obj->send_full;
obj->send_last = now();
}
}
static int bthrottler_brecvl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline) {
struct bthrottler_sock *obj =
dsock_cont(bvfs, struct bthrottler_sock, bvfs);
/* If recv-throttling is off forward the call. */
if(obj->recv_full == 0) return brecvl(obj->s, first, last, deadline);
/* Get rid of the corner case. */
size_t bytes;
int rc = iol_check(first, last, NULL, &bytes);
if(dsock_slow(rc < 0)) return -1;
if(dsock_slow(bytes == 0)) return 0;
size_t pos = 0;
while(1) {
/* If there's capacity receive as much data as possible. */
if(obj->recv_remaining) {
size_t torecv = bytes < obj->recv_remaining ?
bytes : obj->recv_remaining;
struct iol_slice slc;
iol_slice_init(&slc, first, last, pos, torecv);
int rc = brecvl(obj->s, &slc.first, slc.last, deadline);
iol_slice_term(&slc);
if(dsock_slow(rc < 0)) return -1;
obj->recv_remaining -= torecv;
pos += torecv;
bytes -= torecv;
if(bytes == 0) return 0;
}
/* Wait till capacity can be renewed. */
int rc = msleep(obj->recv_last + obj->recv_interval);
if(dsock_slow(rc < 0)) return -1;
/* Renew the capacity. */
obj->recv_remaining = obj->recv_full;
obj->recv_last = now();
}
}
static void bthrottler_hclose(struct hvfs *hvfs) {
struct bthrottler_sock *obj = (struct bthrottler_sock*)hvfs;
if(dsock_fast(obj->s >= 0)) {
int rc = hclose(obj->s);
dsock_assert(rc == 0);
}
free(obj);
}