s390-cpumsf.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2018
  4. * Auxtrace support for s390 CPU-Measurement Sampling Facility
  5. *
  6. * Author(s): Thomas Richter <tmricht@linux.ibm.com>
  7. *
  8. * Auxiliary traces are collected during 'perf record' using rbd000 event.
  9. * Several PERF_RECORD_XXX are generated during recording:
  10. *
  11. * PERF_RECORD_AUX:
  12. * Records that new data landed in the AUX buffer part.
  13. * PERF_RECORD_AUXTRACE:
  14. * Defines auxtrace data. Followed by the actual data. The contents of
  15. * the auxtrace data is dependent on the event and the CPU.
  16. * This record is generated by perf record command. For details
  17. * see Documentation/perf.data-file-format.txt.
  18. * PERF_RECORD_AUXTRACE_INFO:
  19. * Defines a table of contains for PERF_RECORD_AUXTRACE records. This
  20. * record is generated during 'perf record' command. Each record contains
  21. * up to 256 entries describing offset and size of the AUXTRACE data in the
  22. * perf.data file.
  23. * PERF_RECORD_AUXTRACE_ERROR:
  24. * Indicates an error during AUXTRACE collection such as buffer overflow.
  25. * PERF_RECORD_FINISHED_ROUND:
  26. * Perf events are not necessarily in time stamp order, as they can be
  27. * collected in parallel on different CPUs. If the events should be
  28. * processed in time order they need to be sorted first.
  29. * Perf report guarantees that there is no reordering over a
  30. * PERF_RECORD_FINISHED_ROUND boundary event. All perf records with a
  31. * time stamp lower than this record are processed (and displayed) before
  32. * the succeeding perf record are processed.
  33. *
  34. * These records are evaluated during perf report command.
  35. *
  36. * 1. PERF_RECORD_AUXTRACE_INFO is used to set up the infrastructure for
  37. * auxiliary trace data processing. See s390_cpumsf_process_auxtrace_info()
  38. * below.
  39. * Auxiliary trace data is collected per CPU. To merge the data into the report
  40. * an auxtrace_queue is created for each CPU. It is assumed that the auxtrace
  41. * data is in ascending order.
  42. *
  43. * Each queue has a double linked list of auxtrace_buffers. This list contains
  44. * the offset and size of a CPU's auxtrace data. During auxtrace processing
  45. * the data portion is mmap()'ed.
  46. *
  47. * To sort the queues in chronological order, all queue access is controlled
  48. * by the auxtrace_heap. This is basicly a stack, each stack element has two
  49. * entries, the queue number and a time stamp. However the stack is sorted by
  50. * the time stamps. The highest time stamp is at the bottom the lowest
  51. * (nearest) time stamp is at the top. That sort order is maintained at all
  52. * times!
  53. *
  54. * After the auxtrace infrastructure has been setup, the auxtrace queues are
  55. * filled with data (offset/size pairs) and the auxtrace_heap is populated.
  56. *
  57. * 2. PERF_RECORD_XXX processing triggers access to the auxtrace_queues.
  58. * Each record is handled by s390_cpumsf_process_event(). The time stamp of
  59. * the perf record is compared with the time stamp located on the auxtrace_heap
  60. * top element. If that time stamp is lower than the time stamp from the
  61. * record sample, the auxtrace queues will be processed. As auxtrace queues
  62. * control many auxtrace_buffers and each buffer can be quite large, the
  63. * auxtrace buffer might be processed only partially. In this case the
  64. * position in the auxtrace_buffer of that queue is remembered and the time
  65. * stamp of the last processed entry of the auxtrace_buffer replaces the
  66. * current auxtrace_heap top.
  67. *
  68. * 3. Auxtrace_queues might run of out data and are feeded by the
  69. * PERF_RECORD_AUXTRACE handling, see s390_cpumsf_process_auxtrace_event().
  70. *
  71. * Event Generation
  72. * Each sampling-data entry in the auxilary trace data generates a perf sample.
  73. * This sample is filled
  74. * with data from the auxtrace such as PID/TID, instruction address, CPU state,
  75. * etc. This sample is processed with perf_session__deliver_synth_event() to
  76. * be included into the GUI.
  77. *
  78. * 4. PERF_RECORD_FINISHED_ROUND event is used to process all the remaining
  79. * auxiliary traces entries until the time stamp of this record is reached
  80. * auxtrace_heap top. This is triggered by ordered_event->deliver().
  81. *
  82. *
  83. * Perf event processing.
  84. * Event processing of PERF_RECORD_XXX entries relies on time stamp entries.
  85. * This is the function call sequence:
  86. *
  87. * __cmd_report()
  88. * |
  89. * perf_session__process_events()
  90. * |
  91. * __perf_session__process_events()
  92. * |
  93. * perf_session__process_event()
  94. * | This functions splits the PERF_RECORD_XXX records.
  95. * | - Those generated by perf record command (type number equal or higher
  96. * | than PERF_RECORD_USER_TYPE_START) are handled by
  97. * | perf_session__process_user_event(see below)
  98. * | - Those generated by the kernel are handled by
  99. * | perf_evlist__parse_sample_timestamp()
  100. * |
  101. * perf_evlist__parse_sample_timestamp()
  102. * | Extract time stamp from sample data.
  103. * |
  104. * perf_session__queue_event()
  105. * | If timestamp is positive the sample is entered into an ordered_event
  106. * | list, sort order is the timestamp. The event processing is deferred until
  107. * | later (see perf_session__process_user_event()).
  108. * | Other timestamps (0 or -1) are handled immediately by
  109. * | perf_session__deliver_event(). These are events generated at start up
  110. * | of command perf record. They create PERF_RECORD_COMM and PERF_RECORD_MMAP*
  111. * | records. They are needed to create a list of running processes and its
  112. * | memory mappings and layout. They are needed at the beginning to enable
  113. * | command perf report to create process trees and memory mappings.
  114. * |
  115. * perf_session__deliver_event()
  116. * | Delivers a PERF_RECORD_XXX entry for handling.
  117. * |
  118. * auxtrace__process_event()
  119. * | The timestamp of the PERF_RECORD_XXX entry is taken to correlate with
  120. * | time stamps from the auxiliary trace buffers. This enables
  121. * | synchronization between auxiliary trace data and the events on the
  122. * | perf.data file.
  123. * |
  124. * machine__deliver_event()
  125. * | Handles the PERF_RECORD_XXX event. This depends on the record type.
  126. * It might update the process tree, update a process memory map or enter
  127. * a sample with IP and call back chain data into GUI data pool.
  128. *
  129. *
  130. * Deferred processing determined by perf_session__process_user_event() is
  131. * finally processed when a PERF_RECORD_FINISHED_ROUND is encountered. These
  132. * are generated during command perf record.
  133. * The timestamp of PERF_RECORD_FINISHED_ROUND event is taken to process all
  134. * PERF_RECORD_XXX entries stored in the ordered_event list. This list was
  135. * built up while reading the perf.data file.
  136. * Each event is now processed by calling perf_session__deliver_event().
  137. * This enables time synchronization between the data in the perf.data file and
  138. * the data in the auxiliary trace buffers.
  139. */
  140. #include <endian.h>
  141. #include <errno.h>
  142. #include <byteswap.h>
  143. #include <inttypes.h>
  144. #include <linux/kernel.h>
  145. #include <linux/types.h>
  146. #include <linux/bitops.h>
  147. #include <linux/log2.h>
  148. #include <linux/zalloc.h>
  149. #include <sys/stat.h>
  150. #include <sys/types.h>
  151. #include "color.h"
  152. #include "evsel.h"
  153. #include "evlist.h"
  154. #include "machine.h"
  155. #include "session.h"
  156. #include "tool.h"
  157. #include "debug.h"
  158. #include "auxtrace.h"
  159. #include "s390-cpumsf.h"
  160. #include "s390-cpumsf-kernel.h"
  161. #include "s390-cpumcf-kernel.h"
  162. #include "config.h"
  163. struct s390_cpumsf {
  164. struct auxtrace auxtrace;
  165. struct auxtrace_queues queues;
  166. struct auxtrace_heap heap;
  167. struct perf_session *session;
  168. struct machine *machine;
  169. u32 auxtrace_type;
  170. u32 pmu_type;
  171. u16 machine_type;
  172. bool data_queued;
  173. bool use_logfile;
  174. char *logdir;
  175. };
  176. struct s390_cpumsf_queue {
  177. struct s390_cpumsf *sf;
  178. unsigned int queue_nr;
  179. struct auxtrace_buffer *buffer;
  180. int cpu;
  181. FILE *logfile;
  182. FILE *logfile_ctr;
  183. };
  184. /* Check if the raw data should be dumped to file. If this is the case and
  185. * the file to dump to has not been opened for writing, do so.
  186. *
  187. * Return 0 on success and greater zero on error so processing continues.
  188. */
  189. static int s390_cpumcf_dumpctr(struct s390_cpumsf *sf,
  190. struct perf_sample *sample)
  191. {
  192. struct s390_cpumsf_queue *sfq;
  193. struct auxtrace_queue *q;
  194. int rc = 0;
  195. if (!sf->use_logfile || sf->queues.nr_queues <= sample->cpu)
  196. return rc;
  197. q = &sf->queues.queue_array[sample->cpu];
  198. sfq = q->priv;
  199. if (!sfq) /* Queue not yet allocated */
  200. return rc;
  201. if (!sfq->logfile_ctr) {
  202. char *name;
  203. rc = (sf->logdir)
  204. ? asprintf(&name, "%s/aux.ctr.%02x",
  205. sf->logdir, sample->cpu)
  206. : asprintf(&name, "aux.ctr.%02x", sample->cpu);
  207. if (rc > 0)
  208. sfq->logfile_ctr = fopen(name, "w");
  209. if (sfq->logfile_ctr == NULL) {
  210. pr_err("Failed to open counter set log file %s, "
  211. "continue...\n", name);
  212. rc = 1;
  213. }
  214. free(name);
  215. }
  216. if (sfq->logfile_ctr) {
  217. /* See comment above for -4 */
  218. size_t n = fwrite(sample->raw_data, sample->raw_size - 4, 1,
  219. sfq->logfile_ctr);
  220. if (n != 1) {
  221. pr_err("Failed to write counter set data\n");
  222. rc = 1;
  223. }
  224. }
  225. return rc;
  226. }
  227. /* Display s390 CPU measurement facility basic-sampling data entry
  228. * Data written on s390 in big endian byte order and contains bit
  229. * fields across byte boundaries.
  230. */
  231. static bool s390_cpumsf_basic_show(const char *color, size_t pos,
  232. struct hws_basic_entry *basicp)
  233. {
  234. struct hws_basic_entry *basic = basicp;
  235. #if __BYTE_ORDER == __LITTLE_ENDIAN
  236. struct hws_basic_entry local;
  237. unsigned long long word = be64toh(*(unsigned long long *)basicp);
  238. memset(&local, 0, sizeof(local));
  239. local.def = be16toh(basicp->def);
  240. local.prim_asn = word & 0xffff;
  241. local.CL = word >> 30 & 0x3;
  242. local.I = word >> 32 & 0x1;
  243. local.AS = word >> 33 & 0x3;
  244. local.P = word >> 35 & 0x1;
  245. local.W = word >> 36 & 0x1;
  246. local.T = word >> 37 & 0x1;
  247. local.U = word >> 40 & 0xf;
  248. local.ia = be64toh(basicp->ia);
  249. local.gpp = be64toh(basicp->gpp);
  250. local.hpp = be64toh(basicp->hpp);
  251. basic = &local;
  252. #endif
  253. if (basic->def != 1) {
  254. pr_err("Invalid AUX trace basic entry [%#08zx]\n", pos);
  255. return false;
  256. }
  257. color_fprintf(stdout, color, " [%#08zx] Basic Def:%04x Inst:%#04x"
  258. " %c%c%c%c AS:%d ASN:%#04x IA:%#018llx\n"
  259. "\t\tCL:%d HPP:%#018llx GPP:%#018llx\n",
  260. pos, basic->def, basic->U,
  261. basic->T ? 'T' : ' ',
  262. basic->W ? 'W' : ' ',
  263. basic->P ? 'P' : ' ',
  264. basic->I ? 'I' : ' ',
  265. basic->AS, basic->prim_asn, basic->ia, basic->CL,
  266. basic->hpp, basic->gpp);
  267. return true;
  268. }
  269. /* Display s390 CPU measurement facility diagnostic-sampling data entry.
  270. * Data written on s390 in big endian byte order and contains bit
  271. * fields across byte boundaries.
  272. */
  273. static bool s390_cpumsf_diag_show(const char *color, size_t pos,
  274. struct hws_diag_entry *diagp)
  275. {
  276. struct hws_diag_entry *diag = diagp;
  277. #if __BYTE_ORDER == __LITTLE_ENDIAN
  278. struct hws_diag_entry local;
  279. unsigned long long word = be64toh(*(unsigned long long *)diagp);
  280. local.def = be16toh(diagp->def);
  281. local.I = word >> 32 & 0x1;
  282. diag = &local;
  283. #endif
  284. if (diag->def < S390_CPUMSF_DIAG_DEF_FIRST) {
  285. pr_err("Invalid AUX trace diagnostic entry [%#08zx]\n", pos);
  286. return false;
  287. }
  288. color_fprintf(stdout, color, " [%#08zx] Diag Def:%04x %c\n",
  289. pos, diag->def, diag->I ? 'I' : ' ');
  290. return true;
  291. }
  292. /* Return TOD timestamp contained in an trailer entry */
  293. static unsigned long long trailer_timestamp(struct hws_trailer_entry *te,
  294. int idx)
  295. {
  296. /* te->t set: TOD in STCKE format, bytes 8-15
  297. * to->t not set: TOD in STCK format, bytes 0-7
  298. */
  299. unsigned long long ts;
  300. memcpy(&ts, &te->timestamp[idx], sizeof(ts));
  301. return be64toh(ts);
  302. }
  303. /* Display s390 CPU measurement facility trailer entry */
  304. static bool s390_cpumsf_trailer_show(const char *color, size_t pos,
  305. struct hws_trailer_entry *te)
  306. {
  307. #if __BYTE_ORDER == __LITTLE_ENDIAN
  308. struct hws_trailer_entry local;
  309. const unsigned long long flags = be64toh(te->flags);
  310. memset(&local, 0, sizeof(local));
  311. local.f = flags >> 63 & 0x1;
  312. local.a = flags >> 62 & 0x1;
  313. local.t = flags >> 61 & 0x1;
  314. local.bsdes = be16toh((flags >> 16 & 0xffff));
  315. local.dsdes = be16toh((flags & 0xffff));
  316. memcpy(&local.timestamp, te->timestamp, sizeof(te->timestamp));
  317. local.overflow = be64toh(te->overflow);
  318. local.clock_base = be64toh(te->progusage[0]) >> 63 & 1;
  319. local.progusage2 = be64toh(te->progusage2);
  320. te = &local;
  321. #endif
  322. if (te->bsdes != sizeof(struct hws_basic_entry)) {
  323. pr_err("Invalid AUX trace trailer entry [%#08zx]\n", pos);
  324. return false;
  325. }
  326. color_fprintf(stdout, color, " [%#08zx] Trailer %c%c%c bsdes:%d"
  327. " dsdes:%d Overflow:%lld Time:%#llx\n"
  328. "\t\tC:%d TOD:%#lx\n",
  329. pos,
  330. te->f ? 'F' : ' ',
  331. te->a ? 'A' : ' ',
  332. te->t ? 'T' : ' ',
  333. te->bsdes, te->dsdes, te->overflow,
  334. trailer_timestamp(te, te->clock_base),
  335. te->clock_base, te->progusage2);
  336. return true;
  337. }
  338. /* Test a sample data block. It must be 4KB or a multiple thereof in size and
  339. * 4KB page aligned. Each sample data page has a trailer entry at the
  340. * end which contains the sample entry data sizes.
  341. *
  342. * Return true if the sample data block passes the checks and set the
  343. * basic set entry size and diagnostic set entry size.
  344. *
  345. * Return false on failure.
  346. *
  347. * Note: Old hardware does not set the basic or diagnostic entry sizes
  348. * in the trailer entry. Use the type number instead.
  349. */
  350. static bool s390_cpumsf_validate(int machine_type,
  351. unsigned char *buf, size_t len,
  352. unsigned short *bsdes,
  353. unsigned short *dsdes)
  354. {
  355. struct hws_basic_entry *basic = (struct hws_basic_entry *)buf;
  356. struct hws_trailer_entry *te;
  357. *dsdes = *bsdes = 0;
  358. if (len & (S390_CPUMSF_PAGESZ - 1)) /* Illegal size */
  359. return false;
  360. if (be16toh(basic->def) != 1) /* No basic set entry, must be first */
  361. return false;
  362. /* Check for trailer entry at end of SDB */
  363. te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
  364. - sizeof(*te));
  365. *bsdes = be16toh(te->bsdes);
  366. *dsdes = be16toh(te->dsdes);
  367. if (!te->bsdes && !te->dsdes) {
  368. /* Very old hardware, use CPUID */
  369. switch (machine_type) {
  370. case 2097:
  371. case 2098:
  372. *dsdes = 64;
  373. *bsdes = 32;
  374. break;
  375. case 2817:
  376. case 2818:
  377. *dsdes = 74;
  378. *bsdes = 32;
  379. break;
  380. case 2827:
  381. case 2828:
  382. *dsdes = 85;
  383. *bsdes = 32;
  384. break;
  385. case 2964:
  386. case 2965:
  387. *dsdes = 112;
  388. *bsdes = 32;
  389. break;
  390. default:
  391. /* Illegal trailer entry */
  392. return false;
  393. }
  394. }
  395. return true;
  396. }
  397. /* Return true if there is room for another entry */
  398. static bool s390_cpumsf_reached_trailer(size_t entry_sz, size_t pos)
  399. {
  400. size_t payload = S390_CPUMSF_PAGESZ - sizeof(struct hws_trailer_entry);
  401. if (payload - (pos & (S390_CPUMSF_PAGESZ - 1)) < entry_sz)
  402. return false;
  403. return true;
  404. }
  405. /* Dump an auxiliary buffer. These buffers are multiple of
  406. * 4KB SDB pages.
  407. */
  408. static void s390_cpumsf_dump(struct s390_cpumsf *sf,
  409. unsigned char *buf, size_t len)
  410. {
  411. const char *color = PERF_COLOR_BLUE;
  412. struct hws_basic_entry *basic;
  413. struct hws_diag_entry *diag;
  414. unsigned short bsdes, dsdes;
  415. size_t pos = 0;
  416. color_fprintf(stdout, color,
  417. ". ... s390 AUX data: size %zu bytes\n",
  418. len);
  419. if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
  420. &dsdes)) {
  421. pr_err("Invalid AUX trace data block size:%zu"
  422. " (type:%d bsdes:%hd dsdes:%hd)\n",
  423. len, sf->machine_type, bsdes, dsdes);
  424. return;
  425. }
  426. /* s390 kernel always returns 4KB blocks fully occupied,
  427. * no partially filled SDBs.
  428. */
  429. while (pos < len) {
  430. /* Handle Basic entry */
  431. basic = (struct hws_basic_entry *)(buf + pos);
  432. if (s390_cpumsf_basic_show(color, pos, basic))
  433. pos += bsdes;
  434. else
  435. return;
  436. /* Handle Diagnostic entry */
  437. diag = (struct hws_diag_entry *)(buf + pos);
  438. if (s390_cpumsf_diag_show(color, pos, diag))
  439. pos += dsdes;
  440. else
  441. return;
  442. /* Check for trailer entry */
  443. if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
  444. /* Show trailer entry */
  445. struct hws_trailer_entry te;
  446. pos = (pos + S390_CPUMSF_PAGESZ)
  447. & ~(S390_CPUMSF_PAGESZ - 1);
  448. pos -= sizeof(te);
  449. memcpy(&te, buf + pos, sizeof(te));
  450. /* Set descriptor sizes in case of old hardware
  451. * where these values are not set.
  452. */
  453. te.bsdes = bsdes;
  454. te.dsdes = dsdes;
  455. if (s390_cpumsf_trailer_show(color, pos, &te))
  456. pos += sizeof(te);
  457. else
  458. return;
  459. }
  460. }
  461. }
  462. static void s390_cpumsf_dump_event(struct s390_cpumsf *sf, unsigned char *buf,
  463. size_t len)
  464. {
  465. printf(".\n");
  466. s390_cpumsf_dump(sf, buf, len);
  467. }
  468. #define S390_LPP_PID_MASK 0xffffffff
  469. static bool s390_cpumsf_make_event(size_t pos,
  470. struct hws_basic_entry *basic,
  471. struct s390_cpumsf_queue *sfq)
  472. {
  473. struct perf_sample sample = {
  474. .ip = basic->ia,
  475. .pid = basic->hpp & S390_LPP_PID_MASK,
  476. .tid = basic->hpp & S390_LPP_PID_MASK,
  477. .cpumode = PERF_RECORD_MISC_CPUMODE_UNKNOWN,
  478. .cpu = sfq->cpu,
  479. .period = 1
  480. };
  481. union perf_event event;
  482. memset(&event, 0, sizeof(event));
  483. if (basic->CL == 1) /* Native LPAR mode */
  484. sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
  485. : PERF_RECORD_MISC_KERNEL;
  486. else if (basic->CL == 2) /* Guest kernel/user space */
  487. sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
  488. : PERF_RECORD_MISC_GUEST_KERNEL;
  489. else if (basic->gpp || basic->prim_asn != 0xffff)
  490. /* Use heuristics on old hardware */
  491. sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
  492. : PERF_RECORD_MISC_GUEST_KERNEL;
  493. else
  494. sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
  495. : PERF_RECORD_MISC_KERNEL;
  496. event.sample.header.type = PERF_RECORD_SAMPLE;
  497. event.sample.header.misc = sample.cpumode;
  498. event.sample.header.size = sizeof(struct perf_event_header);
  499. pr_debug4("%s pos:%#zx ip:%#" PRIx64 " P:%d CL:%d pid:%d.%d cpumode:%d cpu:%d\n",
  500. __func__, pos, sample.ip, basic->P, basic->CL, sample.pid,
  501. sample.tid, sample.cpumode, sample.cpu);
  502. if (perf_session__deliver_synth_event(sfq->sf->session, &event,
  503. &sample)) {
  504. pr_err("s390 Auxiliary Trace: failed to deliver event\n");
  505. return false;
  506. }
  507. return true;
  508. }
  509. static unsigned long long get_trailer_time(const unsigned char *buf)
  510. {
  511. struct hws_trailer_entry *te;
  512. unsigned long long aux_time, progusage2;
  513. bool clock_base;
  514. te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
  515. - sizeof(*te));
  516. #if __BYTE_ORDER == __LITTLE_ENDIAN
  517. clock_base = be64toh(te->progusage[0]) >> 63 & 0x1;
  518. progusage2 = be64toh(te->progusage[1]);
  519. #else
  520. clock_base = te->clock_base;
  521. progusage2 = te->progusage2;
  522. #endif
  523. if (!clock_base) /* TOD_CLOCK_BASE value missing */
  524. return 0;
  525. /* Correct calculation to convert time stamp in trailer entry to
  526. * nano seconds (taken from arch/s390 function tod_to_ns()).
  527. * TOD_CLOCK_BASE is stored in trailer entry member progusage2.
  528. */
  529. aux_time = trailer_timestamp(te, clock_base) - progusage2;
  530. aux_time = (aux_time >> 9) * 125 + (((aux_time & 0x1ff) * 125) >> 9);
  531. return aux_time;
  532. }
  533. /* Process the data samples of a single queue. The first parameter is a
  534. * pointer to the queue, the second parameter is the time stamp. This
  535. * is the time stamp:
  536. * - of the event that triggered this processing.
  537. * - or the time stamp when the last proccesing of this queue stopped.
  538. * In this case it stopped at a 4KB page boundary and record the
  539. * position on where to continue processing on the next invocation
  540. * (see buffer->use_data and buffer->use_size).
  541. *
  542. * When this function returns the second parameter is updated to
  543. * reflect the time stamp of the last processed auxiliary data entry
  544. * (taken from the trailer entry of that page). The caller uses this
  545. * returned time stamp to record the last processed entry in this
  546. * queue.
  547. *
  548. * The function returns:
  549. * 0: Processing successful. The second parameter returns the
  550. * time stamp from the trailer entry until which position
  551. * processing took place. Subsequent calls resume from this
  552. * position.
  553. * <0: An error occurred during processing. The second parameter
  554. * returns the maximum time stamp.
  555. * >0: Done on this queue. The second parameter returns the
  556. * maximum time stamp.
  557. */
  558. static int s390_cpumsf_samples(struct s390_cpumsf_queue *sfq, u64 *ts)
  559. {
  560. struct s390_cpumsf *sf = sfq->sf;
  561. unsigned char *buf = sfq->buffer->use_data;
  562. size_t len = sfq->buffer->use_size;
  563. struct hws_basic_entry *basic;
  564. unsigned short bsdes, dsdes;
  565. size_t pos = 0;
  566. int err = 1;
  567. u64 aux_ts;
  568. if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
  569. &dsdes)) {
  570. *ts = ~0ULL;
  571. return -1;
  572. }
  573. /* Get trailer entry time stamp and check if entries in
  574. * this auxiliary page are ready for processing. If the
  575. * time stamp of the first entry is too high, whole buffer
  576. * can be skipped. In this case return time stamp.
  577. */
  578. aux_ts = get_trailer_time(buf);
  579. if (!aux_ts) {
  580. pr_err("[%#08" PRIx64 "] Invalid AUX trailer entry TOD clock base\n",
  581. (s64)sfq->buffer->data_offset);
  582. aux_ts = ~0ULL;
  583. goto out;
  584. }
  585. if (aux_ts > *ts) {
  586. *ts = aux_ts;
  587. return 0;
  588. }
  589. while (pos < len) {
  590. /* Handle Basic entry */
  591. basic = (struct hws_basic_entry *)(buf + pos);
  592. if (s390_cpumsf_make_event(pos, basic, sfq))
  593. pos += bsdes;
  594. else {
  595. err = -EBADF;
  596. goto out;
  597. }
  598. pos += dsdes; /* Skip diagnositic entry */
  599. /* Check for trailer entry */
  600. if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
  601. pos = (pos + S390_CPUMSF_PAGESZ)
  602. & ~(S390_CPUMSF_PAGESZ - 1);
  603. /* Check existence of next page */
  604. if (pos >= len)
  605. break;
  606. aux_ts = get_trailer_time(buf + pos);
  607. if (!aux_ts) {
  608. aux_ts = ~0ULL;
  609. goto out;
  610. }
  611. if (aux_ts > *ts) {
  612. *ts = aux_ts;
  613. sfq->buffer->use_data += pos;
  614. sfq->buffer->use_size -= pos;
  615. return 0;
  616. }
  617. }
  618. }
  619. out:
  620. *ts = aux_ts;
  621. sfq->buffer->use_size = 0;
  622. sfq->buffer->use_data = NULL;
  623. return err; /* Buffer completely scanned or error */
  624. }
  625. /* Run the s390 auxiliary trace decoder.
  626. * Select the queue buffer to operate on, the caller already selected
  627. * the proper queue, depending on second parameter 'ts'.
  628. * This is the time stamp until which the auxiliary entries should
  629. * be processed. This value is updated by called functions and
  630. * returned to the caller.
  631. *
  632. * Resume processing in the current buffer. If there is no buffer
  633. * get a new buffer from the queue and setup start position for
  634. * processing.
  635. * When a buffer is completely processed remove it from the queue
  636. * before returning.
  637. *
  638. * This function returns
  639. * 1: When the queue is empty. Second parameter will be set to
  640. * maximum time stamp.
  641. * 0: Normal processing done.
  642. * <0: Error during queue buffer setup. This causes the caller
  643. * to stop processing completely.
  644. */
  645. static int s390_cpumsf_run_decoder(struct s390_cpumsf_queue *sfq,
  646. u64 *ts)
  647. {
  648. struct auxtrace_buffer *buffer;
  649. struct auxtrace_queue *queue;
  650. int err;
  651. queue = &sfq->sf->queues.queue_array[sfq->queue_nr];
  652. /* Get buffer and last position in buffer to resume
  653. * decoding the auxiliary entries. One buffer might be large
  654. * and decoding might stop in between. This depends on the time
  655. * stamp of the trailer entry in each page of the auxiliary
  656. * data and the time stamp of the event triggering the decoding.
  657. */
  658. if (sfq->buffer == NULL) {
  659. sfq->buffer = buffer = auxtrace_buffer__next(queue,
  660. sfq->buffer);
  661. if (!buffer) {
  662. *ts = ~0ULL;
  663. return 1; /* Processing done on this queue */
  664. }
  665. /* Start with a new buffer on this queue */
  666. if (buffer->data) {
  667. buffer->use_size = buffer->size;
  668. buffer->use_data = buffer->data;
  669. }
  670. if (sfq->logfile) { /* Write into log file */
  671. size_t rc = fwrite(buffer->data, buffer->size, 1,
  672. sfq->logfile);
  673. if (rc != 1)
  674. pr_err("Failed to write auxiliary data\n");
  675. }
  676. } else
  677. buffer = sfq->buffer;
  678. if (!buffer->data) {
  679. int fd = perf_data__fd(sfq->sf->session->data);
  680. buffer->data = auxtrace_buffer__get_data(buffer, fd);
  681. if (!buffer->data)
  682. return -ENOMEM;
  683. buffer->use_size = buffer->size;
  684. buffer->use_data = buffer->data;
  685. if (sfq->logfile) { /* Write into log file */
  686. size_t rc = fwrite(buffer->data, buffer->size, 1,
  687. sfq->logfile);
  688. if (rc != 1)
  689. pr_err("Failed to write auxiliary data\n");
  690. }
  691. }
  692. pr_debug4("%s queue_nr:%d buffer:%" PRId64 " offset:%#" PRIx64 " size:%#zx rest:%#zx\n",
  693. __func__, sfq->queue_nr, buffer->buffer_nr, buffer->offset,
  694. buffer->size, buffer->use_size);
  695. err = s390_cpumsf_samples(sfq, ts);
  696. /* If non-zero, there is either an error (err < 0) or the buffer is
  697. * completely done (err > 0). The error is unrecoverable, usually
  698. * some descriptors could not be read successfully, so continue with
  699. * the next buffer.
  700. * In both cases the parameter 'ts' has been updated.
  701. */
  702. if (err) {
  703. sfq->buffer = NULL;
  704. list_del_init(&buffer->list);
  705. auxtrace_buffer__free(buffer);
  706. if (err > 0) /* Buffer done, no error */
  707. err = 0;
  708. }
  709. return err;
  710. }
  711. static struct s390_cpumsf_queue *
  712. s390_cpumsf_alloc_queue(struct s390_cpumsf *sf, unsigned int queue_nr)
  713. {
  714. struct s390_cpumsf_queue *sfq;
  715. sfq = zalloc(sizeof(struct s390_cpumsf_queue));
  716. if (sfq == NULL)
  717. return NULL;
  718. sfq->sf = sf;
  719. sfq->queue_nr = queue_nr;
  720. sfq->cpu = -1;
  721. if (sf->use_logfile) {
  722. char *name;
  723. int rc;
  724. rc = (sf->logdir)
  725. ? asprintf(&name, "%s/aux.smp.%02x",
  726. sf->logdir, queue_nr)
  727. : asprintf(&name, "aux.smp.%02x", queue_nr);
  728. if (rc > 0)
  729. sfq->logfile = fopen(name, "w");
  730. if (sfq->logfile == NULL) {
  731. pr_err("Failed to open auxiliary log file %s,"
  732. "continue...\n", name);
  733. sf->use_logfile = false;
  734. }
  735. free(name);
  736. }
  737. return sfq;
  738. }
  739. static int s390_cpumsf_setup_queue(struct s390_cpumsf *sf,
  740. struct auxtrace_queue *queue,
  741. unsigned int queue_nr, u64 ts)
  742. {
  743. struct s390_cpumsf_queue *sfq = queue->priv;
  744. if (list_empty(&queue->head))
  745. return 0;
  746. if (sfq == NULL) {
  747. sfq = s390_cpumsf_alloc_queue(sf, queue_nr);
  748. if (!sfq)
  749. return -ENOMEM;
  750. queue->priv = sfq;
  751. if (queue->cpu != -1)
  752. sfq->cpu = queue->cpu;
  753. }
  754. return auxtrace_heap__add(&sf->heap, queue_nr, ts);
  755. }
  756. static int s390_cpumsf_setup_queues(struct s390_cpumsf *sf, u64 ts)
  757. {
  758. unsigned int i;
  759. int ret = 0;
  760. for (i = 0; i < sf->queues.nr_queues; i++) {
  761. ret = s390_cpumsf_setup_queue(sf, &sf->queues.queue_array[i],
  762. i, ts);
  763. if (ret)
  764. break;
  765. }
  766. return ret;
  767. }
  768. static int s390_cpumsf_update_queues(struct s390_cpumsf *sf, u64 ts)
  769. {
  770. if (!sf->queues.new_data)
  771. return 0;
  772. sf->queues.new_data = false;
  773. return s390_cpumsf_setup_queues(sf, ts);
  774. }
  775. static int s390_cpumsf_process_queues(struct s390_cpumsf *sf, u64 timestamp)
  776. {
  777. unsigned int queue_nr;
  778. u64 ts;
  779. int ret;
  780. while (1) {
  781. struct auxtrace_queue *queue;
  782. struct s390_cpumsf_queue *sfq;
  783. if (!sf->heap.heap_cnt)
  784. return 0;
  785. if (sf->heap.heap_array[0].ordinal >= timestamp)
  786. return 0;
  787. queue_nr = sf->heap.heap_array[0].queue_nr;
  788. queue = &sf->queues.queue_array[queue_nr];
  789. sfq = queue->priv;
  790. auxtrace_heap__pop(&sf->heap);
  791. if (sf->heap.heap_cnt) {
  792. ts = sf->heap.heap_array[0].ordinal + 1;
  793. if (ts > timestamp)
  794. ts = timestamp;
  795. } else {
  796. ts = timestamp;
  797. }
  798. ret = s390_cpumsf_run_decoder(sfq, &ts);
  799. if (ret < 0) {
  800. auxtrace_heap__add(&sf->heap, queue_nr, ts);
  801. return ret;
  802. }
  803. if (!ret) {
  804. ret = auxtrace_heap__add(&sf->heap, queue_nr, ts);
  805. if (ret < 0)
  806. return ret;
  807. }
  808. }
  809. return 0;
  810. }
  811. static int s390_cpumsf_synth_error(struct s390_cpumsf *sf, int code, int cpu,
  812. pid_t pid, pid_t tid, u64 ip, u64 timestamp)
  813. {
  814. char msg[MAX_AUXTRACE_ERROR_MSG];
  815. union perf_event event;
  816. int err;
  817. strncpy(msg, "Lost Auxiliary Trace Buffer", sizeof(msg) - 1);
  818. auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
  819. code, cpu, pid, tid, ip, msg, timestamp);
  820. err = perf_session__deliver_synth_event(sf->session, &event, NULL);
  821. if (err)
  822. pr_err("s390 Auxiliary Trace: failed to deliver error event,"
  823. "error %d\n", err);
  824. return err;
  825. }
  826. static int s390_cpumsf_lost(struct s390_cpumsf *sf, struct perf_sample *sample)
  827. {
  828. return s390_cpumsf_synth_error(sf, 1, sample->cpu,
  829. sample->pid, sample->tid, 0,
  830. sample->time);
  831. }
  832. static int
  833. s390_cpumsf_process_event(struct perf_session *session,
  834. union perf_event *event,
  835. struct perf_sample *sample,
  836. struct perf_tool *tool)
  837. {
  838. struct s390_cpumsf *sf = container_of(session->auxtrace,
  839. struct s390_cpumsf,
  840. auxtrace);
  841. u64 timestamp = sample->time;
  842. struct evsel *ev_bc000;
  843. int err = 0;
  844. if (dump_trace)
  845. return 0;
  846. if (!tool->ordered_events) {
  847. pr_err("s390 Auxiliary Trace requires ordered events\n");
  848. return -EINVAL;
  849. }
  850. if (event->header.type == PERF_RECORD_SAMPLE &&
  851. sample->raw_size) {
  852. /* Handle event with raw data */
  853. ev_bc000 = perf_evlist__event2evsel(session->evlist, event);
  854. if (ev_bc000 &&
  855. ev_bc000->core.attr.config == PERF_EVENT_CPUM_CF_DIAG)
  856. err = s390_cpumcf_dumpctr(sf, sample);
  857. return err;
  858. }
  859. if (event->header.type == PERF_RECORD_AUX &&
  860. event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
  861. return s390_cpumsf_lost(sf, sample);
  862. if (timestamp) {
  863. err = s390_cpumsf_update_queues(sf, timestamp);
  864. if (!err)
  865. err = s390_cpumsf_process_queues(sf, timestamp);
  866. }
  867. return err;
  868. }
  869. struct s390_cpumsf_synth {
  870. struct perf_tool cpumsf_tool;
  871. struct perf_session *session;
  872. };
  873. static int
  874. s390_cpumsf_process_auxtrace_event(struct perf_session *session,
  875. union perf_event *event __maybe_unused,
  876. struct perf_tool *tool __maybe_unused)
  877. {
  878. struct s390_cpumsf *sf = container_of(session->auxtrace,
  879. struct s390_cpumsf,
  880. auxtrace);
  881. int fd = perf_data__fd(session->data);
  882. struct auxtrace_buffer *buffer;
  883. off_t data_offset;
  884. int err;
  885. if (sf->data_queued)
  886. return 0;
  887. if (perf_data__is_pipe(session->data)) {
  888. data_offset = 0;
  889. } else {
  890. data_offset = lseek(fd, 0, SEEK_CUR);
  891. if (data_offset == -1)
  892. return -errno;
  893. }
  894. err = auxtrace_queues__add_event(&sf->queues, session, event,
  895. data_offset, &buffer);
  896. if (err)
  897. return err;
  898. /* Dump here after copying piped trace out of the pipe */
  899. if (dump_trace) {
  900. if (auxtrace_buffer__get_data(buffer, fd)) {
  901. s390_cpumsf_dump_event(sf, buffer->data,
  902. buffer->size);
  903. auxtrace_buffer__put_data(buffer);
  904. }
  905. }
  906. return 0;
  907. }
  908. static void s390_cpumsf_free_events(struct perf_session *session __maybe_unused)
  909. {
  910. }
  911. static int s390_cpumsf_flush(struct perf_session *session __maybe_unused,
  912. struct perf_tool *tool __maybe_unused)
  913. {
  914. return 0;
  915. }
  916. static void s390_cpumsf_free_queues(struct perf_session *session)
  917. {
  918. struct s390_cpumsf *sf = container_of(session->auxtrace,
  919. struct s390_cpumsf,
  920. auxtrace);
  921. struct auxtrace_queues *queues = &sf->queues;
  922. unsigned int i;
  923. for (i = 0; i < queues->nr_queues; i++) {
  924. struct s390_cpumsf_queue *sfq = (struct s390_cpumsf_queue *)
  925. queues->queue_array[i].priv;
  926. if (sfq != NULL) {
  927. if (sfq->logfile) {
  928. fclose(sfq->logfile);
  929. sfq->logfile = NULL;
  930. }
  931. if (sfq->logfile_ctr) {
  932. fclose(sfq->logfile_ctr);
  933. sfq->logfile_ctr = NULL;
  934. }
  935. }
  936. zfree(&queues->queue_array[i].priv);
  937. }
  938. auxtrace_queues__free(queues);
  939. }
  940. static void s390_cpumsf_free(struct perf_session *session)
  941. {
  942. struct s390_cpumsf *sf = container_of(session->auxtrace,
  943. struct s390_cpumsf,
  944. auxtrace);
  945. auxtrace_heap__free(&sf->heap);
  946. s390_cpumsf_free_queues(session);
  947. session->auxtrace = NULL;
  948. zfree(&sf->logdir);
  949. free(sf);
  950. }
  951. static bool
  952. s390_cpumsf_evsel_is_auxtrace(struct perf_session *session __maybe_unused,
  953. struct evsel *evsel)
  954. {
  955. return evsel->core.attr.type == PERF_TYPE_RAW &&
  956. evsel->core.attr.config == PERF_EVENT_CPUM_SF_DIAG;
  957. }
  958. static int s390_cpumsf_get_type(const char *cpuid)
  959. {
  960. int ret, family = 0;
  961. ret = sscanf(cpuid, "%*[^,],%u", &family);
  962. return (ret == 1) ? family : 0;
  963. }
  964. /* Check itrace options set on perf report command.
  965. * Return true, if none are set or all options specified can be
  966. * handled on s390 (currently only option 'd' for logging.
  967. * Return false otherwise.
  968. */
  969. static bool check_auxtrace_itrace(struct itrace_synth_opts *itops)
  970. {
  971. bool ison = false;
  972. if (!itops || !itops->set)
  973. return true;
  974. ison = itops->inject || itops->instructions || itops->branches ||
  975. itops->transactions || itops->ptwrites ||
  976. itops->pwr_events || itops->errors ||
  977. itops->dont_decode || itops->calls || itops->returns ||
  978. itops->callchain || itops->thread_stack ||
  979. itops->last_branch || itops->add_callchain ||
  980. itops->add_last_branch;
  981. if (!ison)
  982. return true;
  983. pr_err("Unsupported --itrace options specified\n");
  984. return false;
  985. }
  986. /* Check for AUXTRACE dump directory if it is needed.
  987. * On failure print an error message but continue.
  988. * Return 0 on wrong keyword in config file and 1 otherwise.
  989. */
  990. static int s390_cpumsf__config(const char *var, const char *value, void *cb)
  991. {
  992. struct s390_cpumsf *sf = cb;
  993. struct stat stbuf;
  994. int rc;
  995. if (strcmp(var, "auxtrace.dumpdir"))
  996. return 0;
  997. sf->logdir = strdup(value);
  998. if (sf->logdir == NULL) {
  999. pr_err("Failed to find auxtrace log directory %s,"
  1000. " continue with current directory...\n", value);
  1001. return 1;
  1002. }
  1003. rc = stat(sf->logdir, &stbuf);
  1004. if (rc == -1 || !S_ISDIR(stbuf.st_mode)) {
  1005. pr_err("Missing auxtrace log directory %s,"
  1006. " continue with current directory...\n", value);
  1007. zfree(&sf->logdir);
  1008. }
  1009. return 1;
  1010. }
  1011. int s390_cpumsf_process_auxtrace_info(union perf_event *event,
  1012. struct perf_session *session)
  1013. {
  1014. struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
  1015. struct s390_cpumsf *sf;
  1016. int err;
  1017. if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info))
  1018. return -EINVAL;
  1019. sf = zalloc(sizeof(struct s390_cpumsf));
  1020. if (sf == NULL)
  1021. return -ENOMEM;
  1022. if (!check_auxtrace_itrace(session->itrace_synth_opts)) {
  1023. err = -EINVAL;
  1024. goto err_free;
  1025. }
  1026. sf->use_logfile = session->itrace_synth_opts->log;
  1027. if (sf->use_logfile)
  1028. perf_config(s390_cpumsf__config, sf);
  1029. err = auxtrace_queues__init(&sf->queues);
  1030. if (err)
  1031. goto err_free;
  1032. sf->session = session;
  1033. sf->machine = &session->machines.host; /* No kvm support */
  1034. sf->auxtrace_type = auxtrace_info->type;
  1035. sf->pmu_type = PERF_TYPE_RAW;
  1036. sf->machine_type = s390_cpumsf_get_type(session->evlist->env->cpuid);
  1037. sf->auxtrace.process_event = s390_cpumsf_process_event;
  1038. sf->auxtrace.process_auxtrace_event = s390_cpumsf_process_auxtrace_event;
  1039. sf->auxtrace.flush_events = s390_cpumsf_flush;
  1040. sf->auxtrace.free_events = s390_cpumsf_free_events;
  1041. sf->auxtrace.free = s390_cpumsf_free;
  1042. sf->auxtrace.evsel_is_auxtrace = s390_cpumsf_evsel_is_auxtrace;
  1043. session->auxtrace = &sf->auxtrace;
  1044. if (dump_trace)
  1045. return 0;
  1046. err = auxtrace_queues__process_index(&sf->queues, session);
  1047. if (err)
  1048. goto err_free_queues;
  1049. if (sf->queues.populated)
  1050. sf->data_queued = true;
  1051. return 0;
  1052. err_free_queues:
  1053. auxtrace_queues__free(&sf->queues);
  1054. session->auxtrace = NULL;
  1055. err_free:
  1056. zfree(&sf->logdir);
  1057. free(sf);
  1058. return err;
  1059. }