python.c 36 KB

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