@@ -201,20 +201,49 @@ func (group UncoreGroup) StringForPerf() (string, error) {
201201}
202202
203203func MergeUncoreGroups (uncoreGroups []UncoreGroup ) ([]UncoreGroup , error ) {
204- i := 0
205- for i < len (uncoreGroups ) { // this style of for loop is used to allow for removal of elements
206- j := i + 1
207- for j < len (uncoreGroups ) { // len(coreGroups) is recalculated on each iteration
208- tmpGroup := uncoreGroups [i ].Copy () // Copy the group to avoid modifying the original
209- if err := tmpGroup .Merge (uncoreGroups [j ]); err == nil {
210- uncoreGroups [i ] = tmpGroup // Update the group at index i with the merged group
211- // remove the group at index j
212- uncoreGroups = append (uncoreGroups [:j ], uncoreGroups [j + 1 :]... )
204+ // First, eliminate duplicate events across groups (only needs to be done once)
205+ seenEvents := make (map [string ]int ) // map of event name to group index where it first appears
206+ for i := range uncoreGroups {
207+ for j := range uncoreGroups [i ].GeneralPurposeCounters {
208+ event := uncoreGroups [i ].GeneralPurposeCounters [j ]
209+ if event .IsEmpty () {
210+ continue
211+ }
212+
213+ if firstGroupIdx , exists := seenEvents [event .EventName ]; exists {
214+ // Event already exists in another group, remove from current group
215+ slog .Debug ("removing duplicate uncore event from group" ,
216+ slog .String ("event" , event .EventName ),
217+ slog .Int ("firstGroup" , firstGroupIdx ),
218+ slog .Int ("currentGroup" , i ))
219+ uncoreGroups [i ].GeneralPurposeCounters [j ] = UncoreEvent {}
213220 } else {
214- j ++ // Cannot merge these groups, try the next pair
221+ // First time seeing this event, record it
222+ seenEvents [event .EventName ] = i
215223 }
216224 }
217- i ++
225+ }
226+
227+ // Then, keep merging until no more merges are possible
228+ merged := true
229+ for merged {
230+ merged = false
231+ i := 0
232+ for i < len (uncoreGroups ) {
233+ j := i + 1
234+ for j < len (uncoreGroups ) {
235+ tmpGroup := uncoreGroups [i ].Copy ()
236+ if err := tmpGroup .Merge (uncoreGroups [j ]); err == nil {
237+ uncoreGroups [i ] = tmpGroup
238+ // remove the group at index j
239+ uncoreGroups = append (uncoreGroups [:j ], uncoreGroups [j + 1 :]... )
240+ merged = true // mark that we made a change
241+ } else {
242+ j ++
243+ }
244+ }
245+ i ++
246+ }
218247 }
219248 return uncoreGroups , nil
220249}
0 commit comments