File size: 1,914 Bytes
61bba11 | 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 86 87 88 | // Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Preprofile creates an intermediate representation of a pprof profile for use
// during PGO in the compiler. This transformation depends only on the profile
// itself and is thus wasteful to perform in every invocation of the compiler.
//
// Usage:
//
// go tool preprofile [-V] [-o output] -i input
package main
import (
"bufio"
"cmd/internal/objabi"
"cmd/internal/pgo"
"cmd/internal/telemetry/counter"
"flag"
"fmt"
"log"
"os"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: go tool preprofile [-V] [-o output] -i input\n\n")
flag.PrintDefaults()
os.Exit(2)
}
var (
output = flag.String("o", "", "output file path")
input = flag.String("i", "", "input pprof file path")
)
func preprocess(profileFile string, outputFile string) error {
f, err := os.Open(profileFile)
if err != nil {
return fmt.Errorf("error opening profile: %w", err)
}
defer f.Close()
r := bufio.NewReader(f)
d, err := pgo.FromPProf(r)
if err != nil {
return fmt.Errorf("error parsing profile: %w", err)
}
var out *os.File
if outputFile == "" {
out = os.Stdout
} else {
out, err = os.Create(outputFile)
if err != nil {
return fmt.Errorf("error creating output file: %w", err)
}
defer out.Close()
}
w := bufio.NewWriter(out)
if _, err := d.WriteTo(w); err != nil {
return fmt.Errorf("error writing output file: %w", err)
}
return nil
}
func main() {
objabi.AddVersionFlag()
log.SetFlags(0)
log.SetPrefix("preprofile: ")
counter.Open()
flag.Usage = usage
flag.Parse()
counter.Inc("preprofile/invocations")
counter.CountFlags("preprofile/flag:", *flag.CommandLine)
if *input == "" {
log.Print("Input pprof path required (-i)")
usage()
}
if err := preprocess(*input, *output); err != nil {
log.Fatal(err)
}
}
|