@@ -17,13 +17,18 @@ limitations under the License.
17
17
package jsonnet
18
18
19
19
import (
20
+ "bufio"
20
21
"bytes"
21
22
"fmt"
22
23
"io"
24
+ "log"
23
25
"math"
26
+ "math/rand"
27
+ "os"
24
28
"reflect"
25
29
"sort"
26
30
"strconv"
31
+ "strings"
27
32
28
33
"github.com/google/go-jsonnet/ast"
29
34
"github.com/google/go-jsonnet/astgen"
@@ -968,7 +973,45 @@ func jsonToValue(i *interpreter, v interface{}) (value, error) {
968
973
}
969
974
}
970
975
976
+ var (
977
+ stackProfileOut * bufio.Writer
978
+ stackProfileRatio = 0.01
979
+ )
980
+
981
+ func StartStackProfile () {
982
+ var err error
983
+
984
+ if os .Getenv ("JSONNET_STACK_PROFILE" ) != "" {
985
+ stackProfileOutFile , err := os .Create (os .Getenv ("JSONNET_STACK_PROFILE" ))
986
+ if err != nil {
987
+ log .Fatal ("could not create stack profile: " , err )
988
+ }
989
+ stackProfileOut = bufio .NewWriter (stackProfileOutFile )
990
+ }
991
+
992
+ if os .Getenv ("JSONNET_STACK_PROFILE_RATIO" ) != "" {
993
+ stackProfileRatio , err = strconv .ParseFloat (os .Getenv ("JSONNET_STACK_PROFILE_RATIO" ), 64 )
994
+ if err != nil {
995
+ log .Fatal ("could not parse stack profile ratio: " , err )
996
+ }
997
+ }
998
+ }
999
+
1000
+ func StopStackProfile () {
1001
+ if stackProfileOut != nil {
1002
+ stackProfileOut .Flush ()
1003
+ }
1004
+ }
1005
+
971
1006
func (i * interpreter ) EvalInCleanEnv (env * environment , ast ast.Node , trimmable bool ) (value , error ) {
1007
+ if stackProfileOut != nil && rand .Float64 () < stackProfileRatio {
1008
+ stack := []string {}
1009
+ for _ , frame := range i .getCurrentStackTrace () {
1010
+ stack = append (stack , frame .Loc .String ()+ ":" + frame .Name )
1011
+ }
1012
+ fmt .Fprintln (stackProfileOut , strings .Join (stack , ";" )+ " 1" )
1013
+ }
1014
+
972
1015
err := i .newCall (* env , trimmable )
973
1016
if err != nil {
974
1017
return nil , err
0 commit comments