python.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 int is_printable_array(char *p, unsigned int len)
  256. {
  257. unsigned int i;
  258. for (i = 0; i < len; i++) {
  259. if (!isprint(p[i]) && !isspace(p[i]))
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. static PyObject*
  265. tracepoint_field(struct pyrf_event *pe, struct format_field *field)
  266. {
  267. struct pevent *pevent = field->event->pevent;
  268. void *data = pe->sample.raw_data;
  269. PyObject *ret = NULL;
  270. unsigned long long val;
  271. unsigned int offset, len;
  272. if (field->flags & FIELD_IS_ARRAY) {
  273. offset = field->offset;
  274. len = field->size;
  275. if (field->flags & FIELD_IS_DYNAMIC) {
  276. val = pevent_read_number(pevent, data + offset, len);
  277. offset = val;
  278. len = offset >> 16;
  279. offset &= 0xffff;
  280. }
  281. if (field->flags & FIELD_IS_STRING &&
  282. is_printable_array(data + offset, len)) {
  283. ret = PyString_FromString((char *)data + offset);
  284. } else {
  285. ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
  286. field->flags &= ~FIELD_IS_STRING;
  287. }
  288. } else {
  289. val = pevent_read_number(pevent, data + field->offset,
  290. field->size);
  291. if (field->flags & FIELD_IS_POINTER)
  292. ret = PyLong_FromUnsignedLong((unsigned long) val);
  293. else if (field->flags & FIELD_IS_SIGNED)
  294. ret = PyLong_FromLong((long) val);
  295. else
  296. ret = PyLong_FromUnsignedLong((unsigned long) val);
  297. }
  298. return ret;
  299. }
  300. static PyObject*
  301. get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
  302. {
  303. const char *str = PyString_AsString(PyObject_Str(attr_name));
  304. struct perf_evsel *evsel = pevent->evsel;
  305. struct format_field *field;
  306. if (!evsel->tp_format) {
  307. struct event_format *tp_format;
  308. tp_format = trace_event__tp_format_id(evsel->attr.config);
  309. if (!tp_format)
  310. return NULL;
  311. evsel->tp_format = tp_format;
  312. }
  313. field = pevent_find_any_field(evsel->tp_format, str);
  314. if (!field)
  315. return NULL;
  316. return tracepoint_field(pevent, field);
  317. }
  318. static PyObject*
  319. pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
  320. {
  321. PyObject *obj = NULL;
  322. if (is_tracepoint(pevent))
  323. obj = get_tracepoint_field(pevent, attr_name);
  324. return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
  325. }
  326. static PyTypeObject pyrf_sample_event__type = {
  327. PyVarObject_HEAD_INIT(NULL, 0)
  328. .tp_name = "perf.sample_event",
  329. .tp_basicsize = sizeof(struct pyrf_event),
  330. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  331. .tp_doc = pyrf_sample_event__doc,
  332. .tp_members = pyrf_sample_event__members,
  333. .tp_repr = (reprfunc)pyrf_sample_event__repr,
  334. .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
  335. };
  336. static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
  337. static PyMemberDef pyrf_context_switch_event__members[] = {
  338. sample_members
  339. member_def(perf_event_header, type, T_UINT, "event type"),
  340. member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
  341. member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
  342. { .name = NULL, },
  343. };
  344. static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
  345. {
  346. PyObject *ret;
  347. char *s;
  348. if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
  349. pevent->event.context_switch.next_prev_pid,
  350. pevent->event.context_switch.next_prev_tid,
  351. !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
  352. ret = PyErr_NoMemory();
  353. } else {
  354. ret = PyString_FromString(s);
  355. free(s);
  356. }
  357. return ret;
  358. }
  359. static PyTypeObject pyrf_context_switch_event__type = {
  360. PyVarObject_HEAD_INIT(NULL, 0)
  361. .tp_name = "perf.context_switch_event",
  362. .tp_basicsize = sizeof(struct pyrf_event),
  363. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  364. .tp_doc = pyrf_context_switch_event__doc,
  365. .tp_members = pyrf_context_switch_event__members,
  366. .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
  367. };
  368. static int pyrf_event__setup_types(void)
  369. {
  370. int err;
  371. pyrf_mmap_event__type.tp_new =
  372. pyrf_task_event__type.tp_new =
  373. pyrf_comm_event__type.tp_new =
  374. pyrf_lost_event__type.tp_new =
  375. pyrf_read_event__type.tp_new =
  376. pyrf_sample_event__type.tp_new =
  377. pyrf_context_switch_event__type.tp_new =
  378. pyrf_throttle_event__type.tp_new = PyType_GenericNew;
  379. err = PyType_Ready(&pyrf_mmap_event__type);
  380. if (err < 0)
  381. goto out;
  382. err = PyType_Ready(&pyrf_lost_event__type);
  383. if (err < 0)
  384. goto out;
  385. err = PyType_Ready(&pyrf_task_event__type);
  386. if (err < 0)
  387. goto out;
  388. err = PyType_Ready(&pyrf_comm_event__type);
  389. if (err < 0)
  390. goto out;
  391. err = PyType_Ready(&pyrf_throttle_event__type);
  392. if (err < 0)
  393. goto out;
  394. err = PyType_Ready(&pyrf_read_event__type);
  395. if (err < 0)
  396. goto out;
  397. err = PyType_Ready(&pyrf_sample_event__type);
  398. if (err < 0)
  399. goto out;
  400. err = PyType_Ready(&pyrf_context_switch_event__type);
  401. if (err < 0)
  402. goto out;
  403. out:
  404. return err;
  405. }
  406. static PyTypeObject *pyrf_event__type[] = {
  407. [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
  408. [PERF_RECORD_LOST] = &pyrf_lost_event__type,
  409. [PERF_RECORD_COMM] = &pyrf_comm_event__type,
  410. [PERF_RECORD_EXIT] = &pyrf_task_event__type,
  411. [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
  412. [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
  413. [PERF_RECORD_FORK] = &pyrf_task_event__type,
  414. [PERF_RECORD_READ] = &pyrf_read_event__type,
  415. [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
  416. [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
  417. [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
  418. };
  419. static PyObject *pyrf_event__new(union perf_event *event)
  420. {
  421. struct pyrf_event *pevent;
  422. PyTypeObject *ptype;
  423. if ((event->header.type < PERF_RECORD_MMAP ||
  424. event->header.type > PERF_RECORD_SAMPLE) &&
  425. !(event->header.type == PERF_RECORD_SWITCH ||
  426. event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
  427. return NULL;
  428. ptype = pyrf_event__type[event->header.type];
  429. pevent = PyObject_New(struct pyrf_event, ptype);
  430. if (pevent != NULL)
  431. memcpy(&pevent->event, event, event->header.size);
  432. return (PyObject *)pevent;
  433. }
  434. struct pyrf_cpu_map {
  435. PyObject_HEAD
  436. struct cpu_map *cpus;
  437. };
  438. static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
  439. PyObject *args, PyObject *kwargs)
  440. {
  441. static char *kwlist[] = { "cpustr", NULL };
  442. char *cpustr = NULL;
  443. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
  444. kwlist, &cpustr))
  445. return -1;
  446. pcpus->cpus = cpu_map__new(cpustr);
  447. if (pcpus->cpus == NULL)
  448. return -1;
  449. return 0;
  450. }
  451. static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
  452. {
  453. cpu_map__put(pcpus->cpus);
  454. pcpus->ob_type->tp_free((PyObject*)pcpus);
  455. }
  456. static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
  457. {
  458. struct pyrf_cpu_map *pcpus = (void *)obj;
  459. return pcpus->cpus->nr;
  460. }
  461. static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
  462. {
  463. struct pyrf_cpu_map *pcpus = (void *)obj;
  464. if (i >= pcpus->cpus->nr)
  465. return NULL;
  466. return Py_BuildValue("i", pcpus->cpus->map[i]);
  467. }
  468. static PySequenceMethods pyrf_cpu_map__sequence_methods = {
  469. .sq_length = pyrf_cpu_map__length,
  470. .sq_item = pyrf_cpu_map__item,
  471. };
  472. static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
  473. static PyTypeObject pyrf_cpu_map__type = {
  474. PyVarObject_HEAD_INIT(NULL, 0)
  475. .tp_name = "perf.cpu_map",
  476. .tp_basicsize = sizeof(struct pyrf_cpu_map),
  477. .tp_dealloc = (destructor)pyrf_cpu_map__delete,
  478. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  479. .tp_doc = pyrf_cpu_map__doc,
  480. .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
  481. .tp_init = (initproc)pyrf_cpu_map__init,
  482. };
  483. static int pyrf_cpu_map__setup_types(void)
  484. {
  485. pyrf_cpu_map__type.tp_new = PyType_GenericNew;
  486. return PyType_Ready(&pyrf_cpu_map__type);
  487. }
  488. struct pyrf_thread_map {
  489. PyObject_HEAD
  490. struct thread_map *threads;
  491. };
  492. static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
  493. PyObject *args, PyObject *kwargs)
  494. {
  495. static char *kwlist[] = { "pid", "tid", "uid", NULL };
  496. int pid = -1, tid = -1, uid = UINT_MAX;
  497. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
  498. kwlist, &pid, &tid, &uid))
  499. return -1;
  500. pthreads->threads = thread_map__new(pid, tid, uid);
  501. if (pthreads->threads == NULL)
  502. return -1;
  503. return 0;
  504. }
  505. static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
  506. {
  507. thread_map__put(pthreads->threads);
  508. pthreads->ob_type->tp_free((PyObject*)pthreads);
  509. }
  510. static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
  511. {
  512. struct pyrf_thread_map *pthreads = (void *)obj;
  513. return pthreads->threads->nr;
  514. }
  515. static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
  516. {
  517. struct pyrf_thread_map *pthreads = (void *)obj;
  518. if (i >= pthreads->threads->nr)
  519. return NULL;
  520. return Py_BuildValue("i", pthreads->threads->map[i]);
  521. }
  522. static PySequenceMethods pyrf_thread_map__sequence_methods = {
  523. .sq_length = pyrf_thread_map__length,
  524. .sq_item = pyrf_thread_map__item,
  525. };
  526. static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
  527. static PyTypeObject pyrf_thread_map__type = {
  528. PyVarObject_HEAD_INIT(NULL, 0)
  529. .tp_name = "perf.thread_map",
  530. .tp_basicsize = sizeof(struct pyrf_thread_map),
  531. .tp_dealloc = (destructor)pyrf_thread_map__delete,
  532. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  533. .tp_doc = pyrf_thread_map__doc,
  534. .tp_as_sequence = &pyrf_thread_map__sequence_methods,
  535. .tp_init = (initproc)pyrf_thread_map__init,
  536. };
  537. static int pyrf_thread_map__setup_types(void)
  538. {
  539. pyrf_thread_map__type.tp_new = PyType_GenericNew;
  540. return PyType_Ready(&pyrf_thread_map__type);
  541. }
  542. struct pyrf_evsel {
  543. PyObject_HEAD
  544. struct perf_evsel evsel;
  545. };
  546. static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
  547. PyObject *args, PyObject *kwargs)
  548. {
  549. struct perf_event_attr attr = {
  550. .type = PERF_TYPE_HARDWARE,
  551. .config = PERF_COUNT_HW_CPU_CYCLES,
  552. .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
  553. };
  554. static char *kwlist[] = {
  555. "type",
  556. "config",
  557. "sample_freq",
  558. "sample_period",
  559. "sample_type",
  560. "read_format",
  561. "disabled",
  562. "inherit",
  563. "pinned",
  564. "exclusive",
  565. "exclude_user",
  566. "exclude_kernel",
  567. "exclude_hv",
  568. "exclude_idle",
  569. "mmap",
  570. "context_switch",
  571. "comm",
  572. "freq",
  573. "inherit_stat",
  574. "enable_on_exec",
  575. "task",
  576. "watermark",
  577. "precise_ip",
  578. "mmap_data",
  579. "sample_id_all",
  580. "wakeup_events",
  581. "bp_type",
  582. "bp_addr",
  583. "bp_len",
  584. NULL
  585. };
  586. u64 sample_period = 0;
  587. u32 disabled = 0,
  588. inherit = 0,
  589. pinned = 0,
  590. exclusive = 0,
  591. exclude_user = 0,
  592. exclude_kernel = 0,
  593. exclude_hv = 0,
  594. exclude_idle = 0,
  595. mmap = 0,
  596. context_switch = 0,
  597. comm = 0,
  598. freq = 1,
  599. inherit_stat = 0,
  600. enable_on_exec = 0,
  601. task = 0,
  602. watermark = 0,
  603. precise_ip = 0,
  604. mmap_data = 0,
  605. sample_id_all = 1;
  606. int idx = 0;
  607. if (!PyArg_ParseTupleAndKeywords(args, kwargs,
  608. "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
  609. &attr.type, &attr.config, &attr.sample_freq,
  610. &sample_period, &attr.sample_type,
  611. &attr.read_format, &disabled, &inherit,
  612. &pinned, &exclusive, &exclude_user,
  613. &exclude_kernel, &exclude_hv, &exclude_idle,
  614. &mmap, &context_switch, &comm, &freq, &inherit_stat,
  615. &enable_on_exec, &task, &watermark,
  616. &precise_ip, &mmap_data, &sample_id_all,
  617. &attr.wakeup_events, &attr.bp_type,
  618. &attr.bp_addr, &attr.bp_len, &idx))
  619. return -1;
  620. /* union... */
  621. if (sample_period != 0) {
  622. if (attr.sample_freq != 0)
  623. return -1; /* FIXME: throw right exception */
  624. attr.sample_period = sample_period;
  625. }
  626. /* Bitfields */
  627. attr.disabled = disabled;
  628. attr.inherit = inherit;
  629. attr.pinned = pinned;
  630. attr.exclusive = exclusive;
  631. attr.exclude_user = exclude_user;
  632. attr.exclude_kernel = exclude_kernel;
  633. attr.exclude_hv = exclude_hv;
  634. attr.exclude_idle = exclude_idle;
  635. attr.mmap = mmap;
  636. attr.context_switch = context_switch;
  637. attr.comm = comm;
  638. attr.freq = freq;
  639. attr.inherit_stat = inherit_stat;
  640. attr.enable_on_exec = enable_on_exec;
  641. attr.task = task;
  642. attr.watermark = watermark;
  643. attr.precise_ip = precise_ip;
  644. attr.mmap_data = mmap_data;
  645. attr.sample_id_all = sample_id_all;
  646. attr.size = sizeof(attr);
  647. perf_evsel__init(&pevsel->evsel, &attr, idx);
  648. return 0;
  649. }
  650. static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
  651. {
  652. perf_evsel__exit(&pevsel->evsel);
  653. pevsel->ob_type->tp_free((PyObject*)pevsel);
  654. }
  655. static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
  656. PyObject *args, PyObject *kwargs)
  657. {
  658. struct perf_evsel *evsel = &pevsel->evsel;
  659. struct cpu_map *cpus = NULL;
  660. struct thread_map *threads = NULL;
  661. PyObject *pcpus = NULL, *pthreads = NULL;
  662. int group = 0, inherit = 0;
  663. static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
  664. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
  665. &pcpus, &pthreads, &group, &inherit))
  666. return NULL;
  667. if (pthreads != NULL)
  668. threads = ((struct pyrf_thread_map *)pthreads)->threads;
  669. if (pcpus != NULL)
  670. cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  671. evsel->attr.inherit = inherit;
  672. /*
  673. * This will group just the fds for this single evsel, to group
  674. * multiple events, use evlist.open().
  675. */
  676. if (perf_evsel__open(evsel, cpus, threads) < 0) {
  677. PyErr_SetFromErrno(PyExc_OSError);
  678. return NULL;
  679. }
  680. Py_INCREF(Py_None);
  681. return Py_None;
  682. }
  683. static PyMethodDef pyrf_evsel__methods[] = {
  684. {
  685. .ml_name = "open",
  686. .ml_meth = (PyCFunction)pyrf_evsel__open,
  687. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  688. .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
  689. },
  690. { .ml_name = NULL, }
  691. };
  692. static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
  693. static PyTypeObject pyrf_evsel__type = {
  694. PyVarObject_HEAD_INIT(NULL, 0)
  695. .tp_name = "perf.evsel",
  696. .tp_basicsize = sizeof(struct pyrf_evsel),
  697. .tp_dealloc = (destructor)pyrf_evsel__delete,
  698. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  699. .tp_doc = pyrf_evsel__doc,
  700. .tp_methods = pyrf_evsel__methods,
  701. .tp_init = (initproc)pyrf_evsel__init,
  702. };
  703. static int pyrf_evsel__setup_types(void)
  704. {
  705. pyrf_evsel__type.tp_new = PyType_GenericNew;
  706. return PyType_Ready(&pyrf_evsel__type);
  707. }
  708. struct pyrf_evlist {
  709. PyObject_HEAD
  710. struct perf_evlist evlist;
  711. };
  712. static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
  713. PyObject *args, PyObject *kwargs __maybe_unused)
  714. {
  715. PyObject *pcpus = NULL, *pthreads = NULL;
  716. struct cpu_map *cpus;
  717. struct thread_map *threads;
  718. if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
  719. return -1;
  720. threads = ((struct pyrf_thread_map *)pthreads)->threads;
  721. cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
  722. perf_evlist__init(&pevlist->evlist, cpus, threads);
  723. return 0;
  724. }
  725. static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
  726. {
  727. perf_evlist__exit(&pevlist->evlist);
  728. pevlist->ob_type->tp_free((PyObject*)pevlist);
  729. }
  730. static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
  731. PyObject *args, PyObject *kwargs)
  732. {
  733. struct perf_evlist *evlist = &pevlist->evlist;
  734. static char *kwlist[] = { "pages", "overwrite", NULL };
  735. int pages = 128, overwrite = false;
  736. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
  737. &pages, &overwrite))
  738. return NULL;
  739. if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
  740. PyErr_SetFromErrno(PyExc_OSError);
  741. return NULL;
  742. }
  743. Py_INCREF(Py_None);
  744. return Py_None;
  745. }
  746. static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
  747. PyObject *args, PyObject *kwargs)
  748. {
  749. struct perf_evlist *evlist = &pevlist->evlist;
  750. static char *kwlist[] = { "timeout", NULL };
  751. int timeout = -1, n;
  752. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
  753. return NULL;
  754. n = perf_evlist__poll(evlist, timeout);
  755. if (n < 0) {
  756. PyErr_SetFromErrno(PyExc_OSError);
  757. return NULL;
  758. }
  759. return Py_BuildValue("i", n);
  760. }
  761. static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
  762. PyObject *args __maybe_unused,
  763. PyObject *kwargs __maybe_unused)
  764. {
  765. struct perf_evlist *evlist = &pevlist->evlist;
  766. PyObject *list = PyList_New(0);
  767. int i;
  768. for (i = 0; i < evlist->pollfd.nr; ++i) {
  769. PyObject *file;
  770. FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
  771. if (fp == NULL)
  772. goto free_list;
  773. file = PyFile_FromFile(fp, "perf", "r", NULL);
  774. if (file == NULL)
  775. goto free_list;
  776. if (PyList_Append(list, file) != 0) {
  777. Py_DECREF(file);
  778. goto free_list;
  779. }
  780. Py_DECREF(file);
  781. }
  782. return list;
  783. free_list:
  784. return PyErr_NoMemory();
  785. }
  786. static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
  787. PyObject *args,
  788. PyObject *kwargs __maybe_unused)
  789. {
  790. struct perf_evlist *evlist = &pevlist->evlist;
  791. PyObject *pevsel;
  792. struct perf_evsel *evsel;
  793. if (!PyArg_ParseTuple(args, "O", &pevsel))
  794. return NULL;
  795. Py_INCREF(pevsel);
  796. evsel = &((struct pyrf_evsel *)pevsel)->evsel;
  797. evsel->idx = evlist->nr_entries;
  798. perf_evlist__add(evlist, evsel);
  799. return Py_BuildValue("i", evlist->nr_entries);
  800. }
  801. static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
  802. PyObject *args, PyObject *kwargs)
  803. {
  804. struct perf_evlist *evlist = &pevlist->evlist;
  805. union perf_event *event;
  806. int sample_id_all = 1, cpu;
  807. static char *kwlist[] = { "cpu", "sample_id_all", NULL };
  808. int err;
  809. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
  810. &cpu, &sample_id_all))
  811. return NULL;
  812. event = perf_evlist__mmap_read(evlist, cpu);
  813. if (event != NULL) {
  814. PyObject *pyevent = pyrf_event__new(event);
  815. struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
  816. struct perf_evsel *evsel;
  817. if (pyevent == NULL)
  818. return PyErr_NoMemory();
  819. evsel = perf_evlist__event2evsel(evlist, event);
  820. if (!evsel)
  821. return Py_None;
  822. pevent->evsel = evsel;
  823. err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
  824. /* Consume the even only after we parsed it out. */
  825. perf_evlist__mmap_consume(evlist, cpu);
  826. if (err)
  827. return PyErr_Format(PyExc_OSError,
  828. "perf: can't parse sample, err=%d", err);
  829. return pyevent;
  830. }
  831. Py_INCREF(Py_None);
  832. return Py_None;
  833. }
  834. static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
  835. PyObject *args, PyObject *kwargs)
  836. {
  837. struct perf_evlist *evlist = &pevlist->evlist;
  838. int group = 0;
  839. static char *kwlist[] = { "group", NULL };
  840. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
  841. return NULL;
  842. if (group)
  843. perf_evlist__set_leader(evlist);
  844. if (perf_evlist__open(evlist) < 0) {
  845. PyErr_SetFromErrno(PyExc_OSError);
  846. return NULL;
  847. }
  848. Py_INCREF(Py_None);
  849. return Py_None;
  850. }
  851. static PyMethodDef pyrf_evlist__methods[] = {
  852. {
  853. .ml_name = "mmap",
  854. .ml_meth = (PyCFunction)pyrf_evlist__mmap,
  855. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  856. .ml_doc = PyDoc_STR("mmap the file descriptor table.")
  857. },
  858. {
  859. .ml_name = "open",
  860. .ml_meth = (PyCFunction)pyrf_evlist__open,
  861. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  862. .ml_doc = PyDoc_STR("open the file descriptors.")
  863. },
  864. {
  865. .ml_name = "poll",
  866. .ml_meth = (PyCFunction)pyrf_evlist__poll,
  867. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  868. .ml_doc = PyDoc_STR("poll the file descriptor table.")
  869. },
  870. {
  871. .ml_name = "get_pollfd",
  872. .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
  873. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  874. .ml_doc = PyDoc_STR("get the poll file descriptor table.")
  875. },
  876. {
  877. .ml_name = "add",
  878. .ml_meth = (PyCFunction)pyrf_evlist__add,
  879. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  880. .ml_doc = PyDoc_STR("adds an event selector to the list.")
  881. },
  882. {
  883. .ml_name = "read_on_cpu",
  884. .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
  885. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  886. .ml_doc = PyDoc_STR("reads an event.")
  887. },
  888. { .ml_name = NULL, }
  889. };
  890. static Py_ssize_t pyrf_evlist__length(PyObject *obj)
  891. {
  892. struct pyrf_evlist *pevlist = (void *)obj;
  893. return pevlist->evlist.nr_entries;
  894. }
  895. static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
  896. {
  897. struct pyrf_evlist *pevlist = (void *)obj;
  898. struct perf_evsel *pos;
  899. if (i >= pevlist->evlist.nr_entries)
  900. return NULL;
  901. evlist__for_each_entry(&pevlist->evlist, pos) {
  902. if (i-- == 0)
  903. break;
  904. }
  905. return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
  906. }
  907. static PySequenceMethods pyrf_evlist__sequence_methods = {
  908. .sq_length = pyrf_evlist__length,
  909. .sq_item = pyrf_evlist__item,
  910. };
  911. static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
  912. static PyTypeObject pyrf_evlist__type = {
  913. PyVarObject_HEAD_INIT(NULL, 0)
  914. .tp_name = "perf.evlist",
  915. .tp_basicsize = sizeof(struct pyrf_evlist),
  916. .tp_dealloc = (destructor)pyrf_evlist__delete,
  917. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
  918. .tp_as_sequence = &pyrf_evlist__sequence_methods,
  919. .tp_doc = pyrf_evlist__doc,
  920. .tp_methods = pyrf_evlist__methods,
  921. .tp_init = (initproc)pyrf_evlist__init,
  922. };
  923. static int pyrf_evlist__setup_types(void)
  924. {
  925. pyrf_evlist__type.tp_new = PyType_GenericNew;
  926. return PyType_Ready(&pyrf_evlist__type);
  927. }
  928. #define PERF_CONST(name) { #name, PERF_##name }
  929. static struct {
  930. const char *name;
  931. int value;
  932. } perf__constants[] = {
  933. PERF_CONST(TYPE_HARDWARE),
  934. PERF_CONST(TYPE_SOFTWARE),
  935. PERF_CONST(TYPE_TRACEPOINT),
  936. PERF_CONST(TYPE_HW_CACHE),
  937. PERF_CONST(TYPE_RAW),
  938. PERF_CONST(TYPE_BREAKPOINT),
  939. PERF_CONST(COUNT_HW_CPU_CYCLES),
  940. PERF_CONST(COUNT_HW_INSTRUCTIONS),
  941. PERF_CONST(COUNT_HW_CACHE_REFERENCES),
  942. PERF_CONST(COUNT_HW_CACHE_MISSES),
  943. PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
  944. PERF_CONST(COUNT_HW_BRANCH_MISSES),
  945. PERF_CONST(COUNT_HW_BUS_CYCLES),
  946. PERF_CONST(COUNT_HW_CACHE_L1D),
  947. PERF_CONST(COUNT_HW_CACHE_L1I),
  948. PERF_CONST(COUNT_HW_CACHE_LL),
  949. PERF_CONST(COUNT_HW_CACHE_DTLB),
  950. PERF_CONST(COUNT_HW_CACHE_ITLB),
  951. PERF_CONST(COUNT_HW_CACHE_BPU),
  952. PERF_CONST(COUNT_HW_CACHE_OP_READ),
  953. PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
  954. PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
  955. PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
  956. PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
  957. PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
  958. PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
  959. PERF_CONST(COUNT_SW_CPU_CLOCK),
  960. PERF_CONST(COUNT_SW_TASK_CLOCK),
  961. PERF_CONST(COUNT_SW_PAGE_FAULTS),
  962. PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
  963. PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
  964. PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
  965. PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
  966. PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
  967. PERF_CONST(COUNT_SW_EMULATION_FAULTS),
  968. PERF_CONST(COUNT_SW_DUMMY),
  969. PERF_CONST(SAMPLE_IP),
  970. PERF_CONST(SAMPLE_TID),
  971. PERF_CONST(SAMPLE_TIME),
  972. PERF_CONST(SAMPLE_ADDR),
  973. PERF_CONST(SAMPLE_READ),
  974. PERF_CONST(SAMPLE_CALLCHAIN),
  975. PERF_CONST(SAMPLE_ID),
  976. PERF_CONST(SAMPLE_CPU),
  977. PERF_CONST(SAMPLE_PERIOD),
  978. PERF_CONST(SAMPLE_STREAM_ID),
  979. PERF_CONST(SAMPLE_RAW),
  980. PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
  981. PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
  982. PERF_CONST(FORMAT_ID),
  983. PERF_CONST(FORMAT_GROUP),
  984. PERF_CONST(RECORD_MMAP),
  985. PERF_CONST(RECORD_LOST),
  986. PERF_CONST(RECORD_COMM),
  987. PERF_CONST(RECORD_EXIT),
  988. PERF_CONST(RECORD_THROTTLE),
  989. PERF_CONST(RECORD_UNTHROTTLE),
  990. PERF_CONST(RECORD_FORK),
  991. PERF_CONST(RECORD_READ),
  992. PERF_CONST(RECORD_SAMPLE),
  993. PERF_CONST(RECORD_MMAP2),
  994. PERF_CONST(RECORD_AUX),
  995. PERF_CONST(RECORD_ITRACE_START),
  996. PERF_CONST(RECORD_LOST_SAMPLES),
  997. PERF_CONST(RECORD_SWITCH),
  998. PERF_CONST(RECORD_SWITCH_CPU_WIDE),
  999. PERF_CONST(RECORD_MISC_SWITCH_OUT),
  1000. { .name = NULL, },
  1001. };
  1002. static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
  1003. PyObject *args, PyObject *kwargs)
  1004. {
  1005. struct event_format *tp_format;
  1006. static char *kwlist[] = { "sys", "name", NULL };
  1007. char *sys = NULL;
  1008. char *name = NULL;
  1009. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
  1010. &sys, &name))
  1011. return NULL;
  1012. tp_format = trace_event__tp_format(sys, name);
  1013. if (IS_ERR(tp_format))
  1014. return PyInt_FromLong(-1);
  1015. return PyInt_FromLong(tp_format->id);
  1016. }
  1017. static PyMethodDef perf__methods[] = {
  1018. {
  1019. .ml_name = "tracepoint",
  1020. .ml_meth = (PyCFunction) pyrf__tracepoint,
  1021. .ml_flags = METH_VARARGS | METH_KEYWORDS,
  1022. .ml_doc = PyDoc_STR("Get tracepoint config.")
  1023. },
  1024. { .ml_name = NULL, }
  1025. };
  1026. PyMODINIT_FUNC initperf(void)
  1027. {
  1028. PyObject *obj;
  1029. int i;
  1030. PyObject *dict, *module = Py_InitModule("perf", perf__methods);
  1031. if (module == NULL ||
  1032. pyrf_event__setup_types() < 0 ||
  1033. pyrf_evlist__setup_types() < 0 ||
  1034. pyrf_evsel__setup_types() < 0 ||
  1035. pyrf_thread_map__setup_types() < 0 ||
  1036. pyrf_cpu_map__setup_types() < 0)
  1037. return;
  1038. /* The page_size is placed in util object. */
  1039. page_size = sysconf(_SC_PAGE_SIZE);
  1040. Py_INCREF(&pyrf_evlist__type);
  1041. PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
  1042. Py_INCREF(&pyrf_evsel__type);
  1043. PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
  1044. Py_INCREF(&pyrf_mmap_event__type);
  1045. PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
  1046. Py_INCREF(&pyrf_lost_event__type);
  1047. PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
  1048. Py_INCREF(&pyrf_comm_event__type);
  1049. PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
  1050. Py_INCREF(&pyrf_task_event__type);
  1051. PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  1052. Py_INCREF(&pyrf_throttle_event__type);
  1053. PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
  1054. Py_INCREF(&pyrf_task_event__type);
  1055. PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
  1056. Py_INCREF(&pyrf_read_event__type);
  1057. PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
  1058. Py_INCREF(&pyrf_sample_event__type);
  1059. PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
  1060. Py_INCREF(&pyrf_context_switch_event__type);
  1061. PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
  1062. Py_INCREF(&pyrf_thread_map__type);
  1063. PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
  1064. Py_INCREF(&pyrf_cpu_map__type);
  1065. PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
  1066. dict = PyModule_GetDict(module);
  1067. if (dict == NULL)
  1068. goto error;
  1069. for (i = 0; perf__constants[i].name != NULL; i++) {
  1070. obj = PyInt_FromLong(perf__constants[i].value);
  1071. if (obj == NULL)
  1072. goto error;
  1073. PyDict_SetItemString(dict, perf__constants[i].name, obj);
  1074. Py_DECREF(obj);
  1075. }
  1076. error:
  1077. if (PyErr_Occurred())
  1078. PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
  1079. }
  1080. /*
  1081. * Dummy, to avoid dragging all the test_attr infrastructure in the python
  1082. * binding.
  1083. */
  1084. void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
  1085. int fd, int group_fd, unsigned long flags)
  1086. {
  1087. }