|
41 | 41 | #include <fstream> |
42 | 42 | #include <iterator> |
43 | 43 | #include <queue> |
| 44 | +#include <sys/mman.h> // For msync |
44 | 45 |
|
45 | 46 | #ifndef MSG_WAITALL |
46 | 47 | #define MSG_WAITALL 0x100 |
@@ -543,7 +544,11 @@ uint64_t ThreadPerConnectionServer::calculate_total_latency(uint64_t base_latenc |
543 | 544 | void ThreadPerConnectionServer::handle_request(int client_fd, int thread_id, ServerRequest& req, ServerResponse& resp) { |
544 | 545 | uint64_t cacheline_addr = req.addr & ~(63ULL); // 64-byte aligned |
545 | 546 | bool had_coherency_miss = false; |
546 | | - |
| 547 | + |
| 548 | + // CRITICAL: Memory barrier before reading from shared memory |
| 549 | + // This ensures we see all updates from other guests |
| 550 | + std::atomic_thread_fence(std::memory_order_seq_cst); |
| 551 | + |
547 | 552 | // Log CXL Type3 operation with detailed information |
548 | 553 | const char* op_name = (req.op_type == 0) ? "CXL_TYPE3_READ" : "CXL_TYPE3_WRITE"; |
549 | 554 |
|
@@ -617,7 +622,12 @@ void ThreadPerConnectionServer::handle_request(int client_fd, int thread_id, Ser |
617 | 622 |
|
618 | 623 | // Read data from shared memory |
619 | 624 | if (!shm_manager->read_cacheline(req.addr, resp.data, req.size)) { |
620 | | - SPDLOG_ERROR("Thread {}: Failed to read from shared memory at 0x{:x}", |
| 625 | + // Force sync before reporting failure |
| 626 | + auto* metadata_ptr = shm_manager->get_cacheline_metadata(cacheline_addr); |
| 627 | + if (metadata_ptr) { |
| 628 | + msync(metadata_ptr, sizeof(CachelineMetadata), MS_INVALIDATE | MS_SYNC); |
| 629 | + } |
| 630 | + SPDLOG_ERROR("Thread {}: Failed to read from shared memory at 0x{:x}", |
621 | 631 | thread_id, req.addr); |
622 | 632 | resp.status = 1; |
623 | 633 | congestion_info.active_requests--; |
@@ -672,14 +682,26 @@ void ThreadPerConnectionServer::handle_request(int client_fd, int thread_id, Ser |
672 | 682 |
|
673 | 683 | // Write data to shared memory |
674 | 684 | if (!shm_manager->write_cacheline(req.addr, req.data, req.size)) { |
675 | | - SPDLOG_ERROR("Thread {}: Failed to write to shared memory at 0x{:x}", |
| 685 | + SPDLOG_ERROR("Thread {}: Failed to write to shared memory at 0x{:x}", |
676 | 686 | thread_id, req.addr); |
677 | 687 | resp.status = 1; |
678 | 688 | congestion_info.active_requests--; |
679 | 689 | return; |
680 | 690 | } |
681 | | - |
682 | | - // SPDLOG_INFO("Thread {}: WRITE completed successfully at addr=0x{:x}, size={}", |
| 691 | + |
| 692 | + // CRITICAL: Force sync to physical memory so other guests can see it |
| 693 | + auto* metadata_ptr = shm_manager->get_cacheline_metadata(cacheline_addr); |
| 694 | + if (metadata_ptr) { |
| 695 | + msync(metadata_ptr, sizeof(CachelineMetadata), MS_SYNC); |
| 696 | + } |
| 697 | + // Force sync the data as well |
| 698 | + void* data_ptr = shm_manager->get_data_area(); |
| 699 | + if (data_ptr) { |
| 700 | + msync((uint8_t*)data_ptr + (cacheline_addr & ~CACHELINE_MASK), CACHELINE_SIZE, MS_SYNC); |
| 701 | + } |
| 702 | + std::atomic_thread_fence(std::memory_order_release); |
| 703 | + |
| 704 | + // SPDLOG_INFO("Thread {}: WRITE completed successfully at addr=0x{:x}, size={}", |
683 | 705 | // thread_id, req.addr, req.size); |
684 | 706 |
|
685 | 707 | // Verify write by reading back |
|
0 commit comments