summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
Diffstat (limited to 'src/host')
-rw-r--r--src/host/Sessions.zig51
1 files changed, 47 insertions, 4 deletions
diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig
index 32b660b..751961d 100644
--- a/src/host/Sessions.zig
+++ b/src/host/Sessions.zig
@@ -49,8 +49,11 @@ pub const commit_ns: u64 = std.time.ns_per_s - std.time.ns_per_ms;
pub const periodic_ns: u64 = 60 * std.time.ns_per_s;
/// Mark and clip names: no spaces, at most this long.
pub const max_name = 32;
+/// A memory database (a scripted run's) stops recording at this size: ten
+/// hours or more of play at 60 Hz. What it holds stays readable.
+pub const memory_max: u64 = 256 << 20;
-const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end };
+pub const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end };
fn key(k: K) u64 {
return @intFromEnum(k);
}
@@ -96,6 +99,8 @@ recording: ?u64 = null,
/// State may have changed other than by a logged live frame (a replay ran):
/// the next live frame starts with a keyframe.
away: bool = false,
+/// A memory database reached `memory_max`: nothing more is logged.
+full: bool = false,
/// The console line that last changed State, for the next keyframe.
line: [256]u8 = undefined,
line_len: usize = 0,
@@ -113,7 +118,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, want: Mode, path: []const u8, fi
s.buffer = buffer;
switch (want) {
.off => unreachable,
- .memory => s.db = .{ .memory = try Memory.init(std.Io.failing, .{ .buffer = buffer, .file_id = 0 }) },
+ .memory => s.db = .{ .memory = try Memory.init(std.Io.failing, .{ .buffer = buffer, .file_id = 0, .max_size = memory_max }) },
.file => {
s.path = try gpa.dupe(u8, path);
const cwd = std.Io.Dir.cwd();
@@ -298,7 +303,7 @@ pub fn frame(s: *Sessions, one: []abi.Event, state: []const u8, why: ?Why) !void
e.time = @max(e.time, s.last_time);
s.last_time = e.time;
}
- if (s.db == .off) return;
+ if (s.db == .off or s.full) return;
const tick = one[one.len - 1].time;
if (s.batch.items.len > 0 and tick -| s.batch_time >= commit_ns) try s.commit(.none);
if (why) |w| {
@@ -394,7 +399,17 @@ fn commitSynced(s: *Sessions, extra: Extra) !void {
pub fn commit(s: *Sessions, extra: Extra) !void {
switch (s.db) {
.off => return,
- inline .memory, .file => |*db| try db.runContext(Commit(@TypeOf(db.*)){ .s = s, .extra = extra }),
+ inline .memory, .file => |*db| db.runContext(Commit(@TypeOf(db.*)){ .s = s, .extra = extra }) catch |err| {
+ // A memory database that cannot grow (`memory_max`, or no
+ // memory left) stops recording, once.
+ if (s.db == .memory and !s.full) {
+ s.full = true;
+ s.batch.clearRetainingCapacity();
+ s.dropQueued();
+ std.log.warn("sessions: the memory database cannot grow ({t}; {d} MiB at most): no more frames recorded", .{ err, memory_max >> 20 });
+ }
+ return err;
+ },
}
if (s.batch.items.len > 0) {
s.batches += 1;
@@ -1024,3 +1039,31 @@ test "a compacted session database is smaller and holds every session and frame"
try again.events(0, 0, 0, 600, &back);
try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items), std.mem.sliceAsBytes(back.items));
}
+
+test "a memory session stops recording at its bound, and what it holds still reads" {
+ const gpa = std.testing.allocator;
+ var s = try open(gpa, std.testing.io, .memory, "", .{ .build = 7, .source = "", .layout = "a layout" }, 0);
+ defer s.deinit();
+ s.db.memory.core.max_size = 64 << 10;
+ var live: std.ArrayList(abi.Event) = .empty;
+ defer live.deinit(gpa);
+ var state: u64 = 0;
+ var failed: usize = 0;
+ for (0..60 * 60) |f| {
+ const t = f * std.time.ns_per_s / 60;
+ var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)};
+ s.frame(&one, std.mem.asBytes(&state), if (f == 0) .start else null) catch |err| {
+ try std.testing.expectEqual(error.WriteFailed, err);
+ failed += 1;
+ };
+ try live.appendSlice(gpa, &one);
+ state += 1;
+ }
+ try std.testing.expectEqual(@as(usize, 1), failed);
+ const n = (try s.summary(s.session)).frames;
+ try std.testing.expect(n >= 60 and n < 60 * 60);
+ var back: std.ArrayList(abi.Event) = .empty;
+ defer back.deinit(gpa);
+ try s.events(s.session, 0, 0, n, &back);
+ try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items[0..n]), std.mem.sliceAsBytes(back.items));
+}