profile.c 16 KB

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