sleepgraph.py 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237
  1. #!/usr/bin/python2
  2. #
  3. # Tool for analyzing suspend/resume timing
  4. # Copyright (c) 2013, Intel Corporation.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms and conditions of the GNU General Public License,
  8. # version 2, as published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. # more details.
  14. #
  15. # Authors:
  16. # Todd Brandt <todd.e.brandt@linux.intel.com>
  17. #
  18. # Links:
  19. # Home Page
  20. # https://01.org/suspendresume
  21. # Source repo
  22. # git@github.com:01org/pm-graph
  23. #
  24. # Description:
  25. # This tool is designed to assist kernel and OS developers in optimizing
  26. # their linux stack's suspend/resume time. Using a kernel image built
  27. # with a few extra options enabled, the tool will execute a suspend and
  28. # will capture dmesg and ftrace data until resume is complete. This data
  29. # is transformed into a device timeline and a callgraph to give a quick
  30. # and detailed view of which devices and callbacks are taking the most
  31. # time in suspend/resume. The output is a single html file which can be
  32. # viewed in firefox or chrome.
  33. #
  34. # The following kernel build options are required:
  35. # CONFIG_PM_DEBUG=y
  36. # CONFIG_PM_SLEEP_DEBUG=y
  37. # CONFIG_FTRACE=y
  38. # CONFIG_FUNCTION_TRACER=y
  39. # CONFIG_FUNCTION_GRAPH_TRACER=y
  40. # CONFIG_KPROBES=y
  41. # CONFIG_KPROBES_ON_FTRACE=y
  42. #
  43. # For kernel versions older than 3.15:
  44. # The following additional kernel parameters are required:
  45. # (e.g. in file /etc/default/grub)
  46. # GRUB_CMDLINE_LINUX_DEFAULT="... initcall_debug log_buf_len=16M ..."
  47. #
  48. # ----------------- LIBRARIES --------------------
  49. import sys
  50. import time
  51. import os
  52. import string
  53. import re
  54. import platform
  55. import signal
  56. from datetime import datetime
  57. import struct
  58. import ConfigParser
  59. import gzip
  60. from threading import Thread
  61. from subprocess import call, Popen, PIPE
  62. def pprint(msg):
  63. print(msg)
  64. sys.stdout.flush()
  65. # ----------------- CLASSES --------------------
  66. # Class: SystemValues
  67. # Description:
  68. # A global, single-instance container used to
  69. # store system values and test parameters
  70. class SystemValues:
  71. title = 'SleepGraph'
  72. version = '5.2'
  73. ansi = False
  74. rs = 0
  75. display = ''
  76. gzip = False
  77. sync = False
  78. verbose = False
  79. testlog = True
  80. dmesglog = False
  81. ftracelog = False
  82. mindevlen = 0.0
  83. mincglen = 0.0
  84. cgphase = ''
  85. cgtest = -1
  86. cgskip = ''
  87. multitest = {'run': False, 'count': 0, 'delay': 0}
  88. max_graph_depth = 0
  89. callloopmaxgap = 0.0001
  90. callloopmaxlen = 0.005
  91. bufsize = 0
  92. cpucount = 0
  93. memtotal = 204800
  94. memfree = 204800
  95. srgap = 0
  96. cgexp = False
  97. testdir = ''
  98. outdir = ''
  99. tpath = '/sys/kernel/debug/tracing/'
  100. fpdtpath = '/sys/firmware/acpi/tables/FPDT'
  101. epath = '/sys/kernel/debug/tracing/events/power/'
  102. pmdpath = '/sys/power/pm_debug_messages'
  103. traceevents = [
  104. 'suspend_resume',
  105. 'device_pm_callback_end',
  106. 'device_pm_callback_start'
  107. ]
  108. logmsg = ''
  109. testcommand = ''
  110. mempath = '/dev/mem'
  111. powerfile = '/sys/power/state'
  112. mempowerfile = '/sys/power/mem_sleep'
  113. diskpowerfile = '/sys/power/disk'
  114. suspendmode = 'mem'
  115. memmode = ''
  116. diskmode = ''
  117. hostname = 'localhost'
  118. prefix = 'test'
  119. teststamp = ''
  120. sysstamp = ''
  121. dmesgstart = 0.0
  122. dmesgfile = ''
  123. ftracefile = ''
  124. htmlfile = 'output.html'
  125. result = ''
  126. rtcwake = True
  127. rtcwaketime = 15
  128. rtcpath = ''
  129. devicefilter = []
  130. cgfilter = []
  131. stamp = 0
  132. execcount = 1
  133. x2delay = 0
  134. skiphtml = False
  135. usecallgraph = False
  136. usetraceevents = False
  137. usetracemarkers = True
  138. usekprobes = True
  139. usedevsrc = False
  140. useprocmon = False
  141. notestrun = False
  142. cgdump = False
  143. devdump = False
  144. mixedphaseheight = True
  145. devprops = dict()
  146. predelay = 0
  147. postdelay = 0
  148. pmdebug = ''
  149. tracefuncs = {
  150. 'sys_sync': {},
  151. 'ksys_sync': {},
  152. '__pm_notifier_call_chain': {},
  153. 'pm_prepare_console': {},
  154. 'pm_notifier_call_chain': {},
  155. 'freeze_processes': {},
  156. 'freeze_kernel_threads': {},
  157. 'pm_restrict_gfp_mask': {},
  158. 'acpi_suspend_begin': {},
  159. 'acpi_hibernation_begin': {},
  160. 'acpi_hibernation_enter': {},
  161. 'acpi_hibernation_leave': {},
  162. 'acpi_pm_freeze': {},
  163. 'acpi_pm_thaw': {},
  164. 'hibernate_preallocate_memory': {},
  165. 'create_basic_memory_bitmaps': {},
  166. 'swsusp_write': {},
  167. 'suspend_console': {},
  168. 'acpi_pm_prepare': {},
  169. 'syscore_suspend': {},
  170. 'arch_enable_nonboot_cpus_end': {},
  171. 'syscore_resume': {},
  172. 'acpi_pm_finish': {},
  173. 'resume_console': {},
  174. 'acpi_pm_end': {},
  175. 'pm_restore_gfp_mask': {},
  176. 'thaw_processes': {},
  177. 'pm_restore_console': {},
  178. 'CPU_OFF': {
  179. 'func':'_cpu_down',
  180. 'args_x86_64': {'cpu':'%di:s32'},
  181. 'format': 'CPU_OFF[{cpu}]'
  182. },
  183. 'CPU_ON': {
  184. 'func':'_cpu_up',
  185. 'args_x86_64': {'cpu':'%di:s32'},
  186. 'format': 'CPU_ON[{cpu}]'
  187. },
  188. }
  189. dev_tracefuncs = {
  190. # general wait/delay/sleep
  191. 'msleep': { 'args_x86_64': {'time':'%di:s32'}, 'ub': 1 },
  192. 'schedule_timeout': { 'args_x86_64': {'timeout':'%di:s32'}, 'ub': 1 },
  193. 'udelay': { 'func':'__const_udelay', 'args_x86_64': {'loops':'%di:s32'}, 'ub': 1 },
  194. 'usleep_range': { 'args_x86_64': {'min':'%di:s32', 'max':'%si:s32'}, 'ub': 1 },
  195. 'mutex_lock_slowpath': { 'func':'__mutex_lock_slowpath', 'ub': 1 },
  196. 'acpi_os_stall': {'ub': 1},
  197. # ACPI
  198. 'acpi_resume_power_resources': {},
  199. 'acpi_ps_parse_aml': {},
  200. # filesystem
  201. 'ext4_sync_fs': {},
  202. # 80211
  203. 'ath10k_bmi_read_memory': { 'args_x86_64': {'length':'%cx:s32'} },
  204. 'ath10k_bmi_write_memory': { 'args_x86_64': {'length':'%cx:s32'} },
  205. 'ath10k_bmi_fast_download': { 'args_x86_64': {'length':'%cx:s32'} },
  206. 'iwlagn_mac_start': {},
  207. 'iwlagn_alloc_bcast_station': {},
  208. 'iwl_trans_pcie_start_hw': {},
  209. 'iwl_trans_pcie_start_fw': {},
  210. 'iwl_run_init_ucode': {},
  211. 'iwl_load_ucode_wait_alive': {},
  212. 'iwl_alive_start': {},
  213. 'iwlagn_mac_stop': {},
  214. 'iwlagn_mac_suspend': {},
  215. 'iwlagn_mac_resume': {},
  216. 'iwlagn_mac_add_interface': {},
  217. 'iwlagn_mac_remove_interface': {},
  218. 'iwlagn_mac_change_interface': {},
  219. 'iwlagn_mac_config': {},
  220. 'iwlagn_configure_filter': {},
  221. 'iwlagn_mac_hw_scan': {},
  222. 'iwlagn_bss_info_changed': {},
  223. 'iwlagn_mac_channel_switch': {},
  224. 'iwlagn_mac_flush': {},
  225. # ATA
  226. 'ata_eh_recover': { 'args_x86_64': {'port':'+36(%di):s32'} },
  227. # i915
  228. 'i915_gem_resume': {},
  229. 'i915_restore_state': {},
  230. 'intel_opregion_setup': {},
  231. 'g4x_pre_enable_dp': {},
  232. 'vlv_pre_enable_dp': {},
  233. 'chv_pre_enable_dp': {},
  234. 'g4x_enable_dp': {},
  235. 'vlv_enable_dp': {},
  236. 'intel_hpd_init': {},
  237. 'intel_opregion_register': {},
  238. 'intel_dp_detect': {},
  239. 'intel_hdmi_detect': {},
  240. 'intel_opregion_init': {},
  241. 'intel_fbdev_set_suspend': {},
  242. }
  243. cgblacklist = []
  244. kprobes = dict()
  245. timeformat = '%.3f'
  246. cmdline = '%s %s' % \
  247. (os.path.basename(sys.argv[0]), ' '.join(sys.argv[1:]))
  248. sudouser = ''
  249. def __init__(self):
  250. self.archargs = 'args_'+platform.machine()
  251. self.hostname = platform.node()
  252. if(self.hostname == ''):
  253. self.hostname = 'localhost'
  254. rtc = "rtc0"
  255. if os.path.exists('/dev/rtc'):
  256. rtc = os.readlink('/dev/rtc')
  257. rtc = '/sys/class/rtc/'+rtc
  258. if os.path.exists(rtc) and os.path.exists(rtc+'/date') and \
  259. os.path.exists(rtc+'/time') and os.path.exists(rtc+'/wakealarm'):
  260. self.rtcpath = rtc
  261. if (hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()):
  262. self.ansi = True
  263. self.testdir = datetime.now().strftime('suspend-%y%m%d-%H%M%S')
  264. if os.getuid() == 0 and 'SUDO_USER' in os.environ and \
  265. os.environ['SUDO_USER']:
  266. self.sudouser = os.environ['SUDO_USER']
  267. def vprint(self, msg):
  268. self.logmsg += msg+'\n'
  269. if self.verbose or msg.startswith('WARNING:'):
  270. pprint(msg)
  271. def signalHandler(self, signum, frame):
  272. if not self.result:
  273. return
  274. signame = self.signames[signum] if signum in self.signames else 'UNKNOWN'
  275. msg = 'Signal %s caused a tool exit, line %d' % (signame, frame.f_lineno)
  276. sysvals.outputResult({'error':msg})
  277. sys.exit(3)
  278. def signalHandlerInit(self):
  279. capture = ['BUS', 'SYS', 'XCPU', 'XFSZ', 'PWR', 'HUP', 'INT', 'QUIT',
  280. 'ILL', 'ABRT', 'FPE', 'SEGV', 'TERM', 'TSTP']
  281. self.signames = dict()
  282. for i in capture:
  283. s = 'SIG'+i
  284. try:
  285. signum = getattr(signal, s)
  286. signal.signal(signum, self.signalHandler)
  287. except:
  288. continue
  289. self.signames[signum] = s
  290. def rootCheck(self, fatal=True):
  291. if(os.access(self.powerfile, os.W_OK)):
  292. return True
  293. if fatal:
  294. msg = 'This command requires sysfs mount and root access'
  295. pprint('ERROR: %s\n' % msg)
  296. self.outputResult({'error':msg})
  297. sys.exit(1)
  298. return False
  299. def rootUser(self, fatal=False):
  300. if 'USER' in os.environ and os.environ['USER'] == 'root':
  301. return True
  302. if fatal:
  303. msg = 'This command must be run as root'
  304. pprint('ERROR: %s\n' % msg)
  305. self.outputResult({'error':msg})
  306. sys.exit(1)
  307. return False
  308. def getExec(self, cmd):
  309. dirlist = ['/sbin', '/bin', '/usr/sbin', '/usr/bin',
  310. '/usr/local/sbin', '/usr/local/bin']
  311. for path in dirlist:
  312. cmdfull = os.path.join(path, cmd)
  313. if os.path.exists(cmdfull):
  314. return cmdfull
  315. return ''
  316. def setPrecision(self, num):
  317. if num < 0 or num > 6:
  318. return
  319. self.timeformat = '%.{0}f'.format(num)
  320. def setOutputFolder(self, value):
  321. args = dict()
  322. n = datetime.now()
  323. args['date'] = n.strftime('%y%m%d')
  324. args['time'] = n.strftime('%H%M%S')
  325. args['hostname'] = args['host'] = self.hostname
  326. return value.format(**args)
  327. def setOutputFile(self):
  328. if self.dmesgfile != '':
  329. m = re.match('(?P<name>.*)_dmesg\.txt.*', self.dmesgfile)
  330. if(m):
  331. self.htmlfile = m.group('name')+'.html'
  332. if self.ftracefile != '':
  333. m = re.match('(?P<name>.*)_ftrace\.txt.*', self.ftracefile)
  334. if(m):
  335. self.htmlfile = m.group('name')+'.html'
  336. def systemInfo(self, info):
  337. p = c = m = b = ''
  338. if 'baseboard-manufacturer' in info:
  339. m = info['baseboard-manufacturer']
  340. elif 'system-manufacturer' in info:
  341. m = info['system-manufacturer']
  342. if 'baseboard-product-name' in info:
  343. p = info['baseboard-product-name']
  344. elif 'system-product-name' in info:
  345. p = info['system-product-name']
  346. if 'processor-version' in info:
  347. c = info['processor-version']
  348. if 'bios-version' in info:
  349. b = info['bios-version']
  350. self.sysstamp = '# sysinfo | man:%s | plat:%s | cpu:%s | bios:%s | numcpu:%d | memsz:%d | memfr:%d' % \
  351. (m, p, c, b, self.cpucount, self.memtotal, self.memfree)
  352. def printSystemInfo(self, fatal=False):
  353. self.rootCheck(True)
  354. out = dmidecode(self.mempath, fatal)
  355. if len(out) < 1:
  356. return
  357. fmt = '%-24s: %s'
  358. for name in sorted(out):
  359. print fmt % (name, out[name])
  360. print fmt % ('cpucount', ('%d' % self.cpucount))
  361. print fmt % ('memtotal', ('%d kB' % self.memtotal))
  362. print fmt % ('memfree', ('%d kB' % self.memfree))
  363. def cpuInfo(self):
  364. self.cpucount = 0
  365. fp = open('/proc/cpuinfo', 'r')
  366. for line in fp:
  367. if re.match('^processor[ \t]*:[ \t]*[0-9]*', line):
  368. self.cpucount += 1
  369. fp.close()
  370. fp = open('/proc/meminfo', 'r')
  371. for line in fp:
  372. m = re.match('^MemTotal:[ \t]*(?P<sz>[0-9]*) *kB', line)
  373. if m:
  374. self.memtotal = int(m.group('sz'))
  375. m = re.match('^MemFree:[ \t]*(?P<sz>[0-9]*) *kB', line)
  376. if m:
  377. self.memfree = int(m.group('sz'))
  378. fp.close()
  379. def initTestOutput(self, name):
  380. self.prefix = self.hostname
  381. v = open('/proc/version', 'r').read().strip()
  382. kver = string.split(v)[2]
  383. fmt = name+'-%m%d%y-%H%M%S'
  384. testtime = datetime.now().strftime(fmt)
  385. self.teststamp = \
  386. '# '+testtime+' '+self.prefix+' '+self.suspendmode+' '+kver
  387. ext = ''
  388. if self.gzip:
  389. ext = '.gz'
  390. self.dmesgfile = \
  391. self.testdir+'/'+self.prefix+'_'+self.suspendmode+'_dmesg.txt'+ext
  392. self.ftracefile = \
  393. self.testdir+'/'+self.prefix+'_'+self.suspendmode+'_ftrace.txt'+ext
  394. self.htmlfile = \
  395. self.testdir+'/'+self.prefix+'_'+self.suspendmode+'.html'
  396. if not os.path.isdir(self.testdir):
  397. os.mkdir(self.testdir)
  398. def getValueList(self, value):
  399. out = []
  400. for i in value.split(','):
  401. if i.strip():
  402. out.append(i.strip())
  403. return out
  404. def setDeviceFilter(self, value):
  405. self.devicefilter = self.getValueList(value)
  406. def setCallgraphFilter(self, value):
  407. self.cgfilter = self.getValueList(value)
  408. def setCallgraphBlacklist(self, file):
  409. self.cgblacklist = self.listFromFile(file)
  410. def rtcWakeAlarmOn(self):
  411. call('echo 0 > '+self.rtcpath+'/wakealarm', shell=True)
  412. nowtime = open(self.rtcpath+'/since_epoch', 'r').read().strip()
  413. if nowtime:
  414. nowtime = int(nowtime)
  415. else:
  416. # if hardware time fails, use the software time
  417. nowtime = int(datetime.now().strftime('%s'))
  418. alarm = nowtime + self.rtcwaketime
  419. call('echo %d > %s/wakealarm' % (alarm, self.rtcpath), shell=True)
  420. def rtcWakeAlarmOff(self):
  421. call('echo 0 > %s/wakealarm' % self.rtcpath, shell=True)
  422. def initdmesg(self):
  423. # get the latest time stamp from the dmesg log
  424. fp = Popen('dmesg', stdout=PIPE).stdout
  425. ktime = '0'
  426. for line in fp:
  427. line = line.replace('\r\n', '')
  428. idx = line.find('[')
  429. if idx > 1:
  430. line = line[idx:]
  431. m = re.match('[ \t]*(\[ *)(?P<ktime>[0-9\.]*)(\]) (?P<msg>.*)', line)
  432. if(m):
  433. ktime = m.group('ktime')
  434. fp.close()
  435. self.dmesgstart = float(ktime)
  436. def getdmesg(self, testdata):
  437. op = self.writeDatafileHeader(sysvals.dmesgfile, testdata)
  438. # store all new dmesg lines since initdmesg was called
  439. fp = Popen('dmesg', stdout=PIPE).stdout
  440. for line in fp:
  441. line = line.replace('\r\n', '')
  442. idx = line.find('[')
  443. if idx > 1:
  444. line = line[idx:]
  445. m = re.match('[ \t]*(\[ *)(?P<ktime>[0-9\.]*)(\]) (?P<msg>.*)', line)
  446. if(not m):
  447. continue
  448. ktime = float(m.group('ktime'))
  449. if ktime > self.dmesgstart:
  450. op.write(line)
  451. fp.close()
  452. op.close()
  453. def listFromFile(self, file):
  454. list = []
  455. fp = open(file)
  456. for i in fp.read().split('\n'):
  457. i = i.strip()
  458. if i and i[0] != '#':
  459. list.append(i)
  460. fp.close()
  461. return list
  462. def addFtraceFilterFunctions(self, file):
  463. for i in self.listFromFile(file):
  464. if len(i) < 2:
  465. continue
  466. self.tracefuncs[i] = dict()
  467. def getFtraceFilterFunctions(self, current):
  468. self.rootCheck(True)
  469. if not current:
  470. call('cat '+self.tpath+'available_filter_functions', shell=True)
  471. return
  472. master = self.listFromFile(self.tpath+'available_filter_functions')
  473. for i in self.tracefuncs:
  474. if 'func' in self.tracefuncs[i]:
  475. i = self.tracefuncs[i]['func']
  476. if i in master:
  477. print i
  478. else:
  479. print self.colorText(i)
  480. def setFtraceFilterFunctions(self, list):
  481. master = self.listFromFile(self.tpath+'available_filter_functions')
  482. flist = ''
  483. for i in list:
  484. if i not in master:
  485. continue
  486. if ' [' in i:
  487. flist += i.split(' ')[0]+'\n'
  488. else:
  489. flist += i+'\n'
  490. fp = open(self.tpath+'set_graph_function', 'w')
  491. fp.write(flist)
  492. fp.close()
  493. def basicKprobe(self, name):
  494. self.kprobes[name] = {'name': name,'func': name,'args': dict(),'format': name}
  495. def defaultKprobe(self, name, kdata):
  496. k = kdata
  497. for field in ['name', 'format', 'func']:
  498. if field not in k:
  499. k[field] = name
  500. if self.archargs in k:
  501. k['args'] = k[self.archargs]
  502. else:
  503. k['args'] = dict()
  504. k['format'] = name
  505. self.kprobes[name] = k
  506. def kprobeColor(self, name):
  507. if name not in self.kprobes or 'color' not in self.kprobes[name]:
  508. return ''
  509. return self.kprobes[name]['color']
  510. def kprobeDisplayName(self, name, dataraw):
  511. if name not in self.kprobes:
  512. self.basicKprobe(name)
  513. data = ''
  514. quote=0
  515. # first remvoe any spaces inside quotes, and the quotes
  516. for c in dataraw:
  517. if c == '"':
  518. quote = (quote + 1) % 2
  519. if quote and c == ' ':
  520. data += '_'
  521. elif c != '"':
  522. data += c
  523. fmt, args = self.kprobes[name]['format'], self.kprobes[name]['args']
  524. arglist = dict()
  525. # now process the args
  526. for arg in sorted(args):
  527. arglist[arg] = ''
  528. m = re.match('.* '+arg+'=(?P<arg>.*) ', data);
  529. if m:
  530. arglist[arg] = m.group('arg')
  531. else:
  532. m = re.match('.* '+arg+'=(?P<arg>.*)', data);
  533. if m:
  534. arglist[arg] = m.group('arg')
  535. out = fmt.format(**arglist)
  536. out = out.replace(' ', '_').replace('"', '')
  537. return out
  538. def kprobeText(self, kname, kprobe):
  539. name = fmt = func = kname
  540. args = dict()
  541. if 'name' in kprobe:
  542. name = kprobe['name']
  543. if 'format' in kprobe:
  544. fmt = kprobe['format']
  545. if 'func' in kprobe:
  546. func = kprobe['func']
  547. if self.archargs in kprobe:
  548. args = kprobe[self.archargs]
  549. if 'args' in kprobe:
  550. args = kprobe['args']
  551. if re.findall('{(?P<n>[a-z,A-Z,0-9]*)}', func):
  552. doError('Kprobe "%s" has format info in the function name "%s"' % (name, func))
  553. for arg in re.findall('{(?P<n>[a-z,A-Z,0-9]*)}', fmt):
  554. if arg not in args:
  555. doError('Kprobe "%s" is missing argument "%s"' % (name, arg))
  556. val = 'p:%s_cal %s' % (name, func)
  557. for i in sorted(args):
  558. val += ' %s=%s' % (i, args[i])
  559. val += '\nr:%s_ret %s $retval\n' % (name, func)
  560. return val
  561. def addKprobes(self, output=False):
  562. if len(self.kprobes) < 1:
  563. return
  564. if output:
  565. pprint(' kprobe functions in this kernel:')
  566. # first test each kprobe
  567. rejects = []
  568. # sort kprobes: trace, ub-dev, custom, dev
  569. kpl = [[], [], [], []]
  570. linesout = len(self.kprobes)
  571. for name in sorted(self.kprobes):
  572. res = self.colorText('YES', 32)
  573. if not self.testKprobe(name, self.kprobes[name]):
  574. res = self.colorText('NO')
  575. rejects.append(name)
  576. else:
  577. if name in self.tracefuncs:
  578. kpl[0].append(name)
  579. elif name in self.dev_tracefuncs:
  580. if 'ub' in self.dev_tracefuncs[name]:
  581. kpl[1].append(name)
  582. else:
  583. kpl[3].append(name)
  584. else:
  585. kpl[2].append(name)
  586. if output:
  587. pprint(' %s: %s' % (name, res))
  588. kplist = kpl[0] + kpl[1] + kpl[2] + kpl[3]
  589. # remove all failed ones from the list
  590. for name in rejects:
  591. self.kprobes.pop(name)
  592. # set the kprobes all at once
  593. self.fsetVal('', 'kprobe_events')
  594. kprobeevents = ''
  595. for kp in kplist:
  596. kprobeevents += self.kprobeText(kp, self.kprobes[kp])
  597. self.fsetVal(kprobeevents, 'kprobe_events')
  598. if output:
  599. check = self.fgetVal('kprobe_events')
  600. linesack = (len(check.split('\n')) - 1) / 2
  601. pprint(' kprobe functions enabled: %d/%d' % (linesack, linesout))
  602. self.fsetVal('1', 'events/kprobes/enable')
  603. def testKprobe(self, kname, kprobe):
  604. self.fsetVal('0', 'events/kprobes/enable')
  605. kprobeevents = self.kprobeText(kname, kprobe)
  606. if not kprobeevents:
  607. return False
  608. try:
  609. self.fsetVal(kprobeevents, 'kprobe_events')
  610. check = self.fgetVal('kprobe_events')
  611. except:
  612. return False
  613. linesout = len(kprobeevents.split('\n'))
  614. linesack = len(check.split('\n'))
  615. if linesack < linesout:
  616. return False
  617. return True
  618. def setVal(self, val, file, mode='w'):
  619. if not os.path.exists(file):
  620. return False
  621. try:
  622. fp = open(file, mode, 0)
  623. fp.write(val)
  624. fp.flush()
  625. fp.close()
  626. except:
  627. return False
  628. return True
  629. def fsetVal(self, val, path, mode='w'):
  630. return self.setVal(val, self.tpath+path, mode)
  631. def getVal(self, file):
  632. res = ''
  633. if not os.path.exists(file):
  634. return res
  635. try:
  636. fp = open(file, 'r')
  637. res = fp.read()
  638. fp.close()
  639. except:
  640. pass
  641. return res
  642. def fgetVal(self, path):
  643. return self.getVal(self.tpath+path)
  644. def cleanupFtrace(self):
  645. if(self.usecallgraph or self.usetraceevents or self.usedevsrc):
  646. self.fsetVal('0', 'events/kprobes/enable')
  647. self.fsetVal('', 'kprobe_events')
  648. self.fsetVal('1024', 'buffer_size_kb')
  649. if self.pmdebug:
  650. self.setVal(self.pmdebug, self.pmdpath)
  651. def setupAllKprobes(self):
  652. for name in self.tracefuncs:
  653. self.defaultKprobe(name, self.tracefuncs[name])
  654. for name in self.dev_tracefuncs:
  655. self.defaultKprobe(name, self.dev_tracefuncs[name])
  656. def isCallgraphFunc(self, name):
  657. if len(self.tracefuncs) < 1 and self.suspendmode == 'command':
  658. return True
  659. for i in self.tracefuncs:
  660. if 'func' in self.tracefuncs[i]:
  661. f = self.tracefuncs[i]['func']
  662. else:
  663. f = i
  664. if name == f:
  665. return True
  666. return False
  667. def initFtrace(self):
  668. self.printSystemInfo(False)
  669. pprint('INITIALIZING FTRACE...')
  670. # turn trace off
  671. self.fsetVal('0', 'tracing_on')
  672. self.cleanupFtrace()
  673. # pm debug messages
  674. pv = self.getVal(self.pmdpath)
  675. if pv != '1':
  676. self.setVal('1', self.pmdpath)
  677. self.pmdebug = pv
  678. # set the trace clock to global
  679. self.fsetVal('global', 'trace_clock')
  680. self.fsetVal('nop', 'current_tracer')
  681. # set trace buffer to an appropriate value
  682. cpus = max(1, self.cpucount)
  683. if self.bufsize > 0:
  684. tgtsize = self.bufsize
  685. elif self.usecallgraph or self.usedevsrc:
  686. bmax = (1*1024*1024) if self.suspendmode == 'disk' else (3*1024*1024)
  687. tgtsize = min(self.memfree, bmax)
  688. else:
  689. tgtsize = 65536
  690. while not self.fsetVal('%d' % (tgtsize / cpus), 'buffer_size_kb'):
  691. # if the size failed to set, lower it and keep trying
  692. tgtsize -= 65536
  693. if tgtsize < 65536:
  694. tgtsize = int(self.fgetVal('buffer_size_kb')) * cpus
  695. break
  696. pprint('Setting trace buffers to %d kB (%d kB per cpu)' % (tgtsize, tgtsize/cpus))
  697. # initialize the callgraph trace
  698. if(self.usecallgraph):
  699. # set trace type
  700. self.fsetVal('function_graph', 'current_tracer')
  701. self.fsetVal('', 'set_ftrace_filter')
  702. # set trace format options
  703. self.fsetVal('print-parent', 'trace_options')
  704. self.fsetVal('funcgraph-abstime', 'trace_options')
  705. self.fsetVal('funcgraph-cpu', 'trace_options')
  706. self.fsetVal('funcgraph-duration', 'trace_options')
  707. self.fsetVal('funcgraph-proc', 'trace_options')
  708. self.fsetVal('funcgraph-tail', 'trace_options')
  709. self.fsetVal('nofuncgraph-overhead', 'trace_options')
  710. self.fsetVal('context-info', 'trace_options')
  711. self.fsetVal('graph-time', 'trace_options')
  712. self.fsetVal('%d' % self.max_graph_depth, 'max_graph_depth')
  713. cf = ['dpm_run_callback']
  714. if(self.usetraceevents):
  715. cf += ['dpm_prepare', 'dpm_complete']
  716. for fn in self.tracefuncs:
  717. if 'func' in self.tracefuncs[fn]:
  718. cf.append(self.tracefuncs[fn]['func'])
  719. else:
  720. cf.append(fn)
  721. self.setFtraceFilterFunctions(cf)
  722. # initialize the kprobe trace
  723. elif self.usekprobes:
  724. for name in self.tracefuncs:
  725. self.defaultKprobe(name, self.tracefuncs[name])
  726. if self.usedevsrc:
  727. for name in self.dev_tracefuncs:
  728. self.defaultKprobe(name, self.dev_tracefuncs[name])
  729. pprint('INITIALIZING KPROBES...')
  730. self.addKprobes(self.verbose)
  731. if(self.usetraceevents):
  732. # turn trace events on
  733. events = iter(self.traceevents)
  734. for e in events:
  735. self.fsetVal('1', 'events/power/'+e+'/enable')
  736. # clear the trace buffer
  737. self.fsetVal('', 'trace')
  738. def verifyFtrace(self):
  739. # files needed for any trace data
  740. files = ['buffer_size_kb', 'current_tracer', 'trace', 'trace_clock',
  741. 'trace_marker', 'trace_options', 'tracing_on']
  742. # files needed for callgraph trace data
  743. tp = self.tpath
  744. if(self.usecallgraph):
  745. files += [
  746. 'available_filter_functions',
  747. 'set_ftrace_filter',
  748. 'set_graph_function'
  749. ]
  750. for f in files:
  751. if(os.path.exists(tp+f) == False):
  752. return False
  753. return True
  754. def verifyKprobes(self):
  755. # files needed for kprobes to work
  756. files = ['kprobe_events', 'events']
  757. tp = self.tpath
  758. for f in files:
  759. if(os.path.exists(tp+f) == False):
  760. return False
  761. return True
  762. def colorText(self, str, color=31):
  763. if not self.ansi:
  764. return str
  765. return '\x1B[%d;40m%s\x1B[m' % (color, str)
  766. def writeDatafileHeader(self, filename, testdata):
  767. fp = self.openlog(filename, 'w')
  768. fp.write('%s\n%s\n# command | %s\n' % (self.teststamp, self.sysstamp, self.cmdline))
  769. for test in testdata:
  770. if 'fw' in test:
  771. fw = test['fw']
  772. if(fw):
  773. fp.write('# fwsuspend %u fwresume %u\n' % (fw[0], fw[1]))
  774. if 'bat' in test:
  775. (a1, c1), (a2, c2) = test['bat']
  776. fp.write('# battery %s %d %s %d\n' % (a1, c1, a2, c2))
  777. if test['error'] or len(testdata) > 1:
  778. fp.write('# enter_sleep_error %s\n' % test['error'])
  779. return fp
  780. def sudoUserchown(self, dir):
  781. if os.path.exists(dir) and self.sudouser:
  782. cmd = 'chown -R {0}:{0} {1} > /dev/null 2>&1'
  783. call(cmd.format(self.sudouser, dir), shell=True)
  784. def outputResult(self, testdata, num=0):
  785. if not self.result:
  786. return
  787. n = ''
  788. if num > 0:
  789. n = '%d' % num
  790. fp = open(self.result, 'a')
  791. if 'error' in testdata:
  792. fp.write('result%s: fail\n' % n)
  793. fp.write('error%s: %s\n' % (n, testdata['error']))
  794. else:
  795. fp.write('result%s: pass\n' % n)
  796. for v in ['suspend', 'resume', 'boot', 'lastinit']:
  797. if v in testdata:
  798. fp.write('%s%s: %.3f\n' % (v, n, testdata[v]))
  799. for v in ['fwsuspend', 'fwresume']:
  800. if v in testdata:
  801. fp.write('%s%s: %.3f\n' % (v, n, testdata[v] / 1000000.0))
  802. if 'bugurl' in testdata:
  803. fp.write('url%s: %s\n' % (n, testdata['bugurl']))
  804. fp.close()
  805. self.sudoUserchown(self.result)
  806. def configFile(self, file):
  807. dir = os.path.dirname(os.path.realpath(__file__))
  808. if os.path.exists(file):
  809. return file
  810. elif os.path.exists(dir+'/'+file):
  811. return dir+'/'+file
  812. elif os.path.exists(dir+'/config/'+file):
  813. return dir+'/config/'+file
  814. return ''
  815. def openlog(self, filename, mode):
  816. isgz = self.gzip
  817. if mode == 'r':
  818. try:
  819. with gzip.open(filename, mode+'b') as fp:
  820. test = fp.read(64)
  821. isgz = True
  822. except:
  823. isgz = False
  824. if isgz:
  825. return gzip.open(filename, mode+'b')
  826. return open(filename, mode)
  827. sysvals = SystemValues()
  828. switchvalues = ['enable', 'disable', 'on', 'off', 'true', 'false', '1', '0']
  829. switchoff = ['disable', 'off', 'false', '0']
  830. suspendmodename = {
  831. 'freeze': 'Freeze (S0)',
  832. 'standby': 'Standby (S1)',
  833. 'mem': 'Suspend (S3)',
  834. 'disk': 'Hibernate (S4)'
  835. }
  836. # Class: DevProps
  837. # Description:
  838. # Simple class which holds property values collected
  839. # for all the devices used in the timeline.
  840. class DevProps:
  841. def __init__(self):
  842. self.syspath = ''
  843. self.altname = ''
  844. self.async = True
  845. self.xtraclass = ''
  846. self.xtrainfo = ''
  847. def out(self, dev):
  848. return '%s,%s,%d;' % (dev, self.altname, self.async)
  849. def debug(self, dev):
  850. pprint('%s:\n\taltname = %s\n\t async = %s' % (dev, self.altname, self.async))
  851. def altName(self, dev):
  852. if not self.altname or self.altname == dev:
  853. return dev
  854. return '%s [%s]' % (self.altname, dev)
  855. def xtraClass(self):
  856. if self.xtraclass:
  857. return ' '+self.xtraclass
  858. if not self.async:
  859. return ' sync'
  860. return ''
  861. def xtraInfo(self):
  862. if self.xtraclass:
  863. return ' '+self.xtraclass
  864. if self.async:
  865. return ' async_device'
  866. return ' sync_device'
  867. # Class: DeviceNode
  868. # Description:
  869. # A container used to create a device hierachy, with a single root node
  870. # and a tree of child nodes. Used by Data.deviceTopology()
  871. class DeviceNode:
  872. def __init__(self, nodename, nodedepth):
  873. self.name = nodename
  874. self.children = []
  875. self.depth = nodedepth
  876. # Class: Data
  877. # Description:
  878. # The primary container for suspend/resume test data. There is one for
  879. # each test run. The data is organized into a cronological hierarchy:
  880. # Data.dmesg {
  881. # phases {
  882. # 10 sequential, non-overlapping phases of S/R
  883. # contents: times for phase start/end, order/color data for html
  884. # devlist {
  885. # device callback or action list for this phase
  886. # device {
  887. # a single device callback or generic action
  888. # contents: start/stop times, pid/cpu/driver info
  889. # parents/children, html id for timeline/callgraph
  890. # optionally includes an ftrace callgraph
  891. # optionally includes dev/ps data
  892. # }
  893. # }
  894. # }
  895. # }
  896. #
  897. class Data:
  898. phasedef = {
  899. 'suspend_prepare': {'order': 0, 'color': '#CCFFCC'},
  900. 'suspend': {'order': 1, 'color': '#88FF88'},
  901. 'suspend_late': {'order': 2, 'color': '#00AA00'},
  902. 'suspend_noirq': {'order': 3, 'color': '#008888'},
  903. 'suspend_machine': {'order': 4, 'color': '#0000FF'},
  904. 'resume_machine': {'order': 5, 'color': '#FF0000'},
  905. 'resume_noirq': {'order': 6, 'color': '#FF9900'},
  906. 'resume_early': {'order': 7, 'color': '#FFCC00'},
  907. 'resume': {'order': 8, 'color': '#FFFF88'},
  908. 'resume_complete': {'order': 9, 'color': '#FFFFCC'},
  909. }
  910. errlist = {
  911. 'HWERROR' : '.*\[ *Hardware Error *\].*',
  912. 'FWBUG' : '.*\[ *Firmware Bug *\].*',
  913. 'BUG' : '.*BUG.*',
  914. 'ERROR' : '.*ERROR.*',
  915. 'WARNING' : '.*WARNING.*',
  916. 'IRQ' : '.*genirq: .*',
  917. 'TASKFAIL': '.*Freezing of tasks failed.*',
  918. }
  919. def __init__(self, num):
  920. idchar = 'abcdefghij'
  921. self.start = 0.0 # test start
  922. self.end = 0.0 # test end
  923. self.tSuspended = 0.0 # low-level suspend start
  924. self.tResumed = 0.0 # low-level resume start
  925. self.tKernSus = 0.0 # kernel level suspend start
  926. self.tKernRes = 0.0 # kernel level resume end
  927. self.fwValid = False # is firmware data available
  928. self.fwSuspend = 0 # time spent in firmware suspend
  929. self.fwResume = 0 # time spent in firmware resume
  930. self.html_device_id = 0
  931. self.stamp = 0
  932. self.outfile = ''
  933. self.kerror = False
  934. self.battery = 0
  935. self.enterfail = ''
  936. self.currphase = ''
  937. self.pstl = dict() # process timeline
  938. self.testnumber = num
  939. self.idstr = idchar[num]
  940. self.dmesgtext = [] # dmesg text file in memory
  941. self.dmesg = dict() # root data structure
  942. self.errorinfo = {'suspend':[],'resume':[]}
  943. self.tLow = [] # time spent in low-level suspends (standby/freeze)
  944. self.devpids = []
  945. self.devicegroups = 0
  946. def sortedPhases(self):
  947. return sorted(self.dmesg, key=lambda k:self.dmesg[k]['order'])
  948. def initDevicegroups(self):
  949. # called when phases are all finished being added
  950. for phase in self.dmesg.keys():
  951. if '*' in phase:
  952. p = phase.split('*')
  953. pnew = '%s%d' % (p[0], len(p))
  954. self.dmesg[pnew] = self.dmesg.pop(phase)
  955. self.devicegroups = []
  956. for phase in self.sortedPhases():
  957. self.devicegroups.append([phase])
  958. def nextPhase(self, phase, offset):
  959. order = self.dmesg[phase]['order'] + offset
  960. for p in self.dmesg:
  961. if self.dmesg[p]['order'] == order:
  962. return p
  963. return ''
  964. def lastPhase(self):
  965. plist = self.sortedPhases()
  966. if len(plist) < 1:
  967. return ''
  968. return plist[-1]
  969. def extractErrorInfo(self):
  970. lf = sysvals.openlog(sysvals.dmesgfile, 'r')
  971. i = 0
  972. list = []
  973. for line in lf:
  974. i += 1
  975. m = re.match('[ \t]*(\[ *)(?P<ktime>[0-9\.]*)(\]) (?P<msg>.*)', line)
  976. if not m:
  977. continue
  978. t = float(m.group('ktime'))
  979. if t < self.start or t > self.end:
  980. continue
  981. dir = 'suspend' if t < self.tSuspended else 'resume'
  982. msg = m.group('msg')
  983. for err in self.errlist:
  984. if re.match(self.errlist[err], msg):
  985. list.append((err, dir, t, i, i))
  986. self.kerror = True
  987. break
  988. for e in list:
  989. type, dir, t, idx1, idx2 = e
  990. sysvals.vprint('kernel %s found in %s at %f' % (type, dir, t))
  991. self.errorinfo[dir].append((type, t, idx1, idx2))
  992. if self.kerror:
  993. sysvals.dmesglog = True
  994. lf.close()
  995. def setStart(self, time):
  996. self.start = time
  997. def setEnd(self, time):
  998. self.end = time
  999. def isTraceEventOutsideDeviceCalls(self, pid, time):
  1000. for phase in self.sortedPhases():
  1001. list = self.dmesg[phase]['list']
  1002. for dev in list:
  1003. d = list[dev]
  1004. if(d['pid'] == pid and time >= d['start'] and
  1005. time < d['end']):
  1006. return False
  1007. return True
  1008. def sourcePhase(self, start):
  1009. for phase in self.sortedPhases():
  1010. if 'machine' in phase:
  1011. continue
  1012. pend = self.dmesg[phase]['end']
  1013. if start <= pend:
  1014. return phase
  1015. return 'resume_complete'
  1016. def sourceDevice(self, phaselist, start, end, pid, type):
  1017. tgtdev = ''
  1018. for phase in phaselist:
  1019. list = self.dmesg[phase]['list']
  1020. for devname in list:
  1021. dev = list[devname]
  1022. # pid must match
  1023. if dev['pid'] != pid:
  1024. continue
  1025. devS = dev['start']
  1026. devE = dev['end']
  1027. if type == 'device':
  1028. # device target event is entirely inside the source boundary
  1029. if(start < devS or start >= devE or end <= devS or end > devE):
  1030. continue
  1031. elif type == 'thread':
  1032. # thread target event will expand the source boundary
  1033. if start < devS:
  1034. dev['start'] = start
  1035. if end > devE:
  1036. dev['end'] = end
  1037. tgtdev = dev
  1038. break
  1039. return tgtdev
  1040. def addDeviceFunctionCall(self, displayname, kprobename, proc, pid, start, end, cdata, rdata):
  1041. # try to place the call in a device
  1042. phases = self.sortedPhases()
  1043. tgtdev = self.sourceDevice(phases, start, end, pid, 'device')
  1044. # calls with device pids that occur outside device bounds are dropped
  1045. # TODO: include these somehow
  1046. if not tgtdev and pid in self.devpids:
  1047. return False
  1048. # try to place the call in a thread
  1049. if not tgtdev:
  1050. tgtdev = self.sourceDevice(phases, start, end, pid, 'thread')
  1051. # create new thread blocks, expand as new calls are found
  1052. if not tgtdev:
  1053. if proc == '<...>':
  1054. threadname = 'kthread-%d' % (pid)
  1055. else:
  1056. threadname = '%s-%d' % (proc, pid)
  1057. tgtphase = self.sourcePhase(start)
  1058. self.newAction(tgtphase, threadname, pid, '', start, end, '', ' kth', '')
  1059. return self.addDeviceFunctionCall(displayname, kprobename, proc, pid, start, end, cdata, rdata)
  1060. # this should not happen
  1061. if not tgtdev:
  1062. sysvals.vprint('[%f - %f] %s-%d %s %s %s' % \
  1063. (start, end, proc, pid, kprobename, cdata, rdata))
  1064. return False
  1065. # place the call data inside the src element of the tgtdev
  1066. if('src' not in tgtdev):
  1067. tgtdev['src'] = []
  1068. dtf = sysvals.dev_tracefuncs
  1069. ubiquitous = False
  1070. if kprobename in dtf and 'ub' in dtf[kprobename]:
  1071. ubiquitous = True
  1072. title = cdata+' '+rdata
  1073. mstr = '\(.*\) *(?P<args>.*) *\((?P<caller>.*)\+.* arg1=(?P<ret>.*)'
  1074. m = re.match(mstr, title)
  1075. if m:
  1076. c = m.group('caller')
  1077. a = m.group('args').strip()
  1078. r = m.group('ret')
  1079. if len(r) > 6:
  1080. r = ''
  1081. else:
  1082. r = 'ret=%s ' % r
  1083. if ubiquitous and c in dtf and 'ub' in dtf[c]:
  1084. return False
  1085. color = sysvals.kprobeColor(kprobename)
  1086. e = DevFunction(displayname, a, c, r, start, end, ubiquitous, proc, pid, color)
  1087. tgtdev['src'].append(e)
  1088. return True
  1089. def overflowDevices(self):
  1090. # get a list of devices that extend beyond the end of this test run
  1091. devlist = []
  1092. for phase in self.sortedPhases():
  1093. list = self.dmesg[phase]['list']
  1094. for devname in list:
  1095. dev = list[devname]
  1096. if dev['end'] > self.end:
  1097. devlist.append(dev)
  1098. return devlist
  1099. def mergeOverlapDevices(self, devlist):
  1100. # merge any devices that overlap devlist
  1101. for dev in devlist:
  1102. devname = dev['name']
  1103. for phase in self.sortedPhases():
  1104. list = self.dmesg[phase]['list']
  1105. if devname not in list:
  1106. continue
  1107. tdev = list[devname]
  1108. o = min(dev['end'], tdev['end']) - max(dev['start'], tdev['start'])
  1109. if o <= 0:
  1110. continue
  1111. dev['end'] = tdev['end']
  1112. if 'src' not in dev or 'src' not in tdev:
  1113. continue
  1114. dev['src'] += tdev['src']
  1115. del list[devname]
  1116. def usurpTouchingThread(self, name, dev):
  1117. # the caller test has priority of this thread, give it to him
  1118. for phase in self.sortedPhases():
  1119. list = self.dmesg[phase]['list']
  1120. if name in list:
  1121. tdev = list[name]
  1122. if tdev['start'] - dev['end'] < 0.1:
  1123. dev['end'] = tdev['end']
  1124. if 'src' not in dev:
  1125. dev['src'] = []
  1126. if 'src' in tdev:
  1127. dev['src'] += tdev['src']
  1128. del list[name]
  1129. break
  1130. def stitchTouchingThreads(self, testlist):
  1131. # merge any threads between tests that touch
  1132. for phase in self.sortedPhases():
  1133. list = self.dmesg[phase]['list']
  1134. for devname in list:
  1135. dev = list[devname]
  1136. if 'htmlclass' not in dev or 'kth' not in dev['htmlclass']:
  1137. continue
  1138. for data in testlist:
  1139. data.usurpTouchingThread(devname, dev)
  1140. def optimizeDevSrc(self):
  1141. # merge any src call loops to reduce timeline size
  1142. for phase in self.sortedPhases():
  1143. list = self.dmesg[phase]['list']
  1144. for dev in list:
  1145. if 'src' not in list[dev]:
  1146. continue
  1147. src = list[dev]['src']
  1148. p = 0
  1149. for e in sorted(src, key=lambda event: event.time):
  1150. if not p or not e.repeat(p):
  1151. p = e
  1152. continue
  1153. # e is another iteration of p, move it into p
  1154. p.end = e.end
  1155. p.length = p.end - p.time
  1156. p.count += 1
  1157. src.remove(e)
  1158. def trimTimeVal(self, t, t0, dT, left):
  1159. if left:
  1160. if(t > t0):
  1161. if(t - dT < t0):
  1162. return t0
  1163. return t - dT
  1164. else:
  1165. return t
  1166. else:
  1167. if(t < t0 + dT):
  1168. if(t > t0):
  1169. return t0 + dT
  1170. return t + dT
  1171. else:
  1172. return t
  1173. def trimTime(self, t0, dT, left):
  1174. self.tSuspended = self.trimTimeVal(self.tSuspended, t0, dT, left)
  1175. self.tResumed = self.trimTimeVal(self.tResumed, t0, dT, left)
  1176. self.start = self.trimTimeVal(self.start, t0, dT, left)
  1177. self.tKernSus = self.trimTimeVal(self.tKernSus, t0, dT, left)
  1178. self.tKernRes = self.trimTimeVal(self.tKernRes, t0, dT, left)
  1179. self.end = self.trimTimeVal(self.end, t0, dT, left)
  1180. for phase in self.sortedPhases():
  1181. p = self.dmesg[phase]
  1182. p['start'] = self.trimTimeVal(p['start'], t0, dT, left)
  1183. p['end'] = self.trimTimeVal(p['end'], t0, dT, left)
  1184. list = p['list']
  1185. for name in list:
  1186. d = list[name]
  1187. d['start'] = self.trimTimeVal(d['start'], t0, dT, left)
  1188. d['end'] = self.trimTimeVal(d['end'], t0, dT, left)
  1189. d['length'] = d['end'] - d['start']
  1190. if('ftrace' in d):
  1191. cg = d['ftrace']
  1192. cg.start = self.trimTimeVal(cg.start, t0, dT, left)
  1193. cg.end = self.trimTimeVal(cg.end, t0, dT, left)
  1194. for line in cg.list:
  1195. line.time = self.trimTimeVal(line.time, t0, dT, left)
  1196. if('src' in d):
  1197. for e in d['src']:
  1198. e.time = self.trimTimeVal(e.time, t0, dT, left)
  1199. for dir in ['suspend', 'resume']:
  1200. list = []
  1201. for e in self.errorinfo[dir]:
  1202. type, tm, idx1, idx2 = e
  1203. tm = self.trimTimeVal(tm, t0, dT, left)
  1204. list.append((type, tm, idx1, idx2))
  1205. self.errorinfo[dir] = list
  1206. def trimFreezeTime(self, tZero):
  1207. # trim out any standby or freeze clock time
  1208. lp = ''
  1209. for phase in self.sortedPhases():
  1210. if 'resume_machine' in phase and 'suspend_machine' in lp:
  1211. tS, tR = self.dmesg[lp]['end'], self.dmesg[phase]['start']
  1212. tL = tR - tS
  1213. if tL > 0:
  1214. left = True if tR > tZero else False
  1215. self.trimTime(tS, tL, left)
  1216. self.tLow.append('%.0f'%(tL*1000))
  1217. lp = phase
  1218. def getTimeValues(self):
  1219. sktime = (self.tSuspended - self.tKernSus) * 1000
  1220. rktime = (self.tKernRes - self.tResumed) * 1000
  1221. return (sktime, rktime)
  1222. def setPhase(self, phase, ktime, isbegin, order=-1):
  1223. if(isbegin):
  1224. # phase start over current phase
  1225. if self.currphase:
  1226. if 'resume_machine' not in self.currphase:
  1227. sysvals.vprint('WARNING: phase %s failed to end' % self.currphase)
  1228. self.dmesg[self.currphase]['end'] = ktime
  1229. phases = self.dmesg.keys()
  1230. color = self.phasedef[phase]['color']
  1231. count = len(phases) if order < 0 else order
  1232. # create unique name for every new phase
  1233. while phase in phases:
  1234. phase += '*'
  1235. self.dmesg[phase] = {'list': dict(), 'start': -1.0, 'end': -1.0,
  1236. 'row': 0, 'color': color, 'order': count}
  1237. self.dmesg[phase]['start'] = ktime
  1238. self.currphase = phase
  1239. else:
  1240. # phase end without a start
  1241. if phase not in self.currphase:
  1242. if self.currphase:
  1243. sysvals.vprint('WARNING: %s ended instead of %s, ftrace corruption?' % (phase, self.currphase))
  1244. else:
  1245. sysvals.vprint('WARNING: %s ended without a start, ftrace corruption?' % phase)
  1246. return phase
  1247. phase = self.currphase
  1248. self.dmesg[phase]['end'] = ktime
  1249. self.currphase = ''
  1250. return phase
  1251. def sortedDevices(self, phase):
  1252. list = self.dmesg[phase]['list']
  1253. slist = []
  1254. tmp = dict()
  1255. for devname in list:
  1256. dev = list[devname]
  1257. if dev['length'] == 0:
  1258. continue
  1259. tmp[dev['start']] = devname
  1260. for t in sorted(tmp):
  1261. slist.append(tmp[t])
  1262. return slist
  1263. def fixupInitcalls(self, phase):
  1264. # if any calls never returned, clip them at system resume end
  1265. phaselist = self.dmesg[phase]['list']
  1266. for devname in phaselist:
  1267. dev = phaselist[devname]
  1268. if(dev['end'] < 0):
  1269. for p in self.sortedPhases():
  1270. if self.dmesg[p]['end'] > dev['start']:
  1271. dev['end'] = self.dmesg[p]['end']
  1272. break
  1273. sysvals.vprint('%s (%s): callback didnt return' % (devname, phase))
  1274. def deviceFilter(self, devicefilter):
  1275. for phase in self.sortedPhases():
  1276. list = self.dmesg[phase]['list']
  1277. rmlist = []
  1278. for name in list:
  1279. keep = False
  1280. for filter in devicefilter:
  1281. if filter in name or \
  1282. ('drv' in list[name] and filter in list[name]['drv']):
  1283. keep = True
  1284. if not keep:
  1285. rmlist.append(name)
  1286. for name in rmlist:
  1287. del list[name]
  1288. def fixupInitcallsThatDidntReturn(self):
  1289. # if any calls never returned, clip them at system resume end
  1290. for phase in self.sortedPhases():
  1291. self.fixupInitcalls(phase)
  1292. def phaseOverlap(self, phases):
  1293. rmgroups = []
  1294. newgroup = []
  1295. for group in self.devicegroups:
  1296. for phase in phases:
  1297. if phase not in group:
  1298. continue
  1299. for p in group:
  1300. if p not in newgroup:
  1301. newgroup.append(p)
  1302. if group not in rmgroups:
  1303. rmgroups.append(group)
  1304. for group in rmgroups:
  1305. self.devicegroups.remove(group)
  1306. self.devicegroups.append(newgroup)
  1307. def newActionGlobal(self, name, start, end, pid=-1, color=''):
  1308. # which phase is this device callback or action in
  1309. phases = self.sortedPhases()
  1310. targetphase = 'none'
  1311. htmlclass = ''
  1312. overlap = 0.0
  1313. myphases = []
  1314. for phase in phases:
  1315. pstart = self.dmesg[phase]['start']
  1316. pend = self.dmesg[phase]['end']
  1317. # see if the action overlaps this phase
  1318. o = max(0, min(end, pend) - max(start, pstart))
  1319. if o > 0:
  1320. myphases.append(phase)
  1321. # set the target phase to the one that overlaps most
  1322. if o > overlap:
  1323. if overlap > 0 and phase == 'post_resume':
  1324. continue
  1325. targetphase = phase
  1326. overlap = o
  1327. # if no target phase was found, pin it to the edge
  1328. if targetphase == 'none':
  1329. p0start = self.dmesg[phases[0]]['start']
  1330. if start <= p0start:
  1331. targetphase = phases[0]
  1332. else:
  1333. targetphase = phases[-1]
  1334. if pid == -2:
  1335. htmlclass = ' bg'
  1336. elif pid == -3:
  1337. htmlclass = ' ps'
  1338. if len(myphases) > 1:
  1339. htmlclass = ' bg'
  1340. self.phaseOverlap(myphases)
  1341. if targetphase in phases:
  1342. newname = self.newAction(targetphase, name, pid, '', start, end, '', htmlclass, color)
  1343. return (targetphase, newname)
  1344. return False
  1345. def newAction(self, phase, name, pid, parent, start, end, drv, htmlclass='', color=''):
  1346. # new device callback for a specific phase
  1347. self.html_device_id += 1
  1348. devid = '%s%d' % (self.idstr, self.html_device_id)
  1349. list = self.dmesg[phase]['list']
  1350. length = -1.0
  1351. if(start >= 0 and end >= 0):
  1352. length = end - start
  1353. if pid == -2:
  1354. i = 2
  1355. origname = name
  1356. while(name in list):
  1357. name = '%s[%d]' % (origname, i)
  1358. i += 1
  1359. list[name] = {'name': name, 'start': start, 'end': end, 'pid': pid,
  1360. 'par': parent, 'length': length, 'row': 0, 'id': devid, 'drv': drv }
  1361. if htmlclass:
  1362. list[name]['htmlclass'] = htmlclass
  1363. if color:
  1364. list[name]['color'] = color
  1365. return name
  1366. def deviceChildren(self, devname, phase):
  1367. devlist = []
  1368. list = self.dmesg[phase]['list']
  1369. for child in list:
  1370. if(list[child]['par'] == devname):
  1371. devlist.append(child)
  1372. return devlist
  1373. def maxDeviceNameSize(self, phase):
  1374. size = 0
  1375. for name in self.dmesg[phase]['list']:
  1376. if len(name) > size:
  1377. size = len(name)
  1378. return size
  1379. def printDetails(self):
  1380. sysvals.vprint('Timeline Details:')
  1381. sysvals.vprint(' test start: %f' % self.start)
  1382. sysvals.vprint('kernel suspend start: %f' % self.tKernSus)
  1383. tS = tR = False
  1384. for phase in self.sortedPhases():
  1385. devlist = self.dmesg[phase]['list']
  1386. dc, ps, pe = len(devlist), self.dmesg[phase]['start'], self.dmesg[phase]['end']
  1387. if not tS and ps >= self.tSuspended:
  1388. sysvals.vprint(' machine suspended: %f' % self.tSuspended)
  1389. tS = True
  1390. if not tR and ps >= self.tResumed:
  1391. sysvals.vprint(' machine resumed: %f' % self.tResumed)
  1392. tR = True
  1393. sysvals.vprint('%20s: %f - %f (%d devices)' % (phase, ps, pe, dc))
  1394. if sysvals.devdump:
  1395. sysvals.vprint(''.join('-' for i in range(80)))
  1396. maxname = '%d' % self.maxDeviceNameSize(phase)
  1397. fmt = '%3d) %'+maxname+'s - %f - %f'
  1398. c = 1
  1399. for name in devlist:
  1400. s = devlist[name]['start']
  1401. e = devlist[name]['end']
  1402. sysvals.vprint(fmt % (c, name, s, e))
  1403. c += 1
  1404. sysvals.vprint(''.join('-' for i in range(80)))
  1405. sysvals.vprint(' kernel resume end: %f' % self.tKernRes)
  1406. sysvals.vprint(' test end: %f' % self.end)
  1407. def deviceChildrenAllPhases(self, devname):
  1408. devlist = []
  1409. for phase in self.sortedPhases():
  1410. list = self.deviceChildren(devname, phase)
  1411. for dev in list:
  1412. if dev not in devlist:
  1413. devlist.append(dev)
  1414. return devlist
  1415. def masterTopology(self, name, list, depth):
  1416. node = DeviceNode(name, depth)
  1417. for cname in list:
  1418. # avoid recursions
  1419. if name == cname:
  1420. continue
  1421. clist = self.deviceChildrenAllPhases(cname)
  1422. cnode = self.masterTopology(cname, clist, depth+1)
  1423. node.children.append(cnode)
  1424. return node
  1425. def printTopology(self, node):
  1426. html = ''
  1427. if node.name:
  1428. info = ''
  1429. drv = ''
  1430. for phase in self.sortedPhases():
  1431. list = self.dmesg[phase]['list']
  1432. if node.name in list:
  1433. s = list[node.name]['start']
  1434. e = list[node.name]['end']
  1435. if list[node.name]['drv']:
  1436. drv = ' {'+list[node.name]['drv']+'}'
  1437. info += ('<li>%s: %.3fms</li>' % (phase, (e-s)*1000))
  1438. html += '<li><b>'+node.name+drv+'</b>'
  1439. if info:
  1440. html += '<ul>'+info+'</ul>'
  1441. html += '</li>'
  1442. if len(node.children) > 0:
  1443. html += '<ul>'
  1444. for cnode in node.children:
  1445. html += self.printTopology(cnode)
  1446. html += '</ul>'
  1447. return html
  1448. def rootDeviceList(self):
  1449. # list of devices graphed
  1450. real = []
  1451. for phase in self.dmesg:
  1452. list = self.dmesg[phase]['list']
  1453. for dev in list:
  1454. if list[dev]['pid'] >= 0 and dev not in real:
  1455. real.append(dev)
  1456. # list of top-most root devices
  1457. rootlist = []
  1458. for phase in self.dmesg:
  1459. list = self.dmesg[phase]['list']
  1460. for dev in list:
  1461. pdev = list[dev]['par']
  1462. pid = list[dev]['pid']
  1463. if(pid < 0 or re.match('[0-9]*-[0-9]*\.[0-9]*[\.0-9]*\:[\.0-9]*$', pdev)):
  1464. continue
  1465. if pdev and pdev not in real and pdev not in rootlist:
  1466. rootlist.append(pdev)
  1467. return rootlist
  1468. def deviceTopology(self):
  1469. rootlist = self.rootDeviceList()
  1470. master = self.masterTopology('', rootlist, 0)
  1471. return self.printTopology(master)
  1472. def selectTimelineDevices(self, widfmt, tTotal, mindevlen):
  1473. # only select devices that will actually show up in html
  1474. self.tdevlist = dict()
  1475. for phase in self.dmesg:
  1476. devlist = []
  1477. list = self.dmesg[phase]['list']
  1478. for dev in list:
  1479. length = (list[dev]['end'] - list[dev]['start']) * 1000
  1480. width = widfmt % (((list[dev]['end']-list[dev]['start'])*100)/tTotal)
  1481. if width != '0.000000' and length >= mindevlen:
  1482. devlist.append(dev)
  1483. self.tdevlist[phase] = devlist
  1484. def addHorizontalDivider(self, devname, devend):
  1485. phase = 'suspend_prepare'
  1486. self.newAction(phase, devname, -2, '', \
  1487. self.start, devend, '', ' sec', '')
  1488. if phase not in self.tdevlist:
  1489. self.tdevlist[phase] = []
  1490. self.tdevlist[phase].append(devname)
  1491. d = DevItem(0, phase, self.dmesg[phase]['list'][devname])
  1492. return d
  1493. def addProcessUsageEvent(self, name, times):
  1494. # get the start and end times for this process
  1495. maxC = 0
  1496. tlast = 0
  1497. start = -1
  1498. end = -1
  1499. for t in sorted(times):
  1500. if tlast == 0:
  1501. tlast = t
  1502. continue
  1503. if name in self.pstl[t]:
  1504. if start == -1 or tlast < start:
  1505. start = tlast
  1506. if end == -1 or t > end:
  1507. end = t
  1508. tlast = t
  1509. if start == -1 or end == -1:
  1510. return 0
  1511. # add a new action for this process and get the object
  1512. out = self.newActionGlobal(name, start, end, -3)
  1513. if not out:
  1514. return 0
  1515. phase, devname = out
  1516. dev = self.dmesg[phase]['list'][devname]
  1517. # get the cpu exec data
  1518. tlast = 0
  1519. clast = 0
  1520. cpuexec = dict()
  1521. for t in sorted(times):
  1522. if tlast == 0 or t <= start or t > end:
  1523. tlast = t
  1524. continue
  1525. list = self.pstl[t]
  1526. c = 0
  1527. if name in list:
  1528. c = list[name]
  1529. if c > maxC:
  1530. maxC = c
  1531. if c != clast:
  1532. key = (tlast, t)
  1533. cpuexec[key] = c
  1534. tlast = t
  1535. clast = c
  1536. dev['cpuexec'] = cpuexec
  1537. return maxC
  1538. def createProcessUsageEvents(self):
  1539. # get an array of process names
  1540. proclist = []
  1541. for t in self.pstl:
  1542. pslist = self.pstl[t]
  1543. for ps in pslist:
  1544. if ps not in proclist:
  1545. proclist.append(ps)
  1546. # get a list of data points for suspend and resume
  1547. tsus = []
  1548. tres = []
  1549. for t in sorted(self.pstl):
  1550. if t < self.tSuspended:
  1551. tsus.append(t)
  1552. else:
  1553. tres.append(t)
  1554. # process the events for suspend and resume
  1555. if len(proclist) > 0:
  1556. sysvals.vprint('Process Execution:')
  1557. for ps in proclist:
  1558. c = self.addProcessUsageEvent(ps, tsus)
  1559. if c > 0:
  1560. sysvals.vprint('%25s (sus): %d' % (ps, c))
  1561. c = self.addProcessUsageEvent(ps, tres)
  1562. if c > 0:
  1563. sysvals.vprint('%25s (res): %d' % (ps, c))
  1564. def handleEndMarker(self, time):
  1565. dm = self.dmesg
  1566. self.setEnd(time)
  1567. self.initDevicegroups()
  1568. # give suspend_prepare an end if needed
  1569. if 'suspend_prepare' in dm and dm['suspend_prepare']['end'] < 0:
  1570. dm['suspend_prepare']['end'] = time
  1571. # assume resume machine ends at next phase start
  1572. if 'resume_machine' in dm and dm['resume_machine']['end'] < 0:
  1573. np = self.nextPhase('resume_machine', 1)
  1574. if np:
  1575. dm['resume_machine']['end'] = dm[np]['start']
  1576. # if kernel resume end not found, assume its the end marker
  1577. if self.tKernRes == 0.0:
  1578. self.tKernRes = time
  1579. # if kernel suspend start not found, assume its the end marker
  1580. if self.tKernSus == 0.0:
  1581. self.tKernSus = time
  1582. # set resume complete to end at end marker
  1583. if 'resume_complete' in dm:
  1584. dm['resume_complete']['end'] = time
  1585. def debugPrint(self):
  1586. for p in self.sortedPhases():
  1587. list = self.dmesg[p]['list']
  1588. for devname in list:
  1589. dev = list[devname]
  1590. if 'ftrace' in dev:
  1591. dev['ftrace'].debugPrint(' [%s]' % devname)
  1592. # Class: DevFunction
  1593. # Description:
  1594. # A container for kprobe function data we want in the dev timeline
  1595. class DevFunction:
  1596. def __init__(self, name, args, caller, ret, start, end, u, proc, pid, color):
  1597. self.row = 0
  1598. self.count = 1
  1599. self.name = name
  1600. self.args = args
  1601. self.caller = caller
  1602. self.ret = ret
  1603. self.time = start
  1604. self.length = end - start
  1605. self.end = end
  1606. self.ubiquitous = u
  1607. self.proc = proc
  1608. self.pid = pid
  1609. self.color = color
  1610. def title(self):
  1611. cnt = ''
  1612. if self.count > 1:
  1613. cnt = '(x%d)' % self.count
  1614. l = '%0.3fms' % (self.length * 1000)
  1615. if self.ubiquitous:
  1616. title = '%s(%s)%s <- %s, %s(%s)' % \
  1617. (self.name, self.args, cnt, self.caller, self.ret, l)
  1618. else:
  1619. title = '%s(%s) %s%s(%s)' % (self.name, self.args, self.ret, cnt, l)
  1620. return title.replace('"', '')
  1621. def text(self):
  1622. if self.count > 1:
  1623. text = '%s(x%d)' % (self.name, self.count)
  1624. else:
  1625. text = self.name
  1626. return text
  1627. def repeat(self, tgt):
  1628. # is the tgt call just a repeat of this call (e.g. are we in a loop)
  1629. dt = self.time - tgt.end
  1630. # only combine calls if -all- attributes are identical
  1631. if tgt.caller == self.caller and \
  1632. tgt.name == self.name and tgt.args == self.args and \
  1633. tgt.proc == self.proc and tgt.pid == self.pid and \
  1634. tgt.ret == self.ret and dt >= 0 and \
  1635. dt <= sysvals.callloopmaxgap and \
  1636. self.length < sysvals.callloopmaxlen:
  1637. return True
  1638. return False
  1639. # Class: FTraceLine
  1640. # Description:
  1641. # A container for a single line of ftrace data. There are six basic types:
  1642. # callgraph line:
  1643. # call: " dpm_run_callback() {"
  1644. # return: " }"
  1645. # leaf: " dpm_run_callback();"
  1646. # trace event:
  1647. # tracing_mark_write: SUSPEND START or RESUME COMPLETE
  1648. # suspend_resume: phase or custom exec block data
  1649. # device_pm_callback: device callback info
  1650. class FTraceLine:
  1651. def __init__(self, t, m='', d=''):
  1652. self.length = 0.0
  1653. self.fcall = False
  1654. self.freturn = False
  1655. self.fevent = False
  1656. self.fkprobe = False
  1657. self.depth = 0
  1658. self.name = ''
  1659. self.type = ''
  1660. self.time = float(t)
  1661. if not m and not d:
  1662. return
  1663. # is this a trace event
  1664. if(d == 'traceevent' or re.match('^ *\/\* *(?P<msg>.*) \*\/ *$', m)):
  1665. if(d == 'traceevent'):
  1666. # nop format trace event
  1667. msg = m
  1668. else:
  1669. # function_graph format trace event
  1670. em = re.match('^ *\/\* *(?P<msg>.*) \*\/ *$', m)
  1671. msg = em.group('msg')
  1672. emm = re.match('^(?P<call>.*?): (?P<msg>.*)', msg)
  1673. if(emm):
  1674. self.name = emm.group('msg')
  1675. self.type = emm.group('call')
  1676. else:
  1677. self.name = msg
  1678. km = re.match('^(?P<n>.*)_cal$', self.type)
  1679. if km:
  1680. self.fcall = True
  1681. self.fkprobe = True
  1682. self.type = km.group('n')
  1683. return
  1684. km = re.match('^(?P<n>.*)_ret$', self.type)
  1685. if km:
  1686. self.freturn = True
  1687. self.fkprobe = True
  1688. self.type = km.group('n')
  1689. return
  1690. self.fevent = True
  1691. return
  1692. # convert the duration to seconds
  1693. if(d):
  1694. self.length = float(d)/1000000
  1695. # the indentation determines the depth
  1696. match = re.match('^(?P<d> *)(?P<o>.*)$', m)
  1697. if(not match):
  1698. return
  1699. self.depth = self.getDepth(match.group('d'))
  1700. m = match.group('o')
  1701. # function return
  1702. if(m[0] == '}'):
  1703. self.freturn = True
  1704. if(len(m) > 1):
  1705. # includes comment with function name
  1706. match = re.match('^} *\/\* *(?P<n>.*) *\*\/$', m)
  1707. if(match):
  1708. self.name = match.group('n').strip()
  1709. # function call
  1710. else:
  1711. self.fcall = True
  1712. # function call with children
  1713. if(m[-1] == '{'):
  1714. match = re.match('^(?P<n>.*) *\(.*', m)
  1715. if(match):
  1716. self.name = match.group('n').strip()
  1717. # function call with no children (leaf)
  1718. elif(m[-1] == ';'):
  1719. self.freturn = True
  1720. match = re.match('^(?P<n>.*) *\(.*', m)
  1721. if(match):
  1722. self.name = match.group('n').strip()
  1723. # something else (possibly a trace marker)
  1724. else:
  1725. self.name = m
  1726. def isCall(self):
  1727. return self.fcall and not self.freturn
  1728. def isReturn(self):
  1729. return self.freturn and not self.fcall
  1730. def isLeaf(self):
  1731. return self.fcall and self.freturn
  1732. def getDepth(self, str):
  1733. return len(str)/2
  1734. def debugPrint(self, info=''):
  1735. if self.isLeaf():
  1736. pprint(' -- %12.6f (depth=%02d): %s(); (%.3f us) %s' % (self.time, \
  1737. self.depth, self.name, self.length*1000000, info))
  1738. elif self.freturn:
  1739. pprint(' -- %12.6f (depth=%02d): %s} (%.3f us) %s' % (self.time, \
  1740. self.depth, self.name, self.length*1000000, info))
  1741. else:
  1742. pprint(' -- %12.6f (depth=%02d): %s() { (%.3f us) %s' % (self.time, \
  1743. self.depth, self.name, self.length*1000000, info))
  1744. def startMarker(self):
  1745. # Is this the starting line of a suspend?
  1746. if not self.fevent:
  1747. return False
  1748. if sysvals.usetracemarkers:
  1749. if(self.name == 'SUSPEND START'):
  1750. return True
  1751. return False
  1752. else:
  1753. if(self.type == 'suspend_resume' and
  1754. re.match('suspend_enter\[.*\] begin', self.name)):
  1755. return True
  1756. return False
  1757. def endMarker(self):
  1758. # Is this the ending line of a resume?
  1759. if not self.fevent:
  1760. return False
  1761. if sysvals.usetracemarkers:
  1762. if(self.name == 'RESUME COMPLETE'):
  1763. return True
  1764. return False
  1765. else:
  1766. if(self.type == 'suspend_resume' and
  1767. re.match('thaw_processes\[.*\] end', self.name)):
  1768. return True
  1769. return False
  1770. # Class: FTraceCallGraph
  1771. # Description:
  1772. # A container for the ftrace callgraph of a single recursive function.
  1773. # This can be a dpm_run_callback, dpm_prepare, or dpm_complete callgraph
  1774. # Each instance is tied to a single device in a single phase, and is
  1775. # comprised of an ordered list of FTraceLine objects
  1776. class FTraceCallGraph:
  1777. vfname = 'missing_function_name'
  1778. def __init__(self, pid, sv):
  1779. self.id = ''
  1780. self.invalid = False
  1781. self.name = ''
  1782. self.partial = False
  1783. self.ignore = False
  1784. self.start = -1.0
  1785. self.end = -1.0
  1786. self.list = []
  1787. self.depth = 0
  1788. self.pid = pid
  1789. self.sv = sv
  1790. def addLine(self, line):
  1791. # if this is already invalid, just leave
  1792. if(self.invalid):
  1793. if(line.depth == 0 and line.freturn):
  1794. return 1
  1795. return 0
  1796. # invalidate on bad depth
  1797. if(self.depth < 0):
  1798. self.invalidate(line)
  1799. return 0
  1800. # ignore data til we return to the current depth
  1801. if self.ignore:
  1802. if line.depth > self.depth:
  1803. return 0
  1804. else:
  1805. self.list[-1].freturn = True
  1806. self.list[-1].length = line.time - self.list[-1].time
  1807. self.ignore = False
  1808. # if this is a return at self.depth, no more work is needed
  1809. if line.depth == self.depth and line.isReturn():
  1810. if line.depth == 0:
  1811. self.end = line.time
  1812. return 1
  1813. return 0
  1814. # compare current depth with this lines pre-call depth
  1815. prelinedep = line.depth
  1816. if line.isReturn():
  1817. prelinedep += 1
  1818. last = 0
  1819. lasttime = line.time
  1820. if len(self.list) > 0:
  1821. last = self.list[-1]
  1822. lasttime = last.time
  1823. if last.isLeaf():
  1824. lasttime += last.length
  1825. # handle low misalignments by inserting returns
  1826. mismatch = prelinedep - self.depth
  1827. warning = self.sv.verbose and abs(mismatch) > 1
  1828. info = []
  1829. if mismatch < 0:
  1830. idx = 0
  1831. # add return calls to get the depth down
  1832. while prelinedep < self.depth:
  1833. self.depth -= 1
  1834. if idx == 0 and last and last.isCall():
  1835. # special case, turn last call into a leaf
  1836. last.depth = self.depth
  1837. last.freturn = True
  1838. last.length = line.time - last.time
  1839. if warning:
  1840. info.append(('[make leaf]', last))
  1841. else:
  1842. vline = FTraceLine(lasttime)
  1843. vline.depth = self.depth
  1844. vline.name = self.vfname
  1845. vline.freturn = True
  1846. self.list.append(vline)
  1847. if warning:
  1848. if idx == 0:
  1849. info.append(('', last))
  1850. info.append(('[add return]', vline))
  1851. idx += 1
  1852. if warning:
  1853. info.append(('', line))
  1854. # handle high misalignments by inserting calls
  1855. elif mismatch > 0:
  1856. idx = 0
  1857. if warning:
  1858. info.append(('', last))
  1859. # add calls to get the depth up
  1860. while prelinedep > self.depth:
  1861. if idx == 0 and line.isReturn():
  1862. # special case, turn this return into a leaf
  1863. line.fcall = True
  1864. prelinedep -= 1
  1865. if warning:
  1866. info.append(('[make leaf]', line))
  1867. else:
  1868. vline = FTraceLine(lasttime)
  1869. vline.depth = self.depth
  1870. vline.name = self.vfname
  1871. vline.fcall = True
  1872. self.list.append(vline)
  1873. self.depth += 1
  1874. if not last:
  1875. self.start = vline.time
  1876. if warning:
  1877. info.append(('[add call]', vline))
  1878. idx += 1
  1879. if warning and ('[make leaf]', line) not in info:
  1880. info.append(('', line))
  1881. if warning:
  1882. pprint('WARNING: ftrace data missing, corrections made:')
  1883. for i in info:
  1884. t, obj = i
  1885. if obj:
  1886. obj.debugPrint(t)
  1887. # process the call and set the new depth
  1888. skipadd = False
  1889. md = self.sv.max_graph_depth
  1890. if line.isCall():
  1891. # ignore blacklisted/overdepth funcs
  1892. if (md and self.depth >= md - 1) or (line.name in self.sv.cgblacklist):
  1893. self.ignore = True
  1894. else:
  1895. self.depth += 1
  1896. elif line.isReturn():
  1897. self.depth -= 1
  1898. # remove blacklisted/overdepth/empty funcs that slipped through
  1899. if (last and last.isCall() and last.depth == line.depth) or \
  1900. (md and last and last.depth >= md) or \
  1901. (line.name in self.sv.cgblacklist):
  1902. while len(self.list) > 0 and self.list[-1].depth > line.depth:
  1903. self.list.pop(-1)
  1904. if len(self.list) == 0:
  1905. self.invalid = True
  1906. return 1
  1907. self.list[-1].freturn = True
  1908. self.list[-1].length = line.time - self.list[-1].time
  1909. self.list[-1].name = line.name
  1910. skipadd = True
  1911. if len(self.list) < 1:
  1912. self.start = line.time
  1913. # check for a mismatch that returned all the way to callgraph end
  1914. res = 1
  1915. if mismatch < 0 and self.list[-1].depth == 0 and self.list[-1].freturn:
  1916. line = self.list[-1]
  1917. skipadd = True
  1918. res = -1
  1919. if not skipadd:
  1920. self.list.append(line)
  1921. if(line.depth == 0 and line.freturn):
  1922. if(self.start < 0):
  1923. self.start = line.time
  1924. self.end = line.time
  1925. if line.fcall:
  1926. self.end += line.length
  1927. if self.list[0].name == self.vfname:
  1928. self.invalid = True
  1929. if res == -1:
  1930. self.partial = True
  1931. return res
  1932. return 0
  1933. def invalidate(self, line):
  1934. if(len(self.list) > 0):
  1935. first = self.list[0]
  1936. self.list = []
  1937. self.list.append(first)
  1938. self.invalid = True
  1939. id = 'task %s' % (self.pid)
  1940. window = '(%f - %f)' % (self.start, line.time)
  1941. if(self.depth < 0):
  1942. pprint('Data misalignment for '+id+\
  1943. ' (buffer overflow), ignoring this callback')
  1944. else:
  1945. pprint('Too much data for '+id+\
  1946. ' '+window+', ignoring this callback')
  1947. def slice(self, dev):
  1948. minicg = FTraceCallGraph(dev['pid'], self.sv)
  1949. minicg.name = self.name
  1950. mydepth = -1
  1951. good = False
  1952. for l in self.list:
  1953. if(l.time < dev['start'] or l.time > dev['end']):
  1954. continue
  1955. if mydepth < 0:
  1956. if l.name == 'mutex_lock' and l.freturn:
  1957. mydepth = l.depth
  1958. continue
  1959. elif l.depth == mydepth and l.name == 'mutex_unlock' and l.fcall:
  1960. good = True
  1961. break
  1962. l.depth -= mydepth
  1963. minicg.addLine(l)
  1964. if not good or len(minicg.list) < 1:
  1965. return 0
  1966. return minicg
  1967. def repair(self, enddepth):
  1968. # bring the depth back to 0 with additional returns
  1969. fixed = False
  1970. last = self.list[-1]
  1971. for i in reversed(range(enddepth)):
  1972. t = FTraceLine(last.time)
  1973. t.depth = i
  1974. t.freturn = True
  1975. fixed = self.addLine(t)
  1976. if fixed != 0:
  1977. self.end = last.time
  1978. return True
  1979. return False
  1980. def postProcess(self):
  1981. if len(self.list) > 0:
  1982. self.name = self.list[0].name
  1983. stack = dict()
  1984. cnt = 0
  1985. last = 0
  1986. for l in self.list:
  1987. # ftrace bug: reported duration is not reliable
  1988. # check each leaf and clip it at max possible length
  1989. if last and last.isLeaf():
  1990. if last.length > l.time - last.time:
  1991. last.length = l.time - last.time
  1992. if l.isCall():
  1993. stack[l.depth] = l
  1994. cnt += 1
  1995. elif l.isReturn():
  1996. if(l.depth not in stack):
  1997. if self.sv.verbose:
  1998. pprint('Post Process Error: Depth missing')
  1999. l.debugPrint()
  2000. return False
  2001. # calculate call length from call/return lines
  2002. cl = stack[l.depth]
  2003. cl.length = l.time - cl.time
  2004. if cl.name == self.vfname:
  2005. cl.name = l.name
  2006. stack.pop(l.depth)
  2007. l.length = 0
  2008. cnt -= 1
  2009. last = l
  2010. if(cnt == 0):
  2011. # trace caught the whole call tree
  2012. return True
  2013. elif(cnt < 0):
  2014. if self.sv.verbose:
  2015. pprint('Post Process Error: Depth is less than 0')
  2016. return False
  2017. # trace ended before call tree finished
  2018. return self.repair(cnt)
  2019. def deviceMatch(self, pid, data):
  2020. found = ''
  2021. # add the callgraph data to the device hierarchy
  2022. borderphase = {
  2023. 'dpm_prepare': 'suspend_prepare',
  2024. 'dpm_complete': 'resume_complete'
  2025. }
  2026. if(self.name in borderphase):
  2027. p = borderphase[self.name]
  2028. list = data.dmesg[p]['list']
  2029. for devname in list:
  2030. dev = list[devname]
  2031. if(pid == dev['pid'] and
  2032. self.start <= dev['start'] and
  2033. self.end >= dev['end']):
  2034. cg = self.slice(dev)
  2035. if cg:
  2036. dev['ftrace'] = cg
  2037. found = devname
  2038. return found
  2039. for p in data.sortedPhases():
  2040. if(data.dmesg[p]['start'] <= self.start and
  2041. self.start <= data.dmesg[p]['end']):
  2042. list = data.dmesg[p]['list']
  2043. for devname in list:
  2044. dev = list[devname]
  2045. if(pid == dev['pid'] and
  2046. self.start <= dev['start'] and
  2047. self.end >= dev['end']):
  2048. dev['ftrace'] = self
  2049. found = devname
  2050. break
  2051. break
  2052. return found
  2053. def newActionFromFunction(self, data):
  2054. name = self.name
  2055. if name in ['dpm_run_callback', 'dpm_prepare', 'dpm_complete']:
  2056. return
  2057. fs = self.start
  2058. fe = self.end
  2059. if fs < data.start or fe > data.end:
  2060. return
  2061. phase = ''
  2062. for p in data.sortedPhases():
  2063. if(data.dmesg[p]['start'] <= self.start and
  2064. self.start < data.dmesg[p]['end']):
  2065. phase = p
  2066. break
  2067. if not phase:
  2068. return
  2069. out = data.newActionGlobal(name, fs, fe, -2)
  2070. if out:
  2071. phase, myname = out
  2072. data.dmesg[phase]['list'][myname]['ftrace'] = self
  2073. def debugPrint(self, info=''):
  2074. pprint('%s pid=%d [%f - %f] %.3f us' % \
  2075. (self.name, self.pid, self.start, self.end,
  2076. (self.end - self.start)*1000000))
  2077. for l in self.list:
  2078. if l.isLeaf():
  2079. pprint('%f (%02d): %s(); (%.3f us)%s' % (l.time, \
  2080. l.depth, l.name, l.length*1000000, info))
  2081. elif l.freturn:
  2082. pprint('%f (%02d): %s} (%.3f us)%s' % (l.time, \
  2083. l.depth, l.name, l.length*1000000, info))
  2084. else:
  2085. pprint('%f (%02d): %s() { (%.3f us)%s' % (l.time, \
  2086. l.depth, l.name, l.length*1000000, info))
  2087. pprint(' ')
  2088. class DevItem:
  2089. def __init__(self, test, phase, dev):
  2090. self.test = test
  2091. self.phase = phase
  2092. self.dev = dev
  2093. def isa(self, cls):
  2094. if 'htmlclass' in self.dev and cls in self.dev['htmlclass']:
  2095. return True
  2096. return False
  2097. # Class: Timeline
  2098. # Description:
  2099. # A container for a device timeline which calculates
  2100. # all the html properties to display it correctly
  2101. class Timeline:
  2102. html_tblock = '<div id="block{0}" class="tblock" style="left:{1}%;width:{2}%;"><div class="tback" style="height:{3}px"></div>\n'
  2103. html_device = '<div id="{0}" title="{1}" class="thread{7}" style="left:{2}%;top:{3}px;height:{4}px;width:{5}%;{8}">{6}</div>\n'
  2104. html_phase = '<div class="phase" style="left:{0}%;width:{1}%;top:{2}px;height:{3}px;background:{4}">{5}</div>\n'
  2105. html_phaselet = '<div id="{0}" class="phaselet" style="left:{1}%;width:{2}%;background:{3}"></div>\n'
  2106. html_legend = '<div id="p{3}" class="square" style="left:{0}%;background:{1}">&nbsp;{2}</div>\n'
  2107. def __init__(self, rowheight, scaleheight):
  2108. self.html = ''
  2109. self.height = 0 # total timeline height
  2110. self.scaleH = scaleheight # timescale (top) row height
  2111. self.rowH = rowheight # device row height
  2112. self.bodyH = 0 # body height
  2113. self.rows = 0 # total timeline rows
  2114. self.rowlines = dict()
  2115. self.rowheight = dict()
  2116. def createHeader(self, sv, stamp):
  2117. if(not stamp['time']):
  2118. return
  2119. self.html += '<div class="version"><a href="https://01.org/suspendresume">%s v%s</a></div>' \
  2120. % (sv.title, sv.version)
  2121. if sv.logmsg and sv.testlog:
  2122. self.html += '<button id="showtest" class="logbtn btnfmt">log</button>'
  2123. if sv.dmesglog:
  2124. self.html += '<button id="showdmesg" class="logbtn btnfmt">dmesg</button>'
  2125. if sv.ftracelog:
  2126. self.html += '<button id="showftrace" class="logbtn btnfmt">ftrace</button>'
  2127. headline_stamp = '<div class="stamp">{0} {1} {2} {3}</div>\n'
  2128. self.html += headline_stamp.format(stamp['host'], stamp['kernel'],
  2129. stamp['mode'], stamp['time'])
  2130. if 'man' in stamp and 'plat' in stamp and 'cpu' in stamp and \
  2131. stamp['man'] and stamp['plat'] and stamp['cpu']:
  2132. headline_sysinfo = '<div class="stamp sysinfo">{0} {1} <i>with</i> {2}</div>\n'
  2133. self.html += headline_sysinfo.format(stamp['man'], stamp['plat'], stamp['cpu'])
  2134. # Function: getDeviceRows
  2135. # Description:
  2136. # determine how may rows the device funcs will take
  2137. # Arguments:
  2138. # rawlist: the list of devices/actions for a single phase
  2139. # Output:
  2140. # The total number of rows needed to display this phase of the timeline
  2141. def getDeviceRows(self, rawlist):
  2142. # clear all rows and set them to undefined
  2143. sortdict = dict()
  2144. for item in rawlist:
  2145. item.row = -1
  2146. sortdict[item] = item.length
  2147. sortlist = sorted(sortdict, key=sortdict.get, reverse=True)
  2148. remaining = len(sortlist)
  2149. rowdata = dict()
  2150. row = 1
  2151. # try to pack each row with as many ranges as possible
  2152. while(remaining > 0):
  2153. if(row not in rowdata):
  2154. rowdata[row] = []
  2155. for i in sortlist:
  2156. if(i.row >= 0):
  2157. continue
  2158. s = i.time
  2159. e = i.time + i.length
  2160. valid = True
  2161. for ritem in rowdata[row]:
  2162. rs = ritem.time
  2163. re = ritem.time + ritem.length
  2164. if(not (((s <= rs) and (e <= rs)) or
  2165. ((s >= re) and (e >= re)))):
  2166. valid = False
  2167. break
  2168. if(valid):
  2169. rowdata[row].append(i)
  2170. i.row = row
  2171. remaining -= 1
  2172. row += 1
  2173. return row
  2174. # Function: getPhaseRows
  2175. # Description:
  2176. # Organize the timeline entries into the smallest
  2177. # number of rows possible, with no entry overlapping
  2178. # Arguments:
  2179. # devlist: the list of devices/actions in a group of contiguous phases
  2180. # Output:
  2181. # The total number of rows needed to display this phase of the timeline
  2182. def getPhaseRows(self, devlist, row=0, sortby='length'):
  2183. # clear all rows and set them to undefined
  2184. remaining = len(devlist)
  2185. rowdata = dict()
  2186. sortdict = dict()
  2187. myphases = []
  2188. # initialize all device rows to -1 and calculate devrows
  2189. for item in devlist:
  2190. dev = item.dev
  2191. tp = (item.test, item.phase)
  2192. if tp not in myphases:
  2193. myphases.append(tp)
  2194. dev['row'] = -1
  2195. if sortby == 'start':
  2196. # sort by start 1st, then length 2nd
  2197. sortdict[item] = (-1*float(dev['start']), float(dev['end']) - float(dev['start']))
  2198. else:
  2199. # sort by length 1st, then name 2nd
  2200. sortdict[item] = (float(dev['end']) - float(dev['start']), item.dev['name'])
  2201. if 'src' in dev:
  2202. dev['devrows'] = self.getDeviceRows(dev['src'])
  2203. # sort the devlist by length so that large items graph on top
  2204. sortlist = sorted(sortdict, key=sortdict.get, reverse=True)
  2205. orderedlist = []
  2206. for item in sortlist:
  2207. if item.dev['pid'] == -2:
  2208. orderedlist.append(item)
  2209. for item in sortlist:
  2210. if item not in orderedlist:
  2211. orderedlist.append(item)
  2212. # try to pack each row with as many devices as possible
  2213. while(remaining > 0):
  2214. rowheight = 1
  2215. if(row not in rowdata):
  2216. rowdata[row] = []
  2217. for item in orderedlist:
  2218. dev = item.dev
  2219. if(dev['row'] < 0):
  2220. s = dev['start']
  2221. e = dev['end']
  2222. valid = True
  2223. for ritem in rowdata[row]:
  2224. rs = ritem.dev['start']
  2225. re = ritem.dev['end']
  2226. if(not (((s <= rs) and (e <= rs)) or
  2227. ((s >= re) and (e >= re)))):
  2228. valid = False
  2229. break
  2230. if(valid):
  2231. rowdata[row].append(item)
  2232. dev['row'] = row
  2233. remaining -= 1
  2234. if 'devrows' in dev and dev['devrows'] > rowheight:
  2235. rowheight = dev['devrows']
  2236. for t, p in myphases:
  2237. if t not in self.rowlines or t not in self.rowheight:
  2238. self.rowlines[t] = dict()
  2239. self.rowheight[t] = dict()
  2240. if p not in self.rowlines[t] or p not in self.rowheight[t]:
  2241. self.rowlines[t][p] = dict()
  2242. self.rowheight[t][p] = dict()
  2243. rh = self.rowH
  2244. # section headers should use a different row height
  2245. if len(rowdata[row]) == 1 and \
  2246. 'htmlclass' in rowdata[row][0].dev and \
  2247. 'sec' in rowdata[row][0].dev['htmlclass']:
  2248. rh = 15
  2249. self.rowlines[t][p][row] = rowheight
  2250. self.rowheight[t][p][row] = rowheight * rh
  2251. row += 1
  2252. if(row > self.rows):
  2253. self.rows = int(row)
  2254. return row
  2255. def phaseRowHeight(self, test, phase, row):
  2256. return self.rowheight[test][phase][row]
  2257. def phaseRowTop(self, test, phase, row):
  2258. top = 0
  2259. for i in sorted(self.rowheight[test][phase]):
  2260. if i >= row:
  2261. break
  2262. top += self.rowheight[test][phase][i]
  2263. return top
  2264. def calcTotalRows(self):
  2265. # Calculate the heights and offsets for the header and rows
  2266. maxrows = 0
  2267. standardphases = []
  2268. for t in self.rowlines:
  2269. for p in self.rowlines[t]:
  2270. total = 0
  2271. for i in sorted(self.rowlines[t][p]):
  2272. total += self.rowlines[t][p][i]
  2273. if total > maxrows:
  2274. maxrows = total
  2275. if total == len(self.rowlines[t][p]):
  2276. standardphases.append((t, p))
  2277. self.height = self.scaleH + (maxrows*self.rowH)
  2278. self.bodyH = self.height - self.scaleH
  2279. # if there is 1 line per row, draw them the standard way
  2280. for t, p in standardphases:
  2281. for i in sorted(self.rowheight[t][p]):
  2282. self.rowheight[t][p][i] = self.bodyH/len(self.rowlines[t][p])
  2283. def createZoomBox(self, mode='command', testcount=1):
  2284. # Create bounding box, add buttons
  2285. html_zoombox = '<center><button id="zoomin">ZOOM IN +</button><button id="zoomout">ZOOM OUT -</button><button id="zoomdef">ZOOM 1:1</button></center>\n'
  2286. html_timeline = '<div id="dmesgzoombox" class="zoombox">\n<div id="{0}" class="timeline" style="height:{1}px">\n'
  2287. html_devlist1 = '<button id="devlist1" class="devlist" style="float:left;">Device Detail{0}</button>'
  2288. html_devlist2 = '<button id="devlist2" class="devlist" style="float:right;">Device Detail2</button>\n'
  2289. if mode != 'command':
  2290. if testcount > 1:
  2291. self.html += html_devlist2
  2292. self.html += html_devlist1.format('1')
  2293. else:
  2294. self.html += html_devlist1.format('')
  2295. self.html += html_zoombox
  2296. self.html += html_timeline.format('dmesg', self.height)
  2297. # Function: createTimeScale
  2298. # Description:
  2299. # Create the timescale for a timeline block
  2300. # Arguments:
  2301. # m0: start time (mode begin)
  2302. # mMax: end time (mode end)
  2303. # tTotal: total timeline time
  2304. # mode: suspend or resume
  2305. # Output:
  2306. # The html code needed to display the time scale
  2307. def createTimeScale(self, m0, mMax, tTotal, mode):
  2308. timescale = '<div class="t" style="right:{0}%">{1}</div>\n'
  2309. rline = '<div class="t" style="left:0;border-left:1px solid black;border-right:0;">{0}</div>\n'
  2310. output = '<div class="timescale">\n'
  2311. # set scale for timeline
  2312. mTotal = mMax - m0
  2313. tS = 0.1
  2314. if(tTotal <= 0):
  2315. return output+'</div>\n'
  2316. if(tTotal > 4):
  2317. tS = 1
  2318. divTotal = int(mTotal/tS) + 1
  2319. divEdge = (mTotal - tS*(divTotal-1))*100/mTotal
  2320. for i in range(divTotal):
  2321. htmlline = ''
  2322. if(mode == 'suspend'):
  2323. pos = '%0.3f' % (100 - ((float(i)*tS*100)/mTotal) - divEdge)
  2324. val = '%0.fms' % (float(i-divTotal+1)*tS*1000)
  2325. if(i == divTotal - 1):
  2326. val = mode
  2327. htmlline = timescale.format(pos, val)
  2328. else:
  2329. pos = '%0.3f' % (100 - ((float(i)*tS*100)/mTotal))
  2330. val = '%0.fms' % (float(i)*tS*1000)
  2331. htmlline = timescale.format(pos, val)
  2332. if(i == 0):
  2333. htmlline = rline.format(mode)
  2334. output += htmlline
  2335. self.html += output+'</div>\n'
  2336. # Class: TestProps
  2337. # Description:
  2338. # A list of values describing the properties of these test runs
  2339. class TestProps:
  2340. stampfmt = '# [a-z]*-(?P<m>[0-9]{2})(?P<d>[0-9]{2})(?P<y>[0-9]{2})-'+\
  2341. '(?P<H>[0-9]{2})(?P<M>[0-9]{2})(?P<S>[0-9]{2})'+\
  2342. ' (?P<host>.*) (?P<mode>.*) (?P<kernel>.*)$'
  2343. batteryfmt = '^# battery (?P<a1>\w*) (?P<c1>\d*) (?P<a2>\w*) (?P<c2>\d*)'
  2344. testerrfmt = '^# enter_sleep_error (?P<e>.*)'
  2345. sysinfofmt = '^# sysinfo .*'
  2346. cmdlinefmt = '^# command \| (?P<cmd>.*)'
  2347. kparamsfmt = '^# kparams \| (?P<kp>.*)'
  2348. devpropfmt = '# Device Properties: .*'
  2349. tracertypefmt = '# tracer: (?P<t>.*)'
  2350. firmwarefmt = '# fwsuspend (?P<s>[0-9]*) fwresume (?P<r>[0-9]*)$'
  2351. procexecfmt = 'ps - (?P<ps>.*)$'
  2352. ftrace_line_fmt_fg = \
  2353. '^ *(?P<time>[0-9\.]*) *\| *(?P<cpu>[0-9]*)\)'+\
  2354. ' *(?P<proc>.*)-(?P<pid>[0-9]*) *\|'+\
  2355. '[ +!#\*@$]*(?P<dur>[0-9\.]*) .*\| (?P<msg>.*)'
  2356. ftrace_line_fmt_nop = \
  2357. ' *(?P<proc>.*)-(?P<pid>[0-9]*) *\[(?P<cpu>[0-9]*)\] *'+\
  2358. '(?P<flags>.{4}) *(?P<time>[0-9\.]*): *'+\
  2359. '(?P<msg>.*)'
  2360. def __init__(self):
  2361. self.stamp = ''
  2362. self.sysinfo = ''
  2363. self.cmdline = ''
  2364. self.kparams = ''
  2365. self.testerror = []
  2366. self.battery = []
  2367. self.fwdata = []
  2368. self.ftrace_line_fmt = self.ftrace_line_fmt_nop
  2369. self.cgformat = False
  2370. self.data = 0
  2371. self.ktemp = dict()
  2372. def setTracerType(self, tracer):
  2373. if(tracer == 'function_graph'):
  2374. self.cgformat = True
  2375. self.ftrace_line_fmt = self.ftrace_line_fmt_fg
  2376. elif(tracer == 'nop'):
  2377. self.ftrace_line_fmt = self.ftrace_line_fmt_nop
  2378. else:
  2379. doError('Invalid tracer format: [%s]' % tracer)
  2380. def parseStamp(self, data, sv):
  2381. # global test data
  2382. m = re.match(self.stampfmt, self.stamp)
  2383. data.stamp = {'time': '', 'host': '', 'mode': ''}
  2384. dt = datetime(int(m.group('y'))+2000, int(m.group('m')),
  2385. int(m.group('d')), int(m.group('H')), int(m.group('M')),
  2386. int(m.group('S')))
  2387. data.stamp['time'] = dt.strftime('%B %d %Y, %I:%M:%S %p')
  2388. data.stamp['host'] = m.group('host')
  2389. data.stamp['mode'] = m.group('mode')
  2390. data.stamp['kernel'] = m.group('kernel')
  2391. if re.match(self.sysinfofmt, self.sysinfo):
  2392. for f in self.sysinfo.split('|'):
  2393. if '#' in f:
  2394. continue
  2395. tmp = f.strip().split(':', 1)
  2396. key = tmp[0]
  2397. val = tmp[1]
  2398. data.stamp[key] = val
  2399. sv.hostname = data.stamp['host']
  2400. sv.suspendmode = data.stamp['mode']
  2401. if sv.suspendmode == 'command' and sv.ftracefile != '':
  2402. modes = ['on', 'freeze', 'standby', 'mem', 'disk']
  2403. fp = sysvals.openlog(sv.ftracefile, 'r')
  2404. for line in fp:
  2405. m = re.match('.* machine_suspend\[(?P<mode>.*)\]', line)
  2406. if m and m.group('mode') in ['1', '2', '3', '4']:
  2407. sv.suspendmode = modes[int(m.group('mode'))]
  2408. data.stamp['mode'] = sv.suspendmode
  2409. break
  2410. fp.close()
  2411. m = re.match(self.cmdlinefmt, self.cmdline)
  2412. if m:
  2413. sv.cmdline = m.group('cmd')
  2414. if self.kparams:
  2415. m = re.match(self.kparamsfmt, self.kparams)
  2416. if m:
  2417. sv.kparams = m.group('kp')
  2418. if not sv.stamp:
  2419. sv.stamp = data.stamp
  2420. # firmware data
  2421. if sv.suspendmode == 'mem' and len(self.fwdata) > data.testnumber:
  2422. data.fwSuspend, data.fwResume = self.fwdata[data.testnumber]
  2423. if(data.fwSuspend > 0 or data.fwResume > 0):
  2424. data.fwValid = True
  2425. # battery data
  2426. if len(self.battery) > data.testnumber:
  2427. m = re.match(self.batteryfmt, self.battery[data.testnumber])
  2428. if m:
  2429. data.battery = m.groups()
  2430. # sleep mode enter errors
  2431. if len(self.testerror) > data.testnumber:
  2432. m = re.match(self.testerrfmt, self.testerror[data.testnumber])
  2433. if m:
  2434. data.enterfail = m.group('e')
  2435. # Class: TestRun
  2436. # Description:
  2437. # A container for a suspend/resume test run. This is necessary as
  2438. # there could be more than one, and they need to be separate.
  2439. class TestRun:
  2440. def __init__(self, dataobj):
  2441. self.data = dataobj
  2442. self.ftemp = dict()
  2443. self.ttemp = dict()
  2444. class ProcessMonitor:
  2445. def __init__(self):
  2446. self.proclist = dict()
  2447. self.running = False
  2448. def procstat(self):
  2449. c = ['cat /proc/[1-9]*/stat 2>/dev/null']
  2450. process = Popen(c, shell=True, stdout=PIPE)
  2451. running = dict()
  2452. for line in process.stdout:
  2453. data = line.split()
  2454. pid = data[0]
  2455. name = re.sub('[()]', '', data[1])
  2456. user = int(data[13])
  2457. kern = int(data[14])
  2458. kjiff = ujiff = 0
  2459. if pid not in self.proclist:
  2460. self.proclist[pid] = {'name' : name, 'user' : user, 'kern' : kern}
  2461. else:
  2462. val = self.proclist[pid]
  2463. ujiff = user - val['user']
  2464. kjiff = kern - val['kern']
  2465. val['user'] = user
  2466. val['kern'] = kern
  2467. if ujiff > 0 or kjiff > 0:
  2468. running[pid] = ujiff + kjiff
  2469. process.wait()
  2470. out = ''
  2471. for pid in running:
  2472. jiffies = running[pid]
  2473. val = self.proclist[pid]
  2474. if out:
  2475. out += ','
  2476. out += '%s-%s %d' % (val['name'], pid, jiffies)
  2477. return 'ps - '+out
  2478. def processMonitor(self, tid):
  2479. while self.running:
  2480. out = self.procstat()
  2481. if out:
  2482. sysvals.fsetVal(out, 'trace_marker')
  2483. def start(self):
  2484. self.thread = Thread(target=self.processMonitor, args=(0,))
  2485. self.running = True
  2486. self.thread.start()
  2487. def stop(self):
  2488. self.running = False
  2489. # ----------------- FUNCTIONS --------------------
  2490. # Function: doesTraceLogHaveTraceEvents
  2491. # Description:
  2492. # Quickly determine if the ftrace log has all of the trace events,
  2493. # markers, and/or kprobes required for primary parsing.
  2494. def doesTraceLogHaveTraceEvents():
  2495. kpcheck = ['_cal: (', '_cpu_down()']
  2496. techeck = ['suspend_resume', 'device_pm_callback']
  2497. tmcheck = ['tracing_mark_write']
  2498. sysvals.usekprobes = False
  2499. fp = sysvals.openlog(sysvals.ftracefile, 'r')
  2500. for line in fp:
  2501. # check for kprobes
  2502. if not sysvals.usekprobes:
  2503. for i in kpcheck:
  2504. if i in line:
  2505. sysvals.usekprobes = True
  2506. # check for all necessary trace events
  2507. check = techeck[:]
  2508. for i in techeck:
  2509. if i in line:
  2510. check.remove(i)
  2511. techeck = check
  2512. # check for all necessary trace markers
  2513. check = tmcheck[:]
  2514. for i in tmcheck:
  2515. if i in line:
  2516. check.remove(i)
  2517. tmcheck = check
  2518. fp.close()
  2519. sysvals.usetraceevents = True if len(techeck) < 2 else False
  2520. sysvals.usetracemarkers = True if len(tmcheck) == 0 else False
  2521. # Function: appendIncompleteTraceLog
  2522. # Description:
  2523. # [deprecated for kernel 3.15 or newer]
  2524. # Adds callgraph data which lacks trace event data. This is only
  2525. # for timelines generated from 3.15 or older
  2526. # Arguments:
  2527. # testruns: the array of Data objects obtained from parseKernelLog
  2528. def appendIncompleteTraceLog(testruns):
  2529. # create TestRun vessels for ftrace parsing
  2530. testcnt = len(testruns)
  2531. testidx = 0
  2532. testrun = []
  2533. for data in testruns:
  2534. testrun.append(TestRun(data))
  2535. # extract the callgraph and traceevent data
  2536. sysvals.vprint('Analyzing the ftrace data (%s)...' % \
  2537. os.path.basename(sysvals.ftracefile))
  2538. tp = TestProps()
  2539. tf = sysvals.openlog(sysvals.ftracefile, 'r')
  2540. data = 0
  2541. for line in tf:
  2542. # remove any latent carriage returns
  2543. line = line.replace('\r\n', '')
  2544. # grab the stamp and sysinfo
  2545. if re.match(tp.stampfmt, line):
  2546. tp.stamp = line
  2547. continue
  2548. elif re.match(tp.sysinfofmt, line):
  2549. tp.sysinfo = line
  2550. continue
  2551. elif re.match(tp.cmdlinefmt, line):
  2552. tp.cmdline = line
  2553. continue
  2554. elif re.match(tp.batteryfmt, line):
  2555. tp.battery.append(line)
  2556. continue
  2557. elif re.match(tp.testerrfmt, line):
  2558. tp.testerror.append(line)
  2559. continue
  2560. # determine the trace data type (required for further parsing)
  2561. m = re.match(tp.tracertypefmt, line)
  2562. if(m):
  2563. tp.setTracerType(m.group('t'))
  2564. continue
  2565. # device properties line
  2566. if(re.match(tp.devpropfmt, line)):
  2567. devProps(line)
  2568. continue
  2569. # parse only valid lines, if this is not one move on
  2570. m = re.match(tp.ftrace_line_fmt, line)
  2571. if(not m):
  2572. continue
  2573. # gather the basic message data from the line
  2574. m_time = m.group('time')
  2575. m_pid = m.group('pid')
  2576. m_msg = m.group('msg')
  2577. if(tp.cgformat):
  2578. m_param3 = m.group('dur')
  2579. else:
  2580. m_param3 = 'traceevent'
  2581. if(m_time and m_pid and m_msg):
  2582. t = FTraceLine(m_time, m_msg, m_param3)
  2583. pid = int(m_pid)
  2584. else:
  2585. continue
  2586. # the line should be a call, return, or event
  2587. if(not t.fcall and not t.freturn and not t.fevent):
  2588. continue
  2589. # look for the suspend start marker
  2590. if(t.startMarker()):
  2591. data = testrun[testidx].data
  2592. tp.parseStamp(data, sysvals)
  2593. data.setStart(t.time)
  2594. continue
  2595. if(not data):
  2596. continue
  2597. # find the end of resume
  2598. if(t.endMarker()):
  2599. data.setEnd(t.time)
  2600. testidx += 1
  2601. if(testidx >= testcnt):
  2602. break
  2603. continue
  2604. # trace event processing
  2605. if(t.fevent):
  2606. continue
  2607. # call/return processing
  2608. elif sysvals.usecallgraph:
  2609. # create a callgraph object for the data
  2610. if(pid not in testrun[testidx].ftemp):
  2611. testrun[testidx].ftemp[pid] = []
  2612. testrun[testidx].ftemp[pid].append(FTraceCallGraph(pid, sysvals))
  2613. # when the call is finished, see which device matches it
  2614. cg = testrun[testidx].ftemp[pid][-1]
  2615. res = cg.addLine(t)
  2616. if(res != 0):
  2617. testrun[testidx].ftemp[pid].append(FTraceCallGraph(pid, sysvals))
  2618. if(res == -1):
  2619. testrun[testidx].ftemp[pid][-1].addLine(t)
  2620. tf.close()
  2621. for test in testrun:
  2622. # add the callgraph data to the device hierarchy
  2623. for pid in test.ftemp:
  2624. for cg in test.ftemp[pid]:
  2625. if len(cg.list) < 1 or cg.invalid or (cg.end - cg.start == 0):
  2626. continue
  2627. if(not cg.postProcess()):
  2628. id = 'task %s cpu %s' % (pid, m.group('cpu'))
  2629. sysvals.vprint('Sanity check failed for '+\
  2630. id+', ignoring this callback')
  2631. continue
  2632. callstart = cg.start
  2633. callend = cg.end
  2634. for p in test.data.sortedPhases():
  2635. if(test.data.dmesg[p]['start'] <= callstart and
  2636. callstart <= test.data.dmesg[p]['end']):
  2637. list = test.data.dmesg[p]['list']
  2638. for devname in list:
  2639. dev = list[devname]
  2640. if(pid == dev['pid'] and
  2641. callstart <= dev['start'] and
  2642. callend >= dev['end']):
  2643. dev['ftrace'] = cg
  2644. break
  2645. # Function: parseTraceLog
  2646. # Description:
  2647. # Analyze an ftrace log output file generated from this app during
  2648. # the execution phase. Used when the ftrace log is the primary data source
  2649. # and includes the suspend_resume and device_pm_callback trace events
  2650. # The ftrace filename is taken from sysvals
  2651. # Output:
  2652. # An array of Data objects
  2653. def parseTraceLog(live=False):
  2654. sysvals.vprint('Analyzing the ftrace data (%s)...' % \
  2655. os.path.basename(sysvals.ftracefile))
  2656. if(os.path.exists(sysvals.ftracefile) == False):
  2657. doError('%s does not exist' % sysvals.ftracefile)
  2658. if not live:
  2659. sysvals.setupAllKprobes()
  2660. ksuscalls = ['pm_prepare_console']
  2661. krescalls = ['pm_restore_console']
  2662. tracewatch = []
  2663. if sysvals.usekprobes:
  2664. tracewatch += ['sync_filesystems', 'freeze_processes', 'syscore_suspend',
  2665. 'syscore_resume', 'resume_console', 'thaw_processes', 'CPU_ON',
  2666. 'CPU_OFF', 'timekeeping_freeze', 'acpi_suspend']
  2667. # extract the callgraph and traceevent data
  2668. tp = TestProps()
  2669. testruns = []
  2670. testdata = []
  2671. testrun = 0
  2672. data = 0
  2673. tf = sysvals.openlog(sysvals.ftracefile, 'r')
  2674. phase = 'suspend_prepare'
  2675. for line in tf:
  2676. # remove any latent carriage returns
  2677. line = line.replace('\r\n', '')
  2678. # stamp and sysinfo lines
  2679. if re.match(tp.stampfmt, line):
  2680. tp.stamp = line
  2681. continue
  2682. elif re.match(tp.sysinfofmt, line):
  2683. tp.sysinfo = line
  2684. continue
  2685. elif re.match(tp.cmdlinefmt, line):
  2686. tp.cmdline = line
  2687. continue
  2688. elif re.match(tp.batteryfmt, line):
  2689. tp.battery.append(line)
  2690. continue
  2691. elif re.match(tp.testerrfmt, line):
  2692. tp.testerror.append(line)
  2693. continue
  2694. # firmware line: pull out any firmware data
  2695. m = re.match(tp.firmwarefmt, line)
  2696. if(m):
  2697. tp.fwdata.append((int(m.group('s')), int(m.group('r'))))
  2698. continue
  2699. # tracer type line: determine the trace data type
  2700. m = re.match(tp.tracertypefmt, line)
  2701. if(m):
  2702. tp.setTracerType(m.group('t'))
  2703. continue
  2704. # device properties line
  2705. if(re.match(tp.devpropfmt, line)):
  2706. devProps(line)
  2707. continue
  2708. # ignore all other commented lines
  2709. if line[0] == '#':
  2710. continue
  2711. # ftrace line: parse only valid lines
  2712. m = re.match(tp.ftrace_line_fmt, line)
  2713. if(not m):
  2714. continue
  2715. # gather the basic message data from the line
  2716. m_time = m.group('time')
  2717. m_proc = m.group('proc')
  2718. m_pid = m.group('pid')
  2719. m_msg = m.group('msg')
  2720. if(tp.cgformat):
  2721. m_param3 = m.group('dur')
  2722. else:
  2723. m_param3 = 'traceevent'
  2724. if(m_time and m_pid and m_msg):
  2725. t = FTraceLine(m_time, m_msg, m_param3)
  2726. pid = int(m_pid)
  2727. else:
  2728. continue
  2729. # the line should be a call, return, or event
  2730. if(not t.fcall and not t.freturn and not t.fevent):
  2731. continue
  2732. # find the start of suspend
  2733. if(t.startMarker()):
  2734. data = Data(len(testdata))
  2735. testdata.append(data)
  2736. testrun = TestRun(data)
  2737. testruns.append(testrun)
  2738. tp.parseStamp(data, sysvals)
  2739. data.setStart(t.time)
  2740. data.first_suspend_prepare = True
  2741. phase = data.setPhase('suspend_prepare', t.time, True)
  2742. continue
  2743. if(not data):
  2744. continue
  2745. # process cpu exec line
  2746. if t.type == 'tracing_mark_write':
  2747. m = re.match(tp.procexecfmt, t.name)
  2748. if(m):
  2749. proclist = dict()
  2750. for ps in m.group('ps').split(','):
  2751. val = ps.split()
  2752. if not val:
  2753. continue
  2754. name = val[0].replace('--', '-')
  2755. proclist[name] = int(val[1])
  2756. data.pstl[t.time] = proclist
  2757. continue
  2758. # find the end of resume
  2759. if(t.endMarker()):
  2760. data.handleEndMarker(t.time)
  2761. if(not sysvals.usetracemarkers):
  2762. # no trace markers? then quit and be sure to finish recording
  2763. # the event we used to trigger resume end
  2764. if('thaw_processes' in testrun.ttemp and len(testrun.ttemp['thaw_processes']) > 0):
  2765. # if an entry exists, assume this is its end
  2766. testrun.ttemp['thaw_processes'][-1]['end'] = t.time
  2767. break
  2768. continue
  2769. # trace event processing
  2770. if(t.fevent):
  2771. if(t.type == 'suspend_resume'):
  2772. # suspend_resume trace events have two types, begin and end
  2773. if(re.match('(?P<name>.*) begin$', t.name)):
  2774. isbegin = True
  2775. elif(re.match('(?P<name>.*) end$', t.name)):
  2776. isbegin = False
  2777. else:
  2778. continue
  2779. m = re.match('(?P<name>.*)\[(?P<val>[0-9]*)\] .*', t.name)
  2780. if(m):
  2781. val = m.group('val')
  2782. if val == '0':
  2783. name = m.group('name')
  2784. else:
  2785. name = m.group('name')+'['+val+']'
  2786. else:
  2787. m = re.match('(?P<name>.*) .*', t.name)
  2788. name = m.group('name')
  2789. # ignore these events
  2790. if(name.split('[')[0] in tracewatch):
  2791. continue
  2792. # -- phase changes --
  2793. # start of kernel suspend
  2794. if(re.match('suspend_enter\[.*', t.name)):
  2795. if(isbegin):
  2796. data.tKernSus = t.time
  2797. continue
  2798. # suspend_prepare start
  2799. elif(re.match('dpm_prepare\[.*', t.name)):
  2800. if isbegin and data.first_suspend_prepare:
  2801. data.first_suspend_prepare = False
  2802. if data.tKernSus == 0:
  2803. data.tKernSus = t.time
  2804. continue
  2805. phase = data.setPhase('suspend_prepare', t.time, isbegin)
  2806. continue
  2807. # suspend start
  2808. elif(re.match('dpm_suspend\[.*', t.name)):
  2809. phase = data.setPhase('suspend', t.time, isbegin)
  2810. continue
  2811. # suspend_late start
  2812. elif(re.match('dpm_suspend_late\[.*', t.name)):
  2813. phase = data.setPhase('suspend_late', t.time, isbegin)
  2814. continue
  2815. # suspend_noirq start
  2816. elif(re.match('dpm_suspend_noirq\[.*', t.name)):
  2817. phase = data.setPhase('suspend_noirq', t.time, isbegin)
  2818. continue
  2819. # suspend_machine/resume_machine
  2820. elif(re.match('machine_suspend\[.*', t.name)):
  2821. if(isbegin):
  2822. lp = data.lastPhase()
  2823. phase = data.setPhase('suspend_machine', data.dmesg[lp]['end'], True)
  2824. data.setPhase(phase, t.time, False)
  2825. if data.tSuspended == 0:
  2826. data.tSuspended = t.time
  2827. else:
  2828. phase = data.setPhase('resume_machine', t.time, True)
  2829. if(sysvals.suspendmode in ['mem', 'disk']):
  2830. susp = phase.replace('resume', 'suspend')
  2831. if susp in data.dmesg:
  2832. data.dmesg[susp]['end'] = t.time
  2833. data.tSuspended = t.time
  2834. data.tResumed = t.time
  2835. continue
  2836. # resume_noirq start
  2837. elif(re.match('dpm_resume_noirq\[.*', t.name)):
  2838. phase = data.setPhase('resume_noirq', t.time, isbegin)
  2839. continue
  2840. # resume_early start
  2841. elif(re.match('dpm_resume_early\[.*', t.name)):
  2842. phase = data.setPhase('resume_early', t.time, isbegin)
  2843. continue
  2844. # resume start
  2845. elif(re.match('dpm_resume\[.*', t.name)):
  2846. phase = data.setPhase('resume', t.time, isbegin)
  2847. continue
  2848. # resume complete start
  2849. elif(re.match('dpm_complete\[.*', t.name)):
  2850. phase = data.setPhase('resume_complete', t.time, isbegin)
  2851. continue
  2852. # skip trace events inside devices calls
  2853. if(not data.isTraceEventOutsideDeviceCalls(pid, t.time)):
  2854. continue
  2855. # global events (outside device calls) are graphed
  2856. if(name not in testrun.ttemp):
  2857. testrun.ttemp[name] = []
  2858. if(isbegin):
  2859. # create a new list entry
  2860. testrun.ttemp[name].append(\
  2861. {'begin': t.time, 'end': t.time, 'pid': pid})
  2862. else:
  2863. if(len(testrun.ttemp[name]) > 0):
  2864. # if an entry exists, assume this is its end
  2865. testrun.ttemp[name][-1]['end'] = t.time
  2866. # device callback start
  2867. elif(t.type == 'device_pm_callback_start'):
  2868. if phase not in data.dmesg:
  2869. continue
  2870. m = re.match('(?P<drv>.*) (?P<d>.*), parent: *(?P<p>.*), .*',\
  2871. t.name);
  2872. if(not m):
  2873. continue
  2874. drv = m.group('drv')
  2875. n = m.group('d')
  2876. p = m.group('p')
  2877. if(n and p):
  2878. data.newAction(phase, n, pid, p, t.time, -1, drv)
  2879. if pid not in data.devpids:
  2880. data.devpids.append(pid)
  2881. # device callback finish
  2882. elif(t.type == 'device_pm_callback_end'):
  2883. if phase not in data.dmesg:
  2884. continue
  2885. m = re.match('(?P<drv>.*) (?P<d>.*), err.*', t.name);
  2886. if(not m):
  2887. continue
  2888. n = m.group('d')
  2889. list = data.dmesg[phase]['list']
  2890. if(n in list):
  2891. dev = list[n]
  2892. dev['length'] = t.time - dev['start']
  2893. dev['end'] = t.time
  2894. # kprobe event processing
  2895. elif(t.fkprobe):
  2896. kprobename = t.type
  2897. kprobedata = t.name
  2898. key = (kprobename, pid)
  2899. # displayname is generated from kprobe data
  2900. displayname = ''
  2901. if(t.fcall):
  2902. displayname = sysvals.kprobeDisplayName(kprobename, kprobedata)
  2903. if not displayname:
  2904. continue
  2905. if(key not in tp.ktemp):
  2906. tp.ktemp[key] = []
  2907. tp.ktemp[key].append({
  2908. 'pid': pid,
  2909. 'begin': t.time,
  2910. 'end': t.time,
  2911. 'name': displayname,
  2912. 'cdata': kprobedata,
  2913. 'proc': m_proc,
  2914. })
  2915. # start of kernel resume
  2916. if(phase == 'suspend_prepare' and kprobename in ksuscalls):
  2917. data.tKernSus = t.time
  2918. elif(t.freturn):
  2919. if(key not in tp.ktemp) or len(tp.ktemp[key]) < 1:
  2920. continue
  2921. e = tp.ktemp[key][-1]
  2922. if e['begin'] < 0.0 or t.time - e['begin'] < 0.000001:
  2923. tp.ktemp[key].pop()
  2924. else:
  2925. e['end'] = t.time
  2926. e['rdata'] = kprobedata
  2927. # end of kernel resume
  2928. if(phase != 'suspend_prepare' and kprobename in krescalls):
  2929. if phase in data.dmesg:
  2930. data.dmesg[phase]['end'] = t.time
  2931. data.tKernRes = t.time
  2932. # callgraph processing
  2933. elif sysvals.usecallgraph:
  2934. # create a callgraph object for the data
  2935. key = (m_proc, pid)
  2936. if(key not in testrun.ftemp):
  2937. testrun.ftemp[key] = []
  2938. testrun.ftemp[key].append(FTraceCallGraph(pid, sysvals))
  2939. # when the call is finished, see which device matches it
  2940. cg = testrun.ftemp[key][-1]
  2941. res = cg.addLine(t)
  2942. if(res != 0):
  2943. testrun.ftemp[key].append(FTraceCallGraph(pid, sysvals))
  2944. if(res == -1):
  2945. testrun.ftemp[key][-1].addLine(t)
  2946. tf.close()
  2947. if data and not data.devicegroups:
  2948. sysvals.vprint('WARNING: end marker is missing')
  2949. data.handleEndMarker(t.time)
  2950. if sysvals.suspendmode == 'command':
  2951. for test in testruns:
  2952. for p in test.data.sortedPhases():
  2953. if p == 'suspend_prepare':
  2954. test.data.dmesg[p]['start'] = test.data.start
  2955. test.data.dmesg[p]['end'] = test.data.end
  2956. else:
  2957. test.data.dmesg[p]['start'] = test.data.end
  2958. test.data.dmesg[p]['end'] = test.data.end
  2959. test.data.tSuspended = test.data.end
  2960. test.data.tResumed = test.data.end
  2961. test.data.fwValid = False
  2962. # dev source and procmon events can be unreadable with mixed phase height
  2963. if sysvals.usedevsrc or sysvals.useprocmon:
  2964. sysvals.mixedphaseheight = False
  2965. # expand phase boundaries so there are no gaps
  2966. for data in testdata:
  2967. lp = data.sortedPhases()[0]
  2968. for p in data.sortedPhases():
  2969. if(p != lp and not ('machine' in p and 'machine' in lp)):
  2970. data.dmesg[lp]['end'] = data.dmesg[p]['start']
  2971. lp = p
  2972. for i in range(len(testruns)):
  2973. test = testruns[i]
  2974. data = test.data
  2975. # find the total time range for this test (begin, end)
  2976. tlb, tle = data.start, data.end
  2977. if i < len(testruns) - 1:
  2978. tle = testruns[i+1].data.start
  2979. # add the process usage data to the timeline
  2980. if sysvals.useprocmon:
  2981. data.createProcessUsageEvents()
  2982. # add the traceevent data to the device hierarchy
  2983. if(sysvals.usetraceevents):
  2984. # add actual trace funcs
  2985. for name in test.ttemp:
  2986. for event in test.ttemp[name]:
  2987. data.newActionGlobal(name, event['begin'], event['end'], event['pid'])
  2988. # add the kprobe based virtual tracefuncs as actual devices
  2989. for key in tp.ktemp:
  2990. name, pid = key
  2991. if name not in sysvals.tracefuncs:
  2992. continue
  2993. for e in tp.ktemp[key]:
  2994. kb, ke = e['begin'], e['end']
  2995. if kb == ke or tlb > kb or tle <= kb:
  2996. continue
  2997. color = sysvals.kprobeColor(name)
  2998. data.newActionGlobal(e['name'], kb, ke, pid, color)
  2999. # add config base kprobes and dev kprobes
  3000. if sysvals.usedevsrc:
  3001. for key in tp.ktemp:
  3002. name, pid = key
  3003. if name in sysvals.tracefuncs or name not in sysvals.dev_tracefuncs:
  3004. continue
  3005. for e in tp.ktemp[key]:
  3006. kb, ke = e['begin'], e['end']
  3007. if kb == ke or tlb > kb or tle <= kb:
  3008. continue
  3009. data.addDeviceFunctionCall(e['name'], name, e['proc'], pid, kb,
  3010. ke, e['cdata'], e['rdata'])
  3011. if sysvals.usecallgraph:
  3012. # add the callgraph data to the device hierarchy
  3013. sortlist = dict()
  3014. for key in test.ftemp:
  3015. proc, pid = key
  3016. for cg in test.ftemp[key]:
  3017. if len(cg.list) < 1 or cg.invalid or (cg.end - cg.start == 0):
  3018. continue
  3019. if(not cg.postProcess()):
  3020. id = 'task %s' % (pid)
  3021. sysvals.vprint('Sanity check failed for '+\
  3022. id+', ignoring this callback')
  3023. continue
  3024. # match cg data to devices
  3025. devname = ''
  3026. if sysvals.suspendmode != 'command':
  3027. devname = cg.deviceMatch(pid, data)
  3028. if not devname:
  3029. sortkey = '%f%f%d' % (cg.start, cg.end, pid)
  3030. sortlist[sortkey] = cg
  3031. elif len(cg.list) > 1000000:
  3032. sysvals.vprint('WARNING: the callgraph for %s is massive (%d lines)' %\
  3033. (devname, len(cg.list)))
  3034. # create blocks for orphan cg data
  3035. for sortkey in sorted(sortlist):
  3036. cg = sortlist[sortkey]
  3037. name = cg.name
  3038. if sysvals.isCallgraphFunc(name):
  3039. sysvals.vprint('Callgraph found for task %d: %.3fms, %s' % (cg.pid, (cg.end - cg.start)*1000, name))
  3040. cg.newActionFromFunction(data)
  3041. if sysvals.suspendmode == 'command':
  3042. return (testdata, '')
  3043. # fill in any missing phases
  3044. error = []
  3045. for data in testdata:
  3046. tn = '' if len(testdata) == 1 else ('%d' % (data.testnumber + 1))
  3047. terr = ''
  3048. phasedef = data.phasedef
  3049. lp = 'suspend_prepare'
  3050. for p in sorted(phasedef, key=lambda k:phasedef[k]['order']):
  3051. if p not in data.dmesg:
  3052. if not terr:
  3053. pprint('TEST%s FAILED: %s failed in %s phase' % (tn, sysvals.suspendmode, lp))
  3054. terr = '%s%s failed in %s phase' % (sysvals.suspendmode, tn, lp)
  3055. error.append(terr)
  3056. if data.tSuspended == 0:
  3057. data.tSuspended = data.dmesg[lp]['end']
  3058. if data.tResumed == 0:
  3059. data.tResumed = data.dmesg[lp]['end']
  3060. data.fwValid = False
  3061. sysvals.vprint('WARNING: phase "%s" is missing!' % p)
  3062. lp = p
  3063. if not terr and data.enterfail:
  3064. pprint('test%s FAILED: enter %s failed with %s' % (tn, sysvals.suspendmode, data.enterfail))
  3065. terr = 'test%s failed to enter %s mode' % (tn, sysvals.suspendmode)
  3066. error.append(terr)
  3067. if data.tSuspended == 0:
  3068. data.tSuspended = data.tKernRes
  3069. if data.tResumed == 0:
  3070. data.tResumed = data.tSuspended
  3071. if(len(sysvals.devicefilter) > 0):
  3072. data.deviceFilter(sysvals.devicefilter)
  3073. data.fixupInitcallsThatDidntReturn()
  3074. if sysvals.usedevsrc:
  3075. data.optimizeDevSrc()
  3076. # x2: merge any overlapping devices between test runs
  3077. if sysvals.usedevsrc and len(testdata) > 1:
  3078. tc = len(testdata)
  3079. for i in range(tc - 1):
  3080. devlist = testdata[i].overflowDevices()
  3081. for j in range(i + 1, tc):
  3082. testdata[j].mergeOverlapDevices(devlist)
  3083. testdata[0].stitchTouchingThreads(testdata[1:])
  3084. return (testdata, ', '.join(error))
  3085. # Function: loadKernelLog
  3086. # Description:
  3087. # [deprecated for kernel 3.15.0 or newer]
  3088. # load the dmesg file into memory and fix up any ordering issues
  3089. # The dmesg filename is taken from sysvals
  3090. # Output:
  3091. # An array of empty Data objects with only their dmesgtext attributes set
  3092. def loadKernelLog():
  3093. sysvals.vprint('Analyzing the dmesg data (%s)...' % \
  3094. os.path.basename(sysvals.dmesgfile))
  3095. if(os.path.exists(sysvals.dmesgfile) == False):
  3096. doError('%s does not exist' % sysvals.dmesgfile)
  3097. # there can be multiple test runs in a single file
  3098. tp = TestProps()
  3099. tp.stamp = datetime.now().strftime('# suspend-%m%d%y-%H%M%S localhost mem unknown')
  3100. testruns = []
  3101. data = 0
  3102. lf = sysvals.openlog(sysvals.dmesgfile, 'r')
  3103. for line in lf:
  3104. line = line.replace('\r\n', '')
  3105. idx = line.find('[')
  3106. if idx > 1:
  3107. line = line[idx:]
  3108. # grab the stamp and sysinfo
  3109. if re.match(tp.stampfmt, line):
  3110. tp.stamp = line
  3111. continue
  3112. elif re.match(tp.sysinfofmt, line):
  3113. tp.sysinfo = line
  3114. continue
  3115. elif re.match(tp.cmdlinefmt, line):
  3116. tp.cmdline = line
  3117. continue
  3118. elif re.match(tp.batteryfmt, line):
  3119. tp.battery.append(line)
  3120. continue
  3121. elif re.match(tp.testerrfmt, line):
  3122. tp.testerror.append(line)
  3123. continue
  3124. m = re.match(tp.firmwarefmt, line)
  3125. if(m):
  3126. tp.fwdata.append((int(m.group('s')), int(m.group('r'))))
  3127. continue
  3128. m = re.match('[ \t]*(\[ *)(?P<ktime>[0-9\.]*)(\]) (?P<msg>.*)', line)
  3129. if(not m):
  3130. continue
  3131. msg = m.group("msg")
  3132. if(re.match('PM: Syncing filesystems.*', msg)):
  3133. if(data):
  3134. testruns.append(data)
  3135. data = Data(len(testruns))
  3136. tp.parseStamp(data, sysvals)
  3137. if(not data):
  3138. continue
  3139. m = re.match('.* *(?P<k>[0-9]\.[0-9]{2}\.[0-9]-.*) .*', msg)
  3140. if(m):
  3141. sysvals.stamp['kernel'] = m.group('k')
  3142. m = re.match('PM: Preparing system for (?P<m>.*) sleep', msg)
  3143. if(m):
  3144. sysvals.stamp['mode'] = sysvals.suspendmode = m.group('m')
  3145. data.dmesgtext.append(line)
  3146. lf.close()
  3147. if data:
  3148. testruns.append(data)
  3149. if len(testruns) < 1:
  3150. pprint('ERROR: dmesg log has no suspend/resume data: %s' \
  3151. % sysvals.dmesgfile)
  3152. # fix lines with same timestamp/function with the call and return swapped
  3153. for data in testruns:
  3154. last = ''
  3155. for line in data.dmesgtext:
  3156. mc = re.match('.*(\[ *)(?P<t>[0-9\.]*)(\]) calling '+\
  3157. '(?P<f>.*)\+ @ .*, parent: .*', line)
  3158. mr = re.match('.*(\[ *)(?P<t>[0-9\.]*)(\]) call '+\
  3159. '(?P<f>.*)\+ returned .* after (?P<dt>.*) usecs', last)
  3160. if(mc and mr and (mc.group('t') == mr.group('t')) and
  3161. (mc.group('f') == mr.group('f'))):
  3162. i = data.dmesgtext.index(last)
  3163. j = data.dmesgtext.index(line)
  3164. data.dmesgtext[i] = line
  3165. data.dmesgtext[j] = last
  3166. last = line
  3167. return testruns
  3168. # Function: parseKernelLog
  3169. # Description:
  3170. # [deprecated for kernel 3.15.0 or newer]
  3171. # Analyse a dmesg log output file generated from this app during
  3172. # the execution phase. Create a set of device structures in memory
  3173. # for subsequent formatting in the html output file
  3174. # This call is only for legacy support on kernels where the ftrace
  3175. # data lacks the suspend_resume or device_pm_callbacks trace events.
  3176. # Arguments:
  3177. # data: an empty Data object (with dmesgtext) obtained from loadKernelLog
  3178. # Output:
  3179. # The filled Data object
  3180. def parseKernelLog(data):
  3181. phase = 'suspend_runtime'
  3182. if(data.fwValid):
  3183. sysvals.vprint('Firmware Suspend = %u ns, Firmware Resume = %u ns' % \
  3184. (data.fwSuspend, data.fwResume))
  3185. # dmesg phase match table
  3186. dm = {
  3187. 'suspend_prepare': ['PM: Syncing filesystems.*'],
  3188. 'suspend': ['PM: Entering [a-z]* sleep.*', 'Suspending console.*'],
  3189. 'suspend_late': ['PM: suspend of devices complete after.*'],
  3190. 'suspend_noirq': ['PM: late suspend of devices complete after.*'],
  3191. 'suspend_machine': ['PM: noirq suspend of devices complete after.*'],
  3192. 'resume_machine': ['ACPI: Low-level resume complete.*'],
  3193. 'resume_noirq': ['ACPI: Waking up from system sleep state.*'],
  3194. 'resume_early': ['PM: noirq resume of devices complete after.*'],
  3195. 'resume': ['PM: early resume of devices complete after.*'],
  3196. 'resume_complete': ['PM: resume of devices complete after.*'],
  3197. 'post_resume': ['.*Restarting tasks \.\.\..*'],
  3198. }
  3199. if(sysvals.suspendmode == 'standby'):
  3200. dm['resume_machine'] = ['PM: Restoring platform NVS memory']
  3201. elif(sysvals.suspendmode == 'disk'):
  3202. dm['suspend_late'] = ['PM: freeze of devices complete after.*']
  3203. dm['suspend_noirq'] = ['PM: late freeze of devices complete after.*']
  3204. dm['suspend_machine'] = ['PM: noirq freeze of devices complete after.*']
  3205. dm['resume_machine'] = ['PM: Restoring platform NVS memory']
  3206. dm['resume_early'] = ['PM: noirq restore of devices complete after.*']
  3207. dm['resume'] = ['PM: early restore of devices complete after.*']
  3208. dm['resume_complete'] = ['PM: restore of devices complete after.*']
  3209. elif(sysvals.suspendmode == 'freeze'):
  3210. dm['resume_machine'] = ['ACPI: resume from mwait']
  3211. # action table (expected events that occur and show up in dmesg)
  3212. at = {
  3213. 'sync_filesystems': {
  3214. 'smsg': 'PM: Syncing filesystems.*',
  3215. 'emsg': 'PM: Preparing system for mem sleep.*' },
  3216. 'freeze_user_processes': {
  3217. 'smsg': 'Freezing user space processes .*',
  3218. 'emsg': 'Freezing remaining freezable tasks.*' },
  3219. 'freeze_tasks': {
  3220. 'smsg': 'Freezing remaining freezable tasks.*',
  3221. 'emsg': 'PM: Entering (?P<mode>[a-z,A-Z]*) sleep.*' },
  3222. 'ACPI prepare': {
  3223. 'smsg': 'ACPI: Preparing to enter system sleep state.*',
  3224. 'emsg': 'PM: Saving platform NVS memory.*' },
  3225. 'PM vns': {
  3226. 'smsg': 'PM: Saving platform NVS memory.*',
  3227. 'emsg': 'Disabling non-boot CPUs .*' },
  3228. }
  3229. t0 = -1.0
  3230. cpu_start = -1.0
  3231. prevktime = -1.0
  3232. actions = dict()
  3233. for line in data.dmesgtext:
  3234. # parse each dmesg line into the time and message
  3235. m = re.match('[ \t]*(\[ *)(?P<ktime>[0-9\.]*)(\]) (?P<msg>.*)', line)
  3236. if(m):
  3237. val = m.group('ktime')
  3238. try:
  3239. ktime = float(val)
  3240. except:
  3241. continue
  3242. msg = m.group('msg')
  3243. # initialize data start to first line time
  3244. if t0 < 0:
  3245. data.setStart(ktime)
  3246. t0 = ktime
  3247. else:
  3248. continue
  3249. # check for a phase change line
  3250. phasechange = False
  3251. for p in dm:
  3252. for s in dm[p]:
  3253. if(re.match(s, msg)):
  3254. phasechange, phase = True, p
  3255. break
  3256. # hack for determining resume_machine end for freeze
  3257. if(not sysvals.usetraceevents and sysvals.suspendmode == 'freeze' \
  3258. and phase == 'resume_machine' and \
  3259. re.match('calling (?P<f>.*)\+ @ .*, parent: .*', msg)):
  3260. data.setPhase(phase, ktime, False)
  3261. phase = 'resume_noirq'
  3262. data.setPhase(phase, ktime, True)
  3263. if phasechange:
  3264. if phase == 'suspend_prepare':
  3265. data.setPhase(phase, ktime, True)
  3266. data.setStart(ktime)
  3267. data.tKernSus = ktime
  3268. elif phase == 'suspend':
  3269. lp = data.lastPhase()
  3270. if lp:
  3271. data.setPhase(lp, ktime, False)
  3272. data.setPhase(phase, ktime, True)
  3273. elif phase == 'suspend_late':
  3274. lp = data.lastPhase()
  3275. if lp:
  3276. data.setPhase(lp, ktime, False)
  3277. data.setPhase(phase, ktime, True)
  3278. elif phase == 'suspend_noirq':
  3279. lp = data.lastPhase()
  3280. if lp:
  3281. data.setPhase(lp, ktime, False)
  3282. data.setPhase(phase, ktime, True)
  3283. elif phase == 'suspend_machine':
  3284. lp = data.lastPhase()
  3285. if lp:
  3286. data.setPhase(lp, ktime, False)
  3287. data.setPhase(phase, ktime, True)
  3288. elif phase == 'resume_machine':
  3289. lp = data.lastPhase()
  3290. if(sysvals.suspendmode in ['freeze', 'standby']):
  3291. data.tSuspended = prevktime
  3292. if lp:
  3293. data.setPhase(lp, prevktime, False)
  3294. else:
  3295. data.tSuspended = ktime
  3296. if lp:
  3297. data.setPhase(lp, prevktime, False)
  3298. data.tResumed = ktime
  3299. data.setPhase(phase, ktime, True)
  3300. elif phase == 'resume_noirq':
  3301. lp = data.lastPhase()
  3302. if lp:
  3303. data.setPhase(lp, ktime, False)
  3304. data.setPhase(phase, ktime, True)
  3305. elif phase == 'resume_early':
  3306. lp = data.lastPhase()
  3307. if lp:
  3308. data.setPhase(lp, ktime, False)
  3309. data.setPhase(phase, ktime, True)
  3310. elif phase == 'resume':
  3311. lp = data.lastPhase()
  3312. if lp:
  3313. data.setPhase(lp, ktime, False)
  3314. data.setPhase(phase, ktime, True)
  3315. elif phase == 'resume_complete':
  3316. lp = data.lastPhase()
  3317. if lp:
  3318. data.setPhase(lp, ktime, False)
  3319. data.setPhase(phase, ktime, True)
  3320. elif phase == 'post_resume':
  3321. lp = data.lastPhase()
  3322. if lp:
  3323. data.setPhase(lp, ktime, False)
  3324. data.setEnd(ktime)
  3325. data.tKernRes = ktime
  3326. break
  3327. # -- device callbacks --
  3328. if(phase in data.sortedPhases()):
  3329. # device init call
  3330. if(re.match('calling (?P<f>.*)\+ @ .*, parent: .*', msg)):
  3331. sm = re.match('calling (?P<f>.*)\+ @ '+\
  3332. '(?P<n>.*), parent: (?P<p>.*)', msg);
  3333. f = sm.group('f')
  3334. n = sm.group('n')
  3335. p = sm.group('p')
  3336. if(f and n and p):
  3337. data.newAction(phase, f, int(n), p, ktime, -1, '')
  3338. # device init return
  3339. elif(re.match('call (?P<f>.*)\+ returned .* after '+\
  3340. '(?P<t>.*) usecs', msg)):
  3341. sm = re.match('call (?P<f>.*)\+ returned .* after '+\
  3342. '(?P<t>.*) usecs(?P<a>.*)', msg);
  3343. f = sm.group('f')
  3344. t = sm.group('t')
  3345. list = data.dmesg[phase]['list']
  3346. if(f in list):
  3347. dev = list[f]
  3348. dev['length'] = int(t)
  3349. dev['end'] = ktime
  3350. # if trace events are not available, these are better than nothing
  3351. if(not sysvals.usetraceevents):
  3352. # look for known actions
  3353. for a in at:
  3354. if(re.match(at[a]['smsg'], msg)):
  3355. if(a not in actions):
  3356. actions[a] = []
  3357. actions[a].append({'begin': ktime, 'end': ktime})
  3358. if(re.match(at[a]['emsg'], msg)):
  3359. if(a in actions):
  3360. actions[a][-1]['end'] = ktime
  3361. # now look for CPU on/off events
  3362. if(re.match('Disabling non-boot CPUs .*', msg)):
  3363. # start of first cpu suspend
  3364. cpu_start = ktime
  3365. elif(re.match('Enabling non-boot CPUs .*', msg)):
  3366. # start of first cpu resume
  3367. cpu_start = ktime
  3368. elif(re.match('smpboot: CPU (?P<cpu>[0-9]*) is now offline', msg)):
  3369. # end of a cpu suspend, start of the next
  3370. m = re.match('smpboot: CPU (?P<cpu>[0-9]*) is now offline', msg)
  3371. cpu = 'CPU'+m.group('cpu')
  3372. if(cpu not in actions):
  3373. actions[cpu] = []
  3374. actions[cpu].append({'begin': cpu_start, 'end': ktime})
  3375. cpu_start = ktime
  3376. elif(re.match('CPU(?P<cpu>[0-9]*) is up', msg)):
  3377. # end of a cpu resume, start of the next
  3378. m = re.match('CPU(?P<cpu>[0-9]*) is up', msg)
  3379. cpu = 'CPU'+m.group('cpu')
  3380. if(cpu not in actions):
  3381. actions[cpu] = []
  3382. actions[cpu].append({'begin': cpu_start, 'end': ktime})
  3383. cpu_start = ktime
  3384. prevktime = ktime
  3385. data.initDevicegroups()
  3386. # fill in any missing phases
  3387. phasedef = data.phasedef
  3388. terr, lp = '', 'suspend_prepare'
  3389. for p in sorted(phasedef, key=lambda k:phasedef[k]['order']):
  3390. if p not in data.dmesg:
  3391. if not terr:
  3392. pprint('TEST FAILED: %s failed in %s phase' % (sysvals.suspendmode, lp))
  3393. terr = '%s failed in %s phase' % (sysvals.suspendmode, lp)
  3394. if data.tSuspended == 0:
  3395. data.tSuspended = data.dmesg[lp]['end']
  3396. if data.tResumed == 0:
  3397. data.tResumed = data.dmesg[lp]['end']
  3398. sysvals.vprint('WARNING: phase "%s" is missing!' % p)
  3399. lp = p
  3400. lp = data.sortedPhases()[0]
  3401. for p in data.sortedPhases():
  3402. if(p != lp and not ('machine' in p and 'machine' in lp)):
  3403. data.dmesg[lp]['end'] = data.dmesg[p]['start']
  3404. lp = p
  3405. if data.tSuspended == 0:
  3406. data.tSuspended = data.tKernRes
  3407. if data.tResumed == 0:
  3408. data.tResumed = data.tSuspended
  3409. # fill in any actions we've found
  3410. for name in actions:
  3411. for event in actions[name]:
  3412. data.newActionGlobal(name, event['begin'], event['end'])
  3413. if(len(sysvals.devicefilter) > 0):
  3414. data.deviceFilter(sysvals.devicefilter)
  3415. data.fixupInitcallsThatDidntReturn()
  3416. return True
  3417. def callgraphHTML(sv, hf, num, cg, title, color, devid):
  3418. html_func_top = '<article id="{0}" class="atop" style="background:{1}">\n<input type="checkbox" class="pf" id="f{2}" checked/><label for="f{2}">{3} {4}</label>\n'
  3419. html_func_start = '<article>\n<input type="checkbox" class="pf" id="f{0}" checked/><label for="f{0}">{1} {2}</label>\n'
  3420. html_func_end = '</article>\n'
  3421. html_func_leaf = '<article>{0} {1}</article>\n'
  3422. cgid = devid
  3423. if cg.id:
  3424. cgid += cg.id
  3425. cglen = (cg.end - cg.start) * 1000
  3426. if cglen < sv.mincglen:
  3427. return num
  3428. fmt = '<r>(%.3f ms @ '+sv.timeformat+' to '+sv.timeformat+')</r>'
  3429. flen = fmt % (cglen, cg.start, cg.end)
  3430. hf.write(html_func_top.format(cgid, color, num, title, flen))
  3431. num += 1
  3432. for line in cg.list:
  3433. if(line.length < 0.000000001):
  3434. flen = ''
  3435. else:
  3436. fmt = '<n>(%.3f ms @ '+sv.timeformat+')</n>'
  3437. flen = fmt % (line.length*1000, line.time)
  3438. if line.isLeaf():
  3439. hf.write(html_func_leaf.format(line.name, flen))
  3440. elif line.freturn:
  3441. hf.write(html_func_end)
  3442. else:
  3443. hf.write(html_func_start.format(num, line.name, flen))
  3444. num += 1
  3445. hf.write(html_func_end)
  3446. return num
  3447. def addCallgraphs(sv, hf, data):
  3448. hf.write('<section id="callgraphs" class="callgraph">\n')
  3449. # write out the ftrace data converted to html
  3450. num = 0
  3451. for p in data.sortedPhases():
  3452. if sv.cgphase and p != sv.cgphase:
  3453. continue
  3454. list = data.dmesg[p]['list']
  3455. for devname in data.sortedDevices(p):
  3456. if len(sv.cgfilter) > 0 and devname not in sv.cgfilter:
  3457. continue
  3458. dev = list[devname]
  3459. color = 'white'
  3460. if 'color' in data.dmesg[p]:
  3461. color = data.dmesg[p]['color']
  3462. if 'color' in dev:
  3463. color = dev['color']
  3464. name = devname
  3465. if(devname in sv.devprops):
  3466. name = sv.devprops[devname].altName(devname)
  3467. if sv.suspendmode in suspendmodename:
  3468. name += ' '+p
  3469. if('ftrace' in dev):
  3470. cg = dev['ftrace']
  3471. num = callgraphHTML(sv, hf, num, cg,
  3472. name, color, dev['id'])
  3473. if('ftraces' in dev):
  3474. for cg in dev['ftraces']:
  3475. num = callgraphHTML(sv, hf, num, cg,
  3476. name+' &rarr; '+cg.name, color, dev['id'])
  3477. hf.write('\n\n </section>\n')
  3478. # Function: createHTMLSummarySimple
  3479. # Description:
  3480. # Create summary html file for a series of tests
  3481. # Arguments:
  3482. # testruns: array of Data objects from parseTraceLog
  3483. def createHTMLSummarySimple(testruns, htmlfile, title):
  3484. # write the html header first (html head, css code, up to body start)
  3485. html = '<!DOCTYPE html>\n<html>\n<head>\n\
  3486. <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n\
  3487. <title>SleepGraph Summary</title>\n\
  3488. <style type=\'text/css\'>\n\
  3489. .stamp {width: 100%;text-align:center;background:#888;line-height:30px;color:white;font: 25px Arial;}\n\
  3490. table {width:100%;border-collapse: collapse;}\n\
  3491. .summary {border:1px solid;}\n\
  3492. th {border: 1px solid black;background:#222;color:white;}\n\
  3493. td {font: 14px "Times New Roman";text-align: center;}\n\
  3494. tr.head td {border: 1px solid black;background:#aaa;}\n\
  3495. tr.alt {background-color:#ddd;}\n\
  3496. tr.notice {color:red;}\n\
  3497. .minval {background-color:#BBFFBB;}\n\
  3498. .medval {background-color:#BBBBFF;}\n\
  3499. .maxval {background-color:#FFBBBB;}\n\
  3500. .head a {color:#000;text-decoration: none;}\n\
  3501. </style>\n</head>\n<body>\n'
  3502. # extract the test data into list
  3503. list = dict()
  3504. tAvg, tMin, tMax, tMed = [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [[], []]
  3505. iMin, iMed, iMax = [0, 0], [0, 0], [0, 0]
  3506. num = 0
  3507. lastmode = ''
  3508. cnt = dict()
  3509. for data in sorted(testruns, key=lambda v:(v['mode'], v['host'], v['kernel'], v['time'])):
  3510. mode = data['mode']
  3511. if mode not in list:
  3512. list[mode] = {'data': [], 'avg': [0,0], 'min': [0,0], 'max': [0,0], 'med': [0,0]}
  3513. if lastmode and lastmode != mode and num > 0:
  3514. for i in range(2):
  3515. s = sorted(tMed[i])
  3516. list[lastmode]['med'][i] = s[int(len(s)/2)]
  3517. iMed[i] = tMed[i].index(list[lastmode]['med'][i])
  3518. list[lastmode]['avg'] = [tAvg[0] / num, tAvg[1] / num]
  3519. list[lastmode]['min'] = tMin
  3520. list[lastmode]['max'] = tMax
  3521. list[lastmode]['idx'] = (iMin, iMed, iMax)
  3522. tAvg, tMin, tMax, tMed = [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [[], []]
  3523. iMin, iMed, iMax = [0, 0], [0, 0], [0, 0]
  3524. num = 0
  3525. tVal = [float(data['suspend']), float(data['resume'])]
  3526. list[mode]['data'].append([data['host'], data['kernel'],
  3527. data['time'], tVal[0], tVal[1], data['url'], data['result'],
  3528. data['issues'], data['sus_worst'], data['sus_worsttime'],
  3529. data['res_worst'], data['res_worsttime']])
  3530. idx = len(list[mode]['data']) - 1
  3531. if data['result'] not in cnt:
  3532. cnt[data['result']] = 1
  3533. else:
  3534. cnt[data['result']] += 1
  3535. if data['result'] == 'pass':
  3536. for i in range(2):
  3537. tMed[i].append(tVal[i])
  3538. tAvg[i] += tVal[i]
  3539. if tMin[i] == 0 or tVal[i] < tMin[i]:
  3540. iMin[i] = idx
  3541. tMin[i] = tVal[i]
  3542. if tMax[i] == 0 or tVal[i] > tMax[i]:
  3543. iMax[i] = idx
  3544. tMax[i] = tVal[i]
  3545. num += 1
  3546. lastmode = mode
  3547. if lastmode and num > 0:
  3548. for i in range(2):
  3549. s = sorted(tMed[i])
  3550. list[lastmode]['med'][i] = s[int(len(s)/2)]
  3551. iMed[i] = tMed[i].index(list[lastmode]['med'][i])
  3552. list[lastmode]['avg'] = [tAvg[0] / num, tAvg[1] / num]
  3553. list[lastmode]['min'] = tMin
  3554. list[lastmode]['max'] = tMax
  3555. list[lastmode]['idx'] = (iMin, iMed, iMax)
  3556. # group test header
  3557. desc = []
  3558. for ilk in sorted(cnt, reverse=True):
  3559. if cnt[ilk] > 0:
  3560. desc.append('%d %s' % (cnt[ilk], ilk))
  3561. html += '<div class="stamp">%s (%d tests: %s)</div>\n' % (title, len(testruns), ', '.join(desc))
  3562. th = '\t<th>{0}</th>\n'
  3563. td = '\t<td>{0}</td>\n'
  3564. tdh = '\t<td{1}>{0}</td>\n'
  3565. tdlink = '\t<td><a href="{0}">html</a></td>\n'
  3566. # table header
  3567. html += '<table class="summary">\n<tr>\n' + th.format('#') +\
  3568. th.format('Mode') + th.format('Host') + th.format('Kernel') +\
  3569. th.format('Test Time') + th.format('Result') + th.format('Issues') +\
  3570. th.format('Suspend') + th.format('Resume') +\
  3571. th.format('Worst Suspend Device') + th.format('SD Time') +\
  3572. th.format('Worst Resume Device') + th.format('RD Time') +\
  3573. th.format('Detail') + '</tr>\n'
  3574. # export list into html
  3575. head = '<tr class="head"><td>{0}</td><td>{1}</td>'+\
  3576. '<td colspan=12 class="sus">Suspend Avg={2} '+\
  3577. '<span class=minval><a href="#s{10}min">Min={3}</a></span> '+\
  3578. '<span class=medval><a href="#s{10}med">Med={4}</a></span> '+\
  3579. '<span class=maxval><a href="#s{10}max">Max={5}</a></span> '+\
  3580. 'Resume Avg={6} '+\
  3581. '<span class=minval><a href="#r{10}min">Min={7}</a></span> '+\
  3582. '<span class=medval><a href="#r{10}med">Med={8}</a></span> '+\
  3583. '<span class=maxval><a href="#r{10}max">Max={9}</a></span></td>'+\
  3584. '</tr>\n'
  3585. headnone = '<tr class="head"><td>{0}</td><td>{1}</td><td colspan=12></td></tr>\n'
  3586. for mode in list:
  3587. # header line for each suspend mode
  3588. num = 0
  3589. tAvg, tMin, tMax, tMed = list[mode]['avg'], list[mode]['min'],\
  3590. list[mode]['max'], list[mode]['med']
  3591. count = len(list[mode]['data'])
  3592. if 'idx' in list[mode]:
  3593. iMin, iMed, iMax = list[mode]['idx']
  3594. html += head.format('%d' % count, mode.upper(),
  3595. '%.3f' % tAvg[0], '%.3f' % tMin[0], '%.3f' % tMed[0], '%.3f' % tMax[0],
  3596. '%.3f' % tAvg[1], '%.3f' % tMin[1], '%.3f' % tMed[1], '%.3f' % tMax[1],
  3597. mode.lower()
  3598. )
  3599. else:
  3600. iMin = iMed = iMax = [-1, -1, -1]
  3601. html += headnone.format('%d' % count, mode.upper())
  3602. for d in list[mode]['data']:
  3603. # row classes - alternate row color
  3604. rcls = ['alt'] if num % 2 == 1 else []
  3605. if d[6] != 'pass':
  3606. rcls.append('notice')
  3607. html += '<tr class="'+(' '.join(rcls))+'">\n' if len(rcls) > 0 else '<tr>\n'
  3608. # figure out if the line has sus or res highlighted
  3609. idx = list[mode]['data'].index(d)
  3610. tHigh = ['', '']
  3611. for i in range(2):
  3612. tag = 's%s' % mode if i == 0 else 'r%s' % mode
  3613. if idx == iMin[i]:
  3614. tHigh[i] = ' id="%smin" class=minval title="Minimum"' % tag
  3615. elif idx == iMax[i]:
  3616. tHigh[i] = ' id="%smax" class=maxval title="Maximum"' % tag
  3617. elif idx == iMed[i]:
  3618. tHigh[i] = ' id="%smed" class=medval title="Median"' % tag
  3619. html += td.format("%d" % (list[mode]['data'].index(d) + 1)) # row
  3620. html += td.format(mode) # mode
  3621. html += td.format(d[0]) # host
  3622. html += td.format(d[1]) # kernel
  3623. html += td.format(d[2]) # time
  3624. html += td.format(d[6]) # result
  3625. html += td.format(d[7]) # issues
  3626. html += tdh.format('%.3f ms' % d[3], tHigh[0]) if d[3] else td.format('') # suspend
  3627. html += tdh.format('%.3f ms' % d[4], tHigh[1]) if d[4] else td.format('') # resume
  3628. html += td.format(d[8]) # sus_worst
  3629. html += td.format('%.3f ms' % d[9]) if d[9] else td.format('') # sus_worst time
  3630. html += td.format(d[10]) # res_worst
  3631. html += td.format('%.3f ms' % d[11]) if d[11] else td.format('') # res_worst time
  3632. html += tdlink.format(d[5]) if d[5] else td.format('') # url
  3633. html += '</tr>\n'
  3634. num += 1
  3635. # flush the data to file
  3636. hf = open(htmlfile, 'w')
  3637. hf.write(html+'</table>\n</body>\n</html>\n')
  3638. hf.close()
  3639. def ordinal(value):
  3640. suffix = 'th'
  3641. if value < 10 or value > 19:
  3642. if value % 10 == 1:
  3643. suffix = 'st'
  3644. elif value % 10 == 2:
  3645. suffix = 'nd'
  3646. elif value % 10 == 3:
  3647. suffix = 'rd'
  3648. return '%d%s' % (value, suffix)
  3649. # Function: createHTML
  3650. # Description:
  3651. # Create the output html file from the resident test data
  3652. # Arguments:
  3653. # testruns: array of Data objects from parseKernelLog or parseTraceLog
  3654. # Output:
  3655. # True if the html file was created, false if it failed
  3656. def createHTML(testruns, testfail):
  3657. if len(testruns) < 1:
  3658. pprint('ERROR: Not enough test data to build a timeline')
  3659. return
  3660. kerror = False
  3661. for data in testruns:
  3662. if data.kerror:
  3663. kerror = True
  3664. if(sysvals.suspendmode in ['freeze', 'standby']):
  3665. data.trimFreezeTime(testruns[-1].tSuspended)
  3666. # html function templates
  3667. html_error = '<div id="{1}" title="kernel error/warning" class="err" style="right:{0}%">{2}&rarr;</div>\n'
  3668. html_traceevent = '<div title="{0}" class="traceevent{6}" style="left:{1}%;top:{2}px;height:{3}px;width:{4}%;line-height:{3}px;{7}">{5}</div>\n'
  3669. html_cpuexec = '<div class="jiffie" style="left:{0}%;top:{1}px;height:{2}px;width:{3}%;background:{4};"></div>\n'
  3670. html_timetotal = '<table class="time1">\n<tr>'\
  3671. '<td class="green" title="{3}">{2} Suspend Time: <b>{0} ms</b></td>'\
  3672. '<td class="yellow" title="{4}">{2} Resume Time: <b>{1} ms</b></td>'\
  3673. '</tr>\n</table>\n'
  3674. html_timetotal2 = '<table class="time1">\n<tr>'\
  3675. '<td class="green" title="{4}">{3} Suspend Time: <b>{0} ms</b></td>'\
  3676. '<td class="gray" title="time spent in low-power mode with clock running">'+sysvals.suspendmode+' time: <b>{1} ms</b></td>'\
  3677. '<td class="yellow" title="{5}">{3} Resume Time: <b>{2} ms</b></td>'\
  3678. '</tr>\n</table>\n'
  3679. html_timetotal3 = '<table class="time1">\n<tr>'\
  3680. '<td class="green">Execution Time: <b>{0} ms</b></td>'\
  3681. '<td class="yellow">Command: <b>{1}</b></td>'\
  3682. '</tr>\n</table>\n'
  3683. html_timegroups = '<table class="time2">\n<tr>'\
  3684. '<td class="green" title="time from kernel enter_state({5}) to firmware mode [kernel time only]">{4}Kernel Suspend: {0} ms</td>'\
  3685. '<td class="purple">{4}Firmware Suspend: {1} ms</td>'\
  3686. '<td class="purple">{4}Firmware Resume: {2} ms</td>'\
  3687. '<td class="yellow" title="time from firmware mode to return from kernel enter_state({5}) [kernel time only]">{4}Kernel Resume: {3} ms</td>'\
  3688. '</tr>\n</table>\n'
  3689. html_fail = '<table class="testfail"><tr><td>{0}</td></tr></table>\n'
  3690. # html format variables
  3691. scaleH = 20
  3692. if kerror:
  3693. scaleH = 40
  3694. # device timeline
  3695. devtl = Timeline(30, scaleH)
  3696. # write the test title and general info header
  3697. devtl.createHeader(sysvals, testruns[0].stamp)
  3698. # Generate the header for this timeline
  3699. for data in testruns:
  3700. tTotal = data.end - data.start
  3701. sktime, rktime = data.getTimeValues()
  3702. if(tTotal == 0):
  3703. doError('No timeline data')
  3704. if(len(data.tLow) > 0):
  3705. low_time = '|'.join(data.tLow)
  3706. if sysvals.suspendmode == 'command':
  3707. run_time = '%.0f'%((data.end-data.start)*1000)
  3708. if sysvals.testcommand:
  3709. testdesc = sysvals.testcommand
  3710. else:
  3711. testdesc = 'unknown'
  3712. if(len(testruns) > 1):
  3713. testdesc = ordinal(data.testnumber+1)+' '+testdesc
  3714. thtml = html_timetotal3.format(run_time, testdesc)
  3715. devtl.html += thtml
  3716. elif data.fwValid:
  3717. suspend_time = '%.0f'%(sktime + (data.fwSuspend/1000000.0))
  3718. resume_time = '%.0f'%(rktime + (data.fwResume/1000000.0))
  3719. testdesc1 = 'Total'
  3720. testdesc2 = ''
  3721. stitle = 'time from kernel enter_state(%s) to low-power mode [kernel & firmware time]' % sysvals.suspendmode
  3722. rtitle = 'time from low-power mode to return from kernel enter_state(%s) [firmware & kernel time]' % sysvals.suspendmode
  3723. if(len(testruns) > 1):
  3724. testdesc1 = testdesc2 = ordinal(data.testnumber+1)
  3725. testdesc2 += ' '
  3726. if(len(data.tLow) == 0):
  3727. thtml = html_timetotal.format(suspend_time, \
  3728. resume_time, testdesc1, stitle, rtitle)
  3729. else:
  3730. thtml = html_timetotal2.format(suspend_time, low_time, \
  3731. resume_time, testdesc1, stitle, rtitle)
  3732. devtl.html += thtml
  3733. sftime = '%.3f'%(data.fwSuspend / 1000000.0)
  3734. rftime = '%.3f'%(data.fwResume / 1000000.0)
  3735. devtl.html += html_timegroups.format('%.3f'%sktime, \
  3736. sftime, rftime, '%.3f'%rktime, testdesc2, sysvals.suspendmode)
  3737. else:
  3738. suspend_time = '%.3f' % sktime
  3739. resume_time = '%.3f' % rktime
  3740. testdesc = 'Kernel'
  3741. stitle = 'time from kernel enter_state(%s) to firmware mode [kernel time only]' % sysvals.suspendmode
  3742. rtitle = 'time from firmware mode to return from kernel enter_state(%s) [kernel time only]' % sysvals.suspendmode
  3743. if(len(testruns) > 1):
  3744. testdesc = ordinal(data.testnumber+1)+' '+testdesc
  3745. if(len(data.tLow) == 0):
  3746. thtml = html_timetotal.format(suspend_time, \
  3747. resume_time, testdesc, stitle, rtitle)
  3748. else:
  3749. thtml = html_timetotal2.format(suspend_time, low_time, \
  3750. resume_time, testdesc, stitle, rtitle)
  3751. devtl.html += thtml
  3752. if testfail:
  3753. devtl.html += html_fail.format(testfail)
  3754. # time scale for potentially multiple datasets
  3755. t0 = testruns[0].start
  3756. tMax = testruns[-1].end
  3757. tTotal = tMax - t0
  3758. # determine the maximum number of rows we need to draw
  3759. fulllist = []
  3760. threadlist = []
  3761. pscnt = 0
  3762. devcnt = 0
  3763. for data in testruns:
  3764. data.selectTimelineDevices('%f', tTotal, sysvals.mindevlen)
  3765. for group in data.devicegroups:
  3766. devlist = []
  3767. for phase in group:
  3768. for devname in data.tdevlist[phase]:
  3769. d = DevItem(data.testnumber, phase, data.dmesg[phase]['list'][devname])
  3770. devlist.append(d)
  3771. if d.isa('kth'):
  3772. threadlist.append(d)
  3773. else:
  3774. if d.isa('ps'):
  3775. pscnt += 1
  3776. else:
  3777. devcnt += 1
  3778. fulllist.append(d)
  3779. if sysvals.mixedphaseheight:
  3780. devtl.getPhaseRows(devlist)
  3781. if not sysvals.mixedphaseheight:
  3782. if len(threadlist) > 0 and len(fulllist) > 0:
  3783. if pscnt > 0 and devcnt > 0:
  3784. msg = 'user processes & device pm callbacks'
  3785. elif pscnt > 0:
  3786. msg = 'user processes'
  3787. else:
  3788. msg = 'device pm callbacks'
  3789. d = testruns[0].addHorizontalDivider(msg, testruns[-1].end)
  3790. fulllist.insert(0, d)
  3791. devtl.getPhaseRows(fulllist)
  3792. if len(threadlist) > 0:
  3793. d = testruns[0].addHorizontalDivider('asynchronous kernel threads', testruns[-1].end)
  3794. threadlist.insert(0, d)
  3795. devtl.getPhaseRows(threadlist, devtl.rows)
  3796. devtl.calcTotalRows()
  3797. # draw the full timeline
  3798. devtl.createZoomBox(sysvals.suspendmode, len(testruns))
  3799. for data in testruns:
  3800. # draw each test run and block chronologically
  3801. phases = {'suspend':[],'resume':[]}
  3802. for phase in data.sortedPhases():
  3803. if data.dmesg[phase]['start'] >= data.tSuspended:
  3804. phases['resume'].append(phase)
  3805. else:
  3806. phases['suspend'].append(phase)
  3807. # now draw the actual timeline blocks
  3808. for dir in phases:
  3809. # draw suspend and resume blocks separately
  3810. bname = '%s%d' % (dir[0], data.testnumber)
  3811. if dir == 'suspend':
  3812. m0 = data.start
  3813. mMax = data.tSuspended
  3814. left = '%f' % (((m0-t0)*100.0)/tTotal)
  3815. else:
  3816. m0 = data.tSuspended
  3817. mMax = data.end
  3818. # in an x2 run, remove any gap between blocks
  3819. if len(testruns) > 1 and data.testnumber == 0:
  3820. mMax = testruns[1].start
  3821. left = '%f' % ((((m0-t0)*100.0)+sysvals.srgap/2)/tTotal)
  3822. mTotal = mMax - m0
  3823. # if a timeline block is 0 length, skip altogether
  3824. if mTotal == 0:
  3825. continue
  3826. width = '%f' % (((mTotal*100.0)-sysvals.srgap/2)/tTotal)
  3827. devtl.html += devtl.html_tblock.format(bname, left, width, devtl.scaleH)
  3828. for b in phases[dir]:
  3829. # draw the phase color background
  3830. phase = data.dmesg[b]
  3831. length = phase['end']-phase['start']
  3832. left = '%f' % (((phase['start']-m0)*100.0)/mTotal)
  3833. width = '%f' % ((length*100.0)/mTotal)
  3834. devtl.html += devtl.html_phase.format(left, width, \
  3835. '%.3f'%devtl.scaleH, '%.3f'%devtl.bodyH, \
  3836. data.dmesg[b]['color'], '')
  3837. for e in data.errorinfo[dir]:
  3838. # draw red lines for any kernel errors found
  3839. type, t, idx1, idx2 = e
  3840. id = '%d_%d' % (idx1, idx2)
  3841. right = '%f' % (((mMax-t)*100.0)/mTotal)
  3842. devtl.html += html_error.format(right, id, type)
  3843. for b in phases[dir]:
  3844. # draw the devices for this phase
  3845. phaselist = data.dmesg[b]['list']
  3846. for d in data.tdevlist[b]:
  3847. name = d
  3848. drv = ''
  3849. dev = phaselist[d]
  3850. xtraclass = ''
  3851. xtrainfo = ''
  3852. xtrastyle = ''
  3853. if 'htmlclass' in dev:
  3854. xtraclass = dev['htmlclass']
  3855. if 'color' in dev:
  3856. xtrastyle = 'background:%s;' % dev['color']
  3857. if(d in sysvals.devprops):
  3858. name = sysvals.devprops[d].altName(d)
  3859. xtraclass = sysvals.devprops[d].xtraClass()
  3860. xtrainfo = sysvals.devprops[d].xtraInfo()
  3861. elif xtraclass == ' kth':
  3862. xtrainfo = ' kernel_thread'
  3863. if('drv' in dev and dev['drv']):
  3864. drv = ' {%s}' % dev['drv']
  3865. rowheight = devtl.phaseRowHeight(data.testnumber, b, dev['row'])
  3866. rowtop = devtl.phaseRowTop(data.testnumber, b, dev['row'])
  3867. top = '%.3f' % (rowtop + devtl.scaleH)
  3868. left = '%f' % (((dev['start']-m0)*100)/mTotal)
  3869. width = '%f' % (((dev['end']-dev['start'])*100)/mTotal)
  3870. length = ' (%0.3f ms) ' % ((dev['end']-dev['start'])*1000)
  3871. title = name+drv+xtrainfo+length
  3872. if sysvals.suspendmode == 'command':
  3873. title += sysvals.testcommand
  3874. elif xtraclass == ' ps':
  3875. if 'suspend' in b:
  3876. title += 'pre_suspend_process'
  3877. else:
  3878. title += 'post_resume_process'
  3879. else:
  3880. title += b
  3881. devtl.html += devtl.html_device.format(dev['id'], \
  3882. title, left, top, '%.3f'%rowheight, width, \
  3883. d+drv, xtraclass, xtrastyle)
  3884. if('cpuexec' in dev):
  3885. for t in sorted(dev['cpuexec']):
  3886. start, end = t
  3887. j = float(dev['cpuexec'][t]) / 5
  3888. if j > 1.0:
  3889. j = 1.0
  3890. height = '%.3f' % (rowheight/3)
  3891. top = '%.3f' % (rowtop + devtl.scaleH + 2*rowheight/3)
  3892. left = '%f' % (((start-m0)*100)/mTotal)
  3893. width = '%f' % ((end-start)*100/mTotal)
  3894. color = 'rgba(255, 0, 0, %f)' % j
  3895. devtl.html += \
  3896. html_cpuexec.format(left, top, height, width, color)
  3897. if('src' not in dev):
  3898. continue
  3899. # draw any trace events for this device
  3900. for e in dev['src']:
  3901. height = '%.3f' % devtl.rowH
  3902. top = '%.3f' % (rowtop + devtl.scaleH + (e.row*devtl.rowH))
  3903. left = '%f' % (((e.time-m0)*100)/mTotal)
  3904. width = '%f' % (e.length*100/mTotal)
  3905. xtrastyle = ''
  3906. if e.color:
  3907. xtrastyle = 'background:%s;' % e.color
  3908. devtl.html += \
  3909. html_traceevent.format(e.title(), \
  3910. left, top, height, width, e.text(), '', xtrastyle)
  3911. # draw the time scale, try to make the number of labels readable
  3912. devtl.createTimeScale(m0, mMax, tTotal, dir)
  3913. devtl.html += '</div>\n'
  3914. # timeline is finished
  3915. devtl.html += '</div>\n</div>\n'
  3916. # draw a legend which describes the phases by color
  3917. if sysvals.suspendmode != 'command':
  3918. phasedef = testruns[-1].phasedef
  3919. devtl.html += '<div class="legend">\n'
  3920. pdelta = 100.0/len(phasedef.keys())
  3921. pmargin = pdelta / 4.0
  3922. for phase in sorted(phasedef, key=lambda k:phasedef[k]['order']):
  3923. id, p = '', phasedef[phase]
  3924. for word in phase.split('_'):
  3925. id += word[0]
  3926. order = '%.2f' % ((p['order'] * pdelta) + pmargin)
  3927. name = string.replace(phase, '_', ' &nbsp;')
  3928. devtl.html += devtl.html_legend.format(order, p['color'], name, id)
  3929. devtl.html += '</div>\n'
  3930. hf = open(sysvals.htmlfile, 'w')
  3931. addCSS(hf, sysvals, len(testruns), kerror)
  3932. # write the device timeline
  3933. hf.write(devtl.html)
  3934. hf.write('<div id="devicedetailtitle"></div>\n')
  3935. hf.write('<div id="devicedetail" style="display:none;">\n')
  3936. # draw the colored boxes for the device detail section
  3937. for data in testruns:
  3938. hf.write('<div id="devicedetail%d">\n' % data.testnumber)
  3939. pscolor = 'linear-gradient(to top left, #ccc, #eee)'
  3940. hf.write(devtl.html_phaselet.format('pre_suspend_process', \
  3941. '0', '0', pscolor))
  3942. for b in data.sortedPhases():
  3943. phase = data.dmesg[b]
  3944. length = phase['end']-phase['start']
  3945. left = '%.3f' % (((phase['start']-t0)*100.0)/tTotal)
  3946. width = '%.3f' % ((length*100.0)/tTotal)
  3947. hf.write(devtl.html_phaselet.format(b, left, width, \
  3948. data.dmesg[b]['color']))
  3949. hf.write(devtl.html_phaselet.format('post_resume_process', \
  3950. '0', '0', pscolor))
  3951. if sysvals.suspendmode == 'command':
  3952. hf.write(devtl.html_phaselet.format('cmdexec', '0', '0', pscolor))
  3953. hf.write('</div>\n')
  3954. hf.write('</div>\n')
  3955. # write the ftrace data (callgraph)
  3956. if sysvals.cgtest >= 0 and len(testruns) > sysvals.cgtest:
  3957. data = testruns[sysvals.cgtest]
  3958. else:
  3959. data = testruns[-1]
  3960. if sysvals.usecallgraph:
  3961. addCallgraphs(sysvals, hf, data)
  3962. # add the test log as a hidden div
  3963. if sysvals.testlog and sysvals.logmsg:
  3964. hf.write('<div id="testlog" style="display:none;">\n'+sysvals.logmsg+'</div>\n')
  3965. # add the dmesg log as a hidden div
  3966. if sysvals.dmesglog and sysvals.dmesgfile:
  3967. hf.write('<div id="dmesglog" style="display:none;">\n')
  3968. lf = sysvals.openlog(sysvals.dmesgfile, 'r')
  3969. for line in lf:
  3970. line = line.replace('<', '&lt').replace('>', '&gt')
  3971. hf.write(line)
  3972. lf.close()
  3973. hf.write('</div>\n')
  3974. # add the ftrace log as a hidden div
  3975. if sysvals.ftracelog and sysvals.ftracefile:
  3976. hf.write('<div id="ftracelog" style="display:none;">\n')
  3977. lf = sysvals.openlog(sysvals.ftracefile, 'r')
  3978. for line in lf:
  3979. hf.write(line)
  3980. lf.close()
  3981. hf.write('</div>\n')
  3982. # write the footer and close
  3983. addScriptCode(hf, testruns)
  3984. hf.write('</body>\n</html>\n')
  3985. hf.close()
  3986. return True
  3987. def addCSS(hf, sv, testcount=1, kerror=False, extra=''):
  3988. kernel = sv.stamp['kernel']
  3989. host = sv.hostname[0].upper()+sv.hostname[1:]
  3990. mode = sv.suspendmode
  3991. if sv.suspendmode in suspendmodename:
  3992. mode = suspendmodename[sv.suspendmode]
  3993. title = host+' '+mode+' '+kernel
  3994. # various format changes by flags
  3995. cgchk = 'checked'
  3996. cgnchk = 'not(:checked)'
  3997. if sv.cgexp:
  3998. cgchk = 'not(:checked)'
  3999. cgnchk = 'checked'
  4000. hoverZ = 'z-index:8;'
  4001. if sv.usedevsrc:
  4002. hoverZ = ''
  4003. devlistpos = 'absolute'
  4004. if testcount > 1:
  4005. devlistpos = 'relative'
  4006. scaleTH = 20
  4007. if kerror:
  4008. scaleTH = 60
  4009. # write the html header first (html head, css code, up to body start)
  4010. html_header = '<!DOCTYPE html>\n<html>\n<head>\n\
  4011. <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n\
  4012. <title>'+title+'</title>\n\
  4013. <style type=\'text/css\'>\n\
  4014. body {overflow-y:scroll;}\n\
  4015. .stamp {width:100%;text-align:center;background:gray;line-height:30px;color:white;font:25px Arial;}\n\
  4016. .stamp.sysinfo {font:10px Arial;}\n\
  4017. .callgraph {margin-top:30px;box-shadow:5px 5px 20px black;}\n\
  4018. .callgraph article * {padding-left:28px;}\n\
  4019. h1 {color:black;font:bold 30px Times;}\n\
  4020. t0 {color:black;font:bold 30px Times;}\n\
  4021. t1 {color:black;font:30px Times;}\n\
  4022. t2 {color:black;font:25px Times;}\n\
  4023. t3 {color:black;font:20px Times;white-space:nowrap;}\n\
  4024. t4 {color:black;font:bold 30px Times;line-height:60px;white-space:nowrap;}\n\
  4025. cS {font:bold 13px Times;}\n\
  4026. table {width:100%;}\n\
  4027. .gray {background:rgba(80,80,80,0.1);}\n\
  4028. .green {background:rgba(204,255,204,0.4);}\n\
  4029. .purple {background:rgba(128,0,128,0.2);}\n\
  4030. .yellow {background:rgba(255,255,204,0.4);}\n\
  4031. .blue {background:rgba(169,208,245,0.4);}\n\
  4032. .time1 {font:22px Arial;border:1px solid;}\n\
  4033. .time2 {font:15px Arial;border-bottom:1px solid;border-left:1px solid;border-right:1px solid;}\n\
  4034. .testfail {font:bold 22px Arial;color:red;border:1px dashed;}\n\
  4035. td {text-align:center;}\n\
  4036. r {color:#500000;font:15px Tahoma;}\n\
  4037. n {color:#505050;font:15px Tahoma;}\n\
  4038. .tdhl {color:red;}\n\
  4039. .hide {display:none;}\n\
  4040. .pf {display:none;}\n\
  4041. .pf:'+cgchk+' + label {background:url(\'data:image/svg+xml;utf,<?xml version="1.0" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" height="18" width="18" version="1.1"><circle cx="9" cy="9" r="8" stroke="black" stroke-width="1" fill="white"/><rect x="4" y="8" width="10" height="2" style="fill:black;stroke-width:0"/><rect x="8" y="4" width="2" height="10" style="fill:black;stroke-width:0"/></svg>\') no-repeat left center;}\n\
  4042. .pf:'+cgnchk+' ~ label {background:url(\'data:image/svg+xml;utf,<?xml version="1.0" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" height="18" width="18" version="1.1"><circle cx="9" cy="9" r="8" stroke="black" stroke-width="1" fill="white"/><rect x="4" y="8" width="10" height="2" style="fill:black;stroke-width:0"/></svg>\') no-repeat left center;}\n\
  4043. .pf:'+cgchk+' ~ *:not(:nth-child(2)) {display:none;}\n\
  4044. .zoombox {position:relative;width:100%;overflow-x:scroll;-webkit-user-select:none;-moz-user-select:none;user-select:none;}\n\
  4045. .timeline {position:relative;font-size:14px;cursor:pointer;width:100%; overflow:hidden;background:linear-gradient(#cccccc, white);}\n\
  4046. .thread {position:absolute;height:0%;overflow:hidden;z-index:7;line-height:30px;font-size:14px;border:1px solid;text-align:center;white-space:nowrap;}\n\
  4047. .thread.ps {border-radius:3px;background:linear-gradient(to top, #ccc, #eee);}\n\
  4048. .thread:hover {background:white;border:1px solid red;'+hoverZ+'}\n\
  4049. .thread.sec,.thread.sec:hover {background:black;border:0;color:white;line-height:15px;font-size:10px;}\n\
  4050. .hover {background:white;border:1px solid red;'+hoverZ+'}\n\
  4051. .hover.sync {background:white;}\n\
  4052. .hover.bg,.hover.kth,.hover.sync,.hover.ps {background:white;}\n\
  4053. .jiffie {position:absolute;pointer-events: none;z-index:8;}\n\
  4054. .traceevent {position:absolute;font-size:10px;z-index:7;overflow:hidden;color:black;text-align:center;white-space:nowrap;border-radius:5px;border:1px solid black;background:linear-gradient(to bottom right,#CCC,#969696);}\n\
  4055. .traceevent:hover {color:white;font-weight:bold;border:1px solid white;}\n\
  4056. .phase {position:absolute;overflow:hidden;border:0px;text-align:center;}\n\
  4057. .phaselet {float:left;overflow:hidden;border:0px;text-align:center;min-height:100px;font-size:24px;}\n\
  4058. .t {position:absolute;line-height:'+('%d'%scaleTH)+'px;pointer-events:none;top:0;height:100%;border-right:1px solid black;z-index:6;}\n\
  4059. .err {position:absolute;top:0%;height:100%;border-right:3px solid red;color:red;font:bold 14px Times;line-height:18px;}\n\
  4060. .legend {position:relative; width:100%; height:40px; text-align:center;margin-bottom:20px}\n\
  4061. .legend .square {position:absolute;cursor:pointer;top:10px; width:0px;height:20px;border:1px solid;padding-left:20px;}\n\
  4062. button {height:40px;width:200px;margin-bottom:20px;margin-top:20px;font-size:24px;}\n\
  4063. .btnfmt {position:relative;float:right;height:25px;width:auto;margin-top:3px;margin-bottom:0;font-size:10px;text-align:center;}\n\
  4064. .devlist {position:'+devlistpos+';width:190px;}\n\
  4065. a:link {color:white;text-decoration:none;}\n\
  4066. a:visited {color:white;}\n\
  4067. a:hover {color:white;}\n\
  4068. a:active {color:white;}\n\
  4069. .version {position:relative;float:left;color:white;font-size:10px;line-height:30px;margin-left:10px;}\n\
  4070. #devicedetail {min-height:100px;box-shadow:5px 5px 20px black;}\n\
  4071. .tblock {position:absolute;height:100%;background:#ddd;}\n\
  4072. .tback {position:absolute;width:100%;background:linear-gradient(#ccc, #ddd);}\n\
  4073. .bg {z-index:1;}\n\
  4074. '+extra+'\
  4075. </style>\n</head>\n<body>\n'
  4076. hf.write(html_header)
  4077. # Function: addScriptCode
  4078. # Description:
  4079. # Adds the javascript code to the output html
  4080. # Arguments:
  4081. # hf: the open html file pointer
  4082. # testruns: array of Data objects from parseKernelLog or parseTraceLog
  4083. def addScriptCode(hf, testruns):
  4084. t0 = testruns[0].start * 1000
  4085. tMax = testruns[-1].end * 1000
  4086. # create an array in javascript memory with the device details
  4087. detail = ' var devtable = [];\n'
  4088. for data in testruns:
  4089. topo = data.deviceTopology()
  4090. detail += ' devtable[%d] = "%s";\n' % (data.testnumber, topo)
  4091. detail += ' var bounds = [%f,%f];\n' % (t0, tMax)
  4092. # add the code which will manipulate the data in the browser
  4093. script_code = \
  4094. '<script type="text/javascript">\n'+detail+\
  4095. ' var resolution = -1;\n'\
  4096. ' var dragval = [0, 0];\n'\
  4097. ' function redrawTimescale(t0, tMax, tS) {\n'\
  4098. ' var rline = \'<div class="t" style="left:0;border-left:1px solid black;border-right:0;">\';\n'\
  4099. ' var tTotal = tMax - t0;\n'\
  4100. ' var list = document.getElementsByClassName("tblock");\n'\
  4101. ' for (var i = 0; i < list.length; i++) {\n'\
  4102. ' var timescale = list[i].getElementsByClassName("timescale")[0];\n'\
  4103. ' var m0 = t0 + (tTotal*parseFloat(list[i].style.left)/100);\n'\
  4104. ' var mTotal = tTotal*parseFloat(list[i].style.width)/100;\n'\
  4105. ' var mMax = m0 + mTotal;\n'\
  4106. ' var html = "";\n'\
  4107. ' var divTotal = Math.floor(mTotal/tS) + 1;\n'\
  4108. ' if(divTotal > 1000) continue;\n'\
  4109. ' var divEdge = (mTotal - tS*(divTotal-1))*100/mTotal;\n'\
  4110. ' var pos = 0.0, val = 0.0;\n'\
  4111. ' for (var j = 0; j < divTotal; j++) {\n'\
  4112. ' var htmlline = "";\n'\
  4113. ' var mode = list[i].id[5];\n'\
  4114. ' if(mode == "s") {\n'\
  4115. ' pos = 100 - (((j)*tS*100)/mTotal) - divEdge;\n'\
  4116. ' val = (j-divTotal+1)*tS;\n'\
  4117. ' if(j == divTotal - 1)\n'\
  4118. ' htmlline = \'<div class="t" style="right:\'+pos+\'%"><cS>S&rarr;</cS></div>\';\n'\
  4119. ' else\n'\
  4120. ' htmlline = \'<div class="t" style="right:\'+pos+\'%">\'+val+\'ms</div>\';\n'\
  4121. ' } else {\n'\
  4122. ' pos = 100 - (((j)*tS*100)/mTotal);\n'\
  4123. ' val = (j)*tS;\n'\
  4124. ' htmlline = \'<div class="t" style="right:\'+pos+\'%">\'+val+\'ms</div>\';\n'\
  4125. ' if(j == 0)\n'\
  4126. ' if(mode == "r")\n'\
  4127. ' htmlline = rline+"<cS>&larr;R</cS></div>";\n'\
  4128. ' else\n'\
  4129. ' htmlline = rline+"<cS>0ms</div>";\n'\
  4130. ' }\n'\
  4131. ' html += htmlline;\n'\
  4132. ' }\n'\
  4133. ' timescale.innerHTML = html;\n'\
  4134. ' }\n'\
  4135. ' }\n'\
  4136. ' function zoomTimeline() {\n'\
  4137. ' var dmesg = document.getElementById("dmesg");\n'\
  4138. ' var zoombox = document.getElementById("dmesgzoombox");\n'\
  4139. ' var left = zoombox.scrollLeft;\n'\
  4140. ' var val = parseFloat(dmesg.style.width);\n'\
  4141. ' var newval = 100;\n'\
  4142. ' var sh = window.outerWidth / 2;\n'\
  4143. ' if(this.id == "zoomin") {\n'\
  4144. ' newval = val * 1.2;\n'\
  4145. ' if(newval > 910034) newval = 910034;\n'\
  4146. ' dmesg.style.width = newval+"%";\n'\
  4147. ' zoombox.scrollLeft = ((left + sh) * newval / val) - sh;\n'\
  4148. ' } else if (this.id == "zoomout") {\n'\
  4149. ' newval = val / 1.2;\n'\
  4150. ' if(newval < 100) newval = 100;\n'\
  4151. ' dmesg.style.width = newval+"%";\n'\
  4152. ' zoombox.scrollLeft = ((left + sh) * newval / val) - sh;\n'\
  4153. ' } else {\n'\
  4154. ' zoombox.scrollLeft = 0;\n'\
  4155. ' dmesg.style.width = "100%";\n'\
  4156. ' }\n'\
  4157. ' var tS = [10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1];\n'\
  4158. ' var t0 = bounds[0];\n'\
  4159. ' var tMax = bounds[1];\n'\
  4160. ' var tTotal = tMax - t0;\n'\
  4161. ' var wTotal = tTotal * 100.0 / newval;\n'\
  4162. ' var idx = 7*window.innerWidth/1100;\n'\
  4163. ' for(var i = 0; (i < tS.length)&&((wTotal / tS[i]) < idx); i++);\n'\
  4164. ' if(i >= tS.length) i = tS.length - 1;\n'\
  4165. ' if(tS[i] == resolution) return;\n'\
  4166. ' resolution = tS[i];\n'\
  4167. ' redrawTimescale(t0, tMax, tS[i]);\n'\
  4168. ' }\n'\
  4169. ' function deviceName(title) {\n'\
  4170. ' var name = title.slice(0, title.indexOf(" ("));\n'\
  4171. ' return name;\n'\
  4172. ' }\n'\
  4173. ' function deviceHover() {\n'\
  4174. ' var name = deviceName(this.title);\n'\
  4175. ' var dmesg = document.getElementById("dmesg");\n'\
  4176. ' var dev = dmesg.getElementsByClassName("thread");\n'\
  4177. ' var cpu = -1;\n'\
  4178. ' if(name.match("CPU_ON\[[0-9]*\]"))\n'\
  4179. ' cpu = parseInt(name.slice(7));\n'\
  4180. ' else if(name.match("CPU_OFF\[[0-9]*\]"))\n'\
  4181. ' cpu = parseInt(name.slice(8));\n'\
  4182. ' for (var i = 0; i < dev.length; i++) {\n'\
  4183. ' dname = deviceName(dev[i].title);\n'\
  4184. ' var cname = dev[i].className.slice(dev[i].className.indexOf("thread"));\n'\
  4185. ' if((cpu >= 0 && dname.match("CPU_O[NF]*\\\[*"+cpu+"\\\]")) ||\n'\
  4186. ' (name == dname))\n'\
  4187. ' {\n'\
  4188. ' dev[i].className = "hover "+cname;\n'\
  4189. ' } else {\n'\
  4190. ' dev[i].className = cname;\n'\
  4191. ' }\n'\
  4192. ' }\n'\
  4193. ' }\n'\
  4194. ' function deviceUnhover() {\n'\
  4195. ' var dmesg = document.getElementById("dmesg");\n'\
  4196. ' var dev = dmesg.getElementsByClassName("thread");\n'\
  4197. ' for (var i = 0; i < dev.length; i++) {\n'\
  4198. ' dev[i].className = dev[i].className.slice(dev[i].className.indexOf("thread"));\n'\
  4199. ' }\n'\
  4200. ' }\n'\
  4201. ' function deviceTitle(title, total, cpu) {\n'\
  4202. ' var prefix = "Total";\n'\
  4203. ' if(total.length > 3) {\n'\
  4204. ' prefix = "Average";\n'\
  4205. ' total[1] = (total[1]+total[3])/2;\n'\
  4206. ' total[2] = (total[2]+total[4])/2;\n'\
  4207. ' }\n'\
  4208. ' var devtitle = document.getElementById("devicedetailtitle");\n'\
  4209. ' var name = deviceName(title);\n'\
  4210. ' if(cpu >= 0) name = "CPU"+cpu;\n'\
  4211. ' var driver = "";\n'\
  4212. ' var tS = "<t2>(</t2>";\n'\
  4213. ' var tR = "<t2>)</t2>";\n'\
  4214. ' if(total[1] > 0)\n'\
  4215. ' tS = "<t2>("+prefix+" Suspend:</t2><t0> "+total[1].toFixed(3)+" ms</t0> ";\n'\
  4216. ' if(total[2] > 0)\n'\
  4217. ' tR = " <t2>"+prefix+" Resume:</t2><t0> "+total[2].toFixed(3)+" ms<t2>)</t2></t0>";\n'\
  4218. ' var s = title.indexOf("{");\n'\
  4219. ' var e = title.indexOf("}");\n'\
  4220. ' if((s >= 0) && (e >= 0))\n'\
  4221. ' driver = title.slice(s+1, e) + " <t1>@</t1> ";\n'\
  4222. ' if(total[1] > 0 && total[2] > 0)\n'\
  4223. ' devtitle.innerHTML = "<t0>"+driver+name+"</t0> "+tS+tR;\n'\
  4224. ' else\n'\
  4225. ' devtitle.innerHTML = "<t0>"+title+"</t0>";\n'\
  4226. ' return name;\n'\
  4227. ' }\n'\
  4228. ' function deviceDetail() {\n'\
  4229. ' var devinfo = document.getElementById("devicedetail");\n'\
  4230. ' devinfo.style.display = "block";\n'\
  4231. ' var name = deviceName(this.title);\n'\
  4232. ' var cpu = -1;\n'\
  4233. ' if(name.match("CPU_ON\[[0-9]*\]"))\n'\
  4234. ' cpu = parseInt(name.slice(7));\n'\
  4235. ' else if(name.match("CPU_OFF\[[0-9]*\]"))\n'\
  4236. ' cpu = parseInt(name.slice(8));\n'\
  4237. ' var dmesg = document.getElementById("dmesg");\n'\
  4238. ' var dev = dmesg.getElementsByClassName("thread");\n'\
  4239. ' var idlist = [];\n'\
  4240. ' var pdata = [[]];\n'\
  4241. ' if(document.getElementById("devicedetail1"))\n'\
  4242. ' pdata = [[], []];\n'\
  4243. ' var pd = pdata[0];\n'\
  4244. ' var total = [0.0, 0.0, 0.0];\n'\
  4245. ' for (var i = 0; i < dev.length; i++) {\n'\
  4246. ' dname = deviceName(dev[i].title);\n'\
  4247. ' if((cpu >= 0 && dname.match("CPU_O[NF]*\\\[*"+cpu+"\\\]")) ||\n'\
  4248. ' (name == dname))\n'\
  4249. ' {\n'\
  4250. ' idlist[idlist.length] = dev[i].id;\n'\
  4251. ' var tidx = 1;\n'\
  4252. ' if(dev[i].id[0] == "a") {\n'\
  4253. ' pd = pdata[0];\n'\
  4254. ' } else {\n'\
  4255. ' if(pdata.length == 1) pdata[1] = [];\n'\
  4256. ' if(total.length == 3) total[3]=total[4]=0.0;\n'\
  4257. ' pd = pdata[1];\n'\
  4258. ' tidx = 3;\n'\
  4259. ' }\n'\
  4260. ' var info = dev[i].title.split(" ");\n'\
  4261. ' var pname = info[info.length-1];\n'\
  4262. ' pd[pname] = parseFloat(info[info.length-3].slice(1));\n'\
  4263. ' total[0] += pd[pname];\n'\
  4264. ' if(pname.indexOf("suspend") >= 0)\n'\
  4265. ' total[tidx] += pd[pname];\n'\
  4266. ' else\n'\
  4267. ' total[tidx+1] += pd[pname];\n'\
  4268. ' }\n'\
  4269. ' }\n'\
  4270. ' var devname = deviceTitle(this.title, total, cpu);\n'\
  4271. ' var left = 0.0;\n'\
  4272. ' for (var t = 0; t < pdata.length; t++) {\n'\
  4273. ' pd = pdata[t];\n'\
  4274. ' devinfo = document.getElementById("devicedetail"+t);\n'\
  4275. ' var phases = devinfo.getElementsByClassName("phaselet");\n'\
  4276. ' for (var i = 0; i < phases.length; i++) {\n'\
  4277. ' if(phases[i].id in pd) {\n'\
  4278. ' var w = 100.0*pd[phases[i].id]/total[0];\n'\
  4279. ' var fs = 32;\n'\
  4280. ' if(w < 8) fs = 4*w | 0;\n'\
  4281. ' var fs2 = fs*3/4;\n'\
  4282. ' phases[i].style.width = w+"%";\n'\
  4283. ' phases[i].style.left = left+"%";\n'\
  4284. ' phases[i].title = phases[i].id+" "+pd[phases[i].id]+" ms";\n'\
  4285. ' left += w;\n'\
  4286. ' var time = "<t4 style=\\"font-size:"+fs+"px\\">"+pd[phases[i].id]+" ms<br></t4>";\n'\
  4287. ' var pname = "<t3 style=\\"font-size:"+fs2+"px\\">"+phases[i].id.replace(new RegExp("_", "g"), " ")+"</t3>";\n'\
  4288. ' phases[i].innerHTML = time+pname;\n'\
  4289. ' } else {\n'\
  4290. ' phases[i].style.width = "0%";\n'\
  4291. ' phases[i].style.left = left+"%";\n'\
  4292. ' }\n'\
  4293. ' }\n'\
  4294. ' }\n'\
  4295. ' if(typeof devstats !== \'undefined\')\n'\
  4296. ' callDetail(this.id, this.title);\n'\
  4297. ' var cglist = document.getElementById("callgraphs");\n'\
  4298. ' if(!cglist) return;\n'\
  4299. ' var cg = cglist.getElementsByClassName("atop");\n'\
  4300. ' if(cg.length < 10) return;\n'\
  4301. ' for (var i = 0; i < cg.length; i++) {\n'\
  4302. ' cgid = cg[i].id.split("x")[0]\n'\
  4303. ' if(idlist.indexOf(cgid) >= 0) {\n'\
  4304. ' cg[i].style.display = "block";\n'\
  4305. ' } else {\n'\
  4306. ' cg[i].style.display = "none";\n'\
  4307. ' }\n'\
  4308. ' }\n'\
  4309. ' }\n'\
  4310. ' function callDetail(devid, devtitle) {\n'\
  4311. ' if(!(devid in devstats) || devstats[devid].length < 1)\n'\
  4312. ' return;\n'\
  4313. ' var list = devstats[devid];\n'\
  4314. ' var tmp = devtitle.split(" ");\n'\
  4315. ' var name = tmp[0], phase = tmp[tmp.length-1];\n'\
  4316. ' var dd = document.getElementById(phase);\n'\
  4317. ' var total = parseFloat(tmp[1].slice(1));\n'\
  4318. ' var mlist = [];\n'\
  4319. ' var maxlen = 0;\n'\
  4320. ' var info = []\n'\
  4321. ' for(var i in list) {\n'\
  4322. ' if(list[i][0] == "@") {\n'\
  4323. ' info = list[i].split("|");\n'\
  4324. ' continue;\n'\
  4325. ' }\n'\
  4326. ' var tmp = list[i].split("|");\n'\
  4327. ' var t = parseFloat(tmp[0]), f = tmp[1], c = parseInt(tmp[2]);\n'\
  4328. ' var p = (t*100.0/total).toFixed(2);\n'\
  4329. ' mlist[mlist.length] = [f, c, t.toFixed(2), p+"%"];\n'\
  4330. ' if(f.length > maxlen)\n'\
  4331. ' maxlen = f.length;\n'\
  4332. ' }\n'\
  4333. ' var pad = 5;\n'\
  4334. ' if(mlist.length == 0) pad = 30;\n'\
  4335. ' var html = \'<div style="padding-top:\'+pad+\'px"><t3> <b>\'+name+\':</b>\';\n'\
  4336. ' if(info.length > 2)\n'\
  4337. ' html += " start=<b>"+info[1]+"</b>, end=<b>"+info[2]+"</b>";\n'\
  4338. ' if(info.length > 3)\n'\
  4339. ' html += ", length<i>(w/o overhead)</i>=<b>"+info[3]+" ms</b>";\n'\
  4340. ' if(info.length > 4)\n'\
  4341. ' html += ", return=<b>"+info[4]+"</b>";\n'\
  4342. ' html += "</t3></div>";\n'\
  4343. ' if(mlist.length > 0) {\n'\
  4344. ' html += \'<table class=fstat style="padding-top:\'+(maxlen*5)+\'px;"><tr><th>Function</th>\';\n'\
  4345. ' for(var i in mlist)\n'\
  4346. ' html += "<td class=vt>"+mlist[i][0]+"</td>";\n'\
  4347. ' html += "</tr><tr><th>Calls</th>";\n'\
  4348. ' for(var i in mlist)\n'\
  4349. ' html += "<td>"+mlist[i][1]+"</td>";\n'\
  4350. ' html += "</tr><tr><th>Time(ms)</th>";\n'\
  4351. ' for(var i in mlist)\n'\
  4352. ' html += "<td>"+mlist[i][2]+"</td>";\n'\
  4353. ' html += "</tr><tr><th>Percent</th>";\n'\
  4354. ' for(var i in mlist)\n'\
  4355. ' html += "<td>"+mlist[i][3]+"</td>";\n'\
  4356. ' html += "</tr></table>";\n'\
  4357. ' }\n'\
  4358. ' dd.innerHTML = html;\n'\
  4359. ' var height = (maxlen*5)+100;\n'\
  4360. ' dd.style.height = height+"px";\n'\
  4361. ' document.getElementById("devicedetail").style.height = height+"px";\n'\
  4362. ' }\n'\
  4363. ' function callSelect() {\n'\
  4364. ' var cglist = document.getElementById("callgraphs");\n'\
  4365. ' if(!cglist) return;\n'\
  4366. ' var cg = cglist.getElementsByClassName("atop");\n'\
  4367. ' for (var i = 0; i < cg.length; i++) {\n'\
  4368. ' if(this.id == cg[i].id) {\n'\
  4369. ' cg[i].style.display = "block";\n'\
  4370. ' } else {\n'\
  4371. ' cg[i].style.display = "none";\n'\
  4372. ' }\n'\
  4373. ' }\n'\
  4374. ' }\n'\
  4375. ' function devListWindow(e) {\n'\
  4376. ' var win = window.open();\n'\
  4377. ' var html = "<title>"+e.target.innerHTML+"</title>"+\n'\
  4378. ' "<style type=\\"text/css\\">"+\n'\
  4379. ' " ul {list-style-type:circle;padding-left:10px;margin-left:10px;}"+\n'\
  4380. ' "</style>"\n'\
  4381. ' var dt = devtable[0];\n'\
  4382. ' if(e.target.id != "devlist1")\n'\
  4383. ' dt = devtable[1];\n'\
  4384. ' win.document.write(html+dt);\n'\
  4385. ' }\n'\
  4386. ' function errWindow() {\n'\
  4387. ' var range = this.id.split("_");\n'\
  4388. ' var idx1 = parseInt(range[0]);\n'\
  4389. ' var idx2 = parseInt(range[1]);\n'\
  4390. ' var win = window.open();\n'\
  4391. ' var log = document.getElementById("dmesglog");\n'\
  4392. ' var title = "<title>dmesg log</title>";\n'\
  4393. ' var text = log.innerHTML.split("\\n");\n'\
  4394. ' var html = "";\n'\
  4395. ' for(var i = 0; i < text.length; i++) {\n'\
  4396. ' if(i == idx1) {\n'\
  4397. ' html += "<e id=target>"+text[i]+"</e>\\n";\n'\
  4398. ' } else if(i > idx1 && i <= idx2) {\n'\
  4399. ' html += "<e>"+text[i]+"</e>\\n";\n'\
  4400. ' } else {\n'\
  4401. ' html += text[i]+"\\n";\n'\
  4402. ' }\n'\
  4403. ' }\n'\
  4404. ' win.document.write("<style>e{color:red}</style>"+title+"<pre>"+html+"</pre>");\n'\
  4405. ' win.location.hash = "#target";\n'\
  4406. ' win.document.close();\n'\
  4407. ' }\n'\
  4408. ' function logWindow(e) {\n'\
  4409. ' var name = e.target.id.slice(4);\n'\
  4410. ' var win = window.open();\n'\
  4411. ' var log = document.getElementById(name+"log");\n'\
  4412. ' var title = "<title>"+document.title.split(" ")[0]+" "+name+" log</title>";\n'\
  4413. ' win.document.write(title+"<pre>"+log.innerHTML+"</pre>");\n'\
  4414. ' win.document.close();\n'\
  4415. ' }\n'\
  4416. ' function onMouseDown(e) {\n'\
  4417. ' dragval[0] = e.clientX;\n'\
  4418. ' dragval[1] = document.getElementById("dmesgzoombox").scrollLeft;\n'\
  4419. ' document.onmousemove = onMouseMove;\n'\
  4420. ' }\n'\
  4421. ' function onMouseMove(e) {\n'\
  4422. ' var zoombox = document.getElementById("dmesgzoombox");\n'\
  4423. ' zoombox.scrollLeft = dragval[1] + dragval[0] - e.clientX;\n'\
  4424. ' }\n'\
  4425. ' function onMouseUp(e) {\n'\
  4426. ' document.onmousemove = null;\n'\
  4427. ' }\n'\
  4428. ' function onKeyPress(e) {\n'\
  4429. ' var c = e.charCode;\n'\
  4430. ' if(c != 42 && c != 43 && c != 45) return;\n'\
  4431. ' var click = document.createEvent("Events");\n'\
  4432. ' click.initEvent("click", true, false);\n'\
  4433. ' if(c == 43) \n'\
  4434. ' document.getElementById("zoomin").dispatchEvent(click);\n'\
  4435. ' else if(c == 45)\n'\
  4436. ' document.getElementById("zoomout").dispatchEvent(click);\n'\
  4437. ' else if(c == 42)\n'\
  4438. ' document.getElementById("zoomdef").dispatchEvent(click);\n'\
  4439. ' }\n'\
  4440. ' window.addEventListener("resize", function () {zoomTimeline();});\n'\
  4441. ' window.addEventListener("load", function () {\n'\
  4442. ' var dmesg = document.getElementById("dmesg");\n'\
  4443. ' dmesg.style.width = "100%"\n'\
  4444. ' dmesg.onmousedown = onMouseDown;\n'\
  4445. ' document.onmouseup = onMouseUp;\n'\
  4446. ' document.onkeypress = onKeyPress;\n'\
  4447. ' document.getElementById("zoomin").onclick = zoomTimeline;\n'\
  4448. ' document.getElementById("zoomout").onclick = zoomTimeline;\n'\
  4449. ' document.getElementById("zoomdef").onclick = zoomTimeline;\n'\
  4450. ' var list = document.getElementsByClassName("err");\n'\
  4451. ' for (var i = 0; i < list.length; i++)\n'\
  4452. ' list[i].onclick = errWindow;\n'\
  4453. ' var list = document.getElementsByClassName("logbtn");\n'\
  4454. ' for (var i = 0; i < list.length; i++)\n'\
  4455. ' list[i].onclick = logWindow;\n'\
  4456. ' list = document.getElementsByClassName("devlist");\n'\
  4457. ' for (var i = 0; i < list.length; i++)\n'\
  4458. ' list[i].onclick = devListWindow;\n'\
  4459. ' var dev = dmesg.getElementsByClassName("thread");\n'\
  4460. ' for (var i = 0; i < dev.length; i++) {\n'\
  4461. ' dev[i].onclick = deviceDetail;\n'\
  4462. ' dev[i].onmouseover = deviceHover;\n'\
  4463. ' dev[i].onmouseout = deviceUnhover;\n'\
  4464. ' }\n'\
  4465. ' var dev = dmesg.getElementsByClassName("srccall");\n'\
  4466. ' for (var i = 0; i < dev.length; i++)\n'\
  4467. ' dev[i].onclick = callSelect;\n'\
  4468. ' zoomTimeline();\n'\
  4469. ' });\n'\
  4470. '</script>\n'
  4471. hf.write(script_code);
  4472. def setRuntimeSuspend(before=True):
  4473. global sysvals
  4474. sv = sysvals
  4475. if sv.rs == 0:
  4476. return
  4477. if before:
  4478. # runtime suspend disable or enable
  4479. if sv.rs > 0:
  4480. sv.rstgt, sv.rsval, sv.rsdir = 'on', 'auto', 'enabled'
  4481. else:
  4482. sv.rstgt, sv.rsval, sv.rsdir = 'auto', 'on', 'disabled'
  4483. pprint('CONFIGURING RUNTIME SUSPEND...')
  4484. sv.rslist = deviceInfo(sv.rstgt)
  4485. for i in sv.rslist:
  4486. sv.setVal(sv.rsval, i)
  4487. pprint('runtime suspend %s on all devices (%d changed)' % (sv.rsdir, len(sv.rslist)))
  4488. pprint('waiting 5 seconds...')
  4489. time.sleep(5)
  4490. else:
  4491. # runtime suspend re-enable or re-disable
  4492. for i in sv.rslist:
  4493. sv.setVal(sv.rstgt, i)
  4494. pprint('runtime suspend settings restored on %d devices' % len(sv.rslist))
  4495. # Function: executeSuspend
  4496. # Description:
  4497. # Execute system suspend through the sysfs interface, then copy the output
  4498. # dmesg and ftrace files to the test output directory.
  4499. def executeSuspend():
  4500. pm = ProcessMonitor()
  4501. tp = sysvals.tpath
  4502. testdata = []
  4503. battery = True if getBattery() else False
  4504. # run these commands to prepare the system for suspend
  4505. if sysvals.display:
  4506. pprint('SET DISPLAY TO %s' % sysvals.display.upper())
  4507. displayControl(sysvals.display)
  4508. time.sleep(1)
  4509. if sysvals.sync:
  4510. pprint('SYNCING FILESYSTEMS')
  4511. call('sync', shell=True)
  4512. # mark the start point in the kernel ring buffer just as we start
  4513. sysvals.initdmesg()
  4514. # start ftrace
  4515. if(sysvals.usecallgraph or sysvals.usetraceevents):
  4516. pprint('START TRACING')
  4517. sysvals.fsetVal('1', 'tracing_on')
  4518. if sysvals.useprocmon:
  4519. pm.start()
  4520. # execute however many s/r runs requested
  4521. for count in range(1,sysvals.execcount+1):
  4522. # x2delay in between test runs
  4523. if(count > 1 and sysvals.x2delay > 0):
  4524. sysvals.fsetVal('WAIT %d' % sysvals.x2delay, 'trace_marker')
  4525. time.sleep(sysvals.x2delay/1000.0)
  4526. sysvals.fsetVal('WAIT END', 'trace_marker')
  4527. # start message
  4528. if sysvals.testcommand != '':
  4529. pprint('COMMAND START')
  4530. else:
  4531. if(sysvals.rtcwake):
  4532. pprint('SUSPEND START')
  4533. else:
  4534. pprint('SUSPEND START (press a key to resume)')
  4535. bat1 = getBattery() if battery else False
  4536. # set rtcwake
  4537. if(sysvals.rtcwake):
  4538. pprint('will issue an rtcwake in %d seconds' % sysvals.rtcwaketime)
  4539. sysvals.rtcWakeAlarmOn()
  4540. # start of suspend trace marker
  4541. if(sysvals.usecallgraph or sysvals.usetraceevents):
  4542. sysvals.fsetVal('SUSPEND START', 'trace_marker')
  4543. # predelay delay
  4544. if(count == 1 and sysvals.predelay > 0):
  4545. sysvals.fsetVal('WAIT %d' % sysvals.predelay, 'trace_marker')
  4546. time.sleep(sysvals.predelay/1000.0)
  4547. sysvals.fsetVal('WAIT END', 'trace_marker')
  4548. # initiate suspend or command
  4549. tdata = {'error': ''}
  4550. if sysvals.testcommand != '':
  4551. res = call(sysvals.testcommand+' 2>&1', shell=True);
  4552. if res != 0:
  4553. tdata['error'] = 'cmd returned %d' % res
  4554. else:
  4555. mode = sysvals.suspendmode
  4556. if sysvals.memmode and os.path.exists(sysvals.mempowerfile):
  4557. mode = 'mem'
  4558. pf = open(sysvals.mempowerfile, 'w')
  4559. pf.write(sysvals.memmode)
  4560. pf.close()
  4561. if sysvals.diskmode and os.path.exists(sysvals.diskpowerfile):
  4562. mode = 'disk'
  4563. pf = open(sysvals.diskpowerfile, 'w')
  4564. pf.write(sysvals.diskmode)
  4565. pf.close()
  4566. pf = open(sysvals.powerfile, 'w')
  4567. pf.write(mode)
  4568. # execution will pause here
  4569. try:
  4570. pf.close()
  4571. except Exception as e:
  4572. tdata['error'] = str(e)
  4573. if(sysvals.rtcwake):
  4574. sysvals.rtcWakeAlarmOff()
  4575. # postdelay delay
  4576. if(count == sysvals.execcount and sysvals.postdelay > 0):
  4577. sysvals.fsetVal('WAIT %d' % sysvals.postdelay, 'trace_marker')
  4578. time.sleep(sysvals.postdelay/1000.0)
  4579. sysvals.fsetVal('WAIT END', 'trace_marker')
  4580. # return from suspend
  4581. pprint('RESUME COMPLETE')
  4582. if(sysvals.usecallgraph or sysvals.usetraceevents):
  4583. sysvals.fsetVal('RESUME COMPLETE', 'trace_marker')
  4584. if(sysvals.suspendmode == 'mem' or sysvals.suspendmode == 'command'):
  4585. tdata['fw'] = getFPDT(False)
  4586. bat2 = getBattery() if battery else False
  4587. if battery and bat1 and bat2:
  4588. tdata['bat'] = (bat1, bat2)
  4589. testdata.append(tdata)
  4590. # stop ftrace
  4591. if(sysvals.usecallgraph or sysvals.usetraceevents):
  4592. if sysvals.useprocmon:
  4593. pm.stop()
  4594. sysvals.fsetVal('0', 'tracing_on')
  4595. # grab a copy of the dmesg output
  4596. pprint('CAPTURING DMESG')
  4597. sysvals.getdmesg(testdata)
  4598. # grab a copy of the ftrace output
  4599. if(sysvals.usecallgraph or sysvals.usetraceevents):
  4600. pprint('CAPTURING TRACE')
  4601. op = sysvals.writeDatafileHeader(sysvals.ftracefile, testdata)
  4602. fp = open(tp+'trace', 'r')
  4603. for line in fp:
  4604. op.write(line)
  4605. op.close()
  4606. sysvals.fsetVal('', 'trace')
  4607. devProps()
  4608. def readFile(file):
  4609. if os.path.islink(file):
  4610. return os.readlink(file).split('/')[-1]
  4611. else:
  4612. return sysvals.getVal(file).strip()
  4613. # Function: ms2nice
  4614. # Description:
  4615. # Print out a very concise time string in minutes and seconds
  4616. # Output:
  4617. # The time string, e.g. "1901m16s"
  4618. def ms2nice(val):
  4619. val = int(val)
  4620. h = val / 3600000
  4621. m = (val / 60000) % 60
  4622. s = (val / 1000) % 60
  4623. if h > 0:
  4624. return '%d:%02d:%02d' % (h, m, s)
  4625. if m > 0:
  4626. return '%02d:%02d' % (m, s)
  4627. return '%ds' % s
  4628. def yesno(val):
  4629. list = {'enabled':'A', 'disabled':'S', 'auto':'E', 'on':'D',
  4630. 'active':'A', 'suspended':'S', 'suspending':'S'}
  4631. if val not in list:
  4632. return ' '
  4633. return list[val]
  4634. # Function: deviceInfo
  4635. # Description:
  4636. # Detect all the USB hosts and devices currently connected and add
  4637. # a list of USB device names to sysvals for better timeline readability
  4638. def deviceInfo(output=''):
  4639. if not output:
  4640. pprint('LEGEND\n'\
  4641. '---------------------------------------------------------------------------------------------\n'\
  4642. ' A = async/sync PM queue (A/S) C = runtime active children\n'\
  4643. ' R = runtime suspend enabled/disabled (E/D) rACTIVE = runtime active (min/sec)\n'\
  4644. ' S = runtime status active/suspended (A/S) rSUSPEND = runtime suspend (min/sec)\n'\
  4645. ' U = runtime usage count\n'\
  4646. '---------------------------------------------------------------------------------------------\n'\
  4647. 'DEVICE NAME A R S U C rACTIVE rSUSPEND\n'\
  4648. '---------------------------------------------------------------------------------------------')
  4649. res = []
  4650. tgtval = 'runtime_status'
  4651. lines = dict()
  4652. for dirname, dirnames, filenames in os.walk('/sys/devices'):
  4653. if(not re.match('.*/power', dirname) or
  4654. 'control' not in filenames or
  4655. tgtval not in filenames):
  4656. continue
  4657. name = ''
  4658. dirname = dirname[:-6]
  4659. device = dirname.split('/')[-1]
  4660. power = dict()
  4661. power[tgtval] = readFile('%s/power/%s' % (dirname, tgtval))
  4662. # only list devices which support runtime suspend
  4663. if power[tgtval] not in ['active', 'suspended', 'suspending']:
  4664. continue
  4665. for i in ['product', 'driver', 'subsystem']:
  4666. file = '%s/%s' % (dirname, i)
  4667. if os.path.exists(file):
  4668. name = readFile(file)
  4669. break
  4670. for i in ['async', 'control', 'runtime_status', 'runtime_usage',
  4671. 'runtime_active_kids', 'runtime_active_time',
  4672. 'runtime_suspended_time']:
  4673. if i in filenames:
  4674. power[i] = readFile('%s/power/%s' % (dirname, i))
  4675. if output:
  4676. if power['control'] == output:
  4677. res.append('%s/power/control' % dirname)
  4678. continue
  4679. lines[dirname] = '%-26s %-26s %1s %1s %1s %1s %1s %10s %10s' % \
  4680. (device[:26], name[:26],
  4681. yesno(power['async']), \
  4682. yesno(power['control']), \
  4683. yesno(power['runtime_status']), \
  4684. power['runtime_usage'], \
  4685. power['runtime_active_kids'], \
  4686. ms2nice(power['runtime_active_time']), \
  4687. ms2nice(power['runtime_suspended_time']))
  4688. for i in sorted(lines):
  4689. print lines[i]
  4690. return res
  4691. # Function: devProps
  4692. # Description:
  4693. # Retrieve a list of properties for all devices in the trace log
  4694. def devProps(data=0):
  4695. props = dict()
  4696. if data:
  4697. idx = data.index(': ') + 2
  4698. if idx >= len(data):
  4699. return
  4700. devlist = data[idx:].split(';')
  4701. for dev in devlist:
  4702. f = dev.split(',')
  4703. if len(f) < 3:
  4704. continue
  4705. dev = f[0]
  4706. props[dev] = DevProps()
  4707. props[dev].altname = f[1]
  4708. if int(f[2]):
  4709. props[dev].async = True
  4710. else:
  4711. props[dev].async = False
  4712. sysvals.devprops = props
  4713. if sysvals.suspendmode == 'command' and 'testcommandstring' in props:
  4714. sysvals.testcommand = props['testcommandstring'].altname
  4715. return
  4716. if(os.path.exists(sysvals.ftracefile) == False):
  4717. doError('%s does not exist' % sysvals.ftracefile)
  4718. # first get the list of devices we need properties for
  4719. msghead = 'Additional data added by AnalyzeSuspend'
  4720. alreadystamped = False
  4721. tp = TestProps()
  4722. tf = sysvals.openlog(sysvals.ftracefile, 'r')
  4723. for line in tf:
  4724. if msghead in line:
  4725. alreadystamped = True
  4726. continue
  4727. # determine the trace data type (required for further parsing)
  4728. m = re.match(tp.tracertypefmt, line)
  4729. if(m):
  4730. tp.setTracerType(m.group('t'))
  4731. continue
  4732. # parse only valid lines, if this is not one move on
  4733. m = re.match(tp.ftrace_line_fmt, line)
  4734. if(not m or 'device_pm_callback_start' not in line):
  4735. continue
  4736. m = re.match('.*: (?P<drv>.*) (?P<d>.*), parent: *(?P<p>.*), .*', m.group('msg'));
  4737. if(not m):
  4738. continue
  4739. dev = m.group('d')
  4740. if dev not in props:
  4741. props[dev] = DevProps()
  4742. tf.close()
  4743. if not alreadystamped and sysvals.suspendmode == 'command':
  4744. out = '#\n# '+msghead+'\n# Device Properties: '
  4745. out += 'testcommandstring,%s,0;' % (sysvals.testcommand)
  4746. with sysvals.openlog(sysvals.ftracefile, 'a') as fp:
  4747. fp.write(out+'\n')
  4748. sysvals.devprops = props
  4749. return
  4750. # now get the syspath for each of our target devices
  4751. for dirname, dirnames, filenames in os.walk('/sys/devices'):
  4752. if(re.match('.*/power', dirname) and 'async' in filenames):
  4753. dev = dirname.split('/')[-2]
  4754. if dev in props and (not props[dev].syspath or len(dirname) < len(props[dev].syspath)):
  4755. props[dev].syspath = dirname[:-6]
  4756. # now fill in the properties for our target devices
  4757. for dev in props:
  4758. dirname = props[dev].syspath
  4759. if not dirname or not os.path.exists(dirname):
  4760. continue
  4761. with open(dirname+'/power/async') as fp:
  4762. text = fp.read()
  4763. props[dev].async = False
  4764. if 'enabled' in text:
  4765. props[dev].async = True
  4766. fields = os.listdir(dirname)
  4767. if 'product' in fields:
  4768. with open(dirname+'/product') as fp:
  4769. props[dev].altname = fp.read()
  4770. elif 'name' in fields:
  4771. with open(dirname+'/name') as fp:
  4772. props[dev].altname = fp.read()
  4773. elif 'model' in fields:
  4774. with open(dirname+'/model') as fp:
  4775. props[dev].altname = fp.read()
  4776. elif 'description' in fields:
  4777. with open(dirname+'/description') as fp:
  4778. props[dev].altname = fp.read()
  4779. elif 'id' in fields:
  4780. with open(dirname+'/id') as fp:
  4781. props[dev].altname = fp.read()
  4782. elif 'idVendor' in fields and 'idProduct' in fields:
  4783. idv, idp = '', ''
  4784. with open(dirname+'/idVendor') as fp:
  4785. idv = fp.read().strip()
  4786. with open(dirname+'/idProduct') as fp:
  4787. idp = fp.read().strip()
  4788. props[dev].altname = '%s:%s' % (idv, idp)
  4789. if props[dev].altname:
  4790. out = props[dev].altname.strip().replace('\n', ' ')
  4791. out = out.replace(',', ' ')
  4792. out = out.replace(';', ' ')
  4793. props[dev].altname = out
  4794. # and now write the data to the ftrace file
  4795. if not alreadystamped:
  4796. out = '#\n# '+msghead+'\n# Device Properties: '
  4797. for dev in sorted(props):
  4798. out += props[dev].out(dev)
  4799. with sysvals.openlog(sysvals.ftracefile, 'a') as fp:
  4800. fp.write(out+'\n')
  4801. sysvals.devprops = props
  4802. # Function: getModes
  4803. # Description:
  4804. # Determine the supported power modes on this system
  4805. # Output:
  4806. # A string list of the available modes
  4807. def getModes():
  4808. modes = []
  4809. if(os.path.exists(sysvals.powerfile)):
  4810. fp = open(sysvals.powerfile, 'r')
  4811. modes = string.split(fp.read())
  4812. fp.close()
  4813. if(os.path.exists(sysvals.mempowerfile)):
  4814. deep = False
  4815. fp = open(sysvals.mempowerfile, 'r')
  4816. for m in string.split(fp.read()):
  4817. memmode = m.strip('[]')
  4818. if memmode == 'deep':
  4819. deep = True
  4820. else:
  4821. modes.append('mem-%s' % memmode)
  4822. fp.close()
  4823. if 'mem' in modes and not deep:
  4824. modes.remove('mem')
  4825. if('disk' in modes and os.path.exists(sysvals.diskpowerfile)):
  4826. fp = open(sysvals.diskpowerfile, 'r')
  4827. for m in string.split(fp.read()):
  4828. modes.append('disk-%s' % m.strip('[]'))
  4829. fp.close()
  4830. return modes
  4831. # Function: dmidecode
  4832. # Description:
  4833. # Read the bios tables and pull out system info
  4834. # Arguments:
  4835. # mempath: /dev/mem or custom mem path
  4836. # fatal: True to exit on error, False to return empty dict
  4837. # Output:
  4838. # A dict object with all available key/values
  4839. def dmidecode(mempath, fatal=False):
  4840. out = dict()
  4841. # the list of values to retrieve, with hardcoded (type, idx)
  4842. info = {
  4843. 'bios-vendor': (0, 4),
  4844. 'bios-version': (0, 5),
  4845. 'bios-release-date': (0, 8),
  4846. 'system-manufacturer': (1, 4),
  4847. 'system-product-name': (1, 5),
  4848. 'system-version': (1, 6),
  4849. 'system-serial-number': (1, 7),
  4850. 'baseboard-manufacturer': (2, 4),
  4851. 'baseboard-product-name': (2, 5),
  4852. 'baseboard-version': (2, 6),
  4853. 'baseboard-serial-number': (2, 7),
  4854. 'chassis-manufacturer': (3, 4),
  4855. 'chassis-type': (3, 5),
  4856. 'chassis-version': (3, 6),
  4857. 'chassis-serial-number': (3, 7),
  4858. 'processor-manufacturer': (4, 7),
  4859. 'processor-version': (4, 16),
  4860. }
  4861. if(not os.path.exists(mempath)):
  4862. if(fatal):
  4863. doError('file does not exist: %s' % mempath)
  4864. return out
  4865. if(not os.access(mempath, os.R_OK)):
  4866. if(fatal):
  4867. doError('file is not readable: %s' % mempath)
  4868. return out
  4869. # by default use legacy scan, but try to use EFI first
  4870. memaddr = 0xf0000
  4871. memsize = 0x10000
  4872. for ep in ['/sys/firmware/efi/systab', '/proc/efi/systab']:
  4873. if not os.path.exists(ep) or not os.access(ep, os.R_OK):
  4874. continue
  4875. fp = open(ep, 'r')
  4876. buf = fp.read()
  4877. fp.close()
  4878. i = buf.find('SMBIOS=')
  4879. if i >= 0:
  4880. try:
  4881. memaddr = int(buf[i+7:], 16)
  4882. memsize = 0x20
  4883. except:
  4884. continue
  4885. # read in the memory for scanning
  4886. fp = open(mempath, 'rb')
  4887. try:
  4888. fp.seek(memaddr)
  4889. buf = fp.read(memsize)
  4890. except:
  4891. if(fatal):
  4892. doError('DMI table is unreachable, sorry')
  4893. else:
  4894. return out
  4895. fp.close()
  4896. # search for either an SM table or DMI table
  4897. i = base = length = num = 0
  4898. while(i < memsize):
  4899. if buf[i:i+4] == '_SM_' and i < memsize - 16:
  4900. length = struct.unpack('H', buf[i+22:i+24])[0]
  4901. base, num = struct.unpack('IH', buf[i+24:i+30])
  4902. break
  4903. elif buf[i:i+5] == '_DMI_':
  4904. length = struct.unpack('H', buf[i+6:i+8])[0]
  4905. base, num = struct.unpack('IH', buf[i+8:i+14])
  4906. break
  4907. i += 16
  4908. if base == 0 and length == 0 and num == 0:
  4909. if(fatal):
  4910. doError('Neither SMBIOS nor DMI were found')
  4911. else:
  4912. return out
  4913. # read in the SM or DMI table
  4914. fp = open(mempath, 'rb')
  4915. try:
  4916. fp.seek(base)
  4917. buf = fp.read(length)
  4918. except:
  4919. if(fatal):
  4920. doError('DMI table is unreachable, sorry')
  4921. else:
  4922. return out
  4923. fp.close()
  4924. # scan the table for the values we want
  4925. count = i = 0
  4926. while(count < num and i <= len(buf) - 4):
  4927. type, size, handle = struct.unpack('BBH', buf[i:i+4])
  4928. n = i + size
  4929. while n < len(buf) - 1:
  4930. if 0 == struct.unpack('H', buf[n:n+2])[0]:
  4931. break
  4932. n += 1
  4933. data = buf[i+size:n+2].split('\0')
  4934. for name in info:
  4935. itype, idxadr = info[name]
  4936. if itype == type:
  4937. idx = struct.unpack('B', buf[i+idxadr])[0]
  4938. if idx > 0 and idx < len(data) - 1:
  4939. s = data[idx-1].strip()
  4940. if s and s.lower() != 'to be filled by o.e.m.':
  4941. out[name] = data[idx-1]
  4942. i = n + 2
  4943. count += 1
  4944. return out
  4945. def getBattery():
  4946. p, charge, bat = '/sys/class/power_supply', 0, {}
  4947. if not os.path.exists(p):
  4948. return False
  4949. for d in os.listdir(p):
  4950. type = sysvals.getVal(os.path.join(p, d, 'type')).strip().lower()
  4951. if type != 'battery':
  4952. continue
  4953. for v in ['status', 'energy_now', 'capacity_now']:
  4954. bat[v] = sysvals.getVal(os.path.join(p, d, v)).strip().lower()
  4955. break
  4956. if 'status' not in bat:
  4957. return False
  4958. ac = False if 'discharging' in bat['status'] else True
  4959. for v in ['energy_now', 'capacity_now']:
  4960. if v in bat and bat[v]:
  4961. charge = int(bat[v])
  4962. return (ac, charge)
  4963. def displayControl(cmd):
  4964. xset, ret = 'xset -d :0.0 {0}', 0
  4965. if sysvals.sudouser:
  4966. xset = 'sudo -u %s %s' % (sysvals.sudouser, xset)
  4967. if cmd == 'init':
  4968. ret = call(xset.format('dpms 0 0 0'), shell=True)
  4969. if not ret:
  4970. ret = call(xset.format('s off'), shell=True)
  4971. elif cmd == 'reset':
  4972. ret = call(xset.format('s reset'), shell=True)
  4973. elif cmd in ['on', 'off', 'standby', 'suspend']:
  4974. b4 = displayControl('stat')
  4975. ret = call(xset.format('dpms force %s' % cmd), shell=True)
  4976. if not ret:
  4977. curr = displayControl('stat')
  4978. sysvals.vprint('Display Switched: %s -> %s' % (b4, curr))
  4979. if curr != cmd:
  4980. sysvals.vprint('WARNING: Display failed to change to %s' % cmd)
  4981. if ret:
  4982. sysvals.vprint('WARNING: Display failed to change to %s with xset' % cmd)
  4983. return ret
  4984. elif cmd == 'stat':
  4985. fp = Popen(xset.format('q').split(' '), stdout=PIPE).stdout
  4986. ret = 'unknown'
  4987. for line in fp:
  4988. m = re.match('[\s]*Monitor is (?P<m>.*)', line)
  4989. if(m and len(m.group('m')) >= 2):
  4990. out = m.group('m').lower()
  4991. ret = out[3:] if out[0:2] == 'in' else out
  4992. break
  4993. fp.close()
  4994. return ret
  4995. # Function: getFPDT
  4996. # Description:
  4997. # Read the acpi bios tables and pull out FPDT, the firmware data
  4998. # Arguments:
  4999. # output: True to output the info to stdout, False otherwise
  5000. def getFPDT(output):
  5001. rectype = {}
  5002. rectype[0] = 'Firmware Basic Boot Performance Record'
  5003. rectype[1] = 'S3 Performance Table Record'
  5004. prectype = {}
  5005. prectype[0] = 'Basic S3 Resume Performance Record'
  5006. prectype[1] = 'Basic S3 Suspend Performance Record'
  5007. sysvals.rootCheck(True)
  5008. if(not os.path.exists(sysvals.fpdtpath)):
  5009. if(output):
  5010. doError('file does not exist: %s' % sysvals.fpdtpath)
  5011. return False
  5012. if(not os.access(sysvals.fpdtpath, os.R_OK)):
  5013. if(output):
  5014. doError('file is not readable: %s' % sysvals.fpdtpath)
  5015. return False
  5016. if(not os.path.exists(sysvals.mempath)):
  5017. if(output):
  5018. doError('file does not exist: %s' % sysvals.mempath)
  5019. return False
  5020. if(not os.access(sysvals.mempath, os.R_OK)):
  5021. if(output):
  5022. doError('file is not readable: %s' % sysvals.mempath)
  5023. return False
  5024. fp = open(sysvals.fpdtpath, 'rb')
  5025. buf = fp.read()
  5026. fp.close()
  5027. if(len(buf) < 36):
  5028. if(output):
  5029. doError('Invalid FPDT table data, should '+\
  5030. 'be at least 36 bytes')
  5031. return False
  5032. table = struct.unpack('4sIBB6s8sI4sI', buf[0:36])
  5033. if(output):
  5034. pprint('\n'\
  5035. 'Firmware Performance Data Table (%s)\n'\
  5036. ' Signature : %s\n'\
  5037. ' Table Length : %u\n'\
  5038. ' Revision : %u\n'\
  5039. ' Checksum : 0x%x\n'\
  5040. ' OEM ID : %s\n'\
  5041. ' OEM Table ID : %s\n'\
  5042. ' OEM Revision : %u\n'\
  5043. ' Creator ID : %s\n'\
  5044. ' Creator Revision : 0x%x\n'\
  5045. '' % (table[0], table[0], table[1], table[2], table[3],
  5046. table[4], table[5], table[6], table[7], table[8]))
  5047. if(table[0] != 'FPDT'):
  5048. if(output):
  5049. doError('Invalid FPDT table')
  5050. return False
  5051. if(len(buf) <= 36):
  5052. return False
  5053. i = 0
  5054. fwData = [0, 0]
  5055. records = buf[36:]
  5056. fp = open(sysvals.mempath, 'rb')
  5057. while(i < len(records)):
  5058. header = struct.unpack('HBB', records[i:i+4])
  5059. if(header[0] not in rectype):
  5060. i += header[1]
  5061. continue
  5062. if(header[1] != 16):
  5063. i += header[1]
  5064. continue
  5065. addr = struct.unpack('Q', records[i+8:i+16])[0]
  5066. try:
  5067. fp.seek(addr)
  5068. first = fp.read(8)
  5069. except:
  5070. if(output):
  5071. pprint('Bad address 0x%x in %s' % (addr, sysvals.mempath))
  5072. return [0, 0]
  5073. rechead = struct.unpack('4sI', first)
  5074. recdata = fp.read(rechead[1]-8)
  5075. if(rechead[0] == 'FBPT'):
  5076. record = struct.unpack('HBBIQQQQQ', recdata)
  5077. if(output):
  5078. pprint('%s (%s)\n'\
  5079. ' Reset END : %u ns\n'\
  5080. ' OS Loader LoadImage Start : %u ns\n'\
  5081. ' OS Loader StartImage Start : %u ns\n'\
  5082. ' ExitBootServices Entry : %u ns\n'\
  5083. ' ExitBootServices Exit : %u ns'\
  5084. '' % (rectype[header[0]], rechead[0], record[4], record[5],
  5085. record[6], record[7], record[8]))
  5086. elif(rechead[0] == 'S3PT'):
  5087. if(output):
  5088. pprint('%s (%s)' % (rectype[header[0]], rechead[0]))
  5089. j = 0
  5090. while(j < len(recdata)):
  5091. prechead = struct.unpack('HBB', recdata[j:j+4])
  5092. if(prechead[0] not in prectype):
  5093. continue
  5094. if(prechead[0] == 0):
  5095. record = struct.unpack('IIQQ', recdata[j:j+prechead[1]])
  5096. fwData[1] = record[2]
  5097. if(output):
  5098. pprint(' %s\n'\
  5099. ' Resume Count : %u\n'\
  5100. ' FullResume : %u ns\n'\
  5101. ' AverageResume : %u ns'\
  5102. '' % (prectype[prechead[0]], record[1],
  5103. record[2], record[3]))
  5104. elif(prechead[0] == 1):
  5105. record = struct.unpack('QQ', recdata[j+4:j+prechead[1]])
  5106. fwData[0] = record[1] - record[0]
  5107. if(output):
  5108. pprint(' %s\n'\
  5109. ' SuspendStart : %u ns\n'\
  5110. ' SuspendEnd : %u ns\n'\
  5111. ' SuspendTime : %u ns'\
  5112. '' % (prectype[prechead[0]], record[0],
  5113. record[1], fwData[0]))
  5114. j += prechead[1]
  5115. if(output):
  5116. pprint('')
  5117. i += header[1]
  5118. fp.close()
  5119. return fwData
  5120. # Function: statusCheck
  5121. # Description:
  5122. # Verify that the requested command and options will work, and
  5123. # print the results to the terminal
  5124. # Output:
  5125. # True if the test will work, False if not
  5126. def statusCheck(probecheck=False):
  5127. status = ''
  5128. pprint('Checking this system (%s)...' % platform.node())
  5129. # check we have root access
  5130. res = sysvals.colorText('NO (No features of this tool will work!)')
  5131. if(sysvals.rootCheck(False)):
  5132. res = 'YES'
  5133. pprint(' have root access: %s' % res)
  5134. if(res != 'YES'):
  5135. pprint(' Try running this script with sudo')
  5136. return 'missing root access'
  5137. # check sysfs is mounted
  5138. res = sysvals.colorText('NO (No features of this tool will work!)')
  5139. if(os.path.exists(sysvals.powerfile)):
  5140. res = 'YES'
  5141. pprint(' is sysfs mounted: %s' % res)
  5142. if(res != 'YES'):
  5143. return 'sysfs is missing'
  5144. # check target mode is a valid mode
  5145. if sysvals.suspendmode != 'command':
  5146. res = sysvals.colorText('NO')
  5147. modes = getModes()
  5148. if(sysvals.suspendmode in modes):
  5149. res = 'YES'
  5150. else:
  5151. status = '%s mode is not supported' % sysvals.suspendmode
  5152. pprint(' is "%s" a valid power mode: %s' % (sysvals.suspendmode, res))
  5153. if(res == 'NO'):
  5154. pprint(' valid power modes are: %s' % modes)
  5155. pprint(' please choose one with -m')
  5156. # check if ftrace is available
  5157. res = sysvals.colorText('NO')
  5158. ftgood = sysvals.verifyFtrace()
  5159. if(ftgood):
  5160. res = 'YES'
  5161. elif(sysvals.usecallgraph):
  5162. status = 'ftrace is not properly supported'
  5163. pprint(' is ftrace supported: %s' % res)
  5164. # check if kprobes are available
  5165. res = sysvals.colorText('NO')
  5166. sysvals.usekprobes = sysvals.verifyKprobes()
  5167. if(sysvals.usekprobes):
  5168. res = 'YES'
  5169. else:
  5170. sysvals.usedevsrc = False
  5171. pprint(' are kprobes supported: %s' % res)
  5172. # what data source are we using
  5173. res = 'DMESG'
  5174. if(ftgood):
  5175. sysvals.usetraceevents = True
  5176. for e in sysvals.traceevents:
  5177. if not os.path.exists(sysvals.epath+e):
  5178. sysvals.usetraceevents = False
  5179. if(sysvals.usetraceevents):
  5180. res = 'FTRACE (all trace events found)'
  5181. pprint(' timeline data source: %s' % res)
  5182. # check if rtcwake
  5183. res = sysvals.colorText('NO')
  5184. if(sysvals.rtcpath != ''):
  5185. res = 'YES'
  5186. elif(sysvals.rtcwake):
  5187. status = 'rtcwake is not properly supported'
  5188. pprint(' is rtcwake supported: %s' % res)
  5189. if not probecheck:
  5190. return status
  5191. # verify kprobes
  5192. if sysvals.usekprobes:
  5193. for name in sysvals.tracefuncs:
  5194. sysvals.defaultKprobe(name, sysvals.tracefuncs[name])
  5195. if sysvals.usedevsrc:
  5196. for name in sysvals.dev_tracefuncs:
  5197. sysvals.defaultKprobe(name, sysvals.dev_tracefuncs[name])
  5198. sysvals.addKprobes(True)
  5199. return status
  5200. # Function: doError
  5201. # Description:
  5202. # generic error function for catastrphic failures
  5203. # Arguments:
  5204. # msg: the error message to print
  5205. # help: True if printHelp should be called after, False otherwise
  5206. def doError(msg, help=False):
  5207. if(help == True):
  5208. printHelp()
  5209. pprint('ERROR: %s\n' % msg)
  5210. sysvals.outputResult({'error':msg})
  5211. sys.exit(1)
  5212. # Function: getArgInt
  5213. # Description:
  5214. # pull out an integer argument from the command line with checks
  5215. def getArgInt(name, args, min, max, main=True):
  5216. if main:
  5217. try:
  5218. arg = args.next()
  5219. except:
  5220. doError(name+': no argument supplied', True)
  5221. else:
  5222. arg = args
  5223. try:
  5224. val = int(arg)
  5225. except:
  5226. doError(name+': non-integer value given', True)
  5227. if(val < min or val > max):
  5228. doError(name+': value should be between %d and %d' % (min, max), True)
  5229. return val
  5230. # Function: getArgFloat
  5231. # Description:
  5232. # pull out a float argument from the command line with checks
  5233. def getArgFloat(name, args, min, max, main=True):
  5234. if main:
  5235. try:
  5236. arg = args.next()
  5237. except:
  5238. doError(name+': no argument supplied', True)
  5239. else:
  5240. arg = args
  5241. try:
  5242. val = float(arg)
  5243. except:
  5244. doError(name+': non-numerical value given', True)
  5245. if(val < min or val > max):
  5246. doError(name+': value should be between %f and %f' % (min, max), True)
  5247. return val
  5248. def processData(live=False):
  5249. pprint('PROCESSING DATA')
  5250. error = ''
  5251. if(sysvals.usetraceevents):
  5252. testruns, error = parseTraceLog(live)
  5253. if sysvals.dmesgfile:
  5254. for data in testruns:
  5255. data.extractErrorInfo()
  5256. else:
  5257. testruns = loadKernelLog()
  5258. for data in testruns:
  5259. parseKernelLog(data)
  5260. if(sysvals.ftracefile and (sysvals.usecallgraph or sysvals.usetraceevents)):
  5261. appendIncompleteTraceLog(testruns)
  5262. sysvals.vprint('Command:\n %s' % sysvals.cmdline)
  5263. for data in testruns:
  5264. if data.battery:
  5265. a1, c1, a2, c2 = data.battery
  5266. s = 'Battery:\n Before - AC: %s, Charge: %d\n After - AC: %s, Charge: %d' % \
  5267. (a1, int(c1), a2, int(c2))
  5268. sysvals.vprint(s)
  5269. data.printDetails()
  5270. if sysvals.cgdump:
  5271. for data in testruns:
  5272. data.debugPrint()
  5273. sys.exit(0)
  5274. if len(testruns) < 1:
  5275. pprint('ERROR: Not enough test data to build a timeline')
  5276. return (testruns, {'error': 'timeline generation failed'})
  5277. sysvals.vprint('Creating the html timeline (%s)...' % sysvals.htmlfile)
  5278. createHTML(testruns, error)
  5279. pprint('DONE')
  5280. data = testruns[0]
  5281. stamp = data.stamp
  5282. stamp['suspend'], stamp['resume'] = data.getTimeValues()
  5283. if data.fwValid:
  5284. stamp['fwsuspend'], stamp['fwresume'] = data.fwSuspend, data.fwResume
  5285. if error:
  5286. stamp['error'] = error
  5287. return (testruns, stamp)
  5288. # Function: rerunTest
  5289. # Description:
  5290. # generate an output from an existing set of ftrace/dmesg logs
  5291. def rerunTest():
  5292. if sysvals.ftracefile:
  5293. doesTraceLogHaveTraceEvents()
  5294. if not sysvals.dmesgfile and not sysvals.usetraceevents:
  5295. doError('recreating this html output requires a dmesg file')
  5296. sysvals.setOutputFile()
  5297. if os.path.exists(sysvals.htmlfile):
  5298. if not os.path.isfile(sysvals.htmlfile):
  5299. doError('a directory already exists with this name: %s' % sysvals.htmlfile)
  5300. elif not os.access(sysvals.htmlfile, os.W_OK):
  5301. doError('missing permission to write to %s' % sysvals.htmlfile)
  5302. testruns, stamp = processData(False)
  5303. sysvals.logmsg = ''
  5304. return stamp
  5305. # Function: runTest
  5306. # Description:
  5307. # execute a suspend/resume, gather the logs, and generate the output
  5308. def runTest(n=0):
  5309. # prepare for the test
  5310. sysvals.initFtrace()
  5311. sysvals.initTestOutput('suspend')
  5312. # execute the test
  5313. executeSuspend()
  5314. sysvals.cleanupFtrace()
  5315. if sysvals.skiphtml:
  5316. sysvals.sudoUserchown(sysvals.testdir)
  5317. return
  5318. testruns, stamp = processData(True)
  5319. for data in testruns:
  5320. del data
  5321. sysvals.sudoUserchown(sysvals.testdir)
  5322. sysvals.outputResult(stamp, n)
  5323. if 'error' in stamp:
  5324. return 2
  5325. return 0
  5326. def find_in_html(html, start, end, firstonly=True):
  5327. n, out = 0, []
  5328. while n < len(html):
  5329. m = re.search(start, html[n:])
  5330. if not m:
  5331. break
  5332. i = m.end()
  5333. m = re.search(end, html[n+i:])
  5334. if not m:
  5335. break
  5336. j = m.start()
  5337. str = html[n+i:n+i+j]
  5338. if end == 'ms':
  5339. num = re.search(r'[-+]?\d*\.\d+|\d+', str)
  5340. str = num.group() if num else 'NaN'
  5341. if firstonly:
  5342. return str
  5343. out.append(str)
  5344. n += i+j
  5345. if firstonly:
  5346. return ''
  5347. return out
  5348. def data_from_html(file, outpath, devlist=False):
  5349. html = open(file, 'r').read()
  5350. suspend = find_in_html(html, 'Kernel Suspend', 'ms')
  5351. resume = find_in_html(html, 'Kernel Resume', 'ms')
  5352. line = find_in_html(html, '<div class="stamp">', '</div>')
  5353. stmp = line.split()
  5354. if not suspend or not resume or len(stmp) != 8:
  5355. return False
  5356. try:
  5357. dt = datetime.strptime(' '.join(stmp[3:]), '%B %d %Y, %I:%M:%S %p')
  5358. except:
  5359. return False
  5360. tstr = dt.strftime('%Y/%m/%d %H:%M:%S')
  5361. error = find_in_html(html, '<table class="testfail"><tr><td>', '</td>')
  5362. if error:
  5363. m = re.match('[a-z]* failed in (?P<p>[a-z0-9_]*) phase', error)
  5364. if m:
  5365. result = 'fail in %s' % m.group('p')
  5366. else:
  5367. result = 'fail'
  5368. else:
  5369. result = 'pass'
  5370. ilist = []
  5371. e = find_in_html(html, 'class="err"[\w=":;\.%\- ]*>', '&rarr;</div>', False)
  5372. for i in list(set(e)):
  5373. ilist.append('%sx%d' % (i, e.count(i)) if e.count(i) > 1 else i)
  5374. low = find_in_html(html, 'freeze time: <b>', ' ms</b>')
  5375. if low and '|' in low:
  5376. ilist.append('FREEZEx%d' % len(low.split('|')))
  5377. devices = dict()
  5378. for line in html.split('\n'):
  5379. m = re.match(' *<div id=\"[a,0-9]*\" *title=\"(?P<title>.*)\" class=\"thread.*', line)
  5380. if not m or 'thread kth' in line or 'thread sec' in line:
  5381. continue
  5382. m = re.match('(?P<n>.*) \((?P<t>[0-9,\.]*) ms\) (?P<p>.*)', m.group('title'))
  5383. if not m:
  5384. continue
  5385. name, time, phase = m.group('n'), m.group('t'), m.group('p')
  5386. if ' async' in name or ' sync' in name:
  5387. name = ' '.join(name.split(' ')[:-1])
  5388. d = phase.split('_')[0]
  5389. if d not in devices:
  5390. devices[d] = dict()
  5391. if name not in devices[d]:
  5392. devices[d][name] = 0.0
  5393. devices[d][name] += float(time)
  5394. worst = {'suspend': {'name':'', 'time': 0.0},
  5395. 'resume': {'name':'', 'time': 0.0}}
  5396. for d in devices:
  5397. if d not in worst:
  5398. worst[d] = dict()
  5399. dev = devices[d]
  5400. if len(dev.keys()) > 0:
  5401. n = sorted(dev, key=dev.get, reverse=True)[0]
  5402. worst[d]['name'], worst[d]['time'] = n, dev[n]
  5403. data = {
  5404. 'mode': stmp[2],
  5405. 'host': stmp[0],
  5406. 'kernel': stmp[1],
  5407. 'time': tstr,
  5408. 'result': result,
  5409. 'issues': ' '.join(ilist),
  5410. 'suspend': suspend,
  5411. 'resume': resume,
  5412. 'sus_worst': worst['suspend']['name'],
  5413. 'sus_worsttime': worst['suspend']['time'],
  5414. 'res_worst': worst['resume']['name'],
  5415. 'res_worsttime': worst['resume']['time'],
  5416. 'url': os.path.relpath(file, outpath),
  5417. }
  5418. if devlist:
  5419. data['devlist'] = devices
  5420. return data
  5421. # Function: runSummary
  5422. # Description:
  5423. # create a summary of tests in a sub-directory
  5424. def runSummary(subdir, local=True, genhtml=False):
  5425. inpath = os.path.abspath(subdir)
  5426. outpath = os.path.abspath('.') if local else inpath
  5427. pprint('Generating a summary of folder "%s"' % inpath)
  5428. if genhtml:
  5429. for dirname, dirnames, filenames in os.walk(subdir):
  5430. sysvals.dmesgfile = sysvals.ftracefile = sysvals.htmlfile = ''
  5431. for filename in filenames:
  5432. if(re.match('.*_dmesg.txt', filename)):
  5433. sysvals.dmesgfile = os.path.join(dirname, filename)
  5434. elif(re.match('.*_ftrace.txt', filename)):
  5435. sysvals.ftracefile = os.path.join(dirname, filename)
  5436. sysvals.setOutputFile()
  5437. if sysvals.ftracefile and sysvals.htmlfile and \
  5438. not os.path.exists(sysvals.htmlfile):
  5439. pprint('FTRACE: %s' % sysvals.ftracefile)
  5440. if sysvals.dmesgfile:
  5441. pprint('DMESG : %s' % sysvals.dmesgfile)
  5442. rerunTest()
  5443. testruns = []
  5444. desc = {'host':[],'mode':[],'kernel':[]}
  5445. for dirname, dirnames, filenames in os.walk(subdir):
  5446. for filename in filenames:
  5447. if(not re.match('.*.html', filename)):
  5448. continue
  5449. data = data_from_html(os.path.join(dirname, filename), outpath)
  5450. if(not data):
  5451. continue
  5452. testruns.append(data)
  5453. for key in desc:
  5454. if data[key] not in desc[key]:
  5455. desc[key].append(data[key])
  5456. outfile = os.path.join(outpath, 'summary.html')
  5457. pprint('Summary file: %s' % outfile)
  5458. if len(desc['host']) == len(desc['mode']) == len(desc['kernel']) == 1:
  5459. title = '%s %s %s' % (desc['host'][0], desc['kernel'][0], desc['mode'][0])
  5460. else:
  5461. title = inpath
  5462. createHTMLSummarySimple(testruns, outfile, title)
  5463. # Function: checkArgBool
  5464. # Description:
  5465. # check if a boolean string value is true or false
  5466. def checkArgBool(name, value):
  5467. if value in switchvalues:
  5468. if value in switchoff:
  5469. return False
  5470. return True
  5471. doError('invalid boolean --> (%s: %s), use "true/false" or "1/0"' % (name, value), True)
  5472. return False
  5473. # Function: configFromFile
  5474. # Description:
  5475. # Configure the script via the info in a config file
  5476. def configFromFile(file):
  5477. Config = ConfigParser.ConfigParser()
  5478. Config.read(file)
  5479. sections = Config.sections()
  5480. overridekprobes = False
  5481. overridedevkprobes = False
  5482. if 'Settings' in sections:
  5483. for opt in Config.options('Settings'):
  5484. value = Config.get('Settings', opt).lower()
  5485. option = opt.lower()
  5486. if(option == 'verbose'):
  5487. sysvals.verbose = checkArgBool(option, value)
  5488. elif(option == 'addlogs'):
  5489. sysvals.dmesglog = sysvals.ftracelog = checkArgBool(option, value)
  5490. elif(option == 'dev'):
  5491. sysvals.usedevsrc = checkArgBool(option, value)
  5492. elif(option == 'proc'):
  5493. sysvals.useprocmon = checkArgBool(option, value)
  5494. elif(option == 'x2'):
  5495. if checkArgBool(option, value):
  5496. sysvals.execcount = 2
  5497. elif(option == 'callgraph'):
  5498. sysvals.usecallgraph = checkArgBool(option, value)
  5499. elif(option == 'override-timeline-functions'):
  5500. overridekprobes = checkArgBool(option, value)
  5501. elif(option == 'override-dev-timeline-functions'):
  5502. overridedevkprobes = checkArgBool(option, value)
  5503. elif(option == 'skiphtml'):
  5504. sysvals.skiphtml = checkArgBool(option, value)
  5505. elif(option == 'sync'):
  5506. sysvals.sync = checkArgBool(option, value)
  5507. elif(option == 'rs' or option == 'runtimesuspend'):
  5508. if value in switchvalues:
  5509. if value in switchoff:
  5510. sysvals.rs = -1
  5511. else:
  5512. sysvals.rs = 1
  5513. else:
  5514. doError('invalid value --> (%s: %s), use "enable/disable"' % (option, value), True)
  5515. elif(option == 'display'):
  5516. disopt = ['on', 'off', 'standby', 'suspend']
  5517. if value not in disopt:
  5518. doError('invalid value --> (%s: %s), use %s' % (option, value, disopt), True)
  5519. sysvals.display = value
  5520. elif(option == 'gzip'):
  5521. sysvals.gzip = checkArgBool(option, value)
  5522. elif(option == 'cgfilter'):
  5523. sysvals.setCallgraphFilter(value)
  5524. elif(option == 'cgskip'):
  5525. if value in switchoff:
  5526. sysvals.cgskip = ''
  5527. else:
  5528. sysvals.cgskip = sysvals.configFile(val)
  5529. if(not sysvals.cgskip):
  5530. doError('%s does not exist' % sysvals.cgskip)
  5531. elif(option == 'cgtest'):
  5532. sysvals.cgtest = getArgInt('cgtest', value, 0, 1, False)
  5533. elif(option == 'cgphase'):
  5534. d = Data(0)
  5535. if value not in d.sortedPhases():
  5536. doError('invalid phase --> (%s: %s), valid phases are %s'\
  5537. % (option, value, d.sortedPhases()), True)
  5538. sysvals.cgphase = value
  5539. elif(option == 'fadd'):
  5540. file = sysvals.configFile(value)
  5541. if(not file):
  5542. doError('%s does not exist' % value)
  5543. sysvals.addFtraceFilterFunctions(file)
  5544. elif(option == 'result'):
  5545. sysvals.result = value
  5546. elif(option == 'multi'):
  5547. nums = value.split()
  5548. if len(nums) != 2:
  5549. doError('multi requires 2 integers (exec_count and delay)', True)
  5550. sysvals.multitest['run'] = True
  5551. sysvals.multitest['count'] = getArgInt('multi: n d (exec count)', nums[0], 2, 1000000, False)
  5552. sysvals.multitest['delay'] = getArgInt('multi: n d (delay between tests)', nums[1], 0, 3600, False)
  5553. elif(option == 'devicefilter'):
  5554. sysvals.setDeviceFilter(value)
  5555. elif(option == 'expandcg'):
  5556. sysvals.cgexp = checkArgBool(option, value)
  5557. elif(option == 'srgap'):
  5558. if checkArgBool(option, value):
  5559. sysvals.srgap = 5
  5560. elif(option == 'mode'):
  5561. sysvals.suspendmode = value
  5562. elif(option == 'command' or option == 'cmd'):
  5563. sysvals.testcommand = value
  5564. elif(option == 'x2delay'):
  5565. sysvals.x2delay = getArgInt('x2delay', value, 0, 60000, False)
  5566. elif(option == 'predelay'):
  5567. sysvals.predelay = getArgInt('predelay', value, 0, 60000, False)
  5568. elif(option == 'postdelay'):
  5569. sysvals.postdelay = getArgInt('postdelay', value, 0, 60000, False)
  5570. elif(option == 'maxdepth'):
  5571. sysvals.max_graph_depth = getArgInt('maxdepth', value, 0, 1000, False)
  5572. elif(option == 'rtcwake'):
  5573. if value in switchoff:
  5574. sysvals.rtcwake = False
  5575. else:
  5576. sysvals.rtcwake = True
  5577. sysvals.rtcwaketime = getArgInt('rtcwake', value, 0, 3600, False)
  5578. elif(option == 'timeprec'):
  5579. sysvals.setPrecision(getArgInt('timeprec', value, 0, 6, False))
  5580. elif(option == 'mindev'):
  5581. sysvals.mindevlen = getArgFloat('mindev', value, 0.0, 10000.0, False)
  5582. elif(option == 'callloop-maxgap'):
  5583. sysvals.callloopmaxgap = getArgFloat('callloop-maxgap', value, 0.0, 1.0, False)
  5584. elif(option == 'callloop-maxlen'):
  5585. sysvals.callloopmaxgap = getArgFloat('callloop-maxlen', value, 0.0, 1.0, False)
  5586. elif(option == 'mincg'):
  5587. sysvals.mincglen = getArgFloat('mincg', value, 0.0, 10000.0, False)
  5588. elif(option == 'bufsize'):
  5589. sysvals.bufsize = getArgInt('bufsize', value, 1, 1024*1024*8, False)
  5590. elif(option == 'output-dir'):
  5591. sysvals.outdir = sysvals.setOutputFolder(value)
  5592. if sysvals.suspendmode == 'command' and not sysvals.testcommand:
  5593. doError('No command supplied for mode "command"')
  5594. # compatibility errors
  5595. if sysvals.usedevsrc and sysvals.usecallgraph:
  5596. doError('-dev is not compatible with -f')
  5597. if sysvals.usecallgraph and sysvals.useprocmon:
  5598. doError('-proc is not compatible with -f')
  5599. if overridekprobes:
  5600. sysvals.tracefuncs = dict()
  5601. if overridedevkprobes:
  5602. sysvals.dev_tracefuncs = dict()
  5603. kprobes = dict()
  5604. kprobesec = 'dev_timeline_functions_'+platform.machine()
  5605. if kprobesec in sections:
  5606. for name in Config.options(kprobesec):
  5607. text = Config.get(kprobesec, name)
  5608. kprobes[name] = (text, True)
  5609. kprobesec = 'timeline_functions_'+platform.machine()
  5610. if kprobesec in sections:
  5611. for name in Config.options(kprobesec):
  5612. if name in kprobes:
  5613. doError('Duplicate timeline function found "%s"' % (name))
  5614. text = Config.get(kprobesec, name)
  5615. kprobes[name] = (text, False)
  5616. for name in kprobes:
  5617. function = name
  5618. format = name
  5619. color = ''
  5620. args = dict()
  5621. text, dev = kprobes[name]
  5622. data = text.split()
  5623. i = 0
  5624. for val in data:
  5625. # bracketted strings are special formatting, read them separately
  5626. if val[0] == '[' and val[-1] == ']':
  5627. for prop in val[1:-1].split(','):
  5628. p = prop.split('=')
  5629. if p[0] == 'color':
  5630. try:
  5631. color = int(p[1], 16)
  5632. color = '#'+p[1]
  5633. except:
  5634. color = p[1]
  5635. continue
  5636. # first real arg should be the format string
  5637. if i == 0:
  5638. format = val
  5639. # all other args are actual function args
  5640. else:
  5641. d = val.split('=')
  5642. args[d[0]] = d[1]
  5643. i += 1
  5644. if not function or not format:
  5645. doError('Invalid kprobe: %s' % name)
  5646. for arg in re.findall('{(?P<n>[a-z,A-Z,0-9]*)}', format):
  5647. if arg not in args:
  5648. doError('Kprobe "%s" is missing argument "%s"' % (name, arg))
  5649. if (dev and name in sysvals.dev_tracefuncs) or (not dev and name in sysvals.tracefuncs):
  5650. doError('Duplicate timeline function found "%s"' % (name))
  5651. kp = {
  5652. 'name': name,
  5653. 'func': function,
  5654. 'format': format,
  5655. sysvals.archargs: args
  5656. }
  5657. if color:
  5658. kp['color'] = color
  5659. if dev:
  5660. sysvals.dev_tracefuncs[name] = kp
  5661. else:
  5662. sysvals.tracefuncs[name] = kp
  5663. # Function: printHelp
  5664. # Description:
  5665. # print out the help text
  5666. def printHelp():
  5667. pprint('\n%s v%s\n'\
  5668. 'Usage: sudo sleepgraph <options> <commands>\n'\
  5669. '\n'\
  5670. 'Description:\n'\
  5671. ' This tool is designed to assist kernel and OS developers in optimizing\n'\
  5672. ' their linux stack\'s suspend/resume time. Using a kernel image built\n'\
  5673. ' with a few extra options enabled, the tool will execute a suspend and\n'\
  5674. ' capture dmesg and ftrace data until resume is complete. This data is\n'\
  5675. ' transformed into a device timeline and an optional callgraph to give\n'\
  5676. ' a detailed view of which devices/subsystems are taking the most\n'\
  5677. ' time in suspend/resume.\n'\
  5678. '\n'\
  5679. ' If no specific command is given, the default behavior is to initiate\n'\
  5680. ' a suspend/resume and capture the dmesg/ftrace output as an html timeline.\n'\
  5681. '\n'\
  5682. ' Generates output files in subdirectory: suspend-yymmdd-HHMMSS\n'\
  5683. ' HTML output: <hostname>_<mode>.html\n'\
  5684. ' raw dmesg output: <hostname>_<mode>_dmesg.txt\n'\
  5685. ' raw ftrace output: <hostname>_<mode>_ftrace.txt\n'\
  5686. '\n'\
  5687. 'Options:\n'\
  5688. ' -h Print this help text\n'\
  5689. ' -v Print the current tool version\n'\
  5690. ' -config fn Pull arguments and config options from file fn\n'\
  5691. ' -verbose Print extra information during execution and analysis\n'\
  5692. ' -m mode Mode to initiate for suspend (default: %s)\n'\
  5693. ' -o name Overrides the output subdirectory name when running a new test\n'\
  5694. ' default: suspend-{date}-{time}\n'\
  5695. ' -rtcwake t Wakeup t seconds after suspend, set t to "off" to disable (default: 15)\n'\
  5696. ' -addlogs Add the dmesg and ftrace logs to the html output\n'\
  5697. ' -srgap Add a visible gap in the timeline between sus/res (default: disabled)\n'\
  5698. ' -skiphtml Run the test and capture the trace logs, but skip the timeline (default: disabled)\n'\
  5699. ' -result fn Export a results table to a text file for parsing.\n'\
  5700. ' [testprep]\n'\
  5701. ' -sync Sync the filesystems before starting the test\n'\
  5702. ' -rs on/off Enable/disable runtime suspend for all devices, restore all after test\n'\
  5703. ' -display m Change the display mode to m for the test (on/off/standby/suspend)\n'\
  5704. ' [advanced]\n'\
  5705. ' -gzip Gzip the trace and dmesg logs to save space\n'\
  5706. ' -cmd {s} Run the timeline over a custom command, e.g. "sync -d"\n'\
  5707. ' -proc Add usermode process info into the timeline (default: disabled)\n'\
  5708. ' -dev Add kernel function calls and threads to the timeline (default: disabled)\n'\
  5709. ' -x2 Run two suspend/resumes back to back (default: disabled)\n'\
  5710. ' -x2delay t Include t ms delay between multiple test runs (default: 0 ms)\n'\
  5711. ' -predelay t Include t ms delay before 1st suspend (default: 0 ms)\n'\
  5712. ' -postdelay t Include t ms delay after last resume (default: 0 ms)\n'\
  5713. ' -mindev ms Discard all device blocks shorter than ms milliseconds (e.g. 0.001 for us)\n'\
  5714. ' -multi n d Execute <n> consecutive tests at <d> seconds intervals. The outputs will\n'\
  5715. ' be created in a new subdirectory with a summary page.\n'\
  5716. ' [debug]\n'\
  5717. ' -f Use ftrace to create device callgraphs (default: disabled)\n'\
  5718. ' -maxdepth N limit the callgraph data to N call levels (default: 0=all)\n'\
  5719. ' -expandcg pre-expand the callgraph data in the html output (default: disabled)\n'\
  5720. ' -fadd file Add functions to be graphed in the timeline from a list in a text file\n'\
  5721. ' -filter "d1,d2,..." Filter out all but this comma-delimited list of device names\n'\
  5722. ' -mincg ms Discard all callgraphs shorter than ms milliseconds (e.g. 0.001 for us)\n'\
  5723. ' -cgphase P Only show callgraph data for phase P (e.g. suspend_late)\n'\
  5724. ' -cgtest N Only show callgraph data for test N (e.g. 0 or 1 in an x2 run)\n'\
  5725. ' -timeprec N Number of significant digits in timestamps (0:S, [3:ms], 6:us)\n'\
  5726. ' -cgfilter S Filter the callgraph output in the timeline\n'\
  5727. ' -cgskip file Callgraph functions to skip, off to disable (default: cgskip.txt)\n'\
  5728. ' -bufsize N Set trace buffer size to N kilo-bytes (default: all of free memory)\n'\
  5729. ' -devdump Print out all the raw device data for each phase\n'\
  5730. ' -cgdump Print out all the raw callgraph data\n'\
  5731. '\n'\
  5732. 'Other commands:\n'\
  5733. ' -modes List available suspend modes\n'\
  5734. ' -status Test to see if the system is enabled to run this tool\n'\
  5735. ' -fpdt Print out the contents of the ACPI Firmware Performance Data Table\n'\
  5736. ' -battery Print out battery info (if available)\n'\
  5737. ' -x<mode> Test xset by toggling the given mode (on/off/standby/suspend)\n'\
  5738. ' -sysinfo Print out system info extracted from BIOS\n'\
  5739. ' -devinfo Print out the pm settings of all devices which support runtime suspend\n'\
  5740. ' -flist Print the list of functions currently being captured in ftrace\n'\
  5741. ' -flistall Print all functions capable of being captured in ftrace\n'\
  5742. ' -summary dir Create a summary of tests in this dir [-genhtml builds missing html]\n'\
  5743. ' [redo]\n'\
  5744. ' -ftrace ftracefile Create HTML output using ftrace input (used with -dmesg)\n'\
  5745. ' -dmesg dmesgfile Create HTML output using dmesg (used with -ftrace)\n'\
  5746. '' % (sysvals.title, sysvals.version, sysvals.suspendmode))
  5747. return True
  5748. # ----------------- MAIN --------------------
  5749. # exec start (skipped if script is loaded as library)
  5750. if __name__ == '__main__':
  5751. genhtml = False
  5752. cmd = ''
  5753. simplecmds = ['-sysinfo', '-modes', '-fpdt', '-flist', '-flistall',
  5754. '-devinfo', '-status', '-battery', '-xon', '-xoff', '-xstandby',
  5755. '-xsuspend', '-xinit', '-xreset', '-xstat']
  5756. if '-f' in sys.argv:
  5757. sysvals.cgskip = sysvals.configFile('cgskip.txt')
  5758. # loop through the command line arguments
  5759. args = iter(sys.argv[1:])
  5760. for arg in args:
  5761. if(arg == '-m'):
  5762. try:
  5763. val = args.next()
  5764. except:
  5765. doError('No mode supplied', True)
  5766. if val == 'command' and not sysvals.testcommand:
  5767. doError('No command supplied for mode "command"', True)
  5768. sysvals.suspendmode = val
  5769. elif(arg in simplecmds):
  5770. cmd = arg[1:]
  5771. elif(arg == '-h'):
  5772. printHelp()
  5773. sys.exit(0)
  5774. elif(arg == '-v'):
  5775. pprint("Version %s" % sysvals.version)
  5776. sys.exit(0)
  5777. elif(arg == '-x2'):
  5778. sysvals.execcount = 2
  5779. elif(arg == '-x2delay'):
  5780. sysvals.x2delay = getArgInt('-x2delay', args, 0, 60000)
  5781. elif(arg == '-predelay'):
  5782. sysvals.predelay = getArgInt('-predelay', args, 0, 60000)
  5783. elif(arg == '-postdelay'):
  5784. sysvals.postdelay = getArgInt('-postdelay', args, 0, 60000)
  5785. elif(arg == '-f'):
  5786. sysvals.usecallgraph = True
  5787. elif(arg == '-skiphtml'):
  5788. sysvals.skiphtml = True
  5789. elif(arg == '-cgdump'):
  5790. sysvals.cgdump = True
  5791. elif(arg == '-devdump'):
  5792. sysvals.devdump = True
  5793. elif(arg == '-genhtml'):
  5794. genhtml = True
  5795. elif(arg == '-addlogs'):
  5796. sysvals.dmesglog = sysvals.ftracelog = True
  5797. elif(arg == '-addlogdmesg'):
  5798. sysvals.dmesglog = True
  5799. elif(arg == '-addlogftrace'):
  5800. sysvals.ftracelog = True
  5801. elif(arg == '-verbose'):
  5802. sysvals.verbose = True
  5803. elif(arg == '-proc'):
  5804. sysvals.useprocmon = True
  5805. elif(arg == '-dev'):
  5806. sysvals.usedevsrc = True
  5807. elif(arg == '-sync'):
  5808. sysvals.sync = True
  5809. elif(arg == '-gzip'):
  5810. sysvals.gzip = True
  5811. elif(arg == '-rs'):
  5812. try:
  5813. val = args.next()
  5814. except:
  5815. doError('-rs requires "enable" or "disable"', True)
  5816. if val.lower() in switchvalues:
  5817. if val.lower() in switchoff:
  5818. sysvals.rs = -1
  5819. else:
  5820. sysvals.rs = 1
  5821. else:
  5822. doError('invalid option: %s, use "enable/disable" or "on/off"' % val, True)
  5823. elif(arg == '-display'):
  5824. try:
  5825. val = args.next()
  5826. except:
  5827. doError('-display requires an mode value', True)
  5828. disopt = ['on', 'off', 'standby', 'suspend']
  5829. if val.lower() not in disopt:
  5830. doError('valid display mode values are %s' % disopt, True)
  5831. sysvals.display = val.lower()
  5832. elif(arg == '-maxdepth'):
  5833. sysvals.max_graph_depth = getArgInt('-maxdepth', args, 0, 1000)
  5834. elif(arg == '-rtcwake'):
  5835. try:
  5836. val = args.next()
  5837. except:
  5838. doError('No rtcwake time supplied', True)
  5839. if val.lower() in switchoff:
  5840. sysvals.rtcwake = False
  5841. else:
  5842. sysvals.rtcwake = True
  5843. sysvals.rtcwaketime = getArgInt('-rtcwake', val, 0, 3600, False)
  5844. elif(arg == '-timeprec'):
  5845. sysvals.setPrecision(getArgInt('-timeprec', args, 0, 6))
  5846. elif(arg == '-mindev'):
  5847. sysvals.mindevlen = getArgFloat('-mindev', args, 0.0, 10000.0)
  5848. elif(arg == '-mincg'):
  5849. sysvals.mincglen = getArgFloat('-mincg', args, 0.0, 10000.0)
  5850. elif(arg == '-bufsize'):
  5851. sysvals.bufsize = getArgInt('-bufsize', args, 1, 1024*1024*8)
  5852. elif(arg == '-cgtest'):
  5853. sysvals.cgtest = getArgInt('-cgtest', args, 0, 1)
  5854. elif(arg == '-cgphase'):
  5855. try:
  5856. val = args.next()
  5857. except:
  5858. doError('No phase name supplied', True)
  5859. d = Data(0)
  5860. if val not in d.phasedef:
  5861. doError('invalid phase --> (%s: %s), valid phases are %s'\
  5862. % (arg, val, d.phasedef.keys()), True)
  5863. sysvals.cgphase = val
  5864. elif(arg == '-cgfilter'):
  5865. try:
  5866. val = args.next()
  5867. except:
  5868. doError('No callgraph functions supplied', True)
  5869. sysvals.setCallgraphFilter(val)
  5870. elif(arg == '-cgskip'):
  5871. try:
  5872. val = args.next()
  5873. except:
  5874. doError('No file supplied', True)
  5875. if val.lower() in switchoff:
  5876. sysvals.cgskip = ''
  5877. else:
  5878. sysvals.cgskip = sysvals.configFile(val)
  5879. if(not sysvals.cgskip):
  5880. doError('%s does not exist' % sysvals.cgskip)
  5881. elif(arg == '-callloop-maxgap'):
  5882. sysvals.callloopmaxgap = getArgFloat('-callloop-maxgap', args, 0.0, 1.0)
  5883. elif(arg == '-callloop-maxlen'):
  5884. sysvals.callloopmaxlen = getArgFloat('-callloop-maxlen', args, 0.0, 1.0)
  5885. elif(arg == '-cmd'):
  5886. try:
  5887. val = args.next()
  5888. except:
  5889. doError('No command string supplied', True)
  5890. sysvals.testcommand = val
  5891. sysvals.suspendmode = 'command'
  5892. elif(arg == '-expandcg'):
  5893. sysvals.cgexp = True
  5894. elif(arg == '-srgap'):
  5895. sysvals.srgap = 5
  5896. elif(arg == '-multi'):
  5897. sysvals.multitest['run'] = True
  5898. sysvals.multitest['count'] = getArgInt('-multi n d (exec count)', args, 2, 1000000)
  5899. sysvals.multitest['delay'] = getArgInt('-multi n d (delay between tests)', args, 0, 3600)
  5900. elif(arg == '-o'):
  5901. try:
  5902. val = args.next()
  5903. except:
  5904. doError('No subdirectory name supplied', True)
  5905. sysvals.outdir = sysvals.setOutputFolder(val)
  5906. elif(arg == '-config'):
  5907. try:
  5908. val = args.next()
  5909. except:
  5910. doError('No text file supplied', True)
  5911. file = sysvals.configFile(val)
  5912. if(not file):
  5913. doError('%s does not exist' % val)
  5914. configFromFile(file)
  5915. elif(arg == '-fadd'):
  5916. try:
  5917. val = args.next()
  5918. except:
  5919. doError('No text file supplied', True)
  5920. file = sysvals.configFile(val)
  5921. if(not file):
  5922. doError('%s does not exist' % val)
  5923. sysvals.addFtraceFilterFunctions(file)
  5924. elif(arg == '-dmesg'):
  5925. try:
  5926. val = args.next()
  5927. except:
  5928. doError('No dmesg file supplied', True)
  5929. sysvals.notestrun = True
  5930. sysvals.dmesgfile = val
  5931. if(os.path.exists(sysvals.dmesgfile) == False):
  5932. doError('%s does not exist' % sysvals.dmesgfile)
  5933. elif(arg == '-ftrace'):
  5934. try:
  5935. val = args.next()
  5936. except:
  5937. doError('No ftrace file supplied', True)
  5938. sysvals.notestrun = True
  5939. sysvals.ftracefile = val
  5940. if(os.path.exists(sysvals.ftracefile) == False):
  5941. doError('%s does not exist' % sysvals.ftracefile)
  5942. elif(arg == '-summary'):
  5943. try:
  5944. val = args.next()
  5945. except:
  5946. doError('No directory supplied', True)
  5947. cmd = 'summary'
  5948. sysvals.outdir = val
  5949. sysvals.notestrun = True
  5950. if(os.path.isdir(val) == False):
  5951. doError('%s is not accesible' % val)
  5952. elif(arg == '-filter'):
  5953. try:
  5954. val = args.next()
  5955. except:
  5956. doError('No devnames supplied', True)
  5957. sysvals.setDeviceFilter(val)
  5958. elif(arg == '-result'):
  5959. try:
  5960. val = args.next()
  5961. except:
  5962. doError('No result file supplied', True)
  5963. sysvals.result = val
  5964. sysvals.signalHandlerInit()
  5965. else:
  5966. doError('Invalid argument: '+arg, True)
  5967. # compatibility errors
  5968. if(sysvals.usecallgraph and sysvals.usedevsrc):
  5969. doError('-dev is not compatible with -f')
  5970. if(sysvals.usecallgraph and sysvals.useprocmon):
  5971. doError('-proc is not compatible with -f')
  5972. if sysvals.usecallgraph and sysvals.cgskip:
  5973. sysvals.vprint('Using cgskip file: %s' % sysvals.cgskip)
  5974. sysvals.setCallgraphBlacklist(sysvals.cgskip)
  5975. # callgraph size cannot exceed device size
  5976. if sysvals.mincglen < sysvals.mindevlen:
  5977. sysvals.mincglen = sysvals.mindevlen
  5978. # remove existing buffers before calculating memory
  5979. if(sysvals.usecallgraph or sysvals.usedevsrc):
  5980. sysvals.fsetVal('16', 'buffer_size_kb')
  5981. sysvals.cpuInfo()
  5982. # just run a utility command and exit
  5983. if(cmd != ''):
  5984. ret = 0
  5985. if(cmd == 'status'):
  5986. if not statusCheck(True):
  5987. ret = 1
  5988. elif(cmd == 'fpdt'):
  5989. if not getFPDT(True):
  5990. ret = 1
  5991. elif(cmd == 'battery'):
  5992. out = getBattery()
  5993. if out:
  5994. pprint('AC Connect : %s\nBattery Charge: %d' % out)
  5995. else:
  5996. pprint('no battery found')
  5997. ret = 1
  5998. elif(cmd == 'sysinfo'):
  5999. sysvals.printSystemInfo(True)
  6000. elif(cmd == 'devinfo'):
  6001. deviceInfo()
  6002. elif(cmd == 'modes'):
  6003. print getModes()
  6004. elif(cmd == 'flist'):
  6005. sysvals.getFtraceFilterFunctions(True)
  6006. elif(cmd == 'flistall'):
  6007. sysvals.getFtraceFilterFunctions(False)
  6008. elif(cmd == 'summary'):
  6009. runSummary(sysvals.outdir, True, genhtml)
  6010. elif(cmd in ['xon', 'xoff', 'xstandby', 'xsuspend', 'xinit', 'xreset']):
  6011. sysvals.verbose = True
  6012. ret = displayControl(cmd[1:])
  6013. elif(cmd == 'xstat'):
  6014. pprint('Display Status: %s' % displayControl('stat').upper())
  6015. sys.exit(ret)
  6016. # if instructed, re-analyze existing data files
  6017. if(sysvals.notestrun):
  6018. stamp = rerunTest()
  6019. sysvals.outputResult(stamp)
  6020. sys.exit(0)
  6021. # verify that we can run a test
  6022. error = statusCheck()
  6023. if(error):
  6024. doError(error)
  6025. # extract mem/disk extra modes and convert
  6026. mode = sysvals.suspendmode
  6027. if mode.startswith('mem'):
  6028. memmode = mode.split('-', 1)[-1] if '-' in mode else 'deep'
  6029. if memmode == 'shallow':
  6030. mode = 'standby'
  6031. elif memmode == 's2idle':
  6032. mode = 'freeze'
  6033. else:
  6034. mode = 'mem'
  6035. sysvals.memmode = memmode
  6036. sysvals.suspendmode = mode
  6037. if mode.startswith('disk-'):
  6038. sysvals.diskmode = mode.split('-', 1)[-1]
  6039. sysvals.suspendmode = 'disk'
  6040. sysvals.systemInfo(dmidecode(sysvals.mempath))
  6041. setRuntimeSuspend(True)
  6042. if sysvals.display:
  6043. displayControl('init')
  6044. ret = 0
  6045. if sysvals.multitest['run']:
  6046. # run multiple tests in a separate subdirectory
  6047. if not sysvals.outdir:
  6048. s = 'suspend-x%d' % sysvals.multitest['count']
  6049. sysvals.outdir = datetime.now().strftime(s+'-%y%m%d-%H%M%S')
  6050. if not os.path.isdir(sysvals.outdir):
  6051. os.mkdir(sysvals.outdir)
  6052. for i in range(sysvals.multitest['count']):
  6053. if(i != 0):
  6054. pprint('Waiting %d seconds...' % (sysvals.multitest['delay']))
  6055. time.sleep(sysvals.multitest['delay'])
  6056. pprint('TEST (%d/%d) START' % (i+1, sysvals.multitest['count']))
  6057. fmt = 'suspend-%y%m%d-%H%M%S'
  6058. sysvals.testdir = os.path.join(sysvals.outdir, datetime.now().strftime(fmt))
  6059. ret = runTest(i+1)
  6060. pprint('TEST (%d/%d) COMPLETE' % (i+1, sysvals.multitest['count']))
  6061. sysvals.logmsg = ''
  6062. if not sysvals.skiphtml:
  6063. runSummary(sysvals.outdir, False, False)
  6064. sysvals.sudoUserchown(sysvals.outdir)
  6065. else:
  6066. if sysvals.outdir:
  6067. sysvals.testdir = sysvals.outdir
  6068. # run the test in the current directory
  6069. ret = runTest()
  6070. if sysvals.display:
  6071. displayControl('reset')
  6072. setRuntimeSuspend(False)
  6073. sys.exit(ret)