profile.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * linux/kernel/profile.c
  3. * Simple profiling. Manages a direct-mapped profile hit count buffer,
  4. * with configurable resolution, support for restricting the cpus on
  5. * which profiling is done, and switching between cpu time and
  6. * schedule() calls via kernel command line parameters passed at boot.
  7. *
  8. * Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
  9. * Red Hat, July 2004
  10. * Consolidation of architecture support code for profiling,
  11. * William Irwin, Oracle, July 2004
  12. * Amortized hit count accounting via per-cpu open-addressed hashtables
  13. * to resolve timer interrupt livelocks, William Irwin, Oracle, 2004
  14. */
  15. #include <linux/module.h>
  16. #include <linux/profile.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/notifier.h>
  19. #include <linux/mm.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/cpu.h>
  22. #include <linux/highmem.h>
  23. #include <linux/mutex.h>
  24. #include <asm/sections.h>
  25. #include <asm/irq_regs.h>
  26. #include <asm/ptrace.h>
  27. struct profile_hit {
  28. u32 pc, hits;
  29. };
  30. #define PROFILE_GRPSHIFT 3
  31. #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
  32. #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
  33. #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
  34. /* Oprofile timer tick hook */
  35. static int (*timer_hook)(struct pt_regs *) __read_mostly;
  36. static atomic_t *prof_buffer;
  37. static unsigned long prof_len, prof_shift;
  38. int prof_on __read_mostly;
  39. EXPORT_SYMBOL_GPL(prof_on);
  40. static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
  41. #ifdef CONFIG_SMP
  42. static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
  43. static DEFINE_PER_CPU(int, cpu_profile_flip);
  44. static DEFINE_MUTEX(profile_flip_mutex);
  45. #endif /* CONFIG_SMP */
  46. static int __init profile_setup(char *str)
  47. {
  48. static char __initdata schedstr[] = "schedule";
  49. static char __initdata sleepstr[] = "sleep";
  50. static char __initdata kvmstr[] = "kvm";
  51. int par;
  52. if (!strncmp(str, sleepstr, strlen(sleepstr))) {
  53. #ifdef CONFIG_SCHEDSTATS
  54. prof_on = SLEEP_PROFILING;
  55. if (str[strlen(sleepstr)] == ',')
  56. str += strlen(sleepstr) + 1;
  57. if (get_option(&str, &par))
  58. prof_shift = par;
  59. printk(KERN_INFO
  60. "kernel sleep profiling enabled (shift: %ld)\n",
  61. prof_shift);
  62. #else
  63. printk(KERN_WARNING
  64. "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
  65. #endif /* CONFIG_SCHEDSTATS */
  66. } else if (!strncmp(str, schedstr, strlen(schedstr))) {
  67. prof_on = SCHED_PROFILING;
  68. if (str[strlen(schedstr)] == ',')
  69. str += strlen(schedstr) + 1;
  70. if (get_option(&str, &par))
  71. prof_shift = par;
  72. printk(KERN_INFO
  73. "kernel schedule profiling enabled (shift: %ld)\n",
  74. prof_shift);
  75. } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
  76. prof_on = KVM_PROFILING;
  77. if (str[strlen(kvmstr)] == ',')
  78. str += strlen(kvmstr) + 1;
  79. if (get_option(&str, &par))
  80. prof_shift = par;
  81. printk(KERN_INFO
  82. "kernel KVM profiling enabled (shift: %ld)\n",
  83. prof_shift);
  84. } else if (get_option(&str, &par)) {
  85. prof_shift = par;
  86. prof_on = CPU_PROFILING;
  87. printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
  88. prof_shift);
  89. }
  90. return 1;
  91. }
  92. __setup("profile=", profile_setup);
  93. void __init profile_init(void)
  94. {
  95. if (!prof_on)
  96. return;
  97. /* only text is profiled */
  98. prof_len = (_etext - _stext) >> prof_shift;
  99. prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
  100. }
  101. /* Profile event notifications */
  102. static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
  103. static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
  104. static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
  105. void profile_task_exit(struct task_struct *task)
  106. {
  107. blocking_notifier_call_chain(&task_exit_notifier, 0, task);
  108. }
  109. int profile_handoff_task(struct task_struct *task)
  110. {
  111. int ret;
  112. ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
  113. return (ret == NOTIFY_OK) ? 1 : 0;
  114. }
  115. void profile_munmap(unsigned long addr)
  116. {
  117. blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
  118. }
  119. int task_handoff_register(struct notifier_block *n)
  120. {
  121. return atomic_notifier_chain_register(&task_free_notifier, n);
  122. }
  123. EXPORT_SYMBOL_GPL(task_handoff_register);
  124. int task_handoff_unregister(struct notifier_block *n)
  125. {
  126. return atomic_notifier_chain_unregister(&task_free_notifier, n);
  127. }
  128. EXPORT_SYMBOL_GPL(task_handoff_unregister);
  129. int profile_event_register(enum profile_type type, struct notifier_block *n)
  130. {
  131. int err = -EINVAL;
  132. switch (type) {
  133. case PROFILE_TASK_EXIT:
  134. err = blocking_notifier_chain_register(
  135. &task_exit_notifier, n);
  136. break;
  137. case PROFILE_MUNMAP:
  138. err = blocking_notifier_chain_register(
  139. &munmap_notifier, n);
  140. break;
  141. }
  142. return err;
  143. }
  144. EXPORT_SYMBOL_GPL(profile_event_register);
  145. int profile_event_unregister(enum profile_type type, struct notifier_block *n)
  146. {
  147. int err = -EINVAL;
  148. switch (type) {
  149. case PROFILE_TASK_EXIT:
  150. err = blocking_notifier_chain_unregister(
  151. &task_exit_notifier, n);
  152. break;
  153. case PROFILE_MUNMAP:
  154. err = blocking_notifier_chain_unregister(
  155. &munmap_notifier, n);
  156. break;
  157. }
  158. return err;
  159. }
  160. EXPORT_SYMBOL_GPL(profile_event_unregister);
  161. int register_timer_hook(int (*hook)(struct pt_regs *))
  162. {
  163. if (timer_hook)
  164. return -EBUSY;
  165. timer_hook = hook;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(register_timer_hook);
  169. void unregister_timer_hook(int (*hook)(struct pt_regs *))
  170. {
  171. WARN_ON(hook != timer_hook);
  172. timer_hook = NULL;
  173. /* make sure all CPUs see the NULL hook */
  174. synchronize_sched(); /* Allow ongoing interrupts to complete. */
  175. }
  176. EXPORT_SYMBOL_GPL(unregister_timer_hook);
  177. #ifdef CONFIG_SMP
  178. /*
  179. * Each cpu has a pair of open-addressed hashtables for pending
  180. * profile hits. read_profile() IPI's all cpus to request them
  181. * to flip buffers and flushes their contents to prof_buffer itself.
  182. * Flip requests are serialized by the profile_flip_mutex. The sole
  183. * use of having a second hashtable is for avoiding cacheline
  184. * contention that would otherwise happen during flushes of pending
  185. * profile hits required for the accuracy of reported profile hits
  186. * and so resurrect the interrupt livelock issue.
  187. *
  188. * The open-addressed hashtables are indexed by profile buffer slot
  189. * and hold the number of pending hits to that profile buffer slot on
  190. * a cpu in an entry. When the hashtable overflows, all pending hits
  191. * are accounted to their corresponding profile buffer slots with
  192. * atomic_add() and the hashtable emptied. As numerous pending hits
  193. * may be accounted to a profile buffer slot in a hashtable entry,
  194. * this amortizes a number of atomic profile buffer increments likely
  195. * to be far larger than the number of entries in the hashtable,
  196. * particularly given that the number of distinct profile buffer
  197. * positions to which hits are accounted during short intervals (e.g.
  198. * several seconds) is usually very small. Exclusion from buffer
  199. * flipping is provided by interrupt disablement (note that for
  200. * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
  201. * process context).
  202. * The hash function is meant to be lightweight as opposed to strong,
  203. * and was vaguely inspired by ppc64 firmware-supported inverted
  204. * pagetable hash functions, but uses a full hashtable full of finite
  205. * collision chains, not just pairs of them.
  206. *
  207. * -- wli
  208. */
  209. static void __profile_flip_buffers(void *unused)
  210. {
  211. int cpu = smp_processor_id();
  212. per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
  213. }
  214. static void profile_flip_buffers(void)
  215. {
  216. int i, j, cpu;
  217. mutex_lock(&profile_flip_mutex);
  218. j = per_cpu(cpu_profile_flip, get_cpu());
  219. put_cpu();
  220. on_each_cpu(__profile_flip_buffers, NULL, 1);
  221. for_each_online_cpu(cpu) {
  222. struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
  223. for (i = 0; i < NR_PROFILE_HIT; ++i) {
  224. if (!hits[i].hits) {
  225. if (hits[i].pc)
  226. hits[i].pc = 0;
  227. continue;
  228. }
  229. atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
  230. hits[i].hits = hits[i].pc = 0;
  231. }
  232. }
  233. mutex_unlock(&profile_flip_mutex);
  234. }
  235. static void profile_discard_flip_buffers(void)
  236. {
  237. int i, cpu;
  238. mutex_lock(&profile_flip_mutex);
  239. i = per_cpu(cpu_profile_flip, get_cpu());
  240. put_cpu();
  241. on_each_cpu(__profile_flip_buffers, NULL, 1);
  242. for_each_online_cpu(cpu) {
  243. struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
  244. memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
  245. }
  246. mutex_unlock(&profile_flip_mutex);
  247. }
  248. void profile_hits(int type, void *__pc, unsigned int nr_hits)
  249. {
  250. unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
  251. int i, j, cpu;
  252. struct profile_hit *hits;
  253. if (prof_on != type || !prof_buffer)
  254. return;
  255. pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
  256. i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
  257. secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
  258. cpu = get_cpu();
  259. hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
  260. if (!hits) {
  261. put_cpu();
  262. return;
  263. }
  264. /*
  265. * We buffer the global profiler buffer into a per-CPU
  266. * queue and thus reduce the number of global (and possibly
  267. * NUMA-alien) accesses. The write-queue is self-coalescing:
  268. */
  269. local_irq_save(flags);
  270. do {
  271. for (j = 0; j < PROFILE_GRPSZ; ++j) {
  272. if (hits[i + j].pc == pc) {
  273. hits[i + j].hits += nr_hits;
  274. goto out;
  275. } else if (!hits[i + j].hits) {
  276. hits[i + j].pc = pc;
  277. hits[i + j].hits = nr_hits;
  278. goto out;
  279. }
  280. }
  281. i = (i + secondary) & (NR_PROFILE_HIT - 1);
  282. } while (i != primary);
  283. /*
  284. * Add the current hit(s) and flush the write-queue out
  285. * to the global buffer:
  286. */
  287. atomic_add(nr_hits, &prof_buffer[pc]);
  288. for (i = 0; i < NR_PROFILE_HIT; ++i) {
  289. atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
  290. hits[i].pc = hits[i].hits = 0;
  291. }
  292. out:
  293. local_irq_restore(flags);
  294. put_cpu();
  295. }
  296. static int __devinit profile_cpu_callback(struct notifier_block *info,
  297. unsigned long action, void *__cpu)
  298. {
  299. int node, cpu = (unsigned long)__cpu;
  300. struct page *page;
  301. switch (action) {
  302. case CPU_UP_PREPARE:
  303. case CPU_UP_PREPARE_FROZEN:
  304. node = cpu_to_node(cpu);
  305. per_cpu(cpu_profile_flip, cpu) = 0;
  306. if (!per_cpu(cpu_profile_hits, cpu)[1]) {
  307. page = alloc_pages_node(node,
  308. GFP_KERNEL | __GFP_ZERO,
  309. 0);
  310. if (!page)
  311. return NOTIFY_BAD;
  312. per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
  313. }
  314. if (!per_cpu(cpu_profile_hits, cpu)[0]) {
  315. page = alloc_pages_node(node,
  316. GFP_KERNEL | __GFP_ZERO,
  317. 0);
  318. if (!page)
  319. goto out_free;
  320. per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
  321. }
  322. break;
  323. out_free:
  324. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
  325. per_cpu(cpu_profile_hits, cpu)[1] = NULL;
  326. __free_page(page);
  327. return NOTIFY_BAD;
  328. case CPU_ONLINE:
  329. case CPU_ONLINE_FROZEN:
  330. cpu_set(cpu, prof_cpu_mask);
  331. break;
  332. case CPU_UP_CANCELED:
  333. case CPU_UP_CANCELED_FROZEN:
  334. case CPU_DEAD:
  335. case CPU_DEAD_FROZEN:
  336. cpu_clear(cpu, prof_cpu_mask);
  337. if (per_cpu(cpu_profile_hits, cpu)[0]) {
  338. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
  339. per_cpu(cpu_profile_hits, cpu)[0] = NULL;
  340. __free_page(page);
  341. }
  342. if (per_cpu(cpu_profile_hits, cpu)[1]) {
  343. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
  344. per_cpu(cpu_profile_hits, cpu)[1] = NULL;
  345. __free_page(page);
  346. }
  347. break;
  348. }
  349. return NOTIFY_OK;
  350. }
  351. #else /* !CONFIG_SMP */
  352. #define profile_flip_buffers() do { } while (0)
  353. #define profile_discard_flip_buffers() do { } while (0)
  354. #define profile_cpu_callback NULL
  355. void profile_hits(int type, void *__pc, unsigned int nr_hits)
  356. {
  357. unsigned long pc;
  358. if (prof_on != type || !prof_buffer)
  359. return;
  360. pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
  361. atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
  362. }
  363. #endif /* !CONFIG_SMP */
  364. EXPORT_SYMBOL_GPL(profile_hits);
  365. void profile_tick(int type)
  366. {
  367. struct pt_regs *regs = get_irq_regs();
  368. if (type == CPU_PROFILING && timer_hook)
  369. timer_hook(regs);
  370. if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
  371. profile_hit(type, (void *)profile_pc(regs));
  372. }
  373. #ifdef CONFIG_PROC_FS
  374. #include <linux/proc_fs.h>
  375. #include <asm/uaccess.h>
  376. #include <asm/ptrace.h>
  377. static int prof_cpu_mask_read_proc(char *page, char **start, off_t off,
  378. int count, int *eof, void *data)
  379. {
  380. int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
  381. if (count - len < 2)
  382. return -EINVAL;
  383. len += sprintf(page + len, "\n");
  384. return len;
  385. }
  386. static int prof_cpu_mask_write_proc(struct file *file,
  387. const char __user *buffer, unsigned long count, void *data)
  388. {
  389. cpumask_t *mask = (cpumask_t *)data;
  390. unsigned long full_count = count, err;
  391. cpumask_t new_value;
  392. err = cpumask_parse_user(buffer, count, new_value);
  393. if (err)
  394. return err;
  395. *mask = new_value;
  396. return full_count;
  397. }
  398. void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
  399. {
  400. struct proc_dir_entry *entry;
  401. /* create /proc/irq/prof_cpu_mask */
  402. entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
  403. if (!entry)
  404. return;
  405. entry->data = (void *)&prof_cpu_mask;
  406. entry->read_proc = prof_cpu_mask_read_proc;
  407. entry->write_proc = prof_cpu_mask_write_proc;
  408. }
  409. /*
  410. * This function accesses profiling information. The returned data is
  411. * binary: the sampling step and the actual contents of the profile
  412. * buffer. Use of the program readprofile is recommended in order to
  413. * get meaningful info out of these data.
  414. */
  415. static ssize_t
  416. read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  417. {
  418. unsigned long p = *ppos;
  419. ssize_t read;
  420. char *pnt;
  421. unsigned int sample_step = 1 << prof_shift;
  422. profile_flip_buffers();
  423. if (p >= (prof_len+1)*sizeof(unsigned int))
  424. return 0;
  425. if (count > (prof_len+1)*sizeof(unsigned int) - p)
  426. count = (prof_len+1)*sizeof(unsigned int) - p;
  427. read = 0;
  428. while (p < sizeof(unsigned int) && count > 0) {
  429. if (put_user(*((char *)(&sample_step)+p), buf))
  430. return -EFAULT;
  431. buf++; p++; count--; read++;
  432. }
  433. pnt = (char *)prof_buffer + p - sizeof(atomic_t);
  434. if (copy_to_user(buf, (void *)pnt, count))
  435. return -EFAULT;
  436. read += count;
  437. *ppos += read;
  438. return read;
  439. }
  440. /*
  441. * Writing to /proc/profile resets the counters
  442. *
  443. * Writing a 'profiling multiplier' value into it also re-sets the profiling
  444. * interrupt frequency, on architectures that support this.
  445. */
  446. static ssize_t write_profile(struct file *file, const char __user *buf,
  447. size_t count, loff_t *ppos)
  448. {
  449. #ifdef CONFIG_SMP
  450. extern int setup_profiling_timer(unsigned int multiplier);
  451. if (count == sizeof(int)) {
  452. unsigned int multiplier;
  453. if (copy_from_user(&multiplier, buf, sizeof(int)))
  454. return -EFAULT;
  455. if (setup_profiling_timer(multiplier))
  456. return -EINVAL;
  457. }
  458. #endif
  459. profile_discard_flip_buffers();
  460. memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
  461. return count;
  462. }
  463. static const struct file_operations proc_profile_operations = {
  464. .read = read_profile,
  465. .write = write_profile,
  466. };
  467. #ifdef CONFIG_SMP
  468. static void __init profile_nop(void *unused)
  469. {
  470. }
  471. static int __init create_hash_tables(void)
  472. {
  473. int cpu;
  474. for_each_online_cpu(cpu) {
  475. int node = cpu_to_node(cpu);
  476. struct page *page;
  477. page = alloc_pages_node(node,
  478. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  479. 0);
  480. if (!page)
  481. goto out_cleanup;
  482. per_cpu(cpu_profile_hits, cpu)[1]
  483. = (struct profile_hit *)page_address(page);
  484. page = alloc_pages_node(node,
  485. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  486. 0);
  487. if (!page)
  488. goto out_cleanup;
  489. per_cpu(cpu_profile_hits, cpu)[0]
  490. = (struct profile_hit *)page_address(page);
  491. }
  492. return 0;
  493. out_cleanup:
  494. prof_on = 0;
  495. smp_mb();
  496. on_each_cpu(profile_nop, NULL, 1);
  497. for_each_online_cpu(cpu) {
  498. struct page *page;
  499. if (per_cpu(cpu_profile_hits, cpu)[0]) {
  500. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
  501. per_cpu(cpu_profile_hits, cpu)[0] = NULL;
  502. __free_page(page);
  503. }
  504. if (per_cpu(cpu_profile_hits, cpu)[1]) {
  505. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
  506. per_cpu(cpu_profile_hits, cpu)[1] = NULL;
  507. __free_page(page);
  508. }
  509. }
  510. return -1;
  511. }
  512. #else
  513. #define create_hash_tables() ({ 0; })
  514. #endif
  515. static int __init create_proc_profile(void)
  516. {
  517. struct proc_dir_entry *entry;
  518. if (!prof_on)
  519. return 0;
  520. if (create_hash_tables())
  521. return -1;
  522. entry = proc_create("profile", S_IWUSR | S_IRUGO,
  523. NULL, &proc_profile_operations);
  524. if (!entry)
  525. return 0;
  526. entry->size = (1+prof_len) * sizeof(atomic_t);
  527. hotcpu_notifier(profile_cpu_callback, 0);
  528. return 0;
  529. }
  530. module_init(create_proc_profile);
  531. #endif /* CONFIG_PROC_FS */