Skip to content

Commit

Permalink
Merge pull request #1 from adriansr/feature_kprobes
Browse files Browse the repository at this point in the history
Adapt perf package to our needs
  • Loading branch information
adriansr authored Aug 22, 2019
2 parents 9032b11 + 85d060a commit 9bc9b58
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 31 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
----

This is a clone of the `golang.org/x/sys/unix/linux/perf` submitted by
[acln0](https://github.com/acln0) to review at
https://go-review.googlesource.com/c/sys/+/168059

An alternative working tree for this package can also be found
at https://github.com/acln0/perf

This Elastic fork contains bugfixes and features necessary for
our KProbes implementation.

----

`perf` API client package for Linux. See `man 2 perf_event_open` and
`include/uapi/linux/perf_event.h`.

Expand Down
2 changes: 2 additions & 0 deletions count.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux

package perf

import (
Expand Down
2 changes: 2 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux

package perf

import (
Expand Down
21 changes: 18 additions & 3 deletions perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux

package perf

import (
Expand All @@ -12,6 +14,7 @@ import (
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -92,6 +95,11 @@ type Event struct {
// pollresp receives responses from the poll goroutine associated
// with the ring, back to ReadRawRecord.
pollresp chan pollresp

// recordBuffer is used as storage for records returned by ReadRecord
// and ReadRawRecord. This means memory for records returned from those
// methods will be overwritten by successive calls.
recordBuffer []byte
}

// Open opens the event configured by attr.
Expand Down Expand Up @@ -121,6 +129,12 @@ func Open(a *Attr, pid, cpu int, group *Event) (*Event, error) {
return open(a, pid, cpu, group, 0)
}

// OpenWithFlags is like Open but allows to specify additional flags to be
// passed to perf_event_open(2).
func OpenWithFlags(a *Attr, pid, cpu int, group *Event, flags int) (*Event, error) {
return open(a, pid, cpu, group, flags)
}

// OpenCGroup is like Open, but activates per-container system-wide
// monitoring. If cgroupfs is mounted on /dev/cgroup, and the group to
// monitor is called "test", then cgroupfd must be a file descriptor opened
Expand Down Expand Up @@ -233,7 +247,8 @@ func (ev *Event) MapRingNumPages(num int) error {
return nil
}

size := (1 + num) * unix.Getpagesize()
pgSize := unix.Getpagesize()
size := (1 + num) * pgSize
const prot = unix.PROT_READ | unix.PROT_WRITE
const flags = unix.MAP_SHARED
ring, err := unix.Mmap(ev.perffd, 0, size, prot, flags)
Expand All @@ -248,8 +263,8 @@ func (ev *Event) MapRingNumPages(num int) error {
// observed to do this. Try to detect this condition, and adjust
// the values accordingly.
if meta.Data_offset == 0 && meta.Data_size == 0 {
meta.Data_offset = uint64(unix.Getpagesize())
meta.Data_size = uint64(num) * meta.Data_offset
atomic.StoreUint64(&meta.Data_offset, uint64(pgSize))
atomic.StoreUint64(&meta.Data_size, uint64(num*pgSize))
}

ringdata := ring[meta.Data_offset:]
Expand Down
2 changes: 2 additions & 0 deletions perf_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux

package perf_test

import (
Expand Down
2 changes: 2 additions & 0 deletions perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux

package perf_test

import (
Expand Down
Loading

0 comments on commit 9bc9b58

Please sign in to comment.