github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/extern.go (about)

     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  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.
    10  
    11  Environment Variables
    12  
    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.
    16  
    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 http://golang.org/pkg/runtime/debug/#SetGCPercent.
    23  
    24  The GOGCTRACE variable controls debug output from the garbage collector.
    25  Setting GOGCTRACE=1 causes the garbage collector to emit a single line to standard
    26  error at each collection, summarizing the amount of memory collected and the
    27  length of the pause. Setting GOGCTRACE=2 emits the same summary but also
    28  repeats each collection.
    29  
    30  The GOMAXPROCS variable limits the number of operating system threads that
    31  can execute user-level Go code simultaneously. There is no limit to the number of threads
    32  that can be blocked in system calls on behalf of Go code; those do not count against
    33  the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
    34  the limit.
    35  
    36  The GOTRACEBACK variable controls the amount of output generated when a Go
    37  program fails due to an unrecovered panic or an unexpected runtime condition.
    38  By default, a failure prints a stack trace for every extant goroutine, eliding functions
    39  internal to the run-time system, and then exits with exit code 2.
    40  If GOTRACEBACK=0, the per-goroutine stack traces are omitted entirely.
    41  If GOTRACEBACK=1, the default behavior is used.
    42  If GOTRACEBACK=2, the per-goroutine stack traces include run-time functions.
    43  If GOTRACEBACK=crash, the per-goroutine stack traces include run-time functions,
    44  and if possible the program crashes in an operating-specific manner instead of
    45  exiting. For example, on Unix systems, the program raises SIGABRT to trigger a
    46  core dump.
    47  
    48  The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
    49  the set of Go environment variables. They influence the building of Go programs
    50  (see http://golang.org/cmd/go and http://golang.org/pkg/go/build).
    51  GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
    52  constants or functions in this package, but they do not influence the execution
    53  of the run-time system.
    54  */
    55  package runtime
    56  
    57  // Gosched yields the processor, allowing other goroutines to run.  It does not
    58  // suspend the current goroutine, so execution resumes automatically.
    59  func Gosched()
    60  
    61  // Goexit terminates the goroutine that calls it.  No other goroutine is affected.
    62  // Goexit runs all deferred calls before terminating the goroutine.
    63  func Goexit()
    64  
    65  // Caller reports file and line number information about function invocations on
    66  // the calling goroutine's stack.  The argument skip is the number of stack frames
    67  // to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
    68  // meaning of skip differs between Caller and Callers.) The return values report the
    69  // program counter, file name, and line number within the file of the corresponding
    70  // call.  The boolean ok is false if it was not possible to recover the information.
    71  func Caller(skip int) (pc uintptr, file string, line int, ok bool)
    72  
    73  // Callers fills the slice pc with the program counters of function invocations
    74  // on the calling goroutine's stack.  The argument skip is the number of stack frames
    75  // to skip before recording in pc, with 0 identifying the frame for Callers itself and
    76  // 1 identifying the caller of Callers.
    77  // It returns the number of entries written to pc.
    78  func Callers(skip int, pc []uintptr) int
    79  
    80  type Func struct { // Keep in sync with runtime.h:struct Func
    81  	name   string
    82  	typ    string  // go type string
    83  	src    string  // src file name
    84  	pcln   []byte  // pc/ln tab for this func
    85  	entry  uintptr // entry pc
    86  	pc0    uintptr // starting pc, ln for table
    87  	ln0    int32
    88  	frame  int32 // stack frame size
    89  	args   int32 // in/out args size
    90  	locals int32 // locals size
    91  }
    92  
    93  // FuncForPC returns a *Func describing the function that contains the
    94  // given program counter address, or else nil.
    95  func FuncForPC(pc uintptr) *Func
    96  
    97  // Name returns the name of the function.
    98  func (f *Func) Name() string { return f.name }
    99  
   100  // Entry returns the entry address of the function.
   101  func (f *Func) Entry() uintptr { return f.entry }
   102  
   103  // FileLine returns the file name and line number of the
   104  // source code corresponding to the program counter pc.
   105  // The result will not be accurate if pc is not a program
   106  // counter within f.
   107  func (f *Func) FileLine(pc uintptr) (file string, line int) {
   108  	return funcline_go(f, pc)
   109  }
   110  
   111  // implemented in symtab.c
   112  func funcline_go(*Func, uintptr) (string, int)
   113  
   114  // mid returns the current OS thread (m) id.
   115  func mid() uint32
   116  
   117  // SetFinalizer sets the finalizer associated with x to f.
   118  // When the garbage collector finds an unreachable block
   119  // with an associated finalizer, it clears the association and runs
   120  // f(x) in a separate goroutine.  This makes x reachable again, but
   121  // now without an associated finalizer.  Assuming that SetFinalizer
   122  // is not called again, the next time the garbage collector sees
   123  // that x is unreachable, it will free x.
   124  //
   125  // SetFinalizer(x, nil) clears any finalizer associated with x.
   126  //
   127  // The argument x must be a pointer to an object allocated by
   128  // calling new or by taking the address of a composite literal.
   129  // The argument f must be a function that takes a single argument
   130  // of x's type and can have arbitrary ignored return values.
   131  // If either of these is not true, SetFinalizer aborts the program.
   132  //
   133  // Finalizers are run in dependency order: if A points at B, both have
   134  // finalizers, and they are otherwise unreachable, only the finalizer
   135  // for A runs; once A is freed, the finalizer for B can run.
   136  // If a cyclic structure includes a block with a finalizer, that
   137  // cycle is not guaranteed to be garbage collected and the finalizer
   138  // is not guaranteed to run, because there is no ordering that
   139  // respects the dependencies.
   140  //
   141  // The finalizer for x is scheduled to run at some arbitrary time after
   142  // x becomes unreachable.
   143  // There is no guarantee that finalizers will run before a program exits,
   144  // so typically they are useful only for releasing non-memory resources
   145  // associated with an object during a long-running program.
   146  // For example, an os.File object could use a finalizer to close the
   147  // associated operating system file descriptor when a program discards
   148  // an os.File without calling Close, but it would be a mistake
   149  // to depend on a finalizer to flush an in-memory I/O buffer such as a
   150  // bufio.Writer, because the buffer would not be flushed at program exit.
   151  //
   152  // A single goroutine runs all finalizers for a program, sequentially.
   153  // If a finalizer must run for a long time, it should do so by starting
   154  // a new goroutine.
   155  func SetFinalizer(x, f interface{})
   156  
   157  func getgoroot() string
   158  
   159  // GOROOT returns the root of the Go tree.
   160  // It uses the GOROOT environment variable, if set,
   161  // or else the root used during the Go build.
   162  func GOROOT() string {
   163  	s := getgoroot()
   164  	if s != "" {
   165  		return s
   166  	}
   167  	return defaultGoroot
   168  }
   169  
   170  // Version returns the Go tree's version string.
   171  // It is either a sequence number or, when possible,
   172  // a release tag like "release.2010-03-04".
   173  // A trailing + indicates that the tree had local modifications
   174  // at the time of the build.
   175  func Version() string {
   176  	return theVersion
   177  }
   178  
   179  // GOOS is the running program's operating system target:
   180  // one of darwin, freebsd, linux, and so on.
   181  const GOOS string = theGoos
   182  
   183  // GOARCH is the running program's architecture target:
   184  // 386, amd64, or arm.
   185  const GOARCH string = theGoarch