Blame view

tools/perf/util/python.c 34.7 KB
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1
2
3
4
  #include <Python.h>
  #include <structmember.h>
  #include <inttypes.h>
  #include <poll.h>
1075fbb22   Jiri Olsa   perf python: Add ...
5
  #include <linux/err.h>
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
6
  #include "evlist.h"
56e2e0564   Arnaldo Carvalho de Melo   perf callchain: M...
7
  #include "callchain.h"
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
8
9
10
  #include "evsel.h"
  #include "event.h"
  #include "cpumap.h"
fea013928   Arnaldo Carvalho de Melo   perf tools: Move ...
11
  #include "print_binary.h"
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
12
  #include "thread_map.h"
8afb4c018   Adrian Hunter   perf tools: Re-im...
13
  /*
56e2e0564   Arnaldo Carvalho de Melo   perf callchain: M...
14
15
16
17
18
19
20
21
22
23
24
25
   * Provide these two so that we don't have to link against callchain.c and
   * start dragging hist.c, etc.
   */
  struct callchain_param callchain_param;
  
  int parse_callchain_record(const char *arg __maybe_unused,
  			   struct callchain_param *param __maybe_unused)
  {
  	return 0;
  }
  
  /*
8afb4c018   Adrian Hunter   perf tools: Re-im...
26
27
28
29
   * Support debug printing even though util/debug.c is not linked.  That means
   * implementing 'verbose' and 'eprintf'.
   */
  int verbose;
c95688aac   Jiri Olsa   perf tools: Facto...
30
  int eprintf(int level, int var, const char *fmt, ...)
8afb4c018   Adrian Hunter   perf tools: Re-im...
31
32
33
  {
  	va_list args;
  	int ret = 0;
c95688aac   Jiri Olsa   perf tools: Facto...
34
  	if (var >= level) {
8afb4c018   Adrian Hunter   perf tools: Re-im...
35
36
37
38
39
40
41
  		va_start(args, fmt);
  		ret = vfprintf(stderr, fmt, args);
  		va_end(args);
  	}
  
  	return ret;
  }
cfff2d909   Frederic Weisbecker   perf: Fix undefin...
42
43
44
45
  /* Define PyVarObject_HEAD_INIT for python 2.5 */
  #ifndef PyVarObject_HEAD_INIT
  # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
  #endif
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
46
  PyMODINIT_FUNC initperf(void);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
47
48
49
50
51
52
53
54
55
56
57
58
  #define member_def(type, member, ptype, help) \
  	{ #member, ptype, \
  	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
  	  0, help }
  
  #define sample_member_def(name, member, ptype, help) \
  	{ #name, ptype, \
  	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
  	  0, help }
  
  struct pyrf_event {
  	PyObject_HEAD
377f698db   Jiri Olsa   perf python: Add ...
59
  	struct perf_evsel *evsel;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
60
61
62
  	struct perf_sample sample;
  	union perf_event   event;
  };
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
63
  #define sample_members \
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
64
  	sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"),			 \
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
65
66
  	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
  	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
67
68
69
70
71
  	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
  	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
  	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
  	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
  	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
