LEFT | RIGHT |
| 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 /* |
| 6 The runtime package contains operations that interact with Go's runtime
system, |
| 7 such as functions to control goroutines. It also includes the low-level
type information |
| 8 used by the reflect package; see reflect's documentation for the program
mable |
| 9 interface to the run-time type system. |
| 10 */ |
| 11 package runtime |
| 12 |
| 13 // Gosched yields the processor, allowing other goroutines to run. It does not |
| 14 // suspend the current goroutine, so execution resumes automatically. |
| 15 func Gosched() |
| 16 |
| 17 // Goexit terminates the goroutine that calls it. No other goroutine is affecte
d. |
| 18 func Goexit() |
| 19 |
| 20 // Breakpoint() executes a breakpoint trap. |
1 func Breakpoint() | 21 func Breakpoint() |
2 | 22 |
3 // Caller reports file and line number information about function invocations on | 23 // Caller reports file and line number information about function invocations on |
4 // the calling goroutine's stack. The argument is the number of stack frames to | 24 // the calling goroutine's stack. The argument skip is the number of stack fram
es to |
5 // ascend, with 0 identifying the the caller of Caller. The return values repor
t the | 25 // ascend, with 0 identifying the the caller of Caller. The return values repor
t the |
6 // program counter, file name, and line number within the file of the correspond
ing | 26 // program counter, file name, and line number within the file of the correspond
ing |
7 // call. The boolean ok is false if it was not possible to recover the informat
ion. | 27 // call. The boolean ok is false if it was not possible to recover the informat
ion. |
8 func Caller(n int) (pc uintptr, file string, line int, ok bool) | 28 func Caller(skip int) (pc uintptr, file string, line int, ok bool) |
| 29 |
| 30 // Callers fills the slice pc with the program counters of function invocations |
| 31 // on the calling goroutine's stack. The argument skip is the number of stack f
rames |
| 32 // to skip before recording in pc, with 0 starting at the caller of Caller. |
| 33 // It returns the number of entries written to pc. |
| 34 func Callers(skip int, pc []int) int |
9 | 35 |
10 // mid returns the current os thread (m) id. | 36 // mid returns the current os thread (m) id. |
11 func mid() uint32 | 37 func mid() uint32 |
| 38 |
| 39 // LockOSThread wires the calling goroutine to its current operating system thre
ad. |
| 40 // Until the calling goroutine exits or calls UnlockOSThread, it will always |
| 41 // execute in that thread, and no other goroutine can. |
| 42 // LockOSThread cannot be used during init functions. |
| 43 func LockOSThread() |
| 44 |
| 45 // UnlockOSThread unwires the calling goroutine from its fixed operating system
thread. |
| 46 // If the calling goroutine has not called LockOSThread, UnlockOSThread is a no-
op. |
| 47 func UnlockOSThread() |
| 48 |
| 49 // GOMAXPROCS sets the maximum number of CPUs that can be executing |
| 50 // simultaneously. This call will go away when the scheduler improves. |
| 51 func GOMAXPROCS(n int) |
| 52 |
| 53 // Cgocalls returns the number of cgo calls made by the current process. |
| 54 func Cgocalls() int64 |
| 55 |
| 56 // Semacquire waits until *s > 0 and then atomically decrements it. |
| 57 // It is intended as a simple sleep primitive for use by the synchronization |
| 58 // library and should not be used directly. |
| 59 func Semacquire(s *uint32) |
| 60 |
| 61 // Semrelease atomically increments *s and notifies a waiting goroutine |
| 62 // if one is blocked in Semacquire. |
| 63 // It is intended as a simple wakeup primitive for use by the synchronization |
| 64 // library and should not be used directly. |
| 65 func Semrelease(s *uint32) |
| 66 |
| 67 // Sigrecv returns a bitmask of signals that have arrived since the last call to
Sigrecv. |
| 68 // It blocks until at least one signal arrives. |
| 69 func Sigrecv() uint32 |
| 70 |
| 71 // Signame returns a string describing the signal, or "" if the signal is unknow
n. |
| 72 func Signame(sig int32) string |
| 73 |
| 74 // Siginit enables receipt of signals via Sigrecv. It should typically |
| 75 // be called during initialization. |
| 76 func Siginit() |
| 77 |
| 78 type MemStatsType struct { |
| 79 Alloc uint64 |
| 80 TotalAlloc uint64 |
| 81 Sys uint64 |
| 82 Stacks uint64 |
| 83 InusePages uint64 |
| 84 NextGC uint64 |
| 85 HeapAlloc uint64 |
| 86 Lookups uint64 |
| 87 Mallocs uint64 |
| 88 PauseNs uint64 |
| 89 NumGC uint32 |
| 90 EnableGC bool |
| 91 DebugGC bool |
| 92 BySize [67]struct { |
| 93 Size uint32 |
| 94 Mallocs uint64 |
| 95 Frees uint64 |
| 96 } |
| 97 } |
| 98 |
| 99 // MemStats holds statistics about the memory system. |
| 100 // The statistics are only approximate, as they are not interlocked on update. |
| 101 var MemStats MemStatsType |
| 102 |
| 103 // Alloc allocates a block of the given size. |
| 104 // FOR TESTING AND DEBUGGING ONLY. |
| 105 func Alloc(uintptr) *byte |
| 106 |
| 107 // Free frees the block starting at the given pointer. |
| 108 // FOR TESTING AND DEBUGGING ONLY. |
| 109 func Free(*byte) |
| 110 |
| 111 // Lookup returns the base and size of the block containing the given pointer. |
| 112 // FOR TESTING AND DEBUGGING ONLY. |
| 113 func Lookup(*byte) (*byte, uintptr) |
| 114 |
| 115 // GC runs a garbage collection. |
| 116 func GC() |
| 117 |
| 118 // SetFinalizer sets the finalizer associated with x to f. |
| 119 // When the garbage collector finds an unreachable block |
| 120 // with an associated finalizer, it clears the association and creates |
| 121 // a new goroutine running f(x). Creating the new goroutine makes |
| 122 // x reachable again, but now without an associated finalizer. |
| 123 // Assuming that SetFinalizer is not called again, the next time |
| 124 // the garbage collector sees that x is unreachable, it will free x. |
| 125 // |
| 126 // SetFinalizer(x, nil) clears any finalizer associated with f. |
| 127 // |
| 128 // The argument x must be a pointer to an object allocated by |
| 129 // calling new or by taking the address of a composite literal. |
| 130 // The argument f must be a function that takes a single argument |
| 131 // of x's type and returns no arguments. If either of these is not |
| 132 // true, SetFinalizer aborts the program. |
| 133 // |
| 134 // Finalizers are run in dependency order: if A points at B, both have |
| 135 // finalizers, and they are otherwise unreachable, only the finalizer |
| 136 // for A runs; once A is freed, the finalizer for B can run. |
| 137 // If a cyclic structure includes a block with a finalizer, that |
| 138 // cycle is not guaranteed to be garbage collected and the finalizer |
| 139 // is not guaranteed to run, because there is no ordering that |
| 140 // respects the dependencies. |
| 141 // |
| 142 // The finalizer for x is scheduled to run at some arbitrary time after |
| 143 // x becomes unreachable. |
| 144 // There is no guarantee that finalizers will run before a program exits, |
| 145 // so typically they are useful only for releasing non-memory resources |
| 146 // associated with an object during a long-running program. |
| 147 // For example, an os.File object could use a finalizer to close the |
| 148 // associated operating system file descriptor when a program discards |
| 149 // an os.File without calling Close, but it would be a mistake |
| 150 // to depend on a finalizer to flush an in-memory I/O buffer such as a |
| 151 // bufio.Writer, because the buffer would not be flushed at program exit. |
| 152 // |
| 153 // TODO(rsc): make os.File use SetFinalizer |
| 154 // TODO(rsc): allow f to have (ignored) return values |
| 155 // |
| 156 func SetFinalizer(x, f interface{}) |
| 157 |
| 158 func getgoroot() string |
| 159 |
| 160 // GOROOT returns the root of the Go tree. |
| 161 // It uses the GOROOT environment variable, if set, |
| 162 // or else the root used during the Go build. |
| 163 func GOROOT() string { |
| 164 s := getgoroot() |
| 165 if s != "" { |
| 166 return s |
| 167 } |
| 168 return defaultGoroot |
| 169 } |
| 170 |
| 171 // Version returns the Go tree's version string. |
| 172 // It is either a sequence number or, when possible, |
| 173 // a release tag like "release.2010-03-04". |
| 174 // A trailing + indicates that the tree had local modifications |
| 175 // at the time of the build. |
| 176 func Version() string { return defaultVersion } |
| 177 |
| 178 // MemProfileKind specifies how frequently to record |
| 179 // memory allocations in the memory profiler. |
| 180 type MemProfileKind int |
| 181 |
| 182 const ( |
| 183 MemProfileNone MemProfileKind = iota // no profiling |
| 184 MemProfileSample // profile random sample |
| 185 MemProfileAll // profile every allocation |
| 186 ) |
| 187 |
| 188 // SetMemProfileKind sets the fraction of memory allocations |
| 189 // that are recorded and reported in the memory profile. |
| 190 // Profiling an allocation has a small overhead, so the default |
| 191 // is to profile only a random sample, weighted by block size. |
| 192 func SetMemProfileKind(kind MemProfileKind) |
LEFT | RIGHT |