header.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946
  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 cpu_cache_level__sort(const void *a, const void *b)
  854. {
  855. struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
  856. struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
  857. return cache_a->level - cache_b->level;
  858. }
  859. static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
  860. {
  861. if (a->level != b->level)
  862. return false;
  863. if (a->line_size != b->line_size)
  864. return false;
  865. if (a->sets != b->sets)
  866. return false;
  867. if (a->ways != b->ways)
  868. return false;
  869. if (strcmp(a->type, b->type))
  870. return false;
  871. if (strcmp(a->size, b->size))
  872. return false;
  873. if (strcmp(a->map, b->map))
  874. return false;
  875. return true;
  876. }
  877. static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
  878. {
  879. char path[PATH_MAX], file[PATH_MAX];
  880. struct stat st;
  881. size_t len;
  882. scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
  883. scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
  884. if (stat(file, &st))
  885. return 1;
  886. scnprintf(file, PATH_MAX, "%s/level", path);
  887. if (sysfs__read_int(file, (int *) &cache->level))
  888. return -1;
  889. scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
  890. if (sysfs__read_int(file, (int *) &cache->line_size))
  891. return -1;
  892. scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
  893. if (sysfs__read_int(file, (int *) &cache->sets))
  894. return -1;
  895. scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
  896. if (sysfs__read_int(file, (int *) &cache->ways))
  897. return -1;
  898. scnprintf(file, PATH_MAX, "%s/type", path);
  899. if (sysfs__read_str(file, &cache->type, &len))
  900. return -1;
  901. cache->type[len] = 0;
  902. cache->type = rtrim(cache->type);
  903. scnprintf(file, PATH_MAX, "%s/size", path);
  904. if (sysfs__read_str(file, &cache->size, &len)) {
  905. free(cache->type);
  906. return -1;
  907. }
  908. cache->size[len] = 0;
  909. cache->size = rtrim(cache->size);
  910. scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
  911. if (sysfs__read_str(file, &cache->map, &len)) {
  912. free(cache->size);
  913. free(cache->type);
  914. return -1;
  915. }
  916. cache->map[len] = 0;
  917. cache->map = rtrim(cache->map);
  918. return 0;
  919. }
  920. static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
  921. {
  922. fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
  923. }
  924. static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
  925. {
  926. u32 i, cnt = 0;
  927. long ncpus;
  928. u32 nr, cpu;
  929. u16 level;
  930. ncpus = sysconf(_SC_NPROCESSORS_CONF);
  931. if (ncpus < 0)
  932. return -1;
  933. nr = (u32)(ncpus & UINT_MAX);
  934. for (cpu = 0; cpu < nr; cpu++) {
  935. for (level = 0; level < 10; level++) {
  936. struct cpu_cache_level c;
  937. int err;
  938. err = cpu_cache_level__read(&c, cpu, level);
  939. if (err < 0)
  940. return err;
  941. if (err == 1)
  942. break;
  943. for (i = 0; i < cnt; i++) {
  944. if (cpu_cache_level__cmp(&c, &caches[i]))
  945. break;
  946. }
  947. if (i == cnt)
  948. caches[cnt++] = c;
  949. else
  950. cpu_cache_level__free(&c);
  951. if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
  952. goto out;
  953. }
  954. }
  955. out:
  956. *cntp = cnt;
  957. return 0;
  958. }
  959. #define MAX_CACHES (MAX_NR_CPUS * 4)
  960. static int write_cache(struct feat_fd *ff,
  961. struct perf_evlist *evlist __maybe_unused)
  962. {
  963. struct cpu_cache_level caches[MAX_CACHES];
  964. u32 cnt = 0, i, version = 1;
  965. int ret;
  966. ret = build_caches(caches, MAX_CACHES, &cnt);
  967. if (ret)
  968. goto out;
  969. qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
  970. ret = do_write(ff, &version, sizeof(u32));
  971. if (ret < 0)
  972. goto out;
  973. ret = do_write(ff, &cnt, sizeof(u32));
  974. if (ret < 0)
  975. goto out;
  976. for (i = 0; i < cnt; i++) {
  977. struct cpu_cache_level *c = &caches[i];
  978. #define _W(v) \
  979. ret = do_write(ff, &c->v, sizeof(u32)); \
  980. if (ret < 0) \
  981. goto out;
  982. _W(level)
  983. _W(line_size)
  984. _W(sets)
  985. _W(ways)
  986. #undef _W
  987. #define _W(v) \
  988. ret = do_write_string(ff, (const char *) c->v); \
  989. if (ret < 0) \
  990. goto out;
  991. _W(type)
  992. _W(size)
  993. _W(map)
  994. #undef _W
  995. }
  996. out:
  997. for (i = 0; i < cnt; i++)
  998. cpu_cache_level__free(&caches[i]);
  999. return ret;
  1000. }
  1001. static int write_stat(struct feat_fd *ff __maybe_unused,
  1002. struct perf_evlist *evlist __maybe_unused)
  1003. {
  1004. return 0;
  1005. }
  1006. static int write_sample_time(struct feat_fd *ff,
  1007. struct perf_evlist *evlist)
  1008. {
  1009. int ret;
  1010. ret = do_write(ff, &evlist->first_sample_time,
  1011. sizeof(evlist->first_sample_time));
  1012. if (ret < 0)
  1013. return ret;
  1014. return do_write(ff, &evlist->last_sample_time,
  1015. sizeof(evlist->last_sample_time));
  1016. }
  1017. static int memory_node__read(struct memory_node *n, unsigned long idx)
  1018. {
  1019. unsigned int phys, size = 0;
  1020. char path[PATH_MAX];
  1021. struct dirent *ent;
  1022. DIR *dir;
  1023. #define for_each_memory(mem, dir) \
  1024. while ((ent = readdir(dir))) \
  1025. if (strcmp(ent->d_name, ".") && \
  1026. strcmp(ent->d_name, "..") && \
  1027. sscanf(ent->d_name, "memory%u", &mem) == 1)
  1028. scnprintf(path, PATH_MAX,
  1029. "%s/devices/system/node/node%lu",
  1030. sysfs__mountpoint(), idx);
  1031. dir = opendir(path);
  1032. if (!dir) {
  1033. pr_warning("failed: cant' open memory sysfs data\n");
  1034. return -1;
  1035. }
  1036. for_each_memory(phys, dir) {
  1037. size = max(phys, size);
  1038. }
  1039. size++;
  1040. n->set = bitmap_alloc(size);
  1041. if (!n->set) {
  1042. closedir(dir);
  1043. return -ENOMEM;
  1044. }
  1045. n->node = idx;
  1046. n->size = size;
  1047. rewinddir(dir);
  1048. for_each_memory(phys, dir) {
  1049. set_bit(phys, n->set);
  1050. }
  1051. closedir(dir);
  1052. return 0;
  1053. }
  1054. static int memory_node__sort(const void *a, const void *b)
  1055. {
  1056. const struct memory_node *na = a;
  1057. const struct memory_node *nb = b;
  1058. return na->node - nb->node;
  1059. }
  1060. static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
  1061. {
  1062. char path[PATH_MAX];
  1063. struct dirent *ent;
  1064. DIR *dir;
  1065. u64 cnt = 0;
  1066. int ret = 0;
  1067. scnprintf(path, PATH_MAX, "%s/devices/system/node/",
  1068. sysfs__mountpoint());
  1069. dir = opendir(path);
  1070. if (!dir) {
  1071. pr_debug2("%s: could't read %s, does this arch have topology information?\n",
  1072. __func__, path);
  1073. return -1;
  1074. }
  1075. while (!ret && (ent = readdir(dir))) {
  1076. unsigned int idx;
  1077. int r;
  1078. if (!strcmp(ent->d_name, ".") ||
  1079. !strcmp(ent->d_name, ".."))
  1080. continue;
  1081. r = sscanf(ent->d_name, "node%u", &idx);
  1082. if (r != 1)
  1083. continue;
  1084. if (WARN_ONCE(cnt >= size,
  1085. "failed to write MEM_TOPOLOGY, way too many nodes\n"))
  1086. return -1;
  1087. ret = memory_node__read(&nodes[cnt++], idx);
  1088. }
  1089. *cntp = cnt;
  1090. closedir(dir);
  1091. if (!ret)
  1092. qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
  1093. return ret;
  1094. }
  1095. #define MAX_MEMORY_NODES 2000
  1096. /*
  1097. * The MEM_TOPOLOGY holds physical memory map for every
  1098. * node in system. The format of data is as follows:
  1099. *
  1100. * 0 - version | for future changes
  1101. * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
  1102. * 16 - count | number of nodes
  1103. *
  1104. * For each node we store map of physical indexes for
  1105. * each node:
  1106. *
  1107. * 32 - node id | node index
  1108. * 40 - size | size of bitmap
  1109. * 48 - bitmap | bitmap of memory indexes that belongs to node
  1110. */
  1111. static int write_mem_topology(struct feat_fd *ff __maybe_unused,
  1112. struct perf_evlist *evlist __maybe_unused)
  1113. {
  1114. static struct memory_node nodes[MAX_MEMORY_NODES];
  1115. u64 bsize, version = 1, i, nr;
  1116. int ret;
  1117. ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
  1118. (unsigned long long *) &bsize);
  1119. if (ret)
  1120. return ret;
  1121. ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
  1122. if (ret)
  1123. return ret;
  1124. ret = do_write(ff, &version, sizeof(version));
  1125. if (ret < 0)
  1126. goto out;
  1127. ret = do_write(ff, &bsize, sizeof(bsize));
  1128. if (ret < 0)
  1129. goto out;
  1130. ret = do_write(ff, &nr, sizeof(nr));
  1131. if (ret < 0)
  1132. goto out;
  1133. for (i = 0; i < nr; i++) {
  1134. struct memory_node *n = &nodes[i];
  1135. #define _W(v) \
  1136. ret = do_write(ff, &n->v, sizeof(n->v)); \
  1137. if (ret < 0) \
  1138. goto out;
  1139. _W(node)
  1140. _W(size)
  1141. #undef _W
  1142. ret = do_write_bitmap(ff, n->set, n->size);
  1143. if (ret < 0)
  1144. goto out;
  1145. }
  1146. out:
  1147. return ret;
  1148. }
  1149. static void print_hostname(struct feat_fd *ff, FILE *fp)
  1150. {
  1151. fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
  1152. }
  1153. static void print_osrelease(struct feat_fd *ff, FILE *fp)
  1154. {
  1155. fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
  1156. }
  1157. static void print_arch(struct feat_fd *ff, FILE *fp)
  1158. {
  1159. fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
  1160. }
  1161. static void print_cpudesc(struct feat_fd *ff, FILE *fp)
  1162. {
  1163. fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
  1164. }
  1165. static void print_nrcpus(struct feat_fd *ff, FILE *fp)
  1166. {
  1167. fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
  1168. fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
  1169. }
  1170. static void print_version(struct feat_fd *ff, FILE *fp)
  1171. {
  1172. fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
  1173. }
  1174. static void print_cmdline(struct feat_fd *ff, FILE *fp)
  1175. {
  1176. int nr, i;
  1177. nr = ff->ph->env.nr_cmdline;
  1178. fprintf(fp, "# cmdline : ");
  1179. for (i = 0; i < nr; i++) {
  1180. char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
  1181. if (!argv_i) {
  1182. fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
  1183. } else {
  1184. char *mem = argv_i;
  1185. do {
  1186. char *quote = strchr(argv_i, '\'');
  1187. if (!quote)
  1188. break;
  1189. *quote++ = '\0';
  1190. fprintf(fp, "%s\\\'", argv_i);
  1191. argv_i = quote;
  1192. } while (1);
  1193. fprintf(fp, "%s ", argv_i);
  1194. free(mem);
  1195. }
  1196. }
  1197. fputc('\n', fp);
  1198. }
  1199. static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
  1200. {
  1201. struct perf_header *ph = ff->ph;
  1202. int cpu_nr = ph->env.nr_cpus_avail;
  1203. int nr, i;
  1204. char *str;
  1205. nr = ph->env.nr_sibling_cores;
  1206. str = ph->env.sibling_cores;
  1207. for (i = 0; i < nr; i++) {
  1208. fprintf(fp, "# sibling cores : %s\n", str);
  1209. str += strlen(str) + 1;
  1210. }
  1211. nr = ph->env.nr_sibling_threads;
  1212. str = ph->env.sibling_threads;
  1213. for (i = 0; i < nr; i++) {
  1214. fprintf(fp, "# sibling threads : %s\n", str);
  1215. str += strlen(str) + 1;
  1216. }
  1217. if (ph->env.cpu != NULL) {
  1218. for (i = 0; i < cpu_nr; i++)
  1219. fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
  1220. ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
  1221. } else
  1222. fprintf(fp, "# Core ID and Socket ID information is not available\n");
  1223. }
  1224. static void free_event_desc(struct perf_evsel *events)
  1225. {
  1226. struct perf_evsel *evsel;
  1227. if (!events)
  1228. return;
  1229. for (evsel = events; evsel->attr.size; evsel++) {
  1230. zfree(&evsel->name);
  1231. zfree(&evsel->id);
  1232. }
  1233. free(events);
  1234. }
  1235. static struct perf_evsel *read_event_desc(struct feat_fd *ff)
  1236. {
  1237. struct perf_evsel *evsel, *events = NULL;
  1238. u64 *id;
  1239. void *buf = NULL;
  1240. u32 nre, sz, nr, i, j;
  1241. size_t msz;
  1242. /* number of events */
  1243. if (do_read_u32(ff, &nre))
  1244. goto error;
  1245. if (do_read_u32(ff, &sz))
  1246. goto error;
  1247. /* buffer to hold on file attr struct */
  1248. buf = malloc(sz);
  1249. if (!buf)
  1250. goto error;
  1251. /* the last event terminates with evsel->attr.size == 0: */
  1252. events = calloc(nre + 1, sizeof(*events));
  1253. if (!events)
  1254. goto error;
  1255. msz = sizeof(evsel->attr);
  1256. if (sz < msz)
  1257. msz = sz;
  1258. for (i = 0, evsel = events; i < nre; evsel++, i++) {
  1259. evsel->idx = i;
  1260. /*
  1261. * must read entire on-file attr struct to
  1262. * sync up with layout.
  1263. */
  1264. if (__do_read(ff, buf, sz))
  1265. goto error;
  1266. if (ff->ph->needs_swap)
  1267. perf_event__attr_swap(buf);
  1268. memcpy(&evsel->attr, buf, msz);
  1269. if (do_read_u32(ff, &nr))
  1270. goto error;
  1271. if (ff->ph->needs_swap)
  1272. evsel->needs_swap = true;
  1273. evsel->name = do_read_string(ff);
  1274. if (!evsel->name)
  1275. goto error;
  1276. if (!nr)
  1277. continue;
  1278. id = calloc(nr, sizeof(*id));
  1279. if (!id)
  1280. goto error;
  1281. evsel->ids = nr;
  1282. evsel->id = id;
  1283. for (j = 0 ; j < nr; j++) {
  1284. if (do_read_u64(ff, id))
  1285. goto error;
  1286. id++;
  1287. }
  1288. }
  1289. out:
  1290. free(buf);
  1291. return events;
  1292. error:
  1293. free_event_desc(events);
  1294. events = NULL;
  1295. goto out;
  1296. }
  1297. static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
  1298. void *priv __maybe_unused)
  1299. {
  1300. return fprintf(fp, ", %s = %s", name, val);
  1301. }
  1302. static void print_event_desc(struct feat_fd *ff, FILE *fp)
  1303. {
  1304. struct perf_evsel *evsel, *events;
  1305. u32 j;
  1306. u64 *id;
  1307. if (ff->events)
  1308. events = ff->events;
  1309. else
  1310. events = read_event_desc(ff);
  1311. if (!events) {
  1312. fprintf(fp, "# event desc: not available or unable to read\n");
  1313. return;
  1314. }
  1315. for (evsel = events; evsel->attr.size; evsel++) {
  1316. fprintf(fp, "# event : name = %s, ", evsel->name);
  1317. if (evsel->ids) {
  1318. fprintf(fp, ", id = {");
  1319. for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
  1320. if (j)
  1321. fputc(',', fp);
  1322. fprintf(fp, " %"PRIu64, *id);
  1323. }
  1324. fprintf(fp, " }");
  1325. }
  1326. perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
  1327. fputc('\n', fp);
  1328. }
  1329. free_event_desc(events);
  1330. ff->events = NULL;
  1331. }
  1332. static void print_total_mem(struct feat_fd *ff, FILE *fp)
  1333. {
  1334. fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
  1335. }
  1336. static void print_numa_topology(struct feat_fd *ff, FILE *fp)
  1337. {
  1338. int i;
  1339. struct numa_node *n;
  1340. for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
  1341. n = &ff->ph->env.numa_nodes[i];
  1342. fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
  1343. " free = %"PRIu64" kB\n",
  1344. n->node, n->mem_total, n->mem_free);
  1345. fprintf(fp, "# node%u cpu list : ", n->node);
  1346. cpu_map__fprintf(n->map, fp);
  1347. }
  1348. }
  1349. static void print_cpuid(struct feat_fd *ff, FILE *fp)
  1350. {
  1351. fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
  1352. }
  1353. static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
  1354. {
  1355. fprintf(fp, "# contains samples with branch stack\n");
  1356. }
  1357. static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
  1358. {
  1359. fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
  1360. }
  1361. static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
  1362. {
  1363. fprintf(fp, "# contains stat data\n");
  1364. }
  1365. static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
  1366. {
  1367. int i;
  1368. fprintf(fp, "# CPU cache info:\n");
  1369. for (i = 0; i < ff->ph->env.caches_cnt; i++) {
  1370. fprintf(fp, "# ");
  1371. cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
  1372. }
  1373. }
  1374. static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
  1375. {
  1376. const char *delimiter = "# pmu mappings: ";
  1377. char *str, *tmp;
  1378. u32 pmu_num;
  1379. u32 type;
  1380. pmu_num = ff->ph->env.nr_pmu_mappings;
  1381. if (!pmu_num) {
  1382. fprintf(fp, "# pmu mappings: not available\n");
  1383. return;
  1384. }
  1385. str = ff->ph->env.pmu_mappings;
  1386. while (pmu_num) {
  1387. type = strtoul(str, &tmp, 0);
  1388. if (*tmp != ':')
  1389. goto error;
  1390. str = tmp + 1;
  1391. fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
  1392. delimiter = ", ";
  1393. str += strlen(str) + 1;
  1394. pmu_num--;
  1395. }
  1396. fprintf(fp, "\n");
  1397. if (!pmu_num)
  1398. return;
  1399. error:
  1400. fprintf(fp, "# pmu mappings: unable to read\n");
  1401. }
  1402. static void print_group_desc(struct feat_fd *ff, FILE *fp)
  1403. {
  1404. struct perf_session *session;
  1405. struct perf_evsel *evsel;
  1406. u32 nr = 0;
  1407. session = container_of(ff->ph, struct perf_session, header);
  1408. evlist__for_each_entry(session->evlist, evsel) {
  1409. if (perf_evsel__is_group_leader(evsel) &&
  1410. evsel->nr_members > 1) {
  1411. fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
  1412. perf_evsel__name(evsel));
  1413. nr = evsel->nr_members - 1;
  1414. } else if (nr) {
  1415. fprintf(fp, ",%s", perf_evsel__name(evsel));
  1416. if (--nr == 0)
  1417. fprintf(fp, "}\n");
  1418. }
  1419. }
  1420. }
  1421. static void print_sample_time(struct feat_fd *ff, FILE *fp)
  1422. {
  1423. struct perf_session *session;
  1424. char time_buf[32];
  1425. double d;
  1426. session = container_of(ff->ph, struct perf_session, header);
  1427. timestamp__scnprintf_usec(session->evlist->first_sample_time,
  1428. time_buf, sizeof(time_buf));
  1429. fprintf(fp, "# time of first sample : %s\n", time_buf);
  1430. timestamp__scnprintf_usec(session->evlist->last_sample_time,
  1431. time_buf, sizeof(time_buf));
  1432. fprintf(fp, "# time of last sample : %s\n", time_buf);
  1433. d = (double)(session->evlist->last_sample_time -
  1434. session->evlist->first_sample_time) / NSEC_PER_MSEC;
  1435. fprintf(fp, "# sample duration : %10.3f ms\n", d);
  1436. }
  1437. static void memory_node__fprintf(struct memory_node *n,
  1438. unsigned long long bsize, FILE *fp)
  1439. {
  1440. char buf_map[100], buf_size[50];
  1441. unsigned long long size;
  1442. size = bsize * bitmap_weight(n->set, n->size);
  1443. unit_number__scnprintf(buf_size, 50, size);
  1444. bitmap_scnprintf(n->set, n->size, buf_map, 100);
  1445. fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
  1446. }
  1447. static void print_mem_topology(struct feat_fd *ff, FILE *fp)
  1448. {
  1449. struct memory_node *nodes;
  1450. int i, nr;
  1451. nodes = ff->ph->env.memory_nodes;
  1452. nr = ff->ph->env.nr_memory_nodes;
  1453. fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
  1454. nr, ff->ph->env.memory_bsize);
  1455. for (i = 0; i < nr; i++) {
  1456. memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
  1457. }
  1458. }
  1459. static int __event_process_build_id(struct build_id_event *bev,
  1460. char *filename,
  1461. struct perf_session *session)
  1462. {
  1463. int err = -1;
  1464. struct machine *machine;
  1465. u16 cpumode;
  1466. struct dso *dso;
  1467. enum dso_kernel_type dso_type;
  1468. machine = perf_session__findnew_machine(session, bev->pid);
  1469. if (!machine)
  1470. goto out;
  1471. cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1472. switch (cpumode) {
  1473. case PERF_RECORD_MISC_KERNEL:
  1474. dso_type = DSO_TYPE_KERNEL;
  1475. break;
  1476. case PERF_RECORD_MISC_GUEST_KERNEL:
  1477. dso_type = DSO_TYPE_GUEST_KERNEL;
  1478. break;
  1479. case PERF_RECORD_MISC_USER:
  1480. case PERF_RECORD_MISC_GUEST_USER:
  1481. dso_type = DSO_TYPE_USER;
  1482. break;
  1483. default:
  1484. goto out;
  1485. }
  1486. dso = machine__findnew_dso(machine, filename);
  1487. if (dso != NULL) {
  1488. char sbuild_id[SBUILD_ID_SIZE];
  1489. dso__set_build_id(dso, &bev->build_id);
  1490. if (dso_type != DSO_TYPE_USER) {
  1491. struct kmod_path m = { .name = NULL, };
  1492. if (!kmod_path__parse_name(&m, filename) && m.kmod)
  1493. dso__set_module_info(dso, &m, machine);
  1494. else
  1495. dso->kernel = dso_type;
  1496. free(m.name);
  1497. }
  1498. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1499. sbuild_id);
  1500. pr_debug("build id event received for %s: %s\n",
  1501. dso->long_name, sbuild_id);
  1502. dso__put(dso);
  1503. }
  1504. err = 0;
  1505. out:
  1506. return err;
  1507. }
  1508. static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
  1509. int input, u64 offset, u64 size)
  1510. {
  1511. struct perf_session *session = container_of(header, struct perf_session, header);
  1512. struct {
  1513. struct perf_event_header header;
  1514. u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
  1515. char filename[0];
  1516. } old_bev;
  1517. struct build_id_event bev;
  1518. char filename[PATH_MAX];
  1519. u64 limit = offset + size;
  1520. while (offset < limit) {
  1521. ssize_t len;
  1522. if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
  1523. return -1;
  1524. if (header->needs_swap)
  1525. perf_event_header__bswap(&old_bev.header);
  1526. len = old_bev.header.size - sizeof(old_bev);
  1527. if (readn(input, filename, len) != len)
  1528. return -1;
  1529. bev.header = old_bev.header;
  1530. /*
  1531. * As the pid is the missing value, we need to fill
  1532. * it properly. The header.misc value give us nice hint.
  1533. */
  1534. bev.pid = HOST_KERNEL_ID;
  1535. if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
  1536. bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
  1537. bev.pid = DEFAULT_GUEST_KERNEL_ID;
  1538. memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
  1539. __event_process_build_id(&bev, filename, session);
  1540. offset += bev.header.size;
  1541. }
  1542. return 0;
  1543. }
  1544. static int perf_header__read_build_ids(struct perf_header *header,
  1545. int input, u64 offset, u64 size)
  1546. {
  1547. struct perf_session *session = container_of(header, struct perf_session, header);
  1548. struct build_id_event bev;
  1549. char filename[PATH_MAX];
  1550. u64 limit = offset + size, orig_offset = offset;
  1551. int err = -1;
  1552. while (offset < limit) {
  1553. ssize_t len;
  1554. if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
  1555. goto out;
  1556. if (header->needs_swap)
  1557. perf_event_header__bswap(&bev.header);
  1558. len = bev.header.size - sizeof(bev);
  1559. if (readn(input, filename, len) != len)
  1560. goto out;
  1561. /*
  1562. * The a1645ce1 changeset:
  1563. *
  1564. * "perf: 'perf kvm' tool for monitoring guest performance from host"
  1565. *
  1566. * Added a field to struct build_id_event that broke the file
  1567. * format.
  1568. *
  1569. * Since the kernel build-id is the first entry, process the
  1570. * table using the old format if the well known
  1571. * '[kernel.kallsyms]' string for the kernel build-id has the
  1572. * first 4 characters chopped off (where the pid_t sits).
  1573. */
  1574. if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
  1575. if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
  1576. return -1;
  1577. return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
  1578. }
  1579. __event_process_build_id(&bev, filename, session);
  1580. offset += bev.header.size;
  1581. }
  1582. err = 0;
  1583. out:
  1584. return err;
  1585. }
  1586. /* Macro for features that simply need to read and store a string. */
  1587. #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
  1588. static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
  1589. {\
  1590. ff->ph->env.__feat_env = do_read_string(ff); \
  1591. return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
  1592. }
  1593. FEAT_PROCESS_STR_FUN(hostname, hostname);
  1594. FEAT_PROCESS_STR_FUN(osrelease, os_release);
  1595. FEAT_PROCESS_STR_FUN(version, version);
  1596. FEAT_PROCESS_STR_FUN(arch, arch);
  1597. FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
  1598. FEAT_PROCESS_STR_FUN(cpuid, cpuid);
  1599. static int process_tracing_data(struct feat_fd *ff, void *data)
  1600. {
  1601. ssize_t ret = trace_report(ff->fd, data, false);
  1602. return ret < 0 ? -1 : 0;
  1603. }
  1604. static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
  1605. {
  1606. if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
  1607. pr_debug("Failed to read buildids, continuing...\n");
  1608. return 0;
  1609. }
  1610. static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
  1611. {
  1612. int ret;
  1613. u32 nr_cpus_avail, nr_cpus_online;
  1614. ret = do_read_u32(ff, &nr_cpus_avail);
  1615. if (ret)
  1616. return ret;
  1617. ret = do_read_u32(ff, &nr_cpus_online);
  1618. if (ret)
  1619. return ret;
  1620. ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
  1621. ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
  1622. return 0;
  1623. }
  1624. static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
  1625. {
  1626. u64 total_mem;
  1627. int ret;
  1628. ret = do_read_u64(ff, &total_mem);
  1629. if (ret)
  1630. return -1;
  1631. ff->ph->env.total_mem = (unsigned long long)total_mem;
  1632. return 0;
  1633. }
  1634. static struct perf_evsel *
  1635. perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
  1636. {
  1637. struct perf_evsel *evsel;
  1638. evlist__for_each_entry(evlist, evsel) {
  1639. if (evsel->idx == idx)
  1640. return evsel;
  1641. }
  1642. return NULL;
  1643. }
  1644. static void
  1645. perf_evlist__set_event_name(struct perf_evlist *evlist,
  1646. struct perf_evsel *event)
  1647. {
  1648. struct perf_evsel *evsel;
  1649. if (!event->name)
  1650. return;
  1651. evsel = perf_evlist__find_by_index(evlist, event->idx);
  1652. if (!evsel)
  1653. return;
  1654. if (evsel->name)
  1655. return;
  1656. evsel->name = strdup(event->name);
  1657. }
  1658. static int
  1659. process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
  1660. {
  1661. struct perf_session *session;
  1662. struct perf_evsel *evsel, *events = read_event_desc(ff);
  1663. if (!events)
  1664. return 0;
  1665. session = container_of(ff->ph, struct perf_session, header);
  1666. if (session->data->is_pipe) {
  1667. /* Save events for reading later by print_event_desc,
  1668. * since they can't be read again in pipe mode. */
  1669. ff->events = events;
  1670. }
  1671. for (evsel = events; evsel->attr.size; evsel++)
  1672. perf_evlist__set_event_name(session->evlist, evsel);
  1673. if (!session->data->is_pipe)
  1674. free_event_desc(events);
  1675. return 0;
  1676. }
  1677. static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
  1678. {
  1679. char *str, *cmdline = NULL, **argv = NULL;
  1680. u32 nr, i, len = 0;
  1681. if (do_read_u32(ff, &nr))
  1682. return -1;
  1683. ff->ph->env.nr_cmdline = nr;
  1684. cmdline = zalloc(ff->size + nr + 1);
  1685. if (!cmdline)
  1686. return -1;
  1687. argv = zalloc(sizeof(char *) * (nr + 1));
  1688. if (!argv)
  1689. goto error;
  1690. for (i = 0; i < nr; i++) {
  1691. str = do_read_string(ff);
  1692. if (!str)
  1693. goto error;
  1694. argv[i] = cmdline + len;
  1695. memcpy(argv[i], str, strlen(str) + 1);
  1696. len += strlen(str) + 1;
  1697. free(str);
  1698. }
  1699. ff->ph->env.cmdline = cmdline;
  1700. ff->ph->env.cmdline_argv = (const char **) argv;
  1701. return 0;
  1702. error:
  1703. free(argv);
  1704. free(cmdline);
  1705. return -1;
  1706. }
  1707. static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
  1708. {
  1709. u32 nr, i;
  1710. char *str;
  1711. struct strbuf sb;
  1712. int cpu_nr = ff->ph->env.nr_cpus_avail;
  1713. u64 size = 0;
  1714. struct perf_header *ph = ff->ph;
  1715. bool do_core_id_test = true;
  1716. ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
  1717. if (!ph->env.cpu)
  1718. return -1;
  1719. if (do_read_u32(ff, &nr))
  1720. goto free_cpu;
  1721. ph->env.nr_sibling_cores = nr;
  1722. size += sizeof(u32);
  1723. if (strbuf_init(&sb, 128) < 0)
  1724. goto free_cpu;
  1725. for (i = 0; i < nr; i++) {
  1726. str = do_read_string(ff);
  1727. if (!str)
  1728. goto error;
  1729. /* include a NULL character at the end */
  1730. if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
  1731. goto error;
  1732. size += string_size(str);
  1733. free(str);
  1734. }
  1735. ph->env.sibling_cores = strbuf_detach(&sb, NULL);
  1736. if (do_read_u32(ff, &nr))
  1737. return -1;
  1738. ph->env.nr_sibling_threads = nr;
  1739. size += sizeof(u32);
  1740. for (i = 0; i < nr; i++) {
  1741. str = do_read_string(ff);
  1742. if (!str)
  1743. goto error;
  1744. /* include a NULL character at the end */
  1745. if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
  1746. goto error;
  1747. size += string_size(str);
  1748. free(str);
  1749. }
  1750. ph->env.sibling_threads = strbuf_detach(&sb, NULL);
  1751. /*
  1752. * The header may be from old perf,
  1753. * which doesn't include core id and socket id information.
  1754. */
  1755. if (ff->size <= size) {
  1756. zfree(&ph->env.cpu);
  1757. return 0;
  1758. }
  1759. /* On s390 the socket_id number is not related to the numbers of cpus.
  1760. * The socket_id number might be higher than the numbers of cpus.
  1761. * This depends on the configuration.
  1762. * AArch64 is the same.
  1763. */
  1764. if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
  1765. || !strncmp(ph->env.arch, "aarch64", 7)))
  1766. do_core_id_test = false;
  1767. for (i = 0; i < (u32)cpu_nr; i++) {
  1768. if (do_read_u32(ff, &nr))
  1769. goto free_cpu;
  1770. ph->env.cpu[i].core_id = nr;
  1771. if (do_read_u32(ff, &nr))
  1772. goto free_cpu;
  1773. if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
  1774. pr_debug("socket_id number is too big."
  1775. "You may need to upgrade the perf tool.\n");
  1776. goto free_cpu;
  1777. }
  1778. ph->env.cpu[i].socket_id = nr;
  1779. }
  1780. return 0;
  1781. error:
  1782. strbuf_release(&sb);
  1783. free_cpu:
  1784. zfree(&ph->env.cpu);
  1785. return -1;
  1786. }
  1787. static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
  1788. {
  1789. struct numa_node *nodes, *n;
  1790. u32 nr, i;
  1791. char *str;
  1792. /* nr nodes */
  1793. if (do_read_u32(ff, &nr))
  1794. return -1;
  1795. nodes = zalloc(sizeof(*nodes) * nr);
  1796. if (!nodes)
  1797. return -ENOMEM;
  1798. for (i = 0; i < nr; i++) {
  1799. n = &nodes[i];
  1800. /* node number */
  1801. if (do_read_u32(ff, &n->node))
  1802. goto error;
  1803. if (do_read_u64(ff, &n->mem_total))
  1804. goto error;
  1805. if (do_read_u64(ff, &n->mem_free))
  1806. goto error;
  1807. str = do_read_string(ff);
  1808. if (!str)
  1809. goto error;
  1810. n->map = cpu_map__new(str);
  1811. if (!n->map)
  1812. goto error;
  1813. free(str);
  1814. }
  1815. ff->ph->env.nr_numa_nodes = nr;
  1816. ff->ph->env.numa_nodes = nodes;
  1817. return 0;
  1818. error:
  1819. free(nodes);
  1820. return -1;
  1821. }
  1822. static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
  1823. {
  1824. char *name;
  1825. u32 pmu_num;
  1826. u32 type;
  1827. struct strbuf sb;
  1828. if (do_read_u32(ff, &pmu_num))
  1829. return -1;
  1830. if (!pmu_num) {
  1831. pr_debug("pmu mappings not available\n");
  1832. return 0;
  1833. }
  1834. ff->ph->env.nr_pmu_mappings = pmu_num;
  1835. if (strbuf_init(&sb, 128) < 0)
  1836. return -1;
  1837. while (pmu_num) {
  1838. if (do_read_u32(ff, &type))
  1839. goto error;
  1840. name = do_read_string(ff);
  1841. if (!name)
  1842. goto error;
  1843. if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
  1844. goto error;
  1845. /* include a NULL character at the end */
  1846. if (strbuf_add(&sb, "", 1) < 0)
  1847. goto error;
  1848. if (!strcmp(name, "msr"))
  1849. ff->ph->env.msr_pmu_type = type;
  1850. free(name);
  1851. pmu_num--;
  1852. }
  1853. ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
  1854. return 0;
  1855. error:
  1856. strbuf_release(&sb);
  1857. return -1;
  1858. }
  1859. static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
  1860. {
  1861. size_t ret = -1;
  1862. u32 i, nr, nr_groups;
  1863. struct perf_session *session;
  1864. struct perf_evsel *evsel, *leader = NULL;
  1865. struct group_desc {
  1866. char *name;
  1867. u32 leader_idx;
  1868. u32 nr_members;
  1869. } *desc;
  1870. if (do_read_u32(ff, &nr_groups))
  1871. return -1;
  1872. ff->ph->env.nr_groups = nr_groups;
  1873. if (!nr_groups) {
  1874. pr_debug("group desc not available\n");
  1875. return 0;
  1876. }
  1877. desc = calloc(nr_groups, sizeof(*desc));
  1878. if (!desc)
  1879. return -1;
  1880. for (i = 0; i < nr_groups; i++) {
  1881. desc[i].name = do_read_string(ff);
  1882. if (!desc[i].name)
  1883. goto out_free;
  1884. if (do_read_u32(ff, &desc[i].leader_idx))
  1885. goto out_free;
  1886. if (do_read_u32(ff, &desc[i].nr_members))
  1887. goto out_free;
  1888. }
  1889. /*
  1890. * Rebuild group relationship based on the group_desc
  1891. */
  1892. session = container_of(ff->ph, struct perf_session, header);
  1893. session->evlist->nr_groups = nr_groups;
  1894. i = nr = 0;
  1895. evlist__for_each_entry(session->evlist, evsel) {
  1896. if (evsel->idx == (int) desc[i].leader_idx) {
  1897. evsel->leader = evsel;
  1898. /* {anon_group} is a dummy name */
  1899. if (strcmp(desc[i].name, "{anon_group}")) {
  1900. evsel->group_name = desc[i].name;
  1901. desc[i].name = NULL;
  1902. }
  1903. evsel->nr_members = desc[i].nr_members;
  1904. if (i >= nr_groups || nr > 0) {
  1905. pr_debug("invalid group desc\n");
  1906. goto out_free;
  1907. }
  1908. leader = evsel;
  1909. nr = evsel->nr_members - 1;
  1910. i++;
  1911. } else if (nr) {
  1912. /* This is a group member */
  1913. evsel->leader = leader;
  1914. nr--;
  1915. }
  1916. }
  1917. if (i != nr_groups || nr != 0) {
  1918. pr_debug("invalid group desc\n");
  1919. goto out_free;
  1920. }
  1921. ret = 0;
  1922. out_free:
  1923. for (i = 0; i < nr_groups; i++)
  1924. zfree(&desc[i].name);
  1925. free(desc);
  1926. return ret;
  1927. }
  1928. static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
  1929. {
  1930. struct perf_session *session;
  1931. int err;
  1932. session = container_of(ff->ph, struct perf_session, header);
  1933. err = auxtrace_index__process(ff->fd, ff->size, session,
  1934. ff->ph->needs_swap);
  1935. if (err < 0)
  1936. pr_err("Failed to process auxtrace index\n");
  1937. return err;
  1938. }
  1939. static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
  1940. {
  1941. struct cpu_cache_level *caches;
  1942. u32 cnt, i, version;
  1943. if (do_read_u32(ff, &version))
  1944. return -1;
  1945. if (version != 1)
  1946. return -1;
  1947. if (do_read_u32(ff, &cnt))
  1948. return -1;
  1949. caches = zalloc(sizeof(*caches) * cnt);
  1950. if (!caches)
  1951. return -1;
  1952. for (i = 0; i < cnt; i++) {
  1953. struct cpu_cache_level c;
  1954. #define _R(v) \
  1955. if (do_read_u32(ff, &c.v))\
  1956. goto out_free_caches; \
  1957. _R(level)
  1958. _R(line_size)
  1959. _R(sets)
  1960. _R(ways)
  1961. #undef _R
  1962. #define _R(v) \
  1963. c.v = do_read_string(ff); \
  1964. if (!c.v) \
  1965. goto out_free_caches;
  1966. _R(type)
  1967. _R(size)
  1968. _R(map)
  1969. #undef _R
  1970. caches[i] = c;
  1971. }
  1972. ff->ph->env.caches = caches;
  1973. ff->ph->env.caches_cnt = cnt;
  1974. return 0;
  1975. out_free_caches:
  1976. free(caches);
  1977. return -1;
  1978. }
  1979. static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
  1980. {
  1981. struct perf_session *session;
  1982. u64 first_sample_time, last_sample_time;
  1983. int ret;
  1984. session = container_of(ff->ph, struct perf_session, header);
  1985. ret = do_read_u64(ff, &first_sample_time);
  1986. if (ret)
  1987. return -1;
  1988. ret = do_read_u64(ff, &last_sample_time);
  1989. if (ret)
  1990. return -1;
  1991. session->evlist->first_sample_time = first_sample_time;
  1992. session->evlist->last_sample_time = last_sample_time;
  1993. return 0;
  1994. }
  1995. static int process_mem_topology(struct feat_fd *ff,
  1996. void *data __maybe_unused)
  1997. {
  1998. struct memory_node *nodes;
  1999. u64 version, i, nr, bsize;
  2000. int ret = -1;
  2001. if (do_read_u64(ff, &version))
  2002. return -1;
  2003. if (version != 1)
  2004. return -1;
  2005. if (do_read_u64(ff, &bsize))
  2006. return -1;
  2007. if (do_read_u64(ff, &nr))
  2008. return -1;
  2009. nodes = zalloc(sizeof(*nodes) * nr);
  2010. if (!nodes)
  2011. return -1;
  2012. for (i = 0; i < nr; i++) {
  2013. struct memory_node n;
  2014. #define _R(v) \
  2015. if (do_read_u64(ff, &n.v)) \
  2016. goto out; \
  2017. _R(node)
  2018. _R(size)
  2019. #undef _R
  2020. if (do_read_bitmap(ff, &n.set, &n.size))
  2021. goto out;
  2022. nodes[i] = n;
  2023. }
  2024. ff->ph->env.memory_bsize = bsize;
  2025. ff->ph->env.memory_nodes = nodes;
  2026. ff->ph->env.nr_memory_nodes = nr;
  2027. ret = 0;
  2028. out:
  2029. if (ret)
  2030. free(nodes);
  2031. return ret;
  2032. }
  2033. struct feature_ops {
  2034. int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
  2035. void (*print)(struct feat_fd *ff, FILE *fp);
  2036. int (*process)(struct feat_fd *ff, void *data);
  2037. const char *name;
  2038. bool full_only;
  2039. bool synthesize;
  2040. };
  2041. #define FEAT_OPR(n, func, __full_only) \
  2042. [HEADER_##n] = { \
  2043. .name = __stringify(n), \
  2044. .write = write_##func, \
  2045. .print = print_##func, \
  2046. .full_only = __full_only, \
  2047. .process = process_##func, \
  2048. .synthesize = true \
  2049. }
  2050. #define FEAT_OPN(n, func, __full_only) \
  2051. [HEADER_##n] = { \
  2052. .name = __stringify(n), \
  2053. .write = write_##func, \
  2054. .print = print_##func, \
  2055. .full_only = __full_only, \
  2056. .process = process_##func \
  2057. }
  2058. /* feature_ops not implemented: */
  2059. #define print_tracing_data NULL
  2060. #define print_build_id NULL
  2061. #define process_branch_stack NULL
  2062. #define process_stat NULL
  2063. static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
  2064. FEAT_OPN(TRACING_DATA, tracing_data, false),
  2065. FEAT_OPN(BUILD_ID, build_id, false),
  2066. FEAT_OPR(HOSTNAME, hostname, false),
  2067. FEAT_OPR(OSRELEASE, osrelease, false),
  2068. FEAT_OPR(VERSION, version, false),
  2069. FEAT_OPR(ARCH, arch, false),
  2070. FEAT_OPR(NRCPUS, nrcpus, false),
  2071. FEAT_OPR(CPUDESC, cpudesc, false),
  2072. FEAT_OPR(CPUID, cpuid, false),
  2073. FEAT_OPR(TOTAL_MEM, total_mem, false),
  2074. FEAT_OPR(EVENT_DESC, event_desc, false),
  2075. FEAT_OPR(CMDLINE, cmdline, false),
  2076. FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
  2077. FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
  2078. FEAT_OPN(BRANCH_STACK, branch_stack, false),
  2079. FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
  2080. FEAT_OPR(GROUP_DESC, group_desc, false),
  2081. FEAT_OPN(AUXTRACE, auxtrace, false),
  2082. FEAT_OPN(STAT, stat, false),
  2083. FEAT_OPN(CACHE, cache, true),
  2084. FEAT_OPR(SAMPLE_TIME, sample_time, false),
  2085. FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
  2086. };
  2087. struct header_print_data {
  2088. FILE *fp;
  2089. bool full; /* extended list of headers */
  2090. };
  2091. static int perf_file_section__fprintf_info(struct perf_file_section *section,
  2092. struct perf_header *ph,
  2093. int feat, int fd, void *data)
  2094. {
  2095. struct header_print_data *hd = data;
  2096. struct feat_fd ff;
  2097. if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
  2098. pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
  2099. "%d, continuing...\n", section->offset, feat);
  2100. return 0;
  2101. }
  2102. if (feat >= HEADER_LAST_FEATURE) {
  2103. pr_warning("unknown feature %d\n", feat);
  2104. return 0;
  2105. }
  2106. if (!feat_ops[feat].print)
  2107. return 0;
  2108. ff = (struct feat_fd) {
  2109. .fd = fd,
  2110. .ph = ph,
  2111. };
  2112. if (!feat_ops[feat].full_only || hd->full)
  2113. feat_ops[feat].print(&ff, hd->fp);
  2114. else
  2115. fprintf(hd->fp, "# %s info available, use -I to display\n",
  2116. feat_ops[feat].name);
  2117. return 0;
  2118. }
  2119. int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
  2120. {
  2121. struct header_print_data hd;
  2122. struct perf_header *header = &session->header;
  2123. int fd = perf_data__fd(session->data);
  2124. struct stat st;
  2125. time_t stctime;
  2126. int ret, bit;
  2127. hd.fp = fp;
  2128. hd.full = full;
  2129. ret = fstat(fd, &st);
  2130. if (ret == -1)
  2131. return -1;
  2132. stctime = st.st_ctime;
  2133. fprintf(fp, "# captured on : %s", ctime(&stctime));
  2134. fprintf(fp, "# header version : %u\n", header->version);
  2135. fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
  2136. fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
  2137. fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
  2138. perf_header__process_sections(header, fd, &hd,
  2139. perf_file_section__fprintf_info);
  2140. if (session->data->is_pipe)
  2141. return 0;
  2142. fprintf(fp, "# missing features: ");
  2143. for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
  2144. if (bit)
  2145. fprintf(fp, "%s ", feat_ops[bit].name);
  2146. }
  2147. fprintf(fp, "\n");
  2148. return 0;
  2149. }
  2150. static int do_write_feat(struct feat_fd *ff, int type,
  2151. struct perf_file_section **p,
  2152. struct perf_evlist *evlist)
  2153. {
  2154. int err;
  2155. int ret = 0;
  2156. if (perf_header__has_feat(ff->ph, type)) {
  2157. if (!feat_ops[type].write)
  2158. return -1;
  2159. if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
  2160. return -1;
  2161. (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
  2162. err = feat_ops[type].write(ff, evlist);
  2163. if (err < 0) {
  2164. pr_debug("failed to write feature %s\n", feat_ops[type].name);
  2165. /* undo anything written */
  2166. lseek(ff->fd, (*p)->offset, SEEK_SET);
  2167. return -1;
  2168. }
  2169. (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
  2170. (*p)++;
  2171. }
  2172. return ret;
  2173. }
  2174. static int perf_header__adds_write(struct perf_header *header,
  2175. struct perf_evlist *evlist, int fd)
  2176. {
  2177. int nr_sections;
  2178. struct feat_fd ff;
  2179. struct perf_file_section *feat_sec, *p;
  2180. int sec_size;
  2181. u64 sec_start;
  2182. int feat;
  2183. int err;
  2184. ff = (struct feat_fd){
  2185. .fd = fd,
  2186. .ph = header,
  2187. };
  2188. nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
  2189. if (!nr_sections)
  2190. return 0;
  2191. feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
  2192. if (feat_sec == NULL)
  2193. return -ENOMEM;
  2194. sec_size = sizeof(*feat_sec) * nr_sections;
  2195. sec_start = header->feat_offset;
  2196. lseek(fd, sec_start + sec_size, SEEK_SET);
  2197. for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
  2198. if (do_write_feat(&ff, feat, &p, evlist))
  2199. perf_header__clear_feat(header, feat);
  2200. }
  2201. lseek(fd, sec_start, SEEK_SET);
  2202. /*
  2203. * may write more than needed due to dropped feature, but
  2204. * this is okay, reader will skip the mising entries
  2205. */
  2206. err = do_write(&ff, feat_sec, sec_size);
  2207. if (err < 0)
  2208. pr_debug("failed to write feature section\n");
  2209. free(feat_sec);
  2210. return err;
  2211. }
  2212. int perf_header__write_pipe(int fd)
  2213. {
  2214. struct perf_pipe_file_header f_header;
  2215. struct feat_fd ff;
  2216. int err;
  2217. ff = (struct feat_fd){ .fd = fd };
  2218. f_header = (struct perf_pipe_file_header){
  2219. .magic = PERF_MAGIC,
  2220. .size = sizeof(f_header),
  2221. };
  2222. err = do_write(&ff, &f_header, sizeof(f_header));
  2223. if (err < 0) {
  2224. pr_debug("failed to write perf pipe header\n");
  2225. return err;
  2226. }
  2227. return 0;
  2228. }
  2229. int perf_session__write_header(struct perf_session *session,
  2230. struct perf_evlist *evlist,
  2231. int fd, bool at_exit)
  2232. {
  2233. struct perf_file_header f_header;
  2234. struct perf_file_attr f_attr;
  2235. struct perf_header *header = &session->header;
  2236. struct perf_evsel *evsel;
  2237. struct feat_fd ff;
  2238. u64 attr_offset;
  2239. int err;
  2240. ff = (struct feat_fd){ .fd = fd};
  2241. lseek(fd, sizeof(f_header), SEEK_SET);
  2242. evlist__for_each_entry(session->evlist, evsel) {
  2243. evsel->id_offset = lseek(fd, 0, SEEK_CUR);
  2244. err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
  2245. if (err < 0) {
  2246. pr_debug("failed to write perf header\n");
  2247. return err;
  2248. }
  2249. }
  2250. attr_offset = lseek(ff.fd, 0, SEEK_CUR);
  2251. evlist__for_each_entry(evlist, evsel) {
  2252. f_attr = (struct perf_file_attr){
  2253. .attr = evsel->attr,
  2254. .ids = {
  2255. .offset = evsel->id_offset,
  2256. .size = evsel->ids * sizeof(u64),
  2257. }
  2258. };
  2259. err = do_write(&ff, &f_attr, sizeof(f_attr));
  2260. if (err < 0) {
  2261. pr_debug("failed to write perf header attribute\n");
  2262. return err;
  2263. }
  2264. }
  2265. if (!header->data_offset)
  2266. header->data_offset = lseek(fd, 0, SEEK_CUR);
  2267. header->feat_offset = header->data_offset + header->data_size;
  2268. if (at_exit) {
  2269. err = perf_header__adds_write(header, evlist, fd);
  2270. if (err < 0)
  2271. return err;
  2272. }
  2273. f_header = (struct perf_file_header){
  2274. .magic = PERF_MAGIC,
  2275. .size = sizeof(f_header),
  2276. .attr_size = sizeof(f_attr),
  2277. .attrs = {
  2278. .offset = attr_offset,
  2279. .size = evlist->nr_entries * sizeof(f_attr),
  2280. },
  2281. .data = {
  2282. .offset = header->data_offset,
  2283. .size = header->data_size,
  2284. },
  2285. /* event_types is ignored, store zeros */
  2286. };
  2287. memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
  2288. lseek(fd, 0, SEEK_SET);
  2289. err = do_write(&ff, &f_header, sizeof(f_header));
  2290. if (err < 0) {
  2291. pr_debug("failed to write perf header\n");
  2292. return err;
  2293. }
  2294. lseek(fd, header->data_offset + header->data_size, SEEK_SET);
  2295. return 0;
  2296. }
  2297. static int perf_header__getbuffer64(struct perf_header *header,
  2298. int fd, void *buf, size_t size)
  2299. {
  2300. if (readn(fd, buf, size) <= 0)
  2301. return -1;
  2302. if (header->needs_swap)
  2303. mem_bswap_64(buf, size);
  2304. return 0;
  2305. }
  2306. int perf_header__process_sections(struct perf_header *header, int fd,
  2307. void *data,
  2308. int (*process)(struct perf_file_section *section,
  2309. struct perf_header *ph,
  2310. int feat, int fd, void *data))
  2311. {
  2312. struct perf_file_section *feat_sec, *sec;
  2313. int nr_sections;
  2314. int sec_size;
  2315. int feat;
  2316. int err;
  2317. nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
  2318. if (!nr_sections)
  2319. return 0;
  2320. feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
  2321. if (!feat_sec)
  2322. return -1;
  2323. sec_size = sizeof(*feat_sec) * nr_sections;
  2324. lseek(fd, header->feat_offset, SEEK_SET);
  2325. err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
  2326. if (err < 0)
  2327. goto out_free;
  2328. for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
  2329. err = process(sec++, header, feat, fd, data);
  2330. if (err < 0)
  2331. goto out_free;
  2332. }
  2333. err = 0;
  2334. out_free:
  2335. free(feat_sec);
  2336. return err;
  2337. }
  2338. static const int attr_file_abi_sizes[] = {
  2339. [0] = PERF_ATTR_SIZE_VER0,
  2340. [1] = PERF_ATTR_SIZE_VER1,
  2341. [2] = PERF_ATTR_SIZE_VER2,
  2342. [3] = PERF_ATTR_SIZE_VER3,
  2343. [4] = PERF_ATTR_SIZE_VER4,
  2344. 0,
  2345. };
  2346. /*
  2347. * In the legacy file format, the magic number is not used to encode endianness.
  2348. * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
  2349. * on ABI revisions, we need to try all combinations for all endianness to
  2350. * detect the endianness.
  2351. */
  2352. static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
  2353. {
  2354. uint64_t ref_size, attr_size;
  2355. int i;
  2356. for (i = 0 ; attr_file_abi_sizes[i]; i++) {
  2357. ref_size = attr_file_abi_sizes[i]
  2358. + sizeof(struct perf_file_section);
  2359. if (hdr_sz != ref_size) {
  2360. attr_size = bswap_64(hdr_sz);
  2361. if (attr_size != ref_size)
  2362. continue;
  2363. ph->needs_swap = true;
  2364. }
  2365. pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
  2366. i,
  2367. ph->needs_swap);
  2368. return 0;
  2369. }
  2370. /* could not determine endianness */
  2371. return -1;
  2372. }
  2373. #define PERF_PIPE_HDR_VER0 16
  2374. static const size_t attr_pipe_abi_sizes[] = {
  2375. [0] = PERF_PIPE_HDR_VER0,
  2376. 0,
  2377. };
  2378. /*
  2379. * In the legacy pipe format, there is an implicit assumption that endiannesss
  2380. * between host recording the samples, and host parsing the samples is the
  2381. * same. This is not always the case given that the pipe output may always be
  2382. * redirected into a file and analyzed on a different machine with possibly a
  2383. * different endianness and perf_event ABI revsions in the perf tool itself.
  2384. */
  2385. static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
  2386. {
  2387. u64 attr_size;
  2388. int i;
  2389. for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
  2390. if (hdr_sz != attr_pipe_abi_sizes[i]) {
  2391. attr_size = bswap_64(hdr_sz);
  2392. if (attr_size != hdr_sz)
  2393. continue;
  2394. ph->needs_swap = true;
  2395. }
  2396. pr_debug("Pipe ABI%d perf.data file detected\n", i);
  2397. return 0;
  2398. }
  2399. return -1;
  2400. }
  2401. bool is_perf_magic(u64 magic)
  2402. {
  2403. if (!memcmp(&magic, __perf_magic1, sizeof(magic))
  2404. || magic == __perf_magic2
  2405. || magic == __perf_magic2_sw)
  2406. return true;
  2407. return false;
  2408. }
  2409. static int check_magic_endian(u64 magic, uint64_t hdr_sz,
  2410. bool is_pipe, struct perf_header *ph)
  2411. {
  2412. int ret;
  2413. /* check for legacy format */
  2414. ret = memcmp(&magic, __perf_magic1, sizeof(magic));
  2415. if (ret == 0) {
  2416. ph->version = PERF_HEADER_VERSION_1;
  2417. pr_debug("legacy perf.data format\n");
  2418. if (is_pipe)
  2419. return try_all_pipe_abis(hdr_sz, ph);
  2420. return try_all_file_abis(hdr_sz, ph);
  2421. }
  2422. /*
  2423. * the new magic number serves two purposes:
  2424. * - unique number to identify actual perf.data files
  2425. * - encode endianness of file
  2426. */
  2427. ph->version = PERF_HEADER_VERSION_2;
  2428. /* check magic number with one endianness */
  2429. if (magic == __perf_magic2)
  2430. return 0;
  2431. /* check magic number with opposite endianness */
  2432. if (magic != __perf_magic2_sw)
  2433. return -1;
  2434. ph->needs_swap = true;
  2435. return 0;
  2436. }
  2437. int perf_file_header__read(struct perf_file_header *header,
  2438. struct perf_header *ph, int fd)
  2439. {
  2440. ssize_t ret;
  2441. lseek(fd, 0, SEEK_SET);
  2442. ret = readn(fd, header, sizeof(*header));
  2443. if (ret <= 0)
  2444. return -1;
  2445. if (check_magic_endian(header->magic,
  2446. header->attr_size, false, ph) < 0) {
  2447. pr_debug("magic/endian check failed\n");
  2448. return -1;
  2449. }
  2450. if (ph->needs_swap) {
  2451. mem_bswap_64(header, offsetof(struct perf_file_header,
  2452. adds_features));
  2453. }
  2454. if (header->size != sizeof(*header)) {
  2455. /* Support the previous format */
  2456. if (header->size == offsetof(typeof(*header), adds_features))
  2457. bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
  2458. else
  2459. return -1;
  2460. } else if (ph->needs_swap) {
  2461. /*
  2462. * feature bitmap is declared as an array of unsigned longs --
  2463. * not good since its size can differ between the host that
  2464. * generated the data file and the host analyzing the file.
  2465. *
  2466. * We need to handle endianness, but we don't know the size of
  2467. * the unsigned long where the file was generated. Take a best
  2468. * guess at determining it: try 64-bit swap first (ie., file
  2469. * created on a 64-bit host), and check if the hostname feature
  2470. * bit is set (this feature bit is forced on as of fbe96f2).
  2471. * If the bit is not, undo the 64-bit swap and try a 32-bit
  2472. * swap. If the hostname bit is still not set (e.g., older data
  2473. * file), punt and fallback to the original behavior --
  2474. * clearing all feature bits and setting buildid.
  2475. */
  2476. mem_bswap_64(&header->adds_features,
  2477. BITS_TO_U64(HEADER_FEAT_BITS));
  2478. if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
  2479. /* unswap as u64 */
  2480. mem_bswap_64(&header->adds_features,
  2481. BITS_TO_U64(HEADER_FEAT_BITS));
  2482. /* unswap as u32 */
  2483. mem_bswap_32(&header->adds_features,
  2484. BITS_TO_U32(HEADER_FEAT_BITS));
  2485. }
  2486. if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
  2487. bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
  2488. set_bit(HEADER_BUILD_ID, header->adds_features);
  2489. }
  2490. }
  2491. memcpy(&ph->adds_features, &header->adds_features,
  2492. sizeof(ph->adds_features));
  2493. ph->data_offset = header->data.offset;
  2494. ph->data_size = header->data.size;
  2495. ph->feat_offset = header->data.offset + header->data.size;
  2496. return 0;
  2497. }
  2498. static int perf_file_section__process(struct perf_file_section *section,
  2499. struct perf_header *ph,
  2500. int feat, int fd, void *data)
  2501. {
  2502. struct feat_fd fdd = {
  2503. .fd = fd,
  2504. .ph = ph,
  2505. .size = section->size,
  2506. .offset = section->offset,
  2507. };
  2508. if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
  2509. pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
  2510. "%d, continuing...\n", section->offset, feat);
  2511. return 0;
  2512. }
  2513. if (feat >= HEADER_LAST_FEATURE) {
  2514. pr_debug("unknown feature %d, continuing...\n", feat);
  2515. return 0;
  2516. }
  2517. if (!feat_ops[feat].process)
  2518. return 0;
  2519. return feat_ops[feat].process(&fdd, data);
  2520. }
  2521. static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
  2522. struct perf_header *ph, int fd,
  2523. bool repipe)
  2524. {
  2525. struct feat_fd ff = {
  2526. .fd = STDOUT_FILENO,
  2527. .ph = ph,
  2528. };
  2529. ssize_t ret;
  2530. ret = readn(fd, header, sizeof(*header));
  2531. if (ret <= 0)
  2532. return -1;
  2533. if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
  2534. pr_debug("endian/magic failed\n");
  2535. return -1;
  2536. }
  2537. if (ph->needs_swap)
  2538. header->size = bswap_64(header->size);
  2539. if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
  2540. return -1;
  2541. return 0;
  2542. }
  2543. static int perf_header__read_pipe(struct perf_session *session)
  2544. {
  2545. struct perf_header *header = &session->header;
  2546. struct perf_pipe_file_header f_header;
  2547. if (perf_file_header__read_pipe(&f_header, header,
  2548. perf_data__fd(session->data),
  2549. session->repipe) < 0) {
  2550. pr_debug("incompatible file format\n");
  2551. return -EINVAL;
  2552. }
  2553. return 0;
  2554. }
  2555. static int read_attr(int fd, struct perf_header *ph,
  2556. struct perf_file_attr *f_attr)
  2557. {
  2558. struct perf_event_attr *attr = &f_attr->attr;
  2559. size_t sz, left;
  2560. size_t our_sz = sizeof(f_attr->attr);
  2561. ssize_t ret;
  2562. memset(f_attr, 0, sizeof(*f_attr));
  2563. /* read minimal guaranteed structure */
  2564. ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
  2565. if (ret <= 0) {
  2566. pr_debug("cannot read %d bytes of header attr\n",
  2567. PERF_ATTR_SIZE_VER0);
  2568. return -1;
  2569. }
  2570. /* on file perf_event_attr size */
  2571. sz = attr->size;
  2572. if (ph->needs_swap)
  2573. sz = bswap_32(sz);
  2574. if (sz == 0) {
  2575. /* assume ABI0 */
  2576. sz = PERF_ATTR_SIZE_VER0;
  2577. } else if (sz > our_sz) {
  2578. pr_debug("file uses a more recent and unsupported ABI"
  2579. " (%zu bytes extra)\n", sz - our_sz);
  2580. return -1;
  2581. }
  2582. /* what we have not yet read and that we know about */
  2583. left = sz - PERF_ATTR_SIZE_VER0;
  2584. if (left) {
  2585. void *ptr = attr;
  2586. ptr += PERF_ATTR_SIZE_VER0;
  2587. ret = readn(fd, ptr, left);
  2588. }
  2589. /* read perf_file_section, ids are read in caller */
  2590. ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
  2591. return ret <= 0 ? -1 : 0;
  2592. }
  2593. static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
  2594. struct tep_handle *pevent)
  2595. {
  2596. struct event_format *event;
  2597. char bf[128];
  2598. /* already prepared */
  2599. if (evsel->tp_format)
  2600. return 0;
  2601. if (pevent == NULL) {
  2602. pr_debug("broken or missing trace data\n");
  2603. return -1;
  2604. }
  2605. event = tep_find_event(pevent, evsel->attr.config);
  2606. if (event == NULL) {
  2607. pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
  2608. return -1;
  2609. }
  2610. if (!evsel->name) {
  2611. snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
  2612. evsel->name = strdup(bf);
  2613. if (evsel->name == NULL)
  2614. return -1;
  2615. }
  2616. evsel->tp_format = event;
  2617. return 0;
  2618. }
  2619. static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
  2620. struct tep_handle *pevent)
  2621. {
  2622. struct perf_evsel *pos;
  2623. evlist__for_each_entry(evlist, pos) {
  2624. if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
  2625. perf_evsel__prepare_tracepoint_event(pos, pevent))
  2626. return -1;
  2627. }
  2628. return 0;
  2629. }
  2630. int perf_session__read_header(struct perf_session *session)
  2631. {
  2632. struct perf_data *data = session->data;
  2633. struct perf_header *header = &session->header;
  2634. struct perf_file_header f_header;
  2635. struct perf_file_attr f_attr;
  2636. u64 f_id;
  2637. int nr_attrs, nr_ids, i, j;
  2638. int fd = perf_data__fd(data);
  2639. session->evlist = perf_evlist__new();
  2640. if (session->evlist == NULL)
  2641. return -ENOMEM;
  2642. session->evlist->env = &header->env;
  2643. session->machines.host.env = &header->env;
  2644. if (perf_data__is_pipe(data))
  2645. return perf_header__read_pipe(session);
  2646. if (perf_file_header__read(&f_header, header, fd) < 0)
  2647. return -EINVAL;
  2648. /*
  2649. * Sanity check that perf.data was written cleanly; data size is
  2650. * initialized to 0 and updated only if the on_exit function is run.
  2651. * If data size is still 0 then the file contains only partial
  2652. * information. Just warn user and process it as much as it can.
  2653. */
  2654. if (f_header.data.size == 0) {
  2655. pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
  2656. "Was the 'perf record' command properly terminated?\n",
  2657. data->file.path);
  2658. }
  2659. if (f_header.attr_size == 0) {
  2660. pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
  2661. "Was the 'perf record' command properly terminated?\n",
  2662. data->file.path);
  2663. return -EINVAL;
  2664. }
  2665. nr_attrs = f_header.attrs.size / f_header.attr_size;
  2666. lseek(fd, f_header.attrs.offset, SEEK_SET);
  2667. for (i = 0; i < nr_attrs; i++) {
  2668. struct perf_evsel *evsel;
  2669. off_t tmp;
  2670. if (read_attr(fd, header, &f_attr) < 0)
  2671. goto out_errno;
  2672. if (header->needs_swap) {
  2673. f_attr.ids.size = bswap_64(f_attr.ids.size);
  2674. f_attr.ids.offset = bswap_64(f_attr.ids.offset);
  2675. perf_event__attr_swap(&f_attr.attr);
  2676. }
  2677. tmp = lseek(fd, 0, SEEK_CUR);
  2678. evsel = perf_evsel__new(&f_attr.attr);
  2679. if (evsel == NULL)
  2680. goto out_delete_evlist;
  2681. evsel->needs_swap = header->needs_swap;
  2682. /*
  2683. * Do it before so that if perf_evsel__alloc_id fails, this
  2684. * entry gets purged too at perf_evlist__delete().
  2685. */
  2686. perf_evlist__add(session->evlist, evsel);
  2687. nr_ids = f_attr.ids.size / sizeof(u64);
  2688. /*
  2689. * We don't have the cpu and thread maps on the header, so
  2690. * for allocating the perf_sample_id table we fake 1 cpu and
  2691. * hattr->ids threads.
  2692. */
  2693. if (perf_evsel__alloc_id(evsel, 1, nr_ids))
  2694. goto out_delete_evlist;
  2695. lseek(fd, f_attr.ids.offset, SEEK_SET);
  2696. for (j = 0; j < nr_ids; j++) {
  2697. if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
  2698. goto out_errno;
  2699. perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
  2700. }
  2701. lseek(fd, tmp, SEEK_SET);
  2702. }
  2703. perf_header__process_sections(header, fd, &session->tevent,
  2704. perf_file_section__process);
  2705. if (perf_evlist__prepare_tracepoint_events(session->evlist,
  2706. session->tevent.pevent))
  2707. goto out_delete_evlist;
  2708. return 0;
  2709. out_errno:
  2710. return -errno;
  2711. out_delete_evlist:
  2712. perf_evlist__delete(session->evlist);
  2713. session->evlist = NULL;
  2714. return -ENOMEM;
  2715. }
  2716. int perf_event__synthesize_attr(struct perf_tool *tool,
  2717. struct perf_event_attr *attr, u32 ids, u64 *id,
  2718. perf_event__handler_t process)
  2719. {
  2720. union perf_event *ev;
  2721. size_t size;
  2722. int err;
  2723. size = sizeof(struct perf_event_attr);
  2724. size = PERF_ALIGN(size, sizeof(u64));
  2725. size += sizeof(struct perf_event_header);
  2726. size += ids * sizeof(u64);
  2727. ev = zalloc(size);
  2728. if (ev == NULL)
  2729. return -ENOMEM;
  2730. ev->attr.attr = *attr;
  2731. memcpy(ev->attr.id, id, ids * sizeof(u64));
  2732. ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
  2733. ev->attr.header.size = (u16)size;
  2734. if (ev->attr.header.size == size)
  2735. err = process(tool, ev, NULL, NULL);
  2736. else
  2737. err = -E2BIG;
  2738. free(ev);
  2739. return err;
  2740. }
  2741. int perf_event__synthesize_features(struct perf_tool *tool,
  2742. struct perf_session *session,
  2743. struct perf_evlist *evlist,
  2744. perf_event__handler_t process)
  2745. {
  2746. struct perf_header *header = &session->header;
  2747. struct feat_fd ff;
  2748. struct feature_event *fe;
  2749. size_t sz, sz_hdr;
  2750. int feat, ret;
  2751. sz_hdr = sizeof(fe->header);
  2752. sz = sizeof(union perf_event);
  2753. /* get a nice alignment */
  2754. sz = PERF_ALIGN(sz, page_size);
  2755. memset(&ff, 0, sizeof(ff));
  2756. ff.buf = malloc(sz);
  2757. if (!ff.buf)
  2758. return -ENOMEM;
  2759. ff.size = sz - sz_hdr;
  2760. for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
  2761. if (!feat_ops[feat].synthesize) {
  2762. pr_debug("No record header feature for header :%d\n", feat);
  2763. continue;
  2764. }
  2765. ff.offset = sizeof(*fe);
  2766. ret = feat_ops[feat].write(&ff, evlist);
  2767. if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
  2768. pr_debug("Error writing feature\n");
  2769. continue;
  2770. }
  2771. /* ff.buf may have changed due to realloc in do_write() */
  2772. fe = ff.buf;
  2773. memset(fe, 0, sizeof(*fe));
  2774. fe->feat_id = feat;
  2775. fe->header.type = PERF_RECORD_HEADER_FEATURE;
  2776. fe->header.size = ff.offset;
  2777. ret = process(tool, ff.buf, NULL, NULL);
  2778. if (ret) {
  2779. free(ff.buf);
  2780. return ret;
  2781. }
  2782. }
  2783. /* Send HEADER_LAST_FEATURE mark. */
  2784. fe = ff.buf;
  2785. fe->feat_id = HEADER_LAST_FEATURE;
  2786. fe->header.type = PERF_RECORD_HEADER_FEATURE;
  2787. fe->header.size = sizeof(*fe);
  2788. ret = process(tool, ff.buf, NULL, NULL);
  2789. free(ff.buf);
  2790. return ret;
  2791. }
  2792. int perf_event__process_feature(struct perf_tool *tool,
  2793. union perf_event *event,
  2794. struct perf_session *session __maybe_unused)
  2795. {
  2796. struct feat_fd ff = { .fd = 0 };
  2797. struct feature_event *fe = (struct feature_event *)event;
  2798. int type = fe->header.type;
  2799. u64 feat = fe->feat_id;
  2800. if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
  2801. pr_warning("invalid record type %d in pipe-mode\n", type);
  2802. return 0;
  2803. }
  2804. if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
  2805. pr_warning("invalid record type %d in pipe-mode\n", type);
  2806. return -1;
  2807. }
  2808. if (!feat_ops[feat].process)
  2809. return 0;
  2810. ff.buf = (void *)fe->data;
  2811. ff.size = event->header.size - sizeof(*fe);
  2812. ff.ph = &session->header;
  2813. if (feat_ops[feat].process(&ff, NULL))
  2814. return -1;
  2815. if (!feat_ops[feat].print || !tool->show_feat_hdr)
  2816. return 0;
  2817. if (!feat_ops[feat].full_only ||
  2818. tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
  2819. feat_ops[feat].print(&ff, stdout);
  2820. } else {
  2821. fprintf(stdout, "# %s info available, use -I to display\n",
  2822. feat_ops[feat].name);
  2823. }
  2824. return 0;
  2825. }
  2826. static struct event_update_event *
  2827. event_update_event__new(size_t size, u64 type, u64 id)
  2828. {
  2829. struct event_update_event *ev;
  2830. size += sizeof(*ev);
  2831. size = PERF_ALIGN(size, sizeof(u64));
  2832. ev = zalloc(size);
  2833. if (ev) {
  2834. ev->header.type = PERF_RECORD_EVENT_UPDATE;
  2835. ev->header.size = (u16)size;
  2836. ev->type = type;
  2837. ev->id = id;
  2838. }
  2839. return ev;
  2840. }
  2841. int
  2842. perf_event__synthesize_event_update_unit(struct perf_tool *tool,
  2843. struct perf_evsel *evsel,
  2844. perf_event__handler_t process)
  2845. {
  2846. struct event_update_event *ev;
  2847. size_t size = strlen(evsel->unit);
  2848. int err;
  2849. ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
  2850. if (ev == NULL)
  2851. return -ENOMEM;
  2852. strlcpy(ev->data, evsel->unit, size + 1);
  2853. err = process(tool, (union perf_event *)ev, NULL, NULL);
  2854. free(ev);
  2855. return err;
  2856. }
  2857. int
  2858. perf_event__synthesize_event_update_scale(struct perf_tool *tool,
  2859. struct perf_evsel *evsel,
  2860. perf_event__handler_t process)
  2861. {
  2862. struct event_update_event *ev;
  2863. struct event_update_event_scale *ev_data;
  2864. int err;
  2865. ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
  2866. if (ev == NULL)
  2867. return -ENOMEM;
  2868. ev_data = (struct event_update_event_scale *) ev->data;
  2869. ev_data->scale = evsel->scale;
  2870. err = process(tool, (union perf_event*) ev, NULL, NULL);
  2871. free(ev);
  2872. return err;
  2873. }
  2874. int
  2875. perf_event__synthesize_event_update_name(struct perf_tool *tool,
  2876. struct perf_evsel *evsel,
  2877. perf_event__handler_t process)
  2878. {
  2879. struct event_update_event *ev;
  2880. size_t len = strlen(evsel->name);
  2881. int err;
  2882. ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
  2883. if (ev == NULL)
  2884. return -ENOMEM;
  2885. strlcpy(ev->data, evsel->name, len + 1);
  2886. err = process(tool, (union perf_event*) ev, NULL, NULL);
  2887. free(ev);
  2888. return err;
  2889. }
  2890. int
  2891. perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
  2892. struct perf_evsel *evsel,
  2893. perf_event__handler_t process)
  2894. {
  2895. size_t size = sizeof(struct event_update_event);
  2896. struct event_update_event *ev;
  2897. int max, err;
  2898. u16 type;
  2899. if (!evsel->own_cpus)
  2900. return 0;
  2901. ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
  2902. if (!ev)
  2903. return -ENOMEM;
  2904. ev->header.type = PERF_RECORD_EVENT_UPDATE;
  2905. ev->header.size = (u16)size;
  2906. ev->type = PERF_EVENT_UPDATE__CPUS;
  2907. ev->id = evsel->id[0];
  2908. cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
  2909. evsel->own_cpus,
  2910. type, max);
  2911. err = process(tool, (union perf_event*) ev, NULL, NULL);
  2912. free(ev);
  2913. return err;
  2914. }
  2915. size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
  2916. {
  2917. struct event_update_event *ev = &event->event_update;
  2918. struct event_update_event_scale *ev_scale;
  2919. struct event_update_event_cpus *ev_cpus;
  2920. struct cpu_map *map;
  2921. size_t ret;
  2922. ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
  2923. switch (ev->type) {
  2924. case PERF_EVENT_UPDATE__SCALE:
  2925. ev_scale = (struct event_update_event_scale *) ev->data;
  2926. ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
  2927. break;
  2928. case PERF_EVENT_UPDATE__UNIT:
  2929. ret += fprintf(fp, "... unit: %s\n", ev->data);
  2930. break;
  2931. case PERF_EVENT_UPDATE__NAME:
  2932. ret += fprintf(fp, "... name: %s\n", ev->data);
  2933. break;
  2934. case PERF_EVENT_UPDATE__CPUS:
  2935. ev_cpus = (struct event_update_event_cpus *) ev->data;
  2936. ret += fprintf(fp, "... ");
  2937. map = cpu_map__new_data(&ev_cpus->cpus);
  2938. if (map)
  2939. ret += cpu_map__fprintf(map, fp);
  2940. else
  2941. ret += fprintf(fp, "failed to get cpus\n");
  2942. break;
  2943. default:
  2944. ret += fprintf(fp, "... unknown type\n");
  2945. break;
  2946. }
  2947. return ret;
  2948. }
  2949. int perf_event__synthesize_attrs(struct perf_tool *tool,
  2950. struct perf_session *session,
  2951. perf_event__handler_t process)
  2952. {
  2953. struct perf_evsel *evsel;
  2954. int err = 0;
  2955. evlist__for_each_entry(session->evlist, evsel) {
  2956. err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
  2957. evsel->id, process);
  2958. if (err) {
  2959. pr_debug("failed to create perf header attribute\n");
  2960. return err;
  2961. }
  2962. }
  2963. return err;
  2964. }
  2965. static bool has_unit(struct perf_evsel *counter)
  2966. {
  2967. return counter->unit && *counter->unit;
  2968. }
  2969. static bool has_scale(struct perf_evsel *counter)
  2970. {
  2971. return counter->scale != 1;
  2972. }
  2973. int perf_event__synthesize_extra_attr(struct perf_tool *tool,
  2974. struct perf_evlist *evsel_list,
  2975. perf_event__handler_t process,
  2976. bool is_pipe)
  2977. {
  2978. struct perf_evsel *counter;
  2979. int err;
  2980. /*
  2981. * Synthesize other events stuff not carried within
  2982. * attr event - unit, scale, name
  2983. */
  2984. evlist__for_each_entry(evsel_list, counter) {
  2985. if (!counter->supported)
  2986. continue;
  2987. /*
  2988. * Synthesize unit and scale only if it's defined.
  2989. */
  2990. if (has_unit(counter)) {
  2991. err = perf_event__synthesize_event_update_unit(tool, counter, process);
  2992. if (err < 0) {
  2993. pr_err("Couldn't synthesize evsel unit.\n");
  2994. return err;
  2995. }
  2996. }
  2997. if (has_scale(counter)) {
  2998. err = perf_event__synthesize_event_update_scale(tool, counter, process);
  2999. if (err < 0) {
  3000. pr_err("Couldn't synthesize evsel counter.\n");
  3001. return err;
  3002. }
  3003. }
  3004. if (counter->own_cpus) {
  3005. err = perf_event__synthesize_event_update_cpus(tool, counter, process);
  3006. if (err < 0) {
  3007. pr_err("Couldn't synthesize evsel cpus.\n");
  3008. return err;
  3009. }
  3010. }
  3011. /*
  3012. * Name is needed only for pipe output,
  3013. * perf.data carries event names.
  3014. */
  3015. if (is_pipe) {
  3016. err = perf_event__synthesize_event_update_name(tool, counter, process);
  3017. if (err < 0) {
  3018. pr_err("Couldn't synthesize evsel name.\n");
  3019. return err;
  3020. }
  3021. }
  3022. }
  3023. return 0;
  3024. }
  3025. int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
  3026. union perf_event *event,
  3027. struct perf_evlist **pevlist)
  3028. {
  3029. u32 i, ids, n_ids;
  3030. struct perf_evsel *evsel;
  3031. struct perf_evlist *evlist = *pevlist;
  3032. if (evlist == NULL) {
  3033. *pevlist = evlist = perf_evlist__new();
  3034. if (evlist == NULL)
  3035. return -ENOMEM;
  3036. }
  3037. evsel = perf_evsel__new(&event->attr.attr);
  3038. if (evsel == NULL)
  3039. return -ENOMEM;
  3040. perf_evlist__add(evlist, evsel);
  3041. ids = event->header.size;
  3042. ids -= (void *)&event->attr.id - (void *)event;
  3043. n_ids = ids / sizeof(u64);
  3044. /*
  3045. * We don't have the cpu and thread maps on the header, so
  3046. * for allocating the perf_sample_id table we fake 1 cpu and
  3047. * hattr->ids threads.
  3048. */
  3049. if (perf_evsel__alloc_id(evsel, 1, n_ids))
  3050. return -ENOMEM;
  3051. for (i = 0; i < n_ids; i++) {
  3052. perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
  3053. }
  3054. return 0;
  3055. }
  3056. int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
  3057. union perf_event *event,
  3058. struct perf_evlist **pevlist)
  3059. {
  3060. struct event_update_event *ev = &event->event_update;
  3061. struct event_update_event_scale *ev_scale;
  3062. struct event_update_event_cpus *ev_cpus;
  3063. struct perf_evlist *evlist;
  3064. struct perf_evsel *evsel;
  3065. struct cpu_map *map;
  3066. if (!pevlist || *pevlist == NULL)
  3067. return -EINVAL;
  3068. evlist = *pevlist;
  3069. evsel = perf_evlist__id2evsel(evlist, ev->id);
  3070. if (evsel == NULL)
  3071. return -EINVAL;
  3072. switch (ev->type) {
  3073. case PERF_EVENT_UPDATE__UNIT:
  3074. evsel->unit = strdup(ev->data);
  3075. break;
  3076. case PERF_EVENT_UPDATE__NAME:
  3077. evsel->name = strdup(ev->data);
  3078. break;
  3079. case PERF_EVENT_UPDATE__SCALE:
  3080. ev_scale = (struct event_update_event_scale *) ev->data;
  3081. evsel->scale = ev_scale->scale;
  3082. break;
  3083. case PERF_EVENT_UPDATE__CPUS:
  3084. ev_cpus = (struct event_update_event_cpus *) ev->data;
  3085. map = cpu_map__new_data(&ev_cpus->cpus);
  3086. if (map)
  3087. evsel->own_cpus = map;
  3088. else
  3089. pr_err("failed to get event_update cpus\n");
  3090. default:
  3091. break;
  3092. }
  3093. return 0;
  3094. }
  3095. int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
  3096. struct perf_evlist *evlist,
  3097. perf_event__handler_t process)
  3098. {
  3099. union perf_event ev;
  3100. struct tracing_data *tdata;
  3101. ssize_t size = 0, aligned_size = 0, padding;
  3102. struct feat_fd ff;
  3103. int err __maybe_unused = 0;
  3104. /*
  3105. * We are going to store the size of the data followed
  3106. * by the data contents. Since the fd descriptor is a pipe,
  3107. * we cannot seek back to store the size of the data once
  3108. * we know it. Instead we:
  3109. *
  3110. * - write the tracing data to the temp file
  3111. * - get/write the data size to pipe
  3112. * - write the tracing data from the temp file
  3113. * to the pipe
  3114. */
  3115. tdata = tracing_data_get(&evlist->entries, fd, true);
  3116. if (!tdata)
  3117. return -1;
  3118. memset(&ev, 0, sizeof(ev));
  3119. ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
  3120. size = tdata->size;
  3121. aligned_size = PERF_ALIGN(size, sizeof(u64));
  3122. padding = aligned_size - size;
  3123. ev.tracing_data.header.size = sizeof(ev.tracing_data);
  3124. ev.tracing_data.size = aligned_size;
  3125. process(tool, &ev, NULL, NULL);
  3126. /*
  3127. * The put function will copy all the tracing data
  3128. * stored in temp file to the pipe.
  3129. */
  3130. tracing_data_put(tdata);
  3131. ff = (struct feat_fd){ .fd = fd };
  3132. if (write_padded(&ff, NULL, 0, padding))
  3133. return -1;
  3134. return aligned_size;
  3135. }
  3136. int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
  3137. union perf_event *event,
  3138. struct perf_session *session)
  3139. {
  3140. ssize_t size_read, padding, size = event->tracing_data.size;
  3141. int fd = perf_data__fd(session->data);
  3142. off_t offset = lseek(fd, 0, SEEK_CUR);
  3143. char buf[BUFSIZ];
  3144. /* setup for reading amidst mmap */
  3145. lseek(fd, offset + sizeof(struct tracing_data_event),
  3146. SEEK_SET);
  3147. size_read = trace_report(fd, &session->tevent,
  3148. session->repipe);
  3149. padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
  3150. if (readn(fd, buf, padding) < 0) {
  3151. pr_err("%s: reading input file", __func__);
  3152. return -1;
  3153. }
  3154. if (session->repipe) {
  3155. int retw = write(STDOUT_FILENO, buf, padding);
  3156. if (retw <= 0 || retw != padding) {
  3157. pr_err("%s: repiping tracing data padding", __func__);
  3158. return -1;
  3159. }
  3160. }
  3161. if (size_read + padding != size) {
  3162. pr_err("%s: tracing data size mismatch", __func__);
  3163. return -1;
  3164. }
  3165. perf_evlist__prepare_tracepoint_events(session->evlist,
  3166. session->tevent.pevent);
  3167. return size_read + padding;
  3168. }
  3169. int perf_event__synthesize_build_id(struct perf_tool *tool,
  3170. struct dso *pos, u16 misc,
  3171. perf_event__handler_t process,
  3172. struct machine *machine)
  3173. {
  3174. union perf_event ev;
  3175. size_t len;
  3176. int err = 0;
  3177. if (!pos->hit)
  3178. return err;
  3179. memset(&ev, 0, sizeof(ev));
  3180. len = pos->long_name_len + 1;
  3181. len = PERF_ALIGN(len, NAME_ALIGN);
  3182. memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
  3183. ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
  3184. ev.build_id.header.misc = misc;
  3185. ev.build_id.pid = machine->pid;
  3186. ev.build_id.header.size = sizeof(ev.build_id) + len;
  3187. memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
  3188. err = process(tool, &ev, NULL, machine);
  3189. return err;
  3190. }
  3191. int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
  3192. union perf_event *event,
  3193. struct perf_session *session)
  3194. {
  3195. __event_process_build_id(&event->build_id,
  3196. event->build_id.filename,
  3197. session);
  3198. return 0;
  3199. }