python.c 30 KB

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