Source file src/pkg/runtime/debug.go
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 5 package runtime 6 7 // Breakpoint() executes a breakpoint trap. 8 func Breakpoint() 9 10 // LockOSThread wires the calling goroutine to its current operating system thread. 11 // Until the calling goroutine exits or calls UnlockOSThread, it will always 12 // execute in that thread, and no other goroutine can. 13 // LockOSThread cannot be used during init functions. 14 func LockOSThread() 15 16 // UnlockOSThread unwires the calling goroutine from its fixed operating system thread. 17 // If the calling goroutine has not called LockOSThread, UnlockOSThread is a no-op. 18 func UnlockOSThread() 19 20 // GOMAXPROCS sets the maximum number of CPUs that can be executing 21 // simultaneously and returns the previous setting. If n < 1, it does not 22 // change the current setting. 23 // This call will go away when the scheduler improves. 24 func GOMAXPROCS(n int) int 25 26 // Cgocalls returns the number of cgo calls made by the current process. 27 func Cgocalls() int64 28 29 // Goroutines returns the number of goroutines that currently exist. 30 func Goroutines() int32 31 32 // Alloc allocates a block of the given size. 33 // FOR TESTING AND DEBUGGING ONLY. 34 func Alloc(uintptr) *byte 35 36 // Free frees the block starting at the given pointer. 37 // FOR TESTING AND DEBUGGING ONLY. 38 func Free(*byte) 39 40 // Lookup returns the base and size of the block containing the given pointer. 41 // FOR TESTING AND DEBUGGING ONLY. 42 func Lookup(*byte) (*byte, uintptr) 43 44 // MemProfileRate controls the fraction of memory allocations 45 // that are recorded and reported in the memory profile. 46 // The profiler aims to sample an average of 47 // one allocation per MemProfileRate bytes allocated. 48 // 49 // To include every allocated block in the profile, set MemProfileRate to 1. 50 // To turn off profiling entirely, set MemProfileRate to 0. 51 // 52 // The tools that process the memory profiles assume that the 53 // profile rate is constant across the lifetime of the program 54 // and equal to the current value. Programs that change the 55 // memory profiling rate should do so just once, as early as 56 // possible in the execution of the program (for example, 57 // at the beginning of main). 58 var MemProfileRate int = 512 * 1024 59 60 // A MemProfileRecord describes the live objects allocated 61 // by a particular call sequence (stack trace). 62 type MemProfileRecord struct { 63 AllocBytes, FreeBytes int64 // number of bytes allocated, freed 64 AllocObjects, FreeObjects int64 // number of objects allocated, freed 65 Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry 66 } 67 68 // InUseBytes returns the number of bytes in use (AllocBytes - FreeBytes). 69 func (r *MemProfileRecord) InUseBytes() int64 { return r.AllocBytes - r.FreeBytes } 70 71 // InUseObjects returns the number of objects in use (AllocObjects - FreeObjects). 72 func (r *MemProfileRecord) InUseObjects() int64 { 73 return r.AllocObjects - r.FreeObjects 74 } 75 76 // Stack returns the stack trace associated with the record, 77 // a prefix of r.Stack0. 78 func (r *MemProfileRecord) Stack() []uintptr { 79 for i, v := range r.Stack0 { 80 if v == 0 { 81 return r.Stack0[0:i] 82 } 83 } 84 return r.Stack0[0:] 85 } 86 87 // MemProfile returns n, the number of records in the current memory profile. 88 // If len(p) >= n, MemProfile copies the profile into p and returns n, true. 89 // If len(p) < n, MemProfile does not change p and returns n, false. 90 // 91 // If inuseZero is true, the profile includes allocation records 92 // where r.AllocBytes > 0 but r.AllocBytes == r.FreeBytes. 93 // These are sites where memory was allocated, but it has all 94 // been released back to the runtime. 95 // Most clients should use the runtime/pprof package or 96 // the testing package's -test.memprofile flag instead 97 // of calling MemProfile directly. 98 func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool) 99 100 // CPUProfile returns the next chunk of binary CPU profiling stack trace data, 101 // blocking until data is available. If profiling is turned off and all the profile 102 // data accumulated while it was on has been returned, CPUProfile returns nil. 103 // The caller must save the returned data before calling CPUProfile again. 104 // Most clients should use the runtime/pprof package or 105 // the testing package's -test.cpuprofile flag instead of calling 106 // CPUProfile directly. 107 func CPUProfile() []byte 108 109 // SetCPUProfileRate sets the CPU profiling rate to hz samples per second. 110 // If hz <= 0, SetCPUProfileRate turns off profiling. 111 // If the profiler is on, the rate cannot be changed without first turning it off. 112 // Most clients should use the runtime/pprof package or 113 // the testing package's -test.cpuprofile flag instead of calling 114 // SetCPUProfileRate directly. 115 func SetCPUProfileRate(hz int)