aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/metrics
diff options
context:
space:
mode:
authorMichael Anthony Knyszek <mknyszek@google.com>2020-07-01 16:02:42 +0000
committerMichael Knyszek <mknyszek@google.com>2020-10-26 18:29:05 +0000
commitb08dfbaa439e4e396b979e02ea2e7d36972e8b7a (patch)
tree20fbf60331aeb10a72f74625c45f4b0bcb3ca527 /src/runtime/metrics
parent79781e8dd382ac34e502ed6a088dff6860a08c05 (diff)
downloadgo-b08dfbaa439e4e396b979e02ea2e7d36972e8b7a.tar.xz
runtime,runtime/metrics: add memory metrics
This change adds support for a variety of runtime memory metrics and contains the base implementation of Read for the runtime/metrics package, which lives in the runtime. It also adds testing infrastructure for the metrics package, and a bunch of format and documentation tests. For #37112. Change-Id: I16a2c4781eeeb2de0abcb045c15105f1210e2d8a Reviewed-on: https://go-review.googlesource.com/c/go/+/247041 Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com> Trust: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime/metrics')
-rw-r--r--src/runtime/metrics/description.go80
-rw-r--r--src/runtime/metrics/description_test.go125
-rw-r--r--src/runtime/metrics/doc.go56
-rw-r--r--src/runtime/metrics/sample.go10
4 files changed, 267 insertions, 4 deletions
diff --git a/src/runtime/metrics/description.go b/src/runtime/metrics/description.go
index 32bb950a72..2e7df7e09f 100644
--- a/src/runtime/metrics/description.go
+++ b/src/runtime/metrics/description.go
@@ -10,7 +10,7 @@ type Description struct {
//
// The format of the metric may be described by the following regular expression.
//
- // ^(?P<name>/[^:]+):(?P<unit>[^:*\/]+(?:[*\/][^:*\/]+)*)$
+ // ^(?P<name>/[^:]+):(?P<unit>[^:*/]+(?:[*/][^:*/]+)*)$
//
// The format splits the name into two components, separated by a colon: a path which always
// starts with a /, and a machine-parseable unit. The name may contain any valid Unicode
@@ -26,6 +26,9 @@ type Description struct {
// A complete name might look like "/memory/heap/free:bytes".
Name string
+ // Description is an English language sentence describing the metric.
+ Description string
+
// Kind is the kind of value for this metric.
//
// The purpose of this field is to allow users to filter out metrics whose values are
@@ -44,7 +47,80 @@ type Description struct {
StopTheWorld bool
}
-var allDesc = []Description{}
+// The English language descriptions below must be kept in sync with the
+// descriptions of each metric in doc.go.
+var allDesc = []Description{
+ {
+ Name: "/memory/classes/heap/free:bytes",
+ Description: "Memory that is available for allocation, and may be returned to the underlying system.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/heap/objects:bytes",
+ Description: "Memory occupied by live objects and dead objects that have not yet been collected.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/heap/released:bytes",
+ Description: "Memory that has been returned to the underlying system.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/heap/stacks:bytes",
+ Description: "Memory allocated from the heap that is occupied by stacks.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/heap/unused:bytes",
+ Description: "Memory that is unavailable for allocation, but cannot be returned to the underlying system.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/metadata/mcache/free:bytes",
+ Description: "Memory that is reserved for runtime mcache structures, but not in-use.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/metadata/mcache/inuse:bytes",
+ Description: "Memory that is occupied by runtime mcache structures that are currently being used.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/metadata/mspan/free:bytes",
+ Description: "Memory that is reserved for runtime mspan structures, but not in-use.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/metadata/mspan/inuse:bytes",
+ Description: "Memory that is occupied by runtime mspan structures that are currently being used.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/metadata/other:bytes",
+ Description: "Memory that is reserved for or used to hold runtime metadata.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/os-stacks:bytes",
+ Description: "Stack memory allocated by the underlying operating system.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/other:bytes",
+ Description: "Memory used by execution trace buffers, structures for debugging the runtime, finalizer and profiler specials, and more.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/profiling/buckets:bytes",
+ Description: "Memory that is used by the stack trace hash map used for profiling.",
+ Kind: KindUint64,
+ },
+ {
+ Name: "/memory/classes/total:bytes",
+ Description: "All memory mapped by the Go runtime into the current process as read-write. Note that this does not include memory mapped by code called via cgo or via the syscall package. Sum of all metrics in /memory/classes.",
+ Kind: KindUint64,
+ },
+}
// All returns a slice of containing metric descriptions for all supported metrics.
func All() []Description {
diff --git a/src/runtime/metrics/description_test.go b/src/runtime/metrics/description_test.go
new file mode 100644
index 0000000000..e966a281a1
--- /dev/null
+++ b/src/runtime/metrics/description_test.go
@@ -0,0 +1,125 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package metrics_test
+
+import (
+ "bufio"
+ "os"
+ "path/filepath"
+ "regexp"
+ "runtime"
+ "runtime/metrics"
+ "strings"
+ "testing"
+)
+
+func TestDescriptionNameFormat(t *testing.T) {
+ r := regexp.MustCompile("^(?P<name>/[^:]+):(?P<unit>[^:*/]+(?:[*/][^:*/]+)*)$")
+ descriptions := metrics.All()
+ for _, desc := range descriptions {
+ if !r.MatchString(desc.Name) {
+ t.Errorf("metrics %q does not match regexp %s", desc.Name, r)
+ }
+ }
+}
+
+func extractMetricDocs(t *testing.T) map[string]string {
+ if runtime.GOOS == "android" {
+ t.Skip("no access to Go source on android")
+ }
+
+ // Get doc.go.
+ _, filename, _, _ := runtime.Caller(0)
+ filename = filepath.Join(filepath.Dir(filename), "doc.go")
+
+ f, err := os.Open(filename)
+ if err != nil {
+ t.Fatal(err)
+ }
+ const (
+ stateSearch = iota // look for list of metrics
+ stateNextMetric // look for next metric
+ stateNextDescription // build description
+ )
+ state := stateSearch
+ s := bufio.NewScanner(f)
+ result := make(map[string]string)
+ var metric string
+ var prevMetric string
+ var desc strings.Builder
+ for s.Scan() {
+ line := strings.TrimSpace(s.Text())
+ switch state {
+ case stateSearch:
+ if line == "Supported metrics" {
+ state = stateNextMetric
+ }
+ case stateNextMetric:
+ // Ignore empty lines until we find a non-empty
+ // one. This will be our metric name.
+ if len(line) != 0 {
+ prevMetric = metric
+ metric = line
+ if prevMetric > metric {
+ t.Errorf("metrics %s and %s are out of lexicographical order", prevMetric, metric)
+ }
+ state = stateNextDescription
+ }
+ case stateNextDescription:
+ if len(line) == 0 || line == `*/` {
+ // An empty line means we're done.
+ // Write down the description and look
+ // for a new metric.
+ result[metric] = desc.String()
+ desc.Reset()
+ state = stateNextMetric
+ } else {
+ // As long as we're seeing data, assume that's
+ // part of the description and append it.
+ if desc.Len() != 0 {
+ // Turn previous newlines into spaces.
+ desc.WriteString(" ")
+ }
+ desc.WriteString(line)
+ }
+ }
+ if line == `*/` {
+ break
+ }
+ }
+ if state == stateSearch {
+ t.Fatalf("failed to find supported metrics docs in %s", filename)
+ }
+ return result
+}
+
+func TestDescriptionDocs(t *testing.T) {
+ docs := extractMetricDocs(t)
+ descriptions := metrics.All()
+ for _, d := range descriptions {
+ want := d.Description
+ got, ok := docs[d.Name]
+ if !ok {
+ t.Errorf("no docs found for metric %s", d.Name)
+ continue
+ }
+ if got != want {
+ t.Errorf("mismatched description and docs for metric %s", d.Name)
+ t.Errorf("want: %q, got %q", want, got)
+ continue
+ }
+ }
+ if len(docs) > len(descriptions) {
+ docsLoop:
+ for name, _ := range docs {
+ for _, d := range descriptions {
+ if name == d.Name {
+ continue docsLoop
+ }
+ }
+ t.Errorf("stale documentation for non-existent metric: %s", name)
+ }
+ }
+}
diff --git a/src/runtime/metrics/doc.go b/src/runtime/metrics/doc.go
index b48c22ba30..fb4e23a2b5 100644
--- a/src/runtime/metrics/doc.go
+++ b/src/runtime/metrics/doc.go
@@ -44,6 +44,60 @@ the documentation of the Name field of the Description struct.
Supported metrics
-TODO(mknyszek): List them here as they're added.
+ /memory/classes/heap/free:bytes
+ Memory that is available for allocation, and may be returned
+ to the underlying system.
+
+ /memory/classes/heap/objects:bytes
+ Memory occupied by live objects and dead objects that have
+ not yet been collected.
+
+ /memory/classes/heap/released:bytes
+ Memory that has been returned to the underlying system.
+
+ /memory/classes/heap/stacks:bytes
+ Memory allocated from the heap that is occupied by stacks.
+
+ /memory/classes/heap/unused:bytes
+ Memory that is unavailable for allocation, but cannot be
+ returned to the underlying system.
+
+ /memory/classes/metadata/mcache/free:bytes
+ Memory that is reserved for runtime mcache structures, but
+ not in-use.
+
+ /memory/classes/metadata/mcache/inuse:bytes
+ Memory that is occupied by runtime mcache structures that
+ are currently being used.
+
+ /memory/classes/metadata/mspan/free:bytes
+ Memory that is reserved for runtime mspan structures, but
+ not in-use.
+
+ /memory/classes/metadata/mspan/inuse:bytes
+ Memory that is occupied by runtime mspan structures that are
+ currently being used.
+
+ /memory/classes/metadata/other:bytes
+ Memory that is reserved for or used to hold runtime
+ metadata.
+
+ /memory/classes/os-stacks:bytes
+ Stack memory allocated by the underlying operating system.
+
+ /memory/classes/other:bytes
+ Memory used by execution trace buffers, structures for
+ debugging the runtime, finalizer and profiler specials, and
+ more.
+
+ /memory/classes/profiling/buckets:bytes
+ Memory that is used by the stack trace hash map used for
+ profiling.
+
+ /memory/classes/total:bytes
+ All memory mapped by the Go runtime into the current process
+ as read-write. Note that this does not include memory mapped
+ by code called via cgo or via the syscall package.
+ Sum of all metrics in /memory/classes.
*/
package metrics
diff --git a/src/runtime/metrics/sample.go b/src/runtime/metrics/sample.go
index c7a3fc424a..b4b0979aa6 100644
--- a/src/runtime/metrics/sample.go
+++ b/src/runtime/metrics/sample.go
@@ -4,6 +4,11 @@
package metrics
+import (
+ _ "runtime" // depends on the runtime via a linkname'd function
+ "unsafe"
+)
+
// Sample captures a single metric sample.
type Sample struct {
// Name is the name of the metric sampled.
@@ -16,6 +21,9 @@ type Sample struct {
Value Value
}
+// Implemented in the runtime.
+func runtime_readMetrics(unsafe.Pointer, int, int)
+
// Read populates each Value field in the given slice of metric samples.
//
// Desired metrics should be present in the slice with the appropriate name.
@@ -25,5 +33,5 @@ type Sample struct {
// will have the value populated as KindBad to indicate that the name is
// unknown.
func Read(m []Sample) {
- panic("unimplemented")
+ runtime_readMetrics(unsafe.Pointer(&m[0]), len(m), cap(m))
}