perf_counter.c 14 KB

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