72
73
74
75
76
77
78
  	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
  
  static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
  
  static PyMemberDef pyrf_mmap_event__members[] = {
  	sample_members
  	member_def(perf_event_header, type, T_UINT, "event type"),
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
79
  	member_def(perf_event_header, misc, T_UINT, "event misc"),
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
80
81
  	member_def(mmap_event, pid, T_UINT, "event pid"),
  	member_def(mmap_event, tid, T_UINT, "event tid"),
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
82
83
84
  	member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
  	member_def(mmap_event, len, T_ULONGLONG, "map length"),
  	member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
85
  	member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
86
  	{ .name = NULL, },
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
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
  };
  
  static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
  {
  	PyObject *ret;
  	char *s;
  
  	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
  			 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
  			 "filename: %s }",
  		     pevent->event.mmap.pid, pevent->event.mmap.tid,
  		     pevent->event.mmap.start, pevent->event.mmap.len,
  		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
  		ret = PyErr_NoMemory();
  	} else {
  		ret = PyString_FromString(s);
  		free(s);
  	}
  	return ret;
  }
  
  static PyTypeObject pyrf_mmap_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.mmap_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_mmap_event__doc,
  	.tp_members	= pyrf_mmap_event__members,
  	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
  };
  
  static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
  
  static PyMemberDef pyrf_task_event__members[] = {
  	sample_members
  	member_def(perf_event_header, type, T_UINT, "event type"),
  	member_def(fork_event, pid, T_UINT, "event pid"),
  	member_def(fork_event, ppid, T_UINT, "event ppid"),
  	member_def(fork_event, tid, T_UINT, "event tid"),
  	member_def(fork_event, ptid, T_UINT, "event ptid"),
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
127
128
  	member_def(fork_event, time, T_ULONGLONG, "timestamp"),
  	{ .name = NULL, },
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
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
  };
  
  static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
  {
  	return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
  				   "ptid: %u, time: %" PRIu64 "}",
  				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
  				   pevent->event.fork.pid,
  				   pevent->event.fork.ppid,
  				   pevent->event.fork.tid,
  				   pevent->event.fork.ptid,
  				   pevent->event.fork.time);
  }
  
  static PyTypeObject pyrf_task_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.task_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_task_event__doc,
  	.tp_members	= pyrf_task_event__members,
  	.tp_repr	= (reprfunc)pyrf_task_event__repr,
  };
  
  static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
  
  static PyMemberDef pyrf_comm_event__members[] = {
  	sample_members
  	member_def(perf_event_header, type, T_UINT, "event type"),
  	member_def(comm_event, pid, T_UINT, "event pid"),
  	member_def(comm_event, tid, T_UINT, "event tid"),
  	member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
161
  	{ .name = NULL, },
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
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
  };
  
  static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
  {
  	return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
  				   pevent->event.comm.pid,
  				   pevent->event.comm.tid,
  				   pevent->event.comm.comm);
  }
  
  static PyTypeObject pyrf_comm_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.comm_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_comm_event__doc,
  	.tp_members	= pyrf_comm_event__members,
  	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
  };
  
  static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
  
  static PyMemberDef pyrf_throttle_event__members[] = {
  	sample_members
  	member_def(perf_event_header, type, T_UINT, "event type"),
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
187
188
189
190
  	member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
  	member_def(throttle_event, id, T_ULONGLONG, "event id"),
  	member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
  	{ .name = NULL, },
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  };
  
  static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
  {
  	struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
  
  	return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
  				   ", stream_id: %" PRIu64 " }",
  				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
  				   te->time, te->id, te->stream_id);
  }
  
  static PyTypeObject pyrf_throttle_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.throttle_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_throttle_event__doc,
  	.tp_members	= pyrf_throttle_event__members,
  	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
  };
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
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
  static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
  
  static PyMemberDef pyrf_lost_event__members[] = {
  	sample_members
  	member_def(lost_event, id, T_ULONGLONG, "event id"),
  	member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
  	{ .name = NULL, },
  };
  
  static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
  {
  	PyObject *ret;
  	char *s;
  
  	if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
  			 "lost: %#" PRIx64 " }",
  		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
  		ret = PyErr_NoMemory();
  	} else {
  		ret = PyString_FromString(s);
  		free(s);
  	}
  	return ret;
  }
  
  static PyTypeObject pyrf_lost_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.lost_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_lost_event__doc,
  	.tp_members	= pyrf_lost_event__members,
  	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
  };
  
  static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
  
  static PyMemberDef pyrf_read_event__members[] = {
  	sample_members
  	member_def(read_event, pid, T_UINT, "event pid"),
  	member_def(read_event, tid, T_UINT, "event tid"),
  	{ .name = NULL, },
  };
  
  static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
  {
  	return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
  				   pevent->event.read.pid,
  				   pevent->event.read.tid);
  	/*
   	 * FIXME: return the array of read values,
   	 * making this method useful ;-)
   	 */
  }
  
  static PyTypeObject pyrf_read_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.read_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_read_event__doc,
  	.tp_members	= pyrf_read_event__members,
  	.tp_repr	= (reprfunc)pyrf_read_event__repr,
  };
  
  static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
  
  static PyMemberDef pyrf_sample_event__members[] = {
  	sample_members
  	member_def(perf_event_header, type, T_UINT, "event type"),
  	{ .name = NULL, },
  };
  
  static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
  {
  	PyObject *ret;
  	char *s;
  
  	if (asprintf(&s, "{ type: sample }") < 0) {
  		ret = PyErr_NoMemory();
  	} else {
  		ret = PyString_FromString(s);
  		free(s);
  	}
  	return ret;
  }
bae57e382   Jiri Olsa   perf python: Add ...
298
299
300
301
  static bool is_tracepoint(struct pyrf_event *pevent)
  {
  	return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
  }
bae57e382   Jiri Olsa   perf python: Add ...
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
  static PyObject*
  tracepoint_field(struct pyrf_event *pe, struct format_field *field)
  {
  	struct pevent *pevent = field->event->pevent;
  	void *data = pe->sample.raw_data;
  	PyObject *ret = NULL;
  	unsigned long long val;
  	unsigned int offset, len;
  
  	if (field->flags & FIELD_IS_ARRAY) {
  		offset = field->offset;
  		len    = field->size;
  		if (field->flags & FIELD_IS_DYNAMIC) {
  			val     = pevent_read_number(pevent, data + offset, len);
  			offset  = val;
  			len     = offset >> 16;
  			offset &= 0xffff;
  		}
  		if (field->flags & FIELD_IS_STRING &&
  		    is_printable_array(data + offset, len)) {
  			ret = PyString_FromString((char *)data + offset);
  		} else {
  			ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
  			field->flags &= ~FIELD_IS_STRING;
  		}
  	} else {
  		val = pevent_read_number(pevent, data + field->offset,
  					 field->size);
  		if (field->flags & FIELD_IS_POINTER)
  			ret = PyLong_FromUnsignedLong((unsigned long) val);
  		else if (field->flags & FIELD_IS_SIGNED)
  			ret = PyLong_FromLong((long) val);
  		else
  			ret = PyLong_FromUnsignedLong((unsigned long) val);
  	}
  
  	return ret;
  }
  
  static PyObject*
  get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
  {
  	const char *str = PyString_AsString(PyObject_Str(attr_name));
  	struct perf_evsel *evsel = pevent->evsel;
  	struct format_field *field;
  
  	if (!evsel->tp_format) {
  		struct event_format *tp_format;
  
  		tp_format = trace_event__tp_format_id(evsel->attr.config);
  		if (!tp_format)
  			return NULL;
  
  		evsel->tp_format = tp_format;
  	}
  
  	field = pevent_find_any_field(evsel->tp_format, str);
  	if (!field)
  		return NULL;
  
  	return tracepoint_field(pevent, field);
  }
  
  static PyObject*
  pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
  {
  	PyObject *obj = NULL;
  
  	if (is_tracepoint(pevent))
  		obj = get_tracepoint_field(pevent, attr_name);
  
  	return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
  }
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
375
376
377
378
379
380
381
382
  static PyTypeObject pyrf_sample_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.sample_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_sample_event__doc,
  	.tp_members	= pyrf_sample_event__members,
  	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
