1// Copyright 2024 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 pgo contains the compiler-agnostic portions of PGO profile handling.
6// Notably, parsing pprof profiles and serializing/deserializing from a custom
7// intermediate representation.
8package pgo
9
10// Profile contains the processed data from the PGO profile.
11type Profile struct {
12	// TotalWeight is the aggregated edge weights across the profile. This
13	// helps us determine the percentage threshold for hot/cold
14	// partitioning.
15	TotalWeight int64
16
17	// NamedEdgeMap contains all unique call edges in the profile and their
18	// edge weight.
19	NamedEdgeMap NamedEdgeMap
20}
21
22// NamedCallEdge identifies a call edge by linker symbol names and call site
23// offset.
24type NamedCallEdge struct {
25	CallerName     string
26	CalleeName     string
27	CallSiteOffset int // Line offset from function start line.
28}
29
30// NamedEdgeMap contains all unique call edges in the profile and their
31// edge weight.
32type NamedEdgeMap struct {
33	Weight map[NamedCallEdge]int64
34
35	// ByWeight lists all keys in Weight, sorted by edge weight from
36	// highest to lowest.
37	ByWeight []NamedCallEdge
38}
39
40func emptyProfile() *Profile {
41	// Initialize empty maps/slices for easier use without a requiring a
42	// nil check.
43	return &Profile{
44		NamedEdgeMap: NamedEdgeMap{
45			ByWeight: make([]NamedCallEdge, 0),
46			Weight:   make(map[NamedCallEdge]int64),
47		},
48	}
49}
50
51// WeightInPercentage converts profile weights to a percentage.
52func WeightInPercentage(value int64, total int64) float64 {
53	return (float64(value) / float64(total)) * 100
54}
55
56