]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: add helper for getting an mcache in allocation contexts
authorMichael Anthony Knyszek <mknyszek@google.com>
Mon, 3 Aug 2020 20:08:25 +0000 (20:08 +0000)
committerMichael Knyszek <mknyszek@google.com>
Mon, 26 Oct 2020 18:25:20 +0000 (18:25 +0000)
This change adds a function getMCache which returns the current P's
mcache if it's available, and otherwise tries to get mcache0 if we're
bootstrapping. This function will come in handy as we need to replicate
this behavior in multiple places in future changes.

Change-Id: I536073d6f6dc6c6390269e613ead9f8bcb6e7f98
Reviewed-on: https://go-review.googlesource.com/c/go/+/246976
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/malloc.go
src/runtime/mcache.go

index ee22bad58c1094b9f3fa8d4bfc673642e6ce16fa..6383c34817c668091aa7c45b229a816c30c37483 100644 (file)
@@ -972,19 +972,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
 
        shouldhelpgc := false
        dataSize := size
-       var c *mcache
-       if mp.p != 0 {
-               c = mp.p.ptr().mcache
-       } else {
-               // We will be called without a P while bootstrapping,
-               // in which case we use mcache0, which is set in mallocinit.
-               // mcache0 is cleared when bootstrapping is complete,
-               // by procresize.
-               c = mcache0
-               if c == nil {
-                       throw("malloc called with no P")
-               }
-       }
+       c := getMCache()
        var span *mspan
        var x unsafe.Pointer
        noscan := typ == nil || typ.ptrdata == 0
@@ -1212,16 +1200,7 @@ func reflect_unsafe_NewArray(typ *_type, n int) unsafe.Pointer {
 }
 
 func profilealloc(mp *m, x unsafe.Pointer, size uintptr) {
-       var c *mcache
-       if mp.p != 0 {
-               c = mp.p.ptr().mcache
-       } else {
-               c = mcache0
-               if c == nil {
-                       throw("profilealloc called with no P")
-               }
-       }
-       c.nextSample = nextSample()
+       getMCache().nextSample = nextSample()
        mProf_Malloc(x, size)
 }
 
index c3e0e5e1f7694a0c15ecef9d2140aa679cd0919d..5564e4a47da1ff3f2759f93b041e89fd1f613849 100644 (file)
@@ -131,6 +131,29 @@ func freemcache(c *mcache, recipient *mcache) {
        })
 }
 
+// getMCache is a convenience function which tries to obtain an mcache.
+//
+// Must be running with a P when called (so the caller must be in a
+// non-preemptible state) or must be called during bootstrapping.
+func getMCache() *mcache {
+       // Grab the mcache, since that's where stats live.
+       pp := getg().m.p.ptr()
+       var c *mcache
+       if pp == nil {
+               // We will be called without a P while bootstrapping,
+               // in which case we use mcache0, which is set in mallocinit.
+               // mcache0 is cleared when bootstrapping is complete,
+               // by procresize.
+               c = mcache0
+               if c == nil {
+                       throw("getMCache called with no P or outside bootstrapping")
+               }
+       } else {
+               c = pp.mcache
+       }
+       return c
+}
+
 // donate flushes data and resources which have no global
 // pool to another mcache.
 func (c *mcache) donate(d *mcache) {