Skip to content

Commit

Permalink
overwrite: add --trace_file flag for performance analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
stapelberg committed Jan 13, 2025
1 parent 37e2f95 commit ca8464f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
16 changes: 16 additions & 0 deletions internal/gok/overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"runtime/trace"

"github.com/gokrazy/internal/config"
"github.com/gokrazy/internal/instanceflag"
Expand Down Expand Up @@ -49,6 +50,8 @@ type overwriteImplConfig struct {

sudo string
targetStorageBytes int

traceFile string
}

var overwriteImpl overwriteImplConfig
Expand All @@ -62,9 +65,22 @@ func init() {
overwriteCmd.Flags().StringVarP(&overwriteImpl.mbr, "mbr", "", "", "write the gokrazy master boot record (MBR) to the specified device (e.g. /dev/sdx) or path (e.g. /tmp/mbr.img). only effective if -boot is specified, too")
overwriteCmd.Flags().StringVarP(&overwriteImpl.sudo, "sudo", "", "", "Whether to elevate privileges using sudo when required (one of auto, always, never, default auto)")
overwriteCmd.Flags().IntVarP(&overwriteImpl.targetStorageBytes, "target_storage_bytes", "", 0, "Number of bytes which the target storage device (SD card) has. Required for using -full=<file>")
overwriteCmd.Flags().StringVarP(&overwriteImpl.traceFile, "trace_file", "", "", "If non-empty, write a Go runtime/trace to this file (for performance analysis)")
}

func (r *overwriteImplConfig) run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
if r.traceFile != "" {
out, err := os.Create(r.traceFile)
if err != nil {
return err
}
defer out.Close()
if err := trace.Start(out); err != nil {
return err
}
defer trace.Stop()
}

fileCfg, err := config.ReadFromFile()
if err != nil {
return err
Expand Down
46 changes: 36 additions & 10 deletions internal/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/trace"
"sort"
"strings"
"syscall"
Expand Down Expand Up @@ -1147,24 +1148,37 @@ func (pack *Pack) logic(programName string) error {
buildEnv := &packer.BuildEnv{
BuildDir: packer.BuildDirOrMigrate,
}
if err := buildEnv.Build(bindir, pkgs, packageBuildFlags, packageBuildTags, noBuildPkgs); err != nil {
return err
var buildErr error
trace.WithRegion(context.Background(), "build", func() {
buildErr = buildEnv.Build(bindir, pkgs, packageBuildFlags, packageBuildTags, noBuildPkgs)
})
if buildErr != nil {
return buildErr
}

fmt.Println()

if err := pack.validateTargetArchMatchesKernel(); err != nil {
trace.WithRegion(context.Background(), "validate", func() {
err = pack.validateTargetArchMatchesKernel()
})
if err != nil {
return err
}

root, err := findBins(cfg, buildEnv, bindir)
var root *FileInfo
trace.WithRegion(context.Background(), "findbins", func() {
root, err = findBins(cfg, buildEnv, bindir)
})
if err != nil {
return err
}

packageConfigFiles = make(map[string][]packageConfigFile)

extraFiles, err := FindExtraFiles(cfg)
var extraFiles map[string][]*FileInfo
trace.WithRegion(context.Background(), "findextrafiles", func() {
extraFiles, err = FindExtraFiles(cfg)
})
if err != nil {
return err
}
Expand Down Expand Up @@ -1212,7 +1226,10 @@ func (pack *Pack) logic(programName string) error {
return gokrazyInit.dump(cfg.InternalCompatibilityFlags.OverwriteInit)
}

tmpdir, err := gokrazyInit.build()
var tmpdir string
trace.WithRegion(context.Background(), "buildinit", func() {
tmpdir, err = gokrazyInit.build()
})
if err != nil {
return err
}
Expand Down Expand Up @@ -1292,7 +1309,9 @@ func (pack *Pack) logic(programName string) error {
modules := &FileInfo{
Filename: "modules",
}
_, err := addToFileInfo(modules, modulesDir)
trace.WithRegion(context.Background(), "kernelmod", func() {
_, err = addToFileInfo(modules, modulesDir)
})
if err != nil {
return err
}
Expand Down Expand Up @@ -1402,7 +1421,10 @@ func (pack *Pack) logic(programName string) error {
// as the SBOM should reflect what’s going into gokrazy,
// not its internal implementation details
// (i.e. cfg.InternalCompatibilityFlags untouched).
sbom, _, err := GenerateSBOM(pack.FileCfg)
var sbom []byte
trace.WithRegion(context.Background(), "sbom", func() {
sbom, _, err = GenerateSBOM(pack.FileCfg)
})
if err != nil {
return err
}
Expand Down Expand Up @@ -1580,8 +1602,12 @@ func (pack *Pack) logic(programName string) error {
}

if cfg.InternalCompatibilityFlags.OverwriteRoot != "" {
if err := writeRootFile(cfg.InternalCompatibilityFlags.OverwriteRoot, root); err != nil {
return err
var rootErr error
trace.WithRegion(context.Background(), "writeroot", func() {
writeRootFile(cfg.InternalCompatibilityFlags.OverwriteRoot, root)
})
if rootErr != nil {
return rootErr
}
}

Expand Down

0 comments on commit ca8464f

Please sign in to comment.