-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler.cc
295 lines (226 loc) · 8.7 KB
/
scheduler.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
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
/*
* A library for working with DVSI's AMBE vocoder chips
*
* Copyright (C) 2019-2020 Internet Real-Time Lab, Columbia University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "scheduler.h"
#include <iostream>
#include <mutex>
#include <cstring>
#include <memory>
#include <list>
#include "api.h"
using namespace std::placeholders;
using namespace ambe;
using namespace std;
future<Packet> Scheduler::submit(const Packet& packet) {
auto rv = make_shared<promise<Packet>>();
auto future = rv->get_future();
auto callback = [rv](const Packet& response) {
rv->set_value(move(response));
};
submitAsync(packet, callback);
return future;
}
FifoScheduler::FifoScheduler(TaggingDevice& device) : device(device) {
}
void FifoScheduler::start() {
quit = false;
tag = 0;
terminated = promise<void>();
device.setCallback(bind(&FifoScheduler::recv, this, _1, _2));
}
void FifoScheduler::stop() {
unique_lock<std::mutex> lock(mutex);
// If we have outstanding requests on the queue, wait for all of them to get
// closed. Set a flag and a promise for the receiving thread to resolve once
// the queue is empty.
if (!submitted.empty()) {
quit = true;
auto future = terminated.get_future();
lock.unlock();
future.get();
}
device.setCallback(nullptr);
}
void FifoScheduler::submitAsync(const Packet& packet, ResponseCallback callback) {
// Note: we can lock the mutex before device.send() because the method is
// guaranteed to be non-blocking, i.e., it will not block until the packet
// is written to the device.
lock_guard<std::mutex> lock(mutex);
try {
device.send(++tag, packet.data());
} catch(...) {
callback(Packet());
}
submitted[tag] = callback;
}
void FifoScheduler::recv(int32_t tag, const string& packet) {
lock_guard<std::mutex> lock(mutex);
auto v = submitted.find(tag);
if (v == submitted.end()) {
cerr << "Warning: Received response with unknown tag" << endl;
return;
}
v->second(Packet(move(packet), device.uses_parity, false));
submitted.erase(v);
if (quit && submitted.empty())
terminated.set_value();
}
MultiQueueScheduler::MultiQueueScheduler(FifoDevice& device, unsigned int channels) : device(device) {
if (channels > max_channels)
throw std::logic_error("Invalid number of channels: " + to_string(channels));
this->channels = channels;
int queues = channels * queues_per_channel;
channel_queue = vector<queue<State>>(queues);
submitted_by_queue = vector<unsigned int>(queues, 0);
}
void MultiQueueScheduler::start() {
device.setCallback(bind(&MultiQueueScheduler::recv, this, _1));
runner = thread(&MultiQueueScheduler::run, this);
}
void MultiQueueScheduler::stop() {
// Terminate the background thread by giving it an empty packet to transmit.
// Wait until we get a response. That indicates that all requests buffered
// before this one have been completed.
Scheduler::submit(Packet()).get();
runner.join();
device.setCallback(nullptr);
}
void MultiQueueScheduler::submitAsync(const Packet& packet, ResponseCallback callback) {
process.push(make_tuple(packet, move(callback)));
}
// The recv method will be called on whatever thread the device uses to receive
// packets.
void MultiQueueScheduler::recv(const string& packet) {
process.push(make_tuple(Packet(move(packet), device.uses_parity, false), nullopt));
}
unsigned int MultiQueueScheduler::typeIndex(const Packet& request) const {
// Control packets always get 0. These are either packets for the entire
// chip (such packets carry no channel information), or they will be queued
// in the first queue corresponding to the channel.
switch(request.type()) {
case PacketType::CHANNEL: return 1;
case PacketType::SPEECH: return 0;
case PacketType::CONTROL: return 0;
default: throw logic_error{"Unsupported packet type"};
}
}
int MultiQueueScheduler::queueIndex(const Packet& request) const {
int channel = request.channel();
// If we get a -1 then the packet does not have a channel field and it is
// probably a packet for the whole device (e.g., something like AMBE_RESET).
// If that's the case, indicate to the caller to put the packet into the
// per-device queue by returning -1.
if (channel == -1) return -1;
return queues_per_channel * channel + (typeIndex(request));
}
bool MultiQueueScheduler::canSend(const Packet& request) const {
// The input buffer can store up to four packets. Two of those can be SPEECH
// packets and two can be CHANNEL packets. Thus, the maximum number of
// packets that can be submitted to the chip at any time is the number of
// channel queues (one packet from each can be processing) plus four
// additional packets.
if (submitted.size() >= channel_queue.size() + 4) return false;
// The input buffer is big enough to store only two CHANNEL and two SPEECH
// packets at the same time. Thus, the maximum number of either CHANNEL or
// SPEECH packets that can be submitted at any time is the number of
// channels (each channel can be processing one) plus two. Control packets
// are lumped together with SPEECH packets since such packets are processed
// immediately and don't keep the chip busy.
if (submitted_by_type[typeIndex(request)] >= channels + 2) return false;
// If any channel runs out of data, the above two checks will overcommit and
// might write too many packets into the input buffer. Here we also make
// sure that, at any given time, no more than 2 packets per queue have been
// submitted. The CPU core can be processing one and the other packet will
// be waiting in the input buffer.
int i = queueIndex(request);
if (i > 0 && (submitted_by_queue[i] >= 2)) return false;
// If all the above conditions are satisfied, we can submit the given packet
// to the AMBE chip.
return true;
}
unsigned int MultiQueueScheduler::queued() const {
unsigned int rv = device_queue.size();
for(const auto& q : channel_queue) rv += q.size();
return rv;
}
void MultiQueueScheduler::run() {
unsigned int next = 0;
bool quit = false;
unsigned int queued = 0;
optional<ResponseCallback> terminated;
while (!quit || queued || submitted.size()) {
auto tuple = move(process.pop());
const auto& packet = get<0>(tuple);
auto& callback = get<1>(tuple);
if (!packet.payloadLength()) {
// If it is an empty packet, set a flag to terminate once all
// data has been processed and discard it.
quit = true;
// Notify the stop method once the thread has stopped
if (callback) terminated = callback;
} else if (callback) {
// We got a new request to transmit to the AMBE chip. File it in
// the appropriate queue.
auto i = queueIndex(packet);
if (i == -1) device_queue.push(move(tuple));
else channel_queue[i].push(move(tuple));
queued++;
} else {
// We got a new response from the AMBE chip
if (!submitted.empty()) {
auto& tuple = submitted.front();
const auto& request = get<0>(tuple);
auto& callback = get<1>(tuple);
int i = queueIndex(request);
if (i != -1) {
submitted_by_type[typeIndex(request)]--;
submitted_by_queue[queueIndex(request)]--;
}
// If we have (an optional) promise associated with the request,
// fullfill it with the response packet that we just received.
if (callback) callback.value()(packet);
submitted.pop();
}
}
// First transmit any packets on the high-priority queue 0. Those are
// control packets for the entire AMBE device (and not for a specific
// channel).
while(!device_queue.empty()) {
const auto& request = get<0>(device_queue.front());
if (!canSend(request)) break;
device.send(request.data());
submitted.push(move(device_queue.front()));
device_queue.pop();
queued--;
}
unsigned int queues = channel_queue.size();
for (unsigned int j = 0; (j < queues) && queued; j++, next = (next + 1) % queues) {
if (channel_queue[next].empty()) continue;
const auto& request = get<0>(channel_queue[next].front());
if (!canSend(request)) continue;
device.send(request.data());
submitted_by_type[typeIndex(request)]++;
submitted_by_queue[queueIndex(request)]++;
submitted.push(move(channel_queue[next].front()));
channel_queue[next].pop();
queued--;
j = 0;
}
}
if (terminated) terminated.value()(Packet());
}