profile.c 15 KB

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