...
1
2
3
4
5 package cmerge
6
7
8
9
10 import (
11 "fmt"
12 "internal/coverage"
13 "math"
14 )
15
16 type ModeMergePolicy uint8
17
18 const (
19 ModeMergeStrict ModeMergePolicy = iota
20 ModeMergeRelaxed
21 )
22
23
24
25
26
27 type Merger struct {
28 cmode coverage.CounterMode
29 cgran coverage.CounterGranularity
30 policy ModeMergePolicy
31 overflow bool
32 }
33
34 func (cm *Merger) SetModeMergePolicy(policy ModeMergePolicy) {
35 cm.policy = policy
36 }
37
38
39
40 func (m *Merger) MergeCounters(dst, src []uint32) (error, bool) {
41 if len(src) != len(dst) {
42 return fmt.Errorf("merging counters: len(dst)=%d len(src)=%d", len(dst), len(src)), false
43 }
44 if m.cmode == coverage.CtrModeSet {
45 for i := 0; i < len(src); i++ {
46 if src[i] != 0 {
47 dst[i] = 1
48 }
49 }
50 } else {
51 for i := 0; i < len(src); i++ {
52 dst[i] = m.SaturatingAdd(dst[i], src[i])
53 }
54 }
55 ovf := m.overflow
56 m.overflow = false
57 return nil, ovf
58 }
59
60
61
62
63 func (m *Merger) SaturatingAdd(dst, src uint32) uint32 {
64 result, overflow := SaturatingAdd(dst, src)
65 if overflow {
66 m.overflow = true
67 }
68 return result
69 }
70
71
72
73 func SaturatingAdd(dst, src uint32) (uint32, bool) {
74 d, s := uint64(dst), uint64(src)
75 sum := d + s
76 overflow := false
77 if uint64(uint32(sum)) != sum {
78 overflow = true
79 sum = math.MaxUint32
80 }
81 return uint32(sum), overflow
82 }
83
84
85
86
87
88
89 func (cm *Merger) SetModeAndGranularity(mdf string, cmode coverage.CounterMode, cgran coverage.CounterGranularity) error {
90 if cm.cmode == coverage.CtrModeInvalid {
91
92 cm.cmode = cmode
93 cm.cgran = cgran
94 } else {
95
96 if cm.cgran != cgran {
97 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())
98 }
99
100
101 if cm.cmode != cmode {
102 if cm.policy == ModeMergeStrict {
103 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())
104 }
105
106
107 if cm.cmode < cmode {
108 cm.cmode = cmode
109 }
110 }
111 }
112 return nil
113 }
114
115 func (cm *Merger) ResetModeAndGranularity() {
116 cm.cmode = coverage.CtrModeInvalid
117 cm.cgran = coverage.CtrGranularityInvalid
118 cm.overflow = false
119 }
120
121 func (cm *Merger) Mode() coverage.CounterMode {
122 return cm.cmode
123 }
124
125 func (cm *Merger) Granularity() coverage.CounterGranularity {
126 return cm.cgran
127 }
128
View as plain text