Something went wrong. Try again.
(I'm so sorry!) Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344// SPDX-License-Identifier: GPL-2.0#define PY_SSIZE_T_CLEAN#include <Python.h>
#include <inttypes.h>#include <string.h>
#include <linux/err.h>#include <poll.h>#include <unistd.h>
#include <internal/lib.h>#include <perf/cpumap.h>#include <perf/mmap.h>#include <structmember.h>
#include "addr_location.h"#include "build-id.h"#include "callchain.h"#include "comm.h"#include "config.h"#include "counts.h"#include "data.h"#include "debug.h"#include "dso.h"#include "dwarf-regs.h"#include "event.h"#include "branch.h"#include "evlist.h"#include "evsel.h"#include "expr.h"#include "map.h"#include "metricgroup.h"#include "mmap.h"#include "pmus.h"#include "print_binary.h"#include "record.h"#include "sample.h"#include "session.h"#include "srccode.h"#include "srcline.h"#include "strbuf.h"#include "symbol.h"#include "stat.h"#include "header.h"#include "trace/beauty/syscalltbl.h"#include "thread.h"#include "thread_map.h"#include "tool.h"#include "tp_pmu.h"#include "trace-event.h"
#ifdef HAVE_LIBTRACEEVENT#include <event-parse.h>#endif
PyMODINIT_FUNC PyInit_perf(void);
static PyObject *pyrf_evsel__from_evsel(struct evsel *evsel);
#define member_def(type, member, ptype, help) \ { #member, ptype, \ offsetof(struct pyrf_event, event) + offsetof(struct type, member), \ 0, help }
#define sample_member_def(name, member, ptype, help) \ { #name, ptype, \ offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \ 0, help }
#define CHECK_INITIALIZED(ptr, msg) \ do { \ if (!(ptr)) { \ PyErr_SetString(PyExc_ValueError, msg " not initialized"); \ return NULL; \ } \ } while (0)
#define CHECK_INITIALIZED_INT(ptr, msg) \ do { \ if (!(ptr)) { \ PyErr_SetString(PyExc_ValueError, msg " not initialized"); \ return -1; \ } \ } while (0)
struct pyrf_event { PyObject_HEAD /** @sample: The parsed sample from the event. */ struct perf_sample sample; /** @al: The address location from machine__resolve, lazily computed. */ struct addr_location al; /** @al_resolved: True when machine__resolve been called. */ bool al_resolved; /** @callchain: Resolved callchain, eagerly computed if requested. */ PyObject *callchain; /** @brstack: Resolved branch stack, eagerly computed if requested. */ PyObject *brstack; /** @event: The underlying perf_event that may be in a file or ring buffer. */ union perf_event event;};
#define sample_members \ sample_member_def(sample_pid, pid, T_INT, "event pid"), \ sample_member_def(sample_tid, tid, T_INT, "event tid"), \ sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
static PyObject *pyrf_event__get_evsel(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (!pevent->sample.evsel) Py_RETURN_NONE;
return pyrf_evsel__from_evsel(pevent->sample.evsel);}
static PyGetSetDef pyrf_event__getset[] = { { .name = "evsel", .get = pyrf_event__get_evsel, .set = NULL, .doc = "tracking event.", }, { .name = NULL, },};
static void pyrf_event__delete(struct pyrf_event *pevent){ if (pevent->al_resolved) addr_location__exit(&pevent->al); Py_XDECREF(pevent->callchain); Py_XDECREF(pevent->brstack); perf_sample__exit(&pevent->sample); Py_TYPE(pevent)->tp_free((PyObject *)pevent);}
static const char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
static PyMemberDef pyrf_mmap_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_event_header, misc, T_USHORT, "event misc"), member_def(perf_record_mmap, pid, T_UINT, "event pid"), member_def(perf_record_mmap, tid, T_UINT, "event tid"), member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"), member_def(perf_record_mmap, len, T_ULONGLONG, "map length"), member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"), member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"), { .name = NULL, },};
static PyObject *pyrf_mmap_event__repr(const struct pyrf_event *pevent){ PyObject *ret; char *s;
if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", " "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", " "filename: %s }", pevent->event.mmap.pid, pevent->event.mmap.tid, pevent->event.mmap.start, pevent->event.mmap.len, pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { ret = PyErr_NoMemory(); } else { ret = PyUnicode_FromString(s); free(s); } return ret;}
static PyTypeObject pyrf_mmap_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.mmap_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_mmap_event__doc, .tp_members = pyrf_mmap_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_mmap_event__repr,};
static const char pyrf_mmap2_event__doc[] = PyDoc_STR("perf mmap2 event object.");
static PyObject *pyrf_mmap2_event__get_maj(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (pevent->event.header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) Py_RETURN_NONE;
return PyLong_FromUnsignedLong(pevent->event.mmap2.maj);}
static PyObject *pyrf_mmap2_event__get_min(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (pevent->event.header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) Py_RETURN_NONE;
return PyLong_FromUnsignedLong(pevent->event.mmap2.min);}
static PyObject *pyrf_mmap2_event__get_ino(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (pevent->event.header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) Py_RETURN_NONE;
return PyLong_FromUnsignedLongLong(pevent->event.mmap2.ino);}
static PyObject *pyrf_mmap2_event__get_ino_generation(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (pevent->event.header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) Py_RETURN_NONE;
return PyLong_FromUnsignedLongLong(pevent->event.mmap2.ino_generation);}
static PyObject *pyrf_mmap2_event__get_build_id(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (!(pevent->event.header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) Py_RETURN_NONE;
int size = pevent->event.mmap2.build_id_size;
if (size > 20) size = 20;
return PyBytes_FromStringAndSize((const char *)pevent->event.mmap2.build_id, size);}
static PyGetSetDef pyrf_mmap2_event__getset[] = { { .name = "evsel", .get = pyrf_event__get_evsel, .set = NULL, .doc = "tracking event.", }, { .name = "maj", .get = pyrf_mmap2_event__get_maj, .set = NULL, .doc = "major number.", }, { .name = "min", .get = pyrf_mmap2_event__get_min, .set = NULL, .doc = "minor number.", }, { .name = "ino", .get = pyrf_mmap2_event__get_ino, .set = NULL, .doc = "inode number.", }, { .name = "ino_generation", .get = pyrf_mmap2_event__get_ino_generation, .set = NULL, .doc = "inode generation.", }, { .name = "build_id", .get = pyrf_mmap2_event__get_build_id, .set = NULL, .doc = "binary build ID.", }, { .name = NULL, },};
static PyMemberDef pyrf_mmap2_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_event_header, misc, T_USHORT, "event misc"), member_def(perf_record_mmap2, pid, T_UINT, "event pid"), member_def(perf_record_mmap2, tid, T_UINT, "event tid"), member_def(perf_record_mmap2, start, T_ULONGLONG, "start of the map"), member_def(perf_record_mmap2, len, T_ULONGLONG, "map length"), member_def(perf_record_mmap2, pgoff, T_ULONGLONG, "page offset"), member_def(perf_record_mmap2, prot, T_UINT, "protection"), member_def(perf_record_mmap2, flags, T_UINT, "flags"), member_def(perf_record_mmap2, filename, T_STRING_INPLACE, "backing store"), { .name = NULL, },};
static PyObject *pyrf_mmap2_event__repr(const struct pyrf_event *pevent){ PyObject *ret; char *s;
if (asprintf(&s, "{ type: mmap2, pid: %u, tid: %u, start: %#" PRI_lx64 ", length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", flags: %#x, prot: %#x, filename: %s }", pevent->event.mmap2.pid, pevent->event.mmap2.tid, pevent->event.mmap2.start, pevent->event.mmap2.len, pevent->event.mmap2.pgoff, pevent->event.mmap2.flags, pevent->event.mmap2.prot, pevent->event.mmap2.filename) < 0) return PyErr_NoMemory();
ret = PyUnicode_FromString(s); free(s); return ret;}
static PyTypeObject pyrf_mmap2_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.mmap2_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_mmap2_event__doc, .tp_members = pyrf_mmap2_event__members, .tp_getset = pyrf_mmap2_event__getset, .tp_repr = (reprfunc)pyrf_mmap2_event__repr,};
static const char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
static PyMemberDef pyrf_task_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_fork, pid, T_UINT, "event pid"), member_def(perf_record_fork, ppid, T_UINT, "event ppid"), member_def(perf_record_fork, tid, T_UINT, "event tid"), member_def(perf_record_fork, ptid, T_UINT, "event ptid"), member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"), { .name = NULL, },};
static PyObject *pyrf_task_event__repr(const struct pyrf_event *pevent){ return PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " "ptid: %u, time: %" PRI_lu64 "}", pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", pevent->event.fork.pid, pevent->event.fork.ppid, pevent->event.fork.tid, pevent->event.fork.ptid, pevent->event.fork.time);}
static PyTypeObject pyrf_task_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.task_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_task_event__doc, .tp_members = pyrf_task_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_task_event__repr,};
static const char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
static PyMemberDef pyrf_comm_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_comm, pid, T_UINT, "event pid"), member_def(perf_record_comm, tid, T_UINT, "event tid"), member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"), { .name = NULL, },};
static PyObject *pyrf_comm_event__repr(const struct pyrf_event *pevent){ return PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", pevent->event.comm.pid, pevent->event.comm.tid, pevent->event.comm.comm);}
static PyTypeObject pyrf_comm_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.comm_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_comm_event__doc, .tp_members = pyrf_comm_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_comm_event__repr,};
static const char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
static PyMemberDef pyrf_throttle_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"), member_def(perf_record_throttle, id, T_ULONGLONG, "event id"), member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"), { .name = NULL, },};
static PyObject *pyrf_throttle_event__repr(const struct pyrf_event *pevent){ const struct perf_record_throttle *te = (const struct perf_record_throttle *) (&pevent->event.header + 1);
return PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64 ", stream_id: %" PRI_lu64 " }", pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", te->time, te->id, te->stream_id);}
static PyTypeObject pyrf_throttle_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.throttle_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_throttle_event__doc, .tp_members = pyrf_throttle_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_throttle_event__repr,};
static const char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
static PyMemberDef pyrf_lost_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_lost, id, T_ULONGLONG, "event id"), member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"), { .name = NULL, },};
static PyObject *pyrf_lost_event__repr(const struct pyrf_event *pevent){ PyObject *ret; char *s;
if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", " "lost: %#" PRI_lx64 " }", pevent->event.lost.id, pevent->event.lost.lost) < 0) { ret = PyErr_NoMemory(); } else { ret = PyUnicode_FromString(s); free(s); } return ret;}
static PyTypeObject pyrf_lost_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.lost_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_lost_event__doc, .tp_members = pyrf_lost_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_lost_event__repr,};
static const char pyrf_stat_event__doc[] = PyDoc_STR("perf stat event object.");
static PyMemberDef pyrf_stat_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_stat, id, T_ULONGLONG, "event id"), member_def(perf_record_stat, cpu, T_UINT, "event cpu"), member_def(perf_record_stat, thread, T_UINT, "event thread"), member_def(perf_record_stat, val, T_ULONGLONG, "counter value"), member_def(perf_record_stat, ena, T_ULONGLONG, "enabled time"), member_def(perf_record_stat, run, T_ULONGLONG, "running time"), { .name = NULL, },};
static PyObject *pyrf_stat_event__repr(const struct pyrf_event *pevent){ return PyUnicode_FromFormat( "{ type: stat, id: %llu, cpu: %u, thread: %u, val: %llu, ena: %llu, run: %llu }", pevent->event.stat.id, pevent->event.stat.cpu, pevent->event.stat.thread, pevent->event.stat.val, pevent->event.stat.ena, pevent->event.stat.run);}
static PyTypeObject pyrf_stat_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.stat_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_new = PyType_GenericNew, .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_stat_event__doc, .tp_members = pyrf_stat_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_stat_event__repr,};
static const char pyrf_stat_round_event__doc[] = PyDoc_STR("perf stat round event object.");
static PyMemberDef pyrf_stat_round_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), { .name = "stat_round_type", .type = T_ULONGLONG, .offset = offsetof(struct pyrf_event, event) + offsetof(struct perf_record_stat_round, type), .doc = "round type" }, member_def(perf_record_stat_round, time, T_ULONGLONG, "round time"), { .name = NULL, },};
static PyObject *pyrf_stat_round_event__repr(const struct pyrf_event *pevent){ return PyUnicode_FromFormat("{ type: stat_round, type: %llu, time: %llu }", pevent->event.stat_round.type, pevent->event.stat_round.time);}
static PyTypeObject pyrf_stat_round_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.stat_round_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_new = PyType_GenericNew, .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_stat_round_event__doc, .tp_members = pyrf_stat_round_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_stat_round_event__repr,};
static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
static PyMemberDef pyrf_read_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_read, pid, T_UINT, "event pid"), member_def(perf_record_read, tid, T_UINT, "event tid"), { .name = NULL, },};
static PyObject *pyrf_read_event__repr(const struct pyrf_event *pevent){ return PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", pevent->event.read.pid, pevent->event.read.tid); /* * FIXME: return the array of read values, * making this method useful ;-) */}
static PyTypeObject pyrf_read_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.read_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_read_event__doc, .tp_members = pyrf_read_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_read_event__repr,};
static const char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
static PyMemberDef pyrf_sample_event__members[] = { sample_members sample_member_def(sample_ip, ip, T_ULONGLONG, "event ip"), sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), sample_member_def(sample_phys_addr, phys_addr, T_ULONGLONG, "event physical addr"), sample_member_def(sample_weight, weight, T_ULONGLONG, "event weight"), sample_member_def(sample_data_src, data_src, T_ULONGLONG, "event data source"), sample_member_def(sample_insn_count, insn_cnt, T_ULONGLONG, "event instruction count"), sample_member_def(sample_cyc_count, cyc_cnt, T_ULONGLONG, "event cycle count"), member_def(perf_event_header, type, T_UINT, "event type"), { .name = NULL, },};
static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent){ PyObject *ret; char *s;
if (asprintf(&s, "{ type: sample }") < 0) { ret = PyErr_NoMemory(); } else { ret = PyUnicode_FromString(s); free(s); } return ret;}
#ifdef HAVE_LIBTRACEEVENTstatic bool is_tracepoint(const struct pyrf_event *pevent){ if (!pevent->sample.evsel) return false; return pevent->sample.evsel->core.attr.type == PERF_TYPE_TRACEPOINT;}
static PyObject*tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field){ struct tep_handle *pevent = field->event->tep; void *data = pe->sample.raw_data; PyObject *ret = NULL; unsigned long long val; unsigned int offset, len;
if (field->flags & TEP_FIELD_IS_ARRAY) { offset = field->offset; len = field->size; if (field->flags & TEP_FIELD_IS_DYNAMIC) { val = tep_read_number(pevent, data + offset, len); offset = val; len = offset >> 16; offset &= 0xffff; if (tep_field_is_relative(field->flags)) offset += field->offset + field->size; } if (field->flags & TEP_FIELD_IS_STRING && is_printable_array(data + offset, len)) { ret = PyUnicode_FromString((char *)data + offset); } else { ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); field->flags &= ~TEP_FIELD_IS_STRING; } } else { val = tep_read_number(pevent, data + field->offset, field->size); if (field->flags & TEP_FIELD_IS_POINTER) ret = PyLong_FromUnsignedLong((unsigned long) val); else if (field->flags & TEP_FIELD_IS_SIGNED) ret = PyLong_FromLong((long) val); else ret = PyLong_FromUnsignedLong((unsigned long) val); }
return ret;}
static PyObject*get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name){ struct evsel *evsel = pevent->sample.evsel; struct tep_event *tp_format = evsel__tp_format(evsel); struct tep_format_field *field;
if (IS_ERR_OR_NULL(tp_format)) return NULL;
PyObject *obj = PyObject_Str(attr_name); if (obj == NULL) return NULL;
const char *str = PyUnicode_AsUTF8(obj); if (str == NULL) { Py_DECREF(obj); return NULL; }
field = tep_find_any_field(tp_format, str); Py_DECREF(obj); return field ? tracepoint_field(pevent, field) : NULL;}#endif /* HAVE_LIBTRACEEVENT */
static int pyrf_sample_event__resolve_al(struct pyrf_event *pevent){ struct evsel *evsel = pevent->sample.evsel; struct evlist *evlist = evsel ? evsel->evlist : NULL; struct perf_session *session = evlist ? evlist__session(evlist) : NULL;
if (pevent->al_resolved) return 0;
if (!session) return -1;
addr_location__init(&pevent->al); if (machine__resolve(&session->machines.host, &pevent->al, &pevent->sample) < 0) { addr_location__exit(&pevent->al); return -1; }
pevent->al_resolved = true; return 0;}
static PyObject *pyrf_sample_event__get_dso(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map) Py_RETURN_NONE;
return PyUnicode_FromString(dso__name(map__dso(pevent->al.map)));}
static PyObject *pyrf_sample_event__get_dso_long_name(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map) Py_RETURN_NONE;
return PyUnicode_FromString(dso__long_name(map__dso(pevent->al.map)));}
static PyObject *pyrf_sample_event__get_dso_bid(struct pyrf_event *pevent, void *closure __maybe_unused){ char sbuild_id[SBUILD_ID_SIZE];
if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map) Py_RETURN_NONE;
build_id__snprintf(dso__bid(map__dso(pevent->al.map)), sbuild_id, sizeof(sbuild_id)); return PyUnicode_FromString(sbuild_id);}
static PyObject *pyrf_sample_event__get_map_start(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map) Py_RETURN_NONE;
return PyLong_FromUnsignedLong(map__start(pevent->al.map));}
static PyObject *pyrf_sample_event__get_map_end(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map) Py_RETURN_NONE;
return PyLong_FromUnsignedLong(map__end(pevent->al.map));}
static PyObject *pyrf_sample_event__get_map_pgoff(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map) Py_RETURN_NONE;
return PyLong_FromUnsignedLongLong(map__pgoff(pevent->al.map));}
static PyObject *pyrf_sample_event__get_symbol(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym) Py_RETURN_NONE;
return PyUnicode_FromString(pevent->al.sym->name);}
static PyObject *pyrf_sample_event__get_sym_start(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym) Py_RETURN_NONE;
return PyLong_FromUnsignedLongLong(pevent->al.sym->start);}
static PyObject *pyrf_sample_event__get_sym_end(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym) Py_RETURN_NONE;
return PyLong_FromUnsignedLongLong(pevent->al.sym->end);}
static PyObject *pyrf_sample_event__get_raw_buf(struct pyrf_event *pevent, void *closure __maybe_unused){ if (pevent->event.header.type != PERF_RECORD_SAMPLE) Py_RETURN_NONE;
return PyBytes_FromStringAndSize((const char *)pevent->sample.raw_data, pevent->sample.raw_size);}
static PyObject *pyrf_sample_event__srccode(PyObject *self, PyObject *args){ struct pyrf_event *pevent = (void *)self; u64 addr = pevent->sample.ip; char *srcfile = NULL; char *srccode = NULL; unsigned int line = 0; int len = 0; PyObject *result; struct addr_location al;
if (!PyArg_ParseTuple(args, "|K", &addr)) return NULL;
if (pyrf_sample_event__resolve_al(pevent) < 0) Py_RETURN_NONE;
if (addr != pevent->sample.ip) { addr_location__init(&al); thread__find_symbol_fb(pevent->al.thread, pevent->sample.cpumode, addr, &al); } else { addr_location__init(&al); al.thread = thread__get(pevent->al.thread); al.map = map__get(pevent->al.map); al.sym = pevent->al.sym; al.addr = pevent->al.addr; }
if (al.map) { struct dso *dso = map__dso(al.map);
if (dso) { srcfile = get_srcline_split(dso, map__rip_2objdump(al.map, addr), &line); } } addr_location__exit(&al);
if (srcfile) { srccode = find_sourceline(srcfile, line, &len); result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len); free(srcfile); } else { result = Py_BuildValue("(sIs#)", NULL, 0, NULL, (Py_ssize_t)0); }
return result;}
static PyObject *pyrf_sample_event__insn(PyObject *self, PyObject *args __maybe_unused){ struct pyrf_event *pevent = (void *)self; struct thread *thread; struct machine *machine;
if (pyrf_sample_event__resolve_al(pevent) < 0) Py_RETURN_NONE;
thread = pevent->al.thread;
if (!thread || !thread__maps(thread)) Py_RETURN_NONE;
machine = maps__machine(thread__maps(thread)); if (!machine) Py_RETURN_NONE;
if (pevent->sample.ip && !pevent->sample.insn_len) perf_sample__fetch_insn(&pevent->sample, thread, machine);
if (!pevent->sample.insn_len) Py_RETURN_NONE;
return PyBytes_FromStringAndSize((const char *)pevent->sample.insn, pevent->sample.insn_len);}
struct pyrf_callchain_node { PyObject_HEAD u64 ip; struct map *map; struct symbol *sym;};
static void pyrf_callchain_node__delete(struct pyrf_callchain_node *pnode){ map__put(pnode->map); Py_TYPE(pnode)->tp_free((PyObject *)pnode);}
static PyObject *pyrf_callchain_node__get_ip(struct pyrf_callchain_node *pnode, void *closure __maybe_unused){ return PyLong_FromUnsignedLongLong(pnode->ip);}
static PyObject *pyrf_callchain_node__get_symbol(struct pyrf_callchain_node *pnode, void *closure __maybe_unused){ if (pnode->sym) return PyUnicode_FromString(pnode->sym->name); return PyUnicode_FromString("[unknown]");}
static PyObject *pyrf_callchain_node__get_dso(struct pyrf_callchain_node *pnode, void *closure __maybe_unused){ const char *dsoname = "[unknown]";
if (pnode->map) { struct dso *dso = map__dso(pnode->map);
if (dso) { if (symbol_conf.show_kernel_path && dso__long_name(dso)) dsoname = dso__long_name(dso); else dsoname = dso__name(dso); } } return PyUnicode_FromString(dsoname);}
static PyGetSetDef pyrf_callchain_node__getset[] = { { .name = "ip", .get = (getter)pyrf_callchain_node__get_ip, }, { .name = "symbol", .get = (getter)pyrf_callchain_node__get_symbol, }, { .name = "dso", .get = (getter)pyrf_callchain_node__get_dso, }, { .name = NULL, },};
static PyTypeObject pyrf_callchain_node__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.callchain_node", .tp_basicsize = sizeof(struct pyrf_callchain_node), .tp_dealloc = (destructor)pyrf_callchain_node__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = "perf callchain node object.", .tp_getset = pyrf_callchain_node__getset,};
struct pyrf_callchain_frame { u64 ip; struct map *map; struct symbol *sym;};
struct pyrf_callchain { PyObject_HEAD struct pyrf_callchain_frame *frames; u64 nr_frames;};
static void pyrf_callchain__delete(struct pyrf_callchain *pchain){ if (pchain->frames) { for (u64 i = 0; i < pchain->nr_frames; i++) map__put(pchain->frames[i].map); free(pchain->frames); } Py_TYPE(pchain)->tp_free((PyObject *)pchain);}
static Py_ssize_t pyrf_callchain__length(PyObject *obj){ struct pyrf_callchain *pchain = (void *)obj;
return pchain->nr_frames;}
static PyObject *pyrf_callchain__item(PyObject *obj, Py_ssize_t i){ struct pyrf_callchain *pchain = (void *)obj; struct pyrf_callchain_node *pnode;
if (i < 0 || i >= (Py_ssize_t)pchain->nr_frames) { PyErr_SetString(PyExc_IndexError, "Index out of range"); return NULL; }
pnode = PyObject_New(struct pyrf_callchain_node, &pyrf_callchain_node__type); if (!pnode) return NULL;
pnode->ip = pchain->frames[i].ip; pnode->map = map__get(pchain->frames[i].map); pnode->sym = pchain->frames[i].sym;
return (PyObject *)pnode;}
static PySequenceMethods pyrf_callchain__sequence_methods = { .sq_length = pyrf_callchain__length, .sq_item = pyrf_callchain__item,};
static PyTypeObject pyrf_callchain__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.callchain", .tp_basicsize = sizeof(struct pyrf_callchain), .tp_dealloc = (destructor)pyrf_callchain__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = "perf callchain object.", .tp_as_sequence = &pyrf_callchain__sequence_methods,};
static PyObject *pyrf_sample_event__get_callchain(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (!pevent->callchain) Py_RETURN_NONE;
Py_INCREF(pevent->callchain); return pevent->callchain;}
struct pyrf_branch_entry { PyObject_HEAD u64 from; u64 to; struct branch_flags flags;};
static void pyrf_branch_entry__delete(struct pyrf_branch_entry *pentry){ Py_TYPE(pentry)->tp_free((PyObject *)pentry);}
static PyObject *pyrf_branch_entry__get_from(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyLong_FromUnsignedLongLong(pentry->from);}
static PyObject *pyrf_branch_entry__get_to(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyLong_FromUnsignedLongLong(pentry->to);}
static PyObject *pyrf_branch_entry__get_mispred(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyBool_FromLong(pentry->flags.mispred);}
static PyObject *pyrf_branch_entry__get_predicted(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyBool_FromLong(pentry->flags.predicted);}
static PyObject *pyrf_branch_entry__get_in_tx(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyBool_FromLong(pentry->flags.in_tx);}
static PyObject *pyrf_branch_entry__get_abort(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyBool_FromLong(pentry->flags.abort);}
static PyObject *pyrf_branch_entry__get_cycles(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyLong_FromUnsignedLongLong(pentry->flags.cycles);}
static PyObject *pyrf_branch_entry__get_type(struct pyrf_branch_entry *pentry, void *closure __maybe_unused){ return PyLong_FromUnsignedLongLong((unsigned long long)pentry->flags.type);}
static PyGetSetDef pyrf_branch_entry__getset[] = { { .name = "from_ip", .get = (getter)pyrf_branch_entry__get_from, }, { .name = "to_ip", .get = (getter)pyrf_branch_entry__get_to, }, { .name = "mispred", .get = (getter)pyrf_branch_entry__get_mispred, }, { .name = "predicted", .get = (getter)pyrf_branch_entry__get_predicted, }, { .name = "in_tx", .get = (getter)pyrf_branch_entry__get_in_tx, }, { .name = "abort", .get = (getter)pyrf_branch_entry__get_abort, }, { .name = "cycles", .get = (getter)pyrf_branch_entry__get_cycles, }, { .name = "type", .get = (getter)pyrf_branch_entry__get_type, }, { .name = NULL, },};
static PyTypeObject pyrf_branch_entry__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.branch_entry", .tp_basicsize = sizeof(struct pyrf_branch_entry), .tp_dealloc = (destructor)pyrf_branch_entry__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = "perf branch entry object.", .tp_getset = pyrf_branch_entry__getset,};
struct pyrf_branch_stack { PyObject_HEAD struct branch_entry *entries; u64 nr;};
static void pyrf_branch_stack__delete(struct pyrf_branch_stack *pstack){ free(pstack->entries); Py_TYPE(pstack)->tp_free((PyObject *)pstack);}
static Py_ssize_t pyrf_branch_stack__length(PyObject *obj){ struct pyrf_branch_stack *pstack = (void *)obj;
return pstack->nr;}
static PyObject *pyrf_branch_stack__item(PyObject *obj, Py_ssize_t i){ struct pyrf_branch_stack *pstack = (void *)obj; struct pyrf_branch_entry *pentry;
if (i < 0 || i >= (Py_ssize_t)pstack->nr) { PyErr_SetString(PyExc_IndexError, "Index out of range"); return NULL; }
pentry = PyObject_New(struct pyrf_branch_entry, &pyrf_branch_entry__type); if (!pentry) return NULL;
pentry->from = pstack->entries[i].from; pentry->to = pstack->entries[i].to; pentry->flags = pstack->entries[i].flags;
return (PyObject *)pentry;}
static PySequenceMethods pyrf_branch_stack__sequence_methods = { .sq_length = pyrf_branch_stack__length, .sq_item = pyrf_branch_stack__item,};
static PyTypeObject pyrf_branch_stack__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.branch_stack", .tp_basicsize = sizeof(struct pyrf_branch_stack), .tp_dealloc = (destructor)pyrf_branch_stack__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = "perf branch stack object.", .tp_as_sequence = &pyrf_branch_stack__sequence_methods,};
static PyObject *pyrf_sample_event__get_brstack(PyObject *self, void *closure __maybe_unused){ struct pyrf_event *pevent = (void *)self;
if (!pevent->brstack) Py_RETURN_NONE;
Py_INCREF(pevent->brstack); return pevent->brstack;}
static PyObject*pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name){ PyObject *obj = NULL;
#ifdef HAVE_LIBTRACEEVENT if (is_tracepoint(pevent)) obj = get_tracepoint_field(pevent, attr_name);#endif
return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);}
static PyGetSetDef pyrf_sample_event__getset[] = { { .name = "callchain", .get = pyrf_sample_event__get_callchain, .set = NULL, .doc = "event callchain.", }, { .name = "brstack", .get = pyrf_sample_event__get_brstack, .set = NULL, .doc = "event branch stack.", }, { .name = "raw_buf", .get = (getter)pyrf_sample_event__get_raw_buf, .set = NULL, .doc = "event raw buffer.", }, { .name = "evsel", .get = pyrf_event__get_evsel, .set = NULL, .doc = "tracking event.", }, { .name = "dso", .get = (getter)pyrf_sample_event__get_dso, .set = NULL, .doc = "event dso short name.", }, { .name = "dso_long_name", .get = (getter)pyrf_sample_event__get_dso_long_name, .set = NULL, .doc = "event dso long name.", }, { .name = "dso_bid", .get = (getter)pyrf_sample_event__get_dso_bid, .set = NULL, .doc = "event dso build id.", }, { .name = "map_start", .get = (getter)pyrf_sample_event__get_map_start, .set = NULL, .doc = "event map start address.", }, { .name = "map_end", .get = (getter)pyrf_sample_event__get_map_end, .set = NULL, .doc = "event map end address.", }, { .name = "map_pgoff", .get = (getter)pyrf_sample_event__get_map_pgoff, .set = NULL, .doc = "event map page offset.", }, { .name = "symbol", .get = (getter)pyrf_sample_event__get_symbol, .set = NULL, .doc = "event symbol name.", }, { .name = "sym_start", .get = (getter)pyrf_sample_event__get_sym_start, .set = NULL, .doc = "event symbol start address.", }, { .name = "sym_end", .get = (getter)pyrf_sample_event__get_sym_end, .set = NULL, .doc = "event symbol end address.", }, { .name = NULL, },};
static PyMethodDef pyrf_sample_event__methods[] = { { .ml_name = "srccode", .ml_meth = (PyCFunction)pyrf_sample_event__srccode, .ml_flags = METH_VARARGS, .ml_doc = PyDoc_STR("Get source code for an address.") }, { .ml_name = "insn", .ml_meth = (PyCFunction)pyrf_sample_event__insn, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Get instruction bytes for a sample.") }, { .ml_name = NULL, }};
static PyTypeObject pyrf_sample_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.sample_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_sample_event__doc, .tp_members = pyrf_sample_event__members, .tp_getset = pyrf_sample_event__getset, .tp_methods = pyrf_sample_event__methods, .tp_repr = (reprfunc)pyrf_sample_event__repr, .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,};
static const char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
static PyMemberDef pyrf_context_switch_event__members[] = { sample_members member_def(perf_event_header, type, T_UINT, "event type"), member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"), member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"), { .name = NULL, },};
static PyObject *pyrf_context_switch_event__repr(const struct pyrf_event *pevent){ PyObject *ret; char *s;
if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }", pevent->event.context_switch.next_prev_pid, pevent->event.context_switch.next_prev_tid, !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { ret = PyErr_NoMemory(); } else { ret = PyUnicode_FromString(s); free(s); } return ret;}
static PyTypeObject pyrf_context_switch_event__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.context_switch_event", .tp_basicsize = sizeof(struct pyrf_event), .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_context_switch_event__doc, .tp_members = pyrf_context_switch_event__members, .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_context_switch_event__repr,};
static int pyrf_event__setup_types(void){ int err;
err = PyType_Ready(&pyrf_mmap_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_mmap2_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_lost_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_task_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_comm_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_throttle_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_read_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_sample_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_context_switch_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_stat_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_stat_round_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_callchain_node__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_callchain__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_branch_entry__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_branch_stack__type); if (err < 0) goto out;out: return err;}
static PyTypeObject *pyrf_event__type[] = { [PERF_RECORD_MMAP] = &pyrf_mmap_event__type, [PERF_RECORD_MMAP2] = &pyrf_mmap2_event__type, [PERF_RECORD_LOST] = &pyrf_lost_event__type, [PERF_RECORD_COMM] = &pyrf_comm_event__type, [PERF_RECORD_EXIT] = &pyrf_task_event__type, [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type, [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type, [PERF_RECORD_FORK] = &pyrf_task_event__type, [PERF_RECORD_READ] = &pyrf_read_event__type, [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, [PERF_RECORD_STAT] = &pyrf_stat_event__type, [PERF_RECORD_STAT_ROUND] = &pyrf_stat_round_event__type,};
static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel, struct perf_session *session, struct machine *machine){ struct pyrf_event *pevent; struct perf_sample *sample; int err; u32 min_size; bool needs_swap;
if (!machine) machine = session ? &session->machines.host : NULL;
if (event->header.type >= ARRAY_SIZE(pyrf_event__type) || pyrf_event__type[event->header.type] == NULL) { return PyErr_Format(PyExc_TypeError, "Unexpected header type %u", event->header.type); }
if (perf_event__too_small(event, &min_size)) { return PyErr_Format(PyExc_ValueError, "Event size %u too small for type %u", event->header.size, event->header.type); }
size_t copy_size = event->header.size;
if (copy_size > sizeof(pevent->event)) { return PyErr_Format(PyExc_TypeError, "Unexpected event size: %zd < %zu", sizeof(pevent->event), copy_size); }
pevent = PyObject_New(struct pyrf_event, pyrf_event__type[event->header.type]); if (pevent == NULL) return PyErr_NoMemory();
/* Copy the event for memory safety and initialize variables. */ memcpy(&pevent->event, event, copy_size); if (copy_size < sizeof(pevent->event)) memset((char *)&pevent->event + copy_size, 0, sizeof(pevent->event) - copy_size);
if (event->header.type == PERF_RECORD_MMAP2) pevent->event.mmap2.filename[sizeof(pevent->event.mmap2.filename) - 1] = '\0';
perf_sample__init(&pevent->sample, /*all=*/true); pevent->callchain = NULL; pevent->brstack = NULL; pevent->al_resolved = false; addr_location__init(&pevent->al);
if (!evsel) return (PyObject *)pevent;
/* Parse the sample again so that pointers are within the copied event. */ needs_swap = evsel->needs_swap;
evsel->needs_swap = false; err = evsel__parse_sample(evsel, &pevent->event, &pevent->sample); evsel->needs_swap = needs_swap; if (err < 0) { Py_DECREF(pevent); return PyErr_Format(PyExc_OSError, "perf: can't parse sample, err=%d", err); } sample = &pevent->sample; if (machine && sample->callchain) { struct addr_location al; struct callchain_cursor *cursor; u64 i; struct pyrf_callchain *pchain;
addr_location__init(&al); if (machine__resolve(machine, &al, sample) >= 0) { cursor = get_tls_callchain_cursor(); if (thread__resolve_callchain(al.thread, cursor, sample, NULL, NULL, PERF_MAX_STACK_DEPTH) == 0) { callchain_cursor_commit(cursor);
pchain = PyObject_New(struct pyrf_callchain, &pyrf_callchain__type); if (!pchain) { addr_location__exit(&al); Py_DECREF(pevent); return NULL; } pchain->nr_frames = cursor->nr; pchain->frames = calloc(pchain->nr_frames, sizeof(*pchain->frames)); if (!pchain->frames) { Py_DECREF(pchain); addr_location__exit(&al); Py_DECREF(pevent); return PyErr_NoMemory(); } struct callchain_cursor_node *node;
for (i = 0; i < pchain->nr_frames; i++) { node = callchain_cursor_current(cursor); pchain->frames[i].ip = node->ip; pchain->frames[i].map = map__get(node->ms.map); pchain->frames[i].sym = node->ms.sym; callchain_cursor_advance(cursor); } pevent->callchain = (PyObject *)pchain; } addr_location__exit(&al); } } if (sample->branch_stack) { struct branch_stack *bs = sample->branch_stack; struct branch_entry *entries = perf_sample__branch_entries(sample); struct pyrf_branch_stack *pstack;
pstack = PyObject_New(struct pyrf_branch_stack, &pyrf_branch_stack__type); if (!pstack) { Py_DECREF(pevent); return NULL; } pstack->nr = bs->nr; pstack->entries = calloc(bs->nr, sizeof(struct branch_entry)); if (!pstack->entries) { Py_DECREF(pstack); Py_DECREF(pevent); return PyErr_NoMemory(); } memcpy(pstack->entries, entries, bs->nr * sizeof(struct branch_entry)); pevent->brstack = (PyObject *)pstack; } return (PyObject *)pevent;}
struct pyrf_cpu_map { PyObject_HEAD
struct perf_cpu_map *cpus;};
static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, PyObject *args, PyObject *kwargs){ static char *kwlist[] = { "cpustr", NULL }; char *cpustr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &cpustr)) return -1;
pcpus->cpus = perf_cpu_map__new(cpustr); if (pcpus->cpus == NULL) return -1; return 0;}
static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus){ perf_cpu_map__put(pcpus->cpus); Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);}
static Py_ssize_t pyrf_cpu_map__length(PyObject *obj){ struct pyrf_cpu_map *pcpus = (void *)obj;
return perf_cpu_map__nr(pcpus->cpus);}
static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i){ struct pyrf_cpu_map *pcpus = (void *)obj;
if (i >= perf_cpu_map__nr(pcpus->cpus)) { PyErr_SetString(PyExc_IndexError, "Index out of range"); return NULL; }
return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu);}
static PySequenceMethods pyrf_cpu_map__sequence_methods = { .sq_length = pyrf_cpu_map__length, .sq_item = pyrf_cpu_map__item,};
static const char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
static PyTypeObject pyrf_cpu_map__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.cpu_map", .tp_basicsize = sizeof(struct pyrf_cpu_map), .tp_dealloc = (destructor)pyrf_cpu_map__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_cpu_map__doc, .tp_as_sequence = &pyrf_cpu_map__sequence_methods, .tp_init = (initproc)pyrf_cpu_map__init,};
static int pyrf_cpu_map__setup_types(void){ pyrf_cpu_map__type.tp_new = PyType_GenericNew; return PyType_Ready(&pyrf_cpu_map__type);}
struct pyrf_thread_map { PyObject_HEAD
struct perf_thread_map *threads;};
static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, PyObject *args, PyObject *kwargs){ static char *kwlist[] = { "pid", "tid", NULL }; int pid = -1, tid = -1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, &pid, &tid)) return -1;
pthreads->threads = thread_map__new(pid, tid); if (pthreads->threads == NULL) return -1; return 0;}
static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads){ perf_thread_map__put(pthreads->threads); Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);}
static Py_ssize_t pyrf_thread_map__length(PyObject *obj){ struct pyrf_thread_map *pthreads = (void *)obj;
return perf_thread_map__nr(pthreads->threads);}
static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i){ struct pyrf_thread_map *pthreads = (void *)obj;
if (i >= perf_thread_map__nr(pthreads->threads)) { PyErr_SetString(PyExc_IndexError, "Index out of range"); return NULL; }
return Py_BuildValue("i", perf_thread_map__pid(pthreads->threads, i));}
static PySequenceMethods pyrf_thread_map__sequence_methods = { .sq_length = pyrf_thread_map__length, .sq_item = pyrf_thread_map__item,};
static const char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
static PyTypeObject pyrf_thread_map__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.thread_map", .tp_basicsize = sizeof(struct pyrf_thread_map), .tp_dealloc = (destructor)pyrf_thread_map__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_thread_map__doc, .tp_as_sequence = &pyrf_thread_map__sequence_methods, .tp_init = (initproc)pyrf_thread_map__init,};
static int pyrf_thread_map__setup_types(void){ pyrf_thread_map__type.tp_new = PyType_GenericNew; return PyType_Ready(&pyrf_thread_map__type);}
/** * A python wrapper for perf_pmus that are globally owned by the pmus.c code. */struct pyrf_pmu { PyObject_HEAD
struct perf_pmu *pmu;};
static void pyrf_pmu__delete(struct pyrf_pmu *ppmu){ Py_TYPE(ppmu)->tp_free((PyObject *)ppmu);}
static PyObject *pyrf_pmu__name(PyObject *self){ struct pyrf_pmu *ppmu = (void *)self;
CHECK_INITIALIZED(ppmu->pmu, "pmu"); return PyUnicode_FromString(ppmu->pmu->name);}
static bool add_to_dict(PyObject *dict, const char *key, const char *value){ PyObject *pkey, *pvalue; bool ret;
if (value == NULL) return true;
pkey = PyUnicode_FromString(key); pvalue = PyUnicode_FromString(value);
ret = pkey && pvalue && PyDict_SetItem(dict, pkey, pvalue) == 0; Py_XDECREF(pkey); Py_XDECREF(pvalue); return ret;}
static int pyrf_pmu__events_cb(void *state, struct pmu_event_info *info){ PyObject *py_list = state; PyObject *dict = PyDict_New();
if (!dict) return -ENOMEM;
if (!add_to_dict(dict, "name", info->name) || !add_to_dict(dict, "alias", info->alias) || !add_to_dict(dict, "scale_unit", info->scale_unit) || !add_to_dict(dict, "desc", info->desc) || !add_to_dict(dict, "long_desc", info->long_desc) || !add_to_dict(dict, "encoding_desc", info->encoding_desc) || !add_to_dict(dict, "topic", info->topic) || !add_to_dict(dict, "event_type_desc", info->event_type_desc) || !add_to_dict(dict, "str", info->str) || !add_to_dict(dict, "deprecated", info->deprecated ? "deprecated" : NULL) || PyList_Append(py_list, dict) != 0) { Py_DECREF(dict); return -ENOMEM; } Py_DECREF(dict); return 0;}
static PyObject *pyrf_pmu__events(PyObject *self){ struct pyrf_pmu *ppmu = (void *)self; PyObject *py_list; int ret;
CHECK_INITIALIZED(ppmu->pmu, "pmu");
py_list = PyList_New(0); if (!py_list) return NULL;
ret = perf_pmu__for_each_event(ppmu->pmu, /*skip_duplicate_pmus=*/false, py_list, pyrf_pmu__events_cb); if (ret) { Py_DECREF(py_list); errno = -ret; PyErr_SetFromErrno(PyExc_OSError); return NULL; } return py_list;}
static PyObject *pyrf_pmu__repr(PyObject *self){ struct pyrf_pmu *ppmu = (void *)self;
CHECK_INITIALIZED(ppmu->pmu, "pmu"); return PyUnicode_FromFormat("pmu(%s)", ppmu->pmu->name);}
static const char pyrf_pmu__doc[] = PyDoc_STR("perf Performance Monitoring Unit (PMU) object.");
static PyMethodDef pyrf_pmu__methods[] = { { .ml_name = "events", .ml_meth = (PyCFunction)pyrf_pmu__events, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Returns a sequence of events encoded as a dictionaries.") }, { .ml_name = "name", .ml_meth = (PyCFunction)pyrf_pmu__name, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Name of the PMU including suffixes.") }, { .ml_name = NULL, }};
/** The python type for a perf.pmu. */static PyTypeObject pyrf_pmu__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.pmu", .tp_basicsize = sizeof(struct pyrf_pmu), .tp_dealloc = (destructor)pyrf_pmu__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_pmu__doc, .tp_methods = pyrf_pmu__methods, .tp_str = pyrf_pmu__name, .tp_repr = pyrf_pmu__repr,};
static int pyrf_pmu__setup_types(void){ pyrf_pmu__type.tp_new = PyType_GenericNew; return PyType_Ready(&pyrf_pmu__type);}
/** A python iterator for pmus that has no equivalent in the C code. */struct pyrf_pmu_iterator { PyObject_HEAD struct perf_pmu *pmu;};
static void pyrf_pmu_iterator__dealloc(struct pyrf_pmu_iterator *self){ Py_TYPE(self)->tp_free((PyObject *) self);}
static PyObject *pyrf_pmu_iterator__new(PyTypeObject *type, PyObject *args __maybe_unused, PyObject *kwds __maybe_unused){ struct pyrf_pmu_iterator *itr = (void *)type->tp_alloc(type, 0);
if (itr != NULL) itr->pmu = perf_pmus__scan(/*pmu=*/NULL);
return (PyObject *) itr;}
static PyObject *pyrf_pmu_iterator__iter(PyObject *self){ Py_INCREF(self); return self;}
static PyObject *pyrf_pmu_iterator__iternext(PyObject *self){ struct pyrf_pmu_iterator *itr = (void *)self; struct pyrf_pmu *ppmu;
if (itr->pmu == NULL) { PyErr_SetNone(PyExc_StopIteration); return NULL; } // Create object to return. ppmu = PyObject_New(struct pyrf_pmu, &pyrf_pmu__type); if (ppmu) { ppmu->pmu = itr->pmu; // Advance iterator. itr->pmu = perf_pmus__scan(itr->pmu); } return (PyObject *)ppmu;}
/** The python type for the PMU iterator. */static PyTypeObject pyrf_pmu_iterator__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pmus.iterator", .tp_doc = "Iterator for the pmus string sequence.", .tp_basicsize = sizeof(struct pyrf_pmu_iterator), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT, .tp_new = pyrf_pmu_iterator__new, .tp_dealloc = (destructor) pyrf_pmu_iterator__dealloc, .tp_iter = pyrf_pmu_iterator__iter, .tp_iternext = pyrf_pmu_iterator__iternext,};
static int pyrf_pmu_iterator__setup_types(void){ return PyType_Ready(&pyrf_pmu_iterator__type);}
static PyObject *pyrf__pmus(PyObject *self, PyObject *args){ // Calling the class creates an instance of the iterator. return PyObject_CallObject((PyObject *) &pyrf_pmu_iterator__type, /*args=*/NULL);}
struct pyrf_counts_values { PyObject_HEAD
struct perf_counts_values values;};
static const char pyrf_counts_values__doc[] = PyDoc_STR("perf counts values object.");
static void pyrf_counts_values__delete(struct pyrf_counts_values *pcounts_values){ Py_TYPE(pcounts_values)->tp_free((PyObject *)pcounts_values);}
#define counts_values_member_def(member, ptype, help) \ { #member, ptype, \ offsetof(struct pyrf_counts_values, values.member), \ 0, help }
static PyMemberDef pyrf_counts_values_members[] = { counts_values_member_def(val, T_ULONGLONG, "Value of event"), counts_values_member_def(ena, T_ULONGLONG, "Time for which enabled"), counts_values_member_def(run, T_ULONGLONG, "Time for which running"), counts_values_member_def(id, T_ULONGLONG, "Unique ID for an event"), counts_values_member_def(lost, T_ULONGLONG, "Num of lost samples"), { .name = NULL, },};
static PyObject *pyrf_counts_values_get_values(struct pyrf_counts_values *self, void *closure){ PyObject *vals = PyList_New(5);
if (!vals) return NULL; for (int i = 0; i < 5; i++) { PyObject *val = PyLong_FromUnsignedLongLong(self->values.values[i]);
if (!val) { Py_DECREF(vals); return NULL; } PyList_SetItem(vals, i, val); }
return vals;}
static int pyrf_counts_values_set_values(struct pyrf_counts_values *self, PyObject *list, void *closure){ Py_ssize_t size; PyObject *item = NULL;
if (list == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
if (!PyList_Check(list)) { PyErr_SetString(PyExc_TypeError, "Value assigned must be a list"); return -1; }
size = PyList_Size(list); if (size != 5) { PyErr_SetString(PyExc_ValueError, "List must have exactly 5 entries"); return -1; }
for (Py_ssize_t i = 0; i < size; i++) { unsigned long long val;
item = PyList_GetItem(list, i); if (!PyLong_Check(item)) { PyErr_SetString(PyExc_TypeError, "List members should be numbers"); return -1; } val = PyLong_AsUnsignedLongLong(item); if (val == (unsigned long long)-1 && PyErr_Occurred()) return -1; self->values.values[i] = val; }
return 0;}
static PyGetSetDef pyrf_counts_values_getset[] = { {"values", (getter)pyrf_counts_values_get_values, (setter)pyrf_counts_values_set_values, "Name field", NULL}, { .name = NULL, },};
static PyTypeObject pyrf_counts_values__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.counts_values", .tp_basicsize = sizeof(struct pyrf_counts_values), .tp_dealloc = (destructor)pyrf_counts_values__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_counts_values__doc, .tp_members = pyrf_counts_values_members, .tp_getset = pyrf_counts_values_getset,};
static int pyrf_counts_values__setup_types(void){ pyrf_counts_values__type.tp_new = PyType_GenericNew; return PyType_Ready(&pyrf_counts_values__type);}
struct pyrf_evsel { PyObject_HEAD
struct evsel *evsel;};
static int pyrf_evsel__init(struct pyrf_evsel *pevsel, PyObject *args, PyObject *kwargs){ struct perf_event_attr attr = { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES, .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID, }; static char *kwlist[] = { "type", "config", "sample_freq", "sample_period", "sample_type", "read_format", "disabled", "inherit", "pinned", "exclusive", "exclude_user", "exclude_kernel", "exclude_hv", "exclude_idle", "mmap", "context_switch", "comm", "freq", "inherit_stat", "enable_on_exec", "task", "watermark", "precise_ip", "mmap_data", "sample_id_all", "wakeup_events", "bp_type", "bp_addr", "bp_len", "idx", NULL }; u64 sample_period = 0; u32 disabled = 0, inherit = 0, pinned = 0, exclusive = 0, exclude_user = 0, exclude_kernel = 0, exclude_hv = 0, exclude_idle = 0, mmap = 0, context_switch = 0, comm = 0, freq = 1, inherit_stat = 0, enable_on_exec = 0, task = 0, watermark = 0, precise_ip = 0, mmap_data = 0, sample_id_all = 1, idx = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKKi", kwlist, &attr.type, &attr.config, &attr.sample_freq, &sample_period, &attr.sample_type, &attr.read_format, &disabled, &inherit, &pinned, &exclusive, &exclude_user, &exclude_kernel, &exclude_hv, &exclude_idle, &mmap, &context_switch, &comm, &freq, &inherit_stat, &enable_on_exec, &task, &watermark, &precise_ip, &mmap_data, &sample_id_all, &attr.wakeup_events, &attr.bp_type, &attr.bp_addr, &attr.bp_len, &idx)) return -1;
/* union... */ if (sample_period != 0) { if (attr.sample_freq != 0) return -1; /* FIXME: throw right exception */ attr.sample_period = sample_period; }
/* Bitfields */ attr.disabled = disabled; attr.inherit = inherit; attr.pinned = pinned; attr.exclusive = exclusive; attr.exclude_user = exclude_user; attr.exclude_kernel = exclude_kernel; attr.exclude_hv = exclude_hv; attr.exclude_idle = exclude_idle; attr.mmap = mmap; attr.context_switch = context_switch; attr.comm = comm; attr.freq = freq; attr.inherit_stat = inherit_stat; attr.enable_on_exec = enable_on_exec; attr.task = task; attr.watermark = watermark; attr.precise_ip = precise_ip; attr.mmap_data = mmap_data; attr.sample_id_all = sample_id_all; attr.size = sizeof(attr);
evsel__put(pevsel->evsel); pevsel->evsel = evsel__new(&attr); if (!pevsel->evsel) { PyErr_NoMemory(); return -1; } return 0;}
static void pyrf_evsel__delete(struct pyrf_evsel *pevsel){ evsel__put(pevsel->evsel); Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);}
static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, PyObject *args, PyObject *kwargs){ struct evsel *evsel = pevsel->evsel; struct perf_cpu_map *cpus = NULL; struct perf_thread_map *threads = NULL; PyObject *pcpus = NULL, *pthreads = NULL; int group = 0, inherit = 0; static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
CHECK_INITIALIZED(evsel, "evsel");
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &pcpus, &pthreads, &group, &inherit)) return NULL;
if (pthreads != NULL && pthreads != Py_None) { if (!PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) { PyErr_SetString(PyExc_TypeError, "threads must be a thread_map"); return NULL; } threads = ((struct pyrf_thread_map *)pthreads)->threads; }
if (pcpus != NULL && pcpus != Py_None) { if (!PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) { PyErr_SetString(PyExc_TypeError, "cpus must be a cpu_map"); return NULL; } cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; }
evsel->core.attr.inherit = inherit; /* * This will group just the fds for this single evsel, to group * multiple events, use evlist.open(). */ if (evsel__open(evsel, cpus, threads) < 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
Py_INCREF(Py_None); return Py_None;}
static PyObject *pyrf_evsel__cpus(struct pyrf_evsel *pevsel){ struct pyrf_cpu_map *pcpu_map;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
pcpu_map = PyObject_New(struct pyrf_cpu_map, &pyrf_cpu_map__type); if (pcpu_map) pcpu_map->cpus = perf_cpu_map__get(pevsel->evsel->core.cpus);
return (PyObject *)pcpu_map;}
static PyObject *pyrf_evsel__threads(struct pyrf_evsel *pevsel){ struct pyrf_thread_map *pthread_map;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
pthread_map = PyObject_New(struct pyrf_thread_map, &pyrf_thread_map__type); if (pthread_map) pthread_map->threads = perf_thread_map__get(pevsel->evsel->core.threads);
return (PyObject *)pthread_map;}
/* * Ensure evsel's counts and prev_raw_counts are allocated, the latter * used by tool PMUs to compute the cumulative count as expected by * stat's process_counter_values. */static int evsel__ensure_counts(struct evsel *evsel){ int nthreads, ncpus;
if (evsel->counts != NULL) return 0;
nthreads = perf_thread_map__nr(evsel->core.threads); ncpus = perf_cpu_map__nr(evsel->core.cpus);
evsel->counts = perf_counts__new(ncpus, nthreads); if (evsel->counts == NULL) return -ENOMEM;
evsel->prev_raw_counts = perf_counts__new(ncpus, nthreads); if (evsel->prev_raw_counts == NULL) return -ENOMEM;
return 0;}
static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel, PyObject *args, PyObject *kwargs){ struct evsel *evsel = pevsel->evsel; int cpu = 0, cpu_idx, thread = 0, thread_idx; struct perf_counts_values *old_count, *new_count; struct pyrf_counts_values *count_values;
CHECK_INITIALIZED(evsel, "evsel");
if (!PyArg_ParseTuple(args, "ii", &cpu, &thread)) return NULL;
cpu_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){.cpu = cpu}); if (cpu_idx < 0) { PyErr_Format(PyExc_TypeError, "CPU %d is not part of evsel's CPUs", cpu); return NULL; } thread_idx = perf_thread_map__idx(evsel->core.threads, thread); if (thread_idx < 0) { PyErr_Format(PyExc_TypeError, "Thread %d is not part of evsel's threads", thread); return NULL; }
if (evsel__ensure_counts(evsel)) return PyErr_NoMemory();
count_values = PyObject_New(struct pyrf_counts_values, &pyrf_counts_values__type); if (!count_values) return NULL;
/* Set up pointers to the old and newly read counter values. */ old_count = perf_counts(evsel->prev_raw_counts, cpu_idx, thread_idx); new_count = perf_counts(evsel->counts, cpu_idx, thread_idx); /* Update the value in evsel->counts. */ evsel__read_counter(evsel, cpu_idx, thread_idx); /* Copy the value and turn it into the delta from old_count. */ count_values->values = *new_count; count_values->values.val -= old_count->val; count_values->values.ena -= old_count->ena; count_values->values.run -= old_count->run; /* Save the new count over the old_count for the next read. */ *old_count = *new_count; return (PyObject *)count_values;}
static PyObject *pyrf_evsel__str(PyObject *self){ struct pyrf_evsel *pevsel = (void *)self; struct evsel *evsel = pevsel->evsel;
if (!evsel) return PyUnicode_FromString("evsel(uninitialized)");
return PyUnicode_FromFormat("evsel(%s)", evsel__name(evsel));}
static PyMethodDef pyrf_evsel__methods[] = { { .ml_name = "open", .ml_meth = (PyCFunction)pyrf_evsel__open, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("open the event selector file descriptor table.") }, { .ml_name = "cpus", .ml_meth = (PyCFunction)pyrf_evsel__cpus, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("CPUs the event is to be used with.") }, { .ml_name = "threads", .ml_meth = (PyCFunction)pyrf_evsel__threads, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("threads the event is to be used with.") }, { .ml_name = "read", .ml_meth = (PyCFunction)pyrf_evsel__read, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("read counters") }, { .ml_name = NULL, }};
static PyObject *pyrf_evsel__get_tracking(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
if (pevsel->evsel->tracking) Py_RETURN_TRUE; else Py_RETURN_FALSE;}
static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; int is_true;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
is_true = PyObject_IsTrue(val); if (is_true < 0) return -1;
pevsel->evsel->tracking = is_true; return 0;}
static int pyrf_evsel__set_attr_config(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
new_val = PyLong_AsUnsignedLongLong(val); if (PyErr_Occurred()) return -1;
pevsel->evsel->core.attr.config = new_val; return 0;}
static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLongLong(pevsel->evsel->core.attr.config);}
static int pyrf_evsel__set_attr_read_format(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
new_val = PyLong_AsUnsignedLongLong(val); if (PyErr_Occurred()) return -1;
pevsel->evsel->core.attr.read_format = new_val; return 0;}
static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLongLong(pevsel->evsel->core.attr.read_format);}
static int pyrf_evsel__set_attr_sample_period(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
new_val = PyLong_AsUnsignedLongLong(val); if (PyErr_Occurred()) return -1;
pevsel->evsel->core.attr.sample_period = new_val; return 0;}
static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLongLong(pevsel->evsel->core.attr.sample_period);}
static int pyrf_evsel__set_attr_sample_type(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
new_val = PyLong_AsUnsignedLongLong(val); if (PyErr_Occurred()) return -1;
pevsel->evsel->core.attr.sample_type = new_val; return 0;}
static PyObject *pyrf_evsel__get_attr_sample_type(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLongLong(pevsel->evsel->core.attr.sample_type);}
static PyObject *pyrf_evsel__get_attr_size(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLong(pevsel->evsel->core.attr.size);}
static int pyrf_evsel__set_attr_type(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
new_val = PyLong_AsUnsignedLong(val); if (PyErr_Occurred()) return -1;
pevsel->evsel->core.attr.type = new_val; return 0;}
static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLong(pevsel->evsel->core.attr.type);}
static int pyrf_evsel__set_attr_wakeup_events(PyObject *self, PyObject *val, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self; unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); return -1; }
new_val = PyLong_AsUnsignedLong(val); if (PyErr_Occurred()) return -1;
pevsel->evsel->core.attr.wakeup_events = new_val; return 0;}
static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void *closure __maybe_unused){ struct pyrf_evsel *pevsel = (void *)self;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
return PyLong_FromUnsignedLong(pevsel->evsel->core.attr.wakeup_events);}
static PyObject *pyrf_evsel__get_ids(struct pyrf_evsel *pevsel, void *closure __maybe_unused){ struct evsel *evsel; PyObject *list;
CHECK_INITIALIZED(pevsel->evsel, "evsel");
evsel = pevsel->evsel; list = PyList_New(0);
if (!list) return NULL;
for (u32 i = 0; i < evsel->core.ids; i++) { PyObject *id = PyLong_FromUnsignedLongLong(evsel->core.id[i]); int ret;
if (!id) { Py_DECREF(list); return NULL; } ret = PyList_Append(list, id); Py_DECREF(id); if (ret < 0) { Py_DECREF(list); return NULL; } }
return list;}
static PyGetSetDef pyrf_evsel__getset[] = { { .name = "ids", .get = (getter)pyrf_evsel__get_ids, .set = NULL, .doc = "event IDs.", }, { .name = "tracking", .get = pyrf_evsel__get_tracking, .set = pyrf_evsel__set_tracking, .doc = "tracking event.", }, { .name = "config", .get = pyrf_evsel__get_attr_config, .set = pyrf_evsel__set_attr_config, .doc = "attribute config.", }, { .name = "read_format", .get = pyrf_evsel__get_attr_read_format, .set = pyrf_evsel__set_attr_read_format, .doc = "attribute read_format.", }, { .name = "sample_period", .get = pyrf_evsel__get_attr_sample_period, .set = pyrf_evsel__set_attr_sample_period, .doc = "attribute sample_period.", }, { .name = "sample_type", .get = pyrf_evsel__get_attr_sample_type, .set = pyrf_evsel__set_attr_sample_type, .doc = "attribute sample_type.", }, { .name = "size", .get = pyrf_evsel__get_attr_size, .doc = "attribute size.", }, { .name = "type", .get = pyrf_evsel__get_attr_type, .set = pyrf_evsel__set_attr_type, .doc = "attribute type.", }, { .name = "wakeup_events", .get = pyrf_evsel__get_attr_wakeup_events, .set = pyrf_evsel__set_attr_wakeup_events, .doc = "attribute wakeup_events.", }, { .name = NULL},};
static const char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
static PyObject *pyrf_evsel__getattro(struct pyrf_evsel *pevsel, PyObject *attr_name){ if (!pevsel->evsel) { PyErr_SetString(PyExc_ValueError, "evsel not initialized"); return NULL; } return PyObject_GenericGetAttr((PyObject *) pevsel, attr_name);}
static int pyrf_evsel__setattro(struct pyrf_evsel *pevsel, PyObject *attr_name, PyObject *value){ if (!pevsel->evsel) { PyErr_SetString(PyExc_ValueError, "evsel not initialized"); return -1; } return PyObject_GenericSetAttr((PyObject *) pevsel, attr_name, value);}
static PyTypeObject pyrf_evsel__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.evsel", .tp_basicsize = sizeof(struct pyrf_evsel), .tp_dealloc = (destructor)pyrf_evsel__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_evsel__doc, .tp_getset = pyrf_evsel__getset, .tp_methods = pyrf_evsel__methods, .tp_init = (initproc)pyrf_evsel__init, .tp_str = pyrf_evsel__str, .tp_repr = pyrf_evsel__str, .tp_getattro = (getattrofunc) pyrf_evsel__getattro, .tp_setattro = (setattrofunc) pyrf_evsel__setattro,};
static PyObject *pyrf_evsel__new(PyTypeObject *type, PyObject *args, PyObject *kwargs){ struct pyrf_evsel *pevsel;
pevsel = (struct pyrf_evsel *)PyType_GenericNew(type, args, kwargs); if (pevsel) pevsel->evsel = NULL; return (PyObject *)pevsel;}
static int pyrf_evsel__setup_types(void){ pyrf_evsel__type.tp_new = pyrf_evsel__new; return PyType_Ready(&pyrf_evsel__type);}
struct pyrf_evlist { PyObject_HEAD
struct evlist *evlist;};
static int pyrf_evlist__init(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs __maybe_unused){ PyObject *pcpus = NULL, *pthreads = NULL; struct perf_cpu_map *cpus; struct perf_thread_map *threads;
if (!PyArg_ParseTuple(args, "O!O!", &pyrf_cpu_map__type, &pcpus, &pyrf_thread_map__type, &pthreads)) return -1;
evlist__put(pevlist->evlist); pevlist->evlist = evlist__new(); if (!pevlist->evlist) { PyErr_NoMemory(); return -1; } threads = ((struct pyrf_thread_map *)pthreads)->threads; cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; perf_evlist__set_maps(evlist__core(pevlist->evlist), cpus, threads);
return 0;}
static void pyrf_evlist__delete(struct pyrf_evlist *pevlist){ evlist__put(pevlist->evlist); Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);}
static PyObject *pyrf_evlist__all_cpus(struct pyrf_evlist *pevlist){ struct pyrf_cpu_map *pcpu_map;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
pcpu_map = PyObject_New(struct pyrf_cpu_map, &pyrf_cpu_map__type); if (pcpu_map) pcpu_map->cpus = perf_cpu_map__get(evlist__core(pevlist->evlist)->all_cpus);
return (PyObject *)pcpu_map;}
static PyObject *pyrf_evlist__metrics(struct pyrf_evlist *pevlist){ PyObject *list; struct rb_node *node;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
list = PyList_New(/*len=*/0); if (!list) return NULL;
for (node = rb_first_cached(&evlist__metric_events(pevlist->evlist)->entries); node; node = rb_next(node)) { struct metric_event *me = container_of(node, struct metric_event, nd); struct list_head *pos;
list_for_each(pos, &me->head) { struct metric_expr *expr = container_of(pos, struct metric_expr, nd); PyObject *str = PyUnicode_FromString(expr->metric_name);
if (!str || PyList_Append(list, str) != 0) { Py_DECREF(list); return NULL; } Py_DECREF(str); } } return list;}
static int prepare_metric(const struct metric_expr *mexp, const struct evsel *evsel, struct expr_parse_ctx *pctx, int cpu_idx, int thread_idx){ struct evsel * const *metric_events = mexp->metric_events; struct metric_ref *metric_refs = mexp->metric_refs;
for (int i = 0; metric_events[i]; i++) { struct evsel *cur = metric_events[i]; double val, ena, run; int ret, source_count = 0; struct perf_counts_values *old_count, *new_count; char *n = strdup(evsel__metric_id(cur));
if (!n) return -ENOMEM;
/* * If there are multiple uncore PMUs and we're not reading the * leader's stats, determine the stats for the appropriate * uncore PMU. */ if (evsel && evsel->metric_leader && evsel->pmu != evsel->metric_leader->pmu && cur->pmu == evsel->metric_leader->pmu) { struct evsel *pos;
evlist__for_each_entry(evsel->evlist, pos) { if (pos->pmu != evsel->pmu) continue; if (pos->metric_leader != cur) continue; cur = pos; source_count = 1; break; } }
if (source_count == 0) source_count = evsel__source_count(cur);
ret = evsel__ensure_counts(cur); if (ret) return ret;
/* Set up pointers to the old and newly read counter values. */ old_count = perf_counts(cur->prev_raw_counts, cpu_idx, thread_idx); new_count = perf_counts(cur->counts, cpu_idx, thread_idx); /* Update the value in cur->counts. */ evsel__read_counter(cur, cpu_idx, thread_idx);
val = new_count->val - old_count->val; ena = new_count->ena - old_count->ena; run = new_count->run - old_count->run;
if (ena != run && run != 0) val = val * ena / run; ret = expr__add_id_val_source_count(pctx, n, val, source_count); if (ret) return ret; }
for (int i = 0; metric_refs && metric_refs[i].metric_name; i++) { int ret = expr__add_ref(pctx, &metric_refs[i]);
if (ret) return ret; }
return 0;}
static PyObject *pyrf_evlist__compute_metric(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs){ int ret, cpu = 0, cpu_idx = 0, thread = 0, thread_idx = 0; const char *metric; struct rb_node *node; struct metric_expr *mexp = NULL; struct expr_parse_ctx *pctx; double result = 0; struct evsel *metric_evsel = NULL;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
if (!PyArg_ParseTuple(args, "sii", &metric, &cpu, &thread)) return NULL;
for (node = rb_first_cached(&evlist__metric_events(pevlist->evlist)->entries); mexp == NULL && node; node = rb_next(node)) { struct metric_event *me = container_of(node, struct metric_event, nd); struct list_head *pos;
list_for_each(pos, &me->head) { struct metric_expr *e = container_of(pos, struct metric_expr, nd); struct evsel *pos2;
if (strcmp(e->metric_name, metric)) continue;
if (e->metric_events[0] == NULL) continue;
evlist__for_each_entry(pevlist->evlist, pos2) { if (pos2->metric_leader != e->metric_events[0]) continue; cpu_idx = perf_cpu_map__idx(pos2->core.cpus, (struct perf_cpu){.cpu = cpu}); if (cpu_idx < 0) continue;
thread_idx = perf_thread_map__idx(pos2->core.threads, thread); if (thread_idx < 0) continue; metric_evsel = pos2; mexp = e; goto done; } } }done: if (!mexp) { PyErr_Format(PyExc_TypeError, "Unknown metric '%s' for CPU '%d' and thread '%d'", metric, cpu, thread); return NULL; }
pctx = expr__ctx_new(); if (!pctx) return PyErr_NoMemory();
ret = prepare_metric(mexp, metric_evsel, pctx, cpu_idx, thread_idx); if (ret) { expr__ctx_free(pctx); errno = -ret; PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (expr__parse(&result, pctx, mexp->metric_expr)) result = 0.0;
expr__ctx_free(pctx); return PyFloat_FromDouble(result);}
static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs){ struct evlist *evlist; static char *kwlist[] = { "pages", "overwrite", NULL }; int pages = 128, overwrite = false;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, &pages, &overwrite)) return NULL;
if (evlist__do_mmap(evlist, pages) < 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
Py_INCREF(Py_None); return Py_None;}
static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs){ struct evlist *evlist; static char *kwlist[] = { "timeout", NULL }; int timeout = -1, n;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) return NULL;
n = evlist__poll(evlist, timeout); if (n < 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
return Py_BuildValue("i", n);}
static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, PyObject *args __maybe_unused, PyObject *kwargs __maybe_unused){ struct evlist *evlist; PyObject *list; int i;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; list = PyList_New(0); if (!list) return NULL;
for (i = 0; i < evlist__core(evlist)->pollfd.nr; ++i) { PyObject *file; file = PyFile_FromFd(evlist__core(evlist)->pollfd.entries[i].fd, "perf", "r", -1, NULL, NULL, NULL, 0); if (file == NULL) goto free_list;
if (PyList_Append(list, file) != 0) { Py_DECREF(file); goto free_list; }
Py_DECREF(file); }
return list;free_list: Py_XDECREF(list); return PyErr_NoMemory();}
static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs __maybe_unused){ struct evlist *evlist; PyObject *pevsel; struct evsel *evsel;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; if (!PyArg_ParseTuple(args, "O!", &pyrf_evsel__type, &pevsel)) return NULL;
CHECK_INITIALIZED(((struct pyrf_evsel *)pevsel)->evsel, "evsel");
evsel = ((struct pyrf_evsel *)pevsel)->evsel; CHECK_INITIALIZED(evsel, "evsel");
evsel->core.idx = evlist__nr_entries(evlist); evlist__add(evlist, evsel__get(evsel));
return Py_BuildValue("i", evlist__nr_entries(evlist));}
static struct mmap *get_md(struct evlist *evlist, int cpu){ int i;
for (i = 0; i < evlist__core(evlist)->nr_mmaps; i++) { struct mmap *md = &evlist__mmap(evlist)[i];
if (md->core.cpu.cpu == cpu) return md; }
return NULL;}
static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs){ struct evlist *evlist; union perf_event *event; struct evsel *evsel; int sample_id_all = 1, cpu; static char *kwlist[] = { "cpu", "sample_id_all", NULL }; struct mmap *md; PyObject *pyevent; int err;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, &cpu, &sample_id_all)) return NULL;
md = get_md(evlist, cpu); if (!md) return PyErr_Format(PyExc_TypeError, "Unknown CPU '%d'", cpu);
err = perf_mmap__read_init(&md->core); if (err < 0) { if (err == -EAGAIN) Py_RETURN_NONE; return PyErr_Format(PyExc_OSError, "perf: error mmap read init, err=%d", err); }
event = perf_mmap__read_event(&md->core); if (event == NULL) Py_RETURN_NONE;
evsel = evlist__event2evsel(evlist, event); if (!evsel) { /* Unknown evsel. */ perf_mmap__consume(&md->core); Py_RETURN_NONE; } pyevent = pyrf_event__new(event, evsel, evlist__session(evlist), /*machine=*/NULL); perf_mmap__consume(&md->core); if (pyevent == NULL) return PyErr_Occurred() ? NULL : PyErr_NoMemory();
return pyevent;}
static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs){ struct evlist *evlist;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; if (evlist__open(evlist) < 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
Py_INCREF(Py_None); return Py_None;}
static PyObject *pyrf_evlist__close(struct pyrf_evlist *pevlist){ struct evlist *evlist;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; evlist__close(evlist);
Py_INCREF(Py_None); return Py_None;}
static PyObject *pyrf_evlist__config(struct pyrf_evlist *pevlist){ struct record_opts opts = { .sample_time = true, .mmap_pages = UINT_MAX, .user_freq = UINT_MAX, .user_interval = ULLONG_MAX, .freq = 4000, .target = { .uses_mmap = true, .default_per_cpu = true, }, .nr_threads_synthesize = 1, .ctl_fd = -1, .ctl_fd_ack = -1, .no_buffering = true, .no_inherit = true, }; struct evlist *evlist;
CHECK_INITIALIZED(pevlist->evlist, "evlist");
evlist = pevlist->evlist; evlist__config(evlist, &opts, &callchain_param); Py_INCREF(Py_None); return Py_None;}
static PyObject *pyrf_evlist__disable(struct pyrf_evlist *pevlist){ CHECK_INITIALIZED(pevlist->evlist, "evlist"); evlist__disable(pevlist->evlist); Py_INCREF(Py_None); return Py_None;}
static PyObject *pyrf_evlist__enable(struct pyrf_evlist *pevlist){ CHECK_INITIALIZED(pevlist->evlist, "evlist"); evlist__enable(pevlist->evlist); Py_INCREF(Py_None); return Py_None;}
static PyMethodDef pyrf_evlist__methods[] = { { .ml_name = "all_cpus", .ml_meth = (PyCFunction)pyrf_evlist__all_cpus, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("CPU map union of all evsel CPU maps.") }, { .ml_name = "metrics", .ml_meth = (PyCFunction)pyrf_evlist__metrics, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("List of metric names within the evlist.") }, { .ml_name = "compute_metric", .ml_meth = (PyCFunction)pyrf_evlist__compute_metric, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("compute metric for given name, cpu and thread") }, { .ml_name = "mmap", .ml_meth = (PyCFunction)pyrf_evlist__mmap, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("mmap the file descriptor table.") }, { .ml_name = "open", .ml_meth = (PyCFunction)pyrf_evlist__open, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("open the file descriptors.") }, { .ml_name = "close", .ml_meth = (PyCFunction)pyrf_evlist__close, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("close the file descriptors.") }, { .ml_name = "poll", .ml_meth = (PyCFunction)pyrf_evlist__poll, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("poll the file descriptor table.") }, { .ml_name = "get_pollfd", .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("get the poll file descriptor table.") }, { .ml_name = "add", .ml_meth = (PyCFunction)pyrf_evlist__add, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("adds an event selector to the list.") }, { .ml_name = "read_on_cpu", .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("reads an event.") }, { .ml_name = "config", .ml_meth = (PyCFunction)pyrf_evlist__config, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Apply default record options to the evlist.") }, { .ml_name = "disable", .ml_meth = (PyCFunction)pyrf_evlist__disable, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Disable the evsels in the evlist.") }, { .ml_name = "enable", .ml_meth = (PyCFunction)pyrf_evlist__enable, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Enable the evsels in the evlist.") }, { .ml_name = NULL, }};
static Py_ssize_t pyrf_evlist__length(PyObject *obj){ struct pyrf_evlist *pevlist = (void *)obj;
if (!pevlist->evlist) return 0;
return evlist__nr_entries(pevlist->evlist);}
static PyObject *pyrf_evsel__from_evsel(struct evsel *evsel){ struct pyrf_evsel *pevsel = PyObject_New(struct pyrf_evsel, &pyrf_evsel__type);
if (!pevsel) return NULL;
pevsel->evsel = evsel__get(evsel); return (PyObject *)pevsel;}
static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i){ struct pyrf_evlist *pevlist = (void *)obj; struct evsel *pos;
if (!pevlist->evlist || i >= evlist__nr_entries(pevlist->evlist)) { PyErr_SetString(PyExc_IndexError, "Index out of range"); return NULL; }
evlist__for_each_entry(pevlist->evlist, pos) { if (i-- == 0) break; } return pyrf_evsel__from_evsel(pos);}
static PyObject *pyrf_evlist__str(PyObject *self){ struct pyrf_evlist *pevlist = (void *)self; struct evsel *pos; struct strbuf sb = STRBUF_INIT; bool first = true; PyObject *result;
if (!pevlist->evlist) return PyUnicode_FromString("evlist(uninitialized)");
strbuf_addstr(&sb, "evlist(["); evlist__for_each_entry(pevlist->evlist, pos) { if (!first) strbuf_addch(&sb, ','); strbuf_addstr(&sb, evsel__name(pos)); first = false; } strbuf_addstr(&sb, "])"); result = PyUnicode_FromString(sb.buf); strbuf_release(&sb); return result;}
static PySequenceMethods pyrf_evlist__sequence_methods = { .sq_length = pyrf_evlist__length, .sq_item = pyrf_evlist__item,};
static const char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
static PyObject *pyrf_evlist__getattro(struct pyrf_evlist *pevlist, PyObject *attr_name){ if (!pevlist->evlist) { PyErr_SetString(PyExc_ValueError, "evlist not initialized"); return NULL; } return PyObject_GenericGetAttr((PyObject *) pevlist, attr_name);}
static int pyrf_evlist__setattro(struct pyrf_evlist *pevlist, PyObject *attr_name, PyObject *value){ if (!pevlist->evlist) { PyErr_SetString(PyExc_ValueError, "evlist not initialized"); return -1; } return PyObject_GenericSetAttr((PyObject *) pevlist, attr_name, value);}
static PyTypeObject pyrf_evlist__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.evlist", .tp_basicsize = sizeof(struct pyrf_evlist), .tp_dealloc = (destructor)pyrf_evlist__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_as_sequence = &pyrf_evlist__sequence_methods, .tp_doc = pyrf_evlist__doc, .tp_methods = pyrf_evlist__methods, .tp_init = (initproc)pyrf_evlist__init, .tp_repr = pyrf_evlist__str, .tp_str = pyrf_evlist__str, .tp_getattro = (getattrofunc) pyrf_evlist__getattro, .tp_setattro = (setattrofunc) pyrf_evlist__setattro,};
static PyObject *pyrf_evlist__new(PyTypeObject *type, PyObject *args, PyObject *kwargs){ struct pyrf_evlist *pevlist;
pevlist = (struct pyrf_evlist *)PyType_GenericNew(type, args, kwargs); if (pevlist) pevlist->evlist = NULL; return (PyObject *)pevlist;}
static int pyrf_evlist__setup_types(void){ pyrf_evlist__type.tp_new = pyrf_evlist__new; return PyType_Ready(&pyrf_evlist__type);}
#define PERF_CONST(name) { #name, PERF_##name }
struct perf_constant { const char *name; int value;};
static const struct perf_constant perf__constants[] = { PERF_CONST(TYPE_HARDWARE), PERF_CONST(TYPE_SOFTWARE), PERF_CONST(TYPE_TRACEPOINT), PERF_CONST(TYPE_HW_CACHE), PERF_CONST(TYPE_RAW), PERF_CONST(TYPE_BREAKPOINT),
PERF_CONST(COUNT_HW_CPU_CYCLES), PERF_CONST(COUNT_HW_REF_CPU_CYCLES), PERF_CONST(COUNT_HW_INSTRUCTIONS), PERF_CONST(COUNT_HW_CACHE_REFERENCES), PERF_CONST(COUNT_HW_CACHE_MISSES), PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS), PERF_CONST(COUNT_HW_BRANCH_MISSES), PERF_CONST(COUNT_HW_BUS_CYCLES), PERF_CONST(COUNT_HW_CACHE_L1D), PERF_CONST(COUNT_HW_CACHE_L1I), PERF_CONST(COUNT_HW_CACHE_LL), PERF_CONST(COUNT_HW_CACHE_DTLB), PERF_CONST(COUNT_HW_CACHE_ITLB), PERF_CONST(COUNT_HW_CACHE_BPU), PERF_CONST(COUNT_HW_CACHE_OP_READ), PERF_CONST(COUNT_HW_CACHE_OP_WRITE), PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH), PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS), PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND), PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
PERF_CONST(COUNT_SW_CPU_CLOCK), PERF_CONST(COUNT_SW_TASK_CLOCK), PERF_CONST(COUNT_SW_PAGE_FAULTS), PERF_CONST(COUNT_SW_CONTEXT_SWITCHES), PERF_CONST(COUNT_SW_CPU_MIGRATIONS), PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN), PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ), PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS), PERF_CONST(COUNT_SW_EMULATION_FAULTS), PERF_CONST(COUNT_SW_DUMMY),
PERF_CONST(SAMPLE_IP), PERF_CONST(SAMPLE_TID), PERF_CONST(SAMPLE_TIME), PERF_CONST(SAMPLE_ADDR), PERF_CONST(SAMPLE_READ), PERF_CONST(SAMPLE_CALLCHAIN), PERF_CONST(SAMPLE_ID), PERF_CONST(SAMPLE_CPU), PERF_CONST(SAMPLE_PERIOD), PERF_CONST(SAMPLE_STREAM_ID), PERF_CONST(SAMPLE_RAW),
PERF_CONST(FORMAT_TOTAL_TIME_ENABLED), PERF_CONST(FORMAT_TOTAL_TIME_RUNNING), PERF_CONST(FORMAT_ID), PERF_CONST(FORMAT_GROUP),
PERF_CONST(RECORD_MMAP), PERF_CONST(RECORD_LOST), PERF_CONST(RECORD_COMM), PERF_CONST(RECORD_EXIT), PERF_CONST(RECORD_THROTTLE), PERF_CONST(RECORD_UNTHROTTLE), PERF_CONST(RECORD_FORK), PERF_CONST(RECORD_READ), PERF_CONST(RECORD_SAMPLE), PERF_CONST(RECORD_MMAP2), PERF_CONST(RECORD_AUX), PERF_CONST(RECORD_ITRACE_START), PERF_CONST(RECORD_LOST_SAMPLES), PERF_CONST(RECORD_SWITCH), PERF_CONST(RECORD_SWITCH_CPU_WIDE), PERF_CONST(RECORD_STAT), PERF_CONST(RECORD_STAT_ROUND),
PERF_CONST(RECORD_MISC_SWITCH_OUT), { .name = NULL, },};
static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, PyObject *args, PyObject *kwargs){ static char *kwlist[] = { "sys", "name", NULL }; char *sys = NULL; char *name = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist, &sys, &name)) return NULL;
return PyLong_FromLong(tp_pmu__id(sys, name));}
static PyObject *pyrf_evlist__from_evlist(struct evlist *evlist){ struct pyrf_evlist *pevlist = PyObject_New(struct pyrf_evlist, &pyrf_evlist__type);
if (!pevlist) return NULL;
pevlist->evlist = evlist__get(evlist); return (PyObject *)pevlist;}
static PyObject *pyrf__parse_events(PyObject *self, PyObject *args){ const char *input; struct evlist *evlist = evlist__new(); struct parse_events_error err; PyObject *result; PyObject *pcpus = NULL, *pthreads = NULL; struct perf_cpu_map *cpus; struct perf_thread_map *threads;
if (!evlist) return PyErr_NoMemory();
if (!PyArg_ParseTuple(args, "s|OO", &input, &pcpus, &pthreads)) { evlist__put(evlist); return NULL; }
if (pthreads && pthreads != Py_None && !PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) { PyErr_SetString(PyExc_TypeError, "threads must be a perf.thread_map or None"); evlist__put(evlist); return NULL; }
if (pcpus && pcpus != Py_None && !PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) { PyErr_SetString(PyExc_TypeError, "cpus must be a perf.cpu_map or None"); evlist__put(evlist); return NULL; }
threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; cpus = (pcpus && pcpus != Py_None) ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;
parse_events_error__init(&err); perf_evlist__set_maps(evlist__core(evlist), cpus, threads); if (parse_events(evlist, input, &err)) { parse_events_error__print(&err, input); PyErr_SetFromErrno(PyExc_OSError); evlist__put(evlist); return NULL; } result = pyrf_evlist__from_evlist(evlist); evlist__put(evlist); return result;}
static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args){ const char *input, *pmu = NULL; struct evlist *evlist = evlist__new(); PyObject *result; PyObject *pcpus = NULL, *pthreads = NULL; struct perf_cpu_map *cpus; struct perf_thread_map *threads; int ret;
if (!evlist) return PyErr_NoMemory();
if (!PyArg_ParseTuple(args, "s|sOO", &input, &pmu, &pcpus, &pthreads)) { evlist__put(evlist); return NULL; }
if (pthreads && pthreads != Py_None && !PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) { PyErr_SetString(PyExc_TypeError, "threads must be a perf.thread_map or None"); evlist__put(evlist); return NULL; }
if (pcpus && pcpus != Py_None && !PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) { PyErr_SetString(PyExc_TypeError, "cpus must be a perf.cpu_map or None"); evlist__put(evlist); return NULL; }
threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; cpus = (pcpus && pcpus != Py_None) ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;
perf_evlist__set_maps(evlist__core(evlist), cpus, threads); ret = metricgroup__parse_groups(evlist, pmu ?: "all", /*cputype_filter=*/false, input, /*metric_no_group=*/ false, /*metric_no_merge=*/ false, /*metric_no_threshold=*/ true, /*user_requested_cpu_list=*/ NULL, /*system_wide=*/true, /*hardware_aware_grouping=*/ false); if (ret) { evlist__put(evlist); errno = -ret; PyErr_SetFromErrno(PyExc_OSError); return NULL; } result = pyrf_evlist__from_evlist(evlist); evlist__put(evlist); return result;}
static PyObject *pyrf__metrics_groups(const struct pmu_metric *pm){ PyObject *groups = PyList_New(/*len=*/0); const char *mg = pm->metric_group;
if (!groups) return NULL;
while (mg) { PyObject *val = NULL; const char *sep = strchr(mg, ';'); size_t len = sep ? (size_t)(sep - mg) : strlen(mg);
if (len > 0) { val = PyUnicode_FromStringAndSize(mg, len); if (val) PyList_Append(groups, val);
Py_XDECREF(val); } mg = sep ? sep + 1 : NULL; } return groups;}
static int pyrf__metrics_cb(const struct pmu_metric *pm, const struct pmu_metrics_table *table __maybe_unused, void *vdata){ PyObject *py_list = vdata; PyObject *dict = PyDict_New(); PyObject *key = dict ? PyUnicode_FromString("MetricGroup") : NULL; PyObject *value = key ? pyrf__metrics_groups(pm) : NULL;
if (!value || PyDict_SetItem(dict, key, value) != 0) { Py_XDECREF(key); Py_XDECREF(value); Py_XDECREF(dict); return -ENOMEM; } Py_DECREF(key); Py_DECREF(value);
if (!add_to_dict(dict, "MetricName", pm->metric_name) || !add_to_dict(dict, "PMU", pm->pmu) || !add_to_dict(dict, "MetricExpr", pm->metric_expr) || !add_to_dict(dict, "MetricThreshold", pm->metric_threshold) || !add_to_dict(dict, "ScaleUnit", pm->unit) || !add_to_dict(dict, "Compat", pm->compat) || !add_to_dict(dict, "BriefDescription", pm->desc) || !add_to_dict(dict, "PublicDescription", pm->long_desc) || PyList_Append(py_list, dict) != 0) { Py_DECREF(dict); return -ENOMEM; } Py_DECREF(dict); return 0;}
static PyObject *pyrf__metrics(PyObject *self, PyObject *args){ const struct pmu_metrics_table *table = pmu_metrics_table__find(); PyObject *list = PyList_New(/*len=*/0); int ret;
if (!list) return NULL;
ret = pmu_metrics_table__for_each_metric(table, pyrf__metrics_cb, list); if (!ret) ret = pmu_for_each_sys_metric(pyrf__metrics_cb, list);
if (ret) { Py_DECREF(list); errno = -ret; PyErr_SetFromErrno(PyExc_OSError); return NULL; } return list;}
struct pyrf_data { PyObject_HEAD
struct perf_data data;};
static int pyrf_data__init(struct pyrf_data *pdata, PyObject *args, PyObject *kwargs){ static char *kwlist[] = { "path", "fd", NULL }; char *path = NULL; int fd = -1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", kwlist, &path, &fd)) return -1;
if (pdata->data.open) perf_data__close(&pdata->data); free((char *)pdata->data.path); memset(&pdata->data, 0, sizeof(pdata->data));
if (fd != -1) { struct stat st;
if (fstat(fd, &st) < 0 || !S_ISFIFO(st.st_mode)) { PyErr_SetString(PyExc_ValueError, "fd argument is only supported for pipes"); return -1; } if (!path) path = "-"; else if (strcmp(path, "-") != 0) { PyErr_SetString(PyExc_ValueError, "path must be '-' when fd is provided"); return -1; } fd = dup(fd); if (fd < 0) { PyErr_SetFromErrno(PyExc_OSError); return -1; } } else if (path && strcmp(path, "-") == 0) { fd = dup(0); if (fd < 0) { PyErr_SetFromErrno(PyExc_OSError); return -1; } }
if (!path) path = "perf.data";
pdata->data.path = strdup(path); if (!pdata->data.path) { if (fd != -1) close(fd); PyErr_NoMemory(); return -1; }
pdata->data.mode = PERF_DATA_MODE_READ; pdata->data.file.fd = fd; if (perf_data__open(&pdata->data) < 0) { PyErr_Format(PyExc_IOError, "Failed to open perf data: %s", pdata->data.path ? pdata->data.path : "perf.data"); return -1; } return 0;}
static void pyrf_data__delete(struct pyrf_data *pdata){ perf_data__close(&pdata->data); free((char *)pdata->data.path); Py_TYPE(pdata)->tp_free((PyObject *)pdata);}
static PyObject *pyrf_data__str(PyObject *self){ const struct pyrf_data *pdata = (const struct pyrf_data *)self;
if (!pdata->data.path) return PyUnicode_FromString("[uninitialized]"); return PyUnicode_FromString(pdata->data.path);}
static PyObject *pyrf_data__new(PyTypeObject *type, PyObject *args, PyObject *kwargs){ struct pyrf_data *pdata;
pdata = (struct pyrf_data *)PyType_GenericNew(type, args, kwargs); if (pdata) memset(&pdata->data, 0, sizeof(pdata->data)); return (PyObject *)pdata;}
static const char pyrf_data__doc[] = PyDoc_STR("perf data file object.");
static PyTypeObject pyrf_data__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.data", .tp_basicsize = sizeof(struct pyrf_data), .tp_dealloc = (destructor)pyrf_data__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_data__doc, .tp_init = (initproc)pyrf_data__init, .tp_repr = pyrf_data__str, .tp_str = pyrf_data__str,};
static int pyrf_data__setup_types(void){ pyrf_data__type.tp_new = pyrf_data__new; return PyType_Ready(&pyrf_data__type);}
struct pyrf_thread { PyObject_HEAD
struct thread *thread;};
static void pyrf_thread__delete(struct pyrf_thread *pthread){ thread__put(pthread->thread); Py_TYPE(pthread)->tp_free((PyObject *)pthread);}
static PyObject *pyrf_thread__comm(PyObject *obj){ struct pyrf_thread *pthread = (void *)obj; const char *str;
CHECK_INITIALIZED(pthread->thread, "perf.thread");
str = thread__comm_str(pthread->thread);
if (!str) Py_RETURN_NONE;
return PyUnicode_FromString(str);}
static PyMethodDef pyrf_thread__methods[] = { { .ml_name = "comm", .ml_meth = (PyCFunction)pyrf_thread__comm, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Comm(and) associated with this thread.") }, { .ml_name = NULL, }};
static PyObject *pyrf_thread__get_pid(struct pyrf_thread *pthread, void *closure __maybe_unused){ CHECK_INITIALIZED(pthread->thread, "thread"); return PyLong_FromLong(thread__pid(pthread->thread));}
static PyObject *pyrf_thread__get_tid(struct pyrf_thread *pthread, void *closure __maybe_unused){ CHECK_INITIALIZED(pthread->thread, "thread"); return PyLong_FromLong(thread__tid(pthread->thread));}
static PyObject *pyrf_thread__get_ppid(struct pyrf_thread *pthread, void *closure __maybe_unused){ CHECK_INITIALIZED(pthread->thread, "thread"); return PyLong_FromLong(thread__ppid(pthread->thread));}
static PyObject *pyrf_thread__get_cpu(struct pyrf_thread *pthread, void *closure __maybe_unused){ CHECK_INITIALIZED(pthread->thread, "thread"); return PyLong_FromLong(thread__cpu(pthread->thread));}
static PyGetSetDef pyrf_thread__getset[] = { { .name = "pid", .get = (getter)pyrf_thread__get_pid, .doc = "process ID" }, { .name = "tid", .get = (getter)pyrf_thread__get_tid, .doc = "thread ID" }, { .name = "ppid", .get = (getter)pyrf_thread__get_ppid, .doc = "parent process ID" }, { .name = "cpu", .get = (getter)pyrf_thread__get_cpu, .doc = "cpu number" }, { .name = NULL }};
static const char pyrf_thread__doc[] = PyDoc_STR("perf thread object.");
static PyTypeObject pyrf_thread__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.thread", .tp_basicsize = sizeof(struct pyrf_thread), .tp_dealloc = (destructor)pyrf_thread__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_methods = pyrf_thread__methods, .tp_getset = pyrf_thread__getset, .tp_doc = pyrf_thread__doc,};
static int pyrf_thread__setup_types(void){ return PyType_Ready(&pyrf_thread__type);}
static PyObject *pyrf_thread__from_thread(struct thread *thread){ struct pyrf_thread *pthread = PyObject_New(struct pyrf_thread, &pyrf_thread__type);
if (!pthread) return NULL;
pthread->thread = thread__get(thread); return (PyObject *)pthread;}
struct pyrf_session { PyObject_HEAD
struct perf_session *session; struct perf_tool tool; struct pyrf_data *pdata; PyObject *sample; PyObject *stat;};
static int pyrf_session_tool__sample(const struct perf_tool *tool, union perf_event *event, struct perf_sample *sample, struct machine *machine){ struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); PyObject *pyevent = pyrf_event__new(event, sample->evsel, psession->session, machine); PyObject *ret;
if (pyevent == NULL) return -ENOMEM;
ret = PyObject_CallFunction(psession->sample, "O", pyevent); if (!ret) { Py_DECREF(pyevent); return -1; } Py_DECREF(ret); Py_DECREF(pyevent); return 0;}
static int pyrf_session_tool__stat(const struct perf_tool *tool, struct perf_session *session, union perf_event *event){ struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); struct evsel *evsel = evlist__id2evsel(session->evlist, event->stat.id); PyObject *pyevent = pyrf_event__new(event, /*evsel=*/NULL, psession->session, /*machine=*/NULL); const char *name = evsel ? evsel__name(evsel) : "unknown"; PyObject *ret;
if (pyevent == NULL) return -ENOMEM;
ret = PyObject_CallFunction(psession->stat, "Oz", pyevent, name); if (!ret) { Py_DECREF(pyevent); return -1; } Py_DECREF(ret); Py_DECREF(pyevent); return 0;}
static int pyrf_session_tool__stat_round(const struct perf_tool *tool, struct perf_session *session __maybe_unused, union perf_event *event){ struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); PyObject *pyevent = pyrf_event__new(event, /*evsel=*/NULL, psession->session, /*machine=*/NULL); PyObject *ret;
if (pyevent == NULL) return -ENOMEM;
ret = PyObject_CallFunction(psession->stat, "Oz", pyevent, NULL); if (!ret) { Py_DECREF(pyevent); return -1; } Py_DECREF(ret); Py_DECREF(pyevent); return 0;}
static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObject *args){ struct machine *machine; struct thread *thread = NULL; PyObject *result; int pid;
CHECK_INITIALIZED(psession->session, "session");
if (!PyArg_ParseTuple(args, "i", &pid)) return NULL;
machine = &psession->session->machines.host; thread = machine__find_thread(machine, pid, pid);
if (!thread) { machine = perf_session__find_machine(psession->session, pid); if (machine) thread = machine__find_thread(machine, pid, pid); }
if (!thread) { PyErr_Format(PyExc_TypeError, "Failed to find thread %d", pid); return NULL; } result = pyrf_thread__from_thread(thread); thread__put(thread); return result;}
static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject *kwargs){ struct pyrf_data *pdata; PyObject *sample = NULL, *stat = NULL; static char *kwlist[] = { "data", "sample", "stat", NULL }; struct pyrf_session *psession; struct perf_session *session;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OO", kwlist, &pyrf_data__type, &pdata, &sample, &stat)) return NULL;
psession = PyObject_New(struct pyrf_session, type); if (!psession) return NULL;
psession->session = NULL; psession->sample = NULL; psession->stat = NULL; psession->pdata = NULL;
Py_INCREF(pdata); psession->pdata = pdata;
perf_tool__init(&psession->tool, /*ordered_events=*/true); psession->tool.ordering_requires_timestamps = true;
#define ADD_TOOL(name) \ do { \ if (name) { \ if (!PyCallable_Check(name)) { \ PyErr_SetString(PyExc_TypeError, #name " must be callable"); \ goto err_out; \ } \ psession->tool.name = pyrf_session_tool__##name; \ Py_INCREF(name); \ psession->name = name; \ } \ } while (0)
ADD_TOOL(sample); ADD_TOOL(stat); #undef ADD_TOOL
if (stat) psession->tool.stat_round = pyrf_session_tool__stat_round;
psession->tool.comm = perf_event__process_comm; psession->tool.mmap = perf_event__process_mmap; psession->tool.mmap2 = perf_event__process_mmap2; psession->tool.namespaces = perf_event__process_namespaces; psession->tool.cgroup = perf_event__process_cgroup; psession->tool.exit = perf_event__process_exit; psession->tool.fork = perf_event__process_fork; psession->tool.ksymbol = perf_event__process_ksymbol; psession->tool.text_poke = perf_event__process_text_poke; psession->tool.build_id = perf_event__process_build_id; psession->tool.attr = perf_event__process_attr; psession->tool.feature = perf_event__process_feature;
session = perf_session__new(&pdata->data, &psession->tool); if (IS_ERR(session)) { PyErr_Format(PyExc_IOError, "failed to create session: %ld", PTR_ERR(session)); goto err_out; } psession->session = session;
symbol_conf.use_callchain = true; symbol_conf.show_kernel_path = true; symbol_conf.inline_name = false; if (symbol__init(perf_session__env(session)) < 0) { PyErr_SetString(PyExc_OSError, "perf: symbol__init failed"); goto err_out; }
return (PyObject *)psession;err_out: Py_DECREF(psession); return NULL;}
static void pyrf_session__delete(struct pyrf_session *psession){ perf_session__delete(psession->session); Py_XDECREF(psession->pdata); Py_XDECREF(psession->sample); Py_XDECREF(psession->stat); Py_TYPE(psession)->tp_free((PyObject *)psession);}
static PyObject *pyrf_session__find_thread_events(struct pyrf_session *psession){ int err;
CHECK_INITIALIZED(psession->session, "session");
err = perf_session__process_events(psession->session);
if (PyErr_Occurred()) return NULL;
if (err < 0) { PyErr_Format(PyExc_OSError, "Process events failed: %d", err); return NULL; }
Py_RETURN_NONE;}
static PyMethodDef pyrf_session__methods[] = { { .ml_name = "process_events", .ml_meth = (PyCFunction)pyrf_session__find_thread_events, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Iterate and process events.") }, { .ml_name = "find_thread", .ml_meth = (PyCFunction)pyrf_session__find_thread, .ml_flags = METH_VARARGS, .ml_doc = PyDoc_STR("Returns the thread associated with a pid.") }, { .ml_name = NULL, }};
static const char pyrf_session__doc[] = PyDoc_STR("perf session object.");
static PyObject *pyrf_session__getattro(struct pyrf_session *psession, PyObject *attr_name){ if (!psession->session) { PyErr_SetString(PyExc_ValueError, "session not initialized"); return NULL; } return PyObject_GenericGetAttr((PyObject *) psession, attr_name);}
static int pyrf_session__setattro(struct pyrf_session *psession, PyObject *attr_name, PyObject *value){ if (!psession->session) { PyErr_SetString(PyExc_ValueError, "session not initialized"); return -1; } return PyObject_GenericSetAttr((PyObject *) psession, attr_name, value);}
static PyTypeObject pyrf_session__type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "perf.session", .tp_basicsize = sizeof(struct pyrf_session), .tp_dealloc = (destructor)pyrf_session__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_methods = pyrf_session__methods, .tp_doc = pyrf_session__doc, .tp_new = pyrf_session__new, .tp_getattro = (getattrofunc) pyrf_session__getattro, .tp_setattro = (setattrofunc) pyrf_session__setattro,};
static int pyrf_session__setup_types(void){ return PyType_Ready(&pyrf_session__type);}
static PyObject *pyrf__syscall_name(PyObject *self, PyObject *args, PyObject *kwargs){ const char *name; int id; int elf_machine = EM_HOST; static char *kwlist[] = { "id", "elf_machine", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|$i", kwlist, &id, &elf_machine)) return NULL;
name = syscalltbl__name(elf_machine, id); if (!name) Py_RETURN_NONE; return PyUnicode_FromString(name);}
static PyObject *pyrf__syscall_id(PyObject *self, PyObject *args, PyObject *kwargs){ const char *name; int id; int elf_machine = EM_HOST; static char *kwlist[] = { "name", "elf_machine", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|$i", kwlist, &name, &elf_machine)) return NULL;
id = syscalltbl__id(elf_machine, name); if (id < 0) { PyErr_Format(PyExc_ValueError, "Failed to find syscall %s", name); return NULL; } return PyLong_FromLong(id);}
static PyObject *pyrf__config_get(PyObject *self, PyObject *args){ const char *config_name, *val;
if (!PyArg_ParseTuple(args, "s", &config_name)) return NULL;
val = perf_config_get(config_name); if (!val) Py_RETURN_NONE; return PyUnicode_FromString(val);}
static PyMethodDef perf__methods[] = { { .ml_name = "config_get", .ml_meth = (PyCFunction) pyrf__config_get, .ml_flags = METH_VARARGS, .ml_doc = PyDoc_STR("Get a perf config value.") }, { .ml_name = "metrics", .ml_meth = (PyCFunction) pyrf__metrics, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR( "Returns a list of metrics represented as string values in dictionaries.") }, { .ml_name = "tracepoint", .ml_meth = (PyCFunction) pyrf__tracepoint, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("Get tracepoint config.") }, { .ml_name = "parse_events", .ml_meth = (PyCFunction) pyrf__parse_events, .ml_flags = METH_VARARGS, .ml_doc = PyDoc_STR("Parse a string of events and return an evlist.") }, { .ml_name = "parse_metrics", .ml_meth = (PyCFunction) pyrf__parse_metrics, .ml_flags = METH_VARARGS, .ml_doc = PyDoc_STR( "Parse a string of metrics or metric groups and return an evlist.") }, { .ml_name = "pmus", .ml_meth = (PyCFunction) pyrf__pmus, .ml_flags = METH_NOARGS, .ml_doc = PyDoc_STR("Returns a sequence of pmus.") }, { .ml_name = "syscall_name", .ml_meth = (PyCFunction) pyrf__syscall_name, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("Turns a syscall number to a string.") }, { .ml_name = "syscall_id", .ml_meth = (PyCFunction) pyrf__syscall_id, .ml_flags = METH_VARARGS | METH_KEYWORDS, .ml_doc = PyDoc_STR("Turns a syscall name to a number.") }, { .ml_name = NULL, }};
PyMODINIT_FUNC PyInit_perf(void){ PyObject *obj; int i; PyObject *dict; static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "perf", /* m_name */ "", /* m_doc */ -1, /* m_size */ perf__methods, /* m_methods */ NULL, /* m_reload */ NULL, /* m_traverse */ NULL, /* m_clear */ NULL, /* m_free */ }; PyObject *module = PyModule_Create(&moduledef);
if (module == NULL) return NULL;
if (pyrf_event__setup_types() < 0 || pyrf_evlist__setup_types() < 0 || pyrf_evsel__setup_types() < 0 || pyrf_thread_map__setup_types() < 0 || pyrf_cpu_map__setup_types() < 0 || pyrf_pmu_iterator__setup_types() < 0 || pyrf_pmu__setup_types() < 0 || pyrf_counts_values__setup_types() < 0 || pyrf_data__setup_types() < 0 || pyrf_session__setup_types() < 0 || pyrf_thread__setup_types() < 0) { Py_DECREF(module); return NULL; }
/* The page_size is placed in util object. */ page_size = sysconf(_SC_PAGE_SIZE);
Py_INCREF(&pyrf_evlist__type); PyModule_AddObject(module, "evlist", (PyObject *)&pyrf_evlist__type);
Py_INCREF(&pyrf_evsel__type); PyModule_AddObject(module, "evsel", (PyObject *)&pyrf_evsel__type);
Py_INCREF(&pyrf_thread__type); PyModule_AddObject(module, "thread", (PyObject *)&pyrf_thread__type);
Py_INCREF(&pyrf_callchain__type); PyModule_AddObject(module, "callchain", (PyObject *)&pyrf_callchain__type);
Py_INCREF(&pyrf_callchain_node__type); PyModule_AddObject(module, "callchain_node", (PyObject *)&pyrf_callchain_node__type);
Py_INCREF(&pyrf_mmap_event__type); PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
Py_INCREF(&pyrf_mmap2_event__type); PyModule_AddObject(module, "mmap2_event", (PyObject *)&pyrf_mmap2_event__type);
Py_INCREF(&pyrf_lost_event__type); PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
Py_INCREF(&pyrf_comm_event__type); PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
Py_INCREF(&pyrf_task_event__type); PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
Py_INCREF(&pyrf_throttle_event__type); PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
Py_INCREF(&pyrf_task_event__type); PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
Py_INCREF(&pyrf_read_event__type); PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
Py_INCREF(&pyrf_sample_event__type); PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
Py_INCREF(&pyrf_context_switch_event__type); PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
Py_INCREF(&pyrf_stat_event__type); PyModule_AddObject(module, "stat_event", (PyObject *)&pyrf_stat_event__type);
Py_INCREF(&pyrf_stat_round_event__type); PyModule_AddObject(module, "stat_round_event", (PyObject *)&pyrf_stat_round_event__type);
Py_INCREF(&pyrf_thread_map__type); PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
Py_INCREF(&pyrf_cpu_map__type); PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
Py_INCREF(&pyrf_counts_values__type); PyModule_AddObject(module, "counts_values", (PyObject *)&pyrf_counts_values__type);
Py_INCREF(&pyrf_data__type); PyModule_AddObject(module, "data", (PyObject *)&pyrf_data__type);
Py_INCREF(&pyrf_session__type); PyModule_AddObject(module, "session", (PyObject *)&pyrf_session__type);
Py_INCREF(&pyrf_branch_entry__type); if (PyModule_AddObject(module, "branch_entry", (PyObject *)&pyrf_branch_entry__type) < 0) { Py_DECREF(&pyrf_branch_entry__type); goto error; }
Py_INCREF(&pyrf_branch_stack__type); if (PyModule_AddObject(module, "branch_stack", (PyObject *)&pyrf_branch_stack__type) < 0) { Py_DECREF(&pyrf_branch_stack__type); goto error; }
dict = PyModule_GetDict(module); if (dict == NULL) goto error;
for (i = 0; perf__constants[i].name != NULL; i++) { obj = PyLong_FromLong(perf__constants[i].value); if (obj == NULL) goto error; PyDict_SetItemString(dict, perf__constants[i].name, obj); Py_DECREF(obj); }
error: if (PyErr_Occurred()) { Py_XDECREF(module); return NULL; } return module;}