PageRenderTime 1466ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/profile.c

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