Capture starts the perfetto capture.
(ctx context.Context, start task.Signal, stop task.Signal, w io.Writer, written *int64)
| 77 | |
| 78 | // Capture starts the perfetto capture. |
| 79 | func (p *Process) Capture(ctx context.Context, start task.Signal, stop task.Signal, w io.Writer, written *int64) (int64, error) { |
| 80 | tmp, err := file.Temp() |
| 81 | if err != nil { |
| 82 | return 0, log.Err(ctx, err, "Failed to create a temp file") |
| 83 | } |
| 84 | |
| 85 | // Signal that we are ready to start. |
| 86 | atomic.StoreInt64(written, 1) |
| 87 | |
| 88 | if p.deferred && !start.Wait(ctx) { |
| 89 | return 0, log.Err(ctx, nil, "Cancelled") |
| 90 | } |
| 91 | |
| 92 | if err := p.device.StartPerfettoTrace(ctx, p.config, perfettoTraceFile, stop); err != nil { |
| 93 | return 0, err |
| 94 | } |
| 95 | |
| 96 | if err := p.device.Pull(ctx, perfettoTraceFile, tmp.System()); err != nil { |
| 97 | return 0, err |
| 98 | } |
| 99 | |
| 100 | size := tmp.Info().Size() |
| 101 | atomic.StoreInt64(written, size) |
| 102 | fh, err := os.Open(tmp.System()) |
| 103 | if err != nil { |
| 104 | return 0, log.Err(ctx, err, fmt.Sprintf("Failed to open %s", tmp)) |
| 105 | } |
| 106 | return io.Copy(w, fh) |
| 107 | } |