python.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <Python.h>
  3. #include <structmember.h>
  4. #include <inttypes.h>
  5. #include <poll.h>
  6. #include <linux/err.h>
  7. #include "evlist.h"
  8. #include "callchain.h"
  9. #include "evsel.h"
  10. #include "event.h"
  11. #include "cpumap.h"
  12. #include "print_binary.h"
  13. #include "thread_map.h"
  14. #include "mmap.h"
  15. #if PY_MAJOR_VERSION < 3
  16. #define _PyUnicode_FromString(arg) \
  17. PyString_FromString(arg)
  18. #define _PyUnicode_AsString(arg) \
  19. PyString_AsString(arg)
  20. #define _PyUnicode_FromFormat(...) \
  21. PyString_FromFormat(__VA_ARGS__)
  22. #define _PyLong_FromLong(arg) \
  23. PyInt_FromLong(arg)
  24. #else
  25. #define _PyUnicode_FromString(arg) \
  26. PyUnicode_FromString(arg)
  27. #define _PyUnicode_FromFormat(...) \
  28. PyUnicode_FromFormat(__VA_ARGS__)
  29. #define _PyLong_FromLong(arg) \
  30. PyLong_FromLong(arg)
  31. #endif
  32. #ifndef Py_TYPE
  33. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  34. #endif
  35. /*
  36. * Provide these two so that we don't have to link against callchain.c and
  37. * start dragging hist.c, etc.
  38. */
  39. struct callchain_param callchain_param;
  40. int parse_callchain_record(const char *arg __maybe_unused,
  41. struct callchain_param *param __maybe_unused)
  42. {
  43. return 0;
  44. }
  45. /*
  46. * Support debug printing even though util/debug.c is not linked. That means
  47. * implementing 'verbose' and 'eprintf'.
  48. */
  49. int verbose;
  50. int eprintf(int level, int var, const char *fmt, ...)
  51. {
  52. va_list args;
  53. int ret = 0;
  54. if (var >= level) {
  55. va_start(args, fmt);
  56. ret = vfprintf(stderr, fmt, args);
  57. va_end(args);
  58. }
  59. return ret;
  60. }
  61. /* Define PyVarObject_HEAD_INIT for python 2.5 */
  62. #ifndef PyVarObject_HEAD_INIT
  63. # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
  64. #endif
  65. #if PY_MAJOR_VERSION < 3
  66. PyMODINIT_FUNC initperf(void);
  67. #else
  68. PyMODINIT_FUNC PyInit_perf(void);
  69. #endif
  70. #define member_def(type, member, ptype, help) \
  71. { #member, ptype, \
  72. offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
  73. 0, help }
  74. #define sample_member_def(name, member, ptype, help) \
  75. { #name, ptype, \
  76. offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
  77. 0, help }
  78. struct pyrf_event {
  79. PyObject_HEAD
  80. struct perf_evsel *evsel;
  81. struct perf_sample sample;
  82. union perf_event event;
  83. };
  84. #define sample_members \
  85. sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
  86. sample_member_def(sample_pid, pid, T_INT, "event pid"), \
  87. sample_member_def(sample_tid, tid, T_INT, "event tid"), \
  88. sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
  89. sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
  90. sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
  91. sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
  92. sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
  93. sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
  94. static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
  95. static PyMemberDef pyrf_mmap_event__members[] = {
  96. sample_members
  97. member_def(perf_event_header, type, T_UINT, "event type"),
  98. member_def(perf_event_header, misc, T_UINT, "event misc"),
  99. member_def(mmap_event, pid, T_UINT, "event pid"),
  100. member_def(mmap_event, tid, T_UINT, "event tid"),
  101. member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
  102. member_def(mmap_event, len, T_ULONGLONG, "map length"),
  103. member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
  104. member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
  105. { .name = NULL, },
  106. };
  107. static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
  108. {
  109. PyObject *ret;
  110. char *s;
  111. if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
  112. "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
  113. "filename: %s }",
  114. pevent->event.mmap.pid, pevent->event.mmap.tid,
  115. pevent->event.mmap.start, pevent->event.mmap.len,
  116. pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
  117. ret = PyErr_NoMemory();
  118. } else {
  119. ret = _PyUnicode_FromString(s);
  120. free(s);
  121. }
  122. return ret;
  123. }
  124. static PyTypeObject pyrf_mmap_event__type = {
  125. PyVarObject_HEAD_INIT(NULL, 0)
  126. .tp_name = "perf.mmap_event",
  127. .tp_basicsize = sizeof(struct pyrf_event),
  128. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  129. .tp_doc = pyrf_mmap_event__doc,
  130. .tp_members = pyrf_mmap_event__members,
  131. .tp_repr = (reprfunc)pyrf_mmap_event__repr,
  132. };
  133. static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
  134. static PyMemberDef pyrf_task_event__members[] = {
  135. sample_members
  136. member_def(perf_event_header, type, T_UINT, "event type"),
  137. member_def(fork_event, pid, T_UINT, "event pid"),
  138. member_def(fork_event, ppid, T_UINT, "event ppid"),
  139. member_def(fork_event, tid, T_UINT, "event tid"),
  140. member_def(fork_event, ptid, T_UINT, "event ptid"),
  141. member_def(fork_event, time, T_ULONGLONG, "timestamp"),
  142. { .name = NULL, },
  143. };
  144. static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
  145. {
  146. return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
  147. "ptid: %u, time: %" PRIu64 "}",
  148. pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
  149. pevent->event.fork.pid,
  150. pevent->event.fork.ppid,
  151. pevent->event.fork.tid,
  152. pevent->event.fork.ptid,
  153. pevent->event.fork.time);
  154. }
  155. static PyTypeObject pyrf_task_event__type = {
  156. PyVarObject_HEAD_INIT(NULL, 0)
  157. .tp_name = "perf.task_event",
  158. .tp_basicsize = sizeof(struct pyrf_event),
  159. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  160. .tp_doc = pyrf_task_event__doc,
  161. .tp_members = pyrf_task_event__members,
  162. .tp_repr = (reprfunc)pyrf_task_event__repr,
  163. };
  164. static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
  165. static PyMemberDef pyrf_comm_event__members[] = {
  166. sample_members
  167. member_def(perf_event_header, type, T_UINT, "event type"),
  168. member_def(comm_event, pid, T_UINT, "event pid"),
  169. member_def(comm_event, tid, T_UINT, "event tid"),
  170. member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
  171. { .name = NULL, },
  172. };
  173. static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
  174. {
  175. return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
  176. pevent->event.comm.pid,
  177. pevent->event.comm.tid,
  178. pevent->event.comm.comm);
  179. }
  180. static PyTypeObject pyrf_comm_event__type = {
  181. PyVarObject_HEAD_INIT(NULL, 0)
  182. .tp_name = "perf.comm_event",
  183. .tp_basicsize = sizeof(struct pyrf_event),
  184. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  185. .tp_doc = pyrf_comm_event__doc,
  186. .tp_members = pyrf_comm_event__members,
  187. .tp_repr = (reprfunc)pyrf_comm_event__repr,
  188. };
  189. static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
  190. static PyMemberDef pyrf_throttle_event__members[] = {
  191. sample_members
  192. member_def(perf_event_header, type, T_UINT, "event type"),
  193. member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
  194. member_def(throttle_event, id, T_ULONGLONG, "event id"),
  195. member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
  196. { .name = NULL, },
  197. };
  198. static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
  199. {
  200. struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
  201. return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
  202. ", stream_id: %" PRIu64 " }",
  203. pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
  204. te->time, te->id, te->stream_id);
  205. }
  206. static PyTypeObject pyrf_throttle_event__type = {
  207. PyVarObject_HEAD_INIT(NULL, 0)
  208. .tp_name = "perf.throttle_event",
  209. .tp_basicsize = sizeof(struct pyrf_event),
  210. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  211. .tp_doc = pyrf_throttle_event__doc,
  212. .tp_members = pyrf_throttle_event__members,
  213. .tp_repr = (reprfunc)pyrf_throttle_event__repr,
  214. };
  215. static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
  216. static PyMemberDef pyrf_lost_event__members[] = {
  217. sample_members
  218. member_def(lost_event, id, T_ULONGLONG, "event id"),
  219. member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
  220. { .name = NULL, },
  221. };
  222. static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
  223. {
  224. PyObject *ret;
  225. char *s;
  226. if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
  227. "lost: %#" PRIx64 " }",
  228. pevent->event.lost.id, pevent->event.lost.lost) < 0) {
  229. ret = PyErr_NoMemory();
  230. } else {
  231. ret = _PyUnicode_FromString(s);
  232. free(s);
  233. }
  234. return ret;
  235. }
  236. static PyTypeObject pyrf_lost_event__type = {
  237. PyVarObject_HEAD_INIT(NULL, 0)
  238. .tp_name = "perf.lost_event",
  239. .tp_basicsize = sizeof(struct pyrf_event),
  240. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  241. .tp_doc = pyrf_lost_event__doc,
  242. .tp_members = pyrf_lost_event__members,
  243. .tp_repr = (reprfunc)pyrf_lost_event__repr,
  244. };
  245. static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
  246. static PyMemberDef pyrf_read_event__members[] = {
  247. sample_members
  248. member_def(read_event, pid, T_UINT, "event pid"),
  249. member_def(read_event, tid, T_UINT, "event tid"),
  250. { .name = NULL, },
  251. };
  252. static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
  253. {
  254. return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
  255. pevent->event.read.pid,
  256. pevent->event.read.tid);
  257. /*
  258. * FIXME: return the array of read values,
  259. * making this method useful ;-)
  260. */
  261. }
  262. static PyTypeObject pyrf_read_event__type = {
  263. PyVarObject_HEAD_INIT(NULL, 0)
  264. .tp_name = "perf.read_event",
  265. .tp_basicsize = sizeof(struct pyrf_event),
  266. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  267. .tp_doc = pyrf_read_event__doc,
  268. .tp_members = pyrf_read_event__members,
  269. .tp_repr = (reprfunc)pyrf_read_event__repr,
  270. };
  271. static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
  272. static PyMemberDef pyrf_sample_event__members[] = {
  273. sample_members
  274. member_def(perf_event_header, type, T_UINT, "event type"),
  275. { .name = NULL, },
  276. };
  277. static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
  278. {
  279. PyObject *ret;
  280. char *s;
  281. if (asprintf(&s, "{ type: sample }") < 0) {
  282. ret = PyErr_NoMemory();
  283. } else {
  284. ret = _PyUnicode_FromString(s);
  285. free(s);
  286. }
  287. return ret;
  288. }
  289. static bool is_tracepoint(struct pyrf_event *pevent)
  290. {
  291. return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
  292. }
  293. static PyObject*
  294. tracepoint_field(struct pyrf_event *pe, struct format_field *field)
  295. {
  296. struct tep_handle *pevent = field->event->pevent;
  297. void *data = pe->sample.raw_data;
  298. PyObject *ret = NULL;
  299. unsigned long long val;
  300. unsigned int offset, len;
  301. if (field->flags & FIELD_IS_ARRAY) {
  302. offset = field->offset;
  303. len = field->size;
  304. if (field->flags & FIELD_IS_DYNAMIC) {
  305. val = tep_read_number(pevent, data + offset, len);
  306. offset = val;
  307. len = offset >> 16;
  308. offset &= 0xffff;
  309. }
  310. if (field->flags & FIELD_IS_STRING &&
  311. is_printable_array(data + offset, len)) {
  312. ret = _PyUnicode_FromString((char *)data + offset);
  313. } else {
  314. ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
  315. field->flags &= ~FIELD_IS_STRING;
  316. }
  317. } else {
  318. val = tep_read_number(pevent, data + field->offset,
  319. field->size);
  320. if (field->flags & FIELD_IS_POINTER)
  321. ret = PyLong_FromUnsignedLong((unsigned long) val);
  322. else if (field->flags & FIELD_IS_SIGNED)
  323. ret = PyLong_FromLong((long) val);
  324. else
  325. ret = PyLong_FromUnsignedLong((unsigned long) val);
  326. }
  327. return ret;
  328. }
  329. static PyObject*
  330. get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
  331. {
  332. const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
  333. struct perf_evsel *evsel = pevent->evsel;
  334. struct format_field *field;
  335. if (!evsel->tp_format) {
  336. struct event_format *tp_format;
  337. tp_format = trace_event__tp_format_id(evsel->attr.config);
  338. if (!tp_format)
  339. return NULL;
  340. evsel->tp_format = tp_format;
  341. }
  342. field = tep_find_any_field(evsel->tp_format, str);
  343. if (!field)
  344. return NULL;
  345. return tracepoint_field(pevent, field);
  346. }
  347. static PyObject*
  348. pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
  349. {
  350. PyObject *obj = NULL;
  351. if (is_tracepoint(pevent))
  352. obj = get_tracepoint_field(pevent, attr_name);
  353. return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
  354. }
  355. static PyTypeObject pyrf_sample_event__type = {
  356. PyVarObject_HEAD_INIT(NULL, 0)
  357. .tp_name = "perf.sample_event",
  358. .tp_basicsize = sizeof(struct pyrf_event),
  359. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  360. .tp_doc = pyrf_sample_event__doc,
  361. .tp_members = pyrf_sample_event__members,
  362. .tp_repr = (reprfunc)pyrf_sample_event__repr,
  363. .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
  364. };
  365. static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
  366. static PyMemberDef pyrf_context_switch_event__members[] = {
  367. sample_members
  368. member_def(perf_event_header, type, T_UINT, "event type"),
  369. member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
  370. member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
  371. { .name = NULL, },
  372. };
  373. static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
  374. {
  375. PyObject *ret;
  376. char *s;
  377. if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
  378. pevent->event.context_switch.next_prev_pid,
  379. pevent->event.context_switch.next_prev_tid,
  380. !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
  381. ret = PyErr_NoMemory();
  382. } else {
  383. ret = _PyUnicode_FromString(s);
  384. free(s);
  385. }
  386. return ret;
  387. }
  388. static PyTypeObject pyrf_context_switch_event__type = {
  389. PyVarObject_HEAD_INIT(NULL, 0)
  390. .tp_name = "perf.context_switch_event",
  391. .tp_basicsize = sizeof(struct pyrf_event),
  392. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  393. .tp_doc = pyrf_context_switch_event__doc,
  394. .tp_members = pyrf_context_switch_event__members,
  395. .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
  396. };
  397. static int pyrf_event__setup_types(void)
  398. {
  399. int err;
  400. pyrf_mmap_event__type.tp_new =
  401. pyrf_task_event__type.tp_new =
  402. pyrf_comm_event__type.tp_new =
  403. pyrf_lost_event__type.tp_new =
  404. pyrf_read_event__type.tp_new =
  405. pyrf_sample_event__type.tp_new =
  406. pyrf_context_switch_event__type.tp_new =
  407. pyrf_throttle_event__type.tp_new = PyType_GenericNew;
  408. err = PyType_Ready(&pyrf_mmap_event__type);
  409. if (err < 0)
  410. goto out;
  411. err = PyType_Ready(&pyrf_lost_event__type);
  412. if (err < 0)
  413. goto out;
  414. err = PyType_Ready(&pyrf_task_event__type);
  415. if (err < 0)
  416. goto out;
  417. err = PyType_Ready(&pyrf_comm_event__type);
  418. if (err < 0)
  419. goto out;
  420. err = PyType_Ready(&pyrf_throttle_event__type);
  421. if (err < 0)
  422. goto out;
  423. err = PyType_Ready(&pyrf_read_event__type);
  424. if (err < 0)
  425. goto out;
  426. err = PyType_Ready(&pyrf_sample_event__type);
  427. if (err < 0)
  428. goto out;
  429. err = PyType_Ready(&pyrf_context_switch_event__type);
  430. if (err < 0)
  431. goto out;
  432. out:
  433. return err;
  434. }
  435. static PyTypeObject *pyrf_event__type[] = {
  436. [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
  437. [PERF_RECORD_LOST] = &pyrf_lost_event__type,
  438. [PERF_RECORD_COMM] = &pyrf_comm_event__type,
  439. [PERF_RECORD_EXIT] = &pyrf_task_event__type,
  440. [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
  441. [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
  442. [PERF_RECORD_FORK] = &pyrf_task_event__type,
  443. [PERF_RECORD_READ] = &pyrf_read_event__type,
  444. [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
  445. [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
  446. [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
  447. };
  448. static PyObject *pyrf_event__new(union perf_event *event)
  449. {
  450. struct pyrf_event *pevent;
  451. PyTypeObject *ptype;
  452. if ((event->header.type < PERF_RECORD_MMAP ||
  453. event->header.type > PERF_RECORD_SAMPLE) &&
  454. !(event->header.type == PERF_RECORD_SWITCH ||
  455. event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
  456. return NULL;
  457. ptype = pyrf_event__type[event->header.type];
  458. pevent = PyObject_New(struct pyrf_event, ptype);
  459. if (pevent != NULL)
  460. memcpy(&pevent->event, event, event->header.size);
  461. return (PyObject *)pevent;
  462. }
  463. struct pyrf_cpu_map {
  464. PyObject_HEAD
  465. struct cpu_map *cpus;
  466. };
  467. static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
  468. PyObject *args, PyObject *kwargs)
  469. {
  470. static char *kwlist[] = { "cpustr", NULL };
  471. char *cpustr = NULL;
  472. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
  473. kwlist, &cpustr))
  474. return -1;
  475. pcpus->cpus = cpu_map__new(cpustr);
  476. if (pcpus->cpus == NULL)
  477. return -1;
  478. return 0;
  479. }
  480. static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
  481. {
  482. cpu_map__put(pcpus->cpus);
  483. Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
  484. }
  485. static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
  486. {
  487. struct pyrf_cpu_map *pcpus = (void *)obj;
  488. return pcpus->cpus->nr;
  489. }
  490. static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
  491. {
  492. struct pyrf_cpu_map *pcpus = (void *)obj;
  493. if (i >= pcpus->cpus->nr)
  494. return NULL;
  495. return Py_BuildValue("i", pcpus->cpus->map[i]);
  496. }
  497. static PySequenceMethods pyrf_cpu_map__sequence_methods = {
  498. .sq_length = pyrf_cpu_map__length,
  499. .sq_item = pyrf_cpu_map__item,
  500. };
  501. static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
  502. static PyTypeObject pyrf_cpu_map__type = {
  503. PyVarObject_HEAD_INIT(NULL, 0)
  504. .tp_name = "perf.cpu_map",
  505. .tp_basicsize = sizeof(struct pyrf_cpu_map),
  506. .tp_dealloc = (destructor)pyrf_cpu_map__delete,
  507. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  508. .tp_doc = pyrf_cpu_map__doc,
  509. .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
  510. .tp_init = (initproc)pyrf_cpu_map__init,
  511. };
  512. static int pyrf_cpu_map__setup_types(void)
  513. {
  514. pyrf_cpu_map__type.tp_new = PyType_GenericNew;
  515. return PyType_Ready(&pyrf_cpu_map__type);
  516. }
  517. struct pyrf_thread_map {
  518. PyObject_HEAD
  519. struct thread_map *threads;
  520. };
  521. static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
  522. PyObject *args, PyObject *kwargs)
  523. {
  524. static char *kwlist[] = { "pid", "tid", "uid", NULL };
  525. int pid = -1, tid = -1, uid = UINT_MAX;
  526. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
  527. kwlist, &pid, &tid, &uid))
  528. return -1;
  529. pthreads->threads = thread_map__new(pid, tid, uid);
  530. if (pthreads->threads == NULL)
  531. return -1;
  532. return 0;
  533. }
  534. static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
  535. {
  536. thread_map__put(pthreads->threads);
  537. Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
  538. }
  539. static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
  540. {
  541. struct pyrf_thread_map *pthreads = (void *)obj;
  542. return pthreads->threads->nr;
  543. }
  544. static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
  545. {
  546. struct pyrf_thread_map *pthreads = (void *)obj;
  547. if (i >= pthreads->threads->nr)
  548. return NULL;
  549. return Py_BuildValue("i", pthreads->threads->map[i]);
  550. }
  551. static PySequenceMethods pyrf_thread_map__sequence_methods = {
  552. .sq_length = pyrf_thread_map__length,
  553. .sq_item = pyrf_thread_map__item,
  554. };
  555. static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
  556. static PyTypeObject pyrf_thread_map__type = {
  557. PyVarObject_HEAD_INIT(NULL, 0)
  558. .tp_name = "perf.thread_map",
  559. .tp_basicsize = sizeof(struct pyrf_thread_map),
  560. .tp_dealloc = (destructor)pyrf_thread_map__delete,
  561. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  562. .tp_doc = pyrf_thread_map__doc,
  563. .tp_as_sequence = &pyrf_thread_map__sequence_methods,
  564. .tp_init = (initproc)pyrf_thread_map__init,
  565. };
  566. static int pyrf_thread_map__setup_types(void)
  567. {
  568. pyrf_thread_map__type.tp_new = PyType_GenericNew;
  569. return PyType_Ready(&pyrf_thread_map__type);
  570. }
  571. struct pyrf_evsel {
  572. PyObject_HEAD
  573. struct perf_evsel evsel;
  574. };
  575. static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
  576. PyObject *args, PyObject *kwargs)
  577. {
  578. struct perf_event_attr attr = {
  579. .type = PERF_TYPE_HARDWARE,
  580. .config = PERF_COUNT_HW_CPU_CYCLES,
  581. .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
  582. };
  583. static char *kwlist[] = {
  584. "type",
  585. "config",
  586. "sample_freq",
  587. "sample_period",
  588. "sample_type",
  589. "read_format",
  590. "disabled",
  591. "inherit",
  592. "pinned",
  593. "exclusive",
  594. "exclude_user",
  595. "exclude_kernel",
  596. "exclude_hv",
  597. "exclude_idle",
  598. "mmap",
  599. "context_switch",
  600. "comm",
  601. "freq",
  602. "inherit_stat",
  603. "enable_on_exec",
  604. "task",
  605. "watermark",
  606. "precise_ip",
  607. "mmap_data",
  608. "sample_id_all",
  609. "wakeup_events",
  610. "bp_type",
  611. "bp_addr",
  612. "bp_len",
  613. NULL
  614. };
  615. u64 sample_period = 0;
  616. u32 disabled = 0,
  617. inherit = 0,
  618. pinned = 0,
  619. exclusive = 0,
  620. exclude_user = 0,
  621. exclude_kernel = 0,
  622. exclude_hv = 0,
  623. exclude_idle = 0,
  624. mmap = 0,
  625. context_switch = 0,
  626. comm = 0,
  627. freq = 1,
  628. inherit_stat = 0,
  629. enable_on_exec = 0,
  630. task = 0,
  631. watermark = 0,
  632. precise_ip = 0,
  633. mmap_data = 0,
  634. sample_id_all = 1;
  635. int idx = 0;
  636. if (!PyArg_ParseTupleAndKeywords(args, kwargs,
  637. "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
  638. &attr.type, &attr.config, &attr.sample_freq,
  639. &sample_period, &attr.sample_type,
  640. &attr.read_format, &disabled, &inherit,
  641. &pinned, &exclusive, &exclude_user,
  642. &exclude_kernel, &exclude_hv, &exclude_idle,
  643. &mmap, &context_switch, &comm, &freq, &inherit_stat,
  644. &enable_on_exec, &task, &watermark,
  645. &precise_ip, &mmap_data, &sample_id_all,
  646. &attr.wakeup_events, &attr.bp_type,
  647. &attr.bp_addr, &attr.bp_len, &idx))
  648. return -1;
  649. /* union... */
  650. if (sample_period != 0) {
  651. if (attr.sample_freq != 0)
  652. return -1; /* FIXME: throw right exception */
  653. attr.sample_period = sample_period;
  654. }
  655. /* Bitfields */
  656. attr.disabled = disabled;
  657. attr.inherit = inherit;
  658. attr.pinned = pinned;
  659. attr.exclusive = exclusive;
  660. attr.exclude_user = exclude_user;
  661. attr.exclude_kernel = exclude_kernel;
  662. attr.exclude_hv = exclude_hv;
  663. attr.exclude_idle = exclude_idle;
  664. attr.mmap = mmap;
  665. attr.context_switch = context_switch;
  666. attr.comm = comm;
  667. attr.freq = freq;
  668. attr.inherit_stat = inherit_stat;
  669. attr.enable_on_exec = enable_on_exec;
  670. attr.task = task;
  671. attr.watermark = watermark;
  672. attr.precise_ip = precise_ip;
  673. attr.mmap_data = mmap_data;
  674. attr.sample_id_all = sample_id_all;
  675. attr.size = sizeof(attr);
  676. perf_evsel__init(&pevsel->evsel, &attr, idx);
  677. return 0;
  678. }
  679. static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
  680. {
  681. perf_evsel__exit(&pevsel->evsel);
  682. Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
  683. }
  684. static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
  685. PyObject *args, PyObject *kwargs)
  686. {
  687. struct perf_evsel *evsel = &pevsel->evsel;
  688. struct cpu_map *cpus = NULL;
  689. struct thread_map *threads = NULL;
  690. PyObject *pcpus = NULL, *pthreads = NULL;
  691. int group = 0, inherit = 0;
  692. static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
  693. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
  694. &pcpus, &pthreads, &group, &inherit))
  695. return NULL;
  696. if (pthreads != NULL)
  697. threads = ((struct pyrf_thread_map *)pthreads)->threads;
  698. if (pcpus != NULL)
  699. cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  700. evsel->attr.inherit = inherit;
  701. /*
  702. * This will group just the fds for this single evsel, to group
  703. * multiple events, use evlist.open().
  704. */
  705. if (perf_evsel__open(evsel, cpus, threads) < 0) {
  706. PyErr_SetFromErrno(PyExc_OSError);
  707. return NULL;
  708. }
  709. Py_INCREF(Py_None);
  710. return Py_None;
  711. }
  712. static PyMethodDef pyrf_evsel__methods[] = {
  713. {
  714. .ml_name = "open",
  715. .ml_meth = (PyCFunction)pyrf_evsel__open,
  716. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  717. .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
  718. },
  719. { .ml_name = NULL, }
  720. };
  721. static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
  722. static PyTypeObject pyrf_evsel__type = {
  723. PyVarObject_HEAD_INIT(NULL, 0)
  724. .tp_name = "perf.evsel",
  725. .tp_basicsize = sizeof(struct pyrf_evsel),
  726. .tp_dealloc = (destructor)pyrf_evsel__delete,
  727. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  728. .tp_doc = pyrf_evsel__doc,
  729. .tp_methods = pyrf_evsel__methods,
  730. .tp_init = (initproc)pyrf_evsel__init,
  731. };
  732. static int pyrf_evsel__setup_types(void)
  733. {
  734. pyrf_evsel__type.tp_new = PyType_GenericNew;
  735. return PyType_Ready(&pyrf_evsel__type);
  736. }
  737. struct pyrf_evlist {
  738. PyObject_HEAD
  739. struct perf_evlist evlist;
  740. };
  741. static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
  742. PyObject *args, PyObject *kwargs __maybe_unused)
  743. {
  744. PyObject *pcpus = NULL, *pthreads = NULL;
  745. struct cpu_map *cpus;
  746. struct thread_map *threads;
  747. if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
  748. return -1;
  749. threads = ((struct pyrf_thread_map *)pthreads)->threads;
  750. cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  751. perf_evlist__init(&pevlist->evlist, cpus, threads);
  752. return 0;
  753. }
  754. static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
  755. {
  756. perf_evlist__exit(&pevlist->evlist);
  757. Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
  758. }
  759. static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
  760. PyObject *args, PyObject *kwargs)
  761. {
  762. struct perf_evlist *evlist = &pevlist->evlist;
  763. static char *kwlist[] = { "pages", "overwrite", NULL };
  764. int pages = 128, overwrite = false;
  765. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
  766. &pages, &overwrite))
  767. return NULL;
  768. if (perf_evlist__mmap(evlist, pages) < 0) {
  769. PyErr_SetFromErrno(PyExc_OSError);
  770. return NULL;
  771. }
  772. Py_INCREF(Py_None);
  773. return Py_None;
  774. }
  775. static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
  776. PyObject *args, PyObject *kwargs)
  777. {
  778. struct perf_evlist *evlist = &pevlist->evlist;
  779. static char *kwlist[] = { "timeout", NULL };
  780. int timeout = -1, n;
  781. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
  782. return NULL;
  783. n = perf_evlist__poll(evlist, timeout);
  784. if (n < 0) {
  785. PyErr_SetFromErrno(PyExc_OSError);
  786. return NULL;
  787. }
  788. return Py_BuildValue("i", n);
  789. }
  790. static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
  791. PyObject *args __maybe_unused,
  792. PyObject *kwargs __maybe_unused)
  793. {
  794. struct perf_evlist *evlist = &pevlist->evlist;
  795. PyObject *list = PyList_New(0);
  796. int i;
  797. for (i = 0; i < evlist->pollfd.nr; ++i) {
  798. PyObject *file;
  799. #if PY_MAJOR_VERSION < 3
  800. FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
  801. if (fp == NULL)
  802. goto free_list;
  803. file = PyFile_FromFile(fp, "perf", "r", NULL);
  804. #else
  805. file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1,
  806. NULL, NULL, NULL, 0);
  807. #endif
  808. if (file == NULL)
  809. goto free_list;
  810. if (PyList_Append(list, file) != 0) {
  811. Py_DECREF(file);
  812. goto free_list;
  813. }
  814. Py_DECREF(file);
  815. }
  816. return list;
  817. free_list:
  818. return PyErr_NoMemory();
  819. }
  820. static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
  821. PyObject *args,
  822. PyObject *kwargs __maybe_unused)
  823. {
  824. struct perf_evlist *evlist = &pevlist->evlist;
  825. PyObject *pevsel;
  826. struct perf_evsel *evsel;
  827. if (!PyArg_ParseTuple(args, "O", &pevsel))
  828. return NULL;
  829. Py_INCREF(pevsel);
  830. evsel = &((struct pyrf_evsel *)pevsel)->evsel;
  831. evsel->idx = evlist->nr_entries;
  832. perf_evlist__add(evlist, evsel);
  833. return Py_BuildValue("i", evlist->nr_entries);
  834. }
  835. static struct perf_mmap *get_md(struct perf_evlist *evlist, int cpu)
  836. {
  837. int i;
  838. for (i = 0; i < evlist->nr_mmaps; i++) {
  839. struct perf_mmap *md = &evlist->mmap[i];
  840. if (md->cpu == cpu)
  841. return md;
  842. }
  843. return NULL;
  844. }
  845. static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
  846. PyObject *args, PyObject *kwargs)
  847. {
  848. struct perf_evlist *evlist = &pevlist->evlist;
  849. union perf_event *event;
  850. int sample_id_all = 1, cpu;
  851. static char *kwlist[] = { "cpu", "sample_id_all", NULL };
  852. struct perf_mmap *md;
  853. int err;
  854. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
  855. &cpu, &sample_id_all))
  856. return NULL;
  857. md = get_md(evlist, cpu);
  858. if (!md)
  859. return NULL;
  860. if (perf_mmap__read_init(md) < 0)
  861. goto end;
  862. event = perf_mmap__read_event(md);
  863. if (event != NULL) {
  864. PyObject *pyevent = pyrf_event__new(event);
  865. struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
  866. struct perf_evsel *evsel;
  867. if (pyevent == NULL)
  868. return PyErr_NoMemory();
  869. evsel = perf_evlist__event2evsel(evlist, event);
  870. if (!evsel) {
  871. Py_INCREF(Py_None);
  872. return Py_None;
  873. }
  874. pevent->evsel = evsel;
  875. err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
  876. /* Consume the even only after we parsed it out. */
  877. perf_mmap__consume(md);
  878. if (err)
  879. return PyErr_Format(PyExc_OSError,
  880. "perf: can't parse sample, err=%d", err);
  881. return pyevent;
  882. }
  883. end:
  884. Py_INCREF(Py_None);
  885. return Py_None;
  886. }
  887. static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  888. PyObject *args, PyObject *kwargs)
  889. {
  890. struct perf_evlist *evlist = &pevlist->evlist;
  891. int group = 0;
  892. static char *kwlist[] = { "group", NULL };
  893. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
  894. return NULL;
  895. if (group)
  896. perf_evlist__set_leader(evlist);
  897. if (perf_evlist__open(evlist) < 0) {
  898. PyErr_SetFromErrno(PyExc_OSError);
  899. return NULL;
  900. }
  901. Py_INCREF(Py_None);
  902. return Py_None;
  903. }
  904. static PyMethodDef pyrf_evlist__methods[] = {
  905. {
  906. .ml_name = "mmap",
  907. .ml_meth = (PyCFunction)pyrf_evlist__mmap,
  908. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  909. .ml_doc = PyDoc_STR("mmap the file descriptor table.")
  910. },
  911. {
  912. .ml_name = "open",
  913. .ml_meth = (PyCFunction)pyrf_evlist__open,
  914. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  915. .ml_doc = PyDoc_STR("open the file descriptors.")
  916. },
  917. {
  918. .ml_name = "poll",
  919. .ml_meth = (PyCFunction)pyrf_evlist__poll,
  920. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  921. .ml_doc = PyDoc_STR("poll the file descriptor table.")
  922. },
  923. {
  924. .ml_name = "get_pollfd",
  925. .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
  926. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  927. .ml_doc = PyDoc_STR("get the poll file descriptor table.")
  928. },
  929. {
  930. .ml_name = "add",
  931. .ml_meth = (PyCFunction)pyrf_evlist__add,
  932. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  933. .ml_doc = PyDoc_STR("adds an event selector to the list.")
  934. },
  935. {
  936. .ml_name = "read_on_cpu",
  937. .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
  938. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  939. .ml_doc = PyDoc_STR("reads an event.")
  940. },
  941. { .ml_name = NULL, }
  942. };
  943. static Py_ssize_t pyrf_evlist__length(PyObject *obj)
  944. {
  945. struct pyrf_evlist *pevlist = (void *)obj;
  946. return pevlist->evlist.nr_entries;
  947. }
  948. static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
  949. {
  950. struct pyrf_evlist *pevlist = (void *)obj;
  951. struct perf_evsel *pos;
  952. if (i >= pevlist->evlist.nr_entries)
  953. return NULL;
  954. evlist__for_each_entry(&pevlist->evlist, pos) {
  955. if (i-- == 0)
  956. break;
  957. }
  958. return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
  959. }
  960. static PySequenceMethods pyrf_evlist__sequence_methods = {
  961. .sq_length = pyrf_evlist__length,
  962. .sq_item = pyrf_evlist__item,
  963. };
  964. static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
  965. static PyTypeObject pyrf_evlist__type = {
  966. PyVarObject_HEAD_INIT(NULL, 0)
  967. .tp_name = "perf.evlist",
  968. .tp_basicsize = sizeof(struct pyrf_evlist),
  969. .tp_dealloc = (destructor)pyrf_evlist__delete,
  970. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  971. .tp_as_sequence = &pyrf_evlist__sequence_methods,
  972. .tp_doc = pyrf_evlist__doc,
  973. .tp_methods = pyrf_evlist__methods,
  974. .tp_init = (initproc)pyrf_evlist__init,
  975. };
  976. static int pyrf_evlist__setup_types(void)
  977. {
  978. pyrf_evlist__type.tp_new = PyType_GenericNew;
  979. return PyType_Ready(&pyrf_evlist__type);
  980. }
  981. #define PERF_CONST(name) { #name, PERF_##name }
  982. static struct {
  983. const char *name;
  984. int value;
  985. } perf__constants[] = {
  986. PERF_CONST(TYPE_HARDWARE),
  987. PERF_CONST(TYPE_SOFTWARE),
  988. PERF_CONST(TYPE_TRACEPOINT),
  989. PERF_CONST(TYPE_HW_CACHE),
  990. PERF_CONST(TYPE_RAW),
  991. PERF_CONST(TYPE_BREAKPOINT),
  992. PERF_CONST(COUNT_HW_CPU_CYCLES),
  993. PERF_CONST(COUNT_HW_INSTRUCTIONS),
  994. PERF_CONST(COUNT_HW_CACHE_REFERENCES),
  995. PERF_CONST(COUNT_HW_CACHE_MISSES),
  996. PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
  997. PERF_CONST(COUNT_HW_BRANCH_MISSES),
  998. PERF_CONST(COUNT_HW_BUS_CYCLES),
  999. PERF_CONST(COUNT_HW_CACHE_L1D),
  1000. PERF_CONST(COUNT_HW_CACHE_L1I),
  1001. PERF_CONST(COUNT_HW_CACHE_LL),
  1002. PERF_CONST(COUNT_HW_CACHE_DTLB),
  1003. PERF_CONST(COUNT_HW_CACHE_ITLB),
  1004. PERF_CONST(COUNT_HW_CACHE_BPU),
  1005. PERF_CONST(COUNT_HW_CACHE_OP_READ),
  1006. PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
  1007. PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
  1008. PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
  1009. PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
  1010. PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
  1011. PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
  1012. PERF_CONST(COUNT_SW_CPU_CLOCK),
  1013. PERF_CONST(COUNT_SW_TASK_CLOCK),
  1014. PERF_CONST(COUNT_SW_PAGE_FAULTS),
  1015. PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
  1016. PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
  1017. PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
  1018. PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
  1019. PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
  1020. PERF_CONST(COUNT_SW_EMULATION_FAULTS),
  1021. PERF_CONST(COUNT_SW_DUMMY),
  1022. PERF_CONST(SAMPLE_IP),
  1023. PERF_CONST(SAMPLE_TID),
  1024. PERF_CONST(SAMPLE_TIME),
  1025. PERF_CONST(SAMPLE_ADDR),
  1026. PERF_CONST(SAMPLE_READ),
  1027. PERF_CONST(SAMPLE_CALLCHAIN),
  1028. PERF_CONST(SAMPLE_ID),
  1029. PERF_CONST(SAMPLE_CPU),
  1030. PERF_CONST(SAMPLE_PERIOD),
  1031. PERF_CONST(SAMPLE_STREAM_ID),
  1032. PERF_CONST(SAMPLE_RAW),
  1033. PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
  1034. PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
  1035. PERF_CONST(FORMAT_ID),
  1036. PERF_CONST(FORMAT_GROUP),
  1037. PERF_CONST(RECORD_MMAP),
  1038. PERF_CONST(RECORD_LOST),
  1039. PERF_CONST(RECORD_COMM),
  1040. PERF_CONST(RECORD_EXIT),
  1041. PERF_CONST(RECORD_THROTTLE),
  1042. PERF_CONST(RECORD_UNTHROTTLE),
  1043. PERF_CONST(RECORD_FORK),
  1044. PERF_CONST(RECORD_READ),
  1045. PERF_CONST(RECORD_SAMPLE),
  1046. PERF_CONST(RECORD_MMAP2),
  1047. PERF_CONST(RECORD_AUX),
  1048. PERF_CONST(RECORD_ITRACE_START),
  1049. PERF_CONST(RECORD_LOST_SAMPLES),
  1050. PERF_CONST(RECORD_SWITCH),
  1051. PERF_CONST(RECORD_SWITCH_CPU_WIDE),
  1052. PERF_CONST(RECORD_MISC_SWITCH_OUT),
  1053. { .name = NULL, },
  1054. };
  1055. static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
  1056. PyObject *args, PyObject *kwargs)
  1057. {
  1058. struct event_format *tp_format;
  1059. static char *kwlist[] = { "sys", "name", NULL };
  1060. char *sys = NULL;
  1061. char *name = NULL;
  1062. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
  1063. &sys, &name))
  1064. return NULL;
  1065. tp_format = trace_event__tp_format(sys, name);
  1066. if (IS_ERR(tp_format))
  1067. return _PyLong_FromLong(-1);
  1068. return _PyLong_FromLong(tp_format->id);
  1069. }
  1070. static PyMethodDef perf__methods[] = {
  1071. {
  1072. .ml_name = "tracepoint",
  1073. .ml_meth = (PyCFunction) pyrf__tracepoint,
  1074. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  1075. .ml_doc = PyDoc_STR("Get tracepoint config.")
  1076. },
  1077. { .ml_name = NULL, }
  1078. };
  1079. #if PY_MAJOR_VERSION < 3
  1080. PyMODINIT_FUNC initperf(void)
  1081. #else
  1082. PyMODINIT_FUNC PyInit_perf(void)
  1083. #endif
  1084. {
  1085. PyObject *obj;
  1086. int i;
  1087. PyObject *dict;
  1088. #if PY_MAJOR_VERSION < 3
  1089. PyObject *module = Py_InitModule("perf", perf__methods);
  1090. #else
  1091. static struct PyModuleDef moduledef = {
  1092. PyModuleDef_HEAD_INIT,
  1093. "perf", /* m_name */
  1094. "", /* m_doc */
  1095. -1, /* m_size */
  1096. perf__methods, /* m_methods */
  1097. NULL, /* m_reload */
  1098. NULL, /* m_traverse */
  1099. NULL, /* m_clear */
  1100. NULL, /* m_free */
  1101. };
  1102. PyObject *module = PyModule_Create(&moduledef);
  1103. #endif
  1104. if (module == NULL ||
  1105. pyrf_event__setup_types() < 0 ||
  1106. pyrf_evlist__setup_types() < 0 ||
  1107. pyrf_evsel__setup_types() < 0 ||
  1108. pyrf_thread_map__setup_types() < 0 ||
  1109. pyrf_cpu_map__setup_types() < 0)
  1110. #if PY_MAJOR_VERSION < 3
  1111. return;
  1112. #else
  1113. return module;
  1114. #endif
  1115. /* The page_size is placed in util object. */
  1116. page_size = sysconf(_SC_PAGE_SIZE);
  1117. Py_INCREF(&pyrf_evlist__type);
  1118. PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
  1119. Py_INCREF(&pyrf_evsel__type);
  1120. PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
  1121. Py_INCREF(&pyrf_mmap_event__type);
  1122. PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
  1123. Py_INCREF(&pyrf_lost_event__type);
  1124. PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
  1125. Py_INCREF(&pyrf_comm_event__type);
  1126. PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
  1127. Py_INCREF(&pyrf_task_event__type);
  1128. PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  1129. Py_INCREF(&pyrf_throttle_event__type);
  1130. PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
  1131. Py_INCREF(&pyrf_task_event__type);
  1132. PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  1133. Py_INCREF(&pyrf_read_event__type);
  1134. PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
  1135. Py_INCREF(&pyrf_sample_event__type);
  1136. PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
  1137. Py_INCREF(&pyrf_context_switch_event__type);
  1138. PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
  1139. Py_INCREF(&pyrf_thread_map__type);
  1140. PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
  1141. Py_INCREF(&pyrf_cpu_map__type);
  1142. PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
  1143. dict = PyModule_GetDict(module);
  1144. if (dict == NULL)
  1145. goto error;
  1146. for (i = 0; perf__constants[i].name != NULL; i++) {
  1147. obj = _PyLong_FromLong(perf__constants[i].value);
  1148. if (obj == NULL)
  1149. goto error;
  1150. PyDict_SetItemString(dict, perf__constants[i].name, obj);
  1151. Py_DECREF(obj);
  1152. }
  1153. error:
  1154. if (PyErr_Occurred())
  1155. PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
  1156. #if PY_MAJOR_VERSION >= 3
  1157. return module;
  1158. #endif
  1159. }
  1160. /*
  1161. * Dummy, to avoid dragging all the test_attr infrastructure in the python
  1162. * binding.
  1163. */
  1164. void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
  1165. int fd, int group_fd, unsigned long flags)
  1166. {
  1167. }