1 // Copyright 2023 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package adt 16 17 import ( 18 "sync" 19 20 "cuelang.org/go/cue/stats" 21 ) 22 23 // This file contains stats and profiling functionality. 24 25 var ( 26 // counts is a temporary and internal solution for collecting global stats. It is protected with a mutex. 27 counts stats.Counts 28 countsMu sync.Mutex 29 ) 30 31 // AddStats adds the stats of the given OpContext to the global 32 // counters. 33 func AddStats(ctx *OpContext) { 34 countsMu.Lock() 35 counts.Add(ctx.stats) 36 countsMu.Unlock() 37 } 38 39 // TotalStats returns the aggregate counts of all operations 40 // calling AddStats. 41 func TotalStats() stats.Counts { 42 countsMu.Lock() 43 // Shallow copy suffices as it only contains counter fields. 44 s := counts 45 countsMu.Unlock() 46 return s 47 } 48