-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcie_latency_benchmark.c
307 lines (252 loc) · 8.34 KB
/
pcie_latency_benchmark.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
296
297
298
299
300
301
302
303
304
305
306
/*
The MIT License (MIT)
Copyright (c) 2016 EnyxSA
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 <assert.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h>
#include <locale.h>
#include <pci/pci.h>
#include <pci/header.h>
#include <gsl/gsl_statistics_ulong.h>
#include "utils.h"
#define MAX_READ_SIZE 255lu
struct cmd_line
{
uint64_t iteration_count;
uint64_t wait_time_us;
uint64_t limit_ns;
char * slot;
};
static void
print_usage(const char * name)
{
fprintf(stderr, "usage: %s [options] PCIE_SLOT\n"
"where:\n"
"\t PCI_SLOT The slot identifier of the card\n"
"\t e.g. 04:00.0\n"
"\n"
"\t -h Display this usage message\n"
"\t -i ITERATION Define the read count\n"
"\t -w WAIT_TIME_US Define the wait between reads\n"
"\t -l LIMIT_NS Count the sample above this limit\n",
name);
}
static int
parse_cmd_line(int argc, char * argv[], struct cmd_line * cmd_line)
{
int err = -1;
cmd_line->iteration_count = 10 * 1000 * 1000;
cmd_line->wait_time_us = 0;
cmd_line->limit_ns = 0;
int i;
while ((i = getopt(argc, argv, "hi:w:l:")) != -1)
switch(i)
{
default:
case 'h':
print_usage(argv[0]);
goto arg_check_failed;
case 'i':
cmd_line->iteration_count = atoll(optarg);
break;
case 'w':
cmd_line->wait_time_us = atoll(optarg);
break;
case 'l':
cmd_line->limit_ns = atoll(optarg);
break;
}
cmd_line->slot = argv[optind];
if (! cmd_line->slot)
{
print_usage(argv[0]);
goto arg_check_failed;
}
err = 0;
arg_check_failed:
return err;
}
static struct pci_dev *
create_pci_dev(struct pci_access * pci, char * slot)
{
struct pci_filter filter;
pci_filter_init(pci, &filter);
if (pci_filter_parse_slot(&filter, slot))
{
fprintf(stderr, "Failed to parse device id %s\n", slot);
goto pci_filter_parse_failed;
}
pci_init(pci);
struct pci_dev * dev = pci_get_dev(pci,
filter.domain,
filter.bus,
filter.slot,
filter.func);
if (! dev)
{
fprintf(stderr, "Failed to allocate dev\n");
goto pci_get_dev_failed;
}
pci_fill_info(dev, PCI_FILL_IDENT);
return dev;
pci_get_dev_failed:
pci_filter_parse_failed:
return NULL;
}
static void
print_results_above(uint64_t limit_ns,
double ns_per_cycle,
unsigned long * timestamps,
size_t iteration_count,
double max)
{
double limit = limit_ns / ns_per_cycle;
if (limit >= max)
return;
double range = max - limit;
double delta = range / 5.;
fprintf(stdout, "\n");
for (; limit < max; limit += delta)
fprintf(stdout, "Above %'.0lf ns: %'zu\n",
limit * ns_per_cycle,
above(limit, timestamps, iteration_count));
}
static void
print_results(double tsc_ghz,
unsigned long * timestamps,
size_t iteration_count,
unsigned long test_duration_cycles,
uint64_t limit_ns)
{
double ns_per_cycle = 1. / tsc_ghz;
size_t min_index = gsl_stats_ulong_min_index(timestamps, 1,
iteration_count);
size_t max_index = gsl_stats_ulong_max_index(timestamps, 1,
iteration_count);
double mean = gsl_stats_ulong_mean(timestamps, 1, iteration_count);
double sd = gsl_stats_ulong_sd(timestamps, 1, iteration_count);
fprintf(stdout, "Samples count: %'zu\n"
"Sampling duration: %'.0lf ms\n"
"Detected frequency: %.3lf GHz\n"
"\n"
"Min: %'.0lf ns @%zu\n"
"Mean: %'.0lf ns\n"
"Max: %'.0lf ns @%zu\n"
"\n"
"Std: %'.0lf ns\n",
iteration_count,
test_duration_cycles * ns_per_cycle / (1000. * 1000.),
tsc_ghz,
ns_per_cycle * timestamps[min_index], min_index,
ns_per_cycle * mean,
ns_per_cycle * timestamps[max_index], max_index,
ns_per_cycle * sd);
if (limit_ns)
print_results_above(limit_ns,
ns_per_cycle,
timestamps,
iteration_count,
timestamps[max_index]);
}
static void
perform_reads(struct pci_dev * dev,
unsigned long * timestamps,
uint64_t iteration_count,
uint64_t wait_time_us)
{
size_t i;
for (i = 0; i != iteration_count; ++i)
{
struct timestamp t;
read_timestamp_counter(&t);
pci_read_word(dev, PCI_VENDOR_ID);
timestamps[i] = cycle_since_timestamp(&t);
if (wait_time_us)
usleep(wait_time_us);
}
}
static void
print_device_name(struct pci_access * pci, struct pci_dev * dev)
{
char device_name[1024];
if (pci_lookup_name(pci, device_name, sizeof(device_name),
PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
dev->vendor_id, dev->device_id))
fprintf(stdout, "Device: %s\n\n", device_name);
}
int
main(int argc, char* argv[])
{
int err = EXIT_FAILURE;
struct cmd_line cmd_line;
setlocale(LC_ALL, "");
if (parse_cmd_line(argc, argv, &cmd_line) < 0)
goto arg_check_failed;
struct pci_access * pci = pci_alloc();
if (! pci)
goto pci_alloc_failed;
/* This access bypass the kernel and use a memory mapping
* to PCI configuration registers */
pci->method = PCI_ACCESS_I386_TYPE1;
struct pci_dev * dev = create_pci_dev(pci, cmd_line.slot);
if (! dev)
goto create_pci_dev_failed;
print_device_name(pci, dev);
unsigned long * timestamps
= malloc(sizeof(*timestamps) * cmd_line.iteration_count);
if (! timestamps)
{
fprintf(stderr, "Can't allocate timestamp storage (%s)\n",
strerror(errno));
goto malloc_failed;
}
struct timestamp t;
read_timestamp_counter(&t);
perform_reads(dev,
timestamps,
cmd_line.iteration_count,
cmd_line.wait_time_us);
unsigned long test_duration_cycles = cycle_since_timestamp(&t);
double tsc_ghz = get_tsc_ghz();
if (tsc_ghz == 0.) {
fprintf(stderr, "Can't retrieve tsc frequency (%s)\n",
strerror(errno));
goto get_tsc_ghz_failed;
}
print_results(tsc_ghz,
timestamps,
cmd_line.iteration_count,
test_duration_cycles,
cmd_line.limit_ns);
err = EXIT_SUCCESS;
get_tsc_ghz_failed:
free(timestamps);
malloc_failed:
pci_free_dev(dev);
create_pci_dev_failed:
pci_cleanup(pci);
pci_alloc_failed:
arg_check_failed:
return err;
}