This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/device/directio.h
T
aneinanderandClaude Opus 5.5 8931aae8b2 sycl: keep the device out of plain system memory
On kernel 7.0 the xe driver can oops (drm_pagemap_zdd_destroy from
__drm_pagemap_migrate_to_ram) when the CPU faults on a page its shared
virtual memory had migrated into VRAM. The backend reached plain system
memory in several ways; each now uses USM the runtime knows:

- RAD_MEM_HOST_PINNED/_MAPPED are sycl::aligned_alloc_host, not
  posix_memalign + prepare_for_device_copy. O_DIRECT cannot target those
  pages (EFAULT), so DirectFile bounces such transfers through an 8 MiB
  buffer. The 27B load is not slower (10.8 s read, disk-bound).
- Copies with a pageable side are staged through a host-USM bounce,
  synchronously (rare: startup and tests). The ranged copy stages a host
  range list even when its source is on the card, and refuses a pageable
  destination, which its kernel would write.
- scatter_rows' bad-index flag is host USM, not shared.
- Gates are gone (the documented state): a spinning kernel never sees a
  host store to host USM, and a device flag opened by a copy from another
  queue deadlocks behind copies queued past the gate. rad-kbench times
  ungated; the gate tests skip on backends without gates.

Checked: device_test (sycl) 70 cases, 0 failed; rad-kbench fixture
1989/1989; 27B int4 chat output byte-identical (c4c5ba5e727c) at
30.6 tok/s; no xe messages in the kernel log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 17:56:05 +01:00

77 lines
3.0 KiB
C++

/* directio.h -- the file tier's reader (spec §5.4): O_DIRECT reads into pinned buffers, used by
* the weight tier and the KV tier.
*
* The page cache is avoided for two reasons:
* - it is a second copy of every byte already headed for a pinned buffer;
* - it evicts. A large tier read through the page cache throws out the mapped `.rad` pool's
* pages, which shows up later as an untraceable decode stall.
*
* O_DIRECT requires the file offset, transfer length and buffer address to be block-aligned.
* rad_dev_alloc memory is (host.cpp) and .rad pads movement units to RAD_ALIGN_UNIT
* (rad_format.h), so every mover read takes the unbounced path. The bounce is for other callers,
* and for pinned memory O_DIRECT cannot target (EFAULT: a GPU driver's own mapping, such as the
* SYCL backend's malloc_host), which is bounced whole.
*/
#pragma once
#include <atomic>
#include <cstdint>
namespace rad {
/* Bytes the drive has returned through this reader, monotonically. The stall
* watchdog (aql.cpp) counts this alongside the queues' read indices: a host wait
* that moves nothing anywhere -- no queue advanced, no read came back -- is the
* only one that reports a stall. */
inline std::atomic<uint64_t> dio_progress{0};
enum {
RAD_DIO_READ = 1,
RAD_DIO_WRITE = 2, /* implies read: a partial block is a read-modify-write */
RAD_DIO_CREATE = 4,
RAD_DIO_TRUNC = 8,
/* Open buffered when the filesystem refuses O_DIRECT, instead of failing. Off by default: a
* silent buffered fallback makes the weight tier ~3x slower and is invisible in a profile.
* For tests, which run on whatever /tmp is. */
RAD_DIO_ALLOW_BUFFERED = 16,
};
class DirectFile {
public:
DirectFile() = default;
~DirectFile();
DirectFile(const DirectFile&) = delete;
DirectFile& operator=(const DirectFile&) = delete;
/* RAD_OK, or a negative RAD_E_*. `flags` is the RAD_DIO_* set above. */
int open(const char* path, int flags);
void close();
bool is_open() const { return fd_ >= 0; }
/* Whether the open actually got O_DIRECT. False only under RAD_DIO_ALLOW_BUFFERED. */
bool direct() const { return direct_; }
int fd() const { return fd_; }
int64_t size() const;
/* Exact reads and writes: a short transfer is RAD_E_IO, never a partial success, so callers
* need no byte-count check. */
int read_at(void* dst, int64_t off, int64_t bytes);
int write_at(const void* src, int64_t off, int64_t bytes);
/* The alignment every fast-path extent must satisfy. A fixed 4096 rather than the device's
* logical block size (often 512, see STATX_DIOALIGN): it covers every real requirement, equals
* RAD_ALIGN_UNIT, and keeps behaviour independent of the filesystem. */
static constexpr int64_t alignment() { return 4096; }
private:
int bounce_ready();
int bounce_read(void* dst, int64_t off, int64_t bytes);
int fd_ = -1;
bool direct_ = false;
uint8_t* bounce_ = nullptr;
int64_t bounce_bytes_ = 0;
};
} /* namespace rad */