perf_counter.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Performance counter x86 architecture code
  3. *
  4. * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
  6. *
  7. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/perf_counter.h>
  10. #include <linux/capability.h>
  11. #include <linux/notifier.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/kprobes.h>
  14. #include <linux/module.h>
  15. #include <linux/kdebug.h>
  16. #include <linux/sched.h>
  17. #include <asm/perf_counter.h>
  18. #include <asm/apic.h>
  19. static bool perf_counters_initialized __read_mostly;
  20. /*
  21. * Number of (generic) HW counters:
  22. */
  23. static int nr_counters_generic __read_mostly;
  24. static u64 perf_counter_mask __read_mostly;
  25. static int nr_counters_fixed __read_mostly;
  26. struct cpu_hw_counters {
  27. struct perf_counter *counters[X86_PMC_IDX_MAX];
  28. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  29. };
  30. /*
  31. * Intel PerfMon v3. Used on Core2 and later.
  32. */
  33. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
  34. static const int intel_perfmon_event_map[] =
  35. {
  36. [PERF_COUNT_CYCLES] = 0x003c,
  37. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  38. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  39. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  40. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  41. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  42. };
  43. static const int max_intel_perfmon_events = ARRAY_SIZE(intel_perfmon_event_map);
  44. /*
  45. * Propagate counter elapsed time into the generic counter.
  46. * Can only be executed on the CPU where the counter is active.
  47. * Returns the delta events processed.
  48. */
  49. static void
  50. x86_perf_counter_update(struct perf_counter *counter,
  51. struct hw_perf_counter *hwc, int idx)
  52. {
  53. u64 prev_raw_count, new_raw_count, delta;
  54. /*
  55. * Careful: an NMI might modify the previous counter value.
  56. *
  57. * Our tactic to handle this is to first atomically read and
  58. * exchange a new raw count - then add that new-prev delta
  59. * count to the generic counter atomically:
  60. */
  61. again:
  62. prev_raw_count = atomic64_read(&hwc->prev_count);
  63. rdmsrl(hwc->counter_base + idx, new_raw_count);
  64. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  65. new_raw_count) != prev_raw_count)
  66. goto again;
  67. /*
  68. * Now we have the new raw value and have updated the prev
  69. * timestamp already. We can now calculate the elapsed delta
  70. * (counter-)time and add that to the generic counter.
  71. *
  72. * Careful, not all hw sign-extends above the physical width
  73. * of the count, so we do that by clipping the delta to 32 bits:
  74. */
  75. delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
  76. atomic64_add(delta, &counter->count);
  77. atomic64_sub(delta, &hwc->period_left);
  78. }
  79. /*
  80. * Setup the hardware configuration for a given hw_event_type
  81. */
  82. static int __hw_perf_counter_init(struct perf_counter *counter)
  83. {
  84. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  85. struct hw_perf_counter *hwc = &counter->hw;
  86. if (unlikely(!perf_counters_initialized))
  87. return -EINVAL;
  88. /*
  89. * Count user events, and generate PMC IRQs:
  90. * (keep 'enabled' bit clear for now)
  91. */
  92. hwc->config = ARCH_PERFMON_EVENTSEL_USR | ARCH_PERFMON_EVENTSEL_INT;
  93. /*
  94. * If privileged enough, count OS events too, and allow
  95. * NMI events as well:
  96. */
  97. hwc->nmi = 0;
  98. if (capable(CAP_SYS_ADMIN)) {
  99. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  100. if (hw_event->nmi)
  101. hwc->nmi = 1;
  102. }
  103. hwc->config_base = MSR_ARCH_PERFMON_EVENTSEL0;
  104. hwc->counter_base = MSR_ARCH_PERFMON_PERFCTR0;
  105. hwc->irq_period = hw_event->irq_period;
  106. /*
  107. * Intel PMCs cannot be accessed sanely above 32 bit width,
  108. * so we install an artificial 1<<31 period regardless of
  109. * the generic counter period:
  110. */
  111. if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
  112. hwc->irq_period = 0x7FFFFFFF;
  113. atomic64_set(&hwc->period_left, hwc->irq_period);
  114. /*
  115. * Raw event type provide the config in the event structure
  116. */
  117. if (hw_event->raw) {
  118. hwc->config |= hw_event->type;
  119. } else {
  120. if (hw_event->type >= max_intel_perfmon_events)
  121. return -EINVAL;
  122. /*
  123. * The generic map:
  124. */
  125. hwc->config |= intel_perfmon_event_map[hw_event->type];
  126. }
  127. counter->wakeup_pending = 0;
  128. return 0;
  129. }
  130. void hw_perf_enable_all(void)
  131. {
  132. if (unlikely(!perf_counters_initialized))
  133. return;
  134. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, perf_counter_mask);
  135. }
  136. u64 hw_perf_save_disable(void)
  137. {
  138. u64 ctrl;
  139. if (unlikely(!perf_counters_initialized))
  140. return 0;
  141. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  142. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  143. return ctrl;
  144. }
  145. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  146. void hw_perf_restore(u64 ctrl)
  147. {
  148. if (unlikely(!perf_counters_initialized))
  149. return;
  150. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  151. }
  152. EXPORT_SYMBOL_GPL(hw_perf_restore);
  153. static inline void
  154. __pmc_generic_disable(struct perf_counter *counter,
  155. struct hw_perf_counter *hwc, unsigned int idx)
  156. {
  157. int err;
  158. err = wrmsr_safe(hwc->config_base + idx, hwc->config, 0);
  159. }
  160. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_MAX_GENERIC]);
  161. /*
  162. * Set the next IRQ period, based on the hwc->period_left value.
  163. * To be called with the counter disabled in hw:
  164. */
  165. static void
  166. __hw_perf_counter_set_period(struct perf_counter *counter,
  167. struct hw_perf_counter *hwc, int idx)
  168. {
  169. s32 left = atomic64_read(&hwc->period_left);
  170. s32 period = hwc->irq_period;
  171. /*
  172. * If we are way outside a reasoable range then just skip forward:
  173. */
  174. if (unlikely(left <= -period)) {
  175. left = period;
  176. atomic64_set(&hwc->period_left, left);
  177. }
  178. if (unlikely(left <= 0)) {
  179. left += period;
  180. atomic64_set(&hwc->period_left, left);
  181. }
  182. per_cpu(prev_left[idx], smp_processor_id()) = left;
  183. /*
  184. * The hw counter starts counting from this counter offset,
  185. * mark it to be able to extra future deltas:
  186. */
  187. atomic64_set(&hwc->prev_count, (u64)(s64)-left);
  188. wrmsr(hwc->counter_base + idx, -left, 0);
  189. }
  190. static void
  191. __pmc_generic_enable(struct perf_counter *counter,
  192. struct hw_perf_counter *hwc, int idx)
  193. {
  194. wrmsr(hwc->config_base + idx,
  195. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE, 0);
  196. }
  197. static int fixed_mode_idx(struct hw_perf_counter *hwc)
  198. {
  199. return -1;
  200. }
  201. /*
  202. * Find a PMC slot for the freshly enabled / scheduled in counter:
  203. */
  204. static int pmc_generic_enable(struct perf_counter *counter)
  205. {
  206. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  207. struct hw_perf_counter *hwc = &counter->hw;
  208. int idx = hwc->idx;
  209. /* Try to get the previous counter again */
  210. if (test_and_set_bit(idx, cpuc->used)) {
  211. idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
  212. if (idx == nr_counters_generic)
  213. return -EAGAIN;
  214. set_bit(idx, cpuc->used);
  215. hwc->idx = idx;
  216. }
  217. perf_counters_lapic_init(hwc->nmi);
  218. __pmc_generic_disable(counter, hwc, idx);
  219. cpuc->counters[idx] = counter;
  220. __hw_perf_counter_set_period(counter, hwc, idx);
  221. __pmc_generic_enable(counter, hwc, idx);
  222. return 0;
  223. }
  224. void perf_counter_print_debug(void)
  225. {
  226. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left;
  227. struct cpu_hw_counters *cpuc;
  228. int cpu, idx;
  229. if (!nr_counters_generic)
  230. return;
  231. local_irq_disable();
  232. cpu = smp_processor_id();
  233. cpuc = &per_cpu(cpu_hw_counters, cpu);
  234. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  235. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  236. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  237. printk(KERN_INFO "\n");
  238. printk(KERN_INFO "CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  239. printk(KERN_INFO "CPU#%d: status: %016llx\n", cpu, status);
  240. printk(KERN_INFO "CPU#%d: overflow: %016llx\n", cpu, overflow);
  241. printk(KERN_INFO "CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
  242. for (idx = 0; idx < nr_counters_generic; idx++) {
  243. rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
  244. rdmsrl(MSR_ARCH_PERFMON_PERFCTR0 + idx, pmc_count);
  245. prev_left = per_cpu(prev_left[idx], cpu);
  246. printk(KERN_INFO "CPU#%d: PMC%d ctrl: %016llx\n",
  247. cpu, idx, pmc_ctrl);
  248. printk(KERN_INFO "CPU#%d: PMC%d count: %016llx\n",
  249. cpu, idx, pmc_count);
  250. printk(KERN_INFO "CPU#%d: PMC%d left: %016llx\n",
  251. cpu, idx, prev_left);
  252. }
  253. local_irq_enable();
  254. }
  255. static void pmc_generic_disable(struct perf_counter *counter)
  256. {
  257. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  258. struct hw_perf_counter *hwc = &counter->hw;
  259. unsigned int idx = hwc->idx;
  260. __pmc_generic_disable(counter, hwc, idx);
  261. clear_bit(idx, cpuc->used);
  262. cpuc->counters[idx] = NULL;
  263. /*
  264. * Drain the remaining delta count out of a counter
  265. * that we are disabling:
  266. */
  267. x86_perf_counter_update(counter, hwc, idx);
  268. }
  269. static void perf_store_irq_data(struct perf_counter *counter, u64 data)
  270. {
  271. struct perf_data *irqdata = counter->irqdata;
  272. if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
  273. irqdata->overrun++;
  274. } else {
  275. u64 *p = (u64 *) &irqdata->data[irqdata->len];
  276. *p = data;
  277. irqdata->len += sizeof(u64);
  278. }
  279. }
  280. /*
  281. * Save and restart an expired counter. Called by NMI contexts,
  282. * so it has to be careful about preempting normal counter ops:
  283. */
  284. static void perf_save_and_restart(struct perf_counter *counter)
  285. {
  286. struct hw_perf_counter *hwc = &counter->hw;
  287. int idx = hwc->idx;
  288. u64 pmc_ctrl;
  289. rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
  290. x86_perf_counter_update(counter, hwc, idx);
  291. __hw_perf_counter_set_period(counter, hwc, idx);
  292. if (pmc_ctrl & ARCH_PERFMON_EVENTSEL0_ENABLE)
  293. __pmc_generic_enable(counter, hwc, idx);
  294. }
  295. static void
  296. perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown)
  297. {
  298. struct perf_counter *counter, *group_leader = sibling->group_leader;
  299. /*
  300. * Store sibling timestamps (if any):
  301. */
  302. list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
  303. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  304. perf_store_irq_data(sibling, counter->hw_event.type);
  305. perf_store_irq_data(sibling, atomic64_read(&counter->count));
  306. }
  307. }
  308. /*
  309. * This handler is triggered by the local APIC, so the APIC IRQ handling
  310. * rules apply:
  311. */
  312. static void __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
  313. {
  314. int bit, cpu = smp_processor_id();
  315. u64 ack, status, saved_global;
  316. struct cpu_hw_counters *cpuc;
  317. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, saved_global);
  318. /* Disable counters globally */
  319. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  320. ack_APIC_irq();
  321. cpuc = &per_cpu(cpu_hw_counters, cpu);
  322. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  323. if (!status)
  324. goto out;
  325. again:
  326. ack = status;
  327. for_each_bit(bit, (unsigned long *) &status, nr_counters_generic) {
  328. struct perf_counter *counter = cpuc->counters[bit];
  329. clear_bit(bit, (unsigned long *) &status);
  330. if (!counter)
  331. continue;
  332. perf_save_and_restart(counter);
  333. switch (counter->hw_event.record_type) {
  334. case PERF_RECORD_SIMPLE:
  335. continue;
  336. case PERF_RECORD_IRQ:
  337. perf_store_irq_data(counter, instruction_pointer(regs));
  338. break;
  339. case PERF_RECORD_GROUP:
  340. perf_handle_group(counter, &status, &ack);
  341. break;
  342. }
  343. /*
  344. * From NMI context we cannot call into the scheduler to
  345. * do a task wakeup - but we mark these generic as
  346. * wakeup_pending and initate a wakeup callback:
  347. */
  348. if (nmi) {
  349. counter->wakeup_pending = 1;
  350. set_tsk_thread_flag(current, TIF_PERF_COUNTERS);
  351. } else {
  352. wake_up(&counter->waitq);
  353. }
  354. }
  355. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  356. /*
  357. * Repeat if there is more work to be done:
  358. */
  359. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  360. if (status)
  361. goto again;
  362. out:
  363. /*
  364. * Restore - do not reenable when global enable is off:
  365. */
  366. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, saved_global);
  367. }
  368. void smp_perf_counter_interrupt(struct pt_regs *regs)
  369. {
  370. irq_enter();
  371. inc_irq_stat(apic_perf_irqs);
  372. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  373. __smp_perf_counter_interrupt(regs, 0);
  374. irq_exit();
  375. }
  376. /*
  377. * This handler is triggered by NMI contexts:
  378. */
  379. void perf_counter_notify(struct pt_regs *regs)
  380. {
  381. struct cpu_hw_counters *cpuc;
  382. unsigned long flags;
  383. int bit, cpu;
  384. local_irq_save(flags);
  385. cpu = smp_processor_id();
  386. cpuc = &per_cpu(cpu_hw_counters, cpu);
  387. for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
  388. struct perf_counter *counter = cpuc->counters[bit];
  389. if (!counter)
  390. continue;
  391. if (counter->wakeup_pending) {
  392. counter->wakeup_pending = 0;
  393. wake_up(&counter->waitq);
  394. }
  395. }
  396. local_irq_restore(flags);
  397. }
  398. void __cpuinit perf_counters_lapic_init(int nmi)
  399. {
  400. u32 apic_val;
  401. if (!perf_counters_initialized)
  402. return;
  403. /*
  404. * Enable the performance counter vector in the APIC LVT:
  405. */
  406. apic_val = apic_read(APIC_LVTERR);
  407. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  408. if (nmi)
  409. apic_write(APIC_LVTPC, APIC_DM_NMI);
  410. else
  411. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  412. apic_write(APIC_LVTERR, apic_val);
  413. }
  414. static int __kprobes
  415. perf_counter_nmi_handler(struct notifier_block *self,
  416. unsigned long cmd, void *__args)
  417. {
  418. struct die_args *args = __args;
  419. struct pt_regs *regs;
  420. if (likely(cmd != DIE_NMI_IPI))
  421. return NOTIFY_DONE;
  422. regs = args->regs;
  423. apic_write(APIC_LVTPC, APIC_DM_NMI);
  424. __smp_perf_counter_interrupt(regs, 1);
  425. return NOTIFY_STOP;
  426. }
  427. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  428. .notifier_call = perf_counter_nmi_handler
  429. };
  430. void __init init_hw_perf_counters(void)
  431. {
  432. union cpuid10_eax eax;
  433. unsigned int ebx;
  434. unsigned int unused;
  435. union cpuid10_edx edx;
  436. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  437. return;
  438. /*
  439. * Check whether the Architectural PerfMon supports
  440. * Branch Misses Retired Event or not.
  441. */
  442. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  443. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  444. return;
  445. printk(KERN_INFO "Intel Performance Monitoring support detected.\n");
  446. printk(KERN_INFO "... version: %d\n", eax.split.version_id);
  447. printk(KERN_INFO "... num counters: %d\n", eax.split.num_counters);
  448. nr_counters_generic = eax.split.num_counters;
  449. if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
  450. nr_counters_generic = X86_PMC_MAX_GENERIC;
  451. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  452. nr_counters_generic, X86_PMC_MAX_GENERIC);
  453. }
  454. perf_counter_mask = (1 << nr_counters_generic) - 1;
  455. perf_max_counters = nr_counters_generic;
  456. printk(KERN_INFO "... bit width: %d\n", eax.split.bit_width);
  457. printk(KERN_INFO "... mask length: %d\n", eax.split.mask_length);
  458. nr_counters_fixed = edx.split.num_counters_fixed;
  459. if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
  460. nr_counters_fixed = X86_PMC_MAX_FIXED;
  461. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  462. nr_counters_fixed, X86_PMC_MAX_FIXED);
  463. }
  464. printk(KERN_INFO "... fixed counters: %d\n", nr_counters_fixed);
  465. perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  466. printk(KERN_INFO "... counter mask: %016Lx\n", perf_counter_mask);
  467. perf_counters_initialized = true;
  468. perf_counters_lapic_init(0);
  469. register_die_notifier(&perf_counter_nmi_notifier);
  470. }
  471. static void pmc_generic_read(struct perf_counter *counter)
  472. {
  473. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  474. }
  475. static const struct hw_perf_counter_ops x86_perf_counter_ops = {
  476. .enable = pmc_generic_enable,
  477. .disable = pmc_generic_disable,
  478. .read = pmc_generic_read,
  479. };
  480. const struct hw_perf_counter_ops *
  481. hw_perf_counter_init(struct perf_counter *counter)
  482. {
  483. int err;
  484. err = __hw_perf_counter_init(counter);
  485. if (err)
  486. return NULL;
  487. return &x86_perf_counter_ops;
  488. }