python.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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) < 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. PyObject *pyevent = pyrf_event__new(event);
  680. struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
  681. if (pyevent == NULL)
  682. return PyErr_NoMemory();
  683. err = perf_evlist__parse_sample(evlist, event, &pevent->sample, false);
  684. if (err)
  685. return PyErr_Format(PyExc_OSError,
  686. "perf: can't parse sample, err=%d", err);
  687. return pyevent;
  688. }
  689. Py_INCREF(Py_None);
  690. return Py_None;
  691. }
  692. static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  693. PyObject *args, PyObject *kwargs)
  694. {
  695. struct perf_evlist *evlist = &pevlist->evlist;
  696. int group = 0;
  697. static char *kwlist[] = { "group", NULL };
  698. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
  699. return NULL;
  700. if (group)
  701. perf_evlist__set_leader(evlist);
  702. if (perf_evlist__open(evlist) < 0) {
  703. PyErr_SetFromErrno(PyExc_OSError);
  704. return NULL;
  705. }
  706. Py_INCREF(Py_None);
  707. return Py_None;
  708. }
  709. static PyMethodDef pyrf_evlist__methods[] = {
  710. {
  711. .ml_name = "mmap",
  712. .ml_meth = (PyCFunction)pyrf_evlist__mmap,
  713. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  714. .ml_doc = PyDoc_STR("mmap the file descriptor table.")
  715. },
  716. {
  717. .ml_name = "open",
  718. .ml_meth = (PyCFunction)pyrf_evlist__open,
  719. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  720. .ml_doc = PyDoc_STR("open the file descriptors.")
  721. },
  722. {
  723. .ml_name = "poll",
  724. .ml_meth = (PyCFunction)pyrf_evlist__poll,
  725. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  726. .ml_doc = PyDoc_STR("poll the file descriptor table.")
  727. },
  728. {
  729. .ml_name = "get_pollfd",
  730. .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
  731. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  732. .ml_doc = PyDoc_STR("get the poll file descriptor table.")
  733. },
  734. {
  735. .ml_name = "add",
  736. .ml_meth = (PyCFunction)pyrf_evlist__add,
  737. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  738. .ml_doc = PyDoc_STR("adds an event selector to the list.")
  739. },
  740. {
  741. .ml_name = "read_on_cpu",
  742. .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
  743. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  744. .ml_doc = PyDoc_STR("reads an event.")
  745. },
  746. { .ml_name = NULL, }
  747. };
  748. static Py_ssize_t pyrf_evlist__length(PyObject *obj)
  749. {
  750. struct pyrf_evlist *pevlist = (void *)obj;
  751. return pevlist->evlist.nr_entries;
  752. }
  753. static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
  754. {
  755. struct pyrf_evlist *pevlist = (void *)obj;
  756. struct perf_evsel *pos;
  757. if (i >= pevlist->evlist.nr_entries)
  758. return NULL;
  759. list_for_each_entry(pos, &pevlist->evlist.entries, node)
  760. if (i-- == 0)
  761. break;
  762. return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
  763. }
  764. static PySequenceMethods pyrf_evlist__sequence_methods = {
  765. .sq_length = pyrf_evlist__length,
  766. .sq_item = pyrf_evlist__item,
  767. };
  768. static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
  769. static PyTypeObject pyrf_evlist__type = {
  770. PyVarObject_HEAD_INIT(NULL, 0)
  771. .tp_name = "perf.evlist",
  772. .tp_basicsize = sizeof(struct pyrf_evlist),
  773. .tp_dealloc = (destructor)pyrf_evlist__delete,
  774. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  775. .tp_as_sequence = &pyrf_evlist__sequence_methods,
  776. .tp_doc = pyrf_evlist__doc,
  777. .tp_methods = pyrf_evlist__methods,
  778. .tp_init = (initproc)pyrf_evlist__init,
  779. };
  780. static int pyrf_evlist__setup_types(void)
  781. {
  782. pyrf_evlist__type.tp_new = PyType_GenericNew;
  783. return PyType_Ready(&pyrf_evlist__type);
  784. }
  785. static struct {
  786. const char *name;
  787. int value;
  788. } perf__constants[] = {
  789. { "TYPE_HARDWARE", PERF_TYPE_HARDWARE },
  790. { "TYPE_SOFTWARE", PERF_TYPE_SOFTWARE },
  791. { "TYPE_TRACEPOINT", PERF_TYPE_TRACEPOINT },
  792. { "TYPE_HW_CACHE", PERF_TYPE_HW_CACHE },
  793. { "TYPE_RAW", PERF_TYPE_RAW },
  794. { "TYPE_BREAKPOINT", PERF_TYPE_BREAKPOINT },
  795. { "COUNT_HW_CPU_CYCLES", PERF_COUNT_HW_CPU_CYCLES },
  796. { "COUNT_HW_INSTRUCTIONS", PERF_COUNT_HW_INSTRUCTIONS },
  797. { "COUNT_HW_CACHE_REFERENCES", PERF_COUNT_HW_CACHE_REFERENCES },
  798. { "COUNT_HW_CACHE_MISSES", PERF_COUNT_HW_CACHE_MISSES },
  799. { "COUNT_HW_BRANCH_INSTRUCTIONS", PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  800. { "COUNT_HW_BRANCH_MISSES", PERF_COUNT_HW_BRANCH_MISSES },
  801. { "COUNT_HW_BUS_CYCLES", PERF_COUNT_HW_BUS_CYCLES },
  802. { "COUNT_HW_CACHE_L1D", PERF_COUNT_HW_CACHE_L1D },
  803. { "COUNT_HW_CACHE_L1I", PERF_COUNT_HW_CACHE_L1I },
  804. { "COUNT_HW_CACHE_LL", PERF_COUNT_HW_CACHE_LL },
  805. { "COUNT_HW_CACHE_DTLB", PERF_COUNT_HW_CACHE_DTLB },
  806. { "COUNT_HW_CACHE_ITLB", PERF_COUNT_HW_CACHE_ITLB },
  807. { "COUNT_HW_CACHE_BPU", PERF_COUNT_HW_CACHE_BPU },
  808. { "COUNT_HW_CACHE_OP_READ", PERF_COUNT_HW_CACHE_OP_READ },
  809. { "COUNT_HW_CACHE_OP_WRITE", PERF_COUNT_HW_CACHE_OP_WRITE },
  810. { "COUNT_HW_CACHE_OP_PREFETCH", PERF_COUNT_HW_CACHE_OP_PREFETCH },
  811. { "COUNT_HW_CACHE_RESULT_ACCESS", PERF_COUNT_HW_CACHE_RESULT_ACCESS },
  812. { "COUNT_HW_CACHE_RESULT_MISS", PERF_COUNT_HW_CACHE_RESULT_MISS },
  813. { "COUNT_HW_STALLED_CYCLES_FRONTEND", PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  814. { "COUNT_HW_STALLED_CYCLES_BACKEND", PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  815. { "COUNT_SW_CPU_CLOCK", PERF_COUNT_SW_CPU_CLOCK },
  816. { "COUNT_SW_TASK_CLOCK", PERF_COUNT_SW_TASK_CLOCK },
  817. { "COUNT_SW_PAGE_FAULTS", PERF_COUNT_SW_PAGE_FAULTS },
  818. { "COUNT_SW_CONTEXT_SWITCHES", PERF_COUNT_SW_CONTEXT_SWITCHES },
  819. { "COUNT_SW_CPU_MIGRATIONS", PERF_COUNT_SW_CPU_MIGRATIONS },
  820. { "COUNT_SW_PAGE_FAULTS_MIN", PERF_COUNT_SW_PAGE_FAULTS_MIN },
  821. { "COUNT_SW_PAGE_FAULTS_MAJ", PERF_COUNT_SW_PAGE_FAULTS_MAJ },
  822. { "COUNT_SW_ALIGNMENT_FAULTS", PERF_COUNT_SW_ALIGNMENT_FAULTS },
  823. { "COUNT_SW_EMULATION_FAULTS", PERF_COUNT_SW_EMULATION_FAULTS },
  824. { "SAMPLE_IP", PERF_SAMPLE_IP },
  825. { "SAMPLE_TID", PERF_SAMPLE_TID },
  826. { "SAMPLE_TIME", PERF_SAMPLE_TIME },
  827. { "SAMPLE_ADDR", PERF_SAMPLE_ADDR },
  828. { "SAMPLE_READ", PERF_SAMPLE_READ },
  829. { "SAMPLE_CALLCHAIN", PERF_SAMPLE_CALLCHAIN },
  830. { "SAMPLE_ID", PERF_SAMPLE_ID },
  831. { "SAMPLE_CPU", PERF_SAMPLE_CPU },
  832. { "SAMPLE_PERIOD", PERF_SAMPLE_PERIOD },
  833. { "SAMPLE_STREAM_ID", PERF_SAMPLE_STREAM_ID },
  834. { "SAMPLE_RAW", PERF_SAMPLE_RAW },
  835. { "FORMAT_TOTAL_TIME_ENABLED", PERF_FORMAT_TOTAL_TIME_ENABLED },
  836. { "FORMAT_TOTAL_TIME_RUNNING", PERF_FORMAT_TOTAL_TIME_RUNNING },
  837. { "FORMAT_ID", PERF_FORMAT_ID },
  838. { "FORMAT_GROUP", PERF_FORMAT_GROUP },
  839. { "RECORD_MMAP", PERF_RECORD_MMAP },
  840. { "RECORD_LOST", PERF_RECORD_LOST },
  841. { "RECORD_COMM", PERF_RECORD_COMM },
  842. { "RECORD_EXIT", PERF_RECORD_EXIT },
  843. { "RECORD_THROTTLE", PERF_RECORD_THROTTLE },
  844. { "RECORD_UNTHROTTLE", PERF_RECORD_UNTHROTTLE },
  845. { "RECORD_FORK", PERF_RECORD_FORK },
  846. { "RECORD_READ", PERF_RECORD_READ },
  847. { "RECORD_SAMPLE", PERF_RECORD_SAMPLE },
  848. { .name = NULL, },
  849. };
  850. static PyMethodDef perf__methods[] = {
  851. { .ml_name = NULL, }
  852. };
  853. PyMODINIT_FUNC initperf(void)
  854. {
  855. PyObject *obj;
  856. int i;
  857. PyObject *dict, *module = Py_InitModule("perf", perf__methods);
  858. if (module == NULL ||
  859. pyrf_event__setup_types() < 0 ||
  860. pyrf_evlist__setup_types() < 0 ||
  861. pyrf_evsel__setup_types() < 0 ||
  862. pyrf_thread_map__setup_types() < 0 ||
  863. pyrf_cpu_map__setup_types() < 0)
  864. return;
  865. Py_INCREF(&pyrf_evlist__type);
  866. PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
  867. Py_INCREF(&pyrf_evsel__type);
  868. PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
  869. Py_INCREF(&pyrf_thread_map__type);
  870. PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
  871. Py_INCREF(&pyrf_cpu_map__type);
  872. PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
  873. dict = PyModule_GetDict(module);
  874. if (dict == NULL)
  875. goto error;
  876. for (i = 0; perf__constants[i].name != NULL; i++) {
  877. obj = PyInt_FromLong(perf__constants[i].value);
  878. if (obj == NULL)
  879. goto error;
  880. PyDict_SetItemString(dict, perf__constants[i].name, obj);
  881. Py_DECREF(obj);
  882. }
  883. error:
  884. if (PyErr_Occurred())
  885. PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
  886. }