perf_counter.c 14 KB

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