github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/decoder/debug.go (about)

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