1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
// +build linux
// Copyright 2016 Cilium Project
// Copyright 2016 Sylvain Afchain
// Copyright 2016 Kinvolk
//
// 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 elf
import (
"fmt"
"os"
"sort"
"syscall"
"unsafe"
"github.com/iovisor/gobpf/pkg/cpuonline"
)
/*
#include <sys/types.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <linux/perf_event.h>
#include <poll.h>
// from https://github.com/cilium/cilium/blob/master/pkg/bpf/perf.go
struct event_sample {
struct perf_event_header header;
uint32_t size;
uint8_t data[];
};
struct read_state {
void *buf;
int buf_len;
// These two fields are for backward reading: as opposed to normal ring buffers,
// backward read buffers don't update the read pointer when reading.
// So we keep the state externally here.
uint64_t data_head_initialized;
uint64_t data_head;
uint64_t wrapped;
};
static int perf_event_read(int page_count, int page_size, void *_state,
void *_header, void *_sample_ptr, void *_lost_ptr)
{
volatile struct perf_event_mmap_page *header = _header;
uint64_t data_head = *((volatile uint64_t *) &header->data_head);
uint64_t data_tail = header->data_tail;
uint64_t raw_size = (uint64_t)page_count * page_size;
void *base = ((uint8_t *)header) + page_size;
struct read_state *state = _state;
struct event_sample *e;
void *begin, *end;
void **sample_ptr = (void **) _sample_ptr;
void **lost_ptr = (void **) _lost_ptr;
// No data to read on this ring
__sync_synchronize();
if (data_head == data_tail)
return 0;
begin = base + data_tail % raw_size;
e = begin;
end = base + (data_tail + e->header.size) % raw_size;
if (state->buf_len < e->header.size || !state->buf) {
state->buf = realloc(state->buf, e->header.size);
state->buf_len = e->header.size;
}
if (end < begin) {
uint64_t len = base + raw_size - begin;
memcpy(state->buf, begin, len);
memcpy((char *) state->buf + len, base, e->header.size - len);
e = state->buf;
} else {
memcpy(state->buf, begin, e->header.size);
}
switch (e->header.type) {
case PERF_RECORD_SAMPLE:
*sample_ptr = state->buf;
break;
case PERF_RECORD_LOST:
*lost_ptr = state->buf;
break;
}
__sync_synchronize();
header->data_tail += e->header.size;
return e->header.type;
}
static int perf_event_dump_backward(int page_count, int page_size, void *_state,
void *_header, void *_sample_ptr)
{
volatile struct perf_event_mmap_page *header = _header;
uint64_t data_head = header->data_head;
uint64_t raw_size = (uint64_t)page_count * page_size;
void *base = ((uint8_t *)header) + page_size;
struct read_state *state = _state;
struct perf_event_header *p, *head;
void **sample_ptr = (void **) _sample_ptr;
void *begin, *end;
uint64_t new_head;
if (state->data_head_initialized == 0) {
state->data_head_initialized = 1;
state->data_head = data_head & (raw_size - 1);
}
if ((state->wrapped && state->data_head >= data_head) || state->wrapped > 1) {
return 0;
}
begin = p = base + state->data_head;
if (p->type != PERF_RECORD_SAMPLE)
return 0;
new_head = (state->data_head + p->size) & (raw_size - 1);
end = base + new_head;
if (state->buf_len < p->size || !state->buf) {
state->buf = realloc(state->buf, p->size);
state->buf_len = p->size;
}
if (end < begin) {
uint64_t len = base + raw_size - begin;
memcpy(state->buf, begin, len);
memcpy((char *) state->buf + len, base, p->size - len);
} else {
memcpy(state->buf, begin, p->size);
}
*sample_ptr = state->buf;
if (new_head <= state->data_head) {
state->wrapped++;
}
state->data_head = new_head;
return p->type;
}
*/
import "C"
type PerfMap struct {
name string
program *Module
pageCount int
receiverChan chan []byte
lostChan chan uint64
pollStop chan struct{}
timestamp func(*[]byte) uint64
}
// Matching 'struct perf_event_sample in kernel sources
type PerfEventSample struct {
PerfEventHeader
Size uint32
data byte // Size bytes of data
}
func InitPerfMap(b *Module, mapName string, receiverChan chan []byte, lostChan chan uint64) (*PerfMap, error) {
m, ok := b.maps[mapName]
if !ok {
return nil, fmt.Errorf("no map with name %s", mapName)
}
if receiverChan == nil {
return nil, fmt.Errorf("receiverChan is nil")
}
// Maps are initialized in b.Load(), nothing to do here
return &PerfMap{
name: mapName,
program: b,
pageCount: m.pageCount,
receiverChan: receiverChan,
lostChan: lostChan,
pollStop: make(chan struct{}),
}, nil
}
func (pm *PerfMap) SwapAndDumpBackward() (out [][]byte) {
m, ok := pm.program.maps[pm.name]
if !ok {
// should not happen or only when pm.program is
// suddenly changed
panic(fmt.Sprintf("cannot find map %q", pm.name))
}
// step 1: create a new perf ring buffer
pmuFds, headers, bases, err := createPerfRingBuffer(true, true, pm.pageCount)
if err != nil {
return
}
cpus, err := cpuonline.Get()
if err != nil {
return
}
// step 2: swap file descriptors
// after it the ebpf programs will write to the new map
for index, cpu := range cpus {
// assign perf fd to map
err := pm.program.UpdateElement(m, unsafe.Pointer(&cpu), unsafe.Pointer(&pmuFds[index]), 0)
if err != nil {
return
}
}
// step 3: dump old buffer
out = pm.DumpBackward()
// step4: close old buffer
// unmap
for _, base := range m.bases {
err := syscall.Munmap(base)
if err != nil {
return
}
}
for _, fd := range m.pmuFDs {
// disable
_, _, err2 := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), C.PERF_EVENT_IOC_DISABLE, 0)
if err2 != 0 {
return
}
// close
if err := syscall.Close(int(fd)); err != nil {
return
}
}
// update file descriptors to new perf ring buffer
m.pmuFDs = pmuFds
m.headers = headers
m.bases = bases
return
}
func (pm *PerfMap) DumpBackward() (out [][]byte) {
incoming := OrderedBytesArray{timestamp: pm.timestamp}
m, ok := pm.program.maps[pm.name]
if !ok {
// should not happen or only when pm.program is
// suddenly changed
panic(fmt.Sprintf("cannot find map %q", pm.name))
}
cpuCount := len(m.pmuFDs)
pageSize := os.Getpagesize()
for cpu := 0; cpu < cpuCount; cpu++ {
state := C.struct_read_state{}
ringBufferLoop:
for {
var sample *PerfEventSample
ok := C.perf_event_dump_backward(C.int(pm.pageCount), C.int(pageSize),
unsafe.Pointer(&state), unsafe.Pointer(m.headers[cpu]),
unsafe.Pointer(&sample))
switch ok {
case 0:
break ringBufferLoop // nothing to read
case C.PERF_RECORD_SAMPLE:
size := sample.Size - 4
b := C.GoBytes(unsafe.Pointer(&sample.data), C.int(size))
incoming.bytesArray = append(incoming.bytesArray, b)
}
}
}
if incoming.timestamp != nil {
sort.Sort(incoming)
}
return incoming.bytesArray
}
// SetTimestampFunc registers a timestamp callback that will be used to
// reorder the perf events chronologically.
//
// If not set, the order of events sent through receiverChan is not guaranteed.
//
// Typically, the ebpf program will use bpf_ktime_get_ns() to get a timestamp
// and store it in the perf event. The perf event struct is opaque to this
// package, hence the need for a callback.
func (pm *PerfMap) SetTimestampFunc(timestamp func(*[]byte) uint64) {
pm.timestamp = timestamp
}
func (pm *PerfMap) PollStart() {
incoming := OrderedBytesArray{timestamp: pm.timestamp}
m, ok := pm.program.maps[pm.name]
if !ok {
// should not happen or only when pm.program is
// suddenly changed
panic(fmt.Sprintf("cannot find map %q", pm.name))
}
go func() {
cpuCount := len(m.pmuFDs)
pageSize := os.Getpagesize()
state := C.struct_read_state{}
defer func() {
close(pm.receiverChan)
if pm.lostChan != nil {
close(pm.lostChan)
}
}()
for {
select {
case <-pm.pollStop:
break
default:
perfEventPoll(m.pmuFDs)
}
harvestLoop:
for {
select {
case <-pm.pollStop:
return
default:
}
var harvestCount C.int
beforeHarvest := NowNanoseconds()
for cpu := 0; cpu < cpuCount; cpu++ {
ringBufferLoop:
for {
var sample *PerfEventSample
var lost *PerfEventLost
ok := C.perf_event_read(C.int(pm.pageCount), C.int(pageSize),
unsafe.Pointer(&state), unsafe.Pointer(m.headers[cpu]),
unsafe.Pointer(&sample), unsafe.Pointer(&lost))
switch ok {
case 0:
break ringBufferLoop // nothing to read
case C.PERF_RECORD_SAMPLE:
size := sample.Size - 4
b := C.GoBytes(unsafe.Pointer(&sample.data), C.int(size))
incoming.bytesArray = append(incoming.bytesArray, b)
harvestCount++
if pm.timestamp == nil {
continue ringBufferLoop
}
if incoming.timestamp(&b) > beforeHarvest {
// see comment below
break ringBufferLoop
}
case C.PERF_RECORD_LOST:
if pm.lostChan != nil {
select {
case pm.lostChan <- lost.Lost:
case <-pm.pollStop:
return
}
}
default:
// ignore unknown events
}
}
}
if incoming.timestamp != nil {
sort.Sort(incoming)
}
for incoming.Len() > 0 {
if incoming.timestamp != nil && incoming.timestamp(&incoming.bytesArray[0]) > beforeHarvest {
// This record has been sent after the beginning of the harvest. Stop
// processing here to keep the order. "incoming" is sorted, so the next
// elements also must not be processed now.
break harvestLoop
}
select {
case pm.receiverChan <- incoming.bytesArray[0]:
case <-pm.pollStop:
return
}
// remove first element
incoming.bytesArray = incoming.bytesArray[1:]
}
if harvestCount == 0 && len(incoming.bytesArray) == 0 {
break harvestLoop
}
}
}
}()
}
// PollStop stops the goroutine that polls the perf event map.
// Callers must not close receiverChan or lostChan: they will be automatically
// closed on the sender side.
func (pm *PerfMap) PollStop() {
close(pm.pollStop)
}
func perfEventPoll(fds []C.int) error {
var pfds []C.struct_pollfd
for i, _ := range fds {
var pfd C.struct_pollfd
pfd.fd = fds[i]
pfd.events = C.POLLIN
pfds = append(pfds, pfd)
}
_, err := C.poll(&pfds[0], C.nfds_t(len(fds)), 500)
if err != nil {
return fmt.Errorf("error polling: %v", err.(syscall.Errno))
}
return nil
}
// Assume the timestamp is at the beginning of the user struct
type OrderedBytesArray struct {
bytesArray [][]byte
timestamp func(*[]byte) uint64
}
func (a OrderedBytesArray) Len() int {
return len(a.bytesArray)
}
func (a OrderedBytesArray) Swap(i, j int) {
a.bytesArray[i], a.bytesArray[j] = a.bytesArray[j], a.bytesArray[i]
}
func (a OrderedBytesArray) Less(i, j int) bool {
return a.timestamp(&a.bytesArray[i]) < a.timestamp(&a.bytesArray[j])
}
// Matching 'struct perf_event_header in <linux/perf_event.h>
type PerfEventHeader struct {
Type uint32
Misc uint16
TotalSize uint16
}
// Matching 'struct perf_event_lost in kernel sources
type PerfEventLost struct {
PerfEventHeader
Id uint64
Lost uint64
}
// NowNanoseconds returns a time that can be compared to bpf_ktime_get_ns()
func NowNanoseconds() uint64 {
var ts syscall.Timespec
syscall.Syscall(syscall.SYS_CLOCK_GETTIME, 1 /* CLOCK_MONOTONIC */, uintptr(unsafe.Pointer(&ts)), 0)
sec, nsec := ts.Unix()
return 1000*1000*1000*uint64(sec) + uint64(nsec)
}
|