bae57e382   Jiri Olsa   perf python: Add ...
383
  	.tp_getattro	= (getattrofunc) pyrf_sample_event__getattro,
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
384
  };
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
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
  static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
  
  static PyMemberDef pyrf_context_switch_event__members[] = {
  	sample_members
  	member_def(perf_event_header, type, T_UINT, "event type"),
  	member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
  	member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
  	{ .name = NULL, },
  };
  
  static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
  {
  	PyObject *ret;
  	char *s;
  
  	if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
  		     pevent->event.context_switch.next_prev_pid,
  		     pevent->event.context_switch.next_prev_tid,
  		     !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
  		ret = PyErr_NoMemory();
  	} else {
  		ret = PyString_FromString(s);
  		free(s);
  	}
  	return ret;
  }
  
  static PyTypeObject pyrf_context_switch_event__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.context_switch_event",
  	.tp_basicsize	= sizeof(struct pyrf_event),
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_context_switch_event__doc,
  	.tp_members	= pyrf_context_switch_event__members,
  	.tp_repr	= (reprfunc)pyrf_context_switch_event__repr,
  };
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
421
422
423
424
425
426
  static int pyrf_event__setup_types(void)
  {
  	int err;
  	pyrf_mmap_event__type.tp_new =
  	pyrf_task_event__type.tp_new =
  	pyrf_comm_event__type.tp_new =
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
427
428
429
  	pyrf_lost_event__type.tp_new =
  	pyrf_read_event__type.tp_new =
  	pyrf_sample_event__type.tp_new =
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
430
  	pyrf_context_switch_event__type.tp_new =
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
431
432
433
434
  	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
  	err = PyType_Ready(&pyrf_mmap_event__type);
  	if (err < 0)
  		goto out;
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
435
436
437
  	err = PyType_Ready(&pyrf_lost_event__type);
  	if (err < 0)
  		goto out;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
438
439
440
441
442
443
444
445
446
  	err = PyType_Ready(&pyrf_task_event__type);
  	if (err < 0)
  		goto out;
  	err = PyType_Ready(&pyrf_comm_event__type);
  	if (err < 0)
  		goto out;
  	err = PyType_Ready(&pyrf_throttle_event__type);
  	if (err < 0)
  		goto out;
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
447
448
449
450
451
452
  	err = PyType_Ready(&pyrf_read_event__type);
  	if (err < 0)
  		goto out;
  	err = PyType_Ready(&pyrf_sample_event__type);
  	if (err < 0)
  		goto out;
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
453
454
455
  	err = PyType_Ready(&pyrf_context_switch_event__type);
  	if (err < 0)
  		goto out;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
456
457
458
459
460
461
  out:
  	return err;
  }
  
  static PyTypeObject *pyrf_event__type[] = {
  	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
462
  	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
463
464
465
466
467
  	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
  	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
  	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
  	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
  	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
3e9f45a7a   Arnaldo Carvalho de Melo   perf python: Add ...
468
469
  	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
  	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
470
471
  	[PERF_RECORD_SWITCH]	 = &pyrf_context_switch_event__type,
  	[PERF_RECORD_SWITCH_CPU_WIDE]  = &pyrf_context_switch_event__type,
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
472
473
474
475
476
477
  };
  
  static PyObject *pyrf_event__new(union perf_event *event)
  {
  	struct pyrf_event *pevent;
  	PyTypeObject *ptype;
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
478
479
480
481
  	if ((event->header.type < PERF_RECORD_MMAP ||
  	     event->header.type > PERF_RECORD_SAMPLE) &&
  	    !(event->header.type == PERF_RECORD_SWITCH ||
  	      event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
  		return NULL;
  
  	ptype = pyrf_event__type[event->header.type];
  	pevent = PyObject_New(struct pyrf_event, ptype);
  	if (pevent != NULL)
  		memcpy(&pevent->event, event, event->header.size);
  	return (PyObject *)pevent;
  }
  
  struct pyrf_cpu_map {
  	PyObject_HEAD
  
  	struct cpu_map *cpus;
  };
  
  static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
  			      PyObject *args, PyObject *kwargs)
  {
64348153c   Frederic Weisbecker   perf python: Clea...
500
  	static char *kwlist[] = { "cpustr", NULL };
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  	char *cpustr = NULL;
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
  					 kwlist, &cpustr))
  		return -1;
  
  	pcpus->cpus = cpu_map__new(cpustr);
  	if (pcpus->cpus == NULL)
  		return -1;
  	return 0;
  }
  
  static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
  {
f30a79b01   Jiri Olsa   perf tools: Add r...
515
  	cpu_map__put(pcpus->cpus);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  	pcpus->ob_type->tp_free((PyObject*)pcpus);
  }
  
  static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
  {
  	struct pyrf_cpu_map *pcpus = (void *)obj;
  
  	return pcpus->cpus->nr;
  }
  
  static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
  {
  	struct pyrf_cpu_map *pcpus = (void *)obj;
  
  	if (i >= pcpus->cpus->nr)
  		return NULL;
  
  	return Py_BuildValue("i", pcpus->cpus->map[i]);
  }
  
  static PySequenceMethods pyrf_cpu_map__sequence_methods = {
  	.sq_length = pyrf_cpu_map__length,
  	.sq_item   = pyrf_cpu_map__item,
  };
  
  static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
  
  static PyTypeObject pyrf_cpu_map__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.cpu_map",
  	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
  	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_cpu_map__doc,
  	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
  	.tp_init	= (initproc)pyrf_cpu_map__init,
  };
  
  static int pyrf_cpu_map__setup_types(void)
  {
  	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
  	return PyType_Ready(&pyrf_cpu_map__type);
  }
  
  struct pyrf_thread_map {
  	PyObject_HEAD
  
  	struct thread_map *threads;
  };
  
  static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
  				 PyObject *args, PyObject *kwargs)
  {
0d37aa34f   Arnaldo Carvalho de Melo   perf tools: Intro...
569
570
  	static char *kwlist[] = { "pid", "tid", "uid", NULL };
  	int pid = -1, tid = -1, uid = UINT_MAX;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
571

0d37aa34f   Arnaldo Carvalho de Melo   perf tools: Intro...
572
573
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
  					 kwlist, &pid, &tid, &uid))
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
574
  		return -1;
