profile.c 15 KB

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