profile.c 15 KB

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