0d37aa34f   Arnaldo Carvalho de Melo   perf tools: Intro...
575
  	pthreads->threads = thread_map__new(pid, tid, uid);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
576
577
578
579
580
581
582
  	if (pthreads->threads == NULL)
  		return -1;
  	return 0;
  }
  
  static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
  {
186fbb743   Jiri Olsa   perf tools: Add r...
583
  	thread_map__put(pthreads->threads);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
  	pthreads->ob_type->tp_free((PyObject*)pthreads);
  }
  
  static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
  {
  	struct pyrf_thread_map *pthreads = (void *)obj;
  
  	return pthreads->threads->nr;
  }
  
  static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
  {
  	struct pyrf_thread_map *pthreads = (void *)obj;
  
  	if (i >= pthreads->threads->nr)
  		return NULL;
  
  	return Py_BuildValue("i", pthreads->threads->map[i]);
  }
  
  static PySequenceMethods pyrf_thread_map__sequence_methods = {
  	.sq_length = pyrf_thread_map__length,
  	.sq_item   = pyrf_thread_map__item,
  };
  
  static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
  
  static PyTypeObject pyrf_thread_map__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.thread_map",
  	.tp_basicsize	= sizeof(struct pyrf_thread_map),
  	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_thread_map__doc,
  	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
  	.tp_init	= (initproc)pyrf_thread_map__init,
  };
  
  static int pyrf_thread_map__setup_types(void)
  {
  	pyrf_thread_map__type.tp_new = PyType_GenericNew;
  	return PyType_Ready(&pyrf_thread_map__type);
  }
  
  struct pyrf_evsel {
  	PyObject_HEAD
  
  	struct perf_evsel evsel;
  };
  
  static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
  			    PyObject *args, PyObject *kwargs)
  {
  	struct perf_event_attr attr = {
  		.type = PERF_TYPE_HARDWARE,
  		.config = PERF_COUNT_HW_CPU_CYCLES,
  		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
  	};
  	static char *kwlist[] = {
  		"type",
  		"config",
  		"sample_freq",
  		"sample_period",
  		"sample_type",
  		"read_format",
  		"disabled",
  		"inherit",
  		"pinned",
  		"exclusive",
  		"exclude_user",
  		"exclude_kernel",
  		"exclude_hv",
  		"exclude_idle",
  		"mmap",
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
658
  		"context_switch",
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
659
660
661
662
663
664
665
666
667
668
669
670
  		"comm",
  		"freq",
  		"inherit_stat",
  		"enable_on_exec",
  		"task",
  		"watermark",
  		"precise_ip",
  		"mmap_data",
  		"sample_id_all",
  		"wakeup_events",
  		"bp_type",
  		"bp_addr",
64348153c   Frederic Weisbecker   perf python: Clea...
671
672
673
  		"bp_len",
  		 NULL
  	};
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
674
675
676
677
678
679
680
681
682
683
  	u64 sample_period = 0;
  	u32 disabled = 0,
  	    inherit = 0,
  	    pinned = 0,
  	    exclusive = 0,
  	    exclude_user = 0,
  	    exclude_kernel = 0,
  	    exclude_hv = 0,
  	    exclude_idle = 0,
  	    mmap = 0,
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
684
  	    context_switch = 0,
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
685
686
687
688
689
690
691
692
693
694
695
696
  	    comm = 0,
  	    freq = 1,
  	    inherit_stat = 0,
  	    enable_on_exec = 0,
  	    task = 0,
  	    watermark = 0,
  	    precise_ip = 0,
  	    mmap_data = 0,
  	    sample_id_all = 1;
  	int idx = 0;
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
697
  					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
698
699
700
701
702
  					 &attr.type, &attr.config, &attr.sample_freq,
  					 &sample_period, &attr.sample_type,
  					 &attr.read_format, &disabled, &inherit,
  					 &pinned, &exclusive, &exclude_user,
  					 &exclude_kernel, &exclude_hv, &exclude_idle,
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
703
  					 &mmap, &context_switch, &comm, &freq, &inherit_stat,
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  					 &enable_on_exec, &task, &watermark,
  					 &precise_ip, &mmap_data, &sample_id_all,
  					 &attr.wakeup_events, &attr.bp_type,
  					 &attr.bp_addr, &attr.bp_len, &idx))
  		return -1;
  
  	/* union... */
  	if (sample_period != 0) {
  		if (attr.sample_freq != 0)
  			return -1; /* FIXME: throw right exception */
  		attr.sample_period = sample_period;
  	}
  
  	/* Bitfields */
  	attr.disabled	    = disabled;
  	attr.inherit	    = inherit;
  	attr.pinned	    = pinned;
  	attr.exclusive	    = exclusive;
  	attr.exclude_user   = exclude_user;
  	attr.exclude_kernel = exclude_kernel;
  	attr.exclude_hv	    = exclude_hv;
  	attr.exclude_idle   = exclude_idle;
  	attr.mmap	    = mmap;
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
727
  	attr.context_switch = context_switch;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
