profile.c 15 KB

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