This commit is contained in:
Igor 2025-06-23 11:43:29 +02:00 committed by GitHub
commit d7313b47ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 0 deletions

View File

@ -395,6 +395,9 @@ func main() {
cmd.StartCPUProfile() cmd.StartCPUProfile()
defer cmd.StopCPUProfile() defer cmd.StopCPUProfile()
jsonnet.StartStackProfile()
defer jsonnet.StopStackProfile()
vm := jsonnet.MakeVM() vm := jsonnet.MakeVM()
vm.ErrorFormatter.SetColorFormatter(color.New(color.FgRed).Fprintf) vm.ErrorFormatter.SetColorFormatter(color.New(color.FgRed).Fprintf)

View File

@ -17,13 +17,18 @@ limitations under the License.
package jsonnet package jsonnet
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"log"
"math" "math"
"math/rand"
"os"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"strings"
"github.com/google/go-jsonnet/ast" "github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/astgen" "github.com/google/go-jsonnet/astgen"
@ -1009,7 +1014,45 @@ func jsonToValue(i *interpreter, v interface{}) (value, error) {
} }
} }
var (
stackProfileOut *bufio.Writer
stackProfileRatio = 0.01
)
func StartStackProfile() {
var err error
if os.Getenv("JSONNET_STACK_PROFILE") != "" {
stackProfileOutFile, err := os.Create(os.Getenv("JSONNET_STACK_PROFILE"))
if err != nil {
log.Fatal("could not create stack profile: ", err)
}
stackProfileOut = bufio.NewWriter(stackProfileOutFile)
}
if os.Getenv("JSONNET_STACK_PROFILE_RATIO") != "" {
stackProfileRatio, err = strconv.ParseFloat(os.Getenv("JSONNET_STACK_PROFILE_RATIO"), 64)
if err != nil {
log.Fatal("could not parse stack profile ratio: ", err)
}
}
}
func StopStackProfile() {
if stackProfileOut != nil {
stackProfileOut.Flush()
}
}
func (i *interpreter) EvalInCleanEnv(env *environment, ast ast.Node, trimmable bool) (value, error) { func (i *interpreter) EvalInCleanEnv(env *environment, ast ast.Node, trimmable bool) (value, error) {
if stackProfileOut != nil && rand.Float64() < stackProfileRatio {
stack := []string{}
for _, frame := range i.getCurrentStackTrace() {
stack = append(stack, frame.Loc.String()+":"+frame.Name)
}
fmt.Fprintln(stackProfileOut, strings.Join(stack, ";")+" 1")
}
err := i.newCall(*env, trimmable) err := i.newCall(*env, trimmable)
if err != nil { if err != nil {
return nil, err return nil, err