cadvisor/perf/collector_libpfm_test.go
Paweł Szulik 5641a0feae Add perf uncore events
Signed-off-by: Paweł Szulik <pawel.szulik@intel.com>
2020-06-23 18:56:28 +02:00

115 lines
3.1 KiB
Go

// +build libpfm,cgo
// Copyright 2020 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.
// Collector of perf events for a container.
package perf
import (
"bytes"
"encoding/binary"
"testing"
"github.com/stretchr/testify/assert"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/stats"
)
type buffer struct {
*bytes.Buffer
}
func (b buffer) Close() error {
return nil
}
func TestCollector_UpdateStats(t *testing.T) {
collector := collector{uncore: &stats.NoopCollector{}}
notScaledBuffer := buffer{bytes.NewBuffer([]byte{})}
scaledBuffer := buffer{bytes.NewBuffer([]byte{})}
err := binary.Write(notScaledBuffer, binary.LittleEndian, ReadFormat{
Value: 123456789,
TimeEnabled: 100,
TimeRunning: 100,
ID: 1,
})
assert.NoError(t, err)
err = binary.Write(scaledBuffer, binary.LittleEndian, ReadFormat{
Value: 333333333,
TimeEnabled: 3,
TimeRunning: 1,
ID: 2,
})
assert.NoError(t, err)
collector.cpuFiles = map[string]map[int]readerCloser{
"instructions": {0: notScaledBuffer},
"cycles": {11: scaledBuffer},
}
stats := &info.ContainerStats{}
err = collector.UpdateStats(stats)
assert.NoError(t, err)
assert.Len(t, stats.PerfStats, 2)
assert.Contains(t, stats.PerfStats, info.PerfStat{
ScalingRatio: 0.3333333333333333,
Value: 999999999,
Name: "cycles",
Cpu: 11,
})
assert.Contains(t, stats.PerfStats, info.PerfStat{
ScalingRatio: 1,
Value: 123456789,
Name: "instructions",
Cpu: 0,
})
}
func TestCreatePerfEventAttr(t *testing.T) {
event := CustomEvent{
Type: 0x1,
Config: Config{uint64(0x2), uint64(0x3), uint64(0x4)},
Name: "fake_event",
}
attributes := createPerfEventAttr(event)
assert.Equal(t, uint32(1), attributes.Type)
assert.Equal(t, uint64(2), attributes.Config)
assert.Equal(t, uint64(3), attributes.Ext1)
assert.Equal(t, uint64(4), attributes.Ext2)
assert.Equal(t, uint64(65536), attributes.Sample_type)
assert.Equal(t, uint64(7), attributes.Read_format)
assert.Equal(t, uint64(1048578), attributes.Bits)
}
func TestNewCollector(t *testing.T) {
perfCollector := newCollector("cgroup", PerfEvents{
Core: Events{
Events: [][]Event{{"event_1"}, {"event_2"}},
CustomEvents: []CustomEvent{{
Type: 0,
Config: []uint64{1, 2, 3},
Name: "event_2",
}},
},
}, 1, []info.Node{})
assert.Len(t, perfCollector.eventToCustomEvent, 1)
assert.Nil(t, perfCollector.eventToCustomEvent[Event("event_1")])
assert.Same(t, &perfCollector.events.Core.CustomEvents[0], perfCollector.eventToCustomEvent[Event("event_2")])
}