python.c 36 KB

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