🐎 Hash a large library's key beside the platform's make of it
make_library hashed the library's key (verified_key: one SHA-256 pass over the bitcode section and the content key, or the whole bytes' key) before new_library, which needs none of it. In a traced Geekbench compute pass on the 780M the hash was about a third of the library makes' CPU time (90 of 288 samples), and a pipeline create that names a library being made waits for all of it. A library of at least 64 KiB now has its key hashed on a scoped thread of its own while new_library runs on the compile thread; both are joined before make_library returns, so the library is stored, and its create answered, with its key known, as before. Below 64 KiB the hash costs less than starting a thread, and runs inline as before. A thread the host refuses hashes the key after the make.
This commit is contained in:
1 parent
0717a8730f
commit
d707b8450b
3 files changed
+131
-11
No files matched your search
@@ -69,6 +69,10 @@ const KEY_PIECE: usize = 256 << 10;
|
||||
/// for that key of no larger one (its `kRgfxWholeKeyMost`).
|
||||
const WHOLE_KEY_MOST: usize = 4 << 20;
|
||||
|
||||
/// The fewest bytes of a library whose key is hashed on a thread of its own beside the platform's
|
||||
/// make of it ([`make_library`]): below them, the hash takes less time than starting the thread.
|
||||
pub(crate) const KEY_BESIDE_MIN: usize = 64 << 10;
|
||||
|
||||
/// The most bytes of keyed libraries the cache keeps once no id names them.
|
||||
pub(crate) const RELEASED_BUDGET: u64 = 64 << 20;
|
||||
|
||||
@@ -441,23 +445,39 @@ impl<P: Platform> MadeLibrary<P> {
|
||||
}
|
||||
|
||||
/// The middle step of a library create, with no lock held: the key checked and the library made
|
||||
/// from the bytes, unchanged. A platform raise propagates; the caller then stores
|
||||
/// from the bytes, unchanged. The platform's make needs nothing of the key, so the key is hashed
|
||||
/// beside it, on a thread of its own, where the bytes are at least [`KEY_BESIDE_MIN`], and
|
||||
/// joined before this returns: the library is stored, and its create answered, with its key
|
||||
/// known, as when the key is hashed first. A platform raise propagates; the caller then stores
|
||||
/// [`MadeLibrary::refused`].
|
||||
pub(crate) fn make_library<P: Platform>(
|
||||
device: &P::Device,
|
||||
job: &LibraryJob<P>,
|
||||
) -> RaiseResult<MadeLibrary<P>> {
|
||||
// Under the guest's key where the host vouches for it, else, where it cannot, under the key of
|
||||
// the whole bytes.
|
||||
let key = job.guest_key.and_then(|guest| {
|
||||
let key = match verified_key(&job.bytes) {
|
||||
Some(host) => (host == guest).then_some(host)?,
|
||||
None => whole_key(&job.bytes)?,
|
||||
};
|
||||
Some((key, job.bytes.len() as u64))
|
||||
});
|
||||
let bytes = job.bytes.as_slice();
|
||||
let (key, made) = match job.guest_key {
|
||||
Some(guest) if bytes.len() >= KEY_BESIDE_MIN => std::thread::scope(|scope| {
|
||||
let hashing = std::thread::Builder::new()
|
||||
.name("reims-vgpu.graphics-key".into())
|
||||
.spawn_scoped(scope, move || library_key(guest, bytes));
|
||||
let made = device.new_library(bytes);
|
||||
let key = match hashing {
|
||||
// The hash panics nowhere; a panic there unwinds here, as it would inline.
|
||||
Ok(hashing) => hashing
|
||||
.join()
|
||||
.unwrap_or_else(|p| std::panic::resume_unwind(p)),
|
||||
// No thread to hash on: the key is hashed after the make.
|
||||
Err(_) => library_key(guest, bytes),
|
||||
};
|
||||
(key, made)
|
||||
}),
|
||||
guest => (
|
||||
guest.and_then(|guest| library_key(guest, bytes)),
|
||||
device.new_library(bytes),
|
||||
),
|
||||
};
|
||||
// A nil library and a build failure both mean no usable library.
|
||||
let (library, refusal) = match device.new_library(&job.bytes)? {
|
||||
let (library, refusal) = match made? {
|
||||
Ok(Some(library)) => (Some(library), None),
|
||||
Ok(None) => (None, Some("the platform answered nil".to_owned())),
|
||||
Err(e) => (
|
||||
@@ -475,6 +495,18 @@ pub(crate) fn make_library<P: Platform>(
|
||||
})
|
||||
}
|
||||
|
||||
/// The key a library of `bytes`, which the guest sent with key `guest`, is kept under, and the
|
||||
/// bytes' length: the guest's key where the host vouches for it ([`verified_key`]), else, where it
|
||||
/// cannot vouch for any, the key of the whole bytes ([`whole_key`]); none where the host vouches
|
||||
/// for another key than the guest's, or for none and the bytes have no whole key.
|
||||
fn library_key(guest: LibraryKey, bytes: &[u8]) -> Option<(LibraryKey, u64)> {
|
||||
let key = match verified_key(bytes) {
|
||||
Some(host) => (host == guest).then_some(host)?,
|
||||
None => whole_key(bytes)?,
|
||||
};
|
||||
Some((key, bytes.len() as u64))
|
||||
}
|
||||
|
||||
/// A `COMPLETE` for the create at `seqno`.
|
||||
pub(crate) fn complete(seqno: u64, status: u32) -> RgfxComplete {
|
||||
RgfxComplete {
|
||||
|
||||
@@ -219,6 +219,8 @@ pub(crate) use function_table::named_pipeline as function_table_pipeline;
|
||||
pub(crate) use lane::{LANE_MOST, LANE_THREADS};
|
||||
use lane::{Lane, Turn};
|
||||
#[cfg(test)]
|
||||
pub(crate) use library::KEY_BESIDE_MIN as TEST_KEY_BESIDE_MIN;
|
||||
#[cfg(test)]
|
||||
pub(crate) use library::tests::{metallib as test_metallib, sha as test_sha};
|
||||
#[cfg(test)]
|
||||
pub(crate) use library::verified_key as test_key;
|
||||
|
||||
@@ -6509,6 +6509,92 @@ fn a_library_with_a_wrong_key_is_not_kept() {
|
||||
assert_eq!(library_makes(&platform), 2);
|
||||
}
|
||||
|
||||
/// A library of at least `KEY_BESIDE_MIN` bytes has its key hashed on a thread of its own beside
|
||||
/// the platform's make of it, and is stored and answered once both are done, as one hashed first
|
||||
/// is: sent as the bundle sends a large library, in a chunk from its process's block, it is kept
|
||||
/// under its key by the time its create is answered, so another process's lookup right after
|
||||
/// finds it with nothing made again; one sent with another key is made and kept under none.
|
||||
#[test]
|
||||
fn a_large_library_keyed_beside_its_make_is_kept_under_its_key_when_answered() {
|
||||
let raised = Arc::new(AtomicU32::new(0));
|
||||
let (device, platform) = headless_on(&raised);
|
||||
device.mmio_write(RGFX_REG_STATUS.into(), RGFX_STATUS_DRIVER_READY);
|
||||
let mut guest = Guest::new(&device);
|
||||
let bitcode: Vec<u8> = (0..decode::TEST_KEY_BESIDE_MIN + 4096)
|
||||
.map(|i| (i % 251) as u8)
|
||||
.collect();
|
||||
let half = bitcode.len() / 2;
|
||||
let len = |n: usize| u64::try_from(n).expect("a length");
|
||||
let bytes = decode::test_metallib(
|
||||
&bitcode,
|
||||
&[
|
||||
(0, len(half), decode::test_sha(&bitcode[..half])),
|
||||
(
|
||||
len(half),
|
||||
len(bitcode.len() - half),
|
||||
decode::test_sha(&bitcode[half..]),
|
||||
),
|
||||
],
|
||||
);
|
||||
assert!(bytes.len() >= decode::TEST_KEY_BESIDE_MIN);
|
||||
let key = decode::test_key(&bytes).expect("the large library's key");
|
||||
const BLOCK: u64 = RGFX_CONTEXT_ALLOCATIONS_OFFSET + 0x10_0000;
|
||||
const AT: u64 = BLOCK + 0x1000;
|
||||
// SAFETY: as `Guest`'s helpers; the span lies in the region.
|
||||
unsafe { guest.write(AT as usize, &bytes) };
|
||||
guest.send(&device, block_grant(1, BLOCK, 0x4_0000));
|
||||
let create = |obj_id: u64, key: [u8; 32]| {
|
||||
RgfxLibraryFromRegion {
|
||||
header: RgfxMsgHeader {
|
||||
r#type: RGFX_CMD_LIBRARY_FROM_REGION,
|
||||
size: 0,
|
||||
seqno: 0,
|
||||
},
|
||||
obj_id,
|
||||
region_offset: AT,
|
||||
byte_length: len(bytes.len()),
|
||||
total_length: len(bytes.len()),
|
||||
chunk_offset: 0,
|
||||
ack_offset: BLOCK,
|
||||
ack_value: 7,
|
||||
flags: RGFX_LIBRARY_FLAG_KEYED,
|
||||
key,
|
||||
..RgfxLibraryFromRegion::default()
|
||||
}
|
||||
.as_bytes()
|
||||
.to_vec()
|
||||
};
|
||||
assert_eq!(
|
||||
answered(&device, &mut guest, create(obj(1, 10), key)),
|
||||
RGFX_STATUS_OK
|
||||
);
|
||||
assert_eq!(
|
||||
answered(
|
||||
&device,
|
||||
&mut guest,
|
||||
library_lookup(obj(2, 10), bytes.len(), key)
|
||||
),
|
||||
RGFX_STATUS_OK
|
||||
);
|
||||
assert_eq!(library_makes(&platform), 1);
|
||||
// Another key: made, and kept under neither.
|
||||
let mut wrong = key;
|
||||
wrong[0] ^= 1;
|
||||
assert_eq!(
|
||||
answered(&device, &mut guest, create(obj(1, 11), wrong)),
|
||||
RGFX_STATUS_OK
|
||||
);
|
||||
assert_eq!(
|
||||
answered(
|
||||
&device,
|
||||
&mut guest,
|
||||
library_lookup(obj(2, 11), bytes.len(), wrong)
|
||||
),
|
||||
RGFX_STATUS_NOT_FOUND
|
||||
);
|
||||
assert_eq!(library_makes(&platform), 2);
|
||||
}
|
||||
|
||||
/// A library whose tags the host cannot vouch for (Core Image's: they are no SHA-256 of their
|
||||
/// bitcode) is kept under the key of its whole bytes instead of the guest's: a lookup of the
|
||||
/// guest's key misses, one of the whole bytes' key, from another process, finds it with nothing
|
||||
|
||||
Reference in new issue
Block a user