profile.c 16 KB

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