sleepgraph.py 195 KB

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