Skip to content

Commit

Permalink
Reduce peak memory usage by clearing encoded instructions in builder
Browse files Browse the repository at this point in the history
The builder will be kept alive until the replay is done on the device,
while the encoded instructions and resources can be freed before the
replay gets started.
  • Loading branch information
Qining committed Aug 29, 2018
1 parent 409466f commit 445ac77
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions gapis/api/vulkan/find_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (t *findIssues) reportTo(r replay.Result) { t.res = append(t.res, r) }

func (t *findIssues) Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, out transform.Writer) {
ctx = log.Enter(ctx, "findIssues")
cb := CommandBuilder{Thread: cmd.Thread(), Arena: t.state.Arena}

mutateErr := cmd.Mutate(ctx, id, t.state, nil /* no builder */)
if mutateErr != nil {
Expand All @@ -98,6 +97,7 @@ func (t *findIssues) Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, o
}

s := out.State()
cb := CommandBuilder{Thread: cmd.Thread(), Arena: out.State().Arena}
l := s.MemoryLayout
allocated := []api.AllocResult{}
defer func() {
Expand Down Expand Up @@ -269,7 +269,7 @@ func (t *findIssues) Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, o
}

func (t *findIssues) Flush(ctx context.Context, out transform.Writer) {
cb := CommandBuilder{Thread: 0, Arena: t.state.Arena}
cb := CommandBuilder{Thread: 0, Arena: out.State().Arena}
for inst, ch := range t.reportCallbacks {
out.MutateAndWrite(ctx, api.CmdNoID, cb.ReplayDestroyVkDebugReportCallback(inst, ch))
// It is safe to delete keys in loop in Go
Expand Down
5 changes: 4 additions & 1 deletion gapis/api/vulkan/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,11 @@ func (a API) Replay(
transforms.Add(overdraw)
}

if issues == nil {
transforms.Add(readFramebuffer, injector)
}

// Cleanup
transforms.Add(readFramebuffer, injector)
transforms.Add(&destroyResourcesAtEOS{})

if config.DebugReplay {
Expand Down
13 changes: 13 additions & 0 deletions gapis/replay/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,19 @@ func (b *Builder) Build(ctx context.Context) (gapir.Payload, PostDataHandler, No
})
}

// Clear the builder.
defer func() {
b.resourceIDToIdx = nil
b.threadIDToIdx = nil
b.resources = nil
b.reservedMemory = memory.RangeList{}
b.pointerMemory = memory.RangeList{}
b.mappedMemory = mappedMemoryRangeList{}
b.instructions = nil
b.stack = nil
b.Remappings = nil
}()

return payload, handlePost, handleNotification, nil
}

Expand Down

0 comments on commit 445ac77

Please sign in to comment.