Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show task performance #445

Merged
merged 26 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5acf1f5
threadpool: track and show task times
archbirdplus Jun 3, 2024
ad1aa46
debug window: subdivide task performance
archbirdplus Jun 4, 2024
af2962b
indent debug window code
archbirdplus Jun 4, 2024
ae11566
task perfs: remove meshgen for lighting
archbirdplus Jun 5, 2024
ce26e55
task perfs: don't divide by zero
archbirdplus Jun 5, 2024
93a9d04
task perf: move taskType to vtable
archbirdplus Jun 5, 2024
07c89ee
merge master
archbirdplus Oct 6, 2024
993157a
use directEnumArrayLen instead of C-style length
archbirdplus Oct 6, 2024
af927fa
use @tagName for task type classes
archbirdplus Oct 6, 2024
0f705ab
switch to atomics
archbirdplus Oct 6, 2024
abd9281
recategorize LightRefreshTaks, LightMapLoadTask under misc
archbirdplus Oct 6, 2024
61b94bb
add frameCount
archbirdplus Oct 6, 2024
ad81468
update perfs every perfUpdateFrequency frames
archbirdplus Oct 6, 2024
10d7bc3
restore 4k pageSize
archbirdplus Oct 6, 2024
4b609e7
lighting -> meshgenAndLighting
archbirdplus Oct 8, 2024
5e8d166
reset counts on open rather than by time
archbirdplus Oct 10, 2024
ad7fa9d
revert to mutex Performance
archbirdplus Oct 10, 2024
4ac3709
restore 4096 pageSize
archbirdplus Oct 10, 2024
e0b27dc
cleanup
archbirdplus Oct 10, 2024
0ee00d0
correctly use Performance init
archbirdplus Oct 10, 2024
b77a076
track task time per frame properly
archbirdplus Oct 10, 2024
e5144ec
just copy.*
archbirdplus Oct 13, 2024
5fd1127
use % total instead of % frame time
archbirdplus Oct 13, 2024
01358f2
capitalize Total task time
archbirdplus Oct 13, 2024
85422a8
remove total task time
archbirdplus Oct 21, 2024
d6989ad
destroy performance on deinit
archbirdplus Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/audio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const MusicLoadTask = struct {
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .misc,
};

