cc-metric-store/internal/memstore/allocator.go

116 lines
3.1 KiB
Go
Raw Normal View History

2022-07-13 12:57:37 +02:00
package memstore
2022-07-13 16:36:20 +02:00
import (
2022-07-25 15:16:25 +02:00
"errors"
"log"
2022-07-13 16:36:20 +02:00
"os"
"reflect"
"sync"
"unsafe"
2022-07-13 12:57:37 +02:00
2022-07-13 16:36:20 +02:00
"github.com/ClusterCockpit/cc-metric-store/internal/types"
"golang.org/x/sys/unix"
)
const bufferSizeInFloats int = 512
const bufferSizeInBytes int = bufferSizeInFloats * 8
// The allocator rarely used, so a single big lock should be fine!
var allocatorLock sync.Mutex
var allocatorPool [][]byte
2022-07-25 15:16:25 +02:00
var allocatorBatch []byte
2022-07-13 16:36:20 +02:00
func RequestBytes(size int) []byte {
2022-07-25 14:28:49 +02:00
requested := size
2022-07-13 16:36:20 +02:00
size = (size + bufferSizeInBytes - 1) / bufferSizeInBytes * bufferSizeInBytes
2022-07-25 15:16:25 +02:00
// Check allocation caches:
allocatorLock.Lock()
if len(allocatorPool) > 0 && size == bufferSizeInBytes {
bytes := allocatorPool[len(allocatorPool)-1]
allocatorPool = allocatorPool[:len(allocatorPool)-1]
2022-07-13 16:36:20 +02:00
allocatorLock.Unlock()
2022-07-25 15:16:25 +02:00
return bytes[:requested]
} else if cap(allocatorBatch) > size {
bytes := allocatorBatch[:0:size]
allocatorBatch = allocatorBatch[size:]
allocatorLock.Unlock()
return bytes[:requested]
2022-07-13 16:36:20 +02:00
}
2022-07-25 15:16:25 +02:00
allocatorLock.Unlock()
2022-07-13 16:36:20 +02:00
pagesize := os.Getpagesize()
2022-07-25 14:28:49 +02:00
if size < pagesize || size%pagesize != 0 {
2022-07-13 16:36:20 +02:00
panic("page size and buffer size do not go with each other")
}
2022-07-25 14:28:49 +02:00
bytes, err := unix.Mmap(-1, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANONYMOUS|unix.MAP_SHARED)
2022-07-13 16:36:20 +02:00
if err != nil {
2022-07-25 14:28:49 +02:00
panic("unix.Mmap failed: " + err.Error())
2022-07-13 16:36:20 +02:00
}
2022-07-25 14:28:49 +02:00
if cap(bytes) != size {
panic("whoops?")
}
return bytes[:requested]
2022-07-13 16:36:20 +02:00
}
2022-07-25 15:16:25 +02:00
func FillAllocatorCache(estimate int) error {
size := (estimate + bufferSizeInBytes - 1) / bufferSizeInBytes * bufferSizeInBytes
pagesize := os.Getpagesize()
if size < pagesize || size%pagesize != 0 {
return errors.New("estimate to small of buffer size not page size compatible")
}
allocatorLock.Lock()
defer allocatorLock.Unlock()
if len(allocatorBatch) > 0 {
n := len(allocatorBatch) / bufferSizeInBytes
for i := 0; i < n; i++ {
chunk := allocatorBatch[i*bufferSizeInBytes : i*bufferSizeInBytes+bufferSizeInBytes]
allocatorPool = append(allocatorPool, chunk[0:0:bufferSizeInBytes])
}
allocatorBatch = nil
}
bytes, err := unix.Mmap(-1, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANONYMOUS|unix.MAP_SHARED)
if err != nil {
panic("unix.Mmap failed: " + err.Error())
}
log.Printf("batch-allocated %d bytes", cap(bytes))
allocatorBatch = bytes
return nil
}
2022-07-13 16:36:20 +02:00
func ReleaseBytes(bytes []byte) {
if cap(bytes)%bufferSizeInBytes != 0 {
panic("bytes released that do not match the buffer size constraints")
}
allocatorLock.Lock()
defer allocatorLock.Unlock()
n := cap(bytes) / bufferSizeInBytes
for i := 0; i < n; i++ {
chunk := bytes[i*bufferSizeInBytes : i*bufferSizeInBytes+bufferSizeInBytes]
allocatorPool = append(allocatorPool, chunk[0:0:bufferSizeInBytes])
}
}
func ReleaseFloatSlice(slice []types.Float) {
var x types.Float
sh := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
bytes := unsafe.Slice((*byte)(unsafe.Pointer(sh.Data)), sh.Cap*int(unsafe.Sizeof(x)))
ReleaseBytes(bytes)
}
func RequestFloatSlice(size int) []types.Float {
var x types.Float
bytes := RequestBytes(size * int(unsafe.Sizeof(x)))
sh := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
return unsafe.Slice((*types.Float)(unsafe.Pointer(sh.Data)), size)
2022-07-13 12:57:37 +02:00
}