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