-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamspeedtest.c
295 lines (250 loc) · 7.9 KB
/
streamspeedtest.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
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
/**
* stream-speed-test.c
*
* Copyright (c) 2017 TiePie engineering
*
* 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <inttypes.h>
#include <math.h>
#ifdef __WINNT__
#include <windows.h>
#else
#include <poll.h>
#include <sys/eventfd.h>
#endif
#include <string.h>
#include <libtiepie.h>
#define EVENT_COUNT 3
#define DEVICE_GONE \
{ \
fprintf(stderr, "Device gone!\n"); \
status = EXIT_FAILURE; \
goto exit; \
}
#define DATA_READY \
{ \
if(raw) \
ScpGetDataRaw(scp, buffers, channel_count, 0, record_length); \
else \
ScpGetData(scp, (float**)buffers, channel_count, 0, record_length); \
if(LibGetLastStatus() < LIBTIEPIESTATUS_SUCCESS) \
{ \
fprintf(stderr, "%s failed: %s\n", raw ? "ScpGetDataRaw" : "ScpGetData", LibGetLastStatusStr()); \
status = EXIT_FAILURE; \
goto exit; \
} \
n++; \
printf("\r%0.1f %%", 100.0 * n / num_blocks); \
fflush(stdout); \
}
#define DATA_OVERFLOW \
{ \
fprintf(stderr, "Data overflow!\n"); \
status = EXIT_FAILURE; \
goto exit; \
}
int main(int argc, char* argv[])
{
int status = EXIT_SUCCESS;
unsigned int resolution = 0;
unsigned int active_channel_count = 0;
double sample_frequency = 10e3; // 10 kHz
uint64_t record_length = 5000; // 5 kS
double duration = 60; // 60 seconds
bool raw = false;
uint32_t serial_number = 0;
{
int opt;
while((opt = getopt(argc, argv, "b:c:f:l:d:s:rh")) != -1)
{
switch(opt)
{
case 'b':
sscanf(optarg, "%u", &resolution);
break;
case 'c':
sscanf(optarg, "%u", &active_channel_count);
break;
case 'f':
sscanf(optarg, "%lf", &sample_frequency);
break;
case 'l':
sscanf(optarg, "%" PRIu64, &record_length);
break;
case 'd':
sscanf(optarg, "%lf", &duration);
break;
case 's':
sscanf(optarg, "%" PRIu32, &serial_number);
break;
case 'r':
raw = true;
break;
case 'h':
default:
fprintf(stderr, "Usage: %s [-b resolution] [-c active channel count] [-f sample frequency] [-l record length] [-d duration] [-s serial number] [-r]\n", argv[0]);
exit(opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE);
}
}
}
//===========================================================================
TpVersion_t version = LibGetVersion();
printf("libtiepie v%u.%u.%u.%u%s\n" ,
(uint16_t)TPVERSION_MAJOR(version) ,
(uint16_t)TPVERSION_MINOR(version) ,
(uint16_t)TPVERSION_RELEASE(version) ,
(uint16_t)TPVERSION_BUILD(version) ,
LibGetVersionExtra());
LibInit();
// Open device:
LstUpdate();
LibTiePieHandle_t scp;
if(serial_number != 0)
scp = LstOpenOscilloscope(IDKIND_SERIALNUMBER, serial_number);
else
scp = LstOpenOscilloscope(IDKIND_INDEX, 0);
if(scp == LIBTIEPIE_HANDLE_INVALID)
{
fprintf(stderr, "LstOpenOscilloscope failed: %s\n", LibGetLastStatusStr());
status = EXIT_FAILURE;
goto exit_no_mem;
}
const uint16_t channel_count = ScpGetChannelCount(scp);
// Setup device:
ScpSetMeasureMode(scp, MM_STREAM);
if(active_channel_count == 0 || active_channel_count > channel_count)
active_channel_count = channel_count;
printf("Active channel count: %u\n", active_channel_count);
for(uint16_t i = 0; i < channel_count; i++)
printf(" Ch%u: %s\n", i + 1, ScpChSetEnabled(scp, i, i < active_channel_count ? BOOL8_TRUE : BOOL8_FALSE) ? "enabled" : "disabled");
if(resolution != 0)
ScpSetResolution(scp, resolution);
sample_frequency = ScpSetSampleFrequency(scp, sample_frequency);
printf("Sample frequency: %f MHz\n", sample_frequency / 1e6);
printf("Resolution: %u bit\n", ScpGetResolution(scp));
record_length = ScpSetRecordLength(scp, record_length);
printf("Record length: %" PRIu64 " Samples\n", record_length);
printf("Data rate: %f MB/s\n", (active_channel_count * ceil(ScpGetResolution(scp) / 8.0) * sample_frequency) / 1e6);
unsigned int num_blocks = ceil(duration * sample_frequency / record_length);
duration = (record_length * num_blocks) / sample_frequency;
printf("Duration: %f s\n", duration);
printf("Data type: %s\n", raw ? "raw" : "float");
{
// Alloc memory:
const size_t sample_size = raw ? ceil(ScpGetResolution(scp) / 8.0) : sizeof(float);
void* buffers[channel_count];
for(uint16_t i = 0; i < channel_count; i++)
if(i < active_channel_count)
buffers[i] = malloc(record_length * sample_size);
else
buffers[i] = NULL;
// Setup events:
#ifdef __WINNT__
HANDLE events[EVENT_COUNT] = {
CreateEventA(NULL, FALSE, FALSE, NULL),
CreateEventA(NULL, FALSE, FALSE, NULL),
CreateEventA(NULL, FALSE, FALSE, NULL)
};
DevSetEventRemoved(scp, events[0]);
ScpSetEventDataReady(scp, events[1]);
ScpSetEventDataOverflow(scp, events[2]);
#else
int fd_removed = eventfd(0, EFD_NONBLOCK);
int fd_data_ready = eventfd(0, EFD_NONBLOCK);
int fd_data_overflow = eventfd(0, EFD_NONBLOCK);
DevSetEventRemoved(scp, fd_removed);
ScpSetEventDataReady(scp, fd_data_ready);
ScpSetEventDataOverflow(scp, fd_data_overflow);
struct pollfd fds[EVENT_COUNT] = {
{fd: fd_removed, events: POLLIN, revents: 0},
{fd: fd_data_ready, events: POLLIN, revents: 0},
{fd: fd_data_overflow, events: POLLIN, revents: 0}
};
#endif
if(!ScpStart(scp))
{
fprintf(stderr, "ScpStart failed: %s\n", LibGetLastStatusStr());
status = EXIT_FAILURE;
goto exit;
}
unsigned int n = 0;
#ifdef __WINNT__
DWORD r;
while(n < num_blocks && (r = WaitForMultipleObjects(EVENT_COUNT, events, FALSE, INFINITE)) >= WAIT_OBJECT_0)
{
if(r == WAIT_OBJECT_0)
{
DEVICE_GONE
}
else if(r == WAIT_OBJECT_0 + 1)
{
DATA_READY
}
else if(r == WAIT_OBJECT_0 + 2)
{
DATA_OVERFLOW
}
else
{
fprintf(stderr, "WaitForMultipleObjects() returned: %lu\n", r);
status = EXIT_FAILURE;
goto exit;
}
}
#else
int r;
while(n < num_blocks && (r = poll(fds, EVENT_COUNT, -1)) >= 0)
{
for(int i = 0; i < EVENT_COUNT; i++)
{
if(fds[i].revents & POLLIN)
{
eventfd_t tmp;
eventfd_read(fds[i].fd, &tmp);
if(fds[i].fd == fd_removed)
{
DEVICE_GONE
}
if(fds[i].fd == fd_data_overflow)
{
DATA_OVERFLOW
}
if(fds[i].fd == fd_data_ready)
{
DATA_READY
}
}
fds[i].revents = 0;
}
}
#endif
ScpStop(scp);
exit:
for(uint16_t i = 0; i < channel_count; i++)
free(buffers[i]);
}
exit_no_mem:
LibExit();
return status;
}