cuelang.org/go@v0.10.1/internal/core/adt/prof.go (about)

     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  // ResetStats sets the global stats counters to zero.
    32  func ResetStats() {
    33  	countsMu.Lock()
    34  	counts = stats.Counts{}
    35  	countsMu.Unlock()
    36  }
    37  
    38  // AddStats adds the stats of the given OpContext to the global
    39  // counters.
    40  func AddStats(ctx *OpContext) {
    41  	countsMu.Lock()
    42  	counts.Add(ctx.stats)
    43  	countsMu.Unlock()
    44  }
    45  
    46  // TotalStats returns the aggregate counts of all operations
    47  // calling AddStats.
    48  func TotalStats() stats.Counts {
    49  	countsMu.Lock()
    50  	// Shallow copy suffices as it only contains counter fields.
    51  	s := counts
    52  	countsMu.Unlock()
    53  	return s
    54  }