pmcio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Performance counters non port part
  11. */
  12. #include "u.h"
  13. #include "../port/lib.h"
  14. #include "mem.h"
  15. #include "dat.h"
  16. #include "fns.h"
  17. #include "../port/error.h"
  18. #include "amd64.h"
  19. #include "../port/pmc.h"
  20. /* non portable, for intel will be CPUID.0AH.EDX
  21. */
  22. enum {
  23. PeNreg = 4, /* Number of Pe/Pct regs */
  24. };
  25. int
  26. pmcnregs(void)
  27. {
  28. /* could run CPUID to see if there are registers,
  29. * PmcMaxCtrs
  30. */
  31. return PeNreg;
  32. }
  33. //PeHo|PeGo
  34. #define PeAll (PeOS|PeUsr)
  35. #define SetEvMsk(v, e) ((v)|(((e)&PeEvMskL)|(((e)<<(PeEvMsksh-8))&PeEvMskH)))
  36. #define SetUMsk(v, u) ((v)|(((u)<<8ull)&PeUnMsk))
  37. #define GetEvMsk(e) (((e)&PeEvMskL)|(((e)&PeEvMskH)>>(PeEvMsksh-8)))
  38. #define GetUMsk(u) (((u)&PeUnMsk)>>8ull)
  39. static int
  40. pmcuserenab(int enable)
  41. {
  42. uint64_t cr4;
  43. cr4 = cr4get();
  44. if (enable){
  45. cr4 |= Pce;
  46. } else
  47. cr4 &= ~Pce;
  48. cr4put(cr4);
  49. return cr4&Pce;
  50. }
  51. PmcCtlCtrId pmcids[] = {
  52. {"locked instr", "0x024 0x1"},
  53. {"locked cycles nonspec", "0x024 0x4"}, // cycles
  54. {"SMI intr", "0x02b 0x0"},
  55. {"DC access", "0x040 0x0"},
  56. {"DC miss", "0x041 0x0"},
  57. {"DC refills", "0x042 0x1f"},
  58. {"DC evicted", "0x042 0x3f"},
  59. {"L1 DTLB miss", "0x045 0x7"}, //DTLB L2 hit
  60. {"L2 DTLB miss", "0x046 0x7"},
  61. {"L1 DTLB hit", "0x04d 0x3"},
  62. {"global TLB flush", "0x054 0x0"},
  63. {"L2 hit", "0x07d 0x3f"},
  64. {"L2 miss", "0x07e 0xf"},
  65. {"IC miss", "0x081 0x0"},
  66. {"IC refill from L2", "0x082 0x0"},
  67. {"IC refill from system", "0x083 0x0"},
  68. {"L1 ITLB miss", "0x084 0x0"}, //L2 ITLB hit
  69. {"L2 ITLB miss", "0x085 0x3"},
  70. {"DRAM access", "0x0e0 0x3f"},
  71. {"L3 miss core 0", "0x4e1 0x13"}, //core 0 only
  72. {"L3 miss core 1", "0x4e1 0x23"},
  73. {"L3 miss core 2", "0x4e1 0x43"},
  74. {"L3 miss core 3", "0x4e1 0x83"},
  75. {"L3 miss socket", "0x4e1 0xf3"}, //all cores in the socket
  76. {"", ""},
  77. };
  78. int
  79. pmctrans(PmcCtl *p)
  80. {
  81. PmcCtlCtrId *pi;
  82. for (pi = &pmcids[0]; pi->portdesc[0] != '\0'; pi++){
  83. if ( strncmp(p->descstr, pi->portdesc, strlen(pi->portdesc)) == 0){
  84. strncpy(p->descstr, pi->archdesc, strlen(pi->archdesc) + 1);
  85. return 0;
  86. }
  87. }
  88. return 1;
  89. }
  90. static int
  91. getctl(PmcCtl *p, uint32_t regno)
  92. {
  93. uint64_t r, e, u;
  94. r = rdmsr(regno + PerfEvtbase);
  95. p->enab = (r&PeCtEna) != 0;
  96. p->user = (r&PeUsr) != 0;
  97. p->os = (r&PeOS) != 0;
  98. e = GetEvMsk(r);
  99. u = GetUMsk(r);
  100. //TODO inverse translation
  101. snprint(p->descstr, KNAMELEN, "%#ullx %#ullx", e, u);
  102. p->nodesc = 0;
  103. return 0;
  104. }
  105. int
  106. pmcanyenab(void)
  107. {
  108. int i;
  109. PmcCtl p;
  110. for (i = 0; i < pmcnregs(); i++) {
  111. if (getctl(&p, i) < 0)
  112. return -1;
  113. if (p.enab)
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. extern int pmcdebug;
  119. static int
  120. setctl(PmcCtl *p, int regno)
  121. {
  122. uint64_t v, e, u;
  123. char *toks[2];
  124. char str[KNAMELEN];
  125. if (regno >= pmcnregs())
  126. error("invalid reg");
  127. v = rdmsr(regno + PerfEvtbase);
  128. v &= PeEvMskH|PeEvMskL|PeCtEna|PeOS|PeUsr|PeUnMsk;
  129. if (p->enab != PmcCtlNullval)
  130. if (p->enab)
  131. v |= PeCtEna;
  132. else
  133. v &= ~PeCtEna;
  134. if (p->user != PmcCtlNullval)
  135. if (p->user)
  136. v |= PeUsr;
  137. else
  138. v &= ~PeUsr;
  139. if (p->os != PmcCtlNullval)
  140. if (p->os)
  141. v |= PeOS;
  142. else
  143. v &= ~PeOS;
  144. if (pmctrans(p) < 0)
  145. return -1;
  146. if (p->nodesc == 0) {
  147. memmove(str, p->descstr, KNAMELEN);
  148. if (tokenize(str, toks, 2) != 2)
  149. return -1;
  150. e = atoi(toks[0]);
  151. u = atoi(toks[1]);
  152. v &= ~(PeEvMskL|PeEvMskH|PeUnMsk);
  153. v |= SetEvMsk(v, e);
  154. v |= SetUMsk(v, u);
  155. }
  156. if (p->reset != PmcCtlNullval && p->reset) {
  157. v = 0;
  158. wrmsr(regno+ PerfCtrbase, 0);
  159. p->reset = PmcCtlNullval; /* only reset once */
  160. }
  161. wrmsr(regno+ PerfEvtbase, v);
  162. pmcuserenab(pmcanyenab());
  163. if (pmcdebug) {
  164. v = rdmsr(regno+ PerfEvtbase);
  165. print("conf pmc[%#ux]: %#llux\n", regno, v);
  166. }
  167. return 0;
  168. }
  169. int
  170. pmcctlstr(char *str, int nstr, PmcCtl *p)
  171. {
  172. int ns;
  173. ns = 0;
  174. if (p->enab && p->enab != PmcCtlNullval)
  175. ns += snprint(str + ns, nstr - ns, "enable\n");
  176. else
  177. ns += snprint(str + ns, nstr - ns, "disable\n");
  178. if (p->user && p->user != PmcCtlNullval)
  179. ns += snprint(str + ns, nstr - ns, "user\n");
  180. if (p->os && p->user != PmcCtlNullval)
  181. ns += snprint(str + ns, nstr - ns, "os\n");
  182. //TODO, inverse pmctrans?
  183. if(!p->nodesc)
  184. ns += snprint(str + ns, nstr - ns, "%s\n", p->descstr);
  185. else
  186. ns += snprint(str + ns, nstr - ns, "no desc\n");
  187. return ns;
  188. }
  189. int
  190. pmcdescstr(char *str, int nstr)
  191. {
  192. PmcCtlCtrId *pi;
  193. int ns;
  194. ns = 0;
  195. for (pi = &pmcids[0]; pi->portdesc[0] != '\0'; pi++)
  196. ns += snprint(str + ns, nstr - ns, "%s\n",pi->portdesc);
  197. return ns;
  198. }
  199. static uint64_t
  200. getctr(uint32_t regno)
  201. {
  202. return rdmsr(regno + PerfCtrbase);
  203. }
  204. static int
  205. setctr(uint64_t v, uint32_t regno)
  206. {
  207. wrmsr(regno + PerfCtrbase, v);
  208. return 0;
  209. }
  210. static int
  211. notstale(void *x)
  212. {
  213. PmcCtr *p;
  214. p = (PmcCtr *)x;
  215. return !p->stale;
  216. }
  217. static PmcWait*
  218. newpmcw(void)
  219. {
  220. PmcWait *w;
  221. w = malloc(sizeof (PmcWait));
  222. w->r.ref = 1;
  223. return w;
  224. }
  225. static void
  226. pmcwclose(PmcWait *w)
  227. {
  228. if(decref(&w->r))
  229. return;
  230. free(w);
  231. }
  232. /*
  233. * As it is now, it sends an IPI if the processor is otherwise
  234. * ocuppied for it to update the counter. Probably not needed
  235. * for TC/XC as it will be updated every time we cross the kernel
  236. * boundary, but we are doing it now just in case it is idle or
  237. * not being updated NB: this function releases the ilock
  238. */
  239. static void
  240. waitnotstale(Mach *mp, PmcCtr *p)
  241. {
  242. Proc *up = externup();
  243. PmcWait *w;
  244. p->stale = 1;
  245. w = newpmcw();
  246. w->next = p->wq;
  247. p->wq = w;
  248. incref(&w->r);
  249. iunlock(&mp->pmclock);
  250. apicipi(mp->apicno);
  251. if(waserror()){
  252. pmcwclose(w);
  253. nexterror();
  254. }
  255. sleep(&w->rend, notstale, p);
  256. poperror();
  257. pmcwclose(w);
  258. }
  259. /*
  260. * The reason this is not racy is subtle.
  261. *
  262. * If the processor suddenly changes state to busy once I have
  263. * decided not to IPI it, I don't wait for it.
  264. *
  265. * In the other case, I have decided to IPI it and hence, wait.
  266. * The problem then is that it switches to idle (not
  267. * interruptible) and I wait forever but this switch crosses
  268. * kernel boundaries and gets the pmclock. One of us gets there
  269. * first and either I never sleep (p->stale iscleared) or I sleep
  270. * and get waken after. pmclock + rendez locks make sure this is
  271. * the case.
  272. */
  273. static int
  274. shouldipi(Mach *mp)
  275. {
  276. if(!mp->online)
  277. return 0;
  278. if(mp->proc == nil && mp->NIX.nixtype == NIXAC)
  279. return 0;
  280. return 1;
  281. }
  282. uint64_t
  283. pmcgetctr(uint32_t coreno, uint32_t regno)
  284. {
  285. PmcCtr *p;
  286. Mach *mp;
  287. uint64_t v;
  288. if(coreno == machp()->machno){
  289. v = getctr(regno);
  290. if (pmcdebug) {
  291. print("int getctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  292. }
  293. return v;
  294. }
  295. mp = sys->machptr[coreno];
  296. p = &mp->pmc[regno];
  297. ilock(&mp->pmclock);
  298. p->ctrset |= PmcGet;
  299. if(shouldipi(mp)){
  300. waitnotstale(mp, p);
  301. ilock(&mp->pmclock);
  302. }
  303. v = p->ctr;
  304. iunlock(&mp->pmclock);
  305. if (pmcdebug) {
  306. print("ext getctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  307. }
  308. return v;
  309. }
  310. int
  311. pmcsetctr(uint32_t coreno, uint64_t v, uint32_t regno)
  312. {
  313. PmcCtr *p;
  314. Mach *mp;
  315. if(coreno == machp()->machno){
  316. if (pmcdebug) {
  317. print("int getctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  318. }
  319. return setctr(v, regno);
  320. }
  321. mp = sys->machptr[coreno];
  322. p = &mp->pmc[regno];
  323. if (pmcdebug) {
  324. print("ext setctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  325. }
  326. ilock(&mp->pmclock);
  327. p->ctr = v;
  328. p->ctrset |= PmcSet;
  329. if(shouldipi(mp))
  330. waitnotstale(mp, p);
  331. else
  332. iunlock(&mp->pmclock);
  333. return 0;
  334. }
  335. static void
  336. ctl2ctl(PmcCtl *dctl, PmcCtl *sctl)
  337. {
  338. if(sctl->enab != PmcCtlNullval)
  339. dctl->enab = sctl->enab;
  340. if(sctl->user != PmcCtlNullval)
  341. dctl->user = sctl->user;
  342. if(sctl->os != PmcCtlNullval)
  343. dctl->os = sctl->os;
  344. if(sctl->nodesc == 0) {
  345. memmove(dctl->descstr, sctl->descstr, KNAMELEN);
  346. dctl->nodesc = 0;
  347. }
  348. }
  349. int
  350. pmcsetctl(uint32_t coreno, PmcCtl *pctl, uint32_t regno)
  351. {
  352. PmcCtr *p;
  353. Mach *mp;
  354. if(coreno == machp()->machno)
  355. return setctl(pctl, regno);
  356. mp = sys->machptr[coreno];
  357. p = &mp->pmc[regno];
  358. ilock(&mp->pmclock);
  359. ctl2ctl(&p->PmcCtl, pctl);
  360. p->ctlset |= PmcSet;
  361. if(shouldipi(mp))
  362. waitnotstale(mp, p);
  363. else
  364. iunlock(&mp->pmclock);
  365. return 0;
  366. }
  367. int
  368. pmcgetctl(uint32_t coreno, PmcCtl *pctl, uint32_t regno)
  369. {
  370. PmcCtr *p;
  371. Mach *mp;
  372. if(coreno == machp()->machno)
  373. return getctl(pctl, regno);
  374. mp = sys->machptr[coreno];
  375. p = &mp->pmc[regno];
  376. ilock(&mp->pmclock);
  377. p->ctlset |= PmcGet;
  378. if(shouldipi(mp)){
  379. waitnotstale(mp, p);
  380. ilock(&mp->pmclock);
  381. }
  382. memmove(pctl, &p->PmcCtl, sizeof(PmcCtl));
  383. iunlock(&mp->pmclock);
  384. return 0;
  385. }
  386. void
  387. pmcupdate(Mach *m)
  388. {
  389. PmcCtr *p;
  390. int i, maxct, wk;
  391. PmcWait *w;
  392. return;
  393. maxct = pmcnregs();
  394. for (i = 0; i < maxct; i++) {
  395. p = &m->pmc[i];
  396. ilock(&m->pmclock);
  397. if(p->ctrset & PmcSet)
  398. setctr(p->ctr, i);
  399. if(p->ctlset & PmcSet)
  400. setctl(&p->PmcCtl, i);
  401. p->ctr = getctr(i);
  402. getctl(&p->PmcCtl, i);
  403. p->ctrset = PmcIgn;
  404. p->ctlset = PmcIgn;
  405. wk = p->stale;
  406. p->stale = 0;
  407. if(wk){
  408. for(w = p->wq; w != nil; w = w->next){
  409. p->wq = w->next;
  410. wakeup(&w->rend);
  411. pmcwclose(w);
  412. }
  413. }
  414. iunlock(&m->pmclock);
  415. }
  416. }