X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/67f0d86c20139eee996816d44ef75fa52288c515..f04693da1811e670d4cbb981debeecf14d79137c:/services/keepstore/bufferpool.go diff --git a/services/keepstore/bufferpool.go b/services/keepstore/bufferpool.go index 373bfc75a1..623693cd12 100644 --- a/services/keepstore/bufferpool.go +++ b/services/keepstore/bufferpool.go @@ -1,21 +1,31 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( - "log" "sync" + "sync/atomic" "time" + + "github.com/sirupsen/logrus" ) type bufferPool struct { + log logrus.FieldLogger // limiter has a "true" placeholder for each in-use buffer. limiter chan bool + // allocated is the number of bytes currently allocated to buffers. + allocated uint64 // Pool has unused buffers. sync.Pool } -func newBufferPool(count int, bufSize int) *bufferPool { - p := bufferPool{} - p.New = func() interface{} { +func newBufferPool(log logrus.FieldLogger, count int, bufSize int) *bufferPool { + p := bufferPool{log: log} + p.Pool.New = func() interface{} { + atomic.AddUint64(&p.allocated, uint64(bufSize)) return make([]byte, bufSize) } p.limiter = make(chan bool, count) @@ -27,13 +37,13 @@ func (p *bufferPool) Get(size int) []byte { case p.limiter <- true: default: t0 := time.Now() - log.Printf("reached max buffers (%d), waiting", cap(p.limiter)) + p.log.Printf("reached max buffers (%d), waiting", cap(p.limiter)) p.limiter <- true - log.Printf("waited %v for a buffer", time.Since(t0)) + p.log.Printf("waited %v for a buffer", time.Since(t0)) } buf := p.Pool.Get().([]byte) if cap(buf) < size { - log.Fatalf("bufferPool Get(size=%d) but max=%d", size, cap(buf)) + p.log.Fatalf("bufferPool Get(size=%d) but max=%d", size, cap(buf)) } return buf[:size] } @@ -42,3 +52,18 @@ func (p *bufferPool) Put(buf []byte) { p.Pool.Put(buf) <-p.limiter } + +// Alloc returns the number of bytes allocated to buffers. +func (p *bufferPool) Alloc() uint64 { + return atomic.LoadUint64(&p.allocated) +} + +// Cap returns the maximum number of buffers allowed. +func (p *bufferPool) Cap() int { + return cap(p.limiter) +} + +// Len returns the number of buffers in use right now. +func (p *bufferPool) Len() int { + return len(p.limiter) +}