@@ -392,7 +392,7 @@ static inline eio_error_t eio_bring_server_connect(eio_stream_t* this)
392
392
}
393
393
394
394
//Map the file into memory
395
- void * mem = mmap ( NULL , total_mem_req , PROT_READ | PROT_WRITE , MAP_SHARED , bring_fd , 0 );
395
+ void * mem = mmap ( NULL , total_mem_req , PROT_READ | PROT_WRITE , MAP_SHARED , bring_fd , 0 );
396
396
if (mem == MAP_FAILED ){
397
397
ch_log_error ("Could not memory map bring file \"%s\". Error=%s\n" , priv -> name , strerror (errno ));
398
398
result = EIO_EINVALID ;
@@ -487,8 +487,6 @@ static inline eio_error_t eio_bring_server_connect(eio_stream_t* this)
487
487
}
488
488
ch_log_debug1 ("Waiting for client to connect to bring %s...Done\n" , priv -> name );
489
489
490
-
491
-
492
490
priv -> closed = 0 ;
493
491
result = EIO_ENONE ;
494
492
return result ;
@@ -508,31 +506,6 @@ static eio_error_t eio_bring_client_connect(eio_stream_t* this)
508
506
bring_priv_t * priv = IOSTREAM_GET_PRIVATE (this );
509
507
510
508
511
- //Calculate the amount of memory we will need
512
- //Each slot has a requested size, plus some header
513
- const int64_t mem_per_slot = priv -> slot_size + sizeof (bring_slot_header_t );
514
- //Round up each slot so that it's a multiple of 64bits.
515
- const int64_t slot_aligned_size = round_up (mem_per_slot , getpagesize ());
516
- //Figure out the total memory commitment for slots
517
- const int64_t mem_per_ring = slot_aligned_size * priv -> slot_count ;
518
- //Allocate for both server-->client and client-->server connections
519
- const int64_t total_ring_mem = mem_per_ring * 2 ;
520
- //Include the memory required for the headers -- Make sure there's a place for the synchronization pointer
521
- const int64_t header_mem = round_up (sizeof (bring_header_t ),getpagesize ());
522
- //All memory required
523
- const int64_t total_mem_req = total_ring_mem + header_mem ;
524
-
525
- ch_log_debug1 ("Client calculated memory requirements\n" );
526
- ch_log_debug1 ("-------------------------\n" );
527
- ch_log_debug1 ("mem_per_slot %li\n" , mem_per_slot );
528
- ch_log_debug1 ("slot_sligned %li\n" , slot_aligned_size );
529
- ch_log_debug1 ("mem_per_ring %li\n" , mem_per_ring );
530
- ch_log_debug1 ("total_ring_mem %li\n" , total_ring_mem );
531
- ch_log_debug1 ("header_mem %li\n" , header_mem );
532
- ch_log_debug1 ("total_mem_req %li\n" , total_mem_req );
533
- ch_log_debug1 ("-------------------------\n" );
534
-
535
-
536
509
//Now there is a bring file and it should have a header in it
537
510
int bring_fd = shm_open (priv -> name , O_RDWR , (mode_t )(0666 ));
538
511
if (bring_fd < 0 ){
@@ -542,74 +515,74 @@ static eio_error_t eio_bring_client_connect(eio_stream_t* this)
542
515
}
543
516
544
517
ch_log_debug1 ("Doing bring connect client on %s\n" , priv -> name );
545
- //
546
- // //Resize the file big enough to read the bring header only
547
- // if(ftruncate(bring_fd,sizeof(bring_header_t))){
548
- // ch_log_error( "Could not resize shared region \"%s\" to size=%li. Error=%s\n",
549
- // priv->name,
550
- // sizeof(bring_header_t),
551
- // strerror(errno)
552
- // );
553
- // result = EIO_EINVALID;
554
- // goto error_close_file;
555
- // }
556
- //
557
- // //Map the file into memory
558
- // void* mem_tmp = mmap( NULL, sizeof(bring_header_t), PROT_READ, MAP_SHARED, bring_fd, 0);
559
- // if(mem_tmp == MAP_FAILED){
560
- // ch_log_error("Could not memory map bring file \"%s\". Error=%s\n", priv->name, strerror(errno));
561
- // result = EIO_EINVALID;
562
- // goto error_close_file;
563
- // }
564
- //
565
- // //Memory must be page aligned otherwise we're in trouble (TODO - could pass alignment though the file and check..)
566
- // //ch_log_debug3("memory mapped at address =%p\n", mem);
567
- // if( ((uint64_t)mem_tmp) != (((uint64_t)mem_tmp) & ~0xFFF)){
568
- // ch_log_error("Could not memory map bring file \"%s\". Error=%s\n", priv->name, strerror(errno));
569
- // result = EIO_EINVALID;
570
- // goto error_close_file;
571
- // }
572
- //
573
- // //This is a sort of nasty atomic access. Which doesn't leave crap lying around like POSIX semaphores do
574
- // ch_log_debug1("Looking for bring header on %s\n", priv->name);
575
- // bring_header_t* header_tmp_ptr = mem_tmp;
576
- // __sync_synchronize();
577
- // while(header_tmp_ptr->magic != BRING_MAGIC_SERVER){
578
- // __sync_synchronize();
579
- // usleep(100 * 1000);
580
- // }
581
- // ch_log_debug1("Looking for bring header... Done.\n");
582
- //
583
- // bring_header_t header_tmp = *(bring_header_t*)(mem_tmp);
584
- //// ch_log_debug1("Got bring header\n");
585
- //// ch_log_debug1("Client read bring memory offsets\n");
586
- //// ch_log_debug1("-------------------------\n");
587
- //// ch_log_debug1("total_mem %016lx (%li)\n", header_tmp.total_mem, header_tmp.total_mem);
588
- //// ch_log_debug1("rd_slots %016lx (%li)\n", header_tmp.rd_slots, header_tmp.rd_slots);
589
- //// ch_log_debug1("rd_slots_size %016lx (%li)\n", header_tmp.rd_slots_size, header_tmp.rd_slots_size);
590
- //// ch_log_debug1("rd_slots_usr_size %016lx (%li)\n", header_tmp.rd_slot_usr_size, header_tmp.rd_slot_usr_size);
591
- //// ch_log_debug1("rd_mem_start_offset %016lx (%li)\n", header_tmp.rd_mem_start_offset, header_tmp.rd_mem_start_offset);
592
- //// ch_log_debug1("rd_mem_len %016lx (%li)\n", header_tmp.rd_mem_len, header_tmp.rd_mem_len);
593
- //// ch_log_debug1("wr_slots %016lx (%li)\n", header_tmp.wr_slots, header_tmp.wr_slots);
594
- //// ch_log_debug1("wr_slots_size %016lx (%li)\n", header_tmp.wr_slots_size, header_tmp.wr_slots_size);
595
- //// ch_log_debug1("wr_slots_usr_size %016lx (%li)\n", header_tmp.wr_slot_usr_size, header_tmp.wr_slot_usr_size);
596
- //// ch_log_debug1("wr_mem_start_offset %016lx (%li)\n", header_tmp.wr_mem_start_offset,header_tmp.wr_mem_start_offset);
597
- //// ch_log_debug1("wr_mem_len %016lx (%li)\n", header_tmp.wr_mem_len, header_tmp.wr_mem_len);
598
- //// ch_log_debug1("-------------------------\n");
599
- //
600
- // munmap(mem_tmp, sizeof(bring_header_t));//Done with the temporary mapping, do the real one now
601
518
602
- if (ftruncate (bring_fd ,total_mem_req )){
519
+ //Resize the file big enough to read the bring header only
520
+ if (ftruncate (bring_fd ,sizeof (bring_header_t ))){
521
+ ch_log_error ( "Could not resize shared region \"%s\" to size=%li. Error=%s\n" ,
522
+ priv -> name ,
523
+ sizeof (bring_header_t ),
524
+ strerror (errno )
525
+ );
526
+ result = EIO_EINVALID ;
527
+ goto error_close_file ;
528
+ }
529
+
530
+ //Map the file into memory
531
+ void * mem_tmp = mmap ( NULL , sizeof (bring_header_t ), PROT_READ , MAP_SHARED , bring_fd , 0 );
532
+ if (mem_tmp == MAP_FAILED ){
533
+ ch_log_error ("Could not memory map bring file \"%s\". Error=%s\n" , priv -> name , strerror (errno ));
534
+ result = EIO_EINVALID ;
535
+ goto error_close_file ;
536
+ }
537
+
538
+ //Memory must be page aligned otherwise we're in trouble (TODO - could pass alignment though the file and check..)
539
+ //ch_log_debug3("memory mapped at address =%p\n", mem);
540
+ if ( ((uint64_t )mem_tmp ) != (((uint64_t )mem_tmp ) & ~0xFFF )){
541
+ ch_log_error ("Could not memory map bring file \"%s\". Error=%s\n" , priv -> name , strerror (errno ));
542
+ result = EIO_EINVALID ;
543
+ goto error_close_file ;
544
+ }
545
+
546
+ //This is a sort of nasty atomic access. Which doesn't leave crap lying around like POSIX semaphores do
547
+ ch_log_debug1 ("Looking for bring header on %s\n" , priv -> name );
548
+ bring_header_t * header_tmp_ptr = mem_tmp ;
549
+ __sync_synchronize ();
550
+ while (header_tmp_ptr -> magic != BRING_MAGIC_SERVER ){
551
+ __sync_synchronize ();
552
+ usleep (100 * 1000 );
553
+ }
554
+ ch_log_debug1 ("Looking for bring header... Done.\n" );
555
+
556
+ bring_header_t header_tmp = * (bring_header_t * )(mem_tmp );
557
+ // ch_log_debug1("Got bring header\n");
558
+ // ch_log_debug1("Client read bring memory offsets\n");
559
+ // ch_log_debug1("-------------------------\n");
560
+ // ch_log_debug1("total_mem %016lx (%li)\n", header_tmp.total_mem, header_tmp.total_mem);
561
+ // ch_log_debug1("rd_slots %016lx (%li)\n", header_tmp.rd_slots, header_tmp.rd_slots);
562
+ // ch_log_debug1("rd_slots_size %016lx (%li)\n", header_tmp.rd_slots_size, header_tmp.rd_slots_size);
563
+ // ch_log_debug1("rd_slots_usr_size %016lx (%li)\n", header_tmp.rd_slot_usr_size, header_tmp.rd_slot_usr_size);
564
+ // ch_log_debug1("rd_mem_start_offset %016lx (%li)\n", header_tmp.rd_mem_start_offset, header_tmp.rd_mem_start_offset);
565
+ // ch_log_debug1("rd_mem_len %016lx (%li)\n", header_tmp.rd_mem_len, header_tmp.rd_mem_len);
566
+ // ch_log_debug1("wr_slots %016lx (%li)\n", header_tmp.wr_slots, header_tmp.wr_slots);
567
+ // ch_log_debug1("wr_slots_size %016lx (%li)\n", header_tmp.wr_slots_size, header_tmp.wr_slots_size);
568
+ // ch_log_debug1("wr_slots_usr_size %016lx (%li)\n", header_tmp.wr_slot_usr_size, header_tmp.wr_slot_usr_size);
569
+ // ch_log_debug1("wr_mem_start_offset %016lx (%li)\n", header_tmp.wr_mem_start_offset,header_tmp.wr_mem_start_offset);
570
+ // ch_log_debug1("wr_mem_len %016lx (%li)\n", header_tmp.wr_mem_len, header_tmp.wr_mem_len);
571
+ // ch_log_debug1("-------------------------\n");
572
+
573
+ munmap (mem_tmp , sizeof (bring_header_t ));//Done with the temporary mapping, do the real one now
574
+
575
+ if (ftruncate (bring_fd ,header_tmp .total_mem )){
603
576
ch_log_error ( "Could not resize shared region \"%s\" to size=%li. Error=%s\n" ,
604
577
priv -> name ,
605
- total_mem_req ,
578
+ header_tmp . total_mem ,
606
579
strerror (errno )
607
580
);
608
581
result = EIO_EINVALID ;
609
582
goto error_close_file ;
610
583
}
611
584
612
- void * mem = mmap ( NULL , total_mem_req , PROT_READ | PROT_WRITE , MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB , bring_fd , 0 );
585
+ void * mem = mmap ( NULL , header_tmp . total_mem , PROT_READ | PROT_WRITE , MAP_SHARED , bring_fd , 0 );
613
586
if (mem == MAP_FAILED ){
614
587
ch_log_error ("Could not memory map bring file \"%s\". Error=%s\n" , priv -> name , strerror (errno ));
615
588
result = EIO_EINVALID ;
@@ -625,7 +598,7 @@ static eio_error_t eio_bring_client_connect(eio_stream_t* this)
625
598
}
626
599
627
600
//Pin the pages so that they don't get swapped out
628
- if (mlock (mem ,total_mem_req )){
601
+ if (mlock (mem ,header_tmp . total_mem )){
629
602
ch_log_fatal ("Could not lock memory map. Error=%s\n" , strerror (errno ));
630
603
//result = EIO_EINVALID;
631
604
//goto error_unmap_file;
@@ -661,10 +634,10 @@ static eio_error_t eio_bring_client_connect(eio_stream_t* this)
661
634
return EIO_ENONE ;
662
635
663
636
error_unlock_mem :
664
- munlock (mem , total_mem_req );
637
+ munlock (mem , header_tmp . total_mem );
665
638
666
639
//error_unmap_file:
667
- munmap (mem , total_mem_req );
640
+ munmap (mem , header_tmp . total_mem );
668
641
669
642
error_close_file :
670
643
close (bring_fd );
@@ -694,7 +667,7 @@ static eio_error_t bring_construct(eio_stream_t* this, bring_args_t* args)
694
667
bring_priv_t * priv = IOSTREAM_GET_PRIVATE (this );
695
668
696
669
//Make a local copy of the filename in case the supplied name goes away
697
- const uint16_t name_len = strnlen (filename , 1024 ); //Safety bug here? What's a reasonable max on filename size?
670
+ const uint16_t name_len = strlen (filename ); //Safety bug here? What's a reasonable max on filename size?
698
671
priv -> name = calloc (1 , name_len + 1 );
699
672
if (!priv -> name ){
700
673
ch_log_debug3 ("Could allocate filename buffer for file \"%s\". Error=%s\n" , filename , strerror (errno ));
0 commit comments