pmcio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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->ref = 1;
  223. return w;
  224. }
  225. static void
  226. pmcwclose(PmcWait *w)
  227. {
  228. if(decref(w))
  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);
  249. iunlock(&mp->pmclock);
  250. apicipi(mp->apicno);
  251. if(waserror()){
  252. pmcwclose(w);
  253. nexterror();
  254. }
  255. sleep(&w->r, 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->nixtype == NIXAC)
  279. return 0;
  280. return 1;
  281. }
  282. uint64_t
  283. pmcgetctr(uint32_t coreno, uint32_t regno)
  284. {
  285. Proc *up = externup();
  286. PmcCtr *p;
  287. Mach *mp;
  288. uint64_t v;
  289. if(coreno == machp()->machno){
  290. v = getctr(regno);
  291. if (pmcdebug) {
  292. print("int getctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  293. }
  294. return v;
  295. }
  296. mp = sys->machptr[coreno];
  297. p = &mp->pmc[regno];
  298. ilock(&mp->pmclock);
  299. p->ctrset |= PmcGet;
  300. if(shouldipi(mp)){
  301. waitnotstale(mp, p);
  302. ilock(&mp->pmclock);
  303. }
  304. v = p->ctr;
  305. iunlock(&mp->pmclock);
  306. if (pmcdebug) {
  307. print("ext getctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  308. }
  309. return v;
  310. }
  311. int
  312. pmcsetctr(uint32_t coreno, uint64_t v, uint32_t regno)
  313. {
  314. Proc *up = externup();
  315. PmcCtr *p;
  316. Mach *mp;
  317. if(coreno == machp()->machno){
  318. if (pmcdebug) {
  319. print("int getctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  320. }
  321. return setctr(v, regno);
  322. }
  323. mp = sys->machptr[coreno];
  324. p = &mp->pmc[regno];
  325. if (pmcdebug) {
  326. print("ext setctr[%#ux, %#ux] = %#llux\n", regno, coreno, v);
  327. }
  328. ilock(&mp->pmclock);
  329. p->ctr = v;
  330. p->ctrset |= PmcSet;
  331. if(shouldipi(mp))
  332. waitnotstale(mp, p);
  333. else
  334. iunlock(&mp->pmclock);
  335. return 0;
  336. }
  337. static void
  338. ctl2ctl(PmcCtl *dctl, PmcCtl *sctl)
  339. {
  340. if(sctl->enab != PmcCtlNullval)
  341. dctl->enab = sctl->enab;
  342. if(sctl->user != PmcCtlNullval)
  343. dctl->user = sctl->user;
  344. if(sctl->os != PmcCtlNullval)
  345. dctl->os = sctl->os;
  346. if(sctl->nodesc == 0) {
  347. memmove(dctl->descstr, sctl->descstr, KNAMELEN);
  348. dctl->nodesc = 0;
  349. }
  350. }
  351. int
  352. pmcsetctl(uint32_t coreno, PmcCtl *pctl, uint32_t regno)
  353. {
  354. Proc *up = externup();
  355. PmcCtr *p;
  356. Mach *mp;
  357. if(coreno == machp()->machno)
  358. return setctl(pctl, regno);
  359. mp = sys->machptr[coreno];
  360. p = &mp->pmc[regno];
  361. ilock(&mp->pmclock);
  362. ctl2ctl(&p->PmcCtl, pctl);
  363. p->ctlset |= PmcSet;
  364. if(shouldipi(mp))
  365. waitnotstale(mp, p);
  366. else
  367. iunlock(&mp->pmclock);
  368. return 0;
  369. }
  370. int
  371. pmcgetctl(uint32_t coreno, PmcCtl *pctl, uint32_t regno)
  372. {
  373. Proc *up = externup();
  374. PmcCtr *p;
  375. Mach *mp;
  376. if(coreno == machp()->machno)
  377. return getctl(pctl, regno);
  378. mp = sys->machptr[coreno];
  379. p = &mp->pmc[regno];
  380. ilock(&mp->pmclock);
  381. p->ctlset |= PmcGet;
  382. if(shouldipi(mp)){
  383. waitnotstale(mp, p);
  384. ilock(&mp->pmclock);
  385. }
  386. memmove(pctl, &p->PmcCtl, sizeof(PmcCtl));
  387. iunlock(&mp->pmclock);
  388. return 0;
  389. }
  390. void
  391. pmcupdate(Mach *m)
  392. {
  393. PmcCtr *p;
  394. int i, maxct, wk;
  395. PmcWait *w;
  396. return;
  397. maxct = pmcnregs();
  398. for (i = 0; i < maxct; i++) {
  399. p = &m->pmc[i];
  400. ilock(&m->pmclock);
  401. if(p->ctrset & PmcSet)
  402. setctr(p->ctr, i);
  403. if(p->ctlset & PmcSet)
  404. setctl(p, i);
  405. p->ctr = getctr(i);
  406. getctl(p, i);
  407. p->ctrset = PmcIgn;
  408. p->ctlset = PmcIgn;
  409. wk = p->stale;
  410. p->stale = 0;
  411. if(wk){
  412. for(w = p->wq; w != nil; w = w->next){
  413. p->wq = w->next;
  414. wakeup(&w->r);
  415. pmcwclose(w);
  416. }
  417. }
  418. iunlock(&m->pmclock);
  419. }
  420. }