python.c 34 KB

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