header.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include "util.h"
  5. #include "string2.h"
  6. #include <sys/param.h>
  7. #include <sys/types.h>
  8. #include <byteswap.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <linux/compiler.h>
  13. #include <linux/list.h>
  14. #include <linux/kernel.h>
  15. #include <linux/bitops.h>
  16. #include <linux/stringify.h>
  17. #include <sys/stat.h>
  18. #include <sys/utsname.h>
  19. #include <linux/time64.h>
  20. #include <dirent.h>
  21. #include "evlist.h"
  22. #include "evsel.h"
  23. #include "header.h"
  24. #include "memswap.h"
  25. #include "../perf.h"
  26. #include "trace-event.h"
  27. #include "session.h"
  28. #include "symbol.h"
  29. #include "debug.h"
  30. #include "cpumap.h"
  31. #include "pmu.h"
  32. #include "vdso.h"
  33. #include "strbuf.h"
  34. #include "build-id.h"
  35. #include "data.h"
  36. #include <api/fs/fs.h>
  37. #include "asm/bug.h"
  38. #include "tool.h"
  39. #include "time-utils.h"
  40. #include "units.h"
  41. #include "sane_ctype.h"
  42. /*
  43. * magic2 = "PERFILE2"
  44. * must be a numerical value to let the endianness
  45. * determine the memory layout. That way we are able
  46. * to detect endianness when reading the perf.data file
  47. * back.
  48. *
  49. * we check for legacy (PERFFILE) format.
  50. */
  51. static const char *__perf_magic1 = "PERFFILE";
  52. static const u64 __perf_magic2 = 0x32454c4946524550ULL;
  53. static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
  54. #define PERF_MAGIC __perf_magic2
  55. const char perf_version_string[] = PERF_VERSION;
  56. struct perf_file_attr {
  57. struct perf_event_attr attr;
  58. struct perf_file_section ids;
  59. };
  60. struct feat_fd {
  61. struct perf_header *ph;
  62. int fd;
  63. void *buf; /* Either buf != NULL or fd >= 0 */
  64. ssize_t offset;
  65. size_t size;
  66. struct perf_evsel *events;
  67. };
  68. void perf_header__set_feat(struct perf_header *header, int feat)
  69. {
  70. set_bit(feat, header->adds_features);
  71. }
  72. void perf_header__clear_feat(struct perf_header *header, int feat)
  73. {
  74. clear_bit(feat, header->adds_features);
  75. }
  76. bool perf_header__has_feat(const struct perf_header *header, int feat)
  77. {
  78. return test_bit(feat, header->adds_features);
  79. }
  80. static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
  81. {
  82. ssize_t ret = writen(ff->fd, buf, size);
  83. if (ret != (ssize_t)size)
  84. return ret < 0 ? (int)ret : -1;
  85. return 0;
  86. }
  87. static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
  88. {
  89. /* struct perf_event_header::size is u16 */
  90. const size_t max_size = 0xffff - sizeof(struct perf_event_header);
  91. size_t new_size = ff->size;
  92. void *addr;
  93. if (size + ff->offset > max_size)
  94. return -E2BIG;
  95. while (size > (new_size - ff->offset))
  96. new_size <<= 1;
  97. new_size = min(max_size, new_size);
  98. if (ff->size < new_size) {
  99. addr = realloc(ff->buf, new_size);
  100. if (!addr)
  101. return -ENOMEM;
  102. ff->buf = addr;
  103. ff->size = new_size;
  104. }
  105. memcpy(ff->buf + ff->offset, buf, size);
  106. ff->offset += size;
  107. return 0;
  108. }
  109. /* Return: 0 if succeded, -ERR if failed. */
  110. int do_write(struct feat_fd *ff, const void *buf, size_t size)
  111. {
  112. if (!ff->buf)
  113. return __do_write_fd(ff, buf, size);
  114. return __do_write_buf(ff, buf, size);
  115. }
  116. /* Return: 0 if succeded, -ERR if failed. */
  117. static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
  118. {
  119. u64 *p = (u64 *) set;
  120. int i, ret;
  121. ret = do_write(ff, &size, sizeof(size));
  122. if (ret < 0)
  123. return ret;
  124. for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
  125. ret = do_write(ff, p + i, sizeof(*p));
  126. if (ret < 0)
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. /* Return: 0 if succeded, -ERR if failed. */
  132. int write_padded(struct feat_fd *ff, const void *bf,
  133. size_t count, size_t count_aligned)
  134. {
  135. static const char zero_buf[NAME_ALIGN];
  136. int err = do_write(ff, bf, count);
  137. if (!err)
  138. err = do_write(ff, zero_buf, count_aligned - count);
  139. return err;
  140. }
  141. #define string_size(str) \
  142. (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
  143. /* Return: 0 if succeded, -ERR if failed. */
  144. static int do_write_string(struct feat_fd *ff, const char *str)
  145. {
  146. u32 len, olen;
  147. int ret;
  148. olen = strlen(str) + 1;
  149. len = PERF_ALIGN(olen, NAME_ALIGN);
  150. /* write len, incl. \0 */
  151. ret = do_write(ff, &len, sizeof(len));
  152. if (ret < 0)
  153. return ret;
  154. return write_padded(ff, str, olen, len);
  155. }
  156. static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
  157. {
  158. ssize_t ret = readn(ff->fd, addr, size);
  159. if (ret != size)
  160. return ret < 0 ? (int)ret : -1;
  161. return 0;
  162. }
  163. static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
  164. {
  165. if (size > (ssize_t)ff->size - ff->offset)
  166. return -1;
  167. memcpy(addr, ff->buf + ff->offset, size);
  168. ff->offset += size;
  169. return 0;
  170. }
  171. static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
  172. {
  173. if (!ff->buf)
  174. return __do_read_fd(ff, addr, size);
  175. return __do_read_buf(ff, addr, size);
  176. }
  177. static int do_read_u32(struct feat_fd *ff, u32 *addr)
  178. {
  179. int ret;
  180. ret = __do_read(ff, addr, sizeof(*addr));
  181. if (ret)
  182. return ret;
  183. if (ff->ph->needs_swap)
  184. *addr = bswap_32(*addr);
  185. return 0;
  186. }
  187. static int do_read_u64(struct feat_fd *ff, u64 *addr)
  188. {
  189. int ret;
  190. ret = __do_read(ff, addr, sizeof(*addr));
  191. if (ret)
  192. return ret;
  193. if (ff->ph->needs_swap)
  194. *addr = bswap_64(*addr);
  195. return 0;
  196. }
  197. static char *do_read_string(struct feat_fd *ff)
  198. {
  199. u32 len;
  200. char *buf;
  201. if (do_read_u32(ff, &len))
  202. return NULL;
  203. buf = malloc(len);
  204. if (!buf)
  205. return NULL;
  206. if (!__do_read(ff, buf, len)) {
  207. /*
  208. * strings are padded by zeroes
  209. * thus the actual strlen of buf
  210. * may be less than len
  211. */
  212. return buf;
  213. }
  214. free(buf);
  215. return NULL;
  216. }
  217. /* Return: 0 if succeded, -ERR if failed. */
  218. static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
  219. {
  220. unsigned long *set;
  221. u64 size, *p;
  222. int i, ret;
  223. ret = do_read_u64(ff, &size);
  224. if (ret)
  225. return ret;
  226. set = bitmap_alloc(size);
  227. if (!set)
  228. return -ENOMEM;
  229. p = (u64 *) set;
  230. for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
  231. ret = do_read_u64(ff, p + i);
  232. if (ret < 0) {
  233. free(set);
  234. return ret;
  235. }
  236. }
  237. *pset = set;
  238. *psize = size;
  239. return 0;
  240. }
  241. static int write_tracing_data(struct feat_fd *ff,
  242. struct perf_evlist *evlist)
  243. {
  244. if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
  245. return -1;
  246. return read_tracing_data(ff->fd, &evlist->entries);
  247. }
  248. static int write_build_id(struct feat_fd *ff,
  249. struct perf_evlist *evlist __maybe_unused)
  250. {
  251. struct perf_session *session;
  252. int err;
  253. session = container_of(ff->ph, struct perf_session, header);
  254. if (!perf_session__read_build_ids(session, true))
  255. return -1;
  256. if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
  257. return -1;
  258. err = perf_session__write_buildid_table(session, ff);
  259. if (err < 0) {
  260. pr_debug("failed to write buildid table\n");
  261. return err;
  262. }
  263. perf_session__cache_build_ids(session);
  264. return 0;
  265. }
  266. static int write_hostname(struct feat_fd *ff,
  267. struct perf_evlist *evlist __maybe_unused)
  268. {
  269. struct utsname uts;
  270. int ret;
  271. ret = uname(&uts);
  272. if (ret < 0)
  273. return -1;
  274. return do_write_string(ff, uts.nodename);
  275. }
  276. static int write_osrelease(struct feat_fd *ff,
  277. struct perf_evlist *evlist __maybe_unused)
  278. {
  279. struct utsname uts;
  280. int ret;
  281. ret = uname(&uts);
  282. if (ret < 0)
  283. return -1;
  284. return do_write_string(ff, uts.release);
  285. }
  286. static int write_arch(struct feat_fd *ff,
  287. struct perf_evlist *evlist __maybe_unused)
  288. {
  289. struct utsname uts;
  290. int ret;
  291. ret = uname(&uts);
  292. if (ret < 0)
  293. return -1;
  294. return do_write_string(ff, uts.machine);
  295. }
  296. static int write_version(struct feat_fd *ff,
  297. struct perf_evlist *evlist __maybe_unused)
  298. {
  299. return do_write_string(ff, perf_version_string);
  300. }
  301. static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
  302. {
  303. FILE *file;
  304. char *buf = NULL;
  305. char *s, *p;
  306. const char *search = cpuinfo_proc;
  307. size_t len = 0;
  308. int ret = -1;
  309. if (!search)
  310. return -1;
  311. file = fopen("/proc/cpuinfo", "r");
  312. if (!file)
  313. return -1;
  314. while (getline(&buf, &len, file) > 0) {
  315. ret = strncmp(buf, search, strlen(search));
  316. if (!ret)
  317. break;
  318. }
  319. if (ret) {
  320. ret = -1;
  321. goto done;
  322. }
  323. s = buf;
  324. p = strchr(buf, ':');
  325. if (p && *(p+1) == ' ' && *(p+2))
  326. s = p + 2;
  327. p = strchr(s, '\n');
  328. if (p)
  329. *p = '\0';
  330. /* squash extra space characters (branding string) */
  331. p = s;
  332. while (*p) {
  333. if (isspace(*p)) {
  334. char *r = p + 1;
  335. char *q = r;
  336. *p = ' ';
  337. while (*q && isspace(*q))
  338. q++;
  339. if (q != (p+1))
  340. while ((*r++ = *q++));
  341. }
  342. p++;
  343. }
  344. ret = do_write_string(ff, s);
  345. done:
  346. free(buf);
  347. fclose(file);
  348. return ret;
  349. }
  350. static int write_cpudesc(struct feat_fd *ff,
  351. struct perf_evlist *evlist __maybe_unused)
  352. {
  353. const char *cpuinfo_procs[] = CPUINFO_PROC;
  354. unsigned int i;
  355. for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
  356. int ret;
  357. ret = __write_cpudesc(ff, cpuinfo_procs[i]);
  358. if (ret >= 0)
  359. return ret;
  360. }
  361. return -1;
  362. }
  363. static int write_nrcpus(struct feat_fd *ff,
  364. struct perf_evlist *evlist __maybe_unused)
  365. {
  366. long nr;
  367. u32 nrc, nra;
  368. int ret;
  369. nrc = cpu__max_present_cpu();
  370. nr = sysconf(_SC_NPROCESSORS_ONLN);
  371. if (nr < 0)
  372. return -1;
  373. nra = (u32)(nr & UINT_MAX);
  374. ret = do_write(ff, &nrc, sizeof(nrc));
  375. if (ret < 0)
  376. return ret;
  377. return do_write(ff, &nra, sizeof(nra));
  378. }
  379. static int write_event_desc(struct feat_fd *ff,
  380. struct perf_evlist *evlist)
  381. {
  382. struct perf_evsel *evsel;
  383. u32 nre, nri, sz;
  384. int ret;
  385. nre = evlist->nr_entries;
  386. /*
  387. * write number of events
  388. */
  389. ret = do_write(ff, &nre, sizeof(nre));
  390. if (ret < 0)
  391. return ret;
  392. /*
  393. * size of perf_event_attr struct
  394. */
  395. sz = (u32)sizeof(evsel->attr);
  396. ret = do_write(ff, &sz, sizeof(sz));
  397. if (ret < 0)
  398. return ret;
  399. evlist__for_each_entry(evlist, evsel) {
  400. ret = do_write(ff, &evsel->attr, sz);
  401. if (ret < 0)
  402. return ret;
  403. /*
  404. * write number of unique id per event
  405. * there is one id per instance of an event
  406. *
  407. * copy into an nri to be independent of the
  408. * type of ids,
  409. */
  410. nri = evsel->ids;
  411. ret = do_write(ff, &nri, sizeof(nri));
  412. if (ret < 0)
  413. return ret;
  414. /*
  415. * write event string as passed on cmdline
  416. */
  417. ret = do_write_string(ff, perf_evsel__name(evsel));
  418. if (ret < 0)
  419. return ret;
  420. /*
  421. * write unique ids for this event
  422. */
  423. ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
  424. if (ret < 0)
  425. return ret;
  426. }
  427. return 0;
  428. }
  429. static int write_cmdline(struct feat_fd *ff,
  430. struct perf_evlist *evlist __maybe_unused)
  431. {
  432. char buf[MAXPATHLEN];
  433. u32 n;
  434. int i, ret;
  435. /* actual path to perf binary */
  436. ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  437. if (ret <= 0)
  438. return -1;
  439. /* readlink() does not add null termination */
  440. buf[ret] = '\0';
  441. /* account for binary path */
  442. n = perf_env.nr_cmdline + 1;
  443. ret = do_write(ff, &n, sizeof(n));
  444. if (ret < 0)
  445. return ret;
  446. ret = do_write_string(ff, buf);
  447. if (ret < 0)
  448. return ret;
  449. for (i = 0 ; i < perf_env.nr_cmdline; i++) {
  450. ret = do_write_string(ff, perf_env.cmdline_argv[i]);
  451. if (ret < 0)
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. #define CORE_SIB_FMT \
  457. "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
  458. #define THRD_SIB_FMT \
  459. "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
  460. struct cpu_topo {
  461. u32 cpu_nr;
  462. u32 core_sib;
  463. u32 thread_sib;
  464. char **core_siblings;
  465. char **thread_siblings;
  466. };
  467. static int build_cpu_topo(struct cpu_topo *tp, int cpu)
  468. {
  469. FILE *fp;
  470. char filename[MAXPATHLEN];
  471. char *buf = NULL, *p;
  472. size_t len = 0;
  473. ssize_t sret;
  474. u32 i = 0;
  475. int ret = -1;
  476. sprintf(filename, CORE_SIB_FMT, cpu);
  477. fp = fopen(filename, "r");
  478. if (!fp)
  479. goto try_threads;
  480. sret = getline(&buf, &len, fp);
  481. fclose(fp);
  482. if (sret <= 0)
  483. goto try_threads;
  484. p = strchr(buf, '\n');
  485. if (p)
  486. *p = '\0';
  487. for (i = 0; i < tp->core_sib; i++) {
  488. if (!strcmp(buf, tp->core_siblings[i]))
  489. break;
  490. }
  491. if (i == tp->core_sib) {
  492. tp->core_siblings[i] = buf;
  493. tp->core_sib++;
  494. buf = NULL;
  495. len = 0;
  496. }
  497. ret = 0;
  498. try_threads:
  499. sprintf(filename, THRD_SIB_FMT, cpu);
  500. fp = fopen(filename, "r");
  501. if (!fp)
  502. goto done;
  503. if (getline(&buf, &len, fp) <= 0)
  504. goto done;
  505. p = strchr(buf, '\n');
  506. if (p)
  507. *p = '\0';
  508. for (i = 0; i < tp->thread_sib; i++) {
  509. if (!strcmp(buf, tp->thread_siblings[i]))
  510. break;
  511. }
  512. if (i == tp->thread_sib) {
  513. tp->thread_siblings[i] = buf;
  514. tp->thread_sib++;
  515. buf = NULL;
  516. }
  517. ret = 0;
  518. done:
  519. if(fp)
  520. fclose(fp);
  521. free(buf);
  522. return ret;
  523. }
  524. static void free_cpu_topo(struct cpu_topo *tp)
  525. {
  526. u32 i;
  527. if (!tp)
  528. return;
  529. for (i = 0 ; i < tp->core_sib; i++)
  530. zfree(&tp->core_siblings[i]);
  531. for (i = 0 ; i < tp->thread_sib; i++)
  532. zfree(&tp->thread_siblings[i]);
  533. free(tp);
  534. }
  535. static struct cpu_topo *build_cpu_topology(void)
  536. {
  537. struct cpu_topo *tp = NULL;
  538. void *addr;
  539. u32 nr, i;
  540. size_t sz;
  541. long ncpus;
  542. int ret = -1;
  543. struct cpu_map *map;
  544. ncpus = cpu__max_present_cpu();
  545. /* build online CPU map */
  546. map = cpu_map__new(NULL);
  547. if (map == NULL) {
  548. pr_debug("failed to get system cpumap\n");
  549. return NULL;
  550. }
  551. nr = (u32)(ncpus & UINT_MAX);
  552. sz = nr * sizeof(char *);
  553. addr = calloc(1, sizeof(*tp) + 2 * sz);
  554. if (!addr)
  555. goto out_free;
  556. tp = addr;
  557. tp->cpu_nr = nr;
  558. addr += sizeof(*tp);
  559. tp->core_siblings = addr;
  560. addr += sz;
  561. tp->thread_siblings = addr;
  562. for (i = 0; i < nr; i++) {
  563. if (!cpu_map__has(map, i))
  564. continue;
  565. ret = build_cpu_topo(tp, i);
  566. if (ret < 0)
  567. break;
  568. }
  569. out_free:
  570. cpu_map__put(map);
  571. if (ret) {
  572. free_cpu_topo(tp);
  573. tp = NULL;
  574. }
  575. return tp;
  576. }
  577. static int write_cpu_topology(struct feat_fd *ff,
  578. struct perf_evlist *evlist __maybe_unused)
  579. {
  580. struct cpu_topo *tp;
  581. u32 i;
  582. int ret, j;
  583. tp = build_cpu_topology();
  584. if (!tp)
  585. return -1;
  586. ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
  587. if (ret < 0)
  588. goto done;
  589. for (i = 0; i < tp->core_sib; i++) {
  590. ret = do_write_string(ff, tp->core_siblings[i]);
  591. if (ret < 0)
  592. goto done;
  593. }
  594. ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
  595. if (ret < 0)
  596. goto done;
  597. for (i = 0; i < tp->thread_sib; i++) {
  598. ret = do_write_string(ff, tp->thread_siblings[i]);
  599. if (ret < 0)
  600. break;
  601. }
  602. ret = perf_env__read_cpu_topology_map(&perf_env);
  603. if (ret < 0)
  604. goto done;
  605. for (j = 0; j < perf_env.nr_cpus_avail; j++) {
  606. ret = do_write(ff, &perf_env.cpu[j].core_id,
  607. sizeof(perf_env.cpu[j].core_id));
  608. if (ret < 0)
  609. return ret;
  610. ret = do_write(ff, &perf_env.cpu[j].socket_id,
  611. sizeof(perf_env.cpu[j].socket_id));
  612. if (ret < 0)
  613. return ret;
  614. }
  615. done:
  616. free_cpu_topo(tp);
  617. return ret;
  618. }
  619. static int write_total_mem(struct feat_fd *ff,
  620. struct perf_evlist *evlist __maybe_unused)
  621. {
  622. char *buf = NULL;
  623. FILE *fp;
  624. size_t len = 0;
  625. int ret = -1, n;
  626. uint64_t mem;
  627. fp = fopen("/proc/meminfo", "r");
  628. if (!fp)
  629. return -1;
  630. while (getline(&buf, &len, fp) > 0) {
  631. ret = strncmp(buf, "MemTotal:", 9);
  632. if (!ret)
  633. break;
  634. }
  635. if (!ret) {
  636. n = sscanf(buf, "%*s %"PRIu64, &mem);
  637. if (n == 1)
  638. ret = do_write(ff, &mem, sizeof(mem));
  639. } else
  640. ret = -1;
  641. free(buf);
  642. fclose(fp);
  643. return ret;
  644. }
  645. static int write_topo_node(struct feat_fd *ff, int node)
  646. {
  647. char str[MAXPATHLEN];
  648. char field[32];
  649. char *buf = NULL, *p;
  650. size_t len = 0;
  651. FILE *fp;
  652. u64 mem_total, mem_free, mem;
  653. int ret = -1;
  654. sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
  655. fp = fopen(str, "r");
  656. if (!fp)
  657. return -1;
  658. while (getline(&buf, &len, fp) > 0) {
  659. /* skip over invalid lines */
  660. if (!strchr(buf, ':'))
  661. continue;
  662. if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
  663. goto done;
  664. if (!strcmp(field, "MemTotal:"))
  665. mem_total = mem;
  666. if (!strcmp(field, "MemFree:"))
  667. mem_free = mem;
  668. }
  669. fclose(fp);
  670. fp = NULL;
  671. ret = do_write(ff, &mem_total, sizeof(u64));
  672. if (ret)
  673. goto done;
  674. ret = do_write(ff, &mem_free, sizeof(u64));
  675. if (ret)
  676. goto done;
  677. ret = -1;
  678. sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
  679. fp = fopen(str, "r");
  680. if (!fp)
  681. goto done;
  682. if (getline(&buf, &len, fp) <= 0)
  683. goto done;
  684. p = strchr(buf, '\n');
  685. if (p)
  686. *p = '\0';
  687. ret = do_write_string(ff, buf);
  688. done:
  689. free(buf);
  690. if (fp)
  691. fclose(fp);
  692. return ret;
  693. }
  694. static int write_numa_topology(struct feat_fd *ff,
  695. struct perf_evlist *evlist __maybe_unused)
  696. {
  697. char *buf = NULL;
  698. size_t len = 0;
  699. FILE *fp;
  700. struct cpu_map *node_map = NULL;
  701. char *c;
  702. u32 nr, i, j;
  703. int ret = -1;
  704. fp = fopen("/sys/devices/system/node/online", "r");
  705. if (!fp)
  706. return -1;
  707. if (getline(&buf, &len, fp) <= 0)
  708. goto done;
  709. c = strchr(buf, '\n');
  710. if (c)
  711. *c = '\0';
  712. node_map = cpu_map__new(buf);
  713. if (!node_map)
  714. goto done;
  715. nr = (u32)node_map->nr;
  716. ret = do_write(ff, &nr, sizeof(nr));
  717. if (ret < 0)
  718. goto done;
  719. for (i = 0; i < nr; i++) {
  720. j = (u32)node_map->map[i];
  721. ret = do_write(ff, &j, sizeof(j));
  722. if (ret < 0)
  723. break;
  724. ret = write_topo_node(ff, i);
  725. if (ret < 0)
  726. break;
  727. }
  728. done:
  729. free(buf);
  730. fclose(fp);
  731. cpu_map__put(node_map);
  732. return ret;
  733. }
  734. /*
  735. * File format:
  736. *
  737. * struct pmu_mappings {
  738. * u32 pmu_num;
  739. * struct pmu_map {
  740. * u32 type;
  741. * char name[];
  742. * }[pmu_num];
  743. * };
  744. */
  745. static int write_pmu_mappings(struct feat_fd *ff,
  746. struct perf_evlist *evlist __maybe_unused)
  747. {
  748. struct perf_pmu *pmu = NULL;
  749. u32 pmu_num = 0;
  750. int ret;
  751. /*
  752. * Do a first pass to count number of pmu to avoid lseek so this
  753. * works in pipe mode as well.
  754. */
  755. while ((pmu = perf_pmu__scan(pmu))) {
  756. if (!pmu->name)
  757. continue;
  758. pmu_num++;
  759. }
  760. ret = do_write(ff, &pmu_num, sizeof(pmu_num));
  761. if (ret < 0)
  762. return ret;
  763. while ((pmu = perf_pmu__scan(pmu))) {
  764. if (!pmu->name)
  765. continue;
  766. ret = do_write(ff, &pmu->type, sizeof(pmu->type));
  767. if (ret < 0)
  768. return ret;
  769. ret = do_write_string(ff, pmu->name);
  770. if (ret < 0)
  771. return ret;
  772. }
  773. return 0;
  774. }
  775. /*
  776. * File format:
  777. *
  778. * struct group_descs {
  779. * u32 nr_groups;
  780. * struct group_desc {
  781. * char name[];
  782. * u32 leader_idx;
  783. * u32 nr_members;
  784. * }[nr_groups];
  785. * };
  786. */
  787. static int write_group_desc(struct feat_fd *ff,
  788. struct perf_evlist *evlist)
  789. {
  790. u32 nr_groups = evlist->nr_groups;
  791. struct perf_evsel *evsel;
  792. int ret;
  793. ret = do_write(ff, &nr_groups, sizeof(nr_groups));
  794. if (ret < 0)
  795. return ret;
  796. evlist__for_each_entry(evlist, evsel) {
  797. if (perf_evsel__is_group_leader(evsel) &&
  798. evsel->nr_members > 1) {
  799. const char *name = evsel->group_name ?: "{anon_group}";
  800. u32 leader_idx = evsel->idx;
  801. u32 nr_members = evsel->nr_members;
  802. ret = do_write_string(ff, name);
  803. if (ret < 0)
  804. return ret;
  805. ret = do_write(ff, &leader_idx, sizeof(leader_idx));
  806. if (ret < 0)
  807. return ret;
  808. ret = do_write(ff, &nr_members, sizeof(nr_members));
  809. if (ret < 0)
  810. return ret;
  811. }
  812. }
  813. return 0;
  814. }
  815. /*
  816. * default get_cpuid(): nothing gets recorded
  817. * actual implementation must be in arch/$(SRCARCH)/util/header.c
  818. */
  819. int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
  820. {
  821. return -1;
  822. }
  823. static int write_cpuid(struct feat_fd *ff,
  824. struct perf_evlist *evlist __maybe_unused)
  825. {
  826. char buffer[64];
  827. int ret;
  828. ret = get_cpuid(buffer, sizeof(buffer));
  829. if (!ret)
  830. goto write_it;
  831. return -1;
  832. write_it:
  833. return do_write_string(ff, buffer);
  834. }
  835. static int write_branch_stack(struct feat_fd *ff __maybe_unused,
  836. struct perf_evlist *evlist __maybe_unused)
  837. {
  838. return 0;
  839. }
  840. static int write_auxtrace(struct feat_fd *ff,
  841. struct perf_evlist *evlist __maybe_unused)
  842. {
  843. struct perf_session *session;
  844. int err;
  845. if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
  846. return -1;
  847. session = container_of(ff->ph, struct perf_session, header);
  848. err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
  849. if (err < 0)
  850. pr_err("Failed to write auxtrace index\n");
  851. return err;
  852. }
  853. static int write_clockid(struct feat_fd *ff,
  854. struct perf_evlist *evlist __maybe_unused)
  855. {
  856. return do_write(ff, &ff->ph->env.clockid_res_ns,
  857. sizeof(ff->ph->env.clockid_res_ns));
  858. }
  859. static int cpu_cache_level__sort(const void *a, const void *b)
  860. {
  861. struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
  862. struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
  863. return cache_a->level - cache_b->level;
  864. }
  865. static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
  866. {
  867. if (a->level != b->level)
  868. return false;
  869. if (a->line_size != b->line_size)
  870. return false;
  871. if (a->sets != b->sets)
  872. return false;
  873. if (a->ways != b->ways)
  874. return false;
  875. if (strcmp(a->type, b->type))
  876. return false;
  877. if (strcmp(a->size, b->size))
  878. return false;
  879. if (strcmp(a->map, b->map))
  880. return false;
  881. return true;
  882. }
  883. static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
  884. {
  885. char path[PATH_MAX], file[PATH_MAX];
  886. struct stat st;
  887. size_t len;
  888. scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
  889. scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
  890. if (stat(file, &st))
  891. return 1;
  892. scnprintf(file, PATH_MAX, "%s/level", path);
  893. if (sysfs__read_int(file, (int *) &cache->level))
  894. return -1;
  895. scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
  896. if (sysfs__read_int(file, (int *) &cache->line_size))
  897. return -1;
  898. scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
  899. if (sysfs__read_int(file, (int *) &cache->sets))
  900. return -1;
  901. scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
  902. if (sysfs__read_int(file, (int *) &cache->ways))
  903. return -1;
  904. scnprintf(file, PATH_MAX, "%s/type", path);
  905. if (sysfs__read_str(file, &cache->type, &len))
  906. return -1;
  907. cache->type[len] = 0;
  908. cache->type = rtrim(cache->type);
  909. scnprintf(file, PATH_MAX, "%s/size", path);
  910. if (sysfs__read_str(file, &cache->size, &len)) {
  911. free(cache->type);
  912. return -1;
  913. }
  914. cache->size[len] = 0;
  915. cache->size = rtrim(cache->size);
  916. scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
  917. if (sysfs__read_str(file, &cache->map, &len)) {
  918. free(cache->map);
  919. free(cache->type);
  920. return -1;
  921. }
  922. cache->map[len] = 0;
  923. cache->map = rtrim(cache->map);
  924. return 0;
  925. }
  926. static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
  927. {
  928. fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
  929. }
  930. static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
  931. {
  932. u32 i, cnt = 0;
  933. long ncpus;
  934. u32 nr, cpu;
  935. u16 level;
  936. ncpus = sysconf(_SC_NPROCESSORS_CONF);
  937. if (ncpus < 0)
  938. return -1;
  939. nr = (u32)(ncpus & UINT_MAX);
  940. for (cpu = 0; cpu < nr; cpu++) {
  941. for (level = 0; level < 10; level++) {
  942. struct cpu_cache_level c;
  943. int err;
  944. err = cpu_cache_level__read(&c, cpu, level);
  945. if (err < 0)
  946. return err;
  947. if (err == 1)
  948. break;
  949. for (i = 0; i < cnt; i++) {
  950. if (cpu_cache_level__cmp(&c, &caches[i]))
  951. break;
  952. }
  953. if (i == cnt)
  954. caches[cnt++] = c;
  955. else
  956. cpu_cache_level__free(&c);
  957. if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
  958. goto out;
  959. }
  960. }
  961. out:
  962. *cntp = cnt;
  963. return 0;
  964. }
  965. #define MAX_CACHES 2000
  966. static int write_cache(struct feat_fd *ff,
  967. struct perf_evlist *evlist __maybe_unused)
  968. {
  969. struct cpu_cache_level caches[MAX_CACHES];
  970. u32 cnt = 0, i, version = 1;
  971. int ret;
  972. ret = build_caches(caches, MAX_CACHES, &cnt);
  973. if (ret)
  974. goto out;
  975. qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
  976. ret = do_write(ff, &version, sizeof(u32));
  977. if (ret < 0)
  978. goto out;
  979. ret = do_write(ff, &cnt, sizeof(u32));
  980. if (ret < 0)
  981. goto out;
  982. for (i = 0; i < cnt; i++) {
  983. struct cpu_cache_level *c = &caches[i];
  984. #define _W(v) \
  985. ret = do_write(ff, &c->v, sizeof(u32)); \
  986. if (ret < 0) \
  987. goto out;
  988. _W(level)
  989. _W(line_size)
  990. _W(sets)
  991. _W(ways)
  992. #undef _W
  993. #define _W(v) \
  994. ret = do_write_string(ff, (const char *) c->v); \
  995. if (ret < 0) \
  996. goto out;
  997. _W(type)
  998. _W(size)
  999. _W(map)
  1000. #undef _W
  1001. }
  1002. out:
  1003. for (i = 0; i < cnt; i++)
  1004. cpu_cache_level__free(&caches[i]);
  1005. return ret;
  1006. }
  1007. static int write_stat(struct feat_fd *ff __maybe_unused,
  1008. struct perf_evlist *evlist __maybe_unused)
  1009. {
  1010. return 0;
  1011. }
  1012. static int write_sample_time(struct feat_fd *ff,
  1013. struct perf_evlist *evlist)
  1014. {
  1015. int ret;
  1016. ret = do_write(ff, &evlist->first_sample_time,
  1017. sizeof(evlist->first_sample_time));
  1018. if (ret < 0)
  1019. return ret;
  1020. return do_write(ff, &evlist->last_sample_time,
  1021. sizeof(evlist->last_sample_time));
  1022. }
  1023. static int memory_node__read(struct memory_node *n, unsigned long idx)
  1024. {
  1025. unsigned int phys, size = 0;
  1026. char path[PATH_MAX];
  1027. struct dirent *ent;
  1028. DIR *dir;
  1029. #define for_each_memory(mem, dir) \
  1030. while ((ent = readdir(dir))) \
  1031. if (strcmp(ent->d_name, ".") && \
  1032. strcmp(ent->d_name, "..") && \
  1033. sscanf(ent->d_name, "memory%u", &mem) == 1)
  1034. scnprintf(path, PATH_MAX,
  1035. "%s/devices/system/node/node%lu",
  1036. sysfs__mountpoint(), idx);
  1037. dir = opendir(path);
  1038. if (!dir) {
  1039. pr_warning("failed: cant' open memory sysfs data\n");
  1040. return -1;
  1041. }
  1042. for_each_memory(phys, dir) {
  1043. size = max(phys, size);
  1044. }
  1045. size++;
  1046. n->set = bitmap_alloc(size);
  1047. if (!n->set) {
  1048. closedir(dir);
  1049. return -ENOMEM;
  1050. }
  1051. n->node = idx;
  1052. n->size = size;
  1053. rewinddir(dir);
  1054. for_each_memory(phys, dir) {
  1055. set_bit(phys, n->set);
  1056. }
  1057. closedir(dir);
  1058. return 0;
  1059. }
  1060. static int memory_node__sort(const void *a, const void *b)
  1061. {
  1062. const struct memory_node *na = a;
  1063. const struct memory_node *nb = b;
  1064. return na->node - nb->node;
  1065. }
  1066. static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
  1067. {
  1068. char path[PATH_MAX];
  1069. struct dirent *ent;
  1070. DIR *dir;
  1071. u64 cnt = 0;
  1072. int ret = 0;
  1073. scnprintf(path, PATH_MAX, "%s/devices/system/node/",
  1074. sysfs__mountpoint());
  1075. dir = opendir(path);
  1076. if (!dir) {
  1077. pr_debug2("%s: could't read %s, does this arch have topology information?\n",
  1078. __func__, path);
  1079. return -1;
  1080. }
  1081. while (!ret && (ent = readdir(dir))) {
  1082. unsigned int idx;
  1083. int r;
  1084. if (!strcmp(ent->d_name, ".") ||
  1085. !strcmp(ent->d_name, ".."))
  1086. continue;
  1087. r = sscanf(ent->d_name, "node%u", &idx);
  1088. if (r != 1)
  1089. continue;
  1090. if (WARN_ONCE(cnt >= size,
  1091. "failed to write MEM_TOPOLOGY, way too many nodes\n"))
  1092. return -1;
  1093. ret = memory_node__read(&nodes[cnt++], idx);
  1094. }
  1095. *cntp = cnt;
  1096. closedir(dir);
  1097. if (!ret)
  1098. qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
  1099. return ret;
  1100. }
  1101. #define MAX_MEMORY_NODES 2000
  1102. /*
  1103. * The MEM_TOPOLOGY holds physical memory map for every
  1104. * node in system. The format of data is as follows:
  1105. *
  1106. * 0 - version | for future changes
  1107. * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
  1108. * 16 - count | number of nodes
  1109. *
  1110. * For each node we store map of physical indexes for
  1111. * each node:
  1112. *
  1113. * 32 - node id | node index
  1114. * 40 - size | size of bitmap
  1115. * 48 - bitmap | bitmap of memory indexes that belongs to node
  1116. */
  1117. static int write_mem_topology(struct feat_fd *ff __maybe_unused,
  1118. struct perf_evlist *evlist __maybe_unused)
  1119. {
  1120. static struct memory_node nodes[MAX_MEMORY_NODES];
  1121. u64 bsize, version = 1, i, nr;
  1122. int ret;
  1123. ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
  1124. (unsigned long long *) &bsize);
  1125. if (ret)
  1126. return ret;
  1127. ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
  1128. if (ret)
  1129. return ret;
  1130. ret = do_write(ff, &version, sizeof(version));
  1131. if (ret < 0)
  1132. goto out;
  1133. ret = do_write(ff, &bsize, sizeof(bsize));
  1134. if (ret < 0)
  1135. goto out;
  1136. ret = do_write(ff, &nr, sizeof(nr));
  1137. if (ret < 0)
  1138. goto out;
  1139. for (i = 0; i < nr; i++) {
  1140. struct memory_node *n = &nodes[i];
  1141. #define _W(v) \
  1142. ret = do_write(ff, &n->v, sizeof(n->v)); \
  1143. if (ret < 0) \
  1144. goto out;
  1145. _W(node)
  1146. _W(size)
  1147. #undef _W
  1148. ret = do_write_bitmap(ff, n->set, n->size);
  1149. if (ret < 0)
  1150. goto out;
  1151. }
  1152. out:
  1153. return ret;
  1154. }
  1155. static void print_hostname(struct feat_fd *ff, FILE *fp)
  1156. {
  1157. fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
  1158. }
  1159. static void print_osrelease(struct feat_fd *ff, FILE *fp)
  1160. {
  1161. fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
  1162. }
  1163. static void print_arch(struct feat_fd *ff, FILE *fp)
  1164. {
  1165. fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
  1166. }
  1167. static void print_cpudesc(struct feat_fd *ff, FILE *fp)
  1168. {
  1169. fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
  1170. }
  1171. static void print_nrcpus(struct feat_fd *ff, FILE *fp)
  1172. {
  1173. fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
  1174. fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
  1175. }
  1176. static void print_version(struct feat_fd *ff, FILE *fp)
  1177. {
  1178. fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
  1179. }
  1180. static void print_cmdline(struct feat_fd *ff, FILE *fp)
  1181. {
  1182. int nr, i;
  1183. nr = ff->ph->env.nr_cmdline;
  1184. fprintf(fp, "# cmdline : ");
  1185. for (i = 0; i < nr; i++) {
  1186. char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
  1187. if (!argv_i) {
  1188. fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
  1189. } else {
  1190. char *mem = argv_i;
  1191. do {
  1192. char *quote = strchr(argv_i, '\'');
  1193. if (!quote)
  1194. break;
  1195. *quote++ = '\0';
  1196. fprintf(fp, "%s\\\'", argv_i);
  1197. argv_i = quote;
  1198. } while (1);
  1199. fprintf(fp, "%s ", argv_i);
  1200. free(mem);
  1201. }
  1202. }
  1203. fputc('\n', fp);
  1204. }
  1205. static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
  1206. {
  1207. struct perf_header *ph = ff->ph;
  1208. int cpu_nr = ph->env.nr_cpus_avail;
  1209. int nr, i;
  1210. char *str;
  1211. nr = ph->env.nr_sibling_cores;
  1212. str = ph->env.sibling_cores;
  1213. for (i = 0; i < nr; i++) {
  1214. fprintf(fp, "# sibling cores : %s\n", str);
  1215. str += strlen(str) + 1;
  1216. }
  1217. nr = ph->env.nr_sibling_threads;
  1218. str = ph->env.sibling_threads;
  1219. for (i = 0; i < nr; i++) {
  1220. fprintf(fp, "# sibling threads : %s\n", str);
  1221. str += strlen(str) + 1;
  1222. }
  1223. if (ph->env.cpu != NULL) {
  1224. for (i = 0; i < cpu_nr; i++)
  1225. fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
  1226. ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
  1227. } else
  1228. fprintf(fp, "# Core ID and Socket ID information is not available\n");
  1229. }
  1230. static void print_clockid(struct feat_fd *ff, FILE *fp)
  1231. {
  1232. fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
  1233. ff->ph->env.clockid_res_ns * 1000);
  1234. }
  1235. static void free_event_desc(struct perf_evsel *events)
  1236. {
  1237. struct perf_evsel *evsel;
  1238. if (!events)
  1239. return;
  1240. for (evsel = events; evsel->attr.size; evsel++) {
  1241. zfree(&evsel->name);
  1242. zfree(&evsel->id);
  1243. }
  1244. free(events);
  1245. }
  1246. static struct perf_evsel *read_event_desc(struct feat_fd *ff)
  1247. {
  1248. struct perf_evsel *evsel, *events = NULL;
  1249. u64 *id;
  1250. void *buf = NULL;
  1251. u32 nre, sz, nr, i, j;
  1252. size_t msz;
  1253. /* number of events */
  1254. if (do_read_u32(ff, &nre))
  1255. goto error;
  1256. if (do_read_u32(ff, &sz))
  1257. goto error;
  1258. /* buffer to hold on file attr struct */
  1259. buf = malloc(sz);
  1260. if (!buf)
  1261. goto error;
  1262. /* the last event terminates with evsel->attr.size == 0: */
  1263. events = calloc(nre + 1, sizeof(*events));
  1264. if (!events)
  1265. goto error;
  1266. msz = sizeof(evsel->attr);
  1267. if (sz < msz)
  1268. msz = sz;
  1269. for (i = 0, evsel = events; i < nre; evsel++, i++) {
  1270. evsel->idx = i;
  1271. /*
  1272. * must read entire on-file attr struct to
  1273. * sync up with layout.
  1274. */
  1275. if (__do_read(ff, buf, sz))
  1276. goto error;
  1277. if (ff->ph->needs_swap)
  1278. perf_event__attr_swap(buf);
  1279. memcpy(&evsel->attr, buf, msz);
  1280. if (do_read_u32(ff, &nr))
  1281. goto error;
  1282. if (ff->ph->needs_swap)
  1283. evsel->needs_swap = true;
  1284. evsel->name = do_read_string(ff);
  1285. if (!evsel->name)
  1286. goto error;
  1287. if (!nr)
  1288. continue;
  1289. id = calloc(nr, sizeof(*id));
  1290. if (!id)
  1291. goto error;
  1292. evsel->ids = nr;
  1293. evsel->id = id;
  1294. for (j = 0 ; j < nr; j++) {
  1295. if (do_read_u64(ff, id))
  1296. goto error;
  1297. id++;
  1298. }
  1299. }
  1300. out:
  1301. free(buf);
  1302. return events;
  1303. error:
  1304. free_event_desc(events);
  1305. events = NULL;
  1306. goto out;
  1307. }
  1308. static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
  1309. void *priv __maybe_unused)
  1310. {
  1311. return fprintf(fp, ", %s = %s", name, val);
  1312. }
  1313. static void print_event_desc(struct feat_fd *ff, FILE *fp)
  1314. {
  1315. struct perf_evsel *evsel, *events;
  1316. u32 j;
  1317. u64 *id;
  1318. if (ff->events)
  1319. events = ff->events;
  1320. else
  1321. events = read_event_desc(ff);
  1322. if (!events) {
  1323. fprintf(fp, "# event desc: not available or unable to read\n");
  1324. return;
  1325. }
  1326. for (evsel = events; evsel->attr.size; evsel++) {
  1327. fprintf(fp, "# event : name = %s, ", evsel->name);
  1328. if (evsel->ids) {
  1329. fprintf(fp, ", id = {");
  1330. for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
  1331. if (j)
  1332. fputc(',', fp);
  1333. fprintf(fp, " %"PRIu64, *id);
  1334. }
  1335. fprintf(fp, " }");
  1336. }
  1337. perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
  1338. fputc('\n', fp);
  1339. }
  1340. free_event_desc(events);
  1341. ff->events = NULL;
  1342. }
  1343. static void print_total_mem(struct feat_fd *ff, FILE *fp)
  1344. {
  1345. fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
  1346. }
  1347. static void print_numa_topology(struct feat_fd *ff, FILE *fp)
  1348. {
  1349. int i;
  1350. struct numa_node *n;
  1351. for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
  1352. n = &ff->ph->env.numa_nodes[i];
  1353. fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
  1354. " free = %"PRIu64" kB\n",
  1355. n->node, n->mem_total, n->mem_free);
  1356. fprintf(fp, "# node%u cpu list : ", n->node);
  1357. cpu_map__fprintf(n->map, fp);
  1358. }
  1359. }
  1360. static void print_cpuid(struct feat_fd *ff, FILE *fp)
  1361. {
  1362. fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
  1363. }
  1364. static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
  1365. {
  1366. fprintf(fp, "# contains samples with branch stack\n");
  1367. }
  1368. static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
  1369. {
  1370. fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
  1371. }
  1372. static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
  1373. {
  1374. fprintf(fp, "# contains stat data\n");
  1375. }
  1376. static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
  1377. {
  1378. int i;
  1379. fprintf(fp, "# CPU cache info:\n");
  1380. for (i = 0; i < ff->ph->env.caches_cnt; i++) {
  1381. fprintf(fp, "# ");
  1382. cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
  1383. }
  1384. }
  1385. static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
  1386. {
  1387. const char *delimiter = "# pmu mappings: ";
  1388. char *str, *tmp;
  1389. u32 pmu_num;
  1390. u32 type;
  1391. pmu_num = ff->ph->env.nr_pmu_mappings;
  1392. if (!pmu_num) {
  1393. fprintf(fp, "# pmu mappings: not available\n");
  1394. return;
  1395. }
  1396. str = ff->ph->env.pmu_mappings;
  1397. while (pmu_num) {
  1398. type = strtoul(str, &tmp, 0);
  1399. if (*tmp != ':')
  1400. goto error;
  1401. str = tmp + 1;
  1402. fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
  1403. delimiter = ", ";
  1404. str += strlen(str) + 1;
  1405. pmu_num--;
  1406. }
  1407. fprintf(fp, "\n");
  1408. if (!pmu_num)
  1409. return;
  1410. error:
  1411. fprintf(fp, "# pmu mappings: unable to read\n");
  1412. }
  1413. static void print_group_desc(struct feat_fd *ff, FILE *fp)
  1414. {
  1415. struct perf_session *session;
  1416. struct perf_evsel *evsel;
  1417. u32 nr = 0;
  1418. session = container_of(ff->ph, struct perf_session, header);
  1419. evlist__for_each_entry(session->evlist, evsel) {
  1420. if (perf_evsel__is_group_leader(evsel) &&
  1421. evsel->nr_members > 1) {
  1422. fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
  1423. perf_evsel__name(evsel));
  1424. nr = evsel->nr_members - 1;
  1425. } else if (nr) {
  1426. fprintf(fp, ",%s", perf_evsel__name(evsel));
  1427. if (--nr == 0)
  1428. fprintf(fp, "}\n");
  1429. }
  1430. }
  1431. }
  1432. static void print_sample_time(struct feat_fd *ff, FILE *fp)
  1433. {
  1434. struct perf_session *session;
  1435. char time_buf[32];
  1436. double d;
  1437. session = container_of(ff->ph, struct perf_session, header);
  1438. timestamp__scnprintf_usec(session->evlist->first_sample_time,
  1439. time_buf, sizeof(time_buf));
  1440. fprintf(fp, "# time of first sample : %s\n", time_buf);
  1441. timestamp__scnprintf_usec(session->evlist->last_sample_time,
  1442. time_buf, sizeof(time_buf));
  1443. fprintf(fp, "# time of last sample : %s\n", time_buf);
  1444. d = (double)(session->evlist->last_sample_time -
  1445. session->evlist->first_sample_time) / NSEC_PER_MSEC;
  1446. fprintf(fp, "# sample duration : %10.3f ms\n", d);
  1447. }
  1448. static void memory_node__fprintf(struct memory_node *n,
  1449. unsigned long long bsize, FILE *fp)
  1450. {
  1451. char buf_map[100], buf_size[50];
  1452. unsigned long long size;
  1453. size = bsize * bitmap_weight(n->set, n->size);
  1454. unit_number__scnprintf(buf_size, 50, size);
  1455. bitmap_scnprintf(n->set, n->size, buf_map, 100);
  1456. fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
  1457. }
  1458. static void print_mem_topology(struct feat_fd *ff, FILE *fp)
  1459. {
  1460. struct memory_node *nodes;
  1461. int i, nr;
  1462. nodes = ff->ph->env.memory_nodes;
  1463. nr = ff->ph->env.nr_memory_nodes;
  1464. fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
  1465. nr, ff->ph->env.memory_bsize);
  1466. for (i = 0; i < nr; i++) {
  1467. memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
  1468. }
  1469. }
  1470. static int __event_process_build_id(struct build_id_event *bev,
  1471. char *filename,
  1472. struct perf_session *session)
  1473. {
  1474. int err = -1;
  1475. struct machine *machine;
  1476. u16 cpumode;
  1477. struct dso *dso;
  1478. enum dso_kernel_type dso_type;
  1479. machine = perf_session__findnew_machine(session, bev->pid);
  1480. if (!machine)
  1481. goto out;
  1482. cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1483. switch (cpumode) {
  1484. case PERF_RECORD_MISC_KERNEL:
  1485. dso_type = DSO_TYPE_KERNEL;
  1486. break;
  1487. case PERF_RECORD_MISC_GUEST_KERNEL:
  1488. dso_type = DSO_TYPE_GUEST_KERNEL;
  1489. break;
  1490. case PERF_RECORD_MISC_USER:
  1491. case PERF_RECORD_MISC_GUEST_USER:
  1492. dso_type = DSO_TYPE_USER;
  1493. break;
  1494. default:
  1495. goto out;
  1496. }
  1497. dso = machine__findnew_dso(machine, filename);
  1498. if (dso != NULL) {
  1499. char sbuild_id[SBUILD_ID_SIZE];
  1500. dso__set_build_id(dso, &bev->build_id);
  1501. if (dso_type != DSO_TYPE_USER) {
  1502. struct kmod_path m = { .name = NULL, };
  1503. if (!kmod_path__parse_name(&m, filename) && m.kmod)
  1504. dso__set_module_info(dso, &m, machine);
  1505. else
  1506. dso->kernel = dso_type;
  1507. free(m.name);
  1508. }
  1509. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1510. sbuild_id);
  1511. pr_debug("build id event received for %s: %s\n",
  1512. dso->long_name, sbuild_id);
  1513. dso__put(dso);
  1514. }
  1515. err = 0;
  1516. out:
  1517. return err;
  1518. }
  1519. static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
  1520. int input, u64 offset, u64 size)
  1521. {
  1522. struct perf_session *session = container_of(header, struct perf_session, header);
  1523. struct {
  1524. struct perf_event_header header;
  1525. u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
  1526. char filename[0];
  1527. } old_bev;
  1528. struct build_id_event bev;
  1529. char filename[PATH_MAX];
  1530. u64 limit = offset + size;
  1531. while (offset < limit) {
  1532. ssize_t len;
  1533. if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
  1534. return -1;
  1535. if (header->needs_swap)
  1536. perf_event_header__bswap(&old_bev.header);
  1537. len = old_bev.header.size - sizeof(old_bev);
  1538. if (readn(input, filename, len) != len)
  1539. return -1;
  1540. bev.header = old_bev.header;
  1541. /*
  1542. * As the pid is the missing value, we need to fill
  1543. * it properly. The header.misc value give us nice hint.
  1544. */
  1545. bev.pid = HOST_KERNEL_ID;
  1546. if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
  1547. bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
  1548. bev.pid = DEFAULT_GUEST_KERNEL_ID;
  1549. memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
  1550. __event_process_build_id(&bev, filename, session);
  1551. offset += bev.header.size;
  1552. }
  1553. return 0;
  1554. }
  1555. static int perf_header__read_build_ids(struct perf_header *header,
  1556. int input, u64 offset, u64 size)
  1557. {
  1558. struct perf_session *session = container_of(header, struct perf_session, header);
  1559. struct build_id_event bev;
  1560. char filename[PATH_MAX];
  1561. u64 limit = offset + size, orig_offset = offset;
  1562. int err = -1;
  1563. while (offset < limit) {
  1564. ssize_t len;
  1565. if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
  1566. goto out;
  1567. if (header->needs_swap)
  1568. perf_event_header__bswap(&bev.header);
  1569. len = bev.header.size - sizeof(bev);
  1570. if (readn(input, filename, len) != len)
  1571. goto out;
  1572. /*
  1573. * The a1645ce1 changeset:
  1574. *
  1575. * "perf: 'perf kvm' tool for monitoring guest performance from host"
  1576. *
  1577. * Added a field to struct build_id_event that broke the file
  1578. * format.
  1579. *
  1580. * Since the kernel build-id is the first entry, process the
  1581. * table using the old format if the well known
  1582. * '[kernel.kallsyms]' string for the kernel build-id has the
  1583. * first 4 characters chopped off (where the pid_t sits).
  1584. */
  1585. if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
  1586. if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
  1587. return -1;
  1588. return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
  1589. }
  1590. __event_process_build_id(&bev, filename, session);
  1591. offset += bev.header.size;
  1592. }
  1593. err = 0;
  1594. out:
  1595. return err;
  1596. }
  1597. /* Macro for features that simply need to read and store a string. */
  1598. #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
  1599. static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
  1600. {\
  1601. ff->ph->env.__feat_env = do_read_string(ff); \
  1602. return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
  1603. }
  1604. FEAT_PROCESS_STR_FUN(hostname, hostname);
  1605. FEAT_PROCESS_STR_FUN(osrelease, os_release);
  1606. FEAT_PROCESS_STR_FUN(version, version);
  1607. FEAT_PROCESS_STR_FUN(arch, arch);
  1608. FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
  1609. FEAT_PROCESS_STR_FUN(cpuid, cpuid);
  1610. static int process_tracing_data(struct feat_fd *ff, void *data)
  1611. {
  1612. ssize_t ret = trace_report(ff->fd, data, false);
  1613. return ret < 0 ? -1 : 0;
  1614. }
  1615. static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
  1616. {
  1617. if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
  1618. pr_debug("Failed to read buildids, continuing...\n");
  1619. return 0;
  1620. }
  1621. static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
  1622. {
  1623. int ret;
  1624. u32 nr_cpus_avail, nr_cpus_online;
  1625. ret = do_read_u32(ff, &nr_cpus_avail);
  1626. if (ret)
  1627. return ret;
  1628. ret = do_read_u32(ff, &nr_cpus_online);
  1629. if (ret)
  1630. return ret;
  1631. ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
  1632. ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
  1633. return 0;
  1634. }
  1635. static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
  1636. {
  1637. u64 total_mem;
  1638. int ret;
  1639. ret = do_read_u64(ff, &total_mem);
  1640. if (ret)
  1641. return -1;
  1642. ff->ph->env.total_mem = (unsigned long long)total_mem;
  1643. return 0;
  1644. }
  1645. static struct perf_evsel *
  1646. perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
  1647. {
  1648. struct perf_evsel *evsel;
  1649. evlist__for_each_entry(evlist, evsel) {
  1650. if (evsel->idx == idx)
  1651. return evsel;
  1652. }
  1653. return NULL;
  1654. }
  1655. static void
  1656. perf_evlist__set_event_name(struct perf_evlist *evlist,
  1657. struct perf_evsel *event)
  1658. {
  1659. struct perf_evsel *evsel;
  1660. if (!event->name)
  1661. return;
  1662. evsel = perf_evlist__find_by_index(evlist, event->idx);
  1663. if (!evsel)
  1664. return;
  1665. if (evsel->name)
  1666. return;
  1667. evsel->name = strdup(event->name);
  1668. }
  1669. static int
  1670. process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
  1671. {
  1672. struct perf_session *session;
  1673. struct perf_evsel *evsel, *events = read_event_desc(ff);
  1674. if (!events)
  1675. return 0;
  1676. session = container_of(ff->ph, struct perf_session, header);
  1677. if (session->data->is_pipe) {
  1678. /* Save events for reading later by print_event_desc,
  1679. * since they can't be read again in pipe mode. */
  1680. ff->events = events;
  1681. }
  1682. for (evsel = events; evsel->attr.size; evsel++)
  1683. perf_evlist__set_event_name(session->evlist, evsel);
  1684. if (!session->data->is_pipe)
  1685. free_event_desc(events);
  1686. return 0;
  1687. }
  1688. static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
  1689. {
  1690. char *str, *cmdline = NULL, **argv = NULL;
  1691. u32 nr, i, len = 0;
  1692. if (do_read_u32(ff, &nr))
  1693. return -1;
  1694. ff->ph->env.nr_cmdline = nr;
  1695. cmdline = zalloc(ff->size + nr + 1);
  1696. if (!cmdline)
  1697. return -1;
  1698. argv = zalloc(sizeof(char *) * (nr + 1));
  1699. if (!argv)
  1700. goto error;
  1701. for (i = 0; i < nr; i++) {
  1702. str = do_read_string(ff);
  1703. if (!str)
  1704. goto error;
  1705. argv[i] = cmdline + len;
  1706. memcpy(argv[i], str, strlen(str) + 1);
  1707. len += strlen(str) + 1;
  1708. free(str);
  1709. }
  1710. ff->ph->env.cmdline = cmdline;
  1711. ff->ph->env.cmdline_argv = (const char **) argv;
  1712. return 0;
  1713. error:
  1714. free(argv);
  1715. free(cmdline);
  1716. return -1;
  1717. }
  1718. static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
  1719. {
  1720. u32 nr, i;
  1721. char *str;
  1722. struct strbuf sb;
  1723. int cpu_nr = ff->ph->env.nr_cpus_avail;
  1724. u64 size = 0;
  1725. struct perf_header *ph = ff->ph;
  1726. bool do_core_id_test = true;
  1727. ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
  1728. if (!ph->env.cpu)
  1729. return -1;
  1730. if (do_read_u32(ff, &nr))
  1731. goto free_cpu;
  1732. ph->env.nr_sibling_cores = nr;
  1733. size += sizeof(u32);
  1734. if (strbuf_init(&sb, 128) < 0)
  1735. goto free_cpu;
  1736. for (i = 0; i < nr; i++) {
  1737. str = do_read_string(ff);
  1738. if (!str)
  1739. goto error;
  1740. /* include a NULL character at the end */
  1741. if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
  1742. goto error;
  1743. size += string_size(str);
  1744. free(str);
  1745. }
  1746. ph->env.sibling_cores = strbuf_detach(&sb, NULL);
  1747. if (do_read_u32(ff, &nr))
  1748. return -1;
  1749. ph->env.nr_sibling_threads = nr;
  1750. size += sizeof(u32);
  1751. for (i = 0; i < nr; i++) {
  1752. str = do_read_string(ff);
  1753. if (!str)
  1754. goto error;
  1755. /* include a NULL character at the end */
  1756. if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
  1757. goto error;
  1758. size += string_size(str);
  1759. free(str);
  1760. }
  1761. ph->env.sibling_threads = strbuf_detach(&sb, NULL);
  1762. /*
  1763. * The header may be from old perf,
  1764. * which doesn't include core id and socket id information.
  1765. */
  1766. if (ff->size <= size) {
  1767. zfree(&ph->env.cpu);
  1768. return 0;
  1769. }
  1770. /* On s390 the socket_id number is not related to the numbers of cpus.
  1771. * The socket_id number might be higher than the numbers of cpus.
  1772. * This depends on the configuration.
  1773. */
  1774. if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
  1775. do_core_id_test = false;
  1776. for (i = 0; i < (u32)cpu_nr; i++) {
  1777. if (do_read_u32(ff, &nr))
  1778. goto free_cpu;
  1779. ph->env.cpu[i].core_id = nr;
  1780. if (do_read_u32(ff, &nr))
  1781. goto free_cpu;
  1782. if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
  1783. pr_debug("socket_id number is too big."
  1784. "You may need to upgrade the perf tool.\n");
  1785. goto free_cpu;
  1786. }
  1787. ph->env.cpu[i].socket_id = nr;
  1788. }
  1789. return 0;
  1790. error:
  1791. strbuf_release(&sb);
  1792. free_cpu:
  1793. zfree(&ph->env.cpu);
  1794. return -1;
  1795. }
  1796. static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
  1797. {
  1798. struct numa_node *nodes, *n;
  1799. u32 nr, i;
  1800. char *str;
  1801. /* nr nodes */
  1802. if (do_read_u32(ff, &nr))
  1803. return -1;
  1804. nodes = zalloc(sizeof(*nodes) * nr);
  1805. if (!nodes)
  1806. return -ENOMEM;
  1807. for (i = 0; i < nr; i++) {
  1808. n = &nodes[i];
  1809. /* node number */
  1810. if (do_read_u32(ff, &n->node))
  1811. goto error;
  1812. if (do_read_u64(ff, &n->mem_total))
  1813. goto error;
  1814. if (do_read_u64(ff, &n->mem_free))
  1815. goto error;
  1816. str = do_read_string(ff);
  1817. if (!str)
  1818. goto error;
  1819. n->map = cpu_map__new(str);
  1820. if (!n->map)
  1821. goto error;
  1822. free(str);
  1823. }
  1824. ff->ph->env.nr_numa_nodes = nr;
  1825. ff->ph->env.numa_nodes = nodes;
  1826. return 0;
  1827. error:
  1828. free(nodes);
  1829. return -1;
  1830. }
  1831. static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
  1832. {
  1833. char *name;
  1834. u32 pmu_num;
  1835. u32 type;
  1836. struct strbuf sb;
  1837. if (do_read_u32(ff, &pmu_num))
  1838. return -1;
  1839. if (!pmu_num) {
  1840. pr_debug("pmu mappings not available\n");
  1841. return 0;
  1842. }
  1843. ff->ph->env.nr_pmu_mappings = pmu_num;
  1844. if (strbuf_init(&sb, 128) < 0)
  1845. return -1;
  1846. while (pmu_num) {
  1847. if (do_read_u32(ff, &type))
  1848. goto error;
  1849. name = do_read_string(ff);
  1850. if (!name)
  1851. goto error;
  1852. if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
  1853. goto error;
  1854. /* include a NULL character at the end */
  1855. if (strbuf_add(&sb, "", 1) < 0)
  1856. goto error;
  1857. if (!strcmp(name, "msr"))
  1858. ff->ph->env.msr_pmu_type = type;
  1859. free(name);
  1860. pmu_num--;
  1861. }
  1862. ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
  1863. return 0;
  1864. error:
  1865. strbuf_release(&sb);
  1866. return -1;
  1867. }
  1868. static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
  1869. {
  1870. size_t ret = -1;
  1871. u32 i, nr, nr_groups;
  1872. struct perf_session *session;
  1873. struct perf_evsel *evsel, *leader = NULL;
  1874. struct group_desc {
  1875. char *name;
  1876. u32 leader_idx;
  1877. u32 nr_members;
  1878. } *desc;
  1879. if (do_read_u32(ff, &nr_groups))
  1880. return -1;
  1881. ff->ph->env.nr_groups = nr_groups;
  1882. if (!nr_groups) {
  1883. pr_debug("group desc not available\n");
  1884. return 0;
  1885. }
  1886. desc = calloc(nr_groups, sizeof(*desc));
  1887. if (!desc)
  1888. return -1;
  1889. for (i = 0; i < nr_groups; i++) {
  1890. desc[i].name = do_read_string(ff);
  1891. if (!desc[i].name)
  1892. goto out_free;
  1893. if (do_read_u32(ff, &desc[i].leader_idx))
  1894. goto out_free;
  1895. if (do_read_u32(ff, &desc[i].nr_members))
  1896. goto out_free;
  1897. }
  1898. /*
  1899. * Rebuild group relationship based on the group_desc
  1900. */
  1901. session = container_of(ff->ph, struct perf_session, header);
  1902. session->evlist->nr_groups = nr_groups;
  1903. i = nr = 0;
  1904. evlist__for_each_entry(session->evlist, evsel) {
  1905. if (evsel->idx == (int) desc[i].leader_idx) {
  1906. evsel->leader = evsel;
  1907. /* {anon_group} is a dummy name */
  1908. if (strcmp(desc[i].name, "{anon_group}")) {
  1909. evsel->group_name = desc[i].name;
  1910. desc[i].name = NULL;
  1911. }
  1912. evsel->nr_members = desc[i].nr_members;
  1913. if (i >= nr_groups || nr > 0) {
  1914. pr_debug("invalid group desc\n");
  1915. goto out_free;
  1916. }
  1917. leader = evsel;
  1918. nr = evsel->nr_members - 1;
  1919. i++;
  1920. } else if (nr) {
  1921. /* This is a group member */
  1922. evsel->leader = leader;
  1923. nr--;
  1924. }
  1925. }
  1926. if (i != nr_groups || nr != 0) {
  1927. pr_debug("invalid group desc\n");
  1928. goto out_free;
  1929. }
  1930. ret = 0;
  1931. out_free:
  1932. for (i = 0; i < nr_groups; i++)
  1933. zfree(&desc[i].name);
  1934. free(desc);
  1935. return ret;
  1936. }
  1937. static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
  1938. {
  1939. struct perf_session *session;
  1940. int err;
  1941. session = container_of(ff->ph, struct perf_session, header);
  1942. err = auxtrace_index__process(ff->fd, ff->size, session,
  1943. ff->ph->needs_swap);
  1944. if (err < 0)
  1945. pr_err("Failed to process auxtrace index\n");
  1946. return err;
  1947. }
  1948. static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
  1949. {
  1950. struct cpu_cache_level *caches;
  1951. u32 cnt, i, version;
  1952. if (do_read_u32(ff, &version))
  1953. return -1;
  1954. if (version != 1)
  1955. return -1;
  1956. if (do_read_u32(ff, &cnt))
  1957. return -1;
  1958. caches = zalloc(sizeof(*caches) * cnt);
  1959. if (!caches)
  1960. return -1;
  1961. for (i = 0; i < cnt; i++) {
  1962. struct cpu_cache_level c;
  1963. #define _R(v) \
  1964. if (do_read_u32(ff, &c.v))\
  1965. goto out_free_caches; \
  1966. _R(level)
  1967. _R(line_size)
  1968. _R(sets)
  1969. _R(ways)
  1970. #undef _R
  1971. #define _R(v) \
  1972. c.v = do_read_string(ff); \
  1973. if (!c.v) \
  1974. goto out_free_caches;
  1975. _R(type)
  1976. _R(size)
  1977. _R(map)
  1978. #undef _R
  1979. caches[i] = c;
  1980. }
  1981. ff->ph->env.caches = caches;
  1982. ff->ph->env.caches_cnt = cnt;
  1983. return 0;
  1984. out_free_caches:
  1985. free(caches);
  1986. return -1;
  1987. }
  1988. static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
  1989. {
  1990. struct perf_session *session;
  1991. u64 first_sample_time, last_sample_time;
  1992. int ret;
  1993. session = container_of(ff->ph, struct perf_session, header);
  1994. ret = do_read_u64(ff, &first_sample_time);
  1995. if (ret)
  1996. return -1;
  1997. ret = do_read_u64(ff, &last_sample_time);
  1998. if (ret)
  1999. return -1;
  2000. session->evlist->first_sample_time = first_sample_time;
  2001. session->evlist->last_sample_time = last_sample_time;
  2002. return 0;
  2003. }
  2004. static int process_mem_topology(struct feat_fd *ff,
  2005. void *data __maybe_unused)
  2006. {
  2007. struct memory_node *nodes;
  2008. u64 version, i, nr, bsize;
  2009. int ret = -1;
  2010. if (do_read_u64(ff, &version))
  2011. return -1;
  2012. if (version != 1)
  2013. return -1;
  2014. if (do_read_u64(ff, &bsize))
  2015. return -1;
  2016. if (do_read_u64(ff, &nr))
  2017. return -1;
  2018. nodes = zalloc(sizeof(*nodes) * nr);
  2019. if (!nodes)
  2020. return -1;
  2021. for (i = 0; i < nr; i++) {
  2022. struct memory_node n;
  2023. #define _R(v) \
  2024. if (do_read_u64(ff, &n.v)) \
  2025. goto out; \
  2026. _R(node)
  2027. _R(size)
  2028. #undef _R
  2029. if (do_read_bitmap(ff, &n.set, &n.size))
  2030. goto out;
  2031. nodes[i] = n;
  2032. }
  2033. ff->ph->env.memory_bsize = bsize;
  2034. ff->ph->env.memory_nodes = nodes;
  2035. ff->ph->env.nr_memory_nodes = nr;
  2036. ret = 0;
  2037. out:
  2038. if (ret)
  2039. free(nodes);
  2040. return ret;
  2041. }
  2042. static int process_clockid(struct feat_fd *ff,
  2043. void *data __maybe_unused)
  2044. {
  2045. if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
  2046. return -1;
  2047. return 0;
  2048. }
  2049. struct feature_ops {
  2050. int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
  2051. void (*print)(struct feat_fd *ff, FILE *fp);
  2052. int (*process)(struct feat_fd *ff, void *data);
  2053. const char *name;
  2054. bool full_only;
  2055. bool synthesize;
  2056. };
  2057. #define FEAT_OPR(n, func, __full_only) \
  2058. [HEADER_##n] = { \
  2059. .name = __stringify(n), \
  2060. .write = write_##func, \
  2061. .print = print_##func, \
  2062. .full_only = __full_only, \
  2063. .process = process_##func, \
  2064. .synthesize = true \
  2065. }
  2066. #define FEAT_OPN(n, func, __full_only) \
  2067. [HEADER_##n] = { \
  2068. .name = __stringify(n), \
  2069. .write = write_##func, \
  2070. .print = print_##func, \
  2071. .full_only = __full_only, \
  2072. .process = process_##func \
  2073. }
  2074. /* feature_ops not implemented: */
  2075. #define print_tracing_data NULL
  2076. #define print_build_id NULL
  2077. #define process_branch_stack NULL
  2078. #define process_stat NULL
  2079. static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
  2080. FEAT_OPN(TRACING_DATA, tracing_data, false),
  2081. FEAT_OPN(BUILD_ID, build_id, false),
  2082. FEAT_OPR(HOSTNAME, hostname, false),
  2083. FEAT_OPR(OSRELEASE, osrelease, false),
  2084. FEAT_OPR(VERSION, version, false),
  2085. FEAT_OPR(ARCH, arch, false),
  2086. FEAT_OPR(NRCPUS, nrcpus, false),
  2087. FEAT_OPR(CPUDESC, cpudesc, false),
  2088. FEAT_OPR(CPUID, cpuid, false),
  2089. FEAT_OPR(TOTAL_MEM, total_mem, false),
  2090. FEAT_OPR(EVENT_DESC, event_desc, false),
  2091. FEAT_OPR(CMDLINE, cmdline, false),
  2092. FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
  2093. FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
  2094. FEAT_OPN(BRANCH_STACK, branch_stack, false),
  2095. FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
  2096. FEAT_OPR(GROUP_DESC, group_desc, false),
  2097. FEAT_OPN(AUXTRACE, auxtrace, false),
  2098. FEAT_OPN(STAT, stat, false),
  2099. FEAT_OPN(CACHE, cache, true),
  2100. FEAT_OPR(SAMPLE_TIME, sample_time, false),
  2101. FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
  2102. FEAT_OPR(CLOCKID, clockid, false)
  2103. };
  2104. struct header_print_data {
  2105. FILE *fp;
  2106. bool full; /* extended list of headers */
  2107. };
  2108. static int perf_file_section__fprintf_info(struct perf_file_section *section,
  2109. struct perf_header *ph,
  2110. int feat, int fd, void *data)
  2111. {
  2112. struct header_print_data *hd = data;
  2113. struct feat_fd ff;
  2114. if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
  2115. pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
  2116. "%d, continuing...\n", section->offset, feat);
  2117. return 0;
  2118. }
  2119. if (feat >= HEADER_LAST_FEATURE) {
  2120. pr_warning("unknown feature %d\n", feat);
  2121. return 0;
  2122. }
  2123. if (!feat_ops[feat].print)
  2124. return 0;
  2125. ff = (struct feat_fd) {
  2126. .fd = fd,
  2127. .ph = ph,
  2128. };
  2129. if (!feat_ops[feat].full_only || hd->full)
  2130. feat_ops[feat].print(&ff, hd->fp);
  2131. else
  2132. fprintf(hd->fp, "# %s info available, use -I to display\n",
  2133. feat_ops[feat].name);
  2134. return 0;
  2135. }
  2136. int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
  2137. {
  2138. struct header_print_data hd;
  2139. struct perf_header *header = &session->header;
  2140. int fd = perf_data__fd(session->data);
  2141. struct stat st;
  2142. int ret, bit;
  2143. hd.fp = fp;
  2144. hd.full = full;
  2145. ret = fstat(fd, &st);
  2146. if (ret == -1)
  2147. return -1;
  2148. fprintf(fp, "# captured on : %s", ctime(&st.st_ctime));
  2149. fprintf(fp, "# header version : %u\n", header->version);
  2150. fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
  2151. fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
  2152. fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
  2153. perf_header__process_sections(header, fd, &hd,
  2154. perf_file_section__fprintf_info);
  2155. if (session->data->is_pipe)
  2156. return 0;
  2157. fprintf(fp, "# missing features: ");
  2158. for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
  2159. if (bit)
  2160. fprintf(fp, "%s ", feat_ops[bit].name);
  2161. }
  2162. fprintf(fp, "\n");
  2163. return 0;
  2164. }
  2165. static int do_write_feat(struct feat_fd *ff, int type,
  2166. struct perf_file_section **p,
  2167. struct perf_evlist *evlist)
  2168. {
  2169. int err;
  2170. int ret = 0;
  2171. if (perf_header__has_feat(ff->ph, type)) {
  2172. if (!feat_ops[type].write)
  2173. return -1;
  2174. if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
  2175. return -1;
  2176. (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
  2177. err = feat_ops[type].write(ff, evlist);
  2178. if (err < 0) {
  2179. pr_debug("failed to write feature %s\n", feat_ops[type].name);
  2180. /* undo anything written */
  2181. lseek(ff->fd, (*p)->offset, SEEK_SET);
  2182. return -1;
  2183. }
  2184. (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
  2185. (*p)++;
  2186. }
  2187. return ret;
  2188. }
  2189. static int perf_header__adds_write(struct perf_header *header,
  2190. struct perf_evlist *evlist, int fd)
  2191. {
  2192. int nr_sections;
  2193. struct feat_fd ff;
  2194. struct perf_file_section *feat_sec, *p;
  2195. int sec_size;
  2196. u64 sec_start;
  2197. int feat;
  2198. int err;
  2199. ff = (struct feat_fd){
  2200. .fd = fd,
  2201. .ph = header,
  2202. };
  2203. nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
  2204. if (!nr_sections)
  2205. return 0;
  2206. feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
  2207. if (feat_sec == NULL)
  2208. return -ENOMEM;
  2209. sec_size = sizeof(*feat_sec) * nr_sections;
  2210. sec_start = header->feat_offset;
  2211. lseek(fd, sec_start + sec_size, SEEK_SET);
  2212. for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
  2213. if (do_write_feat(&ff, feat, &p, evlist))
  2214. perf_header__clear_feat(header, feat);
  2215. }
  2216. lseek(fd, sec_start, SEEK_SET);
  2217. /*
  2218. * may write more than needed due to dropped feature, but
  2219. * this is okay, reader will skip the mising entries
  2220. */
  2221. err = do_write(&ff, feat_sec, sec_size);
  2222. if (err < 0)
  2223. pr_debug("failed to write feature section\n");
  2224. free(feat_sec);
  2225. return err;
  2226. }
  2227. int perf_header__write_pipe(int fd)
  2228. {
  2229. struct perf_pipe_file_header f_header;
  2230. struct feat_fd ff;
  2231. int err;
  2232. ff = (struct feat_fd){ .fd = fd };
  2233. f_header = (struct perf_pipe_file_header){
  2234. .magic = PERF_MAGIC,
  2235. .size = sizeof(f_header),
  2236. };
  2237. err = do_write(&ff, &f_header, sizeof(f_header));
  2238. if (err < 0) {
  2239. pr_debug("failed to write perf pipe header\n");
  2240. return err;
  2241. }
  2242. return 0;
  2243. }
  2244. int perf_session__write_header(struct perf_session *session,
  2245. struct perf_evlist *evlist,
  2246. int fd, bool at_exit)
  2247. {
  2248. struct perf_file_header f_header;
  2249. struct perf_file_attr f_attr;
  2250. struct perf_header *header = &session->header;
  2251. struct perf_evsel *evsel;
  2252. struct feat_fd ff;
  2253. u64 attr_offset;
  2254. int err;
  2255. ff = (struct feat_fd){ .fd = fd};
  2256. lseek(fd, sizeof(f_header), SEEK_SET);
  2257. evlist__for_each_entry(session->evlist, evsel) {
  2258. evsel->id_offset = lseek(fd, 0, SEEK_CUR);
  2259. err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
  2260. if (err < 0) {
  2261. pr_debug("failed to write perf header\n");
  2262. return err;
  2263. }
  2264. }
  2265. attr_offset = lseek(ff.fd, 0, SEEK_CUR);
  2266. evlist__for_each_entry(evlist, evsel) {
  2267. f_attr = (struct perf_file_attr){
  2268. .attr = evsel->attr,
  2269. .ids = {
  2270. .offset = evsel->id_offset,
  2271. .size = evsel->ids * sizeof(u64),
  2272. }
  2273. };
  2274. err = do_write(&ff, &f_attr, sizeof(f_attr));
  2275. if (err < 0) {
  2276. pr_debug("failed to write perf header attribute\n");
  2277. return err;
  2278. }
  2279. }
  2280. if (!header->data_offset)
  2281. header->data_offset = lseek(fd, 0, SEEK_CUR);
  2282. header->feat_offset = header->data_offset + header->data_size;
  2283. if (at_exit) {
  2284. err = perf_header__adds_write(header, evlist, fd);
  2285. if (err < 0)
  2286. return err;
  2287. }
  2288. f_header = (struct perf_file_header){
  2289. .magic = PERF_MAGIC,
  2290. .size = sizeof(f_header),
  2291. .attr_size = sizeof(f_attr),
  2292. .attrs = {
  2293. .offset = attr_offset,
  2294. .size = evlist->nr_entries * sizeof(f_attr),
  2295. },
  2296. .data = {
  2297. .offset = header->data_offset,
  2298. .size = header->data_size,
  2299. },
  2300. /* event_types is ignored, store zeros */
  2301. };
  2302. memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
  2303. lseek(fd, 0, SEEK_SET);
  2304. err = do_write(&ff, &f_header, sizeof(f_header));
  2305. if (err < 0) {
  2306. pr_debug("failed to write perf header\n");
  2307. return err;
  2308. }
  2309. lseek(fd, header->data_offset + header->data_size, SEEK_SET);
  2310. return 0;
  2311. }
  2312. static int perf_header__getbuffer64(struct perf_header *header,
  2313. int fd, void *buf, size_t size)
  2314. {
  2315. if (readn(fd, buf, size) <= 0)
  2316. return -1;
  2317. if (header->needs_swap)
  2318. mem_bswap_64(buf, size);
  2319. return 0;
  2320. }
  2321. int perf_header__process_sections(struct perf_header *header, int fd,
  2322. void *data,
  2323. int (*process)(struct perf_file_section *section,
  2324. struct perf_header *ph,
  2325. int feat, int fd, void *data))
  2326. {
  2327. struct perf_file_section *feat_sec, *sec;
  2328. int nr_sections;
  2329. int sec_size;
  2330. int feat;
  2331. int err;
  2332. nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
  2333. if (!nr_sections)
  2334. return 0;
  2335. feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
  2336. if (!feat_sec)
  2337. return -1;
  2338. sec_size = sizeof(*feat_sec) * nr_sections;
  2339. lseek(fd, header->feat_offset, SEEK_SET);
  2340. err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
  2341. if (err < 0)
  2342. goto out_free;
  2343. for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
  2344. err = process(sec++, header, feat, fd, data);
  2345. if (err < 0)
  2346. goto out_free;
  2347. }
  2348. err = 0;
  2349. out_free:
  2350. free(feat_sec);
  2351. return err;
  2352. }
  2353. static const int attr_file_abi_sizes[] = {
  2354. [0] = PERF_ATTR_SIZE_VER0,
  2355. [1] = PERF_ATTR_SIZE_VER1,
  2356. [2] = PERF_ATTR_SIZE_VER2,
  2357. [3] = PERF_ATTR_SIZE_VER3,
  2358. [4] = PERF_ATTR_SIZE_VER4,
  2359. 0,
  2360. };
  2361. /*
  2362. * In the legacy file format, the magic number is not used to encode endianness.
  2363. * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
  2364. * on ABI revisions, we need to try all combinations for all endianness to
  2365. * detect the endianness.
  2366. */
  2367. static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
  2368. {
  2369. uint64_t ref_size, attr_size;
  2370. int i;
  2371. for (i = 0 ; attr_file_abi_sizes[i]; i++) {
  2372. ref_size = attr_file_abi_sizes[i]
  2373. + sizeof(struct perf_file_section);
  2374. if (hdr_sz != ref_size) {
  2375. attr_size = bswap_64(hdr_sz);
  2376. if (attr_size != ref_size)
  2377. continue;
  2378. ph->needs_swap = true;
  2379. }
  2380. pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
  2381. i,
  2382. ph->needs_swap);
  2383. return 0;
  2384. }
  2385. /* could not determine endianness */
  2386. return -1;
  2387. }
  2388. #define PERF_PIPE_HDR_VER0 16
  2389. static const size_t attr_pipe_abi_sizes[] = {
  2390. [0] = PERF_PIPE_HDR_VER0,
  2391. 0,
  2392. };
  2393. /*
  2394. * In the legacy pipe format, there is an implicit assumption that endiannesss
  2395. * between host recording the samples, and host parsing the samples is the
  2396. * same. This is not always the case given that the pipe output may always be
  2397. * redirected into a file and analyzed on a different machine with possibly a
  2398. * different endianness and perf_event ABI revsions in the perf tool itself.
  2399. */
  2400. static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
  2401. {
  2402. u64 attr_size;
  2403. int i;
  2404. for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
  2405. if (hdr_sz != attr_pipe_abi_sizes[i]) {
  2406. attr_size = bswap_64(hdr_sz);
  2407. if (attr_size != hdr_sz)
  2408. continue;
  2409. ph->needs_swap = true;
  2410. }
  2411. pr_debug("Pipe ABI%d perf.data file detected\n", i);
  2412. return 0;
  2413. }
  2414. return -1;
  2415. }
  2416. bool is_perf_magic(u64 magic)
  2417. {
  2418. if (!memcmp(&magic, __perf_magic1, sizeof(magic))
  2419. || magic == __perf_magic2
  2420. || magic == __perf_magic2_sw)
  2421. return true;
  2422. return false;
  2423. }
  2424. static int check_magic_endian(u64 magic, uint64_t hdr_sz,
  2425. bool is_pipe, struct perf_header *ph)
  2426. {
  2427. int ret;
  2428. /* check for legacy format */
  2429. ret = memcmp(&magic, __perf_magic1, sizeof(magic));
  2430. if (ret == 0) {
  2431. ph->version = PERF_HEADER_VERSION_1;
  2432. pr_debug("legacy perf.data format\n");
  2433. if (is_pipe)
  2434. return try_all_pipe_abis(hdr_sz, ph);
  2435. return try_all_file_abis(hdr_sz, ph);
  2436. }
  2437. /*
  2438. * the new magic number serves two purposes:
  2439. * - unique number to identify actual perf.data files
  2440. * - encode endianness of file
  2441. */
  2442. ph->version = PERF_HEADER_VERSION_2;
  2443. /* check magic number with one endianness */
  2444. if (magic == __perf_magic2)
  2445. return 0;
  2446. /* check magic number with opposite endianness */
  2447. if (magic != __perf_magic2_sw)
  2448. return -1;
  2449. ph->needs_swap = true;
  2450. return 0;
  2451. }
  2452. int perf_file_header__read(struct perf_file_header *header,
  2453. struct perf_header *ph, int fd)
  2454. {
  2455. ssize_t ret;
  2456. lseek(fd, 0, SEEK_SET);
  2457. ret = readn(fd, header, sizeof(*header));
  2458. if (ret <= 0)
  2459. return -1;
  2460. if (check_magic_endian(header->magic,
  2461. header->attr_size, false, ph) < 0) {
  2462. pr_debug("magic/endian check failed\n");
  2463. return -1;
  2464. }
  2465. if (ph->needs_swap) {
  2466. mem_bswap_64(header, offsetof(struct perf_file_header,
  2467. adds_features));
  2468. }
  2469. if (header->size != sizeof(*header)) {
  2470. /* Support the previous format */
  2471. if (header->size == offsetof(typeof(*header), adds_features))
  2472. bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
  2473. else
  2474. return -1;
  2475. } else if (ph->needs_swap) {
  2476. /*
  2477. * feature bitmap is declared as an array of unsigned longs --
  2478. * not good since its size can differ between the host that
  2479. * generated the data file and the host analyzing the file.
  2480. *
  2481. * We need to handle endianness, but we don't know the size of
  2482. * the unsigned long where the file was generated. Take a best
  2483. * guess at determining it: try 64-bit swap first (ie., file
  2484. * created on a 64-bit host), and check if the hostname feature
  2485. * bit is set (this feature bit is forced on as of fbe96f2).
  2486. * If the bit is not, undo the 64-bit swap and try a 32-bit
  2487. * swap. If the hostname bit is still not set (e.g., older data
  2488. * file), punt and fallback to the original behavior --
  2489. * clearing all feature bits and setting buildid.
  2490. */
  2491. mem_bswap_64(&header->adds_features,
  2492. BITS_TO_U64(HEADER_FEAT_BITS));
  2493. if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
  2494. /* unswap as u64 */
  2495. mem_bswap_64(&header->adds_features,
  2496. BITS_TO_U64(HEADER_FEAT_BITS));
  2497. /* unswap as u32 */
  2498. mem_bswap_32(&header->adds_features,
  2499. BITS_TO_U32(HEADER_FEAT_BITS));
  2500. }
  2501. if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
  2502. bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
  2503. set_bit(HEADER_BUILD_ID, header->adds_features);
  2504. }
  2505. }
  2506. memcpy(&ph->adds_features, &header->adds_features,
  2507. sizeof(ph->adds_features));
  2508. ph->data_offset = header->data.offset;
  2509. ph->data_size = header->data.size;
  2510. ph->feat_offset = header->data.offset + header->data.size;
  2511. return 0;
  2512. }
  2513. static int perf_file_section__process(struct perf_file_section *section,
  2514. struct perf_header *ph,
  2515. int feat, int fd, void *data)
  2516. {
  2517. struct feat_fd fdd = {
  2518. .fd = fd,
  2519. .ph = ph,
  2520. .size = section->size,
  2521. .offset = section->offset,
  2522. };
  2523. if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
  2524. pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
  2525. "%d, continuing...\n", section->offset, feat);
  2526. return 0;
  2527. }
  2528. if (feat >= HEADER_LAST_FEATURE) {
  2529. pr_debug("unknown feature %d, continuing...\n", feat);
  2530. return 0;
  2531. }
  2532. if (!feat_ops[feat].process)
  2533. return 0;
  2534. return feat_ops[feat].process(&fdd, data);
  2535. }
  2536. static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
  2537. struct perf_header *ph, int fd,
  2538. bool repipe)
  2539. {
  2540. struct feat_fd ff = {
  2541. .fd = STDOUT_FILENO,
  2542. .ph = ph,
  2543. };
  2544. ssize_t ret;
  2545. ret = readn(fd, header, sizeof(*header));
  2546. if (ret <= 0)
  2547. return -1;
  2548. if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
  2549. pr_debug("endian/magic failed\n");
  2550. return -1;
  2551. }
  2552. if (ph->needs_swap)
  2553. header->size = bswap_64(header->size);
  2554. if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
  2555. return -1;
  2556. return 0;
  2557. }
  2558. static int perf_header__read_pipe(struct perf_session *session)
  2559. {
  2560. struct perf_header *header = &session->header;
  2561. struct perf_pipe_file_header f_header;
  2562. if (perf_file_header__read_pipe(&f_header, header,
  2563. perf_data__fd(session->data),
  2564. session->repipe) < 0) {
  2565. pr_debug("incompatible file format\n");
  2566. return -EINVAL;
  2567. }
  2568. return 0;
  2569. }
  2570. static int read_attr(int fd, struct perf_header *ph,
  2571. struct perf_file_attr *f_attr)
  2572. {
  2573. struct perf_event_attr *attr = &f_attr->attr;
  2574. size_t sz, left;
  2575. size_t our_sz = sizeof(f_attr->attr);
  2576. ssize_t ret;
  2577. memset(f_attr, 0, sizeof(*f_attr));
  2578. /* read minimal guaranteed structure */
  2579. ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
  2580. if (ret <= 0) {
  2581. pr_debug("cannot read %d bytes of header attr\n",
  2582. PERF_ATTR_SIZE_VER0);
  2583. return -1;
  2584. }
  2585. /* on file perf_event_attr size */
  2586. sz = attr->size;
  2587. if (ph->needs_swap)
  2588. sz = bswap_32(sz);
  2589. if (sz == 0) {
  2590. /* assume ABI0 */
  2591. sz = PERF_ATTR_SIZE_VER0;
  2592. } else if (sz > our_sz) {
  2593. pr_debug("file uses a more recent and unsupported ABI"
  2594. " (%zu bytes extra)\n", sz - our_sz);
  2595. return -1;
  2596. }
  2597. /* what we have not yet read and that we know about */
  2598. left = sz - PERF_ATTR_SIZE_VER0;
  2599. if (left) {
  2600. void *ptr = attr;
  2601. ptr += PERF_ATTR_SIZE_VER0;
  2602. ret = readn(fd, ptr, left);
  2603. }
  2604. /* read perf_file_section, ids are read in caller */
  2605. ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
  2606. return ret <= 0 ? -1 : 0;
  2607. }
  2608. static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
  2609. struct tep_handle *pevent)
  2610. {
  2611. struct tep_event_format *event;
  2612. char bf[128];
  2613. /* already prepared */
  2614. if (evsel->tp_format)
  2615. return 0;
  2616. if (pevent == NULL) {
  2617. pr_debug("broken or missing trace data\n");
  2618. return -1;
  2619. }
  2620. event = tep_find_event(pevent, evsel->attr.config);
  2621. if (event == NULL) {
  2622. pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
  2623. return -1;
  2624. }
  2625. if (!evsel->name) {
  2626. snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
  2627. evsel->name = strdup(bf);
  2628. if (evsel->name == NULL)
  2629. return -1;
  2630. }
  2631. evsel->tp_format = event;
  2632. return 0;
  2633. }
  2634. static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
  2635. struct tep_handle *pevent)
  2636. {
  2637. struct perf_evsel *pos;
  2638. evlist__for_each_entry(evlist, pos) {
  2639. if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
  2640. perf_evsel__prepare_tracepoint_event(pos, pevent))
  2641. return -1;
  2642. }
  2643. return 0;
  2644. }
  2645. int perf_session__read_header(struct perf_session *session)
  2646. {
  2647. struct perf_data *data = session->data;
  2648. struct perf_header *header = &session->header;
  2649. struct perf_file_header f_header;
  2650. struct perf_file_attr f_attr;
  2651. u64 f_id;
  2652. int nr_attrs, nr_ids, i, j;
  2653. int fd = perf_data__fd(data);
  2654. session->evlist = perf_evlist__new();
  2655. if (session->evlist == NULL)
  2656. return -ENOMEM;
  2657. session->evlist->env = &header->env;
  2658. session->machines.host.env = &header->env;
  2659. if (perf_data__is_pipe(data))
  2660. return perf_header__read_pipe(session);
  2661. if (perf_file_header__read(&f_header, header, fd) < 0)
  2662. return -EINVAL;
  2663. /*
  2664. * Sanity check that perf.data was written cleanly; data size is
  2665. * initialized to 0 and updated only if the on_exit function is run.
  2666. * If data size is still 0 then the file contains only partial
  2667. * information. Just warn user and process it as much as it can.
  2668. */
  2669. if (f_header.data.size == 0) {
  2670. pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
  2671. "Was the 'perf record' command properly terminated?\n",
  2672. data->file.path);
  2673. }
  2674. nr_attrs = f_header.attrs.size / f_header.attr_size;
  2675. lseek(fd, f_header.attrs.offset, SEEK_SET);
  2676. for (i = 0; i < nr_attrs; i++) {
  2677. struct perf_evsel *evsel;
  2678. off_t tmp;
  2679. if (read_attr(fd, header, &f_attr) < 0)
  2680. goto out_errno;
  2681. if (header->needs_swap) {
  2682. f_attr.ids.size = bswap_64(f_attr.ids.size);
  2683. f_attr.ids.offset = bswap_64(f_attr.ids.offset);
  2684. perf_event__attr_swap(&f_attr.attr);
  2685. }
  2686. tmp = lseek(fd, 0, SEEK_CUR);
  2687. evsel = perf_evsel__new(&f_attr.attr);
  2688. if (evsel == NULL)
  2689. goto out_delete_evlist;
  2690. evsel->needs_swap = header->needs_swap;
  2691. /*
  2692. * Do it before so that if perf_evsel__alloc_id fails, this
  2693. * entry gets purged too at perf_evlist__delete().
  2694. */
  2695. perf_evlist__add(session->evlist, evsel);
  2696. nr_ids = f_attr.ids.size / sizeof(u64);
  2697. /*
  2698. * We don't have the cpu and thread maps on the header, so
  2699. * for allocating the perf_sample_id table we fake 1 cpu and
  2700. * hattr->ids threads.
  2701. */
  2702. if (perf_evsel__alloc_id(evsel, 1, nr_ids))
  2703. goto out_delete_evlist;
  2704. lseek(fd, f_attr.ids.offset, SEEK_SET);
  2705. for (j = 0; j < nr_ids; j++) {
  2706. if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
  2707. goto out_errno;
  2708. perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
  2709. }
  2710. lseek(fd, tmp, SEEK_SET);
  2711. }
  2712. perf_header__process_sections(header, fd, &session->tevent,
  2713. perf_file_section__process);
  2714. if (perf_evlist__prepare_tracepoint_events(session->evlist,
  2715. session->tevent.pevent))
  2716. goto out_delete_evlist;
  2717. return 0;
  2718. out_errno:
  2719. return -errno;
  2720. out_delete_evlist:
  2721. perf_evlist__delete(session->evlist);
  2722. session->evlist = NULL;
  2723. return -ENOMEM;
  2724. }
  2725. int perf_event__synthesize_attr(struct perf_tool *tool,
  2726. struct perf_event_attr *attr, u32 ids, u64 *id,
  2727. perf_event__handler_t process)
  2728. {
  2729. union perf_event *ev;
  2730. size_t size;
  2731. int err;
  2732. size = sizeof(struct perf_event_attr);
  2733. size = PERF_ALIGN(size, sizeof(u64));
  2734. size += sizeof(struct perf_event_header);
  2735. size += ids * sizeof(u64);
  2736. ev = malloc(size);
  2737. if (ev == NULL)
  2738. return -ENOMEM;
  2739. ev->attr.attr = *attr;
  2740. memcpy(ev->attr.id, id, ids * sizeof(u64));
  2741. ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
  2742. ev->attr.header.size = (u16)size;
  2743. if (ev->attr.header.size == size)
  2744. err = process(tool, ev, NULL, NULL);
  2745. else
  2746. err = -E2BIG;
  2747. free(ev);
  2748. return err;
  2749. }
  2750. int perf_event__synthesize_features(struct perf_tool *tool,
  2751. struct perf_session *session,
  2752. struct perf_evlist *evlist,
  2753. perf_event__handler_t process)
  2754. {
  2755. struct perf_header *header = &session->header;
  2756. struct feat_fd ff;
  2757. struct feature_event *fe;
  2758. size_t sz, sz_hdr;
  2759. int feat, ret;
  2760. sz_hdr = sizeof(fe->header);
  2761. sz = sizeof(union perf_event);
  2762. /* get a nice alignment */
  2763. sz = PERF_ALIGN(sz, page_size);
  2764. memset(&ff, 0, sizeof(ff));
  2765. ff.buf = malloc(sz);
  2766. if (!ff.buf)
  2767. return -ENOMEM;
  2768. ff.size = sz - sz_hdr;
  2769. for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
  2770. if (!feat_ops[feat].synthesize) {
  2771. pr_debug("No record header feature for header :%d\n", feat);
  2772. continue;
  2773. }
  2774. ff.offset = sizeof(*fe);
  2775. ret = feat_ops[feat].write(&ff, evlist);
  2776. if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
  2777. pr_debug("Error writing feature\n");
  2778. continue;
  2779. }
  2780. /* ff.buf may have changed due to realloc in do_write() */
  2781. fe = ff.buf;
  2782. memset(fe, 0, sizeof(*fe));
  2783. fe->feat_id = feat;
  2784. fe->header.type = PERF_RECORD_HEADER_FEATURE;
  2785. fe->header.size = ff.offset;
  2786. ret = process(tool, ff.buf, NULL, NULL);
  2787. if (ret) {
  2788. free(ff.buf);
  2789. return ret;
  2790. }
  2791. }
  2792. /* Send HEADER_LAST_FEATURE mark. */
  2793. fe = ff.buf;
  2794. fe->feat_id = HEADER_LAST_FEATURE;
  2795. fe->header.type = PERF_RECORD_HEADER_FEATURE;
  2796. fe->header.size = sizeof(*fe);
  2797. ret = process(tool, ff.buf, NULL, NULL);
  2798. free(ff.buf);
  2799. return ret;
  2800. }
  2801. int perf_event__process_feature(struct perf_session *session,
  2802. union perf_event *event)
  2803. {
  2804. struct perf_tool *tool = session->tool;
  2805. struct feat_fd ff = { .fd = 0 };
  2806. struct feature_event *fe = (struct feature_event *)event;
  2807. int type = fe->header.type;
  2808. u64 feat = fe->feat_id;
  2809. if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
  2810. pr_warning("invalid record type %d in pipe-mode\n", type);
  2811. return 0;
  2812. }
  2813. if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
  2814. pr_warning("invalid record type %d in pipe-mode\n", type);
  2815. return -1;
  2816. }
  2817. if (!feat_ops[feat].process)
  2818. return 0;
  2819. ff.buf = (void *)fe->data;
  2820. ff.size = event->header.size - sizeof(event->header);
  2821. ff.ph = &session->header;
  2822. if (feat_ops[feat].process(&ff, NULL))
  2823. return -1;
  2824. if (!feat_ops[feat].print || !tool->show_feat_hdr)
  2825. return 0;
  2826. if (!feat_ops[feat].full_only ||
  2827. tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
  2828. feat_ops[feat].print(&ff, stdout);
  2829. } else {
  2830. fprintf(stdout, "# %s info available, use -I to display\n",
  2831. feat_ops[feat].name);
  2832. }
  2833. return 0;
  2834. }
  2835. static struct event_update_event *
  2836. event_update_event__new(size_t size, u64 type, u64 id)
  2837. {
  2838. struct event_update_event *ev;
  2839. size += sizeof(*ev);
  2840. size = PERF_ALIGN(size, sizeof(u64));
  2841. ev = zalloc(size);
  2842. if (ev) {
  2843. ev->header.type = PERF_RECORD_EVENT_UPDATE;
  2844. ev->header.size = (u16)size;
  2845. ev->type = type;
  2846. ev->id = id;
  2847. }
  2848. return ev;
  2849. }
  2850. int
  2851. perf_event__synthesize_event_update_unit(struct perf_tool *tool,
  2852. struct perf_evsel *evsel,
  2853. perf_event__handler_t process)
  2854. {
  2855. struct event_update_event *ev;
  2856. size_t size = strlen(evsel->unit);
  2857. int err;
  2858. ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
  2859. if (ev == NULL)
  2860. return -ENOMEM;
  2861. strncpy(ev->data, evsel->unit, size);
  2862. err = process(tool, (union perf_event *)ev, NULL, NULL);
  2863. free(ev);
  2864. return err;
  2865. }
  2866. int
  2867. perf_event__synthesize_event_update_scale(struct perf_tool *tool,
  2868. struct perf_evsel *evsel,
  2869. perf_event__handler_t process)
  2870. {
  2871. struct event_update_event *ev;
  2872. struct event_update_event_scale *ev_data;
  2873. int err;
  2874. ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
  2875. if (ev == NULL)
  2876. return -ENOMEM;
  2877. ev_data = (struct event_update_event_scale *) ev->data;
  2878. ev_data->scale = evsel->scale;
  2879. err = process(tool, (union perf_event*) ev, NULL, NULL);
  2880. free(ev);
  2881. return err;
  2882. }
  2883. int
  2884. perf_event__synthesize_event_update_name(struct perf_tool *tool,
  2885. struct perf_evsel *evsel,
  2886. perf_event__handler_t process)
  2887. {
  2888. struct event_update_event *ev;
  2889. size_t len = strlen(evsel->name);
  2890. int err;
  2891. ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
  2892. if (ev == NULL)
  2893. return -ENOMEM;
  2894. strncpy(ev->data, evsel->name, len);
  2895. err = process(tool, (union perf_event*) ev, NULL, NULL);
  2896. free(ev);
  2897. return err;
  2898. }
  2899. int
  2900. perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
  2901. struct perf_evsel *evsel,
  2902. perf_event__handler_t process)
  2903. {
  2904. size_t size = sizeof(struct event_update_event);
  2905. struct event_update_event *ev;
  2906. int max, err;
  2907. u16 type;
  2908. if (!evsel->own_cpus)
  2909. return 0;
  2910. ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
  2911. if (!ev)
  2912. return -ENOMEM;
  2913. ev->header.type = PERF_RECORD_EVENT_UPDATE;
  2914. ev->header.size = (u16)size;
  2915. ev->type = PERF_EVENT_UPDATE__CPUS;
  2916. ev->id = evsel->id[0];
  2917. cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
  2918. evsel->own_cpus,
  2919. type, max);
  2920. err = process(tool, (union perf_event*) ev, NULL, NULL);
  2921. free(ev);
  2922. return err;
  2923. }
  2924. size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
  2925. {
  2926. struct event_update_event *ev = &event->event_update;
  2927. struct event_update_event_scale *ev_scale;
  2928. struct event_update_event_cpus *ev_cpus;
  2929. struct cpu_map *map;
  2930. size_t ret;
  2931. ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
  2932. switch (ev->type) {
  2933. case PERF_EVENT_UPDATE__SCALE:
  2934. ev_scale = (struct event_update_event_scale *) ev->data;
  2935. ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
  2936. break;
  2937. case PERF_EVENT_UPDATE__UNIT:
  2938. ret += fprintf(fp, "... unit: %s\n", ev->data);
  2939. break;
  2940. case PERF_EVENT_UPDATE__NAME:
  2941. ret += fprintf(fp, "... name: %s\n", ev->data);
  2942. break;
  2943. case PERF_EVENT_UPDATE__CPUS:
  2944. ev_cpus = (struct event_update_event_cpus *) ev->data;
  2945. ret += fprintf(fp, "... ");
  2946. map = cpu_map__new_data(&ev_cpus->cpus);
  2947. if (map)
  2948. ret += cpu_map__fprintf(map, fp);
  2949. else
  2950. ret += fprintf(fp, "failed to get cpus\n");
  2951. break;
  2952. default:
  2953. ret += fprintf(fp, "... unknown type\n");
  2954. break;
  2955. }
  2956. return ret;
  2957. }
  2958. int perf_event__synthesize_attrs(struct perf_tool *tool,
  2959. struct perf_evlist *evlist,
  2960. perf_event__handler_t process)
  2961. {
  2962. struct perf_evsel *evsel;
  2963. int err = 0;
  2964. evlist__for_each_entry(evlist, evsel) {
  2965. err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
  2966. evsel->id, process);
  2967. if (err) {
  2968. pr_debug("failed to create perf header attribute\n");
  2969. return err;
  2970. }
  2971. }
  2972. return err;
  2973. }
  2974. static bool has_unit(struct perf_evsel *counter)
  2975. {
  2976. return counter->unit && *counter->unit;
  2977. }
  2978. static bool has_scale(struct perf_evsel *counter)
  2979. {
  2980. return counter->scale != 1;
  2981. }
  2982. int perf_event__synthesize_extra_attr(struct perf_tool *tool,
  2983. struct perf_evlist *evsel_list,
  2984. perf_event__handler_t process,
  2985. bool is_pipe)
  2986. {
  2987. struct perf_evsel *counter;
  2988. int err;
  2989. /*
  2990. * Synthesize other events stuff not carried within
  2991. * attr event - unit, scale, name
  2992. */
  2993. evlist__for_each_entry(evsel_list, counter) {
  2994. if (!counter->supported)
  2995. continue;
  2996. /*
  2997. * Synthesize unit and scale only if it's defined.
  2998. */
  2999. if (has_unit(counter)) {
  3000. err = perf_event__synthesize_event_update_unit(tool, counter, process);
  3001. if (err < 0) {
  3002. pr_err("Couldn't synthesize evsel unit.\n");
  3003. return err;
  3004. }
  3005. }
  3006. if (has_scale(counter)) {
  3007. err = perf_event__synthesize_event_update_scale(tool, counter, process);
  3008. if (err < 0) {
  3009. pr_err("Couldn't synthesize evsel counter.\n");
  3010. return err;
  3011. }
  3012. }
  3013. if (counter->own_cpus) {
  3014. err = perf_event__synthesize_event_update_cpus(tool, counter, process);
  3015. if (err < 0) {
  3016. pr_err("Couldn't synthesize evsel cpus.\n");
  3017. return err;
  3018. }
  3019. }
  3020. /*
  3021. * Name is needed only for pipe output,
  3022. * perf.data carries event names.
  3023. */
  3024. if (is_pipe) {
  3025. err = perf_event__synthesize_event_update_name(tool, counter, process);
  3026. if (err < 0) {
  3027. pr_err("Couldn't synthesize evsel name.\n");
  3028. return err;
  3029. }
  3030. }
  3031. }
  3032. return 0;
  3033. }
  3034. int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
  3035. union perf_event *event,
  3036. struct perf_evlist **pevlist)
  3037. {
  3038. u32 i, ids, n_ids;
  3039. struct perf_evsel *evsel;
  3040. struct perf_evlist *evlist = *pevlist;
  3041. if (evlist == NULL) {
  3042. *pevlist = evlist = perf_evlist__new();
  3043. if (evlist == NULL)
  3044. return -ENOMEM;
  3045. }
  3046. evsel = perf_evsel__new(&event->attr.attr);
  3047. if (evsel == NULL)
  3048. return -ENOMEM;
  3049. perf_evlist__add(evlist, evsel);
  3050. ids = event->header.size;
  3051. ids -= (void *)&event->attr.id - (void *)event;
  3052. n_ids = ids / sizeof(u64);
  3053. /*
  3054. * We don't have the cpu and thread maps on the header, so
  3055. * for allocating the perf_sample_id table we fake 1 cpu and
  3056. * hattr->ids threads.
  3057. */
  3058. if (perf_evsel__alloc_id(evsel, 1, n_ids))
  3059. return -ENOMEM;
  3060. for (i = 0; i < n_ids; i++) {
  3061. perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
  3062. }
  3063. return 0;
  3064. }
  3065. int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
  3066. union perf_event *event,
  3067. struct perf_evlist **pevlist)
  3068. {
  3069. struct event_update_event *ev = &event->event_update;
  3070. struct event_update_event_scale *ev_scale;
  3071. struct event_update_event_cpus *ev_cpus;
  3072. struct perf_evlist *evlist;
  3073. struct perf_evsel *evsel;
  3074. struct cpu_map *map;
  3075. if (!pevlist || *pevlist == NULL)
  3076. return -EINVAL;
  3077. evlist = *pevlist;
  3078. evsel = perf_evlist__id2evsel(evlist, ev->id);
  3079. if (evsel == NULL)
  3080. return -EINVAL;
  3081. switch (ev->type) {
  3082. case PERF_EVENT_UPDATE__UNIT:
  3083. evsel->unit = strdup(ev->data);
  3084. break;
  3085. case PERF_EVENT_UPDATE__NAME:
  3086. evsel->name = strdup(ev->data);
  3087. break;
  3088. case PERF_EVENT_UPDATE__SCALE:
  3089. ev_scale = (struct event_update_event_scale *) ev->data;
  3090. evsel->scale = ev_scale->scale;
  3091. break;
  3092. case PERF_EVENT_UPDATE__CPUS:
  3093. ev_cpus = (struct event_update_event_cpus *) ev->data;
  3094. map = cpu_map__new_data(&ev_cpus->cpus);
  3095. if (map)
  3096. evsel->own_cpus = map;
  3097. else
  3098. pr_err("failed to get event_update cpus\n");
  3099. default:
  3100. break;
  3101. }
  3102. return 0;
  3103. }
  3104. int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
  3105. struct perf_evlist *evlist,
  3106. perf_event__handler_t process)
  3107. {
  3108. union perf_event ev;
  3109. struct tracing_data *tdata;
  3110. ssize_t size = 0, aligned_size = 0, padding;
  3111. struct feat_fd ff;
  3112. int err __maybe_unused = 0;
  3113. /*
  3114. * We are going to store the size of the data followed
  3115. * by the data contents. Since the fd descriptor is a pipe,
  3116. * we cannot seek back to store the size of the data once
  3117. * we know it. Instead we:
  3118. *
  3119. * - write the tracing data to the temp file
  3120. * - get/write the data size to pipe
  3121. * - write the tracing data from the temp file
  3122. * to the pipe
  3123. */
  3124. tdata = tracing_data_get(&evlist->entries, fd, true);
  3125. if (!tdata)
  3126. return -1;
  3127. memset(&ev, 0, sizeof(ev));
  3128. ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
  3129. size = tdata->size;
  3130. aligned_size = PERF_ALIGN(size, sizeof(u64));
  3131. padding = aligned_size - size;
  3132. ev.tracing_data.header.size = sizeof(ev.tracing_data);
  3133. ev.tracing_data.size = aligned_size;
  3134. process(tool, &ev, NULL, NULL);
  3135. /*
  3136. * The put function will copy all the tracing data
  3137. * stored in temp file to the pipe.
  3138. */
  3139. tracing_data_put(tdata);
  3140. ff = (struct feat_fd){ .fd = fd };
  3141. if (write_padded(&ff, NULL, 0, padding))
  3142. return -1;
  3143. return aligned_size;
  3144. }
  3145. int perf_event__process_tracing_data(struct perf_session *session,
  3146. union perf_event *event)
  3147. {
  3148. ssize_t size_read, padding, size = event->tracing_data.size;
  3149. int fd = perf_data__fd(session->data);
  3150. off_t offset = lseek(fd, 0, SEEK_CUR);
  3151. char buf[BUFSIZ];
  3152. /* setup for reading amidst mmap */
  3153. lseek(fd, offset + sizeof(struct tracing_data_event),
  3154. SEEK_SET);
  3155. size_read = trace_report(fd, &session->tevent,
  3156. session->repipe);
  3157. padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
  3158. if (readn(fd, buf, padding) < 0) {
  3159. pr_err("%s: reading input file", __func__);
  3160. return -1;
  3161. }
  3162. if (session->repipe) {
  3163. int retw = write(STDOUT_FILENO, buf, padding);
  3164. if (retw <= 0 || retw != padding) {
  3165. pr_err("%s: repiping tracing data padding", __func__);
  3166. return -1;
  3167. }
  3168. }
  3169. if (size_read + padding != size) {
  3170. pr_err("%s: tracing data size mismatch", __func__);
  3171. return -1;
  3172. }
  3173. perf_evlist__prepare_tracepoint_events(session->evlist,
  3174. session->tevent.pevent);
  3175. return size_read + padding;
  3176. }
  3177. int perf_event__synthesize_build_id(struct perf_tool *tool,
  3178. struct dso *pos, u16 misc,
  3179. perf_event__handler_t process,
  3180. struct machine *machine)
  3181. {
  3182. union perf_event ev;
  3183. size_t len;
  3184. int err = 0;
  3185. if (!pos->hit)
  3186. return err;
  3187. memset(&ev, 0, sizeof(ev));
  3188. len = pos->long_name_len + 1;
  3189. len = PERF_ALIGN(len, NAME_ALIGN);
  3190. memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
  3191. ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
  3192. ev.build_id.header.misc = misc;
  3193. ev.build_id.pid = machine->pid;
  3194. ev.build_id.header.size = sizeof(ev.build_id) + len;
  3195. memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
  3196. err = process(tool, &ev, NULL, machine);
  3197. return err;
  3198. }
  3199. int perf_event__process_build_id(struct perf_session *session,
  3200. union perf_event *event)
  3201. {
  3202. __event_process_build_id(&event->build_id,
  3203. event->build_id.filename,
  3204. session);
  3205. return 0;
  3206. }