1 Star 0 Fork 0

zhuchance/kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
buffer.go 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
Rohit Jnagal 提交于 2015-06-12 22:59 . Update cadvisor dependency.
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package summary
import (
info "github.com/google/cadvisor/info/v2"
)
// Manages a buffer of usage samples.
// This is similar to stats buffer in cache/memory.
// The main difference is that we do not pre-allocate the buffer as most containers
// won't live that long.
type SamplesBuffer struct {
// list of collected samples.
samples []info.Usage
// maximum size this buffer can grow to.
maxSize int
// index for the latest sample.
index int
}
// Initializes an empty buffer.
func NewSamplesBuffer(size int) *SamplesBuffer {
return &SamplesBuffer{
index: -1,
maxSize: size,
}
}
// Returns the current number of samples in the buffer.
func (s *SamplesBuffer) Size() int {
return len(s.samples)
}
// Add an element to the buffer. Oldest one is overwritten if required.
func (s *SamplesBuffer) Add(stat info.Usage) {
if len(s.samples) < s.maxSize {
s.samples = append(s.samples, stat)
s.index++
return
}
s.index = (s.index + 1) % s.maxSize
s.samples[s.index] = stat
}
// Returns pointers to the last 'n' stats.
func (s *SamplesBuffer) RecentStats(n int) []*info.Usage {
if n > len(s.samples) {
n = len(s.samples)
}
start := s.index - (n - 1)
if start < 0 {
start += len(s.samples)
}
out := make([]*info.Usage, n)
for i := 0; i < n; i++ {
index := (start + i) % len(s.samples)
out[i] = &s.samples[index]
}
return out
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.0.6

搜索帮助

Cb406eda 1850385 E526c682 1850385