perf_counter.c 14 KB

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