Skip to content

Commit 1033065

Browse files
committed
runtime: add physHugePageSize
This change adds the global physHugePageSize which is initialized in osinit(). physHugePageSize contains the system's transparent huge page (or superpage) size in bytes. For #30333. Change-Id: I2f0198c40729dbbe6e6f2676cef1d57dd107562c Reviewed-on: https://go-review.googlesource.com/c/go/+/170858 Run-TryBot: Michael Knyszek <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 8b2bd6f commit 1033065

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/runtime/malloc.go

+8
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ const (
324324
// mallocinit.
325325
var physPageSize uintptr
326326

327+
// physHugePageSize is the size in bytes of the OS's default physical huge
328+
// page size whose allocation is opaque to the application.
329+
//
330+
// If set, this must be set by the OS init code (typically in osinit) before
331+
// mallocinit. However, setting it at all is optional, and leaving the default
332+
// value is always safe (though potentially less efficient).
333+
var physHugePageSize uintptr
334+
327335
// OS-defined helpers:
328336
//
329337
// sysAlloc obtains a large chunk of zeroed memory from the

src/runtime/os_linux.go

+23
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,31 @@ func sysauxv(auxv []uintptr) int {
261261
return i / 2
262262
}
263263

264+
var sysTHPSizePath = []byte("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size\x00")
265+
266+
func getHugePageSize() uintptr {
267+
var numbuf [20]byte
268+
fd := open(&sysTHPSizePath[0], 0 /* O_RDONLY */, 0)
269+
if fd < 0 {
270+
return 0
271+
}
272+
n := read(fd, noescape(unsafe.Pointer(&numbuf[0])), int32(len(numbuf)))
273+
if n <= 0 {
274+
closefd(fd)
275+
return 0
276+
}
277+
l := n - 1 // remove trailing newline
278+
v, ok := atoi(slicebytetostringtmp(numbuf[:l]))
279+
if !ok || v < 0 {
280+
v = 0
281+
}
282+
closefd(fd)
283+
return uintptr(v)
284+
}
285+
264286
func osinit() {
265287
ncpu = getproccount()
288+
physHugePageSize = getHugePageSize()
266289
}
267290

268291
var urandom_dev = []byte("/dev/urandom\x00")

0 commit comments

Comments
 (0)