python.c 30 KB

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