python.c 36 KB

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