mem-events.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <api/fs/fs.h>
  10. #include <linux/kernel.h>
  11. #include "mem-events.h"
  12. #include "debug.h"
  13. #include "symbol.h"
  14. #include "sort.h"
  15. unsigned int perf_mem_events__loads_ldlat = 30;
  16. #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
  17. struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
  18. E("ldlat-loads", "cpu/mem-loads,ldlat=%u/P", "mem-loads"),
  19. E("ldlat-stores", "cpu/mem-stores/P", "mem-stores"),
  20. };
  21. #undef E
  22. #undef E
  23. static char mem_loads_name[100];
  24. static bool mem_loads_name__init;
  25. char *perf_mem_events__name(int i)
  26. {
  27. if (i == PERF_MEM_EVENTS__LOAD) {
  28. if (!mem_loads_name__init) {
  29. mem_loads_name__init = true;
  30. scnprintf(mem_loads_name, sizeof(mem_loads_name),
  31. perf_mem_events[i].name,
  32. perf_mem_events__loads_ldlat);
  33. }
  34. return mem_loads_name;
  35. }
  36. return (char *)perf_mem_events[i].name;
  37. }
  38. int perf_mem_events__parse(const char *str)
  39. {
  40. char *tok, *saveptr = NULL;
  41. bool found = false;
  42. char *buf;
  43. int j;
  44. /* We need buffer that we know we can write to. */
  45. buf = malloc(strlen(str) + 1);
  46. if (!buf)
  47. return -ENOMEM;
  48. strcpy(buf, str);
  49. tok = strtok_r((char *)buf, ",", &saveptr);
  50. while (tok) {
  51. for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
  52. struct perf_mem_event *e = &perf_mem_events[j];
  53. if (strstr(e->tag, tok))
  54. e->record = found = true;
  55. }
  56. tok = strtok_r(NULL, ",", &saveptr);
  57. }
  58. free(buf);
  59. if (found)
  60. return 0;
  61. pr_err("failed: event '%s' not found, use '-e list' to get list of available events\n", str);
  62. return -1;
  63. }
  64. int perf_mem_events__init(void)
  65. {
  66. const char *mnt = sysfs__mount();
  67. bool found = false;
  68. int j;
  69. if (!mnt)
  70. return -ENOENT;
  71. for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
  72. char path[PATH_MAX];
  73. struct perf_mem_event *e = &perf_mem_events[j];
  74. struct stat st;
  75. scnprintf(path, PATH_MAX, "%s/devices/cpu/events/%s",
  76. mnt, e->sysfs_name);
  77. if (!stat(path, &st))
  78. e->supported = found = true;
  79. }
  80. return found ? 0 : -ENOENT;
  81. }
  82. static const char * const tlb_access[] = {
  83. "N/A",
  84. "HIT",
  85. "MISS",
  86. "L1",
  87. "L2",
  88. "Walker",
  89. "Fault",
  90. };
  91. int perf_mem__tlb_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
  92. {
  93. size_t l = 0, i;
  94. u64 m = PERF_MEM_TLB_NA;
  95. u64 hit, miss;
  96. sz -= 1; /* -1 for null termination */
  97. out[0] = '\0';
  98. if (mem_info)
  99. m = mem_info->data_src.mem_dtlb;
  100. hit = m & PERF_MEM_TLB_HIT;
  101. miss = m & PERF_MEM_TLB_MISS;
  102. /* already taken care of */
  103. m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
  104. for (i = 0; m && i < ARRAY_SIZE(tlb_access); i++, m >>= 1) {
  105. if (!(m & 0x1))
  106. continue;
  107. if (l) {
  108. strcat(out, " or ");
  109. l += 4;
  110. }
  111. l += scnprintf(out + l, sz - l, tlb_access[i]);
  112. }
  113. if (*out == '\0')
  114. l += scnprintf(out, sz - l, "N/A");
  115. if (hit)
  116. l += scnprintf(out + l, sz - l, " hit");
  117. if (miss)
  118. l += scnprintf(out + l, sz - l, " miss");
  119. return l;
  120. }
  121. static const char * const mem_lvl[] = {
  122. "N/A",
  123. "HIT",
  124. "MISS",
  125. "L1",
  126. "LFB",
  127. "L2",
  128. "L3",
  129. "Local RAM",
  130. "Remote RAM (1 hop)",
  131. "Remote RAM (2 hops)",
  132. "Remote Cache (1 hop)",
  133. "Remote Cache (2 hops)",
  134. "I/O",
  135. "Uncached",
  136. };
  137. static const char * const mem_lvlnum[] = {
  138. [PERF_MEM_LVLNUM_ANY_CACHE] = "Any cache",
  139. [PERF_MEM_LVLNUM_LFB] = "LFB",
  140. [PERF_MEM_LVLNUM_RAM] = "RAM",
  141. [PERF_MEM_LVLNUM_PMEM] = "PMEM",
  142. [PERF_MEM_LVLNUM_NA] = "N/A",
  143. };
  144. int perf_mem__lvl_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
  145. {
  146. size_t i, l = 0;
  147. u64 m = PERF_MEM_LVL_NA;
  148. u64 hit, miss;
  149. int printed;
  150. if (mem_info)
  151. m = mem_info->data_src.mem_lvl;
  152. sz -= 1; /* -1 for null termination */
  153. out[0] = '\0';
  154. hit = m & PERF_MEM_LVL_HIT;
  155. miss = m & PERF_MEM_LVL_MISS;
  156. /* already taken care of */
  157. m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS);
  158. if (mem_info && mem_info->data_src.mem_remote) {
  159. strcat(out, "Remote ");
  160. l += 7;
  161. }
  162. printed = 0;
  163. for (i = 0; m && i < ARRAY_SIZE(mem_lvl); i++, m >>= 1) {
  164. if (!(m & 0x1))
  165. continue;
  166. if (printed++) {
  167. strcat(out, " or ");
  168. l += 4;
  169. }
  170. l += scnprintf(out + l, sz - l, mem_lvl[i]);
  171. }
  172. if (mem_info && mem_info->data_src.mem_lvl_num) {
  173. int lvl = mem_info->data_src.mem_lvl_num;
  174. if (printed++) {
  175. strcat(out, " or ");
  176. l += 4;
  177. }
  178. if (mem_lvlnum[lvl])
  179. l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]);
  180. else
  181. l += scnprintf(out + l, sz - l, "L%d", lvl);
  182. }
  183. if (l == 0)
  184. l += scnprintf(out + l, sz - l, "N/A");
  185. if (hit)
  186. l += scnprintf(out + l, sz - l, " hit");
  187. if (miss)
  188. l += scnprintf(out + l, sz - l, " miss");
  189. return l;
  190. }
  191. static const char * const snoop_access[] = {
  192. "N/A",
  193. "None",
  194. "Hit",
  195. "Miss",
  196. "HitM",
  197. };
  198. int perf_mem__snp_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
  199. {
  200. size_t i, l = 0;
  201. u64 m = PERF_MEM_SNOOP_NA;
  202. sz -= 1; /* -1 for null termination */
  203. out[0] = '\0';
  204. if (mem_info)
  205. m = mem_info->data_src.mem_snoop;
  206. for (i = 0; m && i < ARRAY_SIZE(snoop_access); i++, m >>= 1) {
  207. if (!(m & 0x1))
  208. continue;
  209. if (l) {
  210. strcat(out, " or ");
  211. l += 4;
  212. }
  213. l += scnprintf(out + l, sz - l, snoop_access[i]);
  214. }
  215. if (mem_info &&
  216. (mem_info->data_src.mem_snoopx & PERF_MEM_SNOOPX_FWD)) {
  217. if (l) {
  218. strcat(out, " or ");
  219. l += 4;
  220. }
  221. l += scnprintf(out + l, sz - l, "Fwd");
  222. }
  223. if (*out == '\0')
  224. l += scnprintf(out, sz - l, "N/A");
  225. return l;
  226. }
  227. int perf_mem__lck_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
  228. {
  229. u64 mask = PERF_MEM_LOCK_NA;
  230. int l;
  231. if (mem_info)
  232. mask = mem_info->data_src.mem_lock;
  233. if (mask & PERF_MEM_LOCK_NA)
  234. l = scnprintf(out, sz, "N/A");
  235. else if (mask & PERF_MEM_LOCK_LOCKED)
  236. l = scnprintf(out, sz, "Yes");
  237. else
  238. l = scnprintf(out, sz, "No");
  239. return l;
  240. }
  241. int perf_script__meminfo_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
  242. {
  243. int i = 0;
  244. i += perf_mem__lvl_scnprintf(out, sz, mem_info);
  245. i += scnprintf(out + i, sz - i, "|SNP ");
  246. i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
  247. i += scnprintf(out + i, sz - i, "|TLB ");
  248. i += perf_mem__tlb_scnprintf(out + i, sz - i, mem_info);
  249. i += scnprintf(out + i, sz - i, "|LCK ");
  250. i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
  251. return i;
  252. }
  253. int c2c_decode_stats(struct c2c_stats *stats, struct mem_info *mi)
  254. {
  255. union perf_mem_data_src *data_src = &mi->data_src;
  256. u64 daddr = mi->daddr.addr;
  257. u64 op = data_src->mem_op;
  258. u64 lvl = data_src->mem_lvl;
  259. u64 snoop = data_src->mem_snoop;
  260. u64 lock = data_src->mem_lock;
  261. /*
  262. * Skylake might report unknown remote level via this
  263. * bit, consider it when evaluating remote HITMs.
  264. */
  265. bool mrem = data_src->mem_remote;
  266. int err = 0;
  267. #define HITM_INC(__f) \
  268. do { \
  269. stats->__f++; \
  270. stats->tot_hitm++; \
  271. } while (0)
  272. #define P(a, b) PERF_MEM_##a##_##b
  273. stats->nr_entries++;
  274. if (lock & P(LOCK, LOCKED)) stats->locks++;
  275. if (op & P(OP, LOAD)) {
  276. /* load */
  277. stats->load++;
  278. if (!daddr) {
  279. stats->ld_noadrs++;
  280. return -1;
  281. }
  282. if (lvl & P(LVL, HIT)) {
  283. if (lvl & P(LVL, UNC)) stats->ld_uncache++;
  284. if (lvl & P(LVL, IO)) stats->ld_io++;
  285. if (lvl & P(LVL, LFB)) stats->ld_fbhit++;
  286. if (lvl & P(LVL, L1 )) stats->ld_l1hit++;
  287. if (lvl & P(LVL, L2 )) stats->ld_l2hit++;
  288. if (lvl & P(LVL, L3 )) {
  289. if (snoop & P(SNOOP, HITM))
  290. HITM_INC(lcl_hitm);
  291. else
  292. stats->ld_llchit++;
  293. }
  294. if (lvl & P(LVL, LOC_RAM)) {
  295. stats->lcl_dram++;
  296. if (snoop & P(SNOOP, HIT))
  297. stats->ld_shared++;
  298. else
  299. stats->ld_excl++;
  300. }
  301. if ((lvl & P(LVL, REM_RAM1)) ||
  302. (lvl & P(LVL, REM_RAM2)) ||
  303. mrem) {
  304. stats->rmt_dram++;
  305. if (snoop & P(SNOOP, HIT))
  306. stats->ld_shared++;
  307. else
  308. stats->ld_excl++;
  309. }
  310. }
  311. if ((lvl & P(LVL, REM_CCE1)) ||
  312. (lvl & P(LVL, REM_CCE2)) ||
  313. mrem) {
  314. if (snoop & P(SNOOP, HIT))
  315. stats->rmt_hit++;
  316. else if (snoop & P(SNOOP, HITM))
  317. HITM_INC(rmt_hitm);
  318. }
  319. if ((lvl & P(LVL, MISS)))
  320. stats->ld_miss++;
  321. } else if (op & P(OP, STORE)) {
  322. /* store */
  323. stats->store++;
  324. if (!daddr) {
  325. stats->st_noadrs++;
  326. return -1;
  327. }
  328. if (lvl & P(LVL, HIT)) {
  329. if (lvl & P(LVL, UNC)) stats->st_uncache++;
  330. if (lvl & P(LVL, L1 )) stats->st_l1hit++;
  331. }
  332. if (lvl & P(LVL, MISS))
  333. if (lvl & P(LVL, L1)) stats->st_l1miss++;
  334. } else {
  335. /* unparsable data_src? */
  336. stats->noparse++;
  337. return -1;
  338. }
  339. if (!mi->daddr.map || !mi->iaddr.map) {
  340. stats->nomap++;
  341. return -1;
  342. }
  343. #undef P
  344. #undef HITM_INC
  345. return err;
  346. }
  347. void c2c_add_stats(struct c2c_stats *stats, struct c2c_stats *add)
  348. {
  349. stats->nr_entries += add->nr_entries;
  350. stats->locks += add->locks;
  351. stats->store += add->store;
  352. stats->st_uncache += add->st_uncache;
  353. stats->st_noadrs += add->st_noadrs;
  354. stats->st_l1hit += add->st_l1hit;
  355. stats->st_l1miss += add->st_l1miss;
  356. stats->load += add->load;
  357. stats->ld_excl += add->ld_excl;
  358. stats->ld_shared += add->ld_shared;
  359. stats->ld_uncache += add->ld_uncache;
  360. stats->ld_io += add->ld_io;
  361. stats->ld_miss += add->ld_miss;
  362. stats->ld_noadrs += add->ld_noadrs;
  363. stats->ld_fbhit += add->ld_fbhit;
  364. stats->ld_l1hit += add->ld_l1hit;
  365. stats->ld_l2hit += add->ld_l2hit;
  366. stats->ld_llchit += add->ld_llchit;
  367. stats->lcl_hitm += add->lcl_hitm;
  368. stats->rmt_hitm += add->rmt_hitm;
  369. stats->tot_hitm += add->tot_hitm;
  370. stats->rmt_hit += add->rmt_hit;
  371. stats->lcl_dram += add->lcl_dram;
  372. stats->rmt_dram += add->rmt_dram;
  373. stats->nomap += add->nomap;
  374. stats->noparse += add->noparse;
  375. }