| 1 | // Copyright 2023 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package pprof provides minimalistic routines for extracting |
| 6 | // information from profiles. |
| 7 | package pprofscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/ |
| 8 | |
| 9 | import ( |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 12 | ) |
| 13 | |
| 14 | // TotalTime parses the profile data and returns the accumulated time. |
| 15 | // The input should not be gzipped. |
| 16 | func TotalTimescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/TotalTime().(data []byte) (total timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#, err error) { |
| 17 | defer func() { |
| 18 | if x := recover(); x != nil { |
| 19 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("error parsing pprof profile: %v", x) |
| 20 | } |
| 21 | }() |
| 22 | decodescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/decode().(&total, data, msgProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/msgProfile.) |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | // All errors are handled by panicking. |
| 27 | // Constants are copied below to avoid dependency on protobufs or pprof. |
| 28 | |
| 29 | // protobuf wire types, from https://developers.google.com/protocol-buffers/docs/encoding |
| 30 | const ( |
| 31 | wireVarintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/wireVarint. = 0 |
| 32 | wireBytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/wireBytes. = 2 |
| 33 | ) |
| 34 | |
| 35 | // pprof field numbers, from https://github.com/google/pprof/blob/master/proto/profile.proto |
| 36 | const ( |
| 37 | fldProfileSamplescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/fldProfileSample. = 2 // repeated Sample |
| 38 | fldSampleValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/fldSampleValue. = 2 // repeated int64 |
| 39 | ) |
| 40 | |
| 41 | // arbitrary numbering of message types |
| 42 | const ( |
| 43 | msgProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/msgProfile. = 0 |
| 44 | msgSamplescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/msgSample. = 1 |
| 45 | ) |
| 46 | |
| 47 | func decodescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/decode().(total *timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#, data []byte, msg int) { |
| 48 | for len(data) > 0 { |
| 49 | // Read tag (wire type and field number). |
| 50 | tag := varintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/varint().(&data) |
| 51 | |
| 52 | // Read wire value (int or bytes). |
| 53 | wire := tag & 7 |
| 54 | var ival uint64 |
| 55 | var sval []byte |
| 56 | switch wire { |
| 57 | case wireVarintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/wireVarint.: |
| 58 | ival = varintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/varint().(&data) |
| 59 | |
| 60 | case wireBytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/wireBytes.: |
| 61 | n := varintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/varint().(&data) |
| 62 | sval, data = data[:n], data[n:] |
| 63 | |
| 64 | default: |
| 65 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("unexpected wire type: %d", wire)) |
| 66 | } |
| 67 | |
| 68 | // Process field of msg. |
| 69 | fld := tag >> 3 |
| 70 | switch { |
| 71 | case msg == msgProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/msgProfile. && fld == fldProfileSamplescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/fldProfileSample.: |
| 72 | decodescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/decode().(total, sval, msgSamplescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/msgSample.) // recursively decode Sample message |
| 73 | |
| 74 | case msg == msgSamplescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/msgSample., fld == fldSampleValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/fldSampleValue.: |
| 75 | *total += timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(ival) // accumulate time |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func varintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/pprof`/varint().(data *[]byte) (v uint64) { |
| 81 | for i := 0; ; i++ { |
| 82 | b := uint64((*data)[i]) |
| 83 | v += (b & 0x7f) << (7 * i) |
| 84 | if b < 0x80 { |
| 85 | *data = (*data)[i+1:] |
| 86 | return v |
| 87 | } |
| 88 | } |
| 89 | } |