improving device allocator
This commit is contained in:
@@ -61,17 +61,9 @@ jobs:
|
|||||||
which deqp-runner && deqp-runner --version || echo "deqp-runner not found"
|
which deqp-runner && deqp-runner --version || echo "deqp-runner not found"
|
||||||
|
|
||||||
- name: Run Vulkan CTS
|
- name: Run Vulkan CTS
|
||||||
run: zig build cts-soft --release=fast -Ddebug-allocator=true -- -j1
|
run: zig build cts-soft --release=fast -- -j3
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
|
|
||||||
- name: Upload CTS runner logs
|
|
||||||
if: always()
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: cts-runner-logs
|
|
||||||
path: |
|
|
||||||
cts/results.csv
|
|
||||||
|
|
||||||
- name: Verify tests
|
- name: Verify tests
|
||||||
run: ls cts | grep "results.csv";
|
run: ls cts | grep "results.csv";
|
||||||
|
|
||||||
|
|||||||
@@ -325,6 +325,7 @@ fn addMultithreadedCTS(b: *std.Build, target: std.Build.ResolvedTarget, impl: *c
|
|||||||
run.step.dependOn(&impl_lib.step);
|
run.step.dependOn(&impl_lib.step);
|
||||||
|
|
||||||
run.addArg("run");
|
run.addArg("run");
|
||||||
|
run.addArg("--verbose");
|
||||||
run.addArg("--deqp");
|
run.addArg("--deqp");
|
||||||
run.addArg(cts_exe_path);
|
run.addArg(cts_exe_path);
|
||||||
run.addArg("--caselist");
|
run.addArg("--caselist");
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ const SpawnError = std.Thread.SpawnError;
|
|||||||
|
|
||||||
const DeviceAllocator = struct {
|
const DeviceAllocator = struct {
|
||||||
pub inline fn allocator(_: @This()) std.mem.Allocator {
|
pub inline fn allocator(_: @This()) std.mem.Allocator {
|
||||||
return std.heap.smp_allocator;
|
return base.fallback_host_allocator;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,27 +4,21 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const vk = @import("vulkan");
|
const vk = @import("vulkan");
|
||||||
|
|
||||||
|
const fallback_host_allocator = @import("fallback_host_allocator.zig").fallback_host_allocator;
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const Alignment = std.mem.Alignment;
|
const Alignment = std.mem.Alignment;
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
const FallbackAllocator = struct {
|
|
||||||
pub inline fn allocator(_: @This()) std.mem.Allocator {
|
|
||||||
return std.heap.smp_allocator;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
callbacks: ?vk.AllocationCallbacks,
|
callbacks: ?vk.AllocationCallbacks,
|
||||||
scope: vk.SystemAllocationScope,
|
scope: vk.SystemAllocationScope,
|
||||||
fallback_allocator: FallbackAllocator,
|
|
||||||
|
|
||||||
pub fn init(callbacks: ?*const vk.AllocationCallbacks, scope: vk.SystemAllocationScope) Self {
|
pub fn init(callbacks: ?*const vk.AllocationCallbacks, scope: vk.SystemAllocationScope) Self {
|
||||||
const deref_callbacks = if (callbacks) |c| c.* else null;
|
const deref_callbacks = if (callbacks) |c| c.* else null;
|
||||||
return .{
|
return .{
|
||||||
.callbacks = deref_callbacks,
|
.callbacks = deref_callbacks,
|
||||||
.scope = scope,
|
.scope = scope,
|
||||||
.fallback_allocator = .{},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +47,6 @@ pub fn cloneWithScope(self: *Self, scope: vk.SystemAllocationScope) Self {
|
|||||||
return .{
|
return .{
|
||||||
.callbacks = self.callbacks,
|
.callbacks = self.callbacks,
|
||||||
.scope = scope,
|
.scope = scope,
|
||||||
.fallback_allocator = self.fallback_allocator,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +62,7 @@ fn alloc(context: *anyopaque, len: usize, alignment: Alignment, ret_addr: usize)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.getFallbackAllocator().rawAlloc(len, alignment, ret_addr);
|
return fallback_host_allocator.rawAlloc(len, alignment, ret_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool {
|
fn resize(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool {
|
||||||
@@ -77,7 +70,7 @@ fn resize(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize,
|
|||||||
return if (self.callbacks != null)
|
return if (self.callbacks != null)
|
||||||
new_len <= ptr.len
|
new_len <= ptr.len
|
||||||
else
|
else
|
||||||
self.getFallbackAllocator().rawResize(ptr, alignment, new_len, ret_addr);
|
fallback_host_allocator.rawResize(ptr, alignment, new_len, ret_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remap(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
|
fn remap(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
|
||||||
@@ -87,7 +80,7 @@ fn remap(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, r
|
|||||||
return @ptrCast(pfn_reallocation(self.callbacks.?.p_user_data, ptr.ptr, new_len, alignment.toByteUnits(), self.scope));
|
return @ptrCast(pfn_reallocation(self.callbacks.?.p_user_data, ptr.ptr, new_len, alignment.toByteUnits(), self.scope));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self.getFallbackAllocator().rawRemap(ptr, alignment, new_len, ret_addr);
|
return fallback_host_allocator.rawRemap(ptr, alignment, new_len, ret_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn free(context: *anyopaque, ptr: []u8, alignment: Alignment, ret_addr: usize) void {
|
fn free(context: *anyopaque, ptr: []u8, alignment: Alignment, ret_addr: usize) void {
|
||||||
@@ -98,9 +91,5 @@ fn free(context: *anyopaque, ptr: []u8, alignment: Alignment, ret_addr: usize) v
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.getFallbackAllocator().rawFree(ptr, alignment, ret_addr);
|
fallback_host_allocator.rawFree(ptr, alignment, ret_addr);
|
||||||
}
|
|
||||||
|
|
||||||
inline fn getFallbackAllocator(self: *Self) std.mem.Allocator {
|
|
||||||
return self.fallback_allocator.allocator();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const Alignment = std.mem.Alignment;
|
||||||
|
|
||||||
|
/// Atomic based spin mutex
|
||||||
|
const AtomicMutex = struct {
|
||||||
|
mutex: std.atomic.Mutex = .unlocked,
|
||||||
|
|
||||||
|
fn lock(self: *@This()) void {
|
||||||
|
if (self.mutex.tryLock()) {
|
||||||
|
@branchHint(.likely);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (self.mutex.tryLock()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unlock(self: *@This()) void {
|
||||||
|
self.mutex.unlock();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var mutex: AtomicMutex = .{};
|
||||||
|
var child_allocator: std.mem.Allocator = if (builtin.link_libc) std.heap.c_allocator else std.heap.smp_allocator;
|
||||||
|
|
||||||
|
pub const fallback_host_allocator: Allocator = .{
|
||||||
|
.ptr = undefined,
|
||||||
|
.vtable = &vtable,
|
||||||
|
};
|
||||||
|
|
||||||
|
const vtable: Allocator.VTable = .{
|
||||||
|
.alloc = alloc,
|
||||||
|
.resize = resize,
|
||||||
|
.remap = remap,
|
||||||
|
.free = free,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn alloc(_: *anyopaque, len: usize, alignment: Alignment, ret_addr: usize) ?[*]u8 {
|
||||||
|
mutex.lock();
|
||||||
|
defer mutex.unlock();
|
||||||
|
return child_allocator.rawAlloc(len, alignment, ret_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resize(_: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool {
|
||||||
|
mutex.lock();
|
||||||
|
defer mutex.unlock();
|
||||||
|
return child_allocator.rawResize(ptr, alignment, new_len, ret_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remap(_: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
|
||||||
|
mutex.lock();
|
||||||
|
defer mutex.unlock();
|
||||||
|
return child_allocator.rawRemap(ptr, alignment, new_len, ret_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn free(_: *anyopaque, ptr: []u8, alignment: Alignment, ret_addr: usize) void {
|
||||||
|
mutex.lock();
|
||||||
|
defer mutex.unlock();
|
||||||
|
return child_allocator.rawFree(ptr, alignment, ret_addr);
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ pub const format = @import("format.zig");
|
|||||||
pub const config = @import("config");
|
pub const config = @import("config");
|
||||||
|
|
||||||
pub const Dispatchable = @import("Dispatchable.zig").Dispatchable;
|
pub const Dispatchable = @import("Dispatchable.zig").Dispatchable;
|
||||||
|
pub const fallback_host_allocator = @import("fallback_host_allocator.zig").fallback_host_allocator;
|
||||||
pub const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable;
|
pub const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable;
|
||||||
pub const VkError = errors.VkError;
|
pub const VkError = errors.VkError;
|
||||||
pub const VulkanAllocator = @import("VulkanAllocator.zig");
|
pub const VulkanAllocator = @import("VulkanAllocator.zig");
|
||||||
|
|||||||
Reference in New Issue
Block a user