debug.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runtime
  5. // Breakpoint executes a breakpoint trap.
  6. func Breakpoint()
  7. // LockOSThread wires the calling goroutine to its current operating system thread.
  8. // Until the calling goroutine exits or calls UnlockOSThread, it will always
  9. // execute in that thread, and no other goroutine can.
  10. func LockOSThread()
  11. // UnlockOSThread unwires the calling goroutine from its fixed operating system thread.
  12. // If the calling goroutine has not called LockOSThread, UnlockOSThread is a no-op.
  13. func UnlockOSThread()
  14. // GOMAXPROCS sets the maximum number of CPUs that can be executing
  15. // simultaneously and returns the previous setting. If n < 1, it does not
  16. // change the current setting.
  17. // The number of logical CPUs on the local machine can be queried with NumCPU.
  18. // This call will go away when the scheduler improves.
  19. func GOMAXPROCS(n int) int
  20. // NumCPU returns the number of logical CPUs on the local machine.
  21. func NumCPU() int
  22. // NumCgoCall returns the number of cgo calls made by the current process.
  23. func NumCgoCall() int64
  24. // NumGoroutine returns the number of goroutines that currently exist.
  25. func NumGoroutine() int
  26. // MemProfileRate controls the fraction of memory allocations
  27. // that are recorded and reported in the memory profile.
  28. // The profiler aims to sample an average of
  29. // one allocation per MemProfileRate bytes allocated.
  30. //
  31. // To include every allocated block in the profile, set MemProfileRate to 1.
  32. // To turn off profiling entirely, set MemProfileRate to 0.
  33. //
  34. // The tools that process the memory profiles assume that the
  35. // profile rate is constant across the lifetime of the program
  36. // and equal to the current value. Programs that change the
  37. // memory profiling rate should do so just once, as early as
  38. // possible in the execution of the program (for example,
  39. // at the beginning of main).
  40. var MemProfileRate int = 512 * 1024
  41. // A MemProfileRecord describes the live objects allocated
  42. // by a particular call sequence (stack trace).
  43. type MemProfileRecord struct {
  44. AllocBytes, FreeBytes int64 // number of bytes allocated, freed
  45. AllocObjects, FreeObjects int64 // number of objects allocated, freed
  46. Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry
  47. }
  48. // InUseBytes returns the number of bytes in use (AllocBytes - FreeBytes).
  49. func (r *MemProfileRecord) InUseBytes() int64 { return r.AllocBytes - r.FreeBytes }
  50. // InUseObjects returns the number of objects in use (AllocObjects - FreeObjects).
  51. func (r *MemProfileRecord) InUseObjects() int64 {
  52. return r.AllocObjects - r.FreeObjects
  53. }
  54. // Stack returns the stack trace associated with the record,
  55. // a prefix of r.Stack0.
  56. func (r *MemProfileRecord) Stack() []uintptr {
  57. for i, v := range r.Stack0 {
  58. if v == 0 {
  59. return r.Stack0[0:i]
  60. }
  61. }
  62. return r.Stack0[0:]
  63. }
  64. // MemProfile returns n, the number of records in the current memory profile.
  65. // If len(p) >= n, MemProfile copies the profile into p and returns n, true.
  66. // If len(p) < n, MemProfile does not change p and returns n, false.
  67. //
  68. // If inuseZero is true, the profile includes allocation records
  69. // where r.AllocBytes > 0 but r.AllocBytes == r.FreeBytes.
  70. // These are sites where memory was allocated, but it has all
  71. // been released back to the runtime.
  72. //
  73. // Most clients should use the runtime/pprof package or
  74. // the testing package's -test.memprofile flag instead
  75. // of calling MemProfile directly.
  76. func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool)
  77. // A StackRecord describes a single execution stack.
  78. type StackRecord struct {
  79. Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry
  80. }
  81. // Stack returns the stack trace associated with the record,
  82. // a prefix of r.Stack0.
  83. func (r *StackRecord) Stack() []uintptr {
  84. for i, v := range r.Stack0 {
  85. if v == 0 {
  86. return r.Stack0[0:i]
  87. }
  88. }
  89. return r.Stack0[0:]
  90. }
  91. // ThreadCreateProfile returns n, the number of records in the thread creation profile.
  92. // If len(p) >= n, ThreadCreateProfile copies the profile into p and returns n, true.
  93. // If len(p) < n, ThreadCreateProfile does not change p and returns n, false.
  94. //
  95. // Most clients should use the runtime/pprof package instead
  96. // of calling ThreadCreateProfile directly.
  97. func ThreadCreateProfile(p []StackRecord) (n int, ok bool)
  98. // GoroutineProfile returns n, the number of records in the active goroutine stack profile.
  99. // If len(p) >= n, GoroutineProfile copies the profile into p and returns n, true.
  100. // If len(p) < n, GoroutineProfile does not change p and returns n, false.
  101. //
  102. // Most clients should use the runtime/pprof package instead
  103. // of calling GoroutineProfile directly.
  104. func GoroutineProfile(p []StackRecord) (n int, ok bool)
  105. // CPUProfile returns the next chunk of binary CPU profiling stack trace data,
  106. // blocking until data is available. If profiling is turned off and all the profile
  107. // data accumulated while it was on has been returned, CPUProfile returns nil.
  108. // The caller must save the returned data before calling CPUProfile again.
  109. //
  110. // Most clients should use the runtime/pprof package or
  111. // the testing package's -test.cpuprofile flag instead of calling
  112. // CPUProfile directly.
  113. func CPUProfile() []byte
  114. // SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
  115. // If hz <= 0, SetCPUProfileRate turns off profiling.
  116. // If the profiler is on, the rate cannot be changed without first turning it off.
  117. //
  118. // Most clients should use the runtime/pprof package or
  119. // the testing package's -test.cpuprofile flag instead of calling
  120. // SetCPUProfileRate directly.
  121. func SetCPUProfileRate(hz int)
  122. // SetBlockProfileRate controls the fraction of goroutine blocking events
  123. // that are reported in the blocking profile. The profiler aims to sample
  124. // an average of one blocking event per rate nanoseconds spent blocked.
  125. //
  126. // To include every blocking event in the profile, pass rate = 1.
  127. // To turn off profiling entirely, pass rate <= 0.
  128. func SetBlockProfileRate(rate int)
  129. // BlockProfileRecord describes blocking events originated
  130. // at a particular call sequence (stack trace).
  131. type BlockProfileRecord struct {
  132. Count int64
  133. Cycles int64
  134. StackRecord
  135. }
  136. // BlockProfile returns n, the number of records in the current blocking profile.
  137. // If len(p) >= n, BlockProfile copies the profile into p and returns n, true.
  138. // If len(p) < n, BlockProfile does not change p and returns n, false.
  139. //
  140. // Most clients should use the runtime/pprof package or
  141. // the testing package's -test.blockprofile flag instead
  142. // of calling BlockProfile directly.
  143. func BlockProfile(p []BlockProfileRecord) (n int, ok bool)
  144. // Stack formats a stack trace of the calling goroutine into buf
  145. // and returns the number of bytes written to buf.
  146. // If all is true, Stack formats stack traces of all other goroutines
  147. // into buf after the trace for the current goroutine.
  148. func Stack(buf []byte, all bool) int
  149. // Get field tracking information. Only fields with a tag go:"track"
  150. // are tracked. This function will add every such field that is
  151. // referenced to the map. The keys in the map will be
  152. // PkgPath.Name.FieldName. The value will be true for each field
  153. // added.
  154. func Fieldtrack(map[string]bool)