python.c 37 KB

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