perf_counter.c 14 KB

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