python.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. return Py_None;
  858. pevent->evsel = evsel;
  859. err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
  860. /* Consume the even only after we parsed it out. */
  861. perf_mmap__consume(md);
  862. if (err)
  863. return PyErr_Format(PyExc_OSError,
  864. "perf: can't parse sample, err=%d", err);
  865. return pyevent;
  866. }
  867. end:
  868. Py_INCREF(Py_None);
  869. return Py_None;
  870. }
  871. static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  872. PyObject *args, PyObject *kwargs)
  873. {
  874. struct perf_evlist *evlist = &pevlist->evlist;
  875. int group = 0;
  876. static char *kwlist[] = { "group", NULL };
  877. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
  878. return NULL;
  879. if (group)
  880. perf_evlist__set_leader(evlist);
  881. if (perf_evlist__open(evlist) < 0) {
  882. PyErr_SetFromErrno(PyExc_OSError);
  883. return NULL;
  884. }
  885. Py_INCREF(Py_None);
  886. return Py_None;
  887. }
  888. static PyMethodDef pyrf_evlist__methods[] = {
  889. {
  890. .ml_name = "mmap",
  891. .ml_meth = (PyCFunction)pyrf_evlist__mmap,
  892. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  893. .ml_doc = PyDoc_STR("mmap the file descriptor table.")
  894. },
  895. {
  896. .ml_name = "open",
  897. .ml_meth = (PyCFunction)pyrf_evlist__open,
  898. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  899. .ml_doc = PyDoc_STR("open the file descriptors.")
  900. },
  901. {
  902. .ml_name = "poll",
  903. .ml_meth = (PyCFunction)pyrf_evlist__poll,
  904. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  905. .ml_doc = PyDoc_STR("poll the file descriptor table.")
  906. },
  907. {
  908. .ml_name = "get_pollfd",
  909. .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
  910. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  911. .ml_doc = PyDoc_STR("get the poll file descriptor table.")
  912. },
  913. {
  914. .ml_name = "add",
  915. .ml_meth = (PyCFunction)pyrf_evlist__add,
  916. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  917. .ml_doc = PyDoc_STR("adds an event selector to the list.")
  918. },
  919. {
  920. .ml_name = "read_on_cpu",
  921. .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
  922. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  923. .ml_doc = PyDoc_STR("reads an event.")
  924. },
  925. { .ml_name = NULL, }
  926. };
  927. static Py_ssize_t pyrf_evlist__length(PyObject *obj)
  928. {
  929. struct pyrf_evlist *pevlist = (void *)obj;
  930. return pevlist->evlist.nr_entries;
  931. }
  932. static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
  933. {
  934. struct pyrf_evlist *pevlist = (void *)obj;
  935. struct perf_evsel *pos;
  936. if (i >= pevlist->evlist.nr_entries)
  937. return NULL;
  938. evlist__for_each_entry(&pevlist->evlist, pos) {
  939. if (i-- == 0)
  940. break;
  941. }
  942. return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
  943. }
  944. static PySequenceMethods pyrf_evlist__sequence_methods = {
  945. .sq_length = pyrf_evlist__length,
  946. .sq_item = pyrf_evlist__item,
  947. };
  948. static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
  949. static PyTypeObject pyrf_evlist__type = {
  950. PyVarObject_HEAD_INIT(NULL, 0)
  951. .tp_name = "perf.evlist",
  952. .tp_basicsize = sizeof(struct pyrf_evlist),
  953. .tp_dealloc = (destructor)pyrf_evlist__delete,
  954. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  955. .tp_as_sequence = &pyrf_evlist__sequence_methods,
  956. .tp_doc = pyrf_evlist__doc,
  957. .tp_methods = pyrf_evlist__methods,
  958. .tp_init = (initproc)pyrf_evlist__init,
  959. };
  960. static int pyrf_evlist__setup_types(void)
  961. {
  962. pyrf_evlist__type.tp_new = PyType_GenericNew;
  963. return PyType_Ready(&pyrf_evlist__type);
  964. }
  965. #define PERF_CONST(name) { #name, PERF_##name }
  966. static struct {
  967. const char *name;
  968. int value;
  969. } perf__constants[] = {
  970. PERF_CONST(TYPE_HARDWARE),
  971. PERF_CONST(TYPE_SOFTWARE),
  972. PERF_CONST(TYPE_TRACEPOINT),
  973. PERF_CONST(TYPE_HW_CACHE),
  974. PERF_CONST(TYPE_RAW),
  975. PERF_CONST(TYPE_BREAKPOINT),
  976. PERF_CONST(COUNT_HW_CPU_CYCLES),
  977. PERF_CONST(COUNT_HW_INSTRUCTIONS),
  978. PERF_CONST(COUNT_HW_CACHE_REFERENCES),
  979. PERF_CONST(COUNT_HW_CACHE_MISSES),
  980. PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
  981. PERF_CONST(COUNT_HW_BRANCH_MISSES),
  982. PERF_CONST(COUNT_HW_BUS_CYCLES),
  983. PERF_CONST(COUNT_HW_CACHE_L1D),
  984. PERF_CONST(COUNT_HW_CACHE_L1I),
  985. PERF_CONST(COUNT_HW_CACHE_LL),
  986. PERF_CONST(COUNT_HW_CACHE_DTLB),
  987. PERF_CONST(COUNT_HW_CACHE_ITLB),
  988. PERF_CONST(COUNT_HW_CACHE_BPU),
  989. PERF_CONST(COUNT_HW_CACHE_OP_READ),
  990. PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
  991. PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
  992. PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
  993. PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
  994. PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
  995. PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
  996. PERF_CONST(COUNT_SW_CPU_CLOCK),
  997. PERF_CONST(COUNT_SW_TASK_CLOCK),
  998. PERF_CONST(COUNT_SW_PAGE_FAULTS),
  999. PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
  1000. PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
  1001. PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
  1002. PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
  1003. PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
  1004. PERF_CONST(COUNT_SW_EMULATION_FAULTS),
  1005. PERF_CONST(COUNT_SW_DUMMY),
  1006. PERF_CONST(SAMPLE_IP),
  1007. PERF_CONST(SAMPLE_TID),
  1008. PERF_CONST(SAMPLE_TIME),
  1009. PERF_CONST(SAMPLE_ADDR),
  1010. PERF_CONST(SAMPLE_READ),
  1011. PERF_CONST(SAMPLE_CALLCHAIN),
  1012. PERF_CONST(SAMPLE_ID),
  1013. PERF_CONST(SAMPLE_CPU),
  1014. PERF_CONST(SAMPLE_PERIOD),
  1015. PERF_CONST(SAMPLE_STREAM_ID),
  1016. PERF_CONST(SAMPLE_RAW),
  1017. PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
  1018. PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
  1019. PERF_CONST(FORMAT_ID),
  1020. PERF_CONST(FORMAT_GROUP),
  1021. PERF_CONST(RECORD_MMAP),
  1022. PERF_CONST(RECORD_LOST),
  1023. PERF_CONST(RECORD_COMM),
  1024. PERF_CONST(RECORD_EXIT),
  1025. PERF_CONST(RECORD_THROTTLE),
  1026. PERF_CONST(RECORD_UNTHROTTLE),
  1027. PERF_CONST(RECORD_FORK),
  1028. PERF_CONST(RECORD_READ),
  1029. PERF_CONST(RECORD_SAMPLE),
  1030. PERF_CONST(RECORD_MMAP2),
  1031. PERF_CONST(RECORD_AUX),
  1032. PERF_CONST(RECORD_ITRACE_START),
  1033. PERF_CONST(RECORD_LOST_SAMPLES),
  1034. PERF_CONST(RECORD_SWITCH),
  1035. PERF_CONST(RECORD_SWITCH_CPU_WIDE),
  1036. PERF_CONST(RECORD_MISC_SWITCH_OUT),
  1037. { .name = NULL, },
  1038. };
  1039. static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
  1040. PyObject *args, PyObject *kwargs)
  1041. {
  1042. struct event_format *tp_format;
  1043. static char *kwlist[] = { "sys", "name", NULL };
  1044. char *sys = NULL;
  1045. char *name = NULL;
  1046. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
  1047. &sys, &name))
  1048. return NULL;
  1049. tp_format = trace_event__tp_format(sys, name);
  1050. if (IS_ERR(tp_format))
  1051. return _PyLong_FromLong(-1);
  1052. return _PyLong_FromLong(tp_format->id);
  1053. }
  1054. static PyMethodDef perf__methods[] = {
  1055. {
  1056. .ml_name = "tracepoint",
  1057. .ml_meth = (PyCFunction) pyrf__tracepoint,
  1058. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  1059. .ml_doc = PyDoc_STR("Get tracepoint config.")
  1060. },
  1061. { .ml_name = NULL, }
  1062. };
  1063. #if PY_MAJOR_VERSION < 3
  1064. PyMODINIT_FUNC initperf(void)
  1065. #else
  1066. PyMODINIT_FUNC PyInit_perf(void)
  1067. #endif
  1068. {
  1069. PyObject *obj;
  1070. int i;
  1071. PyObject *dict;
  1072. #if PY_MAJOR_VERSION < 3
  1073. PyObject *module = Py_InitModule("perf", perf__methods);
  1074. #else
  1075. static struct PyModuleDef moduledef = {
  1076. PyModuleDef_HEAD_INIT,
  1077. "perf", /* m_name */
  1078. "", /* m_doc */
  1079. -1, /* m_size */
  1080. perf__methods, /* m_methods */
  1081. NULL, /* m_reload */
  1082. NULL, /* m_traverse */
  1083. NULL, /* m_clear */
  1084. NULL, /* m_free */
  1085. };
  1086. PyObject *module = PyModule_Create(&moduledef);
  1087. #endif
  1088. if (module == NULL ||
  1089. pyrf_event__setup_types() < 0 ||
  1090. pyrf_evlist__setup_types() < 0 ||
  1091. pyrf_evsel__setup_types() < 0 ||
  1092. pyrf_thread_map__setup_types() < 0 ||
  1093. pyrf_cpu_map__setup_types() < 0)
  1094. #if PY_MAJOR_VERSION < 3
  1095. return;
  1096. #else
  1097. return module;
  1098. #endif
  1099. /* The page_size is placed in util object. */
  1100. page_size = sysconf(_SC_PAGE_SIZE);
  1101. Py_INCREF(&pyrf_evlist__type);
  1102. PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
  1103. Py_INCREF(&pyrf_evsel__type);
  1104. PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
  1105. Py_INCREF(&pyrf_mmap_event__type);
  1106. PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
  1107. Py_INCREF(&pyrf_lost_event__type);
  1108. PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
  1109. Py_INCREF(&pyrf_comm_event__type);
  1110. PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
  1111. Py_INCREF(&pyrf_task_event__type);
  1112. PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  1113. Py_INCREF(&pyrf_throttle_event__type);
  1114. PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
  1115. Py_INCREF(&pyrf_task_event__type);
  1116. PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  1117. Py_INCREF(&pyrf_read_event__type);
  1118. PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
  1119. Py_INCREF(&pyrf_sample_event__type);
  1120. PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
  1121. Py_INCREF(&pyrf_context_switch_event__type);
  1122. PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
  1123. Py_INCREF(&pyrf_thread_map__type);
  1124. PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
  1125. Py_INCREF(&pyrf_cpu_map__type);
  1126. PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
  1127. dict = PyModule_GetDict(module);
  1128. if (dict == NULL)
  1129. goto error;
  1130. for (i = 0; perf__constants[i].name != NULL; i++) {
  1131. obj = _PyLong_FromLong(perf__constants[i].value);
  1132. if (obj == NULL)
  1133. goto error;
  1134. PyDict_SetItemString(dict, perf__constants[i].name, obj);
  1135. Py_DECREF(obj);
  1136. }
  1137. error:
  1138. if (PyErr_Occurred())
  1139. PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
  1140. #if PY_MAJOR_VERSION >= 3
  1141. return module;
  1142. #endif
  1143. }
  1144. /*
  1145. * Dummy, to avoid dragging all the test_attr infrastructure in the python
  1146. * binding.
  1147. */
  1148. void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
  1149. int fd, int group_fd, unsigned long flags)
  1150. {
  1151. }