Accidentally failed to commit these earlier, as part of:
[official-gcc.git] / libgo / go / runtime / extern.go
blobe0c5aacc55a66f93bab1ac243c7af3d857b86a8e
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.
5 /*
6 Package runtime 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 programmable
9 interface to the run-time type system.
11 Environment Variables
13 The following environment variables ($name or %name%, depending on the host
14 operating system) control the run-time behavior of Go programs. The meanings
15 and use may change from release to release.
17 The GOGC variable sets the initial garbage collection target percentage.
18 A collection is triggered when the ratio of freshly allocated data to live data
19 remaining after the previous collection reaches this percentage. The default
20 is GOGC=100. Setting GOGC=off disables the garbage collector entirely.
21 The runtime/debug package's SetGCPercent function allows changing this
22 percentage at run time. See https://golang.org/pkg/runtime/debug/#SetGCPercent.
24 The GODEBUG variable controls debugging variables within the runtime.
25 It is a comma-separated list of name=val pairs setting these named variables:
27 allocfreetrace: setting allocfreetrace=1 causes every allocation to be
28 profiled and a stack trace printed on each object's allocation and free.
30 cgocheck: setting cgocheck=0 disables all checks for packages
31 using cgo to incorrectly pass Go pointers to non-Go code.
32 Setting cgocheck=1 (the default) enables relatively cheap
33 checks that may miss some errors. Setting cgocheck=2 enables
34 expensive checks that should not miss any errors, but will
35 cause your program to run slower.
37 efence: setting efence=1 causes the allocator to run in a mode
38 where each object is allocated on a unique page and addresses are
39 never recycled.
41 gccheckmark: setting gccheckmark=1 enables verification of the
42 garbage collector's concurrent mark phase by performing a
43 second mark pass while the world is stopped. If the second
44 pass finds a reachable object that was not found by concurrent
45 mark, the garbage collector will panic.
47 gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
48 print information about the internal state of the concurrent pacer.
50 gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
51 onto smaller stacks. In this mode, a goroutine's stack can only grow.
53 gcstackbarrieroff: setting gcstackbarrieroff=1 disables the use of stack barriers
54 that allow the garbage collector to avoid repeating a stack scan during the
55 mark termination phase.
57 gcstackbarrierall: setting gcstackbarrierall=1 installs stack barriers
58 in every stack frame, rather than in exponentially-spaced frames.
60 gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
61 making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
62 also disables concurrent sweeping after the garbage collection finishes.
64 gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
65 error at each collection, summarizing the amount of memory collected and the
66 length of the pause. Setting gctrace=2 emits the same summary but also
67 repeats each collection. The format of this line is subject to change.
68 Currently, it is:
69 gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
70 where the fields are as follows:
71 gc # the GC number, incremented at each GC
72 @#s time in seconds since program start
73 #% percentage of time spent in GC since program start
74 #+...+# wall-clock/CPU times for the phases of the GC
75 #->#-># MB heap size at GC start, at GC end, and live heap
76 # MB goal goal heap size
77 # P number of processors used
78 The phases are stop-the-world (STW) sweep termination, concurrent
79 mark and scan, and STW mark termination. The CPU times
80 for mark/scan are broken down in to assist time (GC performed in
81 line with allocation), background GC time, and idle GC time.
82 If the line ends with "(forced)", this GC was forced by a
83 runtime.GC() call and all phases are STW.
85 Setting gctrace to any value > 0 also causes the garbage collector
86 to emit a summary when memory is released back to the system.
87 This process of returning memory to the system is called scavenging.
88 The format of this summary is subject to change.
89 Currently it is:
90 scvg#: # MB released printed only if non-zero
91 scvg#: inuse: # idle: # sys: # released: # consumed: # (MB)
92 where the fields are as follows:
93 scvg# the scavenge cycle number, incremented at each scavenge
94 inuse: # MB used or partially used spans
95 idle: # MB spans pending scavenging
96 sys: # MB mapped from the system
97 released: # MB released to the system
98 consumed: # MB allocated from the system
100 memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
101 When set to 0 memory profiling is disabled. Refer to the description of
102 MemProfileRate for the default value.
104 memprofilerate: setting memprofilerate=X changes the setting for
105 runtime.MemProfileRate. Refer to the description of this variable for how
106 it is used and its default value.
108 sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
109 with a trivial allocator that obtains memory from the operating system and
110 never reclaims any memory.
112 scavenge: scavenge=1 enables debugging mode of heap scavenger.
114 scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
115 detailed multiline info every X milliseconds, describing state of the scheduler,
116 processors, threads and goroutines.
118 schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
119 error every X milliseconds, summarizing the scheduler state.
121 The net and net/http packages also refer to debugging variables in GODEBUG.
122 See the documentation for those packages for details.
124 The GOMAXPROCS variable limits the number of operating system threads that
125 can execute user-level Go code simultaneously. There is no limit to the number of threads
126 that can be blocked in system calls on behalf of Go code; those do not count against
127 the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
128 the limit.
130 The GOTRACEBACK variable controls the amount of output generated when a Go
131 program fails due to an unrecovered panic or an unexpected runtime condition.
132 By default, a failure prints a stack trace for the current goroutine,
133 eliding functions internal to the run-time system, and then exits with exit code 2.
134 The failure prints stack traces for all goroutines if there is no current goroutine
135 or the failure is internal to the run-time.
136 GOTRACEBACK=none omits the goroutine stack traces entirely.
137 GOTRACEBACK=single (the default) behaves as described above.
138 GOTRACEBACK=all adds stack traces for all user-created goroutines.
139 GOTRACEBACK=system is like ``all'' but adds stack frames for run-time functions
140 and shows goroutines created internally by the run-time.
141 GOTRACEBACK=crash is like ``system'' but crashes in an operating system-specific
142 manner instead of exiting. For example, on Unix systems, the crash raises
143 SIGABRT to trigger a core dump.
144 For historical reasons, the GOTRACEBACK settings 0, 1, and 2 are synonyms for
145 none, all, and system, respectively.
146 The runtime/debug package's SetTraceback function allows increasing the
147 amount of output at run time, but it cannot reduce the amount below that
148 specified by the environment variable.
149 See https://golang.org/pkg/runtime/debug/#SetTraceback.
151 The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
152 the set of Go environment variables. They influence the building of Go programs
153 (see https://golang.org/cmd/go and https://golang.org/pkg/go/build).
154 GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
155 constants or functions in this package, but they do not influence the execution
156 of the run-time system.
158 package runtime
160 import "runtime/internal/sys"
162 // Gosched yields the processor, allowing other goroutines to run. It does not
163 // suspend the current goroutine, so execution resumes automatically.
164 func Gosched()
166 // Goexit terminates the goroutine that calls it. No other goroutine is affected.
167 // Goexit runs all deferred calls before terminating the goroutine.
169 // Calling Goexit from the main goroutine terminates that goroutine
170 // without func main returning. Since func main has not returned,
171 // the program continues execution of other goroutines.
172 // If all other goroutines exit, the program crashes.
173 func Goexit()
175 // Caller reports file and line number information about function invocations on
176 // the calling goroutine's stack. The argument skip is the number of stack frames
177 // to ascend, with 0 identifying the caller of Caller. (For historical reasons the
178 // meaning of skip differs between Caller and Callers.) The return values report the
179 // program counter, file name, and line number within the file of the corresponding
180 // call. The boolean ok is false if it was not possible to recover the information.
181 func Caller(skip int) (pc uintptr, file string, line int, ok bool)
183 // Callers fills the slice pc with the return program counters of function invocations
184 // on the calling goroutine's stack. The argument skip is the number of stack frames
185 // to skip before recording in pc, with 0 identifying the frame for Callers itself and
186 // 1 identifying the caller of Callers.
187 // It returns the number of entries written to pc.
188 func Callers(skip int, pc []uintptr) int
190 // SetFinalizer sets the finalizer associated with obj to the provided
191 // finalizer function. When the garbage collector finds an unreachable block
192 // with an associated finalizer, it clears the association and runs
193 // finalizer(obj) in a separate goroutine. This makes obj reachable again,
194 // but now without an associated finalizer. Assuming that SetFinalizer
195 // is not called again, the next time the garbage collector sees
196 // that obj is unreachable, it will free obj.
198 // SetFinalizer(obj, nil) clears any finalizer associated with obj.
200 // The argument obj must be a pointer to an object allocated by
201 // calling new or by taking the address of a composite literal.
202 // The argument finalizer must be a function that takes a single argument
203 // to which obj's type can be assigned, and can have arbitrary ignored return
204 // values. If either of these is not true, SetFinalizer aborts the
205 // program.
207 // Finalizers are run in dependency order: if A points at B, both have
208 // finalizers, and they are otherwise unreachable, only the finalizer
209 // for A runs; once A is freed, the finalizer for B can run.
210 // If a cyclic structure includes a block with a finalizer, that
211 // cycle is not guaranteed to be garbage collected and the finalizer
212 // is not guaranteed to run, because there is no ordering that
213 // respects the dependencies.
215 // The finalizer for obj is scheduled to run at some arbitrary time after
216 // obj becomes unreachable.
217 // There is no guarantee that finalizers will run before a program exits,
218 // so typically they are useful only for releasing non-memory resources
219 // associated with an object during a long-running program.
220 // For example, an os.File object could use a finalizer to close the
221 // associated operating system file descriptor when a program discards
222 // an os.File without calling Close, but it would be a mistake
223 // to depend on a finalizer to flush an in-memory I/O buffer such as a
224 // bufio.Writer, because the buffer would not be flushed at program exit.
226 // It is not guaranteed that a finalizer will run if the size of *obj is
227 // zero bytes.
229 // It is not guaranteed that a finalizer will run for objects allocated
230 // in initializers for package-level variables. Such objects may be
231 // linker-allocated, not heap-allocated.
233 // A finalizer may run as soon as an object becomes unreachable.
234 // In order to use finalizers correctly, the program must ensure that
235 // the object is reachable until it is no longer required.
236 // Objects stored in global variables, or that can be found by tracing
237 // pointers from a global variable, are reachable. For other objects,
238 // pass the object to a call of the KeepAlive function to mark the
239 // last point in the function where the object must be reachable.
241 // For example, if p points to a struct that contains a file descriptor d,
242 // and p has a finalizer that closes that file descriptor, and if the last
243 // use of p in a function is a call to syscall.Write(p.d, buf, size), then
244 // p may be unreachable as soon as the program enters syscall.Write. The
245 // finalizer may run at that moment, closing p.d, causing syscall.Write
246 // to fail because it is writing to a closed file descriptor (or, worse,
247 // to an entirely different file descriptor opened by a different goroutine).
248 // To avoid this problem, call runtime.KeepAlive(p) after the call to
249 // syscall.Write.
251 // A single goroutine runs all finalizers for a program, sequentially.
252 // If a finalizer must run for a long time, it should do so by starting
253 // a new goroutine.
254 func SetFinalizer(obj interface{}, finalizer interface{})
256 // KeepAlive marks its argument as currently reachable.
257 // This ensures that the object is not freed, and its finalizer is not run,
258 // before the point in the program where KeepAlive is called.
260 // A very simplified example showing where KeepAlive is required:
261 // type File struct { d int }
262 // d, err := syscall.Open("/file/path", syscall.O_RDONLY, 0)
263 // // ... do something if err != nil ...
264 // p := &File{d}
265 // runtime.SetFinalizer(p, func(p *File) { syscall.Close(p.d) })
266 // var buf [10]byte
267 // n, err := syscall.Read(p.d, buf[:])
268 // // Ensure p is not finalized until Read returns.
269 // runtime.KeepAlive(p)
270 // // No more uses of p after this point.
272 // Without the KeepAlive call, the finalizer could run at the start of
273 // syscall.Read, closing the file descriptor before syscall.Read makes
274 // the actual system call.
275 func KeepAlive(interface{})
277 func getgoroot() string
279 // GOROOT returns the root of the Go tree.
280 // It uses the GOROOT environment variable, if set,
281 // or else the root used during the Go build.
282 func GOROOT() string {
283 s := getgoroot()
284 if s != "" {
285 return s
287 return sys.DefaultGoroot
290 // Version returns the Go tree's version string.
291 // It is either the commit hash and date at the time of the build or,
292 // when possible, a release tag like "go1.3".
293 func Version() string {
294 return sys.TheVersion
297 // GOOS is the running program's operating system target:
298 // one of darwin, freebsd, linux, and so on.
299 const GOOS string = sys.GOOS
301 // GOARCH is the running program's architecture target:
302 // 386, amd64, arm, or s390x.
303 const GOARCH string = sys.GOARCH
305 // GCCGOTOOLDIR is the Tool Dir for the gccgo build
306 const GCCGOTOOLDIR string = sys.GccgoToolDir