github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/encoder/debug_go116.go (about)

     1  //go:build go1.16 && !go1.17
     2  // +build go1.16,!go1.17
     3  
     4  /*
     5   * Copyright 2021 ByteDance Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package encoder
    21  
    22  import (
    23  	"os"
    24  	"runtime"
    25  	"runtime/debug"
    26  	"strings"
    27  
    28  	"github.com/goshafaq/sonic/internal/jit"
    29  )
    30  
    31  var (
    32  	debugSyncGC  = os.Getenv("SONIC_SYNC_GC") != ""
    33  	debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
    34  )
    35  
    36  var (
    37  	_Instr_End _Instr = newInsOp(_OP_null)
    38  
    39  	_F_gc       = jit.Func(runtime.GC)
    40  	_F_force_gc = jit.Func(debug.FreeOSMemory)
    41  	_F_println  = jit.Func(println_wrapper)
    42  )
    43  
    44  func println_wrapper(i int, op1 int, op2 int) {
    45  	println(i, " Intrs ", op1, _OpNames[op1], "next: ", op2, _OpNames[op2])
    46  }
    47  
    48  func (self *_Assembler) force_gc() {
    49  	self.call_go(_F_gc)
    50  	self.call_go(_F_force_gc)
    51  }
    52  
    53  func (self *_Assembler) debug_instr(i int, v *_Instr) {
    54  	if debugSyncGC {
    55  		if i+1 == len(self.p) {
    56  			self.print_gc(i, v, &_Instr_End)
    57  		} else {
    58  			next := &(self.p[i+1])
    59  			self.print_gc(i, v, next)
    60  			name := _OpNames[next.op()]
    61  			if strings.Contains(name, "save") {
    62  				return
    63  			}
    64  		}
    65  		self.force_gc()
    66  	}
    67  }