profile.c 16 KB

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