python.c 35 KB

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