Merge branch '8784-dir-listings'
[arvados.git] / services / keepstore / bufferpool.go
index 0b216d76bff893fa30379a4003963431a9d309e9..91417fd52c37a23f940bb78a430cee56a9808c36 100644 (file)
@@ -1,14 +1,22 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
-       "log"
        "sync"
+       "sync/atomic"
        "time"
+
+       log "github.com/Sirupsen/logrus"
 )
 
 type bufferPool struct {
        // 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
 }
@@ -16,6 +24,7 @@ type bufferPool struct {
 func newBufferPool(count int, bufSize int) *bufferPool {
        p := bufferPool{}
        p.New = func() interface{} {
+               atomic.AddUint64(&p.allocated, uint64(bufSize))
                return make([]byte, bufSize)
        }
        p.limiter = make(chan bool, count)
@@ -39,8 +48,21 @@ func (p *bufferPool) Get(size int) []byte {
 }
 
 func (p *bufferPool) Put(buf []byte) {
-       if buf != nil {
-               p.Pool.Put(buf)
-               <-p.limiter
-       }
+       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)
 }