pub fn schedule(musicId: []const u8) void {
Expand Down
19 changes: 19 additions & 0 deletions src/gui/windows/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ const graphics = main.graphics;
const draw = graphics.draw;
const Texture = graphics.Texture;
const Vec2f = main.vec.Vec2f;
const TaskType = main.utils.ThreadPool.TaskType;

const gui = @import("../gui.zig");
const GuiWindow = gui.GuiWindow;
const GuiComponent = gui.GuiComponent;


pub fn onOpen() void {
main.threadPool.performance.clear();
}

pub var window = GuiWindow {
.relativePosition = .{
.{ .attachedToFrame = .{.selfAttachmentPoint = .lower, .otherAttachmentPoint = .lower} },
Expand Down Expand Up @@ -61,6 +67,19 @@ pub fn render() void {
y += 8;
draw.print("Queue size: {}", .{main.threadPool.queueSize()}, 0, y, 8, .left);
y += 8;
const perf = main.threadPool.performance.read();
const values = comptime std.enums.values(TaskType);
var totalUtime: i64 = 0;
for(values) |task|
totalUtime += perf.utime[@intFromEnum(task)];
for(values) |t| {
const name = @tagName(t);
const i = @intFromEnum(t);
const taskTime = @divFloor(perf.utime[i], @max(1, perf.tasks[i]));
const relativeTime = 100.0 * @as(f32, @floatFromInt(perf.utime[i])) / @as(f32, @floatFromInt(totalUtime));
draw.print(" {s}: {} µs/task ({d:.1}%)", .{name, taskTime, relativeTime}, 0, y, 8, .left);
y += 8;
}
draw.print("Mesh Queue size: {}", .{main.renderer.mesh_storage.updatableList.items.len}, 0, y, 8, .left);
y += 8;
{
Expand Down
1 change: 1 addition & 0 deletions src/network.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,7 @@ const ProtocolTask = struct {
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .misc,
};

pub fn schedule(conn: *Connection, protocol: u8, data: []const u8) void {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/chunk_meshing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ pub const ChunkMesh = struct { // MARK: ChunkMesh
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .misc,
};

pub fn scheduleAndDecreaseRefCount(mesh: *ChunkMesh) void {
Expand Down Expand Up @@ -1618,4 +1619,4 @@ pub const ChunkMesh = struct { // MARK: ChunkMesh
chunkList.append(self.chunkAllocation.start);
transparentQuadsDrawn += self.culledSortingCount;
}
};
};
3 changes: 2 additions & 1 deletion src/renderer/mesh_storage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ pub const MeshGenerationTask = struct { // MARK: MeshGenerationTask
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .meshgenAndLighting,
};

pub fn schedule(mesh: *chunk.Chunk) void {
Expand Down Expand Up @@ -937,4 +938,4 @@ pub fn updateLightMap(map: *LightMap.LightMapFragment) void {
mutex.lock();
defer mutex.unlock();
mapUpdatableList.append(map);
}
}
3 changes: 3 additions & 0 deletions src/server/world.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const ChunkManager = struct { // MARK: ChunkManager
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .chunkgen,
};

pub fn scheduleAndDecreaseRefCount(pos: ChunkPosition, source: Source) void {
Expand Down Expand Up @@ -201,6 +202,7 @@ const ChunkManager = struct { // MARK: ChunkManager
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .misc,
};

pub fn scheduleAndDecreaseRefCount(pos: terrain.SurfaceMap.MapFragmentPosition, source: ?*User) void {
Expand Down Expand Up @@ -570,6 +572,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
.isStillNeeded = @ptrCast(&isStillNeeded),
.run = @ptrCast(&run),
.clean = @ptrCast(&clean),
.taskType = .chunkgen,
};

pub fn schedule(pos: ChunkPosition, storeMaps: bool) void {
Expand Down
48 changes: 48 additions & 0 deletions src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,12 @@ pub fn BlockingMaxHeap(comptime T: type) type { // MARK: BlockingMaxHeap
}

pub const ThreadPool = struct { // MARK: ThreadPool
pub const TaskType = enum(usize) {
chunkgen,
meshgenAndLighting,
misc,
};
pub const taskTypes = std.enums.directEnumArrayLen(TaskType, 0);
const Task = struct {
cachedPriority: f32,
self: *anyopaque,
Expand All @@ -1024,6 +1030,41 @@ pub const ThreadPool = struct { // MARK: ThreadPool
isStillNeeded: *const fn(*anyopaque, milliTime: i64) bool,
run: *const fn(*anyopaque) void,
clean: *const fn(*anyopaque) void,
taskType: TaskType = .misc,
};
pub const Performance = struct {
mutex: std.Thread.Mutex = .{},
archbirdplus marked this conversation as resolved.
Show resolved Hide resolved
tasks: [taskTypes]u32,
utime: [taskTypes]i64,
archbirdplus marked this conversation as resolved.
Show resolved Hide resolved

fn add(self: *Performance, task: TaskType, time: i64) void {
self.mutex.lock();
defer self.mutex.unlock();
const i = @intFromEnum(task);
self.tasks[i] += 1;
self.utime[i] += time;
}

pub fn clear(self: *Performance) void {
self.mutex.lock();
defer self.mutex.unlock();
for(0..taskTypes) |i| {
self.tasks[i] = 0;
self.utime[i] = 0;
}
}

fn init(allocator: NeverFailingAllocator) *Performance {
const self = allocator.create(Performance);
self.clear();
return self;
}

pub fn read(self: *Performance) Performance {
self.mutex.lock();
defer self.mutex.unlock();
return self.*;
}
};
const refreshTime: u32 = 100; // The time after which all priorities get refreshed in milliseconds.

Expand All @@ -1032,11 +1073,14 @@ pub const ThreadPool = struct { // MARK: ThreadPool
loadList: *BlockingMaxHeap(Task),
allocator: NeverFailingAllocator,

performance: *Performance,

pub fn init(allocator: NeverFailingAllocator, threadCount: usize) ThreadPool {
const self = ThreadPool {
.threads = allocator.alloc(std.Thread, threadCount),
.currentTasks = allocator.alloc(Atomic(?*const VTable), threadCount),
.loadList = BlockingMaxHeap(Task).init(allocator),
.performance = Performance.init(allocator),
.allocator = allocator,
};
for(self.threads, 0..) |*thread, i| {
Expand Down Expand Up @@ -1064,6 +1108,7 @@ pub const ThreadPool = struct { // MARK: ThreadPool
}
self.allocator.free(self.currentTasks);
self.allocator.free(self.threads);
self.allocator.destroy(self.performance);
}

pub fn closeAllTasksOfType(self: ThreadPool, vtable: *const VTable) void {
Expand Down Expand Up @@ -1098,7 +1143,10 @@ pub const ThreadPool = struct { // MARK: ThreadPool
{
const task = self.loadList.extractMax() catch break;
self.currentTasks[id].store(task.vtable, .monotonic);
const start = std.time.microTimestamp();
task.vtable.run(task.self);
const end = std.time.microTimestamp();
self.performance.add(task.vtable.taskType, end - start);
self.currentTasks[id].store(null, .monotonic);
}

Expand Down
Loading