Skip to content

Commit

Permalink
Move StatsBuffer to be time-based.
Browse files Browse the repository at this point in the history
It now keeps stats for a certain amount of time before expiring them. It
used to keep a certain number of stats instead.
  • Loading branch information
vmarmol committed Apr 22, 2015
1 parent 2ef063d commit d9f8a09
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
16 changes: 8 additions & 8 deletions storage/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type containerStorage struct {
ref info.ContainerReference
recentStats *StatsBuffer
maxNumStats int
maxAge time.Duration
lock sync.RWMutex
}

Expand All @@ -48,18 +48,18 @@ func (self *containerStorage) RecentStats(start, end time.Time, maxStats int) ([
return self.recentStats.InTimeRange(start, end, maxStats), nil
}

func newContainerStore(ref info.ContainerReference, maxNumStats int) *containerStorage {
func newContainerStore(ref info.ContainerReference, maxAge time.Duration) *containerStorage {
return &containerStorage{
ref: ref,
recentStats: NewStatsBuffer(maxNumStats),
maxNumStats: maxNumStats,
recentStats: NewStatsBuffer(maxAge),
maxAge: maxAge,
}
}

type InMemoryStorage struct {
lock sync.RWMutex
containerStorageMap map[string]*containerStorage
maxNumStats int
maxAge time.Duration
backend storage.StorageDriver
}

Expand All @@ -71,7 +71,7 @@ func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.C
self.lock.Lock()
defer self.lock.Unlock()
if cstore, ok = self.containerStorageMap[ref.Name]; !ok {
cstore = newContainerStore(ref, self.maxNumStats)
cstore = newContainerStore(ref, self.maxAge)
self.containerStorageMap[ref.Name] = cstore
}
}()
Expand Down Expand Up @@ -113,12 +113,12 @@ func (self *InMemoryStorage) Close() error {
}

func New(
maxNumStats int,
maxAge time.Duration,
backend storage.StorageDriver,
) *InMemoryStorage {
ret := &InMemoryStorage{
containerStorageMap: make(map[string]*containerStorage, 32),
maxNumStats: maxNumStats,
maxAge: maxAge,
backend: backend,
}
return ret
Expand Down
4 changes: 2 additions & 2 deletions storage/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func getRecentStats(t *testing.T, memoryStorage *InMemoryStorage, numStats int)
}

func TestAddStats(t *testing.T) {
memoryStorage := New(60, nil)
memoryStorage := New(60*time.Second, nil)

assert := assert.New(t)
assert.Nil(memoryStorage.AddStats(containerRef, makeStat(0)))
Expand All @@ -70,7 +70,7 @@ func TestRecentStatsNoRecentStats(t *testing.T) {

// Make an instance of InMemoryStorage with n stats.
func makeWithStats(n int) *InMemoryStorage {
memoryStorage := New(60, nil)
memoryStorage := New(60*time.Second, nil)

for i := 0; i < n; i++ {
memoryStorage.AddStats(containerRef, makeStat(i))
Expand Down
45 changes: 22 additions & 23 deletions storage/memory/stats_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,41 @@ import (
info "github.com/google/cadvisor/info/v1"
)

// A circular buffer for ContainerStats.
// A time-based buffer for ContainerStats. Holds information for a specific time period.
type StatsBuffer struct {
buffer []*info.ContainerStats
size int
index int
age time.Duration
}

// Returns a new thread-compatible StatsBuffer.
func NewStatsBuffer(size int) *StatsBuffer {
func NewStatsBuffer(age time.Duration) *StatsBuffer {
return &StatsBuffer{
buffer: make([]*info.ContainerStats, size),
size: 0,
index: size - 1,
buffer: make([]*info.ContainerStats, 0),
age: age,
}
}

// Adds an element to the start of the buffer (removing one from the end if necessary).
func (self *StatsBuffer) Add(item *info.ContainerStats) {
if self.size < len(self.buffer) {
self.size++
// Remove any elements before the eviction time.
evictTime := item.Timestamp.Add(-self.age)
index := sort.Search(len(self.buffer), func(index int) bool {
return self.buffer[index].Timestamp.After(evictTime)
})
if index < len(self.buffer) {
self.buffer = self.buffer[index:]
}
self.index = (self.index + 1) % len(self.buffer)

copied := *item
self.buffer[self.index] = &copied
self.buffer = append(self.buffer, &copied)
}

// Returns up to maxResult elements in the specified time period (inclusive).
// Results are from first to last. maxResults of -1 means no limit. When first
// and last are specified, maxResults is ignored.
func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*info.ContainerStats {
// No stats, return empty.
if self.size == 0 {
if len(self.buffer) == 0 {
return []*info.ContainerStats{}
}

Expand All @@ -67,12 +70,12 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
var startIndex int
if start.IsZero() {
// None specified, start at the beginning.
startIndex = self.size - 1
startIndex = len(self.buffer) - 1
} else {
// Start is the index before the elements smaller than it. We do this by
// finding the first element smaller than start and taking the index
// before that element
startIndex = sort.Search(self.size, func(index int) bool {
startIndex = sort.Search(len(self.buffer), func(index int) bool {
// buffer[index] < start
return self.Get(index).Timestamp.Before(start)
}) - 1
Expand All @@ -88,12 +91,12 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
endIndex = 0
} else {
// End is the first index smaller than or equal to it (so, not larger).
endIndex = sort.Search(self.size, func(index int) bool {
endIndex = sort.Search(len(self.buffer), func(index int) bool {
// buffer[index] <= t -> !(buffer[index] > t)
return !self.Get(index).Timestamp.After(end)
})
// Check if end is before all the data we have.
if endIndex == self.size {
if endIndex == len(self.buffer) {
return []*info.ContainerStats{}
}
}
Expand All @@ -113,15 +116,11 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
return result
}

// Gets the element at the specified index. Note that elements are stored in LIFO order.
// Gets the element at the specified index. Note that elements are output in LIFO order.
func (self *StatsBuffer) Get(index int) *info.ContainerStats {
calculatedIndex := self.index - index
if calculatedIndex < 0 {
calculatedIndex += len(self.buffer)
}
return self.buffer[calculatedIndex]
return self.buffer[len(self.buffer)-index-1]
}

func (self *StatsBuffer) Size() int {
return self.size
return len(self.buffer)
}
28 changes: 16 additions & 12 deletions storage/memory/stats_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ func expectAllElements(t *testing.T, sb *StatsBuffer, expected []int32) {
expectElements(t, els, expected)
}

func getActualElements(actual []*info.ContainerStats) string {
actualElements := make([]string, len(actual))
for i, element := range actual {
actualElements[i] = strconv.Itoa(int(element.Cpu.LoadAverage))
}
return strings.Join(actualElements, ",")
}

func expectElements(t *testing.T, actual []*info.ContainerStats, expected []int32) {
if len(actual) != len(expected) {
t.Errorf("Expected elements %v, got %v", expected, actual)
t.Errorf("Expected elements %v, got %v", expected, getActualElements(actual))
return
}
for i, el := range actual {
if el.Cpu.LoadAverage != expected[i] {
actualElements := make([]string, len(actual))
for i, element := range actual {
actualElements[i] = strconv.Itoa(int(element.Cpu.LoadAverage))
}
t.Errorf("Expected elements %v, got %v", expected, strings.Join(actualElements, ","))
t.Errorf("Expected elements %v, got %v", expected, getActualElements(actual))
return
}
}
Expand All @@ -77,12 +81,12 @@ func expectElement(t *testing.T, stat *info.ContainerStats, expected int32) {
}

func TestAdd(t *testing.T) {
sb := NewStatsBuffer(5)
sb := NewStatsBuffer(5 * time.Second)

// Add 1.
sb.Add(createStats(1))
sb.Add(createStats(0))
expectSize(t, sb, 1)
expectAllElements(t, sb, []int32{1})
expectAllElements(t, sb, []int32{0})

// Fill the buffer.
for i := 1; i <= 5; i++ {
Expand All @@ -106,7 +110,7 @@ func TestAdd(t *testing.T) {
}

func TestGet(t *testing.T) {
sb := NewStatsBuffer(5)
sb := NewStatsBuffer(5 * time.Second)
sb.Add(createStats(1))
sb.Add(createStats(2))
sb.Add(createStats(3))
Expand All @@ -118,7 +122,7 @@ func TestGet(t *testing.T) {
}

func TestInTimeRange(t *testing.T) {
sb := NewStatsBuffer(5)
sb := NewStatsBuffer(5 * time.Second)
assert := assert.New(t)

var empty time.Time
Expand Down Expand Up @@ -195,7 +199,7 @@ func TestInTimeRange(t *testing.T) {
}

func TestInTimeRangeWithLimit(t *testing.T) {
sb := NewStatsBuffer(5)
sb := NewStatsBuffer(5 * time.Second)
sb.Add(createStats(1))
sb.Add(createStats(2))
sb.Add(createStats(3))
Expand Down
2 changes: 1 addition & 1 deletion storagedriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ func NewMemoryStorage(backendStorageName string) (*memory.InMemoryStorage, error
glog.Infof("No backend storage selected")
}
glog.Infof("Caching %d stats in memory", statsToCache)
storageDriver = memory.New(statsToCache, backendStorage)
storageDriver = memory.New(time.Duration(statsToCache)*time.Second, backendStorage)
return storageDriver, nil
}

0 comments on commit d9f8a09

Please sign in to comment.