-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.c
266 lines (222 loc) · 6.4 KB
/
user.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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020 Park Ju Hyung
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include "crc32c.c"
#define PHYS_ADDR 0x3ec0000000
#define CHEEZE_QUEUE_SIZE 1024
#define CHEEZE_BUF_SIZE (2ULL * 1024 * 1024)
#define ITEMS_PER_HP ((1ULL * 1024 * 1024 * 1024) / CHEEZE_BUF_SIZE)
#define BITS_PER_EVENT (sizeof(uint64_t) * 8)
#define EVENT_BYTES (CHEEZE_QUEUE_SIZE / BITS_PER_EVENT)
#define SEND_OFF 0
#define SEND_SIZE (CHEEZE_QUEUE_SIZE * sizeof(uint8_t))
#define RECV_OFF (SEND_OFF + SEND_SIZE)
#define RECV_SIZE (CHEEZE_QUEUE_SIZE * sizeof(uint8_t))
#define SEQ_OFF (RECV_OFF + RECV_SIZE)
#define SEQ_SIZE (CHEEZE_QUEUE_SIZE * sizeof(uint64_t))
#define REQS_OFF (SEQ_OFF + SEQ_SIZE)
#define REQS_SIZE (CHEEZE_QUEUE_SIZE * sizeof(struct cheeze_req))
#define barrier() __asm__ __volatile__("": : :"memory")
#define ureq_print(u) \
do { \
printf("%s:%d\n id=%d\n op=%d\n pos=%u\n len=%u\n", __func__, __LINE__, u->id, u->op, u->pos, u->len); \
} while (0);
static void *page_addr;
//static void *meta_addr; // page_addr[0] ==> send_event_addr, recv_event_addr, seq_addr, ureq_addr
static uint8_t *send_event_addr; // CHEEZE_QUEUE_SIZE ==> 16B
static uint8_t *recv_event_addr; // 16B
static uint64_t *seq_addr; // 8KB
struct cheeze_req_user *ureq_addr; // sizeof(req) * 1024
static char *data_addr[2]; // page_addr[1]: 1GB, page_addr[2]: 1GB
static uint64_t seq = 0;
enum req_opf {
/* read sectors from the device */
REQ_OP_READ = 0,
/* write sectors to the device */
REQ_OP_WRITE = 1,
/* flush the volatile write cache */
REQ_OP_FLUSH = 2,
/* discard sectors */
REQ_OP_DISCARD = 3,
/* get zone information */
REQ_OP_ZONE_REPORT = 4,
/* securely erase sectors */
REQ_OP_SECURE_ERASE = 5,
/* seset a zone write pointer */
REQ_OP_ZONE_RESET = 6,
/* write the same sector many times */
REQ_OP_WRITE_SAME = 7,
/* write the zero filled sector many times */
REQ_OP_WRITE_ZEROES = 9,
/* SCSI passthrough using struct scsi_request */
REQ_OP_SCSI_IN = 32,
REQ_OP_SCSI_OUT = 33,
/* Driver private requests */
REQ_OP_DRV_IN = 34,
REQ_OP_DRV_OUT = 35,
REQ_OP_LAST,
};
struct cheeze_req_user {
int id;
int op;
unsigned int pos; // sector_t
unsigned int len;
} __attribute__((aligned(8), packed));
#define COPY_TARGET "/dev/hugepages/disk"
#define TRACE_TARGET "/trace"
static inline char *get_buf_addr(char **pdata_addr, int id) {
int idx = id / ITEMS_PER_HP;
return pdata_addr[idx] + ((id % ITEMS_PER_HP) * CHEEZE_BUF_SIZE);
}
static void shm_meta_init(void *ppage_addr) {
//memset(ppage_addr, 0, (1ULL * 1024 * 1024 * 1024));
send_event_addr = ppage_addr + SEND_OFF; // CHEEZE_QUEUE_SIZE ==> 16B
recv_event_addr = ppage_addr + RECV_OFF; // 16B
seq_addr = ppage_addr + SEQ_OFF; // 8KB
ureq_addr = ppage_addr + REQS_OFF; // sizeof(req) * 1024
}
#if 0
// Warning, output is static so this function is not reentrant
static const char *humanSize(uint64_t bytes)
{
static char output[200];
char *suffix[] = { "B", "KiB", "MiB", "GiB", "TiB" };
char length = sizeof(suffix) / sizeof(suffix[0]);
int i = 0;
double dblBytes = bytes;
if (bytes > 1024) {
for (i = 0; (bytes / 1024) > 0 && i < length - 1;
i++, bytes /= 1024)
dblBytes = bytes / 1024.0;
}
sprintf(output, "%.02lf %s", dblBytes, suffix[i]);
return output;
}
#endif
static off_t fdlength(int fd)
{
struct stat st;
off_t cur, ret;
if (!fstat(fd, &st) && S_ISREG(st.st_mode))
return st.st_size;
cur = lseek(fd, 0, SEEK_CUR);
ret = lseek(fd, 0, SEEK_END);
lseek(fd, cur, SEEK_SET);
return ret;
}
static inline uint64_t ts_to_ns(struct timespec* ts) {
return ts->tv_sec * (uint64_t)1000000000L + ts->tv_nsec;
}
#define TOTAL_SIZE (3ULL * 1024L * 1024L * 1024L) // 3 GB
static void mem_init()
{
uint64_t pagesize, addr, len;
int fd;
fd = open("/dev/mem", O_RDWR);
if (fd == -1) {
perror("Failed to open /dev/mem");
exit(1);
}
pagesize = getpagesize();
addr = PHYS_ADDR & (~(pagesize - 1));
len = (PHYS_ADDR & (pagesize - 1)) + TOTAL_SIZE;
page_addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr);
if (page_addr == MAP_FAILED) {
perror("Failed to mmap plain device path");
exit(1);
}
close(fd);
}
int main() {
int copyfd, dumpfd;
char *mem;
uint8_t *send, *recv;
int i, id;
unsigned int j;
uint32_t crc;
struct cheeze_req_user *ureq;
char *buf, *page_buf;
mem_init();
shm_meta_init(((char*)page_addr) + (2ULL * 1024 * 1024 * 1024));
data_addr[0] = ((char *)page_addr) + (1ULL * 1024 * 1024 * 1024);
data_addr[1] = ((char *)page_addr);
copyfd = open(COPY_TARGET, O_RDWR);
if (copyfd < 0) {
perror("Failed to open " COPY_TARGET);
return 1;
}
mem = mmap(NULL, fdlength(copyfd), PROT_READ | PROT_WRITE, MAP_SHARED, copyfd, 0);
if (mem == MAP_FAILED) {
perror("Failed to mmap copy path");
return 1;
}
close(copyfd);
dumpfd = open(TRACE_TARGET, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (dumpfd < 0) {
perror("Failed to open " TRACE_TARGET);
return 1;
}
while (1) {
for (i = 0; i < CHEEZE_QUEUE_SIZE; i++) {
send = &send_event_addr[i];
recv = &recv_event_addr[i];
if (*send) {
id = i;
// printf("id: %d, seq_addr[id]: %lu, seq: %lu\n", id, seq_addr[id], seq);
ureq = ureq_addr + id;
// ureq_print(ureq);
// if (seq_addr[id] == seq) {
buf = mem + (ureq->pos * 4096ULL);
page_buf = get_buf_addr(data_addr, id);
switch (ureq->op) {
case REQ_OP_READ:
write(dumpfd, ureq, sizeof(*ureq));
for (j = 0; j < ureq->len; j += 4096) {
crc = crc32c(0, buf + j, 4096);
write(dumpfd, &crc, sizeof(crc));
}
memcpy(page_buf, buf, ureq->len);
break;
case REQ_OP_WRITE:
memcpy(buf, page_buf, ureq->len);
write(dumpfd, ureq, sizeof(*ureq));
for (j = 0; j < ureq->len; j += 4096) {
crc = crc32c(0, buf + j, 4096);
write(dumpfd, &crc, sizeof(crc));
}
break;
case REQ_OP_DISCARD:
memset(buf, 0, ureq->len);
write(dumpfd, ureq, sizeof(*ureq));
for (j = 0; j < ureq->len; j += 4096) {
crc = 0;
write(dumpfd, &crc, sizeof(crc));
}
break;
}
seq++;
barrier();
*send = 0;
barrier();
*recv = 1;
// } else {
// continue;
// }
}
}
}
close(dumpfd);
return 0;
}