perf_counter.c 15 KB

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