| 2783 | } |
| 2784 | |
| 2785 | bool FileExpertSource::open_direct(std::string& why) { |
| 2786 | #if defined(_WIN32) |
| 2787 | direct_sector_ = 4096; |
| 2788 | for (const std::string& path : paths_) { |
| 2789 | const int wide = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, nullptr, 0); |
| 2790 | std::vector<wchar_t> w((size_t) (wide > 0 ? wide : 1), L'\0'); |
| 2791 | if (wide > 0) MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, w.data(), wide); |
| 2792 | HANDLE h = CreateFileW(w.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 2793 | FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, nullptr); |
| 2794 | if (h == INVALID_HANDLE_VALUE) { |
| 2795 | why += "; cannot open " + path + " unbuffered (error " + std::to_string((unsigned long long) GetLastError()) + |
| 2796 | "), read through the file cache"; |
| 2797 | for (void* d : direct_) CloseHandle((HANDLE) d); |
| 2798 | direct_.clear(); |
| 2799 | return false; |
| 2800 | } |
| 2801 | FILE_STORAGE_INFO fsi{}; |
| 2802 | if (GetFileInformationByHandleEx(h, FileStorageInfo, &fsi, sizeof(fsi))) { |
| 2803 | uint64_t need = std::max<uint64_t>({4096ull, fsi.LogicalBytesPerSector, fsi.PhysicalBytesPerSectorForAtomicity}); |
| 2804 | if (need > (1ull << 20)) need = (1ull << 20); |
| 2805 | uint64_t p2 = 4096ull; |
| 2806 | while (p2 < need) p2 <<= 1; |
| 2807 | direct_sector_ = std::max(direct_sector_, p2); |
| 2808 | } |
| 2809 | direct_.push_back(h); |
| 2810 | } |
| 2811 | if (role_ptr_.empty()) { // experts.bin: blob() now assembles into the stage buffers, sized for the largest blob |
| 2812 | std::lock_guard<std::mutex> lk(stage_mu_); |
| 2813 | for (uint64_t b : layer_blob_bytes_) stage_blob_ = std::max(stage_blob_, b); |
| 2814 | } |
| 2815 | return true; |
| 2816 | #elif defined(__linux__) |
| 2817 | // O_DIRECT: the reads bypass the page cache, as FILE_FLAG_NO_BUFFERING does (4 KiB-aligned windows, read_direct) |
| 2818 | for (const std::string& path : paths_) { |
| 2819 | const int fd = ::open(path.c_str(), O_RDONLY | O_DIRECT | O_CLOEXEC); |
| 2820 | if (fd < 0) { |
| 2821 | why += "; cannot open " + path + " with O_DIRECT (" + std::strerror(errno) + "), read through the file cache"; |
| 2822 | for (void* d : direct_) close_direct(d); |
| 2823 | direct_.clear(); |
| 2824 | return false; |
| 2825 | } |
| 2826 | direct_.push_back((void*) (intptr_t) fd); |
| 2827 | } |
| 2828 | if (role_ptr_.empty()) { // experts.bin: blob() now assembles into the stage buffers, sized for the largest blob |
| 2829 | std::lock_guard<std::mutex> lk(stage_mu_); |
| 2830 | for (uint64_t b : layer_blob_bytes_) stage_blob_ = std::max(stage_blob_, b); |
| 2831 | } |
| 2832 | return true; |
| 2833 | #else |
| 2834 | (void) why; |
| 2835 | return false; |
| 2836 | #endif |
| 2837 | } |
| 2838 | |
| 2839 | bool FileExpertSource::open_prompt_direct(std::string& why) { |
| 2840 | #if defined(__linux__) |