github.com/m3db/m3@v1.5.0/src/x/pool/object.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package pool
    22  
    23  import (
    24  	"errors"
    25  	"math"
    26  	"sync"
    27  	"sync/atomic"
    28  
    29  	"github.com/m3db/m3/src/x/unsafe"
    30  
    31  	"github.com/uber-go/tally"
    32  	"golang.org/x/sys/cpu"
    33  )
    34  
    35  var (
    36  	errPoolAlreadyInitialized      = errors.New("object pool already initialized")
    37  	errPoolAccessBeforeInitialized = errors.New("object pool accessed before it was initialized")
    38  )
    39  
    40  const sampleObjectPoolLengthEvery = 2048
    41  
    42  type objectPool struct {
    43  	_                   cpu.CacheLinePad
    44  	values              chan interface{}
    45  	_                   cpu.CacheLinePad
    46  	alloc               Allocator
    47  	metrics             objectPoolMetrics
    48  	onPoolAccessErrorFn OnPoolAccessErrorFn
    49  	size                int
    50  	refillLowWatermark  int
    51  	refillHighWatermark int
    52  	filling             int32
    53  	initialized         int32
    54  }
    55  
    56  type objectPoolMetrics struct {
    57  	free       tally.Gauge
    58  	total      tally.Gauge
    59  	getOnEmpty tally.Counter
    60  	putOnFull  tally.Counter
    61  }
    62  
    63  // NewObjectPool creates a new pool
    64  func NewObjectPool(opts ObjectPoolOptions) ObjectPool {
    65  	if opts == nil {
    66  		opts = NewObjectPoolOptions()
    67  	}
    68  
    69  	uninitializedAllocatorFn := func() interface{} {
    70  		fn := opts.OnPoolAccessErrorFn()
    71  		fn(errPoolAccessBeforeInitialized)
    72  		return nil
    73  	}
    74  
    75  	if opts.Dynamic() {
    76  		return &dynamicPool{
    77  			pool: sync.Pool{
    78  				New: uninitializedAllocatorFn,
    79  			},
    80  			onPoolAccessErrorFn: opts.OnPoolAccessErrorFn(),
    81  		}
    82  	}
    83  
    84  	m := opts.InstrumentOptions().MetricsScope()
    85  
    86  	p := &objectPool{
    87  		size: opts.Size(),
    88  		refillLowWatermark: int(math.Ceil(
    89  			opts.RefillLowWatermark() * float64(opts.Size()))),
    90  		refillHighWatermark: int(math.Ceil(
    91  			opts.RefillHighWatermark() * float64(opts.Size()))),
    92  		metrics: objectPoolMetrics{
    93  			free:       m.Gauge("free"),
    94  			total:      m.Gauge("total"),
    95  			getOnEmpty: m.Counter("get-on-empty"),
    96  			putOnFull:  m.Counter("put-on-full"),
    97  		},
    98  		onPoolAccessErrorFn: opts.OnPoolAccessErrorFn(),
    99  		alloc:               uninitializedAllocatorFn,
   100  	}
   101  
   102  	p.setGauges()
   103  
   104  	return p
   105  }
   106  
   107  func (p *objectPool) Init(alloc Allocator) {
   108  	if !atomic.CompareAndSwapInt32(&p.initialized, 0, 1) {
   109  		p.onPoolAccessErrorFn(errPoolAlreadyInitialized)
   110  		return
   111  	}
   112  
   113  	p.values = make(chan interface{}, p.size)
   114  	for i := 0; i < cap(p.values); i++ {
   115  		p.values <- alloc()
   116  	}
   117  
   118  	p.alloc = alloc
   119  	p.setGauges()
   120  }
   121  
   122  func (p *objectPool) Get() interface{} {
   123  	var (
   124  		metrics = p.metrics
   125  		v       interface{}
   126  	)
   127  
   128  	select {
   129  	case v = <-p.values:
   130  	default:
   131  		v = p.alloc()
   132  		metrics.getOnEmpty.Inc(1)
   133  	}
   134  
   135  	if unsafe.Fastrandn(sampleObjectPoolLengthEvery) == 0 {
   136  		// inlined setGauges()
   137  		metrics.free.Update(float64(len(p.values)))
   138  		metrics.total.Update(float64(p.size))
   139  	}
   140  
   141  	if p.refillLowWatermark > 0 && len(p.values) <= p.refillLowWatermark {
   142  		p.tryFill()
   143  	}
   144  
   145  	return v
   146  }
   147  
   148  func (p *objectPool) Put(obj interface{}) {
   149  	if p.values == nil {
   150  		p.onPoolAccessErrorFn(errPoolAccessBeforeInitialized)
   151  		return
   152  	}
   153  	select {
   154  	case p.values <- obj:
   155  	default:
   156  		p.metrics.putOnFull.Inc(1)
   157  	}
   158  }
   159  
   160  func (p *objectPool) setGauges() {
   161  	p.metrics.free.Update(float64(len(p.values)))
   162  	p.metrics.total.Update(float64(p.size))
   163  }
   164  
   165  func (p *objectPool) tryFill() {
   166  	if !atomic.CompareAndSwapInt32(&p.filling, 0, 1) {
   167  		return
   168  	}
   169  
   170  	go func() {
   171  		defer atomic.StoreInt32(&p.filling, 0)
   172  
   173  		for len(p.values) < p.refillHighWatermark {
   174  			select {
   175  			case p.values <- p.alloc():
   176  			default:
   177  				return
   178  			}
   179  		}
   180  	}()
   181  }
   182  
   183  var _ ObjectPool = (*dynamicPool)(nil)
   184  
   185  type dynamicPool struct {
   186  	pool                sync.Pool
   187  	onPoolAccessErrorFn OnPoolAccessErrorFn
   188  	initialized         int32
   189  }
   190  
   191  func (d *dynamicPool) Init(alloc Allocator) {
   192  	if !atomic.CompareAndSwapInt32(&d.initialized, 0, 1) {
   193  		d.onPoolAccessErrorFn(errPoolAlreadyInitialized)
   194  		return
   195  	}
   196  
   197  	d.pool.New = alloc
   198  }
   199  
   200  func (d *dynamicPool) Get() interface{} {
   201  	return d.pool.Get()
   202  }
   203  
   204  func (d *dynamicPool) Put(x interface{}) {
   205  	d.pool.Put(x)
   206  }