pmcio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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, "%#llx %#llx", 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. }
  135. if (p->user != PmcCtlNullval) {
  136. if (p->user)
  137. v |= PeUsr;
  138. else
  139. v &= ~PeUsr;
  140. }
  141. if (p->os != PmcCtlNullval) {
  142. if (p->os)
  143. v |= PeOS;
  144. else
  145. v &= ~PeOS;
  146. }
  147. if (pmctrans(p) < 0)
  148. return -1;
  149. if (p->nodesc == 0) {
  150. memmove(str, p->descstr, KNAMELEN);
  151. if (tokenize(str, toks, 2) != 2)
  152. return -1;
  153. e = atoi(toks[0]);
  154. u = atoi(toks[1]);
  155. v &= ~(PeEvMskL|PeEvMskH|PeUnMsk);
  156. v |= SetEvMsk(v, e);
  157. v |= SetUMsk(v, u);
  158. }
  159. if (p->reset != PmcCtlNullval && p->reset) {
  160. v = 0;
  161. wrmsr(regno+ PerfCtrbase, 0);
  162. p->reset = PmcCtlNullval; /* only reset once */
  163. }
  164. wrmsr(regno+ PerfEvtbase, v);
  165. pmcuserenab(pmcanyenab());
  166. if (pmcdebug) {
  167. v = rdmsr(regno+ PerfEvtbase);
  168. print("conf pmc[%#x]: %#llx\n", regno, v);
  169. }
  170. return 0;
  171. }
  172. int
  173. pmcctlstr(char *str, int nstr, PmcCtl *p)
  174. {
  175. int ns;
  176. ns = 0;
  177. if (p->enab && p->enab != PmcCtlNullval)
  178. ns += snprint(str + ns, nstr - ns, "enable\n");
  179. else
  180. ns += snprint(str + ns, nstr - ns, "disable\n");
  181. if (p->user && p->user != PmcCtlNullval)
  182. ns += snprint(str + ns, nstr - ns, "user\n");
  183. if (p->os && p->user != PmcCtlNullval)
  184. ns += snprint(str + ns, nstr - ns, "os\n");
  185. //TODO, inverse pmctrans?
  186. if(!p->nodesc)
  187. ns += snprint(str + ns, nstr - ns, "%s\n", p->descstr);
  188. else
  189. ns += snprint(str + ns, nstr - ns, "no desc\n");
  190. return ns;
  191. }
  192. int
  193. pmcdescstr(char *str, int nstr)
  194. {
  195. PmcCtlCtrId *pi;
  196. int ns;
  197. ns = 0;
  198. for (pi = &pmcids[0]; pi->portdesc[0] != '\0'; pi++)
  199. ns += snprint(str + ns, nstr - ns, "%s\n",pi->portdesc);
  200. return ns;
  201. }
  202. static uint64_t
  203. getctr(uint32_t regno)
  204. {
  205. return rdmsr(regno + PerfCtrbase);
  206. }
  207. static int
  208. setctr(uint64_t v, uint32_t regno)
  209. {
  210. wrmsr(regno + PerfCtrbase, v);
  211. return 0;
  212. }
  213. static int
  214. notstale(void *x)
  215. {
  216. PmcCtr *p;
  217. p = (PmcCtr *)x;
  218. return !p->stale;
  219. }
  220. static PmcWait*
  221. newpmcw(void)
  222. {
  223. PmcWait *w;
  224. w = malloc(sizeof (PmcWait));
  225. w->r.ref = 1;
  226. return w;
  227. }
  228. static void
  229. pmcwclose(PmcWait *w)
  230. {
  231. if(decref(&w->r))
  232. return;
  233. free(w);
  234. }
  235. /*
  236. * As it is now, it sends an IPI if the processor is otherwise
  237. * ocuppied for it to update the counter. Probably not needed
  238. * for TC/XC as it will be updated every time we cross the kernel
  239. * boundary, but we are doing it now just in case it is idle or
  240. * not being updated NB: this function releases the ilock
  241. */
  242. static void
  243. waitnotstale(Mach *mp, PmcCtr *p)
  244. {
  245. Proc *up = externup();
  246. PmcWait *w;
  247. p->stale = 1;
  248. w = newpmcw();
  249. w->next = p->wq;
  250. p->wq = w;
  251. incref(&w->r);
  252. iunlock(&mp->pmclock);
  253. apicipi(mp->apicno);
  254. if(waserror()){
  255. pmcwclose(w);
  256. nexterror();
  257. }
  258. sleep(&w->rend, notstale, p);
  259. poperror();
  260. pmcwclose(w);
  261. }
  262. /*
  263. * The reason this is not racy is subtle.
  264. *
  265. * If the processor suddenly changes state to busy once I have
  266. * decided not to IPI it, I don't wait for it.
  267. *
  268. * In the other case, I have decided to IPI it and hence, wait.
  269. * The problem then is that it switches to idle (not
  270. * interruptible) and I wait forever but this switch crosses
  271. * kernel boundaries and gets the pmclock. One of us gets there
  272. * first and either I never sleep (p->stale iscleared) or I sleep
  273. * and get waken after. pmclock + rendez locks make sure this is
  274. * the case.
  275. */
  276. static int
  277. shouldipi(Mach *mp)
  278. {
  279. if(!mp->online)
  280. return 0;
  281. if(mp->proc == nil && mp->NIX.nixtype == NIXAC)
  282. return 0;
  283. return 1;
  284. }
  285. uint64_t
  286. pmcgetctr(uint32_t coreno, uint32_t regno)
  287. {
  288. PmcCtr *p;
  289. Mach *mp;
  290. uint64_t v;
  291. if(coreno == machp()->machno){
  292. v = getctr(regno);
  293. if (pmcdebug) {
  294. print("int getctr[%#x, %#x] = %#llx\n", regno, coreno, v);
  295. }
  296. return v;
  297. }
  298. mp = sys->machptr[coreno];
  299. p = &mp->pmc[regno];
  300. ilock(&mp->pmclock);
  301. p->ctrset |= PmcGet;
  302. if(shouldipi(mp)){
  303. waitnotstale(mp, p);
  304. ilock(&mp->pmclock);
  305. }
  306. v = p->ctr;
  307. iunlock(&mp->pmclock);
  308. if (pmcdebug) {
  309. print("ext getctr[%#x, %#x] = %#llx\n", regno, coreno, v);
  310. }
  311. return v;
  312. }
  313. int
  314. pmcsetctr(uint32_t coreno, uint64_t v, uint32_t regno)
  315. {
  316. PmcCtr *p;
  317. Mach *mp;
  318. if(coreno == machp()->machno){
  319. if (pmcdebug) {
  320. print("int getctr[%#x, %#x] = %#llx\n", regno, coreno, v);
  321. }
  322. return setctr(v, regno);
  323. }
  324. mp = sys->machptr[coreno];
  325. p = &mp->pmc[regno];
  326. if (pmcdebug) {
  327. print("ext setctr[%#x, %#x] = %#llx\n", regno, coreno, v);
  328. }
  329. ilock(&mp->pmclock);
  330. p->ctr = v;
  331. p->ctrset |= PmcSet;
  332. if(shouldipi(mp))
  333. waitnotstale(mp, p);
  334. else
  335. iunlock(&mp->pmclock);
  336. return 0;
  337. }
  338. static void
  339. ctl2ctl(PmcCtl *dctl, PmcCtl *sctl)
  340. {
  341. if(sctl->enab != PmcCtlNullval)
  342. dctl->enab = sctl->enab;
  343. if(sctl->user != PmcCtlNullval)
  344. dctl->user = sctl->user;
  345. if(sctl->os != PmcCtlNullval)
  346. dctl->os = sctl->os;
  347. if(sctl->nodesc == 0) {
  348. memmove(dctl->descstr, sctl->descstr, KNAMELEN);
  349. dctl->nodesc = 0;
  350. }
  351. }
  352. int
  353. pmcsetctl(uint32_t coreno, PmcCtl *pctl, uint32_t regno)
  354. {
  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. PmcCtr *p;
  374. Mach *mp;
  375. if(coreno == machp()->machno)
  376. return getctl(pctl, regno);
  377. mp = sys->machptr[coreno];
  378. p = &mp->pmc[regno];
  379. ilock(&mp->pmclock);
  380. p->ctlset |= PmcGet;
  381. if(shouldipi(mp)){
  382. waitnotstale(mp, p);
  383. ilock(&mp->pmclock);
  384. }
  385. memmove(pctl, &p->PmcCtl, sizeof(PmcCtl));
  386. iunlock(&mp->pmclock);
  387. return 0;
  388. }
  389. void
  390. pmcupdate(Mach *m)
  391. {
  392. PmcCtr *p;
  393. int i, maxct, wk;
  394. PmcWait *w;
  395. return;
  396. maxct = pmcnregs();
  397. for (i = 0; i < maxct; i++) {
  398. p = &m->pmc[i];
  399. ilock(&m->pmclock);
  400. if(p->ctrset & PmcSet)
  401. setctr(p->ctr, i);
  402. if(p->ctlset & PmcSet)
  403. setctl(&p->PmcCtl, i);
  404. p->ctr = getctr(i);
  405. getctl(&p->PmcCtl, i);
  406. p->ctrset = PmcIgn;
  407. p->ctlset = PmcIgn;
  408. wk = p->stale;
  409. p->stale = 0;
  410. if(wk){
  411. for(w = p->wq; w != nil; w = w->next){
  412. p->wq = w->next;
  413. wakeup(&w->rend);
  414. pmcwclose(w);
  415. }
  416. }
  417. iunlock(&m->pmclock);
  418. }
  419. }