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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
//
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file contains a set of benchmarks for the various portions of the
// streamer implementation.
// Disable sequence checking for recv packet handler so that the benchmark
// code does not need to create new mock packet contents in every recv call.
// This should have very little effect on packet handler performance.
#define SRPH_DONT_CHECK_SEQUENCE 1
#include "common/mock_zero_copy.hpp"
#include "../lib/transport/super_send_packet_handler.hpp"
#include "../lib/transport/super_recv_packet_handler.hpp"
#include "../lib/usrp/device3/device3_flow_ctrl.hpp"
#include <uhd/utils/safe_main.hpp>
#include <uhd/utils/thread.hpp>
#include <uhd/convert.hpp>
#include <uhd/transport/chdr.hpp>
#include <uhd/transport/zero_copy.hpp>
#include <uhd/types/sid.hpp>
#include <boost/program_options.hpp>
#include <chrono>
#include <vector>
namespace po = boost::program_options;
using namespace uhd::transport;
using namespace uhd::usrp;
void benchmark_recv_packet_handler(
const size_t spp,
const std::string& format
) {
const size_t bpi = uhd::convert::get_bytes_per_item(format);
const size_t frame_size = bpi * spp + DEVICE3_RX_MAX_HDR_LEN;
mock_zero_copy::sptr xport(
new mock_zero_copy(
vrt::if_packet_info_t::LINK_TYPE_CHDR,
frame_size,
frame_size));
xport->set_reuse_recv_memory(true);
sph::recv_packet_streamer streamer(spp);
streamer.set_vrt_unpacker(&vrt::chdr::if_hdr_unpack_be);
streamer.set_tick_rate(1.0);
streamer.set_samp_rate(1.0);
uhd::convert::id_type id;
id.output_format = format;
id.num_inputs = 1;
id.input_format = "sc16_item32_be";
id.num_outputs = 1;
streamer.set_converter(id);
streamer.set_xport_chan_get_buff(
0,
[xport](double timeout) {
return xport->get_recv_buff(timeout);
},
false // flush
);
// Create packet for packet handler to read
vrt::if_packet_info_t packet_info;
packet_info.packet_type = vrt::if_packet_info_t::PACKET_TYPE_DATA;
packet_info.num_payload_words32 = spp;
packet_info.num_payload_bytes = packet_info.num_payload_words32*sizeof(uint32_t);
packet_info.has_tsf = true;
packet_info.tsf = 1;
std::vector<uint32_t> recv_data(spp, 0);
xport->push_back_recv_packet(packet_info, recv_data);
// Allocate buffer
std::vector<uint8_t> buffer(spp*bpi);
std::vector<void*> buffers;
buffers.push_back(buffer.data());
// Run benchmark
uhd::rx_metadata_t md;
const auto start_time = std::chrono::steady_clock::now();
const size_t iterations = 1e7;
for (size_t i = 0; i < iterations; i++) {
streamer.recv(buffers, spp, md, 1.0, true);
}
const auto end_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed_time(end_time-start_time);
const double time_per_packet = elapsed_time.count() / iterations;
std::cout << format << ": "
<< time_per_packet / spp * 1e9
<< " ns/sample, "
<< time_per_packet * 1e9
<< " ns/packet\n";
}
void benchmark_send_packet_handler(
const size_t spp,
const std::string& format,
bool use_time_spec
) {
const size_t bpi = uhd::convert::get_bytes_per_item(format);
const size_t frame_size = bpi * spp + DEVICE3_TX_MAX_HDR_LEN;
mock_zero_copy::sptr xport(
new mock_zero_copy(
vrt::if_packet_info_t::LINK_TYPE_CHDR,
frame_size,
frame_size));
xport->set_reuse_send_memory(true);
sph::send_packet_streamer streamer(spp);
streamer.set_vrt_packer(&vrt::chdr::if_hdr_pack_be);
uhd::convert::id_type id;
id.input_format = format;
id.num_inputs = 1;
id.output_format = "sc16_item32_be";
id.num_outputs = 1;
streamer.set_converter(id);
streamer.set_enable_trailer(false);
streamer.set_xport_chan_get_buff(
0,
[xport](double timeout) {
return xport->get_send_buff(timeout);
});
// Allocate buffer
std::vector<uint8_t> buffer(spp*bpi);
std::vector<void*> buffers;
buffers.push_back(buffer.data());
// Run benchmark
uhd::tx_metadata_t md;
md.has_time_spec = use_time_spec;
const auto start_time = std::chrono::steady_clock::now();
const size_t iterations = 1e7;
for (size_t i = 0; i < iterations; i++) {
if (use_time_spec) {
md.time_spec = uhd::time_spec_t(i, 0.0);
}
streamer.send(buffers, spp, md, 1.0);
}
const auto end_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed_time(end_time-start_time);
const double time_per_packet = elapsed_time.count() / iterations;
std::cout << format << ": "
<< time_per_packet / spp * 1e9
<< " ns/sample, "
<< time_per_packet * 1e9
<< " ns/packet\n";
}
void benchmark_device3_rx_flow_ctrl(
bool send_flow_control_packet
) {
// Arbitrary sizes
constexpr uint32_t fc_window = 10000;
mock_zero_copy::sptr xport(
new mock_zero_copy(
vrt::if_packet_info_t::LINK_TYPE_CHDR));
xport->set_reuse_recv_memory(true);
xport->set_reuse_send_memory(true);
boost::shared_ptr<rx_fc_cache_t> fc_cache(new rx_fc_cache_t());
fc_cache->to_host = uhd::ntohx<uint32_t>;
fc_cache->from_host = uhd::htonx<uint32_t>;
fc_cache->pack = vrt::chdr::if_hdr_pack_be;
fc_cache->unpack = vrt::chdr::if_hdr_unpack_be;
fc_cache->xport = xport;
fc_cache->interval = fc_window;
// Create data buffer to pass to flow control function. Number of payload
// words is arbitrary, just has to fit in the buffer.
vrt::if_packet_info_t packet_info;
packet_info.packet_type = vrt::if_packet_info_t::PACKET_TYPE_DATA;
packet_info.num_payload_words32 = 100;
packet_info.num_payload_bytes = packet_info.num_payload_words32*sizeof(uint32_t);
packet_info.has_tsf = false;
std::vector<uint32_t> recv_data(packet_info.num_payload_words32, 0);
xport->push_back_recv_packet(packet_info, recv_data);
auto recv_buffer = xport->get_recv_buff(1.0);
// Run benchmark
const auto start_time = std::chrono::steady_clock::now();
constexpr size_t iterations = 1e7;
for (size_t i = 0; i < iterations; i++) {
fc_cache->total_bytes_consumed = send_flow_control_packet? fc_window:0;
fc_cache->last_byte_count = 0;
rx_flow_ctrl(fc_cache, recv_buffer);
}
const auto end_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed_time(end_time-start_time);
std::cout << elapsed_time.count() / iterations * 1e9
<< " ns per call\n";
}
void benchmark_device3_handle_rx_flow_ctrl_ack() {
// Arbitrary sizes
constexpr uint32_t fc_window = 10000;
mock_zero_copy::sptr xport(
new mock_zero_copy(
vrt::if_packet_info_t::LINK_TYPE_CHDR));
xport->set_reuse_recv_memory(true);
xport->set_reuse_send_memory(true);
boost::shared_ptr<rx_fc_cache_t> fc_cache(new rx_fc_cache_t());
fc_cache->to_host = uhd::ntohx<uint32_t>;
fc_cache->from_host = uhd::htonx<uint32_t>;
fc_cache->pack = vrt::chdr::if_hdr_pack_be;
fc_cache->unpack = vrt::chdr::if_hdr_unpack_be;
fc_cache->xport = xport;
fc_cache->interval = fc_window;
fc_cache->total_bytes_consumed = 100;
// Payload should contain packet count and byte count
std::vector<uint32_t> payload_data;
payload_data.push_back(fc_cache->to_host(10)); // packet count
payload_data.push_back(fc_cache->to_host(100)); // byte count
// Run benchmark
const auto start_time = std::chrono::steady_clock::now();
constexpr size_t iterations = 1e7;
for (size_t i = 0; i < iterations; i++) {
handle_rx_flowctrl_ack(fc_cache, payload_data.data());
}
const auto end_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed_time(end_time-start_time);
std::cout << elapsed_time.count() / iterations * 1e9
<< " ns per call\n";
}
void benchmark_device3_tx_flow_ctrl(
bool send_flow_control_packet
) {
// Arbitrary sizes
constexpr uint32_t fc_window = 10000;
mock_zero_copy::sptr xport(
new mock_zero_copy(
vrt::if_packet_info_t::LINK_TYPE_CHDR));
xport->set_reuse_recv_memory(true);
boost::shared_ptr<tx_fc_cache_t>
fc_cache(new tx_fc_cache_t(fc_window));
fc_cache->to_host = uhd::ntohx<uint32_t>;
fc_cache->from_host = uhd::htonx<uint32_t>;
fc_cache->pack = vrt::chdr::if_hdr_pack_be;
fc_cache->unpack = vrt::chdr::if_hdr_unpack_be;
xport->push_back_flow_ctrl_packet(
vrt::if_packet_info_t::PACKET_TYPE_FC,
1 /*packet*/,
fc_window /*bytes*/);
// Run benchmark
const auto start_time = std::chrono::steady_clock::now();
constexpr size_t iterations = 1e7;
managed_send_buffer::sptr send_buffer = xport->get_send_buff(0.0);
for (size_t i = 0; i < iterations; i++) {
fc_cache->byte_count = send_flow_control_packet? fc_window:0;
fc_cache->last_byte_ack = 0;
tx_flow_ctrl(fc_cache, xport, send_buffer);
}
const auto end_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed_time(end_time-start_time);
std::cout << elapsed_time.count() / iterations * 1e9
<< " ns per call\n";
}
void benchmark_device3_tx_flow_ctrl_ack() {
// Arbitrary sizes
constexpr uint32_t fc_window = 10000;
mock_zero_copy::sptr xport(
new mock_zero_copy(
vrt::if_packet_info_t::LINK_TYPE_CHDR));
xport->set_reuse_send_memory(true);
boost::shared_ptr<tx_fc_cache_t> fc_cache(new tx_fc_cache_t(fc_window));
fc_cache->to_host = uhd::ntohx<uint32_t>;
fc_cache->from_host = uhd::htonx<uint32_t>;
fc_cache->pack = vrt::chdr::if_hdr_pack_be;
fc_cache->unpack = vrt::chdr::if_hdr_unpack_be;
// Run benchmark
const auto start_time = std::chrono::steady_clock::now();
constexpr size_t iterations = 1e7;
uhd::sid_t send_sid;
for (size_t i = 0; i < iterations; i++) {
// Setup fc_cache to require an ack
fc_cache->fc_received = true;
tx_flow_ctrl_ack(fc_cache, xport, send_sid);
}
const auto end_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed_time(end_time-start_time);
std::cout << elapsed_time.count() / iterations * 1e9
<< " ns per call\n";
}
int UHD_SAFE_MAIN(int argc, char *argv[])
{
po::options_description desc("Allowed options");
desc.add_options()
("help", "help message")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
// Print the help message
if (vm.count("help")) {
std::cout << boost::format("UHD Packet Handler Benchmark %s") % desc << std::endl;
std::cout <<
" Benchmark of send and receive packet handlers and flow control\n"
" functions. All benchmarks use mock transport objects. No\n"
" parameters are needed to run this benchmark.\n"
<< std::endl;
return EXIT_FAILURE;
}
uhd::set_thread_priority_safe();
const char* formats[] = {"sc16", "fc32", "fc64" };
constexpr size_t rx_spp = 2000;
constexpr size_t tx_spp = 1000;
std::cout << "----------------------------------------------------------\n";
std::cout << "Benchmark of recv with no flow control and mock transport \n";
std::cout << "----------------------------------------------------------\n";
std::cout << "spp: " << rx_spp << "\n";
for (size_t i = 0; i < std::extent<decltype(formats)>::value; i++) {
benchmark_recv_packet_handler(rx_spp, formats[i]);
}
std::cout << "\n";
std::cout << "----------------------------------------------------------\n";
std::cout << "Benchmark of send with no flow control and mock transport \n";
std::cout << "----------------------------------------------------------\n";
std::cout << "spp: " << tx_spp << "\n";
std::cout << "*** without timespec ***\n";
for (size_t i = 0; i < std::extent<decltype(formats)>::value; i++) {
benchmark_send_packet_handler(tx_spp, formats[i], false);
}
std::cout << "\n";
std::cout << "*** with timespec ***\n";
for (size_t i = 0; i < std::extent<decltype(formats)>::value; i++) {
benchmark_send_packet_handler(tx_spp, formats[i], true);
}
std::cout << "\n";
std::cout << "----------------------------------------------------------\n";
std::cout << " Benchmark of flow control functions with mock transport \n";
std::cout << "----------------------------------------------------------\n";
std::cout << "*** device3_tx_flow_ctrl with no flow control packet ***\n";
benchmark_device3_tx_flow_ctrl(false);
std::cout << "\n";
std::cout << "*** device3_tx_flow_ctrl with flow control packet ***\n";
benchmark_device3_tx_flow_ctrl(true);
std::cout << "\n";
std::cout << "*** device3_tx_flow_ctrl_ack ***\n";
benchmark_device3_tx_flow_ctrl_ack();
std::cout << "\n";
std::cout << "*** device3_rx_flow_ctrl with no flow control packet ***\n";
benchmark_device3_rx_flow_ctrl(false);
std::cout << "\n";
std::cout << "*** device3_rx_flow_ctrl with flow control packet ***\n";
benchmark_device3_rx_flow_ctrl(true);
std::cout << "\n";
std::cout << "*** device3_handle_rx_flow_ctrl_ack ***\n";
benchmark_device3_handle_rx_flow_ctrl_ack();
std::cout << "\n";
return EXIT_SUCCESS;
}
|