728
729
730
731
732
733
734
735
736
  	attr.comm	    = comm;
  	attr.freq	    = freq;
  	attr.inherit_stat   = inherit_stat;
  	attr.enable_on_exec = enable_on_exec;
  	attr.task	    = task;
  	attr.watermark	    = watermark;
  	attr.precise_ip	    = precise_ip;
  	attr.mmap_data	    = mmap_data;
  	attr.sample_id_all  = sample_id_all;
ad4e3c045   Jiri Olsa   perf python: Init...
737
  	attr.size	    = sizeof(attr);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
  
  	perf_evsel__init(&pevsel->evsel, &attr, idx);
  	return 0;
  }
  
  static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
  {
  	perf_evsel__exit(&pevsel->evsel);
  	pevsel->ob_type->tp_free((PyObject*)pevsel);
  }
  
  static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
  				  PyObject *args, PyObject *kwargs)
  {
  	struct perf_evsel *evsel = &pevsel->evsel;
  	struct cpu_map *cpus = NULL;
  	struct thread_map *threads = NULL;
  	PyObject *pcpus = NULL, *pthreads = NULL;
5d2cd9092   Arnaldo Carvalho de Melo   perf evsel: Fix u...
756
  	int group = 0, inherit = 0;
64348153c   Frederic Weisbecker   perf python: Clea...
757
  	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
758
759
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
5d2cd9092   Arnaldo Carvalho de Melo   perf evsel: Fix u...
760
  					 &pcpus, &pthreads, &group, &inherit))
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
761
762
763
764
765
766
767
  		return NULL;
  
  	if (pthreads != NULL)
  		threads = ((struct pyrf_thread_map *)pthreads)->threads;
  
  	if (pcpus != NULL)
  		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
5d2cd9092   Arnaldo Carvalho de Melo   perf evsel: Fix u...
768
  	evsel->attr.inherit = inherit;
727ab04ed   Arnaldo Carvalho de Melo   perf evlist: Fix ...
769
770
771
772
  	/*
  	 * This will group just the fds for this single evsel, to group
  	 * multiple events, use evlist.open().
  	 */
6a4bb04ca   Jiri Olsa   perf tools: Enabl...
773
  	if (perf_evsel__open(evsel, cpus, threads) < 0) {
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  		PyErr_SetFromErrno(PyExc_OSError);
  		return NULL;
  	}
  
  	Py_INCREF(Py_None);
  	return Py_None;
  }
  
  static PyMethodDef pyrf_evsel__methods[] = {
  	{
  		.ml_name  = "open",
  		.ml_meth  = (PyCFunction)pyrf_evsel__open,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
  	},
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
789
  	{ .ml_name = NULL, }
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
  };
  
  static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
  
  static PyTypeObject pyrf_evsel__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.evsel",
  	.tp_basicsize	= sizeof(struct pyrf_evsel),
  	.tp_dealloc	= (destructor)pyrf_evsel__delete,
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_doc		= pyrf_evsel__doc,
  	.tp_methods	= pyrf_evsel__methods,
  	.tp_init	= (initproc)pyrf_evsel__init,
  };
  
  static int pyrf_evsel__setup_types(void)
  {
  	pyrf_evsel__type.tp_new = PyType_GenericNew;
  	return PyType_Ready(&pyrf_evsel__type);
  }
  
  struct pyrf_evlist {
  	PyObject_HEAD
  
  	struct perf_evlist evlist;
  };
  
  static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
1d037ca16   Irina Tirdea   perf tools: Use _...
818
  			     PyObject *args, PyObject *kwargs __maybe_unused)
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
819
  {
7e2ed0975   Arnaldo Carvalho de Melo   perf evlist: Stor...
820
821
822
823
824
825
826
827
828
829
  	PyObject *pcpus = NULL, *pthreads = NULL;
  	struct cpu_map *cpus;
  	struct thread_map *threads;
  
  	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
  		return -1;
  
  	threads = ((struct pyrf_thread_map *)pthreads)->threads;
  	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  	perf_evlist__init(&pevlist->evlist, cpus, threads);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
830
831
832
833
834
835
836
837
838
839
840
841
842
  	return 0;
  }
  
  static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
  {
  	perf_evlist__exit(&pevlist->evlist);
  	pevlist->ob_type->tp_free((PyObject*)pevlist);
  }
  
  static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
  				   PyObject *args, PyObject *kwargs)
  {
  	struct perf_evlist *evlist = &pevlist->evlist;
64348153c   Frederic Weisbecker   perf python: Clea...
843
  	static char *kwlist[] = { "pages", "overwrite", NULL };
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
844
  	int pages = 128, overwrite = false;
7e2ed0975   Arnaldo Carvalho de Melo   perf evlist: Stor...
845
846
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
  					 &pages, &overwrite))
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
847
  		return NULL;
