python.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. #include <Python.h>
  2. #include <structmember.h>
  3. #include <inttypes.h>
  4. #include <poll.h>
  5. #include "evlist.h"
  6. #include "evsel.h"
  7. #include "event.h"
  8. #include "cpumap.h"
  9. #include "thread_map.h"
  10. /* Define PyVarObject_HEAD_INIT for python 2.5 */
  11. #ifndef PyVarObject_HEAD_INIT
  12. # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
  13. #endif
  14. struct throttle_event {
  15. struct perf_event_header header;
  16. u64 time;
  17. u64 id;
  18. u64 stream_id;
  19. };
  20. PyMODINIT_FUNC initperf(void);
  21. #define member_def(type, member, ptype, help) \
  22. { #member, ptype, \
  23. offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
  24. 0, help }
  25. #define sample_member_def(name, member, ptype, help) \
  26. { #name, ptype, \
  27. offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
  28. 0, help }
  29. struct pyrf_event {
  30. PyObject_HEAD
  31. struct perf_sample sample;
  32. union perf_event event;
  33. };
  34. #define sample_members \
  35. sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
  36. sample_member_def(sample_pid, pid, T_INT, "event pid"), \
  37. sample_member_def(sample_tid, tid, T_INT, "event tid"), \
  38. sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
  39. sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
  40. sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
  41. sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
  42. sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
  43. sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
  44. static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
  45. static PyMemberDef pyrf_mmap_event__members[] = {
  46. sample_members
  47. member_def(perf_event_header, type, T_UINT, "event type"),
  48. member_def(mmap_event, pid, T_UINT, "event pid"),
  49. member_def(mmap_event, tid, T_UINT, "event tid"),
  50. member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
  51. member_def(mmap_event, len, T_ULONGLONG, "map length"),
  52. member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
  53. member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
  54. { .name = NULL, },
  55. };
  56. static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
  57. {
  58. PyObject *ret;
  59. char *s;
  60. if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
  61. "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
  62. "filename: %s }",
  63. pevent->event.mmap.pid, pevent->event.mmap.tid,
  64. pevent->event.mmap.start, pevent->event.mmap.len,
  65. pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
  66. ret = PyErr_NoMemory();
  67. } else {
  68. ret = PyString_FromString(s);
  69. free(s);
  70. }
  71. return ret;
  72. }
  73. static PyTypeObject pyrf_mmap_event__type = {
  74. PyVarObject_HEAD_INIT(NULL, 0)
  75. .tp_name = "perf.mmap_event",
  76. .tp_basicsize = sizeof(struct pyrf_event),
  77. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  78. .tp_doc = pyrf_mmap_event__doc,
  79. .tp_members = pyrf_mmap_event__members,
  80. .tp_repr = (reprfunc)pyrf_mmap_event__repr,
  81. };
  82. static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
  83. static PyMemberDef pyrf_task_event__members[] = {
  84. sample_members
  85. member_def(perf_event_header, type, T_UINT, "event type"),
  86. member_def(fork_event, pid, T_UINT, "event pid"),
  87. member_def(fork_event, ppid, T_UINT, "event ppid"),
  88. member_def(fork_event, tid, T_UINT, "event tid"),
  89. member_def(fork_event, ptid, T_UINT, "event ptid"),
  90. member_def(fork_event, time, T_ULONGLONG, "timestamp"),
  91. { .name = NULL, },
  92. };
  93. static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
  94. {
  95. return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
  96. "ptid: %u, time: %" PRIu64 "}",
  97. pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
  98. pevent->event.fork.pid,
  99. pevent->event.fork.ppid,
  100. pevent->event.fork.tid,
  101. pevent->event.fork.ptid,
  102. pevent->event.fork.time);
  103. }
  104. static PyTypeObject pyrf_task_event__type = {
  105. PyVarObject_HEAD_INIT(NULL, 0)
  106. .tp_name = "perf.task_event",
  107. .tp_basicsize = sizeof(struct pyrf_event),
  108. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  109. .tp_doc = pyrf_task_event__doc,
  110. .tp_members = pyrf_task_event__members,
  111. .tp_repr = (reprfunc)pyrf_task_event__repr,
  112. };
  113. static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
  114. static PyMemberDef pyrf_comm_event__members[] = {
  115. sample_members
  116. member_def(perf_event_header, type, T_UINT, "event type"),
  117. member_def(comm_event, pid, T_UINT, "event pid"),
  118. member_def(comm_event, tid, T_UINT, "event tid"),
  119. member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
  120. { .name = NULL, },
  121. };
  122. static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
  123. {
  124. return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
  125. pevent->event.comm.pid,
  126. pevent->event.comm.tid,
  127. pevent->event.comm.comm);
  128. }
  129. static PyTypeObject pyrf_comm_event__type = {
  130. PyVarObject_HEAD_INIT(NULL, 0)
  131. .tp_name = "perf.comm_event",
  132. .tp_basicsize = sizeof(struct pyrf_event),
  133. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  134. .tp_doc = pyrf_comm_event__doc,
  135. .tp_members = pyrf_comm_event__members,
  136. .tp_repr = (reprfunc)pyrf_comm_event__repr,
  137. };
  138. static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
  139. static PyMemberDef pyrf_throttle_event__members[] = {
  140. sample_members
  141. member_def(perf_event_header, type, T_UINT, "event type"),
  142. member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
  143. member_def(throttle_event, id, T_ULONGLONG, "event id"),
  144. member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
  145. { .name = NULL, },
  146. };
  147. static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
  148. {
  149. struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
  150. return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
  151. ", stream_id: %" PRIu64 " }",
  152. pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
  153. te->time, te->id, te->stream_id);
  154. }
  155. static PyTypeObject pyrf_throttle_event__type = {
  156. PyVarObject_HEAD_INIT(NULL, 0)
  157. .tp_name = "perf.throttle_event",
  158. .tp_basicsize = sizeof(struct pyrf_event),
  159. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  160. .tp_doc = pyrf_throttle_event__doc,
  161. .tp_members = pyrf_throttle_event__members,
  162. .tp_repr = (reprfunc)pyrf_throttle_event__repr,
  163. };
  164. static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
  165. static PyMemberDef pyrf_lost_event__members[] = {
  166. sample_members
  167. member_def(lost_event, id, T_ULONGLONG, "event id"),
  168. member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
  169. { .name = NULL, },
  170. };
  171. static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
  172. {
  173. PyObject *ret;
  174. char *s;
  175. if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
  176. "lost: %#" PRIx64 " }",
  177. pevent->event.lost.id, pevent->event.lost.lost) < 0) {
  178. ret = PyErr_NoMemory();
  179. } else {
  180. ret = PyString_FromString(s);
  181. free(s);
  182. }
  183. return ret;
  184. }
  185. static PyTypeObject pyrf_lost_event__type = {
  186. PyVarObject_HEAD_INIT(NULL, 0)
  187. .tp_name = "perf.lost_event",
  188. .tp_basicsize = sizeof(struct pyrf_event),
  189. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  190. .tp_doc = pyrf_lost_event__doc,
  191. .tp_members = pyrf_lost_event__members,
  192. .tp_repr = (reprfunc)pyrf_lost_event__repr,
  193. };
  194. static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
  195. static PyMemberDef pyrf_read_event__members[] = {
  196. sample_members
  197. member_def(read_event, pid, T_UINT, "event pid"),
  198. member_def(read_event, tid, T_UINT, "event tid"),
  199. { .name = NULL, },
  200. };
  201. static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
  202. {
  203. return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
  204. pevent->event.read.pid,
  205. pevent->event.read.tid);
  206. /*
  207. * FIXME: return the array of read values,
  208. * making this method useful ;-)
  209. */
  210. }
  211. static PyTypeObject pyrf_read_event__type = {
  212. PyVarObject_HEAD_INIT(NULL, 0)
  213. .tp_name = "perf.read_event",
  214. .tp_basicsize = sizeof(struct pyrf_event),
  215. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  216. .tp_doc = pyrf_read_event__doc,
  217. .tp_members = pyrf_read_event__members,
  218. .tp_repr = (reprfunc)pyrf_read_event__repr,
  219. };
  220. static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
  221. static PyMemberDef pyrf_sample_event__members[] = {
  222. sample_members
  223. member_def(perf_event_header, type, T_UINT, "event type"),
  224. { .name = NULL, },
  225. };
  226. static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
  227. {
  228. PyObject *ret;
  229. char *s;
  230. if (asprintf(&s, "{ type: sample }") < 0) {
  231. ret = PyErr_NoMemory();
  232. } else {
  233. ret = PyString_FromString(s);
  234. free(s);
  235. }
  236. return ret;
  237. }
  238. static PyTypeObject pyrf_sample_event__type = {
  239. PyVarObject_HEAD_INIT(NULL, 0)
  240. .tp_name = "perf.sample_event",
  241. .tp_basicsize = sizeof(struct pyrf_event),
  242. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  243. .tp_doc = pyrf_sample_event__doc,
  244. .tp_members = pyrf_sample_event__members,
  245. .tp_repr = (reprfunc)pyrf_sample_event__repr,
  246. };
  247. static int pyrf_event__setup_types(void)
  248. {
  249. int err;
  250. pyrf_mmap_event__type.tp_new =
  251. pyrf_task_event__type.tp_new =
  252. pyrf_comm_event__type.tp_new =
  253. pyrf_lost_event__type.tp_new =
  254. pyrf_read_event__type.tp_new =
  255. pyrf_sample_event__type.tp_new =
  256. pyrf_throttle_event__type.tp_new = PyType_GenericNew;
  257. err = PyType_Ready(&pyrf_mmap_event__type);
  258. if (err < 0)
  259. goto out;
  260. err = PyType_Ready(&pyrf_lost_event__type);
  261. if (err < 0)
  262. goto out;
  263. err = PyType_Ready(&pyrf_task_event__type);
  264. if (err < 0)
  265. goto out;
  266. err = PyType_Ready(&pyrf_comm_event__type);
  267. if (err < 0)
  268. goto out;
  269. err = PyType_Ready(&pyrf_throttle_event__type);
  270. if (err < 0)
  271. goto out;
  272. err = PyType_Ready(&pyrf_read_event__type);
  273. if (err < 0)
  274. goto out;
  275. err = PyType_Ready(&pyrf_sample_event__type);
  276. if (err < 0)
  277. goto out;
  278. out:
  279. return err;
  280. }
  281. static PyTypeObject *pyrf_event__type[] = {
  282. [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
  283. [PERF_RECORD_LOST] = &pyrf_lost_event__type,
  284. [PERF_RECORD_COMM] = &pyrf_comm_event__type,
  285. [PERF_RECORD_EXIT] = &pyrf_task_event__type,
  286. [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
  287. [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
  288. [PERF_RECORD_FORK] = &pyrf_task_event__type,
  289. [PERF_RECORD_READ] = &pyrf_read_event__type,
  290. [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
  291. };
  292. static PyObject *pyrf_event__new(union perf_event *event)
  293. {
  294. struct pyrf_event *pevent;
  295. PyTypeObject *ptype;
  296. if (event->header.type < PERF_RECORD_MMAP ||
  297. event->header.type > PERF_RECORD_SAMPLE)
  298. return NULL;
  299. ptype = pyrf_event__type[event->header.type];
  300. pevent = PyObject_New(struct pyrf_event, ptype);
  301. if (pevent != NULL)
  302. memcpy(&pevent->event, event, event->header.size);
  303. return (PyObject *)pevent;
  304. }
  305. struct pyrf_cpu_map {
  306. PyObject_HEAD
  307. struct cpu_map *cpus;
  308. };
  309. static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
  310. PyObject *args, PyObject *kwargs)
  311. {
  312. static char *kwlist[] = { "cpustr", NULL };
  313. char *cpustr = NULL;
  314. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
  315. kwlist, &cpustr))
  316. return -1;
  317. pcpus->cpus = cpu_map__new(cpustr);
  318. if (pcpus->cpus == NULL)
  319. return -1;
  320. return 0;
  321. }
  322. static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
  323. {
  324. cpu_map__delete(pcpus->cpus);
  325. pcpus->ob_type->tp_free((PyObject*)pcpus);
  326. }
  327. static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
  328. {
  329. struct pyrf_cpu_map *pcpus = (void *)obj;
  330. return pcpus->cpus->nr;
  331. }
  332. static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
  333. {
  334. struct pyrf_cpu_map *pcpus = (void *)obj;
  335. if (i >= pcpus->cpus->nr)
  336. return NULL;
  337. return Py_BuildValue("i", pcpus->cpus->map[i]);
  338. }
  339. static PySequenceMethods pyrf_cpu_map__sequence_methods = {
  340. .sq_length = pyrf_cpu_map__length,
  341. .sq_item = pyrf_cpu_map__item,
  342. };
  343. static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
  344. static PyTypeObject pyrf_cpu_map__type = {
  345. PyVarObject_HEAD_INIT(NULL, 0)
  346. .tp_name = "perf.cpu_map",
  347. .tp_basicsize = sizeof(struct pyrf_cpu_map),
  348. .tp_dealloc = (destructor)pyrf_cpu_map__delete,
  349. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  350. .tp_doc = pyrf_cpu_map__doc,
  351. .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
  352. .tp_init = (initproc)pyrf_cpu_map__init,
  353. };
  354. static int pyrf_cpu_map__setup_types(void)
  355. {
  356. pyrf_cpu_map__type.tp_new = PyType_GenericNew;
  357. return PyType_Ready(&pyrf_cpu_map__type);
  358. }
  359. struct pyrf_thread_map {
  360. PyObject_HEAD
  361. struct thread_map *threads;
  362. };
  363. static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
  364. PyObject *args, PyObject *kwargs)
  365. {
  366. static char *kwlist[] = { "pid", "tid", "uid", NULL };
  367. int pid = -1, tid = -1, uid = UINT_MAX;
  368. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
  369. kwlist, &pid, &tid, &uid))
  370. return -1;
  371. pthreads->threads = thread_map__new(pid, tid, uid);
  372. if (pthreads->threads == NULL)
  373. return -1;
  374. return 0;
  375. }
  376. static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
  377. {
  378. thread_map__delete(pthreads->threads);
  379. pthreads->ob_type->tp_free((PyObject*)pthreads);
  380. }
  381. static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
  382. {
  383. struct pyrf_thread_map *pthreads = (void *)obj;
  384. return pthreads->threads->nr;
  385. }
  386. static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
  387. {
  388. struct pyrf_thread_map *pthreads = (void *)obj;
  389. if (i >= pthreads->threads->nr)
  390. return NULL;
  391. return Py_BuildValue("i", pthreads->threads->map[i]);
  392. }
  393. static PySequenceMethods pyrf_thread_map__sequence_methods = {
  394. .sq_length = pyrf_thread_map__length,
  395. .sq_item = pyrf_thread_map__item,
  396. };
  397. static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
  398. static PyTypeObject pyrf_thread_map__type = {
  399. PyVarObject_HEAD_INIT(NULL, 0)
  400. .tp_name = "perf.thread_map",
  401. .tp_basicsize = sizeof(struct pyrf_thread_map),
  402. .tp_dealloc = (destructor)pyrf_thread_map__delete,
  403. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  404. .tp_doc = pyrf_thread_map__doc,
  405. .tp_as_sequence = &pyrf_thread_map__sequence_methods,
  406. .tp_init = (initproc)pyrf_thread_map__init,
  407. };
  408. static int pyrf_thread_map__setup_types(void)
  409. {
  410. pyrf_thread_map__type.tp_new = PyType_GenericNew;
  411. return PyType_Ready(&pyrf_thread_map__type);
  412. }
  413. struct pyrf_evsel {
  414. PyObject_HEAD
  415. struct perf_evsel evsel;
  416. };
  417. static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
  418. PyObject *args, PyObject *kwargs)
  419. {
  420. struct perf_event_attr attr = {
  421. .type = PERF_TYPE_HARDWARE,
  422. .config = PERF_COUNT_HW_CPU_CYCLES,
  423. .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
  424. };
  425. static char *kwlist[] = {
  426. "type",
  427. "config",
  428. "sample_freq",
  429. "sample_period",
  430. "sample_type",
  431. "read_format",
  432. "disabled",
  433. "inherit",
  434. "pinned",
  435. "exclusive",
  436. "exclude_user",
  437. "exclude_kernel",
  438. "exclude_hv",
  439. "exclude_idle",
  440. "mmap",
  441. "comm",
  442. "freq",
  443. "inherit_stat",
  444. "enable_on_exec",
  445. "task",
  446. "watermark",
  447. "precise_ip",
  448. "mmap_data",
  449. "sample_id_all",
  450. "wakeup_events",
  451. "bp_type",
  452. "bp_addr",
  453. "bp_len",
  454. NULL
  455. };
  456. u64 sample_period = 0;
  457. u32 disabled = 0,
  458. inherit = 0,
  459. pinned = 0,
  460. exclusive = 0,
  461. exclude_user = 0,
  462. exclude_kernel = 0,
  463. exclude_hv = 0,
  464. exclude_idle = 0,
  465. mmap = 0,
  466. comm = 0,
  467. freq = 1,
  468. inherit_stat = 0,
  469. enable_on_exec = 0,
  470. task = 0,
  471. watermark = 0,
  472. precise_ip = 0,
  473. mmap_data = 0,
  474. sample_id_all = 1;
  475. int idx = 0;
  476. if (!PyArg_ParseTupleAndKeywords(args, kwargs,
  477. "|iKiKKiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
  478. &attr.type, &attr.config, &attr.sample_freq,
  479. &sample_period, &attr.sample_type,
  480. &attr.read_format, &disabled, &inherit,
  481. &pinned, &exclusive, &exclude_user,
  482. &exclude_kernel, &exclude_hv, &exclude_idle,
  483. &mmap, &comm, &freq, &inherit_stat,
  484. &enable_on_exec, &task, &watermark,
  485. &precise_ip, &mmap_data, &sample_id_all,
  486. &attr.wakeup_events, &attr.bp_type,
  487. &attr.bp_addr, &attr.bp_len, &idx))
  488. return -1;
  489. /* union... */
  490. if (sample_period != 0) {
  491. if (attr.sample_freq != 0)
  492. return -1; /* FIXME: throw right exception */
  493. attr.sample_period = sample_period;
  494. }
  495. /* Bitfields */
  496. attr.disabled = disabled;
  497. attr.inherit = inherit;
  498. attr.pinned = pinned;
  499. attr.exclusive = exclusive;
  500. attr.exclude_user = exclude_user;
  501. attr.exclude_kernel = exclude_kernel;
  502. attr.exclude_hv = exclude_hv;
  503. attr.exclude_idle = exclude_idle;
  504. attr.mmap = mmap;
  505. attr.comm = comm;
  506. attr.freq = freq;
  507. attr.inherit_stat = inherit_stat;
  508. attr.enable_on_exec = enable_on_exec;
  509. attr.task = task;
  510. attr.watermark = watermark;
  511. attr.precise_ip = precise_ip;
  512. attr.mmap_data = mmap_data;
  513. attr.sample_id_all = sample_id_all;
  514. perf_evsel__init(&pevsel->evsel, &attr, idx);
  515. return 0;
  516. }
  517. static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
  518. {
  519. perf_evsel__exit(&pevsel->evsel);
  520. pevsel->ob_type->tp_free((PyObject*)pevsel);
  521. }
  522. static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
  523. PyObject *args, PyObject *kwargs)
  524. {
  525. struct perf_evsel *evsel = &pevsel->evsel;
  526. struct cpu_map *cpus = NULL;
  527. struct thread_map *threads = NULL;
  528. PyObject *pcpus = NULL, *pthreads = NULL;
  529. int group = 0, inherit = 0;
  530. static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
  531. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
  532. &pcpus, &pthreads, &group, &inherit))
  533. return NULL;
  534. if (pthreads != NULL)
  535. threads = ((struct pyrf_thread_map *)pthreads)->threads;
  536. if (pcpus != NULL)
  537. cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  538. evsel->attr.inherit = inherit;
  539. /*
  540. * This will group just the fds for this single evsel, to group
  541. * multiple events, use evlist.open().
  542. */
  543. if (perf_evsel__open(evsel, cpus, threads, group, NULL) < 0) {
  544. PyErr_SetFromErrno(PyExc_OSError);
  545. return NULL;
  546. }
  547. Py_INCREF(Py_None);
  548. return Py_None;
  549. }
  550. static PyMethodDef pyrf_evsel__methods[] = {
  551. {
  552. .ml_name = "open",
  553. .ml_meth = (PyCFunction)pyrf_evsel__open,
  554. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  555. .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
  556. },
  557. { .ml_name = NULL, }
  558. };
  559. static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
  560. static PyTypeObject pyrf_evsel__type = {
  561. PyVarObject_HEAD_INIT(NULL, 0)
  562. .tp_name = "perf.evsel",
  563. .tp_basicsize = sizeof(struct pyrf_evsel),
  564. .tp_dealloc = (destructor)pyrf_evsel__delete,
  565. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  566. .tp_doc = pyrf_evsel__doc,
  567. .tp_methods = pyrf_evsel__methods,
  568. .tp_init = (initproc)pyrf_evsel__init,
  569. };
  570. static int pyrf_evsel__setup_types(void)
  571. {
  572. pyrf_evsel__type.tp_new = PyType_GenericNew;
  573. return PyType_Ready(&pyrf_evsel__type);
  574. }
  575. struct pyrf_evlist {
  576. PyObject_HEAD
  577. struct perf_evlist evlist;
  578. };
  579. static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
  580. PyObject *args, PyObject *kwargs __used)
  581. {
  582. PyObject *pcpus = NULL, *pthreads = NULL;
  583. struct cpu_map *cpus;
  584. struct thread_map *threads;
  585. if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
  586. return -1;
  587. threads = ((struct pyrf_thread_map *)pthreads)->threads;
  588. cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  589. perf_evlist__init(&pevlist->evlist, cpus, threads);
  590. return 0;
  591. }
  592. static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
  593. {
  594. perf_evlist__exit(&pevlist->evlist);
  595. pevlist->ob_type->tp_free((PyObject*)pevlist);
  596. }
  597. static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
  598. PyObject *args, PyObject *kwargs)
  599. {
  600. struct perf_evlist *evlist = &pevlist->evlist;
  601. static char *kwlist[] = { "pages", "overwrite", NULL };
  602. int pages = 128, overwrite = false;
  603. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
  604. &pages, &overwrite))
  605. return NULL;
  606. if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
  607. PyErr_SetFromErrno(PyExc_OSError);
  608. return NULL;
  609. }
  610. Py_INCREF(Py_None);
  611. return Py_None;
  612. }
  613. static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
  614. PyObject *args, PyObject *kwargs)
  615. {
  616. struct perf_evlist *evlist = &pevlist->evlist;
  617. static char *kwlist[] = { "timeout", NULL };
  618. int timeout = -1, n;
  619. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
  620. return NULL;
  621. n = poll(evlist->pollfd, evlist->nr_fds, timeout);
  622. if (n < 0) {
  623. PyErr_SetFromErrno(PyExc_OSError);
  624. return NULL;
  625. }
  626. return Py_BuildValue("i", n);
  627. }
  628. static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
  629. PyObject *args __used, PyObject *kwargs __used)
  630. {
  631. struct perf_evlist *evlist = &pevlist->evlist;
  632. PyObject *list = PyList_New(0);
  633. int i;
  634. for (i = 0; i < evlist->nr_fds; ++i) {
  635. PyObject *file;
  636. FILE *fp = fdopen(evlist->pollfd[i].fd, "r");
  637. if (fp == NULL)
  638. goto free_list;
  639. file = PyFile_FromFile(fp, "perf", "r", NULL);
  640. if (file == NULL)
  641. goto free_list;
  642. if (PyList_Append(list, file) != 0) {
  643. Py_DECREF(file);
  644. goto free_list;
  645. }
  646. Py_DECREF(file);
  647. }
  648. return list;
  649. free_list:
  650. return PyErr_NoMemory();
  651. }
  652. static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
  653. PyObject *args, PyObject *kwargs __used)
  654. {
  655. struct perf_evlist *evlist = &pevlist->evlist;
  656. PyObject *pevsel;
  657. struct perf_evsel *evsel;
  658. if (!PyArg_ParseTuple(args, "O", &pevsel))
  659. return NULL;
  660. Py_INCREF(pevsel);
  661. evsel = &((struct pyrf_evsel *)pevsel)->evsel;
  662. evsel->idx = evlist->nr_entries;
  663. perf_evlist__add(evlist, evsel);
  664. return Py_BuildValue("i", evlist->nr_entries);
  665. }
  666. static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
  667. PyObject *args, PyObject *kwargs)
  668. {
  669. struct perf_evlist *evlist = &pevlist->evlist;
  670. union perf_event *event;
  671. int sample_id_all = 1, cpu;
  672. static char *kwlist[] = { "cpu", "sample_id_all", NULL };
  673. int err;
  674. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
  675. &cpu, &sample_id_all))
  676. return NULL;
  677. event = perf_evlist__mmap_read(evlist, cpu);
  678. if (event != NULL) {
  679. struct perf_evsel *first;
  680. PyObject *pyevent = pyrf_event__new(event);
  681. struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
  682. if (pyevent == NULL)
  683. return PyErr_NoMemory();
  684. first = list_entry(evlist->entries.next, struct perf_evsel, node);
  685. err = perf_event__parse_sample(event, first->attr.sample_type,
  686. perf_evsel__sample_size(first),
  687. sample_id_all, &pevent->sample, false);
  688. if (err)
  689. return PyErr_Format(PyExc_OSError,
  690. "perf: can't parse sample, err=%d", err);
  691. return pyevent;
  692. }
  693. Py_INCREF(Py_None);
  694. return Py_None;
  695. }
  696. static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  697. PyObject *args, PyObject *kwargs)
  698. {
  699. struct perf_evlist *evlist = &pevlist->evlist;
  700. int group = 0;
  701. static char *kwlist[] = { "group", NULL };
  702. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
  703. return NULL;
  704. if (perf_evlist__open(evlist, group) < 0) {
  705. PyErr_SetFromErrno(PyExc_OSError);
  706. return NULL;
  707. }
  708. Py_INCREF(Py_None);
  709. return Py_None;
  710. }
  711. static PyMethodDef pyrf_evlist__methods[] = {
  712. {
  713. .ml_name = "mmap",
  714. .ml_meth = (PyCFunction)pyrf_evlist__mmap,
  715. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  716. .ml_doc = PyDoc_STR("mmap the file descriptor table.")
  717. },
  718. {
  719. .ml_name = "open",
  720. .ml_meth = (PyCFunction)pyrf_evlist__open,
  721. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  722. .ml_doc = PyDoc_STR("open the file descriptors.")
  723. },
  724. {
  725. .ml_name = "poll",
  726. .ml_meth = (PyCFunction)pyrf_evlist__poll,
  727. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  728. .ml_doc = PyDoc_STR("poll the file descriptor table.")
  729. },
  730. {
  731. .ml_name = "get_pollfd",
  732. .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
  733. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  734. .ml_doc = PyDoc_STR("get the poll file descriptor table.")
  735. },
  736. {
  737. .ml_name = "add",
  738. .ml_meth = (PyCFunction)pyrf_evlist__add,
  739. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  740. .ml_doc = PyDoc_STR("adds an event selector to the list.")
  741. },
  742. {
  743. .ml_name = "read_on_cpu",
  744. .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
  745. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  746. .ml_doc = PyDoc_STR("reads an event.")
  747. },
  748. { .ml_name = NULL, }
  749. };
  750. static Py_ssize_t pyrf_evlist__length(PyObject *obj)
  751. {
  752. struct pyrf_evlist *pevlist = (void *)obj;
  753. return pevlist->evlist.nr_entries;
  754. }
  755. static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
  756. {
  757. struct pyrf_evlist *pevlist = (void *)obj;
  758. struct perf_evsel *pos;
  759. if (i >= pevlist->evlist.nr_entries)
  760. return NULL;
  761. list_for_each_entry(pos, &pevlist->evlist.entries, node)
  762. if (i-- == 0)
  763. break;
  764. return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
  765. }
  766. static PySequenceMethods pyrf_evlist__sequence_methods = {
  767. .sq_length = pyrf_evlist__length,
  768. .sq_item = pyrf_evlist__item,
  769. };
  770. static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
  771. static PyTypeObject pyrf_evlist__type = {
  772. PyVarObject_HEAD_INIT(NULL, 0)
  773. .tp_name = "perf.evlist",
  774. .tp_basicsize = sizeof(struct pyrf_evlist),
  775. .tp_dealloc = (destructor)pyrf_evlist__delete,
  776. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  777. .tp_as_sequence = &pyrf_evlist__sequence_methods,
  778. .tp_doc = pyrf_evlist__doc,
  779. .tp_methods = pyrf_evlist__methods,
  780. .tp_init = (initproc)pyrf_evlist__init,
  781. };
  782. static int pyrf_evlist__setup_types(void)
  783. {
  784. pyrf_evlist__type.tp_new = PyType_GenericNew;
  785. return PyType_Ready(&pyrf_evlist__type);
  786. }
  787. static struct {
  788. const char *name;
  789. int value;
  790. } perf__constants[] = {
  791. { "TYPE_HARDWARE", PERF_TYPE_HARDWARE },
  792. { "TYPE_SOFTWARE", PERF_TYPE_SOFTWARE },
  793. { "TYPE_TRACEPOINT", PERF_TYPE_TRACEPOINT },
  794. { "TYPE_HW_CACHE", PERF_TYPE_HW_CACHE },
  795. { "TYPE_RAW", PERF_TYPE_RAW },
  796. { "TYPE_BREAKPOINT", PERF_TYPE_BREAKPOINT },
  797. { "COUNT_HW_CPU_CYCLES", PERF_COUNT_HW_CPU_CYCLES },
  798. { "COUNT_HW_INSTRUCTIONS", PERF_COUNT_HW_INSTRUCTIONS },
  799. { "COUNT_HW_CACHE_REFERENCES", PERF_COUNT_HW_CACHE_REFERENCES },
  800. { "COUNT_HW_CACHE_MISSES", PERF_COUNT_HW_CACHE_MISSES },
  801. { "COUNT_HW_BRANCH_INSTRUCTIONS", PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  802. { "COUNT_HW_BRANCH_MISSES", PERF_COUNT_HW_BRANCH_MISSES },
  803. { "COUNT_HW_BUS_CYCLES", PERF_COUNT_HW_BUS_CYCLES },
  804. { "COUNT_HW_CACHE_L1D", PERF_COUNT_HW_CACHE_L1D },
  805. { "COUNT_HW_CACHE_L1I", PERF_COUNT_HW_CACHE_L1I },
  806. { "COUNT_HW_CACHE_LL", PERF_COUNT_HW_CACHE_LL },
  807. { "COUNT_HW_CACHE_DTLB", PERF_COUNT_HW_CACHE_DTLB },
  808. { "COUNT_HW_CACHE_ITLB", PERF_COUNT_HW_CACHE_ITLB },
  809. { "COUNT_HW_CACHE_BPU", PERF_COUNT_HW_CACHE_BPU },
  810. { "COUNT_HW_CACHE_OP_READ", PERF_COUNT_HW_CACHE_OP_READ },
  811. { "COUNT_HW_CACHE_OP_WRITE", PERF_COUNT_HW_CACHE_OP_WRITE },
  812. { "COUNT_HW_CACHE_OP_PREFETCH", PERF_COUNT_HW_CACHE_OP_PREFETCH },
  813. { "COUNT_HW_CACHE_RESULT_ACCESS", PERF_COUNT_HW_CACHE_RESULT_ACCESS },
  814. { "COUNT_HW_CACHE_RESULT_MISS", PERF_COUNT_HW_CACHE_RESULT_MISS },
  815. { "COUNT_HW_STALLED_CYCLES_FRONTEND", PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  816. { "COUNT_HW_STALLED_CYCLES_BACKEND", PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  817. { "COUNT_SW_CPU_CLOCK", PERF_COUNT_SW_CPU_CLOCK },
  818. { "COUNT_SW_TASK_CLOCK", PERF_COUNT_SW_TASK_CLOCK },
  819. { "COUNT_SW_PAGE_FAULTS", PERF_COUNT_SW_PAGE_FAULTS },
  820. { "COUNT_SW_CONTEXT_SWITCHES", PERF_COUNT_SW_CONTEXT_SWITCHES },
  821. { "COUNT_SW_CPU_MIGRATIONS", PERF_COUNT_SW_CPU_MIGRATIONS },
  822. { "COUNT_SW_PAGE_FAULTS_MIN", PERF_COUNT_SW_PAGE_FAULTS_MIN },
  823. { "COUNT_SW_PAGE_FAULTS_MAJ", PERF_COUNT_SW_PAGE_FAULTS_MAJ },
  824. { "COUNT_SW_ALIGNMENT_FAULTS", PERF_COUNT_SW_ALIGNMENT_FAULTS },
  825. { "COUNT_SW_EMULATION_FAULTS", PERF_COUNT_SW_EMULATION_FAULTS },
  826. { "SAMPLE_IP", PERF_SAMPLE_IP },
  827. { "SAMPLE_TID", PERF_SAMPLE_TID },
  828. { "SAMPLE_TIME", PERF_SAMPLE_TIME },
  829. { "SAMPLE_ADDR", PERF_SAMPLE_ADDR },
  830. { "SAMPLE_READ", PERF_SAMPLE_READ },
  831. { "SAMPLE_CALLCHAIN", PERF_SAMPLE_CALLCHAIN },
  832. { "SAMPLE_ID", PERF_SAMPLE_ID },
  833. { "SAMPLE_CPU", PERF_SAMPLE_CPU },
  834. { "SAMPLE_PERIOD", PERF_SAMPLE_PERIOD },
  835. { "SAMPLE_STREAM_ID", PERF_SAMPLE_STREAM_ID },
  836. { "SAMPLE_RAW", PERF_SAMPLE_RAW },
  837. { "FORMAT_TOTAL_TIME_ENABLED", PERF_FORMAT_TOTAL_TIME_ENABLED },
  838. { "FORMAT_TOTAL_TIME_RUNNING", PERF_FORMAT_TOTAL_TIME_RUNNING },
  839. { "FORMAT_ID", PERF_FORMAT_ID },
  840. { "FORMAT_GROUP", PERF_FORMAT_GROUP },
  841. { "RECORD_MMAP", PERF_RECORD_MMAP },
  842. { "RECORD_LOST", PERF_RECORD_LOST },
  843. { "RECORD_COMM", PERF_RECORD_COMM },
  844. { "RECORD_EXIT", PERF_RECORD_EXIT },
  845. { "RECORD_THROTTLE", PERF_RECORD_THROTTLE },
  846. { "RECORD_UNTHROTTLE", PERF_RECORD_UNTHROTTLE },
  847. { "RECORD_FORK", PERF_RECORD_FORK },
  848. { "RECORD_READ", PERF_RECORD_READ },
  849. { "RECORD_SAMPLE", PERF_RECORD_SAMPLE },
  850. { .name = NULL, },
  851. };
  852. static PyMethodDef perf__methods[] = {
  853. { .ml_name = NULL, }
  854. };
  855. PyMODINIT_FUNC initperf(void)
  856. {
  857. PyObject *obj;
  858. int i;
  859. PyObject *dict, *module = Py_InitModule("perf", perf__methods);
  860. if (module == NULL ||
  861. pyrf_event__setup_types() < 0 ||
  862. pyrf_evlist__setup_types() < 0 ||
  863. pyrf_evsel__setup_types() < 0 ||
  864. pyrf_thread_map__setup_types() < 0 ||
  865. pyrf_cpu_map__setup_types() < 0)
  866. return;
  867. Py_INCREF(&pyrf_evlist__type);
  868. PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
  869. Py_INCREF(&pyrf_evsel__type);
  870. PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
  871. Py_INCREF(&pyrf_thread_map__type);
  872. PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
  873. Py_INCREF(&pyrf_cpu_map__type);
  874. PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
  875. dict = PyModule_GetDict(module);
  876. if (dict == NULL)
  877. goto error;
  878. for (i = 0; perf__constants[i].name != NULL; i++) {
  879. obj = PyInt_FromLong(perf__constants[i].value);
  880. if (obj == NULL)
  881. goto error;
  882. PyDict_SetItemString(dict, perf__constants[i].name, obj);
  883. Py_DECREF(obj);
  884. }
  885. error:
  886. if (PyErr_Occurred())
  887. PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
  888. }