-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileops_bankshot2.c
3358 lines (2765 loc) · 89.1 KB
/
fileops_bankshot2.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// a module which repalces the standart POSIX functions with memory mapped equivalents
#include "fileops_bankshot2.h"
//#include "my_memcpy_nocache.h"
// TODO: manual prefaulting sometimes segfaults
#define MANUAL_PREFAULT 0
#define MMAP_PREFAULT 1
#define DO_ALIGNMENT_CHECKS 0
struct timezone;
struct timeval;
int gettimeofday(struct timeval *tv, struct timezone *tz);
const char *bankshot2_ctrl_dev = "/dev/bankshot2Ctrl0";
int bankshot2_ctrl_fd;
#define NOSANITYCHECK 1
#if NOSANITYCHECK
#define SANITYCHECK(x)
#else
#define SANITYCHECK(x) if(UNLIKELY(!(x))) { ERROR("NVP_SANITY("#x") failed!\n"); exit(101); }
#endif
#define COUNT_EXTENDS 0
#if COUNT_EXTENDS
volatile size_t _bankshot2_wr_extended;
volatile size_t _bankshot2_wr_total;
#endif
#define NVP_DO_LOCKING 1
#define NVP_LOCK_FD_RD(nvf, cpuid) NVP_LOCK_RD( nvf->lock, cpuid)
#define NVP_UNLOCK_FD_RD(nvf, cpuid) NVP_LOCK_UNLOCK_RD(nvf->lock, cpuid)
#define NVP_LOCK_FD_WR(nvf) NVP_LOCK_WR( nvf->lock)
#define NVP_UNLOCK_FD_WR(nvf) NVP_LOCK_UNLOCK_WR(nvf->lock)
#define NVP_LOCK_NODE_RD(nvf, cpuid) NVP_LOCK_RD( nvf->node->lock, cpuid)
#define NVP_UNLOCK_NODE_RD(nvf, cpuid) NVP_LOCK_UNLOCK_RD(nvf->node->lock, cpuid)
#define NVP_LOCK_NODE_WR(nvf) NVP_LOCK_WR( nvf->node->lock)
#define NVP_UNLOCK_NODE_WR(nvf) NVP_LOCK_UNLOCK_WR(nvf->node->lock)
BOOST_PP_SEQ_FOR_EACH(DECLARE_WITHOUT_ALIAS_FUNCTS_IWRAP, _bankshot2_, ALLOPS_WPAREN)
RETT_OPEN _bankshot2_OPEN(INTF_OPEN);
RETT_IOCTL _bankshot2_IOCTL(INTF_IOCTL);
int MMAP_PAGE_SIZE;
void* _bankshot2_zbuf; // holds all zeroes. used for aligned file extending. TODO: does sharing this hurt performance?
pthread_spinlock_t node_lookup_lock;
struct NVFile* _bankshot2_fd_lookup;
struct NVNode* _bankshot2_node_lookup;
int _bankshot2_ino_lookup[1024];
void _bankshot2_init2(void);
//int _bankshot2_extend_map(struct NVFile *nvf, size_t newlen);
void _bankshot2_SIGBUS_handler(int sig);
void _bankshot2_SIGINT_handler(int sig);
void _bankshot2_SIGSEGV_handler(int sig);
void _bankshot2_SIGUSR1_handler(int sig);
void cache_write_back(struct NVFile *nvf);
void bankshot2_clear_mappings(void);
void bankshot2_print_io_stats(void);
RETT_PWRITE _bankshot2_do_pwrite(INTF_PWRITE, int wr_lock, int cpuid); // like PWRITE, but without locks (called by _bankshot2_WRITE)
RETT_PWRITE _bankshot2_do_pread (INTF_PREAD, int cpuid); // like PREAD , but without locks (called by _bankshot2_READ )
RETT_SEEK64 _bankshot2_do_seek64(INTF_SEEK64); // called by bankshot2_seek, bankshot2_seek64, bankshot2_write
#define DO_MSYNC(nvf) do{}while(0)
// DEBUG("NOT doing a msync\n"); }while(0)
/*
DEBUG("Doing a msync on fd %i (node %p)\n", nvf->fd, nvf->node); \
if(msync(nvf->node->data, nvf->node->maplength, MS_SYNC|MS_INVALIDATE)) { \
ERROR("Failed to msync for fd %i\n", nvf->fd); \
assert(0); \
} }while(0)
*/
int _bankshot2_lock_return_val;
int _bankshot2_write_pwrite_lock_handoff;
static sigjmp_buf pread_jumper;
static sigjmp_buf pwrite_jumper;
volatile static int do_pread_memcpy;
volatile static int do_pwrite_memcpy;
int falloc_error;
#define MAX_FALLOC_ERROR 10
//void * mremap(void *old_address, size_t old_size, size_t new_size, int flags);
MODULE_REGISTRATION_F("bankshot2", _bankshot2_, _bankshot2_init2(); );
#define NVP_CHECK_NVF_VALID(nvf) do{ \
if(UNLIKELY(!nvf->valid)) { \
DEBUG("Invalid file descriptor: %i\n", file); \
errno = EBADF; \
NVP_UNLOCK_FD_RD(nvf, cpuid); \
return -1; \
} \
else \
{ \
DEBUG("this function is operating on node %p\n", nvf->node); \
SANITYCHECKNVF(nvf); \
DO_MSYNC(nvf); \
} \
} while(0)
#define NVP_CHECK_NVF_VALID_WR(nvf) do{ \
if(UNLIKELY(!nvf->valid)) { \
DEBUG("Invalid file descriptor: %i\n", file); \
errno = EBADF; \
NVP_UNLOCK_FD_WR(nvf); \
return -1; \
} \
else \
{ \
DEBUG("this function is operating on node %p\n", nvf->node); \
SANITYCHECKNVF(nvf); \
DO_MSYNC(nvf); \
} \
} while(0)
#define SANITYCHECKNVF(nvf) \
SANITYCHECK(nvf->valid); \
SANITYCHECK(nvf->node != NULL); \
SANITYCHECK(nvf->fd >= 0); \
SANITYCHECK(nvf->fd < OPEN_MAX); \
SANITYCHECK(nvf->offset != NULL); \
SANITYCHECK(*nvf->offset >= 0); \
SANITYCHECK(nvf->serialno != 0); \
SANITYCHECK(nvf->serialno == nvf->node->serialno); \
SANITYCHECK(nvf->node->length >=0); \
SANITYCHECK(nvf->node->maplength >= nvf->node->length); \
SANITYCHECK(nvf->node->data != NULL)
#define NVP_WRAP_HAS_FD(op) \
RETT_##op _bankshot2_##op ( INTF_##op ) { \
CHECK_RESOLVE_FILEOPS(_bankshot2_);\
DEBUG("_bankshot2_"#op" is just wrapping %s->"#op"\n", _bankshot2_fileops->name); \
if(UNLIKELY(file>=OPEN_MAX)) { DEBUG("file descriptor too large (%i > %i)\n", file, OPEN_MAX-1); errno = EBADF; return (RETT_##op) -1; } \
if(UNLIKELY(file<0)) { DEBUG("file < 0 (file = %i). return -1;\n", file); errno = EBADF; return (RETT_##op) -1; } \
if(UNLIKELY(!_bankshot2_fd_lookup[file].valid)) { DEBUG("That file descriptor (%i) is invalid\n", file); errno = EBADF; return -1; } \
DEBUG("_bankshot2_" #op " is calling %s->" #op "\n", _bankshot2_fileops->name); \
return (RETT_##op) _bankshot2_fileops->op( CALL_##op ); \
}
#define NVP_WRAP_NO_FD(op) \
RETT_##op _bankshot2_##op ( INTF_##op ) { \
CHECK_RESOLVE_FILEOPS(_bankshot2_);\
DEBUG("_bankshot2_"#op" is just wrapping %s->"#op"\n", _bankshot2_fileops->name); \
return _bankshot2_fileops->op( CALL_##op ); \
}
#define NVP_WRAP_HAS_FD_IWRAP(r, data, elem) NVP_WRAP_HAS_FD(elem)
#define NVP_WRAP_NO_FD_IWRAP(r, data, elem) NVP_WRAP_NO_FD(elem)
//BOOST_PP_SEQ_FOR_EACH(NVP_WRAP_HAS_FD_IWRAP, placeholder, (FSYNC) (FDSYNC) (ACCEPT))
BOOST_PP_SEQ_FOR_EACH(NVP_WRAP_HAS_FD_IWRAP, placeholder, (ACCEPT))
BOOST_PP_SEQ_FOR_EACH(NVP_WRAP_NO_FD_IWRAP, placeholder, (PIPE) (FORK) (SOCKET))
extern long copy_user_nocache(void *dst, const void *src, unsigned size, int zerorest);
static inline int copy_from_user_inatomic_nocache(void *dst, const void *src, unsigned size) {
return copy_user_nocache(dst, src, size, 0);
}
static inline void* my_memcpy_nocache(void* dst, const void* src, unsigned size) {
if(copy_from_user_inatomic_nocache(dst, src, size)) {
return dst;
} else {
return 0;
}
}
static inline void *intel_memcpy(void * __restrict__ b, const void * __restrict__ a, size_t n){
char *s1 = b;
const char *s2 = a;
for(; 0<n; --n)*s1++ = *s2++;
return b;
}
void *(*import_memcpy)(void * __restrict__ b, const void * __restrict__ a, size_t n);
extern void * __memcpy(void * __restrict__ to, const void * __restrict__ from, size_t len);
#define MMX2_MEMCPY_MIN_LEN 0x40
#define MMX_MMREG_SIZE 8
// ftp://ftp.work.acer-euro.com/gpl/AS1800/xine-lib/src/xine-utils/memcpy.c
static void * mmx2_memcpy(void * __restrict__ to, const void * __restrict__ from, size_t len)
{
void *retval;
size_t i;
retval = to;
/* PREFETCH has effect even for MOVSB instruction ;) */
__asm__ __volatile__ (
" prefetchnta (%0)\n"
" prefetchnta 32(%0)\n"
" prefetchnta 64(%0)\n"
" prefetchnta 96(%0)\n"
" prefetchnta 128(%0)\n"
" prefetchnta 160(%0)\n"
" prefetchnta 192(%0)\n"
" prefetchnta 224(%0)\n"
" prefetchnta 256(%0)\n"
" prefetchnta 288(%0)\n"
: : "r" (from) );
if(len >= MMX2_MEMCPY_MIN_LEN)
{
register unsigned long int delta;
/* Align destinition to MMREG_SIZE -boundary */
delta = ((unsigned long int)to)&(MMX_MMREG_SIZE-1);
if(delta)
{
delta=MMX_MMREG_SIZE-delta;
len -= delta;
memcpy(to, from, delta);
}
i = len >> 6; /* len/64 */
len&=63;
for(; i>0; i--)
{
__asm__ __volatile__ (
"prefetchnta 320(%0)\n"
"prefetchnta 352(%0)\n"
"movq (%0), %%mm0\n"
"movq 8(%0), %%mm1\n"
"movq 16(%0), %%mm2\n"
"movq 24(%0), %%mm3\n"
"movq 32(%0), %%mm4\n"
"movq 40(%0), %%mm5\n"
"movq 48(%0), %%mm6\n"
"movq 56(%0), %%mm7\n"
"movntq %%mm0, (%1)\n"
"movntq %%mm1, 8(%1)\n"
"movntq %%mm2, 16(%1)\n"
"movntq %%mm3, 24(%1)\n"
"movntq %%mm4, 32(%1)\n"
"movntq %%mm5, 40(%1)\n"
"movntq %%mm6, 48(%1)\n"
"movntq %%mm7, 56(%1)\n"
:: "r" (from), "r" (to) : "memory");
//((const unsigned char *)from)+=64;
//((unsigned char *)to)+=64;
from = (void*)(((const unsigned char *)from) + 64);
to = (void*)(((unsigned char *)to) + 64);
}
/* since movntq is weakly-ordered, a "sfence"
* is needed to become ordered again. */
__asm__ __volatile__ ("sfence":::"memory");
__asm__ __volatile__ ("emms":::"memory");
}
/*
* Now do the tail of the block
*/
if(len) memcpy(to, from, len);
return retval;
}
static char* memcpy1(char *to, char *from, size_t n)
{
long esi, edi;
int ecx;
esi = (long)from;
edi = (long)to;
asm volatile("rep ; movsl"
: "=&c" (ecx), "=&D" (edi), "=&S" (esi)
: "0" (n / 4), "1" (edi), "2" (esi)
: "memory"
);
return to;
}
//////////////////////////
// modifications to support different FSYNC policies
//#define MEMCPY memcpy
//define MEMCPY intel_memcpy
//#define MEMCPY (void*)copy_from_user_inatomic_nocache
//#define MEMCPY my_memcpy_nocache
#define MEMCPY mmx2_memcpy
//#define MEMCPY memcpy1
#define MMAP mmap
//#define FSYNC fsync
#define FSYNC_POLICY_NONE 0
#define FSYNC_POLICY_FLUSH_ON_FSYNC 1
#define FSYNC_POLICY_UNCACHEABLE_MAP 2
#define FSYNC_POLICY_NONTEMPORAL_WRITES 3
#define FSYNC_POLICY_FLUSH_ON_WRITE 4
#define FSYNC_POLICY FSYNC_POLICY_NONE
#if FSYNC_POLICY == FSYNC_POLICY_NONE
#define FSYNC_MEMCPY MEMCPY
#define FSYNC_MMAP MMAP
#define FSYNC_FSYNC
#elif FSYNC_POLICY == FSYNC_POLICY_FLUSH_ON_FSYNC
#define FSYNC_MEMCPY MEMCPY
#define FSYNC_MMAP MMAP
#define FSYNC_FSYNC fsync_fsync_flush_on_fsync(nvf)
#elif FSYNC_POLICY == FSYNC_POLICY_UNCACHEABLE_MAP
#define FSYNC_MEMCPY MEMCPY
#define FSYNC_MMAP mmap_fsync_uncacheable_map
#define FSYNC_FSYNC
#elif FSYNC_POLICY == FSYNC_POLICY_NONTEMPORAL_WRITES
#define FSYNC_MEMCPY memcpy_fsync_nontemporal_writes
#define FSYNC_MMAP MMAP
#define FSYNC_FSYNC _mm_mfence()
#elif FSYNC_POLICY == FSYNC_POLICY_FLUSH_ON_WRITE
#define FSYNC_MEMCPY memcpy_fsync_flush_on_write
#define FSYNC_MMAP MMAP
#define FSYNC_FSYNC _mm_mfence()
#endif
// void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
// void *memcpy(void *dest, const void *src, size_t n);
// int fsync(int fd);
static inline void fsync_flush_on_fsync(struct NVFile* nvf)
{
_mm_mfence();
do_cflush_len(nvf->node->data, nvf->node->length);
_mm_mfence();
}
void *mmap_fsync_uncacheable_map(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
void* result = MMAP( start, length, prot, flags, fd, offset );
// mark the result as uncacheable // TODO
// not_implemented++;
return result;
}
void *memcpy_fsync_nontemporal_writes(void *dest, const void *src, size_t n)
{
// TODO: an asm version of memcpy, with movdqa replaced by movntdq
// void* result; = FSYNC_MEMCPY( not_implemented );
_mm_mfence();
return NULL;
// return result;
}
void *memcpy_fsync_flush_on_write(void *dest, const void *src, size_t n)
{
// first, perform the memcpy as usual
void* result = MEMCPY(dest, src, n);
// then, flush all the pages which were just modified.
_mm_mfence();
do_cflush_len(dest, n);
_mm_mfence();
return result;
}
//////////////////////////
#define TIME_READ_MEMCPY 0
#if TIME_READ_MEMCPY
long long unsigned int total_memcpy_cycles = 0;
void report_memcpy_usec(void) { printf("Total memcpy time: %llu cycles: %f seconds\n", total_memcpy_cycles, ((float)(total_memcpy_cycles))/(2.27f*1024*1024*1024) ); }
#endif
/* ================= Timing ================= */
enum timing_category {
do_pread_t = 0,
do_pwrite_t,
memcpyr_t,
memcpyw_t,
lookup_t,
insert_t,
kernel_t,
get_inode_t,
evict_inode_t,
read_t,
pread_t,
write_t,
pwrite_t,
open_t,
close_t,
get_node_t,
fsync_t,
fdsync_t,
falloc_t,
TIMING_NUM, // Last item
};
unsigned long long Countstats[TIMING_NUM];
unsigned long long Timingstats[TIMING_NUM];
const char *Timingstring[TIMING_NUM] =
{
"do_pread",
"do_pwrite",
"memcpy_read",
"memcpy_write",
"Tree_lookup",
"Tree_insert",
"kernel",
"Get_inode",
"Evict_inode",
"READ",
"PREAD",
"WRITE",
"PWRITE",
"OPEN",
"CLOSE",
"get_node",
"Fsync",
"Fdsync",
"Fallocate",
};
typedef struct timespec timing_type;
#if MEASURE_TIMING
#define BANKSHOT2_START_TIMING(name, start) \
clock_gettime(CLOCK_MONOTONIC, &start)
#define BANKSHOT2_END_TIMING(name, start) \
{timing_type end; \
clock_gettime(CLOCK_MONOTONIC, &end); \
Countstats[name]++; \
Timingstats[name] += (end.tv_sec - start.tv_sec) * 1e9 \
+ (end.tv_nsec - start.tv_nsec); \
}
void bankshot2_print_time_stats(void)
{
int i;
printf("=========================== Bankshot2 timing stats: ==========================\n");
for (i = 0; i < TIMING_NUM; i++)
printf("%s: count %llu, timing %llu, average %llu\n",
Timingstring[i], Countstats[i], Timingstats[i],
Countstats[i] ? Timingstats[i] / Countstats[i] : 0);
}
#else
#define BANKSHOT2_START_TIMING(name, start) {(void)(start);}
#define BANKSHOT2_END_TIMING(name, start) \
{Countstats[name]++;}
void bankshot2_print_time_stats(void)
{
int i;
printf("=========================== Bankshot2 timing stats: ==========================\n");
for (i = 0; i < TIMING_NUM; i++)
printf("%s: count %llu\n", Timingstring[i], Countstats[i]);
}
#endif
/* ================= IO stats ================= */
unsigned int num_total_open;
unsigned int num_total_close;
unsigned int num_total_read;
unsigned int num_total_write;
unsigned long long total_read_size;
unsigned long long total_write_size;
unsigned int num_total_pread;
unsigned int num_total_pwrite;
unsigned long long total_pread_size;
unsigned long long total_pwrite_size;
unsigned int num_read_recheck;
unsigned int num_write_recheck;
void bankshot2_exit_handler(void);
void _bankshot2_SIGUSR1_handler(int sig)
{
MSG("SIGUSR1: print stats\n");
bankshot2_print_time_stats();
bankshot2_print_io_stats();
/* FIXME: Do we need to do this? */
bankshot2_clear_mappings();
}
void bankshot2_setup_signal_handler(void)
{
struct sigaction act, oact;
act.sa_handler = _bankshot2_SIGSEGV_handler;
act.sa_flags = SA_NODEFER;
sigaction(SIGSEGV, &act, &oact);
signal(SIGUSR1, _bankshot2_SIGUSR1_handler);
}
void _bankshot2_init2(void)
{
#if TIME_READ_MEMCPY
// atexit(report_memcpy_usec);
#endif
_bankshot2_write_pwrite_lock_handoff = 0;
assert(!posix_memalign(((void**)&_bankshot2_zbuf), 4096, 4096));
_bankshot2_fd_lookup = (struct NVFile *)
calloc(OPEN_MAX, sizeof(struct NVFile));
memset(_bankshot2_fd_lookup, 0, OPEN_MAX * sizeof(struct NVFile));
int i;
for(i = 0; i < OPEN_MAX; i++) {
NVP_LOCK_INIT(_bankshot2_fd_lookup[i].lock);
}
_bankshot2_node_lookup = (struct NVNode *)
calloc(OPEN_MAX, sizeof(struct NVNode));
memset(_bankshot2_node_lookup, 0, OPEN_MAX * sizeof(struct NVNode));
for(i = 0; i < OPEN_MAX; i++) {
NVP_LOCK_INIT(_bankshot2_node_lookup[i].lock);
pthread_mutex_init(&_bankshot2_node_lookup[i].mutex, NULL);
}
pthread_spin_init(&node_lookup_lock, PTHREAD_PROCESS_SHARED);
MMAP_PAGE_SIZE = getpagesize();
SANITYCHECK(MMAP_PAGE_SIZE > 100);
#if COUNT_EXTENDS
_bankshot2_wr_extended = 0;
_bankshot2_wr_total = 0;
#endif
DEBUG("Installing SIGBUS handler.\n");
signal(SIGBUS, _bankshot2_SIGBUS_handler);
DEBUG("Installing SIGINT handler.\n");
signal(SIGINT, _bankshot2_SIGINT_handler);
bankshot2_setup_signal_handler();
//TODO
/*
#define GLIBC_LOC ""
MSG("Importing memcpy from %s\n", GLIBC_LOC);
*/
atexit(bankshot2_exit_handler);
}
#if COUNT_EXTENDS
void _bankshot2_print_extend_stats(void) __attribute__ ((destructor));
void _bankshot2_print_extend_stats(void)
{
FILE * pFile;
pFile = fopen ("results.txt","w");
fprintf(pFile, "extended: %li\ntotal: %li\nratio: %f\n", _bankshot2_wr_extended, _bankshot2_wr_total, ((float)_bankshot2_wr_extended)/(_bankshot2_wr_extended+_bankshot2_wr_total));
fclose (pFile);
DEBUG("NVP: writes which extended: %li\n", _bankshot2_wr_extended);
DEBUG("NVP: total writes : %li\n", _bankshot2_wr_total);
//DEBUG("NVP: extended/total : %f\n", ((float)_bankshot2_wr_extended)/(_bankshot2_wr_extended+_bankshot2_wr_total));
}
#endif
/* Get the cache file inode. We're holding NVF lock and node lock. */
static int _bankshot2_get_cache_inode(const char *path, int oflag, int mode,
struct NVFile *nvf)
{
CHECK_RESOLVE_FILEOPS(_bankshot2_);
if(path == NULL) {
DEBUG("Invalid path.\n");
errno = EINVAL;
return -1;
}
DEBUG("_bankshot2_get_cache_inode for %s\n", path);
int result;
struct bankshot2_cache_data data;
timing_type get_inode_time;
struct NVNode* node = nvf->node;
if(!node) {
ERROR("Node does not exist!\n");
assert(0);
}
data.file = nvf->fd;
data.cache_ino = 0;
data.read = nvf->canRead ? 1 : 0;
data.write = nvf->canWrite ? 1 : 0;
DEBUG("Send IOCTL_GET_INODE request 0x%x to %d\n",
BANKSHOT2_IOCTL_GET_INODE, bankshot2_ctrl_fd);
BANKSHOT2_START_TIMING(get_inode_t, get_inode_time);
result = _bankshot2_fileops->IOCTL(bankshot2_ctrl_fd,
BANKSHOT2_IOCTL_GET_INODE, &data);
BANKSHOT2_END_TIMING(get_inode_t, get_inode_time);
if(result<0)
{
DEBUG("IOCTL_GET_INODE failed: %d\n", result);
return result;
}
DEBUG("_bankshot2_get_cache_inode succeeded for path %s: fd %llu returned.\n", path, data.cache_ino);
node->maplength = 0;
node->cache_serialno = data.cache_ino;
node->cache_length = data.cache_file_size;
if (node->num_extents == 0) {
node->extent_tree = RB_ROOT;
node->mmap_extent_tree = RB_ROOT;
}
// bankshot2_print_extent_tree(node);
nvf->cache_serialno = data.cache_ino;
DEBUG("Assign cache ino %d\n", nvf->cache_serialno);
SANITYCHECK(nvf->node->cache_length >= 0);
if(FLAGS_INCLUDE(oflag, O_TRUNC) && nvf->node->cache_length)
{
DEBUG("We just opened a file with O_TRUNC that was already open with nonzero length %li. Updating length.\n", nvf->node->length);
nvf->node->cache_length = 0;
}
DO_MSYNC(nvf);
return nvf->cache_serialno;
}
void bankshot2_cleanup_node(struct NVNode *node)
{
#if USE_BTREE
bankshot2_cleanup_extent_btree(node);
#else
bankshot2_cleanup_extent_tree(node);
#endif
}
void bankshot2_init_node(struct NVNode *node)
{
#if USE_BTREE
int i;
node->root = malloc(1024 * sizeof(unsigned long));
for (i = 0; i < 1024; i++)
node->root[i] = 0;
#endif
}
void bankshot2_clear_mappings(void)
{
struct NVFile *nvf;
struct NVNode *node;
int i;
uint64_t cache_ino;
for (i = 0; i < OPEN_MAX; i++) {
nvf = &_bankshot2_fd_lookup[i];
cache_ino = nvf->cache_serialno;
if (cache_ino) {
DEBUG("Clear cache fd %d mappings\n", cache_ino);
_bankshot2_fileops->IOCTL(bankshot2_ctrl_fd,
BANKSHOT2_IOCTL_REMOVE_MAPPING, &cache_ino);
}
}
if (bankshot2_ctrl_fd)
_bankshot2_fileops->CLOSE(bankshot2_ctrl_fd);
free(_bankshot2_fd_lookup);
pthread_spin_lock(&node_lookup_lock);
for (i = 0; i < OPEN_MAX; i++) {
node = &_bankshot2_node_lookup[i];
bankshot2_cleanup_node(node);
}
pthread_spin_unlock(&node_lookup_lock);
pthread_spin_destroy(&node_lookup_lock);
free(_bankshot2_node_lookup);
}
void bankshot2_print_io_stats(void)
{
// struct NVNode *node = NULL;
// int i;
#if 0
for (i = 0; i < OPEN_MAX; i++) {
node = &_bankshot2_node_lookup[i];
if (node->num_reads)
MSG("Node %d, cache fd %llu: reads %lu, "
"memcpy read %llu, total read %llu, "
"read segfaults %lu;\n",
i, node->cache_serialno, node->num_reads,
node->memcpy_read, node->total_read,
node->num_read_segfaults);
if (node->num_writes)
MSG("Node %d, cache fd %llu: "
"writes %lu, posix_writes %lu, "
"memcpy write %llu, total write %llu, "
"write segfaults %lu\n",
i, node->cache_serialno, node->num_writes,
node->num_posix_writes, node->memcpy_write,
node->total_write, node->num_write_segfaults);
if (node->num_read_kernels)
MSG("Node %d, cache fd %llu: READ: kernel count %lu, "
"mmap count %llu, total mmap length %llu, "
"total actual length %llu (%llu pages), "
"required %llu;\n",
i, node->cache_serialno, node->num_read_kernels,
node->num_read_mmaps, node->total_read_mmap,
node->total_read_actual,
node->total_read_actual >> 12,
node->total_read_required);
if (node->num_write_kernels)
MSG("Node %d, cache fd %llu: WRITE: kernel count %lu, "
"mmap count %llu, total mmap length %llu, "
"total actual length %llu (%llu pages), "
"required %llu\n",
i, node->cache_serialno, node->num_write_kernels,
node->num_write_mmaps, node->total_write_mmap,
node->total_write_actual,
node->total_write_actual >> 12,
node->total_write_required);
}
#endif
printf("================================ Total IO stats: ===============================\n");
printf("Total IO stats:\n");
printf("OPEN %u, CLOSE %u\n", num_total_open, num_total_close);
printf("READ: count %u, total size %llu, average %llu\n",
num_total_read, total_read_size,
num_total_read ? total_read_size / num_total_read : 0);
printf("WRITE: count %u, total size %llu, average %llu\n",
num_total_write, total_write_size,
num_total_write ? total_write_size / num_total_write : 0);
printf("PREAD: count %u, total size %llu, average %llu\n",
num_total_pread, total_pread_size,
num_total_pread ? total_pread_size / num_total_pread : 0);
printf("PWRITE: count %u, total size %llu, average %llu\n",
num_total_pwrite, total_pwrite_size,
num_total_pwrite ? total_pwrite_size / num_total_pwrite : 0);
printf("Read recheck count %u, write recheck count %u\n",
num_read_recheck, num_write_recheck);
}
void bankshot2_exit_handler(void)
{
MSG("Exit: print stats\n");
bankshot2_print_time_stats();
bankshot2_print_io_stats();
bankshot2_clear_mappings();
}
struct NVNode * bankshot2_allocate_node(void)
{
struct NVNode *node = NULL;
int i, candidate = -1;
// Find a NVNode without backing file inode
for (i = 0; i < OPEN_MAX; i++)
{
if(_bankshot2_node_lookup[i].serialno == 0) {
node = &_bankshot2_node_lookup[i];
DEBUG("Allocating unused NVNode %d\n", i);
bankshot2_cleanup_node(node);
bankshot2_init_node(node);
break;
}
if (candidate == -1 && _bankshot2_node_lookup[i].reference == 0)
candidate = i;
}
if (node)
return node;
// Find a NVNode without references
if (candidate != -1) {
node = &_bankshot2_node_lookup[candidate];
DEBUG("Allocating unreferenced NVNode %d\n", candidate);
bankshot2_cleanup_node(node);
bankshot2_init_node(node);
return node;
}
return NULL;
}
struct NVNode * bankshot2_get_node(const char *path, struct stat *file_st)
{
struct NVNode *node = NULL;
int i;
timing_type get_node_time;
BANKSHOT2_START_TIMING(get_node_t, get_node_time);
pthread_spin_lock(&node_lookup_lock);
#if 0
for (i = 0; i < OPEN_MAX; i++)
{
if(_bankshot2_node_lookup[i].serialno == file_st->st_ino) {
DEBUG("File %s is (or was) already open in node %i "
"(this node hasn't been __open'ed yet)! "
"Sharing nodes.\n", path, i);
node = &_bankshot2_node_lookup[i];
break;
}
}
#endif
int index = file_st->st_ino % 1024;
if (_bankshot2_ino_lookup[index]) {
i = _bankshot2_ino_lookup[index];
if(_bankshot2_node_lookup[i].serialno == file_st->st_ino) {
DEBUG("File %s is (or was) already open in node %i "
"(this node hasn't been __open'ed yet)! "
"Sharing nodes.\n", path, i);
node = &_bankshot2_node_lookup[i];
}
}
if(!node) {
DEBUG("File %s is not already open. Allocating new NVNode.\n",
path);
node = bankshot2_allocate_node();
assert(node);
node->length = file_st->st_size;
node->maplength = 0;
node->serialno = file_st->st_ino;
DEBUG("File %s st_ino %llu\n", path, node->serialno);
node->num_extents = 0;
}
node->reference++;
pthread_spin_unlock(&node_lookup_lock);
BANKSHOT2_END_TIMING(get_node_t, get_node_time);
return node;
}
RETT_OPEN _bankshot2_OPEN(INTF_OPEN)
{
int mode = 0;
timing_type open_time;
CHECK_RESOLVE_FILEOPS(_bankshot2_);
BANKSHOT2_START_TIMING(open_t, open_time);
if (path == NULL) {
DEBUG("Invalid path.\n");
errno = EINVAL;
BANKSHOT2_END_TIMING(open_t, open_time);
return -1;
}
DEBUG("\n_bankshot2_OPEN(%s)\n", path);
num_total_open++;
if (!bankshot2_ctrl_fd) {
bankshot2_ctrl_fd = _bankshot2_fileops->OPEN(bankshot2_ctrl_dev,
O_RDWR);
if (!bankshot2_ctrl_fd) {
ERROR("Failed to open bankshot2 ctrl dev.\n");
assert(0);
}
}
DEBUG("Attempting to _bankshot2_OPEN the file \"%s\" with the following flags (0x%X): ", path, oflag);
if((oflag&O_RDWR)||((oflag&O_RDONLY)&&(oflag&O_WRONLY))) {
DEBUG_P("O_RDWR ");
} else if(FLAGS_INCLUDE(oflag,O_WRONLY)) {
DEBUG_P("O_WRONLY ");
} else if(FLAGS_INCLUDE(oflag, O_RDONLY)) {
DEBUG_P("O_RDONLY ");
}
DUMP_FLAGS(oflag,O_APPEND);
DUMP_FLAGS(oflag,O_CREAT);
DUMP_FLAGS(oflag,O_TRUNC);
DUMP_FLAGS(oflag,O_EXCL);
DUMP_FLAGS(oflag,O_SYNC);
DUMP_FLAGS(oflag,O_ASYNC);
DUMP_FLAGS(oflag,O_DSYNC);
DUMP_FLAGS(oflag,O_FSYNC);
DUMP_FLAGS(oflag,O_RSYNC);
DUMP_FLAGS(oflag,O_NOCTTY);
DUMP_FLAGS(oflag,O_NDELAY);
DUMP_FLAGS(oflag,O_NONBLOCK);
DUMP_FLAGS(oflag,O_DIRECTORY);
DUMP_FLAGS(oflag,O_LARGEFILE);
DUMP_FLAGS(oflag,O_NOATIME);
DUMP_FLAGS(oflag,O_DIRECT);
DUMP_FLAGS(oflag,O_NOFOLLOW);
//DUMP_FLAGS(oflag,O_SHLOCK);
//DUMP_FLAGS(oflag,O_EXLOCK);
DEBUG_P("\n");
struct stat file_st;
if(access(path, F_OK)) // file doesn't exist
{
if(FLAGS_INCLUDE(oflag, O_CREAT))
{
DEBUG("File does not exist and is set to be created.\n");
}
else
{
DEBUG("File does not exist and is not set to be created. returning\n");
errno = ENOENT;
BANKSHOT2_END_TIMING(open_t, open_time);
return -1;
}
}
else
{
if(stat(path, &file_st))
{
DEBUG("File exists but failed to get file stats!\n");
errno = EACCES;
BANKSHOT2_END_TIMING(open_t, open_time);
return -1;
}
if(S_ISREG(file_st.st_mode))
{
DEBUG("File at path %s is a regular file, all is well.\n", path);
}
else
{
DEBUG("File at path %s is NOT a regular file! INCONCEIVABLE\n", path);
assert(S_ISREG(file_st.st_mode));
}
}
int result;
struct NVNode* node = NULL;
if(stat(path, &file_st))
{
DEBUG("File didn't exist before calling open.\n");
}
else
{
DEBUG("File exists before we open it. Let's get the lock first.\n");
// Find or allocate a NVNode
node = bankshot2_get_node(path, &file_st);
NVP_LOCK_WR(node->lock);
}
if (FLAGS_INCLUDE(oflag, O_CREAT))
{
va_list arg;
va_start(arg, oflag);
mode = va_arg(arg, int);
va_end(arg);
result = _bankshot2_fileops->OPEN(path, oflag & (~O_APPEND), mode);
} else {
result = _bankshot2_fileops->OPEN(path, oflag & (~O_APPEND));
}
if(result<0)
{
DEBUG("_bankshot2_OPEN->%s_OPEN failed: %s\n", _bankshot2_fileops->name, strerror(errno));
BANKSHOT2_END_TIMING(open_t, open_time);
return result;
}
SANITYCHECK(&_bankshot2_fd_lookup[result] != NULL);