header.c 72 KB

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