profile.c 15 KB

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