Skip to content

Commit

Permalink
docs/reference_guide: add auto-generation of perf event data structur…
Browse files Browse the repository at this point in the history
…e in Python (iovisor#2211)

Follow-up to iovisor#2198.
  • Loading branch information
boat0 authored and yonghong-song committed Feb 16, 2019
1 parent cb2de9a commit 1d1aa3f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions docs/reference_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ while 1:
b.perf_buffer_poll()
```

Note that the data structure transferred will need to be declared in C in the BPF program, and in Python. For example:
Note that the data structure transferred will need to be declared in C in the BPF program. For example:

```C
// define output data structure in C
Expand All @@ -1272,18 +1272,32 @@ struct data_t {
u64 ts;
char comm[TASK_COMM_LEN];
};
BPF_PERF_OUTPUT(events);
[...]
```
In Python, you can either let bcc generate the data structure from C declaration automatically (recommanded):
```Python
def print_event(cpu, data, size):
event = b["events"].event(data)
[...]
```

or define it manually:

```Python
# define output data structure in Python
TASK_COMM_LEN = 16 # linux/sched.h
class Data(ct.Structure):
_fields_ = [("pid", ct.c_ulonglong),
("ts", ct.c_ulonglong),
("comm", ct.c_char * TASK_COMM_LEN)]
```

Perhaps in a future bcc version, the Python data structure will be automatically generated from the C declaration.
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
[...]
```

Examples in situ:
[code](https://github.com/iovisor/bcc/blob/08fbceb7e828f0e3e77688497727c5b2405905fd/examples/tracing/hello_perf_output.py#L59),
Expand Down

0 comments on commit 1d1aa3f

Please sign in to comment.