perfmon.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*
  2. * Copyright (c) 2014-2018 Remy Noel.
  3. * Copyright (c) 2014-2018 Richard Braun.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. *
  19. * Locking order :
  20. *
  21. * thread_runq -+
  22. * |
  23. * event -+-> interrupts -+-> td
  24. * |
  25. * +-> pmu
  26. *
  27. * TODO Kernel/user mode seggregation.
  28. */
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <kern/clock.h>
  35. #include <kern/init.h>
  36. #include <kern/list.h>
  37. #include <kern/log.h>
  38. #include <kern/macros.h>
  39. #include <kern/percpu.h>
  40. #include <kern/perfmon.h>
  41. #include <kern/perfmon_types.h>
  42. #include <kern/spinlock.h>
  43. #include <kern/syscnt.h>
  44. #include <kern/thread.h>
  45. #include <kern/timer.h>
  46. #include <kern/xcall.h>
  47. #include <machine/boot.h>
  48. #include <machine/cpu.h>
  49. /*
  50. * Minimum hardware counter poll interval, in milliseconds.
  51. *
  52. * The main purpose of polling hardware counters is to detect overflows
  53. * when the driver is unable to reliably use overflow interrupts.
  54. */
  55. #define PERFMON_MIN_POLL_INTERVAL 50
  56. /*
  57. * Internal event flags.
  58. */
  59. #define PERFMON_EF_TYPE_CPU 0x100
  60. #define PERFMON_EF_ATTACHED 0x200
  61. #define PERFMON_EF_PUBLIC_MASK (PERFMON_EF_KERN \
  62. | PERFMON_EF_USER \
  63. | PERFMON_EF_RAW)
  64. /*
  65. * Per-CPU performance monitoring counter.
  66. *
  67. * When an event is attached to a processor, the matching per-CPU PMC get
  68. * referenced. When a per-CPU PMC is referenced, its underlying hardware
  69. * counter is active.
  70. *
  71. * Interrupts and preemption must be disabled on access.
  72. */
  73. struct perfmon_cpu_pmc {
  74. unsigned int nr_refs;
  75. unsigned int pmc_id;
  76. unsigned int raw_event_id;
  77. uint64_t raw_value;
  78. uint64_t value;
  79. };
  80. /*
  81. * Per-CPU performance monitoring unit.
  82. *
  83. * Per-CPU PMCs are indexed the same way as global PMCs.
  84. *
  85. * Interrupts and preemption must be disabled on access.
  86. */
  87. struct perfmon_cpu_pmu {
  88. struct perfmon_dev *dev;
  89. unsigned int cpu;
  90. struct perfmon_cpu_pmc pmcs[PERFMON_MAX_PMCS];
  91. struct timer poll_timer;
  92. struct syscnt sc_nr_overflows;
  93. };
  94. /*
  95. * Performance monitoring counter.
  96. *
  97. * When a PMC is used, it maps a raw event to a hardware counter.
  98. * A PMC is used if and only if its reference counter isn't zero.
  99. */
  100. struct perfmon_pmc {
  101. unsigned int nr_refs;
  102. unsigned int pmc_id;
  103. unsigned int raw_event_id;
  104. };
  105. /*
  106. * Performance monitoring unit.
  107. *
  108. * There is a single system-wide logical PMU, used to globally allocate
  109. * PMCs. Reserving a counter across the entire system ensures thread
  110. * migration isn't hindered by performance monitoring.
  111. *
  112. * Locking the global PMU is only required when allocating or releasing
  113. * a PMC. Once allocated, the PMC may safely be accessed without hodling
  114. * the lock.
  115. */
  116. struct perfmon_pmu {
  117. struct perfmon_dev *dev;
  118. struct spinlock lock;
  119. struct perfmon_pmc pmcs[PERFMON_MAX_PMCS];
  120. };
  121. static struct perfmon_pmu perfmon_pmu;
  122. static struct perfmon_cpu_pmu perfmon_cpu_pmu __percpu;
  123. static struct perfmon_pmu *
  124. perfmon_get_pmu(void)
  125. {
  126. return &perfmon_pmu;
  127. }
  128. static struct perfmon_cpu_pmu *
  129. perfmon_get_local_cpu_pmu(void)
  130. {
  131. assert(!thread_preempt_enabled());
  132. return cpu_local_ptr(perfmon_cpu_pmu);
  133. }
  134. static struct perfmon_cpu_pmu *
  135. perfmon_get_cpu_pmu(unsigned int cpu)
  136. {
  137. return percpu_ptr(perfmon_cpu_pmu, cpu);
  138. }
  139. static void __init
  140. perfmon_pmc_init(struct perfmon_pmc *pmc)
  141. {
  142. pmc->nr_refs = 0;
  143. }
  144. static bool
  145. perfmon_pmc_used(const struct perfmon_pmc *pmc)
  146. {
  147. return pmc->nr_refs != 0;
  148. }
  149. static unsigned int
  150. perfmon_pmc_id(const struct perfmon_pmc *pmc)
  151. {
  152. return pmc->pmc_id;
  153. }
  154. static unsigned int
  155. perfmon_pmc_raw_event_id(const struct perfmon_pmc *pmc)
  156. {
  157. return pmc->raw_event_id;
  158. }
  159. static void
  160. perfmon_pmc_use(struct perfmon_pmc *pmc, unsigned int pmc_id,
  161. unsigned int raw_event_id)
  162. {
  163. assert(!perfmon_pmc_used(pmc));
  164. pmc->nr_refs = 1;
  165. pmc->pmc_id = pmc_id;
  166. pmc->raw_event_id = raw_event_id;
  167. }
  168. static void
  169. perfmon_pmc_ref(struct perfmon_pmc *pmc)
  170. {
  171. assert(perfmon_pmc_used(pmc));
  172. pmc->nr_refs++;
  173. }
  174. static void
  175. perfmon_pmc_unref(struct perfmon_pmc *pmc)
  176. {
  177. assert(perfmon_pmc_used(pmc));
  178. pmc->nr_refs--;
  179. }
  180. static unsigned int
  181. perfmon_pmu_get_pmc_index(const struct perfmon_pmu *pmu,
  182. const struct perfmon_pmc *pmc)
  183. {
  184. size_t pmc_index;
  185. pmc_index = pmc - pmu->pmcs;
  186. assert(pmc_index < ARRAY_SIZE(pmu->pmcs));
  187. return pmc_index;
  188. }
  189. static struct perfmon_pmc *
  190. perfmon_pmu_get_pmc(struct perfmon_pmu *pmu, unsigned int index)
  191. {
  192. assert(index < ARRAY_SIZE(pmu->pmcs));
  193. return &pmu->pmcs[index];
  194. }
  195. static void __init
  196. perfmon_pmu_init(struct perfmon_pmu *pmu)
  197. {
  198. pmu->dev = NULL;
  199. spinlock_init(&pmu->lock);
  200. for (unsigned int i = 0; i < ARRAY_SIZE(pmu->pmcs); i++) {
  201. perfmon_pmc_init(perfmon_pmu_get_pmc(pmu, i));
  202. }
  203. }
  204. static void __init
  205. perfmon_pmu_set_dev(struct perfmon_pmu *pmu, struct perfmon_dev *dev)
  206. {
  207. assert(dev);
  208. assert(!pmu->dev);
  209. pmu->dev = dev;
  210. }
  211. static struct perfmon_dev *
  212. perfmon_pmu_get_dev(const struct perfmon_pmu *pmu)
  213. {
  214. return pmu->dev;
  215. }
  216. static void
  217. perfmon_pmu_handle_overflow_intr(const struct perfmon_pmu *pmu)
  218. {
  219. pmu->dev->ops->handle_overflow_intr();
  220. }
  221. static int
  222. perfmon_pmu_translate(const struct perfmon_pmu *pmu,
  223. unsigned int *raw_event_id,
  224. unsigned int event_id)
  225. {
  226. if (!pmu->dev) {
  227. return ENODEV;
  228. }
  229. return pmu->dev->ops->translate(raw_event_id, event_id);
  230. }
  231. static int
  232. perfmon_pmu_alloc_pmc_id(const struct perfmon_pmu *pmu,
  233. unsigned int *pmc_idp,
  234. unsigned int pmc_index,
  235. unsigned int raw_event_id)
  236. {
  237. unsigned int pmc_id;
  238. int error;
  239. if (!pmu->dev) {
  240. return ENODEV;
  241. }
  242. error = pmu->dev->ops->alloc(&pmc_id, pmc_index, raw_event_id);
  243. if (error) {
  244. return error;
  245. }
  246. *pmc_idp = pmc_id;
  247. return 0;
  248. }
  249. static void
  250. perfmon_pmu_free_pmc_id(const struct perfmon_pmu *pmu, unsigned int pmc_id)
  251. {
  252. assert(pmu->dev);
  253. pmu->dev->ops->free(pmc_id);
  254. }
  255. static struct perfmon_pmc *
  256. perfmon_pmu_find_unused_pmc(struct perfmon_pmu *pmu)
  257. {
  258. struct perfmon_pmc *pmc;
  259. for (unsigned int i = 0; i < ARRAY_SIZE(pmu->pmcs); i++) {
  260. pmc = perfmon_pmu_get_pmc(pmu, i);
  261. if (!perfmon_pmc_used(pmc)) {
  262. return pmc;
  263. }
  264. }
  265. return NULL;
  266. }
  267. static int
  268. perfmon_pmu_alloc_pmc(struct perfmon_pmu *pmu, struct perfmon_pmc **pmcp,
  269. unsigned int raw_event_id)
  270. {
  271. unsigned int pmc_id = 0, pmc_index;
  272. struct perfmon_pmc *pmc;
  273. int error;
  274. pmc = perfmon_pmu_find_unused_pmc(pmu);
  275. if (!pmc) {
  276. return EAGAIN;
  277. }
  278. pmc_index = perfmon_pmu_get_pmc_index(pmu, pmc);
  279. error = perfmon_pmu_alloc_pmc_id(pmu, &pmc_id, pmc_index, raw_event_id);
  280. if (error) {
  281. return error;
  282. }
  283. perfmon_pmc_use(pmc, pmc_id, raw_event_id);
  284. *pmcp = pmc;
  285. return 0;
  286. }
  287. static void
  288. perfmon_pmu_free_pmc(struct perfmon_pmu *pmu, struct perfmon_pmc *pmc)
  289. {
  290. unsigned int pmc_id;
  291. assert(!perfmon_pmc_used(pmc));
  292. pmc_id = perfmon_pmc_id(pmc);
  293. perfmon_pmu_free_pmc_id(pmu, pmc_id);
  294. }
  295. static struct perfmon_pmc *
  296. perfmon_pmu_get_pmc_by_raw_event_id(struct perfmon_pmu *pmu,
  297. unsigned int raw_event_id)
  298. {
  299. struct perfmon_pmc *pmc;
  300. for (unsigned int i = 0; i < ARRAY_SIZE(pmu->pmcs); i++) {
  301. pmc = perfmon_pmu_get_pmc(pmu, i);
  302. if (!perfmon_pmc_used(pmc)) {
  303. continue;
  304. }
  305. if (perfmon_pmc_raw_event_id(pmc) == raw_event_id) {
  306. return pmc;
  307. }
  308. }
  309. return NULL;
  310. }
  311. static int
  312. perfmon_pmu_take_pmc(struct perfmon_pmu *pmu, struct perfmon_pmc **pmcp,
  313. unsigned int raw_event_id)
  314. {
  315. struct perfmon_pmc *pmc;
  316. int error;
  317. spinlock_lock(&pmu->lock);
  318. pmc = perfmon_pmu_get_pmc_by_raw_event_id(pmu, raw_event_id);
  319. if (pmc) {
  320. perfmon_pmc_ref(pmc);
  321. error = 0;
  322. } else {
  323. error = perfmon_pmu_alloc_pmc(pmu, &pmc, raw_event_id);
  324. if (error) {
  325. pmc = NULL;
  326. }
  327. }
  328. spinlock_unlock(&pmu->lock);
  329. if (error) {
  330. return error;
  331. }
  332. *pmcp = pmc;
  333. return 0;
  334. }
  335. static void
  336. perfmon_pmu_put_pmc(struct perfmon_pmu *pmu, struct perfmon_pmc *pmc)
  337. {
  338. spinlock_lock(&pmu->lock);
  339. perfmon_pmc_unref(pmc);
  340. if (!perfmon_pmc_used(pmc)) {
  341. perfmon_pmu_free_pmc(pmu, pmc);
  342. }
  343. spinlock_unlock(&pmu->lock);
  344. }
  345. static int
  346. perfmon_check_event_args(unsigned int id, unsigned int flags)
  347. {
  348. if (!((flags & PERFMON_EF_PUBLIC_MASK) == flags)
  349. || !((flags & PERFMON_EF_RAW) || (id < PERFMON_NR_GENERIC_EVENTS))
  350. || !((flags & (PERFMON_EF_KERN | PERFMON_EF_USER)))) {
  351. return EINVAL;
  352. }
  353. return 0;
  354. }
  355. int
  356. perfmon_event_init(struct perfmon_event *event, unsigned int id,
  357. unsigned int flags)
  358. {
  359. int error;
  360. error = perfmon_check_event_args(id, flags);
  361. if (error) {
  362. return error;
  363. }
  364. spinlock_init(&event->lock);
  365. event->flags = flags;
  366. event->id = id;
  367. event->value = 0;
  368. return 0;
  369. }
  370. static bool
  371. perfmon_event_type_cpu(const struct perfmon_event *event)
  372. {
  373. return event->flags & PERFMON_EF_TYPE_CPU;
  374. }
  375. static void
  376. perfmon_event_set_type_cpu(struct perfmon_event *event)
  377. {
  378. event->flags |= PERFMON_EF_TYPE_CPU;
  379. }
  380. static void
  381. perfmon_event_clear_type_cpu(struct perfmon_event *event)
  382. {
  383. event->flags &= ~PERFMON_EF_TYPE_CPU;
  384. }
  385. static bool
  386. perfmon_event_attached(const struct perfmon_event *event)
  387. {
  388. return event->flags & PERFMON_EF_ATTACHED;
  389. }
  390. static unsigned int
  391. perfmon_event_pmc_index(const struct perfmon_event *event)
  392. {
  393. assert(perfmon_event_attached(event));
  394. return event->pmc_index;
  395. }
  396. static void __init
  397. perfmon_cpu_pmc_init(struct perfmon_cpu_pmc *cpu_pmc)
  398. {
  399. cpu_pmc->nr_refs = 0;
  400. }
  401. static bool
  402. perfmon_cpu_pmc_used(const struct perfmon_cpu_pmc *cpu_pmc)
  403. {
  404. return cpu_pmc->nr_refs != 0;
  405. }
  406. static void
  407. perfmon_cpu_pmc_use(struct perfmon_cpu_pmc *cpu_pmc, unsigned int pmc_id,
  408. unsigned int raw_event_id, uint64_t raw_value)
  409. {
  410. assert(!perfmon_cpu_pmc_used(cpu_pmc));
  411. cpu_pmc->nr_refs = 1;
  412. cpu_pmc->pmc_id = pmc_id;
  413. cpu_pmc->raw_event_id = raw_event_id;
  414. cpu_pmc->raw_value = raw_value;
  415. cpu_pmc->value = 0;
  416. }
  417. static void
  418. perfmon_cpu_pmc_ref(struct perfmon_cpu_pmc *cpu_pmc)
  419. {
  420. assert(perfmon_cpu_pmc_used(cpu_pmc));
  421. cpu_pmc->nr_refs++;
  422. }
  423. static void
  424. perfmon_cpu_pmc_unref(struct perfmon_cpu_pmc *cpu_pmc)
  425. {
  426. assert(perfmon_cpu_pmc_used(cpu_pmc));
  427. cpu_pmc->nr_refs--;
  428. }
  429. static unsigned int
  430. perfmon_cpu_pmc_id(const struct perfmon_cpu_pmc *cpu_pmc)
  431. {
  432. return cpu_pmc->pmc_id;
  433. }
  434. static bool
  435. perfmon_cpu_pmc_update(struct perfmon_cpu_pmc *cpu_pmc, uint64_t raw_value,
  436. unsigned int pmc_width)
  437. {
  438. bool overflowed;
  439. uint64_t delta;
  440. delta = raw_value - cpu_pmc->raw_value;
  441. if (pmc_width == 64) {
  442. overflowed = false;
  443. } else {
  444. if (raw_value >= cpu_pmc->raw_value) {
  445. overflowed = false;
  446. } else {
  447. overflowed = true;
  448. delta += (uint64_t)1 << pmc_width;
  449. }
  450. }
  451. cpu_pmc->value += delta;
  452. cpu_pmc->raw_value = raw_value;
  453. return overflowed;
  454. }
  455. static uint64_t
  456. perfmon_cpu_pmc_get_value(const struct perfmon_cpu_pmc *cpu_pmc)
  457. {
  458. return cpu_pmc->value;
  459. }
  460. static struct perfmon_cpu_pmc *
  461. perfmon_cpu_pmu_get_pmc(struct perfmon_cpu_pmu *cpu_pmu, unsigned int index)
  462. {
  463. assert(index < ARRAY_SIZE(cpu_pmu->pmcs));
  464. return &cpu_pmu->pmcs[index];
  465. }
  466. static void
  467. perfmon_cpu_pmu_start(struct perfmon_cpu_pmu *cpu_pmu, unsigned int pmc_id,
  468. unsigned int raw_event_id)
  469. {
  470. cpu_pmu->dev->ops->start(pmc_id, raw_event_id);
  471. }
  472. static void
  473. perfmon_cpu_pmu_stop(struct perfmon_cpu_pmu *cpu_pmu, unsigned int pmc_id)
  474. {
  475. cpu_pmu->dev->ops->stop(pmc_id);
  476. }
  477. static uint64_t
  478. perfmon_cpu_pmu_read(const struct perfmon_cpu_pmu *cpu_pmu, unsigned int pmc_id)
  479. {
  480. return cpu_pmu->dev->ops->read(pmc_id);
  481. }
  482. static void
  483. perfmon_cpu_pmu_use_pmc(struct perfmon_cpu_pmu *cpu_pmu,
  484. struct perfmon_cpu_pmc *cpu_pmc,
  485. unsigned int pmc_id,
  486. unsigned int raw_event_id)
  487. {
  488. uint64_t raw_value;
  489. perfmon_cpu_pmu_start(cpu_pmu, pmc_id, raw_event_id);
  490. raw_value = perfmon_cpu_pmu_read(cpu_pmu, pmc_id);
  491. perfmon_cpu_pmc_use(cpu_pmc, pmc_id, raw_event_id, raw_value);
  492. }
  493. static void
  494. perfmon_cpu_pmu_update_pmc(struct perfmon_cpu_pmu *cpu_pmu,
  495. struct perfmon_cpu_pmc *cpu_pmc)
  496. {
  497. uint64_t raw_value;
  498. bool overflowed;
  499. raw_value = perfmon_cpu_pmu_read(cpu_pmu, perfmon_cpu_pmc_id(cpu_pmc));
  500. overflowed = perfmon_cpu_pmc_update(cpu_pmc, raw_value,
  501. cpu_pmu->dev->pmc_width);
  502. if (overflowed) {
  503. syscnt_inc(&cpu_pmu->sc_nr_overflows);
  504. }
  505. }
  506. static void
  507. perfmon_cpu_pmu_check_overflow(void *arg)
  508. {
  509. struct perfmon_cpu_pmu *cpu_pmu;
  510. struct perfmon_cpu_pmc *cpu_pmc;
  511. assert(!cpu_intr_enabled());
  512. cpu_pmu = arg;
  513. assert(cpu_pmu->cpu == cpu_id());
  514. for (unsigned int i = 0; i < ARRAY_SIZE(cpu_pmu->pmcs); i++) {
  515. cpu_pmc = perfmon_cpu_pmu_get_pmc(cpu_pmu, i);
  516. if (!perfmon_cpu_pmc_used(cpu_pmc)) {
  517. continue;
  518. }
  519. perfmon_cpu_pmu_update_pmc(cpu_pmu, cpu_pmc);
  520. }
  521. }
  522. static void
  523. perfmon_cpu_pmu_poll(struct timer *timer)
  524. {
  525. struct perfmon_cpu_pmu *cpu_pmu;
  526. cpu_pmu = structof(timer, struct perfmon_cpu_pmu, poll_timer);
  527. xcall_call(perfmon_cpu_pmu_check_overflow, cpu_pmu, cpu_pmu->cpu);
  528. timer_schedule(timer, timer_get_time(timer) + cpu_pmu->dev->poll_interval);
  529. }
  530. static void __init
  531. perfmon_cpu_pmu_init(struct perfmon_cpu_pmu *cpu_pmu, unsigned int cpu,
  532. struct perfmon_dev *dev)
  533. {
  534. char name[SYSCNT_NAME_SIZE];
  535. cpu_pmu->dev = dev;
  536. cpu_pmu->cpu = cpu;
  537. for (unsigned int i = 0; i < ARRAY_SIZE(cpu_pmu->pmcs); i++) {
  538. perfmon_cpu_pmc_init(perfmon_cpu_pmu_get_pmc(cpu_pmu, i));
  539. }
  540. if (dev->ops->handle_overflow_intr == NULL) {
  541. assert(dev->poll_interval != 0);
  542. /*
  543. * XXX Ideally, this would be an interrupt timer instead of a high
  544. * priority one, but it can't be because the handler performs
  545. * cross-calls to remote processors, which requires that interrupts
  546. * be enabled. This is one potential user of CPU-bound timers.
  547. */
  548. timer_init(&cpu_pmu->poll_timer, perfmon_cpu_pmu_poll, TIMER_HIGH_PRIO);
  549. timer_schedule(&cpu_pmu->poll_timer, dev->poll_interval);
  550. }
  551. snprintf(name, sizeof(name), "perfmon_nr_overflows/%u", cpu);
  552. syscnt_register(&cpu_pmu->sc_nr_overflows, name);
  553. }
  554. static uint64_t
  555. perfmon_cpu_pmu_load(struct perfmon_cpu_pmu *cpu_pmu, unsigned int pmc_index,
  556. unsigned int pmc_id, unsigned int raw_event_id)
  557. {
  558. struct perfmon_cpu_pmc *cpu_pmc;
  559. assert(!cpu_intr_enabled());
  560. cpu_pmc = perfmon_cpu_pmu_get_pmc(cpu_pmu, pmc_index);
  561. if (perfmon_cpu_pmc_used(cpu_pmc)) {
  562. perfmon_cpu_pmc_ref(cpu_pmc);
  563. perfmon_cpu_pmu_update_pmc(cpu_pmu, cpu_pmc);
  564. } else {
  565. perfmon_cpu_pmu_use_pmc(cpu_pmu, cpu_pmc, pmc_id, raw_event_id);
  566. }
  567. return perfmon_cpu_pmc_get_value(cpu_pmc);
  568. }
  569. static uint64_t
  570. perfmon_cpu_pmu_unload(struct perfmon_cpu_pmu *cpu_pmu, unsigned int pmc_index)
  571. {
  572. struct perfmon_cpu_pmc *cpu_pmc;
  573. unsigned int pmc_id;
  574. uint64_t value;
  575. assert(!cpu_intr_enabled());
  576. cpu_pmc = perfmon_cpu_pmu_get_pmc(cpu_pmu, pmc_index);
  577. pmc_id = perfmon_cpu_pmc_id(cpu_pmc);
  578. perfmon_cpu_pmu_update_pmc(cpu_pmu, cpu_pmc);
  579. value = perfmon_cpu_pmc_get_value(cpu_pmc);
  580. perfmon_cpu_pmc_unref(cpu_pmc);
  581. if (!perfmon_cpu_pmc_used(cpu_pmc)) {
  582. perfmon_cpu_pmu_stop(cpu_pmu, pmc_id);
  583. }
  584. return value;
  585. }
  586. static uint64_t
  587. perfmon_cpu_pmu_sync(struct perfmon_cpu_pmu *cpu_pmu, unsigned int pmc_index)
  588. {
  589. struct perfmon_cpu_pmc *cpu_pmc;
  590. assert(!cpu_intr_enabled());
  591. cpu_pmc = perfmon_cpu_pmu_get_pmc(cpu_pmu, pmc_index);
  592. perfmon_cpu_pmu_update_pmc(cpu_pmu, cpu_pmc);
  593. return perfmon_cpu_pmc_get_value(cpu_pmc);
  594. }
  595. static void
  596. perfmon_td_pmc_init(struct perfmon_td_pmc *td_pmc)
  597. {
  598. td_pmc->nr_refs = 0;
  599. td_pmc->loaded = false;
  600. td_pmc->value = 0;
  601. }
  602. static bool
  603. perfmon_td_pmc_used(const struct perfmon_td_pmc *td_pmc)
  604. {
  605. return td_pmc->nr_refs != 0;
  606. }
  607. static void
  608. perfmon_td_pmc_use(struct perfmon_td_pmc *td_pmc, unsigned int pmc_id,
  609. unsigned int raw_event_id)
  610. {
  611. assert(!perfmon_td_pmc_used(td_pmc));
  612. td_pmc->nr_refs = 1;
  613. td_pmc->loaded = false;
  614. td_pmc->pmc_id = pmc_id;
  615. td_pmc->raw_event_id = raw_event_id;
  616. td_pmc->value = 0;
  617. }
  618. static unsigned int
  619. perfmon_td_pmc_id(const struct perfmon_td_pmc *td_pmc)
  620. {
  621. return td_pmc->pmc_id;
  622. }
  623. static unsigned int
  624. perfmon_td_pmc_raw_event_id(const struct perfmon_td_pmc *td_pmc)
  625. {
  626. return td_pmc->raw_event_id;
  627. }
  628. static void
  629. perfmon_td_pmc_ref(struct perfmon_td_pmc *td_pmc)
  630. {
  631. assert(perfmon_td_pmc_used(td_pmc));
  632. td_pmc->nr_refs++;
  633. }
  634. static void
  635. perfmon_td_pmc_unref(struct perfmon_td_pmc *td_pmc)
  636. {
  637. assert(perfmon_td_pmc_used(td_pmc));
  638. td_pmc->nr_refs--;
  639. }
  640. static bool
  641. perfmon_td_pmc_loaded(const struct perfmon_td_pmc *td_pmc)
  642. {
  643. return td_pmc->loaded;
  644. }
  645. static void
  646. perfmon_td_pmc_load(struct perfmon_td_pmc *td_pmc, uint64_t cpu_pmc_value)
  647. {
  648. assert(!perfmon_td_pmc_loaded(td_pmc));
  649. td_pmc->cpu_pmc_value = cpu_pmc_value;
  650. td_pmc->loaded = true;
  651. }
  652. static void
  653. perfmon_td_pmc_update(struct perfmon_td_pmc *td_pmc, uint64_t cpu_pmc_value)
  654. {
  655. uint64_t delta;
  656. assert(perfmon_td_pmc_loaded(td_pmc));
  657. delta = cpu_pmc_value - td_pmc->cpu_pmc_value;
  658. td_pmc->cpu_pmc_value = cpu_pmc_value;
  659. td_pmc->value += delta;
  660. }
  661. static void
  662. perfmon_td_pmc_unload(struct perfmon_td_pmc *td_pmc, uint64_t cpu_pmc_value)
  663. {
  664. perfmon_td_pmc_update(td_pmc, cpu_pmc_value);
  665. td_pmc->loaded = false;
  666. }
  667. static uint64_t
  668. perfmon_td_pmc_read(const struct perfmon_td_pmc *td_pmc)
  669. {
  670. return td_pmc->value;
  671. }
  672. static unsigned int
  673. perfmon_td_get_pmc_index(const struct perfmon_td *td,
  674. const struct perfmon_td_pmc *td_pmc)
  675. {
  676. size_t pmc_index;
  677. pmc_index = td_pmc - td->pmcs;
  678. assert(pmc_index < ARRAY_SIZE(td->pmcs));
  679. return pmc_index;
  680. }
  681. static struct perfmon_td_pmc *
  682. perfmon_td_get_pmc(struct perfmon_td *td, unsigned int index)
  683. {
  684. assert(index < ARRAY_SIZE(td->pmcs));
  685. return &td->pmcs[index];
  686. }
  687. void
  688. perfmon_td_init(struct perfmon_td *td)
  689. {
  690. spinlock_init(&td->lock);
  691. for (unsigned int i = 0; i < ARRAY_SIZE(td->pmcs); i++) {
  692. perfmon_td_pmc_init(perfmon_td_get_pmc(td, i));
  693. }
  694. }
  695. static void
  696. perfmon_td_load_pmc(struct perfmon_td *td, struct perfmon_td_pmc *td_pmc)
  697. {
  698. unsigned int pmc_index, pmc_id, raw_event_id;
  699. struct perfmon_cpu_pmu *cpu_pmu;
  700. uint64_t cpu_pmc_value;
  701. cpu_pmu = perfmon_get_local_cpu_pmu();
  702. pmc_index = perfmon_td_get_pmc_index(td, td_pmc);
  703. pmc_id = perfmon_td_pmc_id(td_pmc);
  704. raw_event_id = perfmon_td_pmc_raw_event_id(td_pmc);
  705. cpu_pmc_value = perfmon_cpu_pmu_load(cpu_pmu, pmc_index,
  706. pmc_id, raw_event_id);
  707. perfmon_td_pmc_load(td_pmc, cpu_pmc_value);
  708. }
  709. static void
  710. perfmon_td_unload_pmc(struct perfmon_td *td, struct perfmon_td_pmc *td_pmc)
  711. {
  712. struct perfmon_cpu_pmu *cpu_pmu;
  713. unsigned int pmc_index;
  714. uint64_t cpu_pmc_value;
  715. cpu_pmu = perfmon_get_local_cpu_pmu();
  716. pmc_index = perfmon_td_get_pmc_index(td, td_pmc);
  717. cpu_pmc_value = perfmon_cpu_pmu_unload(cpu_pmu, pmc_index);
  718. perfmon_td_pmc_unload(td_pmc, cpu_pmc_value);
  719. }
  720. static void
  721. perfmon_td_update_pmc(struct perfmon_td *td, struct perfmon_td_pmc *td_pmc)
  722. {
  723. struct perfmon_cpu_pmu *cpu_pmu;
  724. unsigned int pmc_index;
  725. uint64_t cpu_pmc_value;
  726. cpu_pmu = perfmon_get_local_cpu_pmu();
  727. pmc_index = perfmon_td_get_pmc_index(td, td_pmc);
  728. cpu_pmc_value = perfmon_cpu_pmu_sync(cpu_pmu, pmc_index);
  729. perfmon_td_pmc_update(td_pmc, cpu_pmc_value);
  730. }
  731. void
  732. perfmon_td_load(struct perfmon_td *td)
  733. {
  734. unsigned int pmc_index, pmc_id, raw_event_id;
  735. struct perfmon_cpu_pmu *cpu_pmu;
  736. struct perfmon_td_pmc *td_pmc;
  737. uint64_t cpu_pmc_value;
  738. assert(!cpu_intr_enabled());
  739. assert(!thread_preempt_enabled());
  740. cpu_pmu = perfmon_get_local_cpu_pmu();
  741. spinlock_lock(&td->lock);
  742. for (unsigned int i = 0; i < ARRAY_SIZE(td->pmcs); i++) {
  743. td_pmc = perfmon_td_get_pmc(td, i);
  744. if (!perfmon_td_pmc_used(td_pmc) || perfmon_td_pmc_loaded(td_pmc)) {
  745. continue;
  746. }
  747. pmc_index = perfmon_td_get_pmc_index(td, td_pmc);
  748. pmc_id = perfmon_td_pmc_id(td_pmc);
  749. raw_event_id = perfmon_td_pmc_raw_event_id(td_pmc);
  750. cpu_pmc_value = perfmon_cpu_pmu_load(cpu_pmu, pmc_index,
  751. pmc_id, raw_event_id);
  752. perfmon_td_pmc_load(td_pmc, cpu_pmc_value);
  753. }
  754. spinlock_unlock(&td->lock);
  755. }
  756. void
  757. perfmon_td_unload(struct perfmon_td *td)
  758. {
  759. struct perfmon_cpu_pmu *cpu_pmu;
  760. struct perfmon_td_pmc *td_pmc;
  761. unsigned int pmc_index;
  762. uint64_t cpu_pmc_value;
  763. assert(!cpu_intr_enabled());
  764. assert(!thread_preempt_enabled());
  765. cpu_pmu = perfmon_get_local_cpu_pmu();
  766. spinlock_lock(&td->lock);
  767. for (unsigned int i = 0; i < ARRAY_SIZE(td->pmcs); i++) {
  768. td_pmc = perfmon_td_get_pmc(td, i);
  769. if (!perfmon_td_pmc_loaded(td_pmc)) {
  770. continue;
  771. }
  772. pmc_index = perfmon_td_get_pmc_index(td, td_pmc);
  773. cpu_pmc_value = perfmon_cpu_pmu_unload(cpu_pmu, pmc_index);
  774. perfmon_td_pmc_unload(td_pmc, cpu_pmc_value);
  775. }
  776. spinlock_unlock(&td->lock);
  777. }
  778. static void
  779. perfmon_event_load(struct perfmon_event *event, uint64_t pmc_value)
  780. {
  781. event->pmc_value = pmc_value;
  782. }
  783. static void
  784. perfmon_event_update(struct perfmon_event *event, uint64_t pmc_value)
  785. {
  786. uint64_t delta;
  787. delta = pmc_value - event->pmc_value;
  788. event->value += delta;
  789. event->pmc_value = pmc_value;
  790. }
  791. static void
  792. perfmon_event_load_cpu_remote(void *arg)
  793. {
  794. struct perfmon_event *event;
  795. struct perfmon_cpu_pmu *cpu_pmu;
  796. const struct perfmon_pmc *pmc;
  797. struct perfmon_pmu *pmu;
  798. unsigned int pmc_index;
  799. uint64_t cpu_pmc_value;
  800. event = arg;
  801. cpu_pmu = perfmon_get_local_cpu_pmu();
  802. pmu = perfmon_get_pmu();
  803. pmc_index = perfmon_event_pmc_index(event);
  804. pmc = perfmon_pmu_get_pmc(pmu, pmc_index);
  805. cpu_pmc_value = perfmon_cpu_pmu_load(cpu_pmu, pmc_index,
  806. perfmon_pmc_id(pmc),
  807. perfmon_pmc_raw_event_id(pmc));
  808. perfmon_event_load(event, cpu_pmc_value);
  809. }
  810. static void
  811. perfmon_event_load_cpu(struct perfmon_event *event, unsigned int cpu)
  812. {
  813. perfmon_event_set_type_cpu(event);
  814. event->cpu = cpu;
  815. xcall_call(perfmon_event_load_cpu_remote, event, cpu);
  816. }
  817. static void
  818. perfmon_event_load_thread_remote(void *arg)
  819. {
  820. struct perfmon_event *event;
  821. struct perfmon_td_pmc *td_pmc;
  822. struct perfmon_td *td;
  823. unsigned int pmc_index;
  824. uint64_t td_pmc_value;
  825. event = arg;
  826. pmc_index = perfmon_event_pmc_index(event);
  827. td = thread_get_perfmon_td(event->thread);
  828. td_pmc = perfmon_td_get_pmc(td, pmc_index);
  829. spinlock_lock(&td->lock);
  830. if (thread_self() == event->thread) {
  831. if (perfmon_td_pmc_loaded(td_pmc)) {
  832. perfmon_td_update_pmc(td, td_pmc);
  833. } else {
  834. perfmon_td_load_pmc(td, td_pmc);
  835. }
  836. }
  837. td_pmc_value = perfmon_td_pmc_read(td_pmc);
  838. spinlock_unlock(&td->lock);
  839. perfmon_event_load(event, td_pmc_value);
  840. }
  841. static void
  842. perfmon_event_load_thread(struct perfmon_event *event, struct thread *thread)
  843. {
  844. struct perfmon_td_pmc *td_pmc;
  845. struct perfmon_td *td;
  846. struct perfmon_pmu *pmu;
  847. const struct perfmon_pmc *pmc;
  848. unsigned int pmc_index;
  849. unsigned long flags;
  850. pmu = perfmon_get_pmu();
  851. thread_ref(thread);
  852. event->thread = thread;
  853. pmc_index = perfmon_event_pmc_index(event);
  854. pmc = perfmon_pmu_get_pmc(pmu, pmc_index);
  855. td = thread_get_perfmon_td(thread);
  856. td_pmc = perfmon_td_get_pmc(td, pmc_index);
  857. spinlock_lock_intr_save(&td->lock, &flags);
  858. if (perfmon_td_pmc_used(td_pmc)) {
  859. perfmon_td_pmc_ref(td_pmc);
  860. } else {
  861. perfmon_td_pmc_use(td_pmc, perfmon_pmc_id(pmc),
  862. perfmon_pmc_raw_event_id(pmc));
  863. }
  864. spinlock_unlock_intr_restore(&td->lock, flags);
  865. xcall_call(perfmon_event_load_thread_remote, event, thread_cpu(thread));
  866. }
  867. static void
  868. perfmon_event_unload_cpu_remote(void *arg)
  869. {
  870. struct perfmon_event *event;
  871. struct perfmon_cpu_pmu *cpu_pmu;
  872. unsigned int pmc_index;
  873. uint64_t cpu_pmc_value;
  874. event = arg;
  875. cpu_pmu = perfmon_get_local_cpu_pmu();
  876. pmc_index = perfmon_event_pmc_index(event);
  877. cpu_pmc_value = perfmon_cpu_pmu_unload(cpu_pmu, pmc_index);
  878. perfmon_event_update(event, cpu_pmc_value);
  879. }
  880. static void
  881. perfmon_event_unload_cpu(struct perfmon_event *event)
  882. {
  883. xcall_call(perfmon_event_unload_cpu_remote, event, event->cpu);
  884. perfmon_event_clear_type_cpu(event);
  885. }
  886. static void
  887. perfmon_event_unload_thread_remote(void *arg)
  888. {
  889. struct perfmon_event *event;
  890. struct perfmon_td_pmc *td_pmc;
  891. struct perfmon_td *td;
  892. unsigned int pmc_index;
  893. uint64_t td_pmc_value;
  894. event = arg;
  895. pmc_index = perfmon_event_pmc_index(event);
  896. td = thread_get_perfmon_td(event->thread);
  897. td_pmc = perfmon_td_get_pmc(td, pmc_index);
  898. spinlock_lock(&td->lock);
  899. if ((thread_self() == event->thread) && perfmon_td_pmc_loaded(td_pmc)) {
  900. if (perfmon_td_pmc_used(td_pmc)) {
  901. perfmon_td_update_pmc(td, td_pmc);
  902. } else {
  903. perfmon_td_unload_pmc(td, td_pmc);
  904. }
  905. }
  906. td_pmc_value = perfmon_td_pmc_read(td_pmc);
  907. spinlock_unlock(&td->lock);
  908. perfmon_event_update(event, td_pmc_value);
  909. }
  910. static void
  911. perfmon_event_unload_thread(struct perfmon_event *event)
  912. {
  913. struct perfmon_td_pmc *td_pmc;
  914. struct perfmon_td *td;
  915. unsigned int pmc_index;
  916. unsigned long flags;
  917. pmc_index = perfmon_event_pmc_index(event);
  918. td = thread_get_perfmon_td(event->thread);
  919. td_pmc = perfmon_td_get_pmc(td, pmc_index);
  920. spinlock_lock_intr_save(&td->lock, &flags);
  921. perfmon_td_pmc_unref(td_pmc);
  922. spinlock_unlock_intr_restore(&td->lock, flags);
  923. xcall_call(perfmon_event_unload_thread_remote, event,
  924. thread_cpu(event->thread));
  925. thread_unref(event->thread);
  926. event->thread = NULL;
  927. }
  928. static void
  929. perfmon_event_sync_cpu_remote(void *arg)
  930. {
  931. struct perfmon_event *event;
  932. struct perfmon_cpu_pmu *cpu_pmu;
  933. unsigned int pmc_index;
  934. uint64_t cpu_pmc_value;
  935. event = arg;
  936. cpu_pmu = perfmon_get_local_cpu_pmu();
  937. pmc_index = perfmon_event_pmc_index(event);
  938. cpu_pmc_value = perfmon_cpu_pmu_sync(cpu_pmu, pmc_index);
  939. perfmon_event_update(event, cpu_pmc_value);
  940. }
  941. static void
  942. perfmon_event_sync_cpu(struct perfmon_event *event)
  943. {
  944. xcall_call(perfmon_event_sync_cpu_remote, event, event->cpu);
  945. }
  946. static void
  947. perfmon_event_sync_thread_remote(void *arg)
  948. {
  949. struct perfmon_event *event;
  950. struct perfmon_td_pmc *td_pmc;
  951. struct perfmon_td *td;
  952. unsigned int pmc_index;
  953. uint64_t td_pmc_value;
  954. event = arg;
  955. pmc_index = perfmon_event_pmc_index(event);
  956. td = thread_get_perfmon_td(event->thread);
  957. td_pmc = perfmon_td_get_pmc(td, pmc_index);
  958. spinlock_lock(&td->lock);
  959. if (thread_self() == event->thread) {
  960. perfmon_td_update_pmc(td, td_pmc);
  961. }
  962. td_pmc_value = perfmon_td_pmc_read(td_pmc);
  963. spinlock_unlock(&td->lock);
  964. perfmon_event_update(event, td_pmc_value);
  965. }
  966. static void
  967. perfmon_event_sync_thread(struct perfmon_event *event)
  968. {
  969. xcall_call(perfmon_event_sync_thread_remote, event,
  970. thread_cpu(event->thread));
  971. }
  972. static int
  973. perfmon_event_attach_pmu(struct perfmon_event *event)
  974. {
  975. unsigned int raw_event_id = 0;
  976. struct perfmon_pmu *pmu;
  977. struct perfmon_pmc *pmc;
  978. int error;
  979. pmu = perfmon_get_pmu();
  980. if (!(event->flags & PERFMON_EF_RAW)) {
  981. error = perfmon_pmu_translate(pmu, &raw_event_id, event->id);
  982. if (error) {
  983. return error;
  984. }
  985. }
  986. error = perfmon_pmu_take_pmc(pmu, &pmc, raw_event_id);
  987. if (error) {
  988. return error;
  989. }
  990. event->pmc_index = perfmon_pmu_get_pmc_index(pmu, pmc);
  991. event->flags |= PERFMON_EF_ATTACHED;
  992. event->value = 0;
  993. return 0;
  994. }
  995. static void
  996. perfmon_event_detach_pmu(struct perfmon_event *event)
  997. {
  998. struct perfmon_pmu *pmu;
  999. struct perfmon_pmc *pmc;
  1000. pmu = perfmon_get_pmu();
  1001. pmc = perfmon_pmu_get_pmc(pmu, perfmon_event_pmc_index(event));
  1002. perfmon_pmu_put_pmc(pmu, pmc);
  1003. event->flags &= ~PERFMON_EF_ATTACHED;
  1004. }
  1005. int
  1006. perfmon_event_attach(struct perfmon_event *event, struct thread *thread)
  1007. {
  1008. int error;
  1009. spinlock_lock(&event->lock);
  1010. if (perfmon_event_attached(event)) {
  1011. error = EINVAL;
  1012. goto error;
  1013. }
  1014. error = perfmon_event_attach_pmu(event);
  1015. if (error) {
  1016. goto error;
  1017. }
  1018. perfmon_event_load_thread(event, thread);
  1019. spinlock_unlock(&event->lock);
  1020. return 0;
  1021. error:
  1022. spinlock_unlock(&event->lock);
  1023. return error;
  1024. }
  1025. int
  1026. perfmon_event_attach_cpu(struct perfmon_event *event, unsigned int cpu)
  1027. {
  1028. int error;
  1029. if (cpu >= cpu_count()) {
  1030. return EINVAL;
  1031. }
  1032. spinlock_lock(&event->lock);
  1033. if (perfmon_event_attached(event)) {
  1034. error = EINVAL;
  1035. goto out;
  1036. }
  1037. error = perfmon_event_attach_pmu(event);
  1038. if (error) {
  1039. goto out;
  1040. }
  1041. perfmon_event_load_cpu(event, cpu);
  1042. error = 0;
  1043. out:
  1044. spinlock_unlock(&event->lock);
  1045. return error;
  1046. }
  1047. int
  1048. perfmon_event_detach(struct perfmon_event *event)
  1049. {
  1050. int error;
  1051. spinlock_lock(&event->lock);
  1052. if (!perfmon_event_attached(event)) {
  1053. error = EINVAL;
  1054. goto out;
  1055. }
  1056. if (perfmon_event_type_cpu(event)) {
  1057. perfmon_event_unload_cpu(event);
  1058. } else {
  1059. perfmon_event_unload_thread(event);
  1060. }
  1061. perfmon_event_detach_pmu(event);
  1062. error = 0;
  1063. out:
  1064. spinlock_unlock(&event->lock);
  1065. return error;
  1066. }
  1067. uint64_t
  1068. perfmon_event_read(struct perfmon_event *event)
  1069. {
  1070. uint64_t value;
  1071. spinlock_lock(&event->lock);
  1072. if (perfmon_event_attached(event)) {
  1073. if (perfmon_event_type_cpu(event)) {
  1074. perfmon_event_sync_cpu(event);
  1075. } else {
  1076. perfmon_event_sync_thread(event);
  1077. }
  1078. }
  1079. value = event->value;
  1080. spinlock_unlock(&event->lock);
  1081. return value;
  1082. }
  1083. static uint64_t __init
  1084. perfmon_compute_poll_interval(uint64_t pmc_width)
  1085. {
  1086. uint64_t cycles, time;
  1087. if (pmc_width == 64) {
  1088. cycles = (uint64_t)-1;
  1089. } else {
  1090. cycles = (uint64_t)1 << pmc_width;
  1091. }
  1092. /*
  1093. * Assume an unrealistically high upper bound on the number of
  1094. * events per cycle to otbain a comfortable margin of safety.
  1095. */
  1096. cycles /= 100;
  1097. time = cycles / (cpu_get_freq() / 1000);
  1098. if (time < PERFMON_MIN_POLL_INTERVAL) {
  1099. log_warning("perfmon: invalid poll interval %llu, forced to %llu",
  1100. (unsigned long long)time,
  1101. (unsigned long long)PERFMON_MIN_POLL_INTERVAL);
  1102. time = PERFMON_MIN_POLL_INTERVAL;
  1103. }
  1104. return clock_ticks_from_ms(time);
  1105. }
  1106. void __init
  1107. perfmon_register(struct perfmon_dev *dev)
  1108. {
  1109. const struct perfmon_dev_ops *ops;
  1110. ops = dev->ops;
  1111. assert(ops->translate && ops->alloc && ops->free
  1112. && ops->start && ops->stop && ops->read);
  1113. assert(dev->pmc_width <= 64);
  1114. if ((dev->ops->handle_overflow_intr == NULL) && (dev->poll_interval == 0)) {
  1115. dev->poll_interval = perfmon_compute_poll_interval(dev->pmc_width);
  1116. }
  1117. perfmon_pmu_set_dev(perfmon_get_pmu(), dev);
  1118. }
  1119. void
  1120. perfmon_overflow_intr(void)
  1121. {
  1122. perfmon_pmu_handle_overflow_intr(perfmon_get_pmu());
  1123. }
  1124. void
  1125. perfmon_report_overflow(unsigned int pmc_index)
  1126. {
  1127. struct perfmon_cpu_pmu *cpu_pmu;
  1128. struct perfmon_cpu_pmc *cpu_pmc;
  1129. assert(!cpu_intr_enabled());
  1130. assert(!thread_preempt_enabled());
  1131. cpu_pmu = perfmon_get_local_cpu_pmu();
  1132. cpu_pmc = perfmon_cpu_pmu_get_pmc(cpu_pmu, pmc_index);
  1133. perfmon_cpu_pmu_update_pmc(cpu_pmu, cpu_pmc);
  1134. }
  1135. static int __init
  1136. perfmon_bootstrap(void)
  1137. {
  1138. perfmon_pmu_init(perfmon_get_pmu());
  1139. return 0;
  1140. }
  1141. INIT_OP_DEFINE(perfmon_bootstrap,
  1142. INIT_OP_DEP(log_setup, true),
  1143. INIT_OP_DEP(spinlock_setup, true));
  1144. static int __init
  1145. perfmon_setup(void)
  1146. {
  1147. struct perfmon_dev *dev;
  1148. dev = perfmon_pmu_get_dev(perfmon_get_pmu());
  1149. if (!dev) {
  1150. return ENODEV;
  1151. }
  1152. for (unsigned int cpu = 0; cpu < cpu_count(); cpu++) {
  1153. perfmon_cpu_pmu_init(perfmon_get_cpu_pmu(cpu), cpu, dev);
  1154. }
  1155. return 0;
  1156. }
  1157. INIT_OP_DEFINE(perfmon_setup,
  1158. INIT_OP_DEP(boot_setup_pmu, true),
  1159. INIT_OP_DEP(cpu_mp_probe, true),
  1160. INIT_OP_DEP(cpu_setup, true),
  1161. INIT_OP_DEP(percpu_setup, true),
  1162. INIT_OP_DEP(perfmon_bootstrap, true),
  1163. INIT_OP_DEP(spinlock_setup, true),
  1164. INIT_OP_DEP(syscnt_setup, true));