profile.c 16 KB

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