7e2ed0975   Arnaldo Carvalho de Melo   perf evlist: Stor...
848
  	if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
849
850
851
852
853
854
855
856
857
858
859
860
  		PyErr_SetFromErrno(PyExc_OSError);
  		return NULL;
  	}
  
  	Py_INCREF(Py_None);
  	return Py_None;
  }
  
  static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
  				   PyObject *args, PyObject *kwargs)
  {
  	struct perf_evlist *evlist = &pevlist->evlist;
64348153c   Frederic Weisbecker   perf python: Clea...
861
  	static char *kwlist[] = { "timeout", NULL };
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
862
863
864
865
  	int timeout = -1, n;
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
  		return NULL;
f66a889db   Arnaldo Carvalho de Melo   perf evlist: Intr...
866
  	n = perf_evlist__poll(evlist, timeout);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
867
868
869
870
871
872
873
874
875
  	if (n < 0) {
  		PyErr_SetFromErrno(PyExc_OSError);
  		return NULL;
  	}
  
  	return Py_BuildValue("i", n);
  }
  
  static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
1d037ca16   Irina Tirdea   perf tools: Use _...
876
877
  					 PyObject *args __maybe_unused,
  					 PyObject *kwargs __maybe_unused)
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
878
879
880
881
  {
  	struct perf_evlist *evlist = &pevlist->evlist;
          PyObject *list = PyList_New(0);
  	int i;
1b85337d0   Arnaldo Carvalho de Melo   tools lib api: Ad...
882
  	for (i = 0; i < evlist->pollfd.nr; ++i) {
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
883
  		PyObject *file;
1b85337d0   Arnaldo Carvalho de Melo   tools lib api: Ad...
884
  		FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
885
886
887
888
889
890
891
892
893
894
895
896
  
  		if (fp == NULL)
  			goto free_list;
  
  		file = PyFile_FromFile(fp, "perf", "r", NULL);
  		if (file == NULL)
  			goto free_list;
  
  		if (PyList_Append(list, file) != 0) {
  			Py_DECREF(file);
  			goto free_list;
  		}
48000a1ae   Arnaldo Carvalho de Melo   perf tools: Remov...
897

877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
898
899
900
901
902
903
904
905
906
907
  		Py_DECREF(file);
  	}
  
  	return list;
  free_list:
  	return PyErr_NoMemory();
  }
  
  
  static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
1d037ca16   Irina Tirdea   perf tools: Use _...
908
909
  				  PyObject *args,
  				  PyObject *kwargs __maybe_unused)
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
  {
  	struct perf_evlist *evlist = &pevlist->evlist;
  	PyObject *pevsel;
  	struct perf_evsel *evsel;
  
  	if (!PyArg_ParseTuple(args, "O", &pevsel))
  		return NULL;
  
  	Py_INCREF(pevsel);
  	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
  	evsel->idx = evlist->nr_entries;
  	perf_evlist__add(evlist, evsel);
  
  	return Py_BuildValue("i", evlist->nr_entries);
  }
  
  static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
  					  PyObject *args, PyObject *kwargs)
  {
  	struct perf_evlist *evlist = &pevlist->evlist;
  	union perf_event *event;
  	int sample_id_all = 1, cpu;
64348153c   Frederic Weisbecker   perf python: Clea...
932
  	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
5538becae   Frederic Weisbecker   perf tools: Propa...
933
  	int err;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
934
935
936
937
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
  					 &cpu, &sample_id_all))
  		return NULL;
aece948f5   Arnaldo Carvalho de Melo   perf evlist: Fix ...
938
  	event = perf_evlist__mmap_read(evlist, cpu);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
939
  	if (event != NULL) {
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
940
941
  		PyObject *pyevent = pyrf_event__new(event);
  		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
377f698db   Jiri Olsa   perf python: Add ...
942
  		struct perf_evsel *evsel;
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
943
944
945
  
  		if (pyevent == NULL)
  			return PyErr_NoMemory();
377f698db   Jiri Olsa   perf python: Add ...
946
947
948
949
950
951
952
  		evsel = perf_evlist__event2evsel(evlist, event);
  		if (!evsel)
  			return Py_None;
  
  		pevent->evsel = evsel;
  
  		err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
e8968e654   Jiri Olsa   perf python: Fix ...
953
954
955
  
  		/* Consume the even only after we parsed it out. */
  		perf_evlist__mmap_consume(evlist, cpu);
5c6970af2   Arnaldo Carvalho de Melo   perf python: Use ...
956
957
958
  		if (err)
  			return PyErr_Format(PyExc_OSError,
  					    "perf: can't parse sample, err=%d", err);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
959
960
  		return pyevent;
  	}
5c6970af2   Arnaldo Carvalho de Melo   perf python: Use ...
961

877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
962
963
964
  	Py_INCREF(Py_None);
  	return Py_None;
  }
