profile.c 15 KB

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