github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/coverage/cmerge/merge.go (about) 1 // Copyright 2022 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 cmerge 6 7 // package cmerge provides a few small utility APIs for helping 8 // with merging of counter data for a given function. 9 10 import ( 11 "fmt" 12 "math" 13 14 "github.com/go-asm/go/coverage" 15 ) 16 17 type ModeMergePolicy uint8 18 19 const ( 20 ModeMergeStrict ModeMergePolicy = iota 21 ModeMergeRelaxed 22 ) 23 24 // Merger provides state and methods to help manage the process of 25 // merging together coverage counter data for a given function, for 26 // tools that need to implicitly merge counter as they read multiple 27 // coverage counter data files. 28 type Merger struct { 29 cmode coverage.CounterMode 30 cgran coverage.CounterGranularity 31 policy ModeMergePolicy 32 overflow bool 33 } 34 35 func (cm *Merger) SetModeMergePolicy(policy ModeMergePolicy) { 36 cm.policy = policy 37 } 38 39 // MergeCounters takes the counter values in 'src' and merges them 40 // into 'dst' according to the correct counter mode. 41 func (m *Merger) MergeCounters(dst, src []uint32) (error, bool) { 42 if len(src) != len(dst) { 43 return fmt.Errorf("merging counters: len(dst)=%d len(src)=%d", len(dst), len(src)), false 44 } 45 if m.cmode == coverage.CtrModeSet { 46 for i := 0; i < len(src); i++ { 47 if src[i] != 0 { 48 dst[i] = 1 49 } 50 } 51 } else { 52 for i := 0; i < len(src); i++ { 53 dst[i] = m.SaturatingAdd(dst[i], src[i]) 54 } 55 } 56 ovf := m.overflow 57 m.overflow = false 58 return nil, ovf 59 } 60 61 // Saturating add does a saturating addition of 'dst' and 'src', 62 // returning added value or math.MaxUint32 if there is an overflow. 63 // Overflows are recorded in case the client needs to track them. 64 func (m *Merger) SaturatingAdd(dst, src uint32) uint32 { 65 result, overflow := SaturatingAdd(dst, src) 66 if overflow { 67 m.overflow = true 68 } 69 return result 70 } 71 72 // Saturating add does a saturating addition of 'dst' and 'src', 73 // returning added value or math.MaxUint32 plus an overflow flag. 74 func SaturatingAdd(dst, src uint32) (uint32, bool) { 75 d, s := uint64(dst), uint64(src) 76 sum := d + s 77 overflow := false 78 if uint64(uint32(sum)) != sum { 79 overflow = true 80 sum = math.MaxUint32 81 } 82 return uint32(sum), overflow 83 } 84 85 // SetModeAndGranularity records the counter mode and granularity for 86 // the current merge. In the specific case of merging across coverage 87 // data files from different binaries, where we're combining data from 88 // more than one meta-data file, we need to check for and resolve 89 // mode/granularity clashes. 90 func (cm *Merger) SetModeAndGranularity(mdf string, cmode coverage.CounterMode, cgran coverage.CounterGranularity) error { 91 if cm.cmode == coverage.CtrModeInvalid { 92 // Set merger mode based on what we're seeing here. 93 cm.cmode = cmode 94 cm.cgran = cgran 95 } else { 96 // Granularity clashes are always errors. 97 if cm.cgran != cgran { 98 return fmt.Errorf("counter granularity clash while reading meta-data file %s: previous file had %s, new file has %s", mdf, cm.cgran.String(), cgran.String()) 99 } 100 // Mode clashes are treated as errors if we're using the 101 // default strict policy. 102 if cm.cmode != cmode { 103 if cm.policy == ModeMergeStrict { 104 return fmt.Errorf("counter mode clash while reading meta-data file %s: previous file had %s, new file has %s", mdf, cm.cmode.String(), cmode.String()) 105 } 106 // In the case of a relaxed mode merge policy, upgrade 107 // mode if needed. 108 if cm.cmode < cmode { 109 cm.cmode = cmode 110 } 111 } 112 } 113 return nil 114 } 115 116 func (cm *Merger) ResetModeAndGranularity() { 117 cm.cmode = coverage.CtrModeInvalid 118 cm.cgran = coverage.CtrGranularityInvalid 119 cm.overflow = false 120 } 121 122 func (cm *Merger) Mode() coverage.CounterMode { 123 return cm.cmode 124 } 125 126 func (cm *Merger) Granularity() coverage.CounterGranularity { 127 return cm.cgran 128 }