forked from Xilinx/RecoNIC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dma_utils.c
executable file
·190 lines (166 loc) · 4.61 KB
/
dma_utils.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
//==============================================================================
// Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
//
//==============================================================================
#include "dma_utils.h"
int verbose = 0;
/* Subtract timespec t2 from t1
*
* Both t1 and t2 must already be normalized
* i.e. 0 <= nsec < 1000000000
*/
static int timespec_check(struct timespec *t);
uint64_t getopt_integer(char *optarg)
{
int rc;
uint64_t value;
rc = sscanf(optarg, "0x%lx", &value);
if (rc <= 0)
sscanf(optarg, "%lu", &value);
//printf("sscanf() = %d, value = 0x%lx\n", rc, value);
return value;
}
void dump_throughput_result(uint64_t size, float result, float lat_result) {
printf("size=%lu ", size);
if (((long long)(result)/GB_DIV)) {
printf("Average BW = %f GB/sec, average latency = %f us\n", ((double)result/GB_DIV), ((double) lat_result * 1000000.0));
} else if (((long long)(result)/MB_DIV)) {
printf("Average BW = %f MB/sec, average latency = %f us\n", ((double)result/MB_DIV), ((double) lat_result * 1000000.0));
} else if (((long long)(result)/KB_DIV)) {
printf("Average BW = %f KB/sec, average latency = %f us\n", ((double)result/KB_DIV), ((double) lat_result * 1000000.0));
} else
printf("Average BW = %f Bytes/sec, average latency = %f us\n", ((double)result), ((double) lat_result * 1000000.0));
}
static int timespec_check(struct timespec *t)
{
if ((t->tv_nsec < 0) || (t->tv_nsec >= 1000000000))
return -1;
return 0;
}
void timespec_sub(struct timespec *t1, struct timespec *t2)
{
if (timespec_check(t1) < 0) {
fprintf(stderr, "invalid time #1: %lld.%.9ld.\n",
(long long)t1->tv_sec, t1->tv_nsec);
return;
}
if (timespec_check(t2) < 0) {
fprintf(stderr, "invalid time #2: %lld.%.9ld.\n",
(long long)t2->tv_sec, t2->tv_nsec);
return;
}
t1->tv_sec -= t2->tv_sec;
t1->tv_nsec -= t2->tv_nsec;
if (t1->tv_nsec >= 1000000000) {
t1->tv_sec++;
t1->tv_nsec -= 1000000000;
} else if (t1->tv_nsec < 0) {
t1->tv_sec--;
t1->tv_nsec += 1000000000;
}
}
ssize_t read_to_buffer(char *fname, int fd, char *buffer, uint64_t size,
uint64_t base)
{
ssize_t rc;
uint64_t count = 0;
char *buf = buffer;
off_t offset = base;
do { /* Support zero byte transfer */
uint64_t bytes = size - count;
if (bytes > RW_MAX_SIZE)
bytes = RW_MAX_SIZE;
if (offset) {
rc = lseek(fd, offset, SEEK_SET);
if (rc < 0) {
fprintf(stderr,
"%s, seek off 0x%lx failed %zd.\n",
fname, offset, rc);
perror("seek file");
return -EIO;
}
if (rc != offset) {
fprintf(stderr,
"%s, seek off 0x%lx != 0x%lx.\n",
fname, rc, offset);
return -EIO;
}
}
/* read data from file into memory buffer */
rc = read(fd, buf, bytes);
if (rc < 0) {
fprintf(stderr,
"%s, read off 0x%lx + 0x%lx failed %zd.\n",
fname, offset, bytes, rc);
perror("read file");
return -EIO;
}
if (rc != bytes) {
fprintf(stderr,
"%s, R off 0x%lx, 0x%lx != 0x%lx.\n",
fname, count, rc, bytes);
return -EIO;
}
count += bytes;
buf += bytes;
offset += bytes;
} while (count < size);
if (count != size) {
fprintf(stderr, "%s, R failed 0x%lx != 0x%lx.\n",
fname, count, size);
return -EIO;
}
return count;
}
ssize_t write_from_buffer(char *fname, int fd, char *buffer, uint64_t size,
uint64_t base)
{
ssize_t rc;
uint64_t count = 0;
char *buf = buffer;
off_t offset = base;
do { /* Support zero byte transfer */
uint64_t bytes = size - count;
if (bytes > RW_MAX_SIZE)
bytes = RW_MAX_SIZE;
if (offset) {
rc = lseek(fd, offset, SEEK_SET);
if (rc < 0) {
fprintf(stderr,
"%s, seek off 0x%lx failed %zd.\n",
fname, offset, rc);
perror("seek file");
return -EIO;
}
if (rc != offset) {
fprintf(stderr,
"%s, seek off 0x%lx != 0x%lx.\n",
fname, rc, offset);
return -EIO;
}
}
/* write data to file from memory buffer */
rc = write(fd, buf, bytes);
if (rc < 0) {
fprintf(stderr, "%s, W off 0x%lx, 0x%lx failed %zd.\n",
fname, offset, bytes, rc);
perror("write file");
return -EIO;
}
if (rc != bytes) {
fprintf(stderr, "%s, W off 0x%lx, 0x%lx != 0x%lx.\n",
fname, offset, rc, bytes);
return -EIO;
}
count += bytes;
buf += bytes;
offset += bytes;
} while (count < size);
if (count != size) {
fprintf(stderr, "%s, R failed 0x%lx != 0x%lx.\n",
fname, count, size);
return -EIO;
}
return count;
}