727ab04ed   Arnaldo Carvalho de Melo   perf evlist: Fix ...
965
966
967
968
969
970
971
972
973
  static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  				   PyObject *args, PyObject *kwargs)
  {
  	struct perf_evlist *evlist = &pevlist->evlist;
  	int group = 0;
  	static char *kwlist[] = { "group", NULL };
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
  		return NULL;
6a4bb04ca   Jiri Olsa   perf tools: Enabl...
974
  	if (group)
63dab225f   Arnaldo Carvalho de Melo   perf evlist: Rena...
975
  		perf_evlist__set_leader(evlist);
6a4bb04ca   Jiri Olsa   perf tools: Enabl...
976
977
  
  	if (perf_evlist__open(evlist) < 0) {
727ab04ed   Arnaldo Carvalho de Melo   perf evlist: Fix ...
978
979
980
981
982
983
984
  		PyErr_SetFromErrno(PyExc_OSError);
  		return NULL;
  	}
  
  	Py_INCREF(Py_None);
  	return Py_None;
  }
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
985
986
987
988
989
990
991
992
  static PyMethodDef pyrf_evlist__methods[] = {
  	{
  		.ml_name  = "mmap",
  		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
  	},
  	{
727ab04ed   Arnaldo Carvalho de Melo   perf evlist: Fix ...
993
994
995
996
997
998
  		.ml_name  = "open",
  		.ml_meth  = (PyCFunction)pyrf_evlist__open,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("open the file descriptors.")
  	},
  	{
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  		.ml_name  = "poll",
  		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
  	},
  	{
  		.ml_name  = "get_pollfd",
  		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
  	},
  	{
  		.ml_name  = "add",
  		.ml_meth  = (PyCFunction)pyrf_evlist__add,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
  	},
  	{
  		.ml_name  = "read_on_cpu",
  		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("reads an event.")
  	},
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
1022
  	{ .ml_name = NULL, }
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  };
  
  static Py_ssize_t pyrf_evlist__length(PyObject *obj)
  {
  	struct pyrf_evlist *pevlist = (void *)obj;
  
  	return pevlist->evlist.nr_entries;
  }
  
  static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
  {
  	struct pyrf_evlist *pevlist = (void *)obj;
  	struct perf_evsel *pos;
  
  	if (i >= pevlist->evlist.nr_entries)
  		return NULL;
e5cadb93d   Arnaldo Carvalho de Melo   perf evlist: Rena...
1039
  	evlist__for_each_entry(&pevlist->evlist, pos) {
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1040
1041
  		if (i-- == 0)
  			break;
0050f7aa1   Arnaldo Carvalho de Melo   perf evlist: Intr...
1042
  	}
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
  
  	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
  }
  
  static PySequenceMethods pyrf_evlist__sequence_methods = {
  	.sq_length = pyrf_evlist__length,
  	.sq_item   = pyrf_evlist__item,
  };
  
  static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
  
  static PyTypeObject pyrf_evlist__type = {
  	PyVarObject_HEAD_INIT(NULL, 0)
  	.tp_name	= "perf.evlist",
  	.tp_basicsize	= sizeof(struct pyrf_evlist),
  	.tp_dealloc	= (destructor)pyrf_evlist__delete,
  	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
  	.tp_doc		= pyrf_evlist__doc,
  	.tp_methods	= pyrf_evlist__methods,
  	.tp_init	= (initproc)pyrf_evlist__init,
  };
  
  static int pyrf_evlist__setup_types(void)
  {
  	pyrf_evlist__type.tp_new = PyType_GenericNew;
  	return PyType_Ready(&pyrf_evlist__type);
  }
5865fe361   Arnaldo Carvalho de Melo   perf python: Add ...
1071
  #define PERF_CONST(name) { #name, PERF_##name }
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1072
1073
1074
1075
  static struct {
  	const char *name;
  	int	    value;
  } perf__constants[] = {
5865fe361   Arnaldo Carvalho de Melo   perf python: Add ...
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  	PERF_CONST(TYPE_HARDWARE),
  	PERF_CONST(TYPE_SOFTWARE),
  	PERF_CONST(TYPE_TRACEPOINT),
  	PERF_CONST(TYPE_HW_CACHE),
  	PERF_CONST(TYPE_RAW),
  	PERF_CONST(TYPE_BREAKPOINT),
  
  	PERF_CONST(COUNT_HW_CPU_CYCLES),
  	PERF_CONST(COUNT_HW_INSTRUCTIONS),
  	PERF_CONST(COUNT_HW_CACHE_REFERENCES),
  	PERF_CONST(COUNT_HW_CACHE_MISSES),
  	PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
  	PERF_CONST(COUNT_HW_BRANCH_MISSES),
  	PERF_CONST(COUNT_HW_BUS_CYCLES),
  	PERF_CONST(COUNT_HW_CACHE_L1D),
  	PERF_CONST(COUNT_HW_CACHE_L1I),
  	PERF_CONST(COUNT_HW_CACHE_LL),
  	PERF_CONST(COUNT_HW_CACHE_DTLB),
  	PERF_CONST(COUNT_HW_CACHE_ITLB),
  	PERF_CONST(COUNT_HW_CACHE_BPU),
  	PERF_CONST(COUNT_HW_CACHE_OP_READ),
  	PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
  	PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
  	PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
  	PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
  
  	PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
  	PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
  
  	PERF_CONST(COUNT_SW_CPU_CLOCK),
  	PERF_CONST(COUNT_SW_TASK_CLOCK),
  	PERF_CONST(COUNT_SW_PAGE_FAULTS),
  	PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
  	PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
  	PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
  	PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
  	PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
  	PERF_CONST(COUNT_SW_EMULATION_FAULTS),
  	PERF_CONST(COUNT_SW_DUMMY),
  
  	PERF_CONST(SAMPLE_IP),
  	PERF_CONST(SAMPLE_TID),
  	PERF_CONST(SAMPLE_TIME),
  	PERF_CONST(SAMPLE_ADDR),
  	PERF_CONST(SAMPLE_READ),
  	PERF_CONST(SAMPLE_CALLCHAIN),
  	PERF_CONST(SAMPLE_ID),
  	PERF_CONST(SAMPLE_CPU),
  	PERF_CONST(SAMPLE_PERIOD),
  	PERF_CONST(SAMPLE_STREAM_ID),
  	PERF_CONST(SAMPLE_RAW),
  
  	PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
  	PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
  	PERF_CONST(FORMAT_ID),
  	PERF_CONST(FORMAT_GROUP),
  
  	PERF_CONST(RECORD_MMAP),
  	PERF_CONST(RECORD_LOST),
  	PERF_CONST(RECORD_COMM),
  	PERF_CONST(RECORD_EXIT),
  	PERF_CONST(RECORD_THROTTLE),
  	PERF_CONST(RECORD_UNTHROTTLE),
  	PERF_CONST(RECORD_FORK),
  	PERF_CONST(RECORD_READ),
  	PERF_CONST(RECORD_SAMPLE),
84576da2f   Arnaldo Carvalho de Melo   perf python: Add ...
1142
1143
1144
1145
1146
1147
  	PERF_CONST(RECORD_MMAP2),
  	PERF_CONST(RECORD_AUX),
  	PERF_CONST(RECORD_ITRACE_START),
  	PERF_CONST(RECORD_LOST_SAMPLES),
  	PERF_CONST(RECORD_SWITCH),
  	PERF_CONST(RECORD_SWITCH_CPU_WIDE),
ae9388024   Arnaldo Carvalho de Melo   perf python: Supp...
1148
1149
  
  	PERF_CONST(RECORD_MISC_SWITCH_OUT),
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
1150
  	{ .name = NULL, },
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1151
  };
