github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/mem.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  package runtime
     6  
     7  import "unsafe"
     8  
     9  // Note: the MemStats struct should be kept in sync with
    10  // struct MStats in malloc.h
    11  
    12  // A MemStats records statistics about the memory allocator.
    13  type MemStats struct {
    14  	// General statistics.
    15  	Alloc      uint64 // bytes allocated and still in use
    16  	TotalAlloc uint64 // bytes allocated (even if freed)
    17  	Sys        uint64 // bytes obtained from system (sum of XxxSys below)
    18  	Lookups    uint64 // number of pointer lookups
    19  	Mallocs    uint64 // number of mallocs
    20  	Frees      uint64 // number of frees
    21  
    22  	// Main allocation heap statistics.
    23  	HeapAlloc    uint64 // bytes allocated and still in use
    24  	HeapSys      uint64 // bytes obtained from system
    25  	HeapIdle     uint64 // bytes in idle spans
    26  	HeapInuse    uint64 // bytes in non-idle span
    27  	HeapReleased uint64 // bytes released to the OS
    28  	HeapObjects  uint64 // total number of allocated objects
    29  
    30  	// Low-level fixed-size structure allocator statistics.
    31  	//	Inuse is bytes used now.
    32  	//	Sys is bytes obtained from system.
    33  	StackInuse  uint64 // bytes used by stack allocator
    34  	StackSys    uint64
    35  	MSpanInuse  uint64 // mspan structures
    36  	MSpanSys    uint64
    37  	MCacheInuse uint64 // mcache structures
    38  	MCacheSys   uint64
    39  	BuckHashSys uint64 // profiling bucket hash table
    40  	GCSys       uint64 // GC metadata
    41  	OtherSys    uint64 // other system allocations
    42  
    43  	// Garbage collector statistics.
    44  	NextGC       uint64 // next collection will happen when HeapAlloc ≥ this amount
    45  	LastGC       uint64 // end time of last collection (nanoseconds since 1970)
    46  	PauseTotalNs uint64
    47  	PauseNs      [256]uint64 // circular buffer of recent GC pause durations, most recent at [(NumGC+255)%256]
    48  	PauseEnd     [256]uint64 // circular buffer of recent GC pause end times
    49  	NumGC        uint32
    50  	EnableGC     bool
    51  	DebugGC      bool
    52  
    53  	// Per-size allocation statistics.
    54  	// 61 is NumSizeClasses in the C code.
    55  	BySize [61]struct {
    56  		Size    uint32
    57  		Mallocs uint64
    58  		Frees   uint64
    59  	}
    60  }
    61  
    62  // Size of the trailing by_size array differs between Go and C,
    63  // and all data after by_size is local to runtime, not exported.
    64  // NumSizeClasses was changed, but we can not change Go struct because of backward compatibility.
    65  // sizeof_C_MStats is what C thinks about size of Go struct.
    66  var sizeof_C_MStats = unsafe.Offsetof(memstats.by_size) + 61*unsafe.Sizeof(memstats.by_size[0])
    67  
    68  func init() {
    69  	var memStats MemStats
    70  	if sizeof_C_MStats != unsafe.Sizeof(memStats) {
    71  		println(sizeof_C_MStats, unsafe.Sizeof(memStats))
    72  		gothrow("MStats vs MemStatsType size mismatch")
    73  	}
    74  }
    75  
    76  // ReadMemStats populates m with memory allocator statistics.
    77  func ReadMemStats(m *MemStats) {
    78  	// Have to acquire worldsema to stop the world,
    79  	// because stoptheworld can only be used by
    80  	// one goroutine at a time, and there might be
    81  	// a pending garbage collection already calling it.
    82  	semacquire(&worldsema, false)
    83  	gp := getg()
    84  	gp.m.gcing = 1
    85  	systemstack(stoptheworld)
    86  
    87  	systemstack(func() {
    88  		readmemstats_m(m)
    89  	})
    90  
    91  	gp.m.gcing = 0
    92  	gp.m.locks++
    93  	semrelease(&worldsema)
    94  	systemstack(starttheworld)
    95  	gp.m.locks--
    96  }
    97  
    98  // Implementation of runtime/debug.WriteHeapDump
    99  func writeHeapDump(fd uintptr) {
   100  	semacquire(&worldsema, false)
   101  	gp := getg()
   102  	gp.m.gcing = 1
   103  	systemstack(stoptheworld)
   104  
   105  	systemstack(func() {
   106  		writeheapdump_m(fd)
   107  	})
   108  
   109  	gp.m.gcing = 0
   110  	gp.m.locks++
   111  	semrelease(&worldsema)
   112  	systemstack(starttheworld)
   113  	gp.m.locks--
   114  }