-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg_fuse.c
383 lines (307 loc) · 9.25 KB
/
pg_fuse.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
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
/*
ParaGrapher FUSE
*/
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#define _GNU_SOURCE
#define FUSE_USE_VERSION 31
#include <fuse.h>
#include <libgen.h>
#include <stddef.h>
#include <numa.h>
#include "aux.c"
#define __PGF_D 0 // __PG_FUSE_DEBUG
#define __PGF_RUB 1 // __PG_FUSE_REMOVE_UNUSED_BLOCKS
const unsigned long block_size = 1024UL * 1024 * 32; // Must be multiplies of BLKSSZGET (512)
const unsigned long idle_time_before_expiration = 1e9 * 10; // in nanoseconds
char* file_path;
char* file_basename;
unsigned long file_size = 0;
char** blocks_mem = NULL;
char** blocks_main_mem = NULL;
int* blocks_status = NULL; // 0: loaded, -1: not loaded, -2: loading, -3: being removed, positive values: users count
unsigned int loaded_blocks_count;
unsigned int blocks_count;
unsigned long* blocks_last_access = NULL;
unsigned long start_time;
unsigned int available_threads;
static struct options
{
const char *file_path;
int show_help;
} options;
static const struct fuse_opt option_spec[] =
{
{ "--file_path=%s", offsetof(struct options, file_path), 1 },
{ "-h", offsetof(struct options, show_help), 1 },
{ "--help", offsetof(struct options, show_help), 1 },
FUSE_OPT_END
};
static void *pg_fuse_init(struct fuse_conn_info *conn, struct fuse_config *cfg)
{
(void) conn;
// cfg->kernel_cache = 0;
// cfg->auto_cache = 0;
// cfg->direct_io = 1;
return NULL;
}
static int pg_fuse_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
{
(void) fi;
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else if (strcmp(path+1, file_basename) == 0)
{
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = file_size;
}
else
res = -ENOENT;
return res;
}
static int pg_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags)
{
(void) offset;
(void) fi;
(void) flags;
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0, FUSE_FILL_DIR_PLUS);
filler(buf, "..", NULL, 0, FUSE_FILL_DIR_PLUS);
filler(buf, file_basename, NULL, 0, FUSE_FILL_DIR_PLUS);
return 0;
}
static int pg_fuse_open(const char *path, struct fuse_file_info *fi)
{
(void) fi;
if (strcmp(path+1, file_basename) != 0)
return -ENOENT;
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
return 0;
}
static int pg_fuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
// printf("read: %lu\n", size);
(void) fi;
if(strcmp(path+1, file_basename) != 0)
return -ENOENT;
if(size == 0)
return 0;
if(offset >= file_size)
return 0;
if(offset + size > file_size)
size = file_size - offset;
unsigned long last_offset = offset + size;
unsigned long total_written_bytes = 0;
for(unsigned long b = offset/block_size; b <= (last_offset - 1)/block_size; b++)
{
// Check if the block has been started loading
int status;
while(1)
{
status = blocks_status[b];
if(status >= 0)
{
if(__sync_bool_compare_and_swap(blocks_status + b, status, status + 1))
{
status = blocks_status[b];
assert(status > 0);
break;
}
else
continue;
}
if(status == -2 || status == -3)
{
sched_yield();
continue;
}
if(status == -1)
{
if(__sync_bool_compare_and_swap(blocks_status + b, -1, -2))
{
status = blocks_status[b];
assert(status == -2);
break;
}
else
continue;
}
}
assert(status > 0 || status == -2);
if(status == -2)
{
// Read the block into memory
unsigned long t0 = __get_nano_time();
__PGF_D && printf("%.2f(s): Block %u loading for pid: %u\n",
(t0 + start_time)/1e9, b, fuse_get_context()->pid);
__PGF_D && fflush(stdout);
blocks_main_mem[b] = numa_alloc_interleaved(4096 + block_size);
assert(blocks_main_mem[b] != NULL);
unsigned long remainder = (unsigned long)blocks_main_mem[b] % 4096;
if(remainder != 0)
blocks_mem[b] = (char*)((unsigned long)blocks_main_mem[b] + 4096 - remainder);
else
blocks_mem[b] = blocks_main_mem[b];
int fd = open(file_path, O_RDONLY | O_DIRECT);
assert(fd > 0);
unsigned long start_byte = block_size * b;
unsigned long length = min(block_size, file_size - start_byte);
unsigned long new_offset = lseek(fd, start_byte, SEEK_SET);
assert(new_offset == start_byte);
unsigned long read_bytes = 0;
while(read_bytes < length)
{
long ret = read(fd, blocks_mem[b] + read_bytes, length - read_bytes);
assert(ret != -1);
read_bytes += ret;
}
close(fd);
fd = -1;
status = __sync_val_compare_and_swap(blocks_status + b, -2, 1);
assert(status == -2);
__PGF_RUB && (blocks_last_access[b] = t0);
__PGF_D && printf("%.2f(s): Block %u loading for pid: %u finished in %.2f seconds\n",
(t0 + start_time)/1e9, b, fuse_get_context()->pid, (__get_nano_time() - t0) /1e9);
__PGF_D && fflush(stdout);
// Release unused blocks
__PGF_RUB && __atomic_fetch_add(&loaded_blocks_count, 1, __ATOMIC_RELAXED);
if(__PGF_RUB)
{
t0 = __get_nano_time();
for(unsigned int b = 0; b < blocks_count && (loaded_blocks_count > 2 * available_threads); b++)
{
if(blocks_status[b] != 0)
continue;
if(t0 < blocks_last_access[b] + idle_time_before_expiration)
continue;
// Lock block for releasing
if(__sync_bool_compare_and_swap(blocks_status + b, 0, -3))
{
numa_free(blocks_main_mem[b],4096 + block_size);
blocks_main_mem[b] = NULL;
blocks_mem[b] = NULL;
blocks_last_access[b] = 0;
__atomic_fetch_sub(&loaded_blocks_count, 1, __ATOMIC_RELAXED);
int status = __sync_val_compare_and_swap(blocks_status + b, -3, -1);
assert(status == -3);
__PGF_D && printf("%.2f(s): Block %u released.\n", (t0 + start_time)/1e9, b);
__PGF_D && fflush(stdout);
}
}
}
// All done.
}
assert(blocks_mem[b] != NULL);
// Write from block to buf
unsigned long b_start = offset % block_size;
unsigned long b_end = min(b_start + size, block_size);
unsigned long b_len = b_end - b_start;
memcpy(buf, blocks_mem[b] + b_start, b_len);
total_written_bytes += b_len;
offset += b_len;
buf += b_len;
size -= b_len;
// Touching block timestamp
__PGF_RUB && (blocks_last_access[b] = __get_nano_time());
// Release the block
status = __atomic_fetch_sub(blocks_status + b, 1, __ATOMIC_RELAXED);
assert(status >= 1);
}
return total_written_bytes;
}
static const struct fuse_operations ops = {
.init = pg_fuse_init,
.getattr = pg_fuse_getattr,
.readdir = pg_fuse_readdir,
.open = pg_fuse_open,
.read = pg_fuse_read,
};
int main(int argc, char *argv[])
{
// Parsing aguments and showing help
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
options.file_path = NULL;
if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
return -1;
if (options.show_help)
{
printf(
"\nParaGrapher FUSE (pg_fuse)\n\n"
"Arguments:\n"
" --file_path=<s> Path to the file\n"
"Usage:\n"
" ./pg_fuse path/to/mountpoint --file_path=path/to/source/file [FUSE options]\n"
"\n"
);
assert(fuse_opt_add_arg(&args, "--help") == 0);
args.argv[0][0] = '\0';
int ret = fuse_main(args.argc, args.argv, &ops, NULL);
fuse_opt_free_args(&args);
return ret;
}
// Check if file_path exists
int file_path_correct = 0;
if(options.file_path == NULL)
{
printf("Error: No file_path has been given. The file path should be given using --file_path argument.\n\n");
return -2;
}
else if(access(options.file_path, F_OK) != 0)
{
printf("Error: The given file_path on \"%s\" is not accesible.\n\n", options.file_path);
return -3;
}
// Initializing the data
file_path = realpath(strdup(options.file_path), NULL);
file_basename = strdup(basename(file_path));
int fd = open(file_path,O_RDONLY);
assert(fd > 0);
struct stat st;
int ret = fstat(fd, &st);
assert(ret == 0);
close(fd);
fd = -1;
file_size = st.st_size;
printf("\nParaGrapher FUSE (pg_fuse) started.\n");
printf("File \"%s\" with size %lu is mounted on \"%s/%s\"\n",
file_path, file_size, realpath(argv[1], NULL), file_basename);
available_threads = get_available_cpus_count();
printf("Available threads: %u\n", available_threads);
blocks_count = 1 + file_size / block_size;
blocks_mem = calloc(blocks_count, sizeof(char*));
assert(blocks_mem != NULL);
blocks_main_mem = calloc(blocks_count, sizeof(char*));
assert(blocks_main_mem != NULL);
blocks_status = calloc(blocks_count, sizeof(int));
assert(blocks_status != NULL);
loaded_blocks_count = 0;
if(__PGF_RUB)
{
blocks_last_access = calloc(blocks_count, sizeof(unsigned long));
assert(blocks_last_access != NULL);
}
for(unsigned int b = 0; b < blocks_count; b++)
{
blocks_status[b] = -1;
blocks_mem[b] = NULL;
blocks_main_mem[b] = NULL;
__PGF_RUB && (blocks_last_access[b] = 0);
}
start_time = - __get_nano_time();
printf("Block size: %lu, Blocks count: %lu\n", block_size, blocks_count);
printf("\n\n");
fflush(stdout);
// Starting fuse
ret = fuse_main(args.argc, args.argv, &ops, NULL);
fuse_opt_free_args(&args);
return ret;
}