1075fbb22   Jiri Olsa   perf python: Add ...
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
  				  PyObject *args, PyObject *kwargs)
  {
  	struct event_format *tp_format;
  	static char *kwlist[] = { "sys", "name", NULL };
  	char *sys  = NULL;
  	char *name = NULL;
  
  	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
  					 &sys, &name))
  		return NULL;
  
  	tp_format = trace_event__tp_format(sys, name);
  	if (IS_ERR(tp_format))
  		return PyInt_FromLong(-1);
  
  	return PyInt_FromLong(tp_format->id);
  }
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1170
  static PyMethodDef perf__methods[] = {
1075fbb22   Jiri Olsa   perf python: Add ...
1171
1172
1173
1174
1175
1176
  	{
  		.ml_name  = "tracepoint",
  		.ml_meth  = (PyCFunction) pyrf__tracepoint,
  		.ml_flags = METH_VARARGS | METH_KEYWORDS,
  		.ml_doc	  = PyDoc_STR("Get tracepoint config.")
  	},
f6bbc1daa   Arnaldo Carvalho de Melo   perf python: Fix ...
1177
  	{ .ml_name = NULL, }
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
  };
  
  PyMODINIT_FUNC initperf(void)
  {
  	PyObject *obj;
  	int i;
  	PyObject *dict, *module = Py_InitModule("perf", perf__methods);
  
  	if (module == NULL ||
  	    pyrf_event__setup_types() < 0 ||
  	    pyrf_evlist__setup_types() < 0 ||
  	    pyrf_evsel__setup_types() < 0 ||
  	    pyrf_thread_map__setup_types() < 0 ||
  	    pyrf_cpu_map__setup_types() < 0)
  		return;
918512b43   Jiri Olsa   perf tools: Unify...
1193
  	/* The page_size is placed in util object. */
0da2e9c24   Arnaldo Carvalho de Melo   perf python: Init...
1194
  	page_size = sysconf(_SC_PAGE_SIZE);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1195
1196
1197
1198
1199
  	Py_INCREF(&pyrf_evlist__type);
  	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
  
  	Py_INCREF(&pyrf_evsel__type);
  	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
85e37de3a   Jiri Olsa   perf python: Put ...
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
  	Py_INCREF(&pyrf_mmap_event__type);
  	PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
  
  	Py_INCREF(&pyrf_lost_event__type);
  	PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
  
  	Py_INCREF(&pyrf_comm_event__type);
  	PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
  
  	Py_INCREF(&pyrf_task_event__type);
  	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  
  	Py_INCREF(&pyrf_throttle_event__type);
  	PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
  
  	Py_INCREF(&pyrf_task_event__type);
  	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  
  	Py_INCREF(&pyrf_read_event__type);
  	PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
  
  	Py_INCREF(&pyrf_sample_event__type);
  	PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
  
  	Py_INCREF(&pyrf_context_switch_event__type);
  	PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
877108e42   Arnaldo Carvalho de Melo   perf tools: Initi...
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
  	Py_INCREF(&pyrf_thread_map__type);
  	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
  
  	Py_INCREF(&pyrf_cpu_map__type);
  	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
  
  	dict = PyModule_GetDict(module);
  	if (dict == NULL)
  		goto error;
  
  	for (i = 0; perf__constants[i].name != NULL; i++) {
  		obj = PyInt_FromLong(perf__constants[i].value);
  		if (obj == NULL)
  			goto error;
  		PyDict_SetItemString(dict, perf__constants[i].name, obj);
  		Py_DECREF(obj);
  	}
  
  error:
  	if (PyErr_Occurred())
  		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
  }
0c6332e9d   Arnaldo Carvalho de Melo   perf python: Fix ...
1248
1249
1250
1251
1252
1253
1254
1255
1256
  
  /*
   * Dummy, to avoid dragging all the test_attr infrastructure in the python
   * binding.
   */
  void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
                       int fd, int group_fd, unsigned long flags)
  {
  }