debug.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. package decoder
  17. import (
  18. `os`
  19. `runtime`
  20. `runtime/debug`
  21. `strings`
  22. `github.com/bytedance/sonic/internal/jit`
  23. )
  24. var (
  25. debugSyncGC = os.Getenv("SONIC_SYNC_GC") != ""
  26. debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
  27. )
  28. var (
  29. _Instr_End _Instr = newInsOp(_OP_nil_1)
  30. _F_gc = jit.Func(runtime.GC)
  31. _F_force_gc = jit.Func(debug.FreeOSMemory)
  32. _F_println = jit.Func(println_wrapper)
  33. _F_print = jit.Func(print)
  34. )
  35. func println_wrapper(i int, op1 int, op2 int){
  36. println(i, " Intrs ", op1, _OpNames[op1], "next: ", op2, _OpNames[op2])
  37. }
  38. func print(i int){
  39. println(i)
  40. }
  41. func (self *_Assembler) force_gc() {
  42. self.call_go(_F_gc)
  43. self.call_go(_F_force_gc)
  44. }
  45. func (self *_Assembler) debug_instr(i int, v *_Instr) {
  46. if debugSyncGC {
  47. if (i+1 == len(self.p)) {
  48. self.print_gc(i, v, &_Instr_End)
  49. } else {
  50. next := &(self.p[i+1])
  51. self.print_gc(i, v, next)
  52. name := _OpNames[next.op()]
  53. if strings.Contains(name, "save") {
  54. return
  55. }
  56. }
  57. self.force_gc()
  58. }
  59. }