@@ -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"
@@ -1009,7 +1014,45 @@ func jsonToValue(i *interpreter, v interface{}) (value, error) {
1009
1014
}
1010
1015
}
1011
1016
1017
+ var (
1018
+ stackProfileOut * bufio.Writer
1019
+ stackProfileRatio = 0.01
1020
+ )
1021
+
1022
+ func StartStackProfile () {
1023
+ var err error
1024
+
1025
+ if os .Getenv ("JSONNET_STACK_PROFILE" ) != "" {
1026
+ stackProfileOutFile , err := os .Create (os .Getenv ("JSONNET_STACK_PROFILE" ))
1027
+ if err != nil {
1028
+ log .Fatal ("could not create stack profile: " , err )
1029
+ }
1030
+ stackProfileOut = bufio .NewWriter (stackProfileOutFile )
1031
+ }
1032
+
1033
+ if os .Getenv ("JSONNET_STACK_PROFILE_RATIO" ) != "" {
1034
+ stackProfileRatio , err = strconv .ParseFloat (os .Getenv ("JSONNET_STACK_PROFILE_RATIO" ), 64 )
1035
+ if err != nil {
1036
+ log .Fatal ("could not parse stack profile ratio: " , err )
1037
+ }
1038
+ }
1039
+ }
1040
+
1041
+ func StopStackProfile () {
1042
+ if stackProfileOut != nil {
1043
+ stackProfileOut .Flush ()
1044
+ }
1045
+ }
1046
+
1012
1047
func (i * interpreter ) EvalInCleanEnv (env * environment , ast ast.Node , trimmable bool ) (value , error ) {
1048
+ if stackProfileOut != nil && rand .Float64 () < stackProfileRatio {
1049
+ stack := []string {}
1050
+ for _ , frame := range i .getCurrentStackTrace () {
1051
+ stack = append (stack , frame .Loc .String ()+ ":" + frame .Name )
1052
+ }
1053
+ fmt .Fprintln (stackProfileOut , strings .Join (stack , ";" )+ " 1" )
1054
+ }
1055
+
1013
1056
err := i .newCall (* env , trimmable )
1014
1057
if err != nil {
1015
1058
return nil , err
0 commit comments