github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/convert/speedscope/units.go (about)

     1  package speedscope
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pyroscope-io/pyroscope/pkg/storage/metadata"
     7  	"github.com/pyroscope-io/pyroscope/pkg/storage/segment"
     8  )
     9  
    10  type unit string
    11  
    12  const (
    13  	unitNone         = unit("none")
    14  	unitNanoseconds  = unit("nanoseconds")
    15  	unitMicroseconds = unit("microseconds")
    16  	unitMilliseconds = unit("milliseconds")
    17  	unitSeconds      = unit("seconds")
    18  	unitBytes        = unit("bytes")
    19  )
    20  
    21  // This number defines how much precision we want to keep when converting
    22  // from doubles to integers
    23  var timePrecisionMultiplier = 100
    24  
    25  func (u unit) defaultSampleRate() uint32 {
    26  	switch u {
    27  	case unitNanoseconds:
    28  		return uint32(timePrecisionMultiplier) * 1000 * 1000 * 1000
    29  	case unitMicroseconds:
    30  		return uint32(timePrecisionMultiplier) * 1000 * 1000
    31  	case unitMilliseconds:
    32  		return uint32(timePrecisionMultiplier) * 1000
    33  	case unitSeconds:
    34  		return uint32(timePrecisionMultiplier)
    35  	case unitNone:
    36  		// 100 is a common default value for sample rate
    37  		return uint32(timePrecisionMultiplier) * 100
    38  	case unitBytes:
    39  		return 0
    40  	default:
    41  		panic("unknown unit " + u)
    42  	}
    43  }
    44  
    45  func (u unit) precisionMultiplier() uint64 {
    46  	switch u {
    47  	case unitNanoseconds:
    48  		return uint64(timePrecisionMultiplier)
    49  	case unitMicroseconds:
    50  		return uint64(timePrecisionMultiplier)
    51  	case unitMilliseconds:
    52  		return uint64(timePrecisionMultiplier)
    53  	case unitSeconds:
    54  		return uint64(timePrecisionMultiplier)
    55  	case unitNone:
    56  		return uint64(timePrecisionMultiplier)
    57  	case unitBytes:
    58  		return 1
    59  	default:
    60  		panic("unknown unit " + u)
    61  	}
    62  }
    63  
    64  func (u unit) chooseMetadataUnit() metadata.Units {
    65  	switch u {
    66  	case unitBytes:
    67  		return metadata.BytesUnits
    68  	default:
    69  		return metadata.SamplesUnits
    70  	}
    71  }
    72  
    73  func (u unit) chooseKey(orig *segment.Key) *segment.Key {
    74  	// This means we'll have duplicate keys if multiple profiles have the same units. Probably ok.
    75  	name := fmt.Sprintf("%s.%s", orig.AppName(), u)
    76  	result := orig.Clone()
    77  	result.Add("__name__", name)
    78  	return result
    79  }