Repository navigation
Expand file tree
/
Copy pathcell_gc.go
More file actions
85 lines (75 loc) · 2.22 KB
/
Copy pathcell_gc.go
File metadata and controls
85 lines (75 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"fmt"
"io"
"github.com/Agent-Field/codeaf/internal/cell"
"github.com/Agent-Field/codeaf/internal/cellbudget"
"github.com/Agent-Field/codeaf/internal/cellstore"
"github.com/Agent-Field/codeaf/internal/config"
)
// cellGC brings the cells' working files under the disk budget now.
func cellGC(_ cell.Cell, args []string, out io.Writer) error {
flags := commandFlags("cell gc")
dry := flags.Bool("dry-run", false, "say what would be removed and remove nothing")
if err := parseCommandFlags(flags, args); err != nil {
return err
}
if flags.NArg() != 0 {
return fmt.Errorf("usage: codeaf cell gc [--dry-run]")
}
rep, err := cellBudget().Collect(context.Background(), *dry)
if err != nil {
return err
}
writeGCReport(out, rep)
return nil
}
func cellBudget() cellbudget.Manager {
m := cellbudget.New(cellstore.Engine{}, cellWorkspace, cellBusy)
m.Limit = int64(config.CellBudgetGBAt(config.ProfileDir()) * (1 << 30))
return m
}
func writeGCReport(out io.Writer, rep cellbudget.Report) {
fmt.Fprintf(out, "cells hold %s, budget %s\n", gcSize(rep.Total), gcLimit(rep.Limit))
for _, a := range rep.Actions {
fmt.Fprintf(out, " %s %s %s%s\n", a.ID, gcSize(a.Bytes), a.Outcome, gcWhy(a.Why))
for _, path := range a.Paths {
fmt.Fprintf(out, " %s\n", path)
}
}
if gcSawRunning(rep) {
fmt.Fprintf(out, "running: %s\n", heldByEngineHint())
}
}
// gcSawRunning is whether the sweep passed over a cell because a session holds it.
func gcSawRunning(rep cellbudget.Report) bool {
for _, a := range rep.Actions {
if a.Why == "running" {
return true
}
}
return false
}
func gcWhy(why string) string {
if why == "" {
return ""
}
return " (" + why + ")"
}
func gcLimit(n int64) string {
if n <= 0 {
return "unlimited"
}
return gcSize(n)
}
// cellsUsage is the doctor row: what the cells hold against the budget, and ""
// when they hold nothing (a machine with no cells has measured nothing, not zero).
func cellsUsage() string {
rep, err := cellBudget().Collect(context.Background(), true)
if err != nil || rep.Total == 0 {
return ""
}
return gcSize(rep.Total) + " of " + gcLimit(rep.Limit)
}
func gcSize(n int64) string { return fmt.Sprintf("%.1f MiB", float64(n)/(1<<20)) }