profile.c 16 KB

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