perf_counter.c 14 KB

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