profile.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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,
  274. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  275. 0);
  276. if (!page)
  277. return NOTIFY_BAD;
  278. per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
  279. }
  280. if (!per_cpu(cpu_profile_hits, cpu)[0]) {
  281. page = alloc_pages_node(node,
  282. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  283. 0);
  284. if (!page)
  285. goto out_free;
  286. per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
  287. }
  288. break;
  289. out_free:
  290. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
  291. per_cpu(cpu_profile_hits, cpu)[1] = NULL;
  292. __free_page(page);
  293. return NOTIFY_BAD;
  294. case CPU_ONLINE:
  295. cpu_set(cpu, prof_cpu_mask);
  296. break;
  297. case CPU_UP_CANCELED:
  298. case CPU_DEAD:
  299. cpu_clear(cpu, prof_cpu_mask);
  300. if (per_cpu(cpu_profile_hits, cpu)[0]) {
  301. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
  302. per_cpu(cpu_profile_hits, cpu)[0] = NULL;
  303. __free_page(page);
  304. }
  305. if (per_cpu(cpu_profile_hits, cpu)[1]) {
  306. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
  307. per_cpu(cpu_profile_hits, cpu)[1] = NULL;
  308. __free_page(page);
  309. }
  310. break;
  311. }
  312. return NOTIFY_OK;
  313. }
  314. #endif /* CONFIG_HOTPLUG_CPU */
  315. #else /* !CONFIG_SMP */
  316. #define profile_flip_buffers() do { } while (0)
  317. #define profile_discard_flip_buffers() do { } while (0)
  318. void profile_hit(int type, void *__pc)
  319. {
  320. unsigned long pc;
  321. if (prof_on != type || !prof_buffer)
  322. return;
  323. pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
  324. atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
  325. }
  326. #endif /* !CONFIG_SMP */
  327. void profile_tick(int type, struct pt_regs *regs)
  328. {
  329. if (type == CPU_PROFILING && timer_hook)
  330. timer_hook(regs);
  331. if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
  332. profile_hit(type, (void *)profile_pc(regs));
  333. }
  334. #ifdef CONFIG_PROC_FS
  335. #include <linux/proc_fs.h>
  336. #include <asm/uaccess.h>
  337. #include <asm/ptrace.h>
  338. static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
  339. int count, int *eof, void *data)
  340. {
  341. int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
  342. if (count - len < 2)
  343. return -EINVAL;
  344. len += sprintf(page + len, "\n");
  345. return len;
  346. }
  347. static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
  348. unsigned long count, void *data)
  349. {
  350. cpumask_t *mask = (cpumask_t *)data;
  351. unsigned long full_count = count, err;
  352. cpumask_t new_value;
  353. err = cpumask_parse(buffer, count, new_value);
  354. if (err)
  355. return err;
  356. *mask = new_value;
  357. return full_count;
  358. }
  359. void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
  360. {
  361. struct proc_dir_entry *entry;
  362. /* create /proc/irq/prof_cpu_mask */
  363. if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
  364. return;
  365. entry->nlink = 1;
  366. entry->data = (void *)&prof_cpu_mask;
  367. entry->read_proc = prof_cpu_mask_read_proc;
  368. entry->write_proc = prof_cpu_mask_write_proc;
  369. }
  370. /*
  371. * This function accesses profiling information. The returned data is
  372. * binary: the sampling step and the actual contents of the profile
  373. * buffer. Use of the program readprofile is recommended in order to
  374. * get meaningful info out of these data.
  375. */
  376. static ssize_t
  377. read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  378. {
  379. unsigned long p = *ppos;
  380. ssize_t read;
  381. char * pnt;
  382. unsigned int sample_step = 1 << prof_shift;
  383. profile_flip_buffers();
  384. if (p >= (prof_len+1)*sizeof(unsigned int))
  385. return 0;
  386. if (count > (prof_len+1)*sizeof(unsigned int) - p)
  387. count = (prof_len+1)*sizeof(unsigned int) - p;
  388. read = 0;
  389. while (p < sizeof(unsigned int) && count > 0) {
  390. put_user(*((char *)(&sample_step)+p),buf);
  391. buf++; p++; count--; read++;
  392. }
  393. pnt = (char *)prof_buffer + p - sizeof(atomic_t);
  394. if (copy_to_user(buf,(void *)pnt,count))
  395. return -EFAULT;
  396. read += count;
  397. *ppos += read;
  398. return read;
  399. }
  400. /*
  401. * Writing to /proc/profile resets the counters
  402. *
  403. * Writing a 'profiling multiplier' value into it also re-sets the profiling
  404. * interrupt frequency, on architectures that support this.
  405. */
  406. static ssize_t write_profile(struct file *file, const char __user *buf,
  407. size_t count, loff_t *ppos)
  408. {
  409. #ifdef CONFIG_SMP
  410. extern int setup_profiling_timer (unsigned int multiplier);
  411. if (count == sizeof(int)) {
  412. unsigned int multiplier;
  413. if (copy_from_user(&multiplier, buf, sizeof(int)))
  414. return -EFAULT;
  415. if (setup_profiling_timer(multiplier))
  416. return -EINVAL;
  417. }
  418. #endif
  419. profile_discard_flip_buffers();
  420. memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
  421. return count;
  422. }
  423. static struct file_operations proc_profile_operations = {
  424. .read = read_profile,
  425. .write = write_profile,
  426. };
  427. #ifdef CONFIG_SMP
  428. static void __init profile_nop(void *unused)
  429. {
  430. }
  431. static int __init create_hash_tables(void)
  432. {
  433. int cpu;
  434. for_each_online_cpu(cpu) {
  435. int node = cpu_to_node(cpu);
  436. struct page *page;
  437. page = alloc_pages_node(node,
  438. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  439. 0);
  440. if (!page)
  441. goto out_cleanup;
  442. per_cpu(cpu_profile_hits, cpu)[1]
  443. = (struct profile_hit *)page_address(page);
  444. page = alloc_pages_node(node,
  445. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  446. 0);
  447. if (!page)
  448. goto out_cleanup;
  449. per_cpu(cpu_profile_hits, cpu)[0]
  450. = (struct profile_hit *)page_address(page);
  451. }
  452. return 0;
  453. out_cleanup:
  454. prof_on = 0;
  455. smp_mb();
  456. on_each_cpu(profile_nop, NULL, 0, 1);
  457. for_each_online_cpu(cpu) {
  458. struct page *page;
  459. if (per_cpu(cpu_profile_hits, cpu)[0]) {
  460. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
  461. per_cpu(cpu_profile_hits, cpu)[0] = NULL;
  462. __free_page(page);
  463. }
  464. if (per_cpu(cpu_profile_hits, cpu)[1]) {
  465. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
  466. per_cpu(cpu_profile_hits, cpu)[1] = NULL;
  467. __free_page(page);
  468. }
  469. }
  470. return -1;
  471. }
  472. #else
  473. #define create_hash_tables() ({ 0; })
  474. #endif
  475. static int __init create_proc_profile(void)
  476. {
  477. struct proc_dir_entry *entry;
  478. if (!prof_on)
  479. return 0;
  480. if (create_hash_tables())
  481. return -1;
  482. if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
  483. return 0;
  484. entry->proc_fops = &proc_profile_operations;
  485. entry->size = (1+prof_len) * sizeof(atomic_t);
  486. hotcpu_notifier(profile_cpu_callback, 0);
  487. return 0;
  488. }
  489. module_init(create_proc_profile);
  490. #endif /* CONFIG_PROC_FS */