Skip to content

Commit

Permalink
Make exclude guest flag from perf event attrs configurable.
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Szulik <pawel.szulik@intel.com>
  • Loading branch information
Paweł Szulik committed Aug 18, 2020
1 parent a6e4fcb commit 4821bbf
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 39 deletions.
23 changes: 6 additions & 17 deletions docs/runtime_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ measure them in transactional manner: all the events in a group must be accounte
in mind that it is impossible to group more events that there are counters available.
* `uncore events` - events which can be counted by PMUs outside core.
* `PMU` - Performance Monitoring Unit
* `exclude_guest` - parameter from ``man perf_event_open``.

#### Getting config values
Using perf tools:
Expand Down Expand Up @@ -193,21 +194,8 @@ perf_event_attr:
"config": [
"0x304"
],
"name": "EVENT_NAME"
}
]
},
"uncore": {
"events": [
["EVENT_NAME"]
],
"custom_events": [
{
"type": 18,
"config": [
"0x304"
],
"name": "EVENT_NAME"
"name": "EVENT_NAME",
"exclude_guest": true
}
]
}
Expand Down Expand Up @@ -237,7 +225,8 @@ Let's explain this by example:
"config": [
"0x304"
],
"name": "uncore_imc_0/cas_count_write"
"name": "uncore_imc_0/cas_count_write",
"exclude_guest": true
},
{
"type": 19,
Expand All @@ -255,7 +244,7 @@ Let's explain this by example:
it would be counted by **all** Integrated Memory Controller PMUs with config provided from libpfm package.
(using this function: https://man7.org/linux/man-pages/man3/pfm_get_os_event_encoding.3.html)

- `uncore_imc_0/cas_count_write` - because of `uncore_imc_0` type and entry in custom events it would be counted by `uncore_imc_0` PMU with provided config.
- `uncore_imc_0/cas_count_write` - because of `uncore_imc_0` type and entry in custom events it would be counted by `uncore_imc_0` PMU with provided config and exclude_guest param.

- `uncore_imc_1/cas_count_all` - because of entry in custom events with type field, event would be counted by PMU with **19** type and provided config.

Expand Down
6 changes: 5 additions & 1 deletion perf/collector_libpfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func createPerfEventAttr(event CustomEvent) *unix.PerfEventAttr {
config.Ext2 = event.Config[2]
}

if event.ExcludeGuest {
config.Bits |= unix.PerfBitExcludeGuest
}

setAttributes(config)
klog.V(5).Infof("perf_event_attr struct prepared: %#v", config)
return config
Expand Down Expand Up @@ -266,7 +270,7 @@ func getPerfEventAttr(name string) (*unix.PerfEventAttr, error) {
func setAttributes(config *unix.PerfEventAttr) {
config.Sample_type = perfSampleIdentifier
config.Read_format = unix.PERF_FORMAT_TOTAL_TIME_ENABLED | unix.PERF_FORMAT_TOTAL_TIME_RUNNING | unix.PERF_FORMAT_ID
config.Bits = perfAttrBitsInherit | perfAttrBitsExcludeGuest
config.Bits |= perfAttrBitsInherit
config.Size = uint32(unsafe.Sizeof(unix.PerfEventAttr{}))
}

Expand Down
65 changes: 51 additions & 14 deletions perf/collector_libpfm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/binary"
"testing"

"golang.org/x/sys/unix"

"github.com/stretchr/testify/assert"

info "github.com/google/cadvisor/info/v1"
Expand Down Expand Up @@ -83,22 +85,57 @@ func TestCollector_UpdateStats(t *testing.T) {
})
}

var createPerfEventAttrCases = []struct {
event CustomEvent
perfEventAttr unix.PerfEventAttr
}{
{
CustomEvent{
Type: 0x1,
Config: Config{0x2, 0x3, 0x4},
Name: "fake_event",
ExcludeGuest: false,
},
unix.PerfEventAttr{
Type: 0x1,
Config: 0x2,
Ext1: 0x3,
Ext2: 0x4,
Sample_type: 0x10000,
Read_format: 0x7,
Bits: 0x2,
},
},
{
CustomEvent{
Type: 0x4,
Config: Config{0x5, 0x1, 0x4},
Name: "another_fake_event",
ExcludeGuest: true,
},
unix.PerfEventAttr{
Type: 0x4,
Config: 0x5,
Ext1: 0x1,
Ext2: 0x4,
Sample_type: 0x10000,
Read_format: 0x7,
Bits: 0x100002,
},
},
}

func TestCreatePerfEventAttr(t *testing.T) {
event := CustomEvent{
Type: 0x1,
Config: Config{uint64(0x2), uint64(0x3), uint64(0x4)},
Name: "fake_event",
for _, testCase := range createPerfEventAttrCases {
attributes := createPerfEventAttr(testCase.event)
assert.Equal(t, testCase.perfEventAttr.Type, attributes.Type)
assert.Equal(t, testCase.perfEventAttr.Config, attributes.Config)
assert.Equal(t, testCase.perfEventAttr.Ext1, attributes.Ext1)
assert.Equal(t, testCase.perfEventAttr.Ext2, attributes.Ext2)
assert.Equal(t, testCase.perfEventAttr.Sample_type, attributes.Sample_type)
assert.Equal(t, testCase.perfEventAttr.Read_format, attributes.Read_format)
assert.Equal(t, testCase.perfEventAttr.Bits, attributes.Bits)
}

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) {
Expand Down
3 changes: 3 additions & 0 deletions perf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type CustomEvent struct {

// Human readable name of metric that will be created from the event.
Name Event `json:"name"`

// See exclude_guest parameter documentation at man perf_event_open.
ExcludeGuest bool `json:"exclude_guest,omitempty"`
}

type Config []uint64
Expand Down
1 change: 1 addition & 0 deletions perf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestConfigParsing(t *testing.T) {
assert.Equal(t, Config{0x5300c0}, events.Core.CustomEvents[0].Config)
assert.Equal(t, uint32(0x04), events.Core.CustomEvents[0].Type)
assert.Equal(t, Event("instructions_retired"), events.Core.CustomEvents[0].Name)
assert.Equal(t, true, events.Core.CustomEvents[0].ExcludeGuest)

assert.Len(t, events.Uncore.Events, 3)
assert.Equal(t, Event("cas_count_write"), events.Uncore.Events[0][0])
Expand Down
6 changes: 4 additions & 2 deletions perf/testing/perf.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"config": [
"0x5300c0"
],
"name": "instructions_retired"
"name": "instructions_retired",
"exclude_guest": true
}
]
},
Expand All @@ -26,7 +27,8 @@
"config": [
"0x5300"
],
"name": "cas_count_write"
"name": "cas_count_write",
"exclude_guest": false
}
]
}
Expand Down
7 changes: 4 additions & 3 deletions perf/uncore_libpfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,10 @@ func (c *uncoreCollector) setupRawNonGroupedUncore(event *CustomEvent, pmus []pm
// PMU isn't set. Register event for all PMUs.
for _, pmu := range pmus {
newEvent := CustomEvent{
Type: pmu.typeOf,
Config: event.Config,
Name: event.Name,
Type: pmu.typeOf,
Config: event.Config,
Name: event.Name,
ExcludeGuest: event.ExcludeGuest,
}
config := createPerfEventAttr(newEvent)
err := c.registerUncoreEvent(config, string(newEvent.Name), pmu.cpus, pmu.name)
Expand Down
4 changes: 2 additions & 2 deletions perf/uncore_libpfm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func TestUncoreCollectorSetup(t *testing.T) {
{"uncore_imc/cas_count_write"},
},
CustomEvents: []CustomEvent{
{18, Config{0x01, 0x02}, "uncore_imc_0/cas_count_read"},
{0, Config{0x01, 0x03}, "uncore_imc/cas_count_write"},
{18, Config{0x01, 0x02}, "uncore_imc_0/cas_count_read", false},
{0, Config{0x01, 0x03}, "uncore_imc/cas_count_write", false},
},
},
}
Expand Down

0 comments on commit 4821bbf

Please sign in to comment.