profile.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. * Nadia Yvette Chambers, Oracle, July 2004
  12. * Amortized hit count accounting via per-cpu open-addressed hashtables
  13. * to resolve timer interrupt livelocks, Nadia Yvette Chambers,
  14. * Oracle, 2004
  15. */
  16. #include <linux/export.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/highmem.h>
  24. #include <linux/mutex.h>
  25. #include <linux/slab.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/sched/stat.h>
  28. #include <asm/sections.h>
  29. #include <asm/irq_regs.h>
  30. #include <asm/ptrace.h>
  31. struct profile_hit {
  32. u32 pc, hits;
  33. };
  34. #define PROFILE_GRPSHIFT 3
  35. #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
  36. #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
  37. #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
  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_var_t prof_cpu_mask;
  43. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  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. int profile_setup(char *str)
  49. {
  50. static const char schedstr[] = "schedule";
  51. static const char sleepstr[] = "sleep";
  52. static const char kvmstr[] = "kvm";
  53. int par;
  54. if (!strncmp(str, sleepstr, strlen(sleepstr))) {
  55. #ifdef CONFIG_SCHEDSTATS
  56. force_schedstat_enabled();
  57. prof_on = SLEEP_PROFILING;
  58. if (str[strlen(sleepstr)] == ',')
  59. str += strlen(sleepstr) + 1;
  60. if (get_option(&str, &par))
  61. prof_shift = par;
  62. pr_info("kernel sleep profiling enabled (shift: %ld)\n",
  63. prof_shift);
  64. #else
  65. pr_warn("kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
  66. #endif /* CONFIG_SCHEDSTATS */
  67. } else if (!strncmp(str, schedstr, strlen(schedstr))) {
  68. prof_on = SCHED_PROFILING;
  69. if (str[strlen(schedstr)] == ',')
  70. str += strlen(schedstr) + 1;
  71. if (get_option(&str, &par))
  72. prof_shift = par;
  73. pr_info("kernel schedule profiling enabled (shift: %ld)\n",
  74. prof_shift);
  75. } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
  76. prof_on = KVM_PROFILING;
  77. if (str[strlen(kvmstr)] == ',')
  78. str += strlen(kvmstr) + 1;
  79. if (get_option(&str, &par))
  80. prof_shift = par;
  81. pr_info("kernel KVM profiling enabled (shift: %ld)\n",
  82. prof_shift);
  83. } else if (get_option(&str, &par)) {
  84. prof_shift = par;
  85. prof_on = CPU_PROFILING;
  86. pr_info("kernel profiling enabled (shift: %ld)\n",
  87. prof_shift);
  88. }
  89. return 1;
  90. }
  91. __setup("profile=", profile_setup);
  92. int __ref profile_init(void)
  93. {
  94. int buffer_bytes;
  95. if (!prof_on)
  96. return 0;
  97. /* only text is profiled */
  98. prof_len = (_etext - _stext) >> prof_shift;
  99. buffer_bytes = prof_len*sizeof(atomic_t);
  100. if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
  101. return -ENOMEM;
  102. cpumask_copy(prof_cpu_mask, cpu_possible_mask);
  103. prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
  104. if (prof_buffer)
  105. return 0;
  106. prof_buffer = alloc_pages_exact(buffer_bytes,
  107. GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
  108. if (prof_buffer)
  109. return 0;
  110. prof_buffer = vzalloc(buffer_bytes);
  111. if (prof_buffer)
  112. return 0;
  113. free_cpumask_var(prof_cpu_mask);
  114. return -ENOMEM;
  115. }
  116. /* Profile event notifications */
  117. static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
  118. static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
  119. static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
  120. void profile_task_exit(struct task_struct *task)
  121. {
  122. blocking_notifier_call_chain(&task_exit_notifier, 0, task);
  123. }
  124. int profile_handoff_task(struct task_struct *task)
  125. {
  126. int ret;
  127. ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
  128. return (ret == NOTIFY_OK) ? 1 : 0;
  129. }
  130. void profile_munmap(unsigned long addr)
  131. {
  132. blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
  133. }
  134. int task_handoff_register(struct notifier_block *n)
  135. {
  136. return atomic_notifier_chain_register(&task_free_notifier, n);
  137. }
  138. EXPORT_SYMBOL_GPL(task_handoff_register);
  139. int task_handoff_unregister(struct notifier_block *n)
  140. {
  141. return atomic_notifier_chain_unregister(&task_free_notifier, n);
  142. }
  143. EXPORT_SYMBOL_GPL(task_handoff_unregister);
  144. int profile_event_register(enum profile_type type, struct notifier_block *n)
  145. {
  146. int err = -EINVAL;
  147. switch (type) {
  148. case PROFILE_TASK_EXIT:
  149. err = blocking_notifier_chain_register(
  150. &task_exit_notifier, n);
  151. break;
  152. case PROFILE_MUNMAP:
  153. err = blocking_notifier_chain_register(
  154. &munmap_notifier, n);
  155. break;
  156. }
  157. return err;
  158. }
  159. EXPORT_SYMBOL_GPL(profile_event_register);
  160. int profile_event_unregister(enum profile_type type, struct notifier_block *n)
  161. {
  162. int err = -EINVAL;
  163. switch (type) {
  164. case PROFILE_TASK_EXIT:
  165. err = blocking_notifier_chain_unregister(
  166. &task_exit_notifier, n);
  167. break;
  168. case PROFILE_MUNMAP:
  169. err = blocking_notifier_chain_unregister(
  170. &munmap_notifier, n);
  171. break;
  172. }
  173. return err;
  174. }
  175. EXPORT_SYMBOL_GPL(profile_event_unregister);
  176. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  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. * -- nyc
  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, 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, 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. static void do_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. pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
  253. i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
  254. secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
  255. cpu = get_cpu();
  256. hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
  257. if (!hits) {
  258. put_cpu();
  259. return;
  260. }
  261. /*
  262. * We buffer the global profiler buffer into a per-CPU
  263. * queue and thus reduce the number of global (and possibly
  264. * NUMA-alien) accesses. The write-queue is self-coalescing:
  265. */
  266. local_irq_save(flags);
  267. do {
  268. for (j = 0; j < PROFILE_GRPSZ; ++j) {
  269. if (hits[i + j].pc == pc) {
  270. hits[i + j].hits += nr_hits;
  271. goto out;
  272. } else if (!hits[i + j].hits) {
  273. hits[i + j].pc = pc;
  274. hits[i + j].hits = nr_hits;
  275. goto out;
  276. }
  277. }
  278. i = (i + secondary) & (NR_PROFILE_HIT - 1);
  279. } while (i != primary);
  280. /*
  281. * Add the current hit(s) and flush the write-queue out
  282. * to the global buffer:
  283. */
  284. atomic_add(nr_hits, &prof_buffer[pc]);
  285. for (i = 0; i < NR_PROFILE_HIT; ++i) {
  286. atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
  287. hits[i].pc = hits[i].hits = 0;
  288. }
  289. out:
  290. local_irq_restore(flags);
  291. put_cpu();
  292. }
  293. static int profile_dead_cpu(unsigned int cpu)
  294. {
  295. struct page *page;
  296. int i;
  297. if (prof_cpu_mask != NULL)
  298. cpumask_clear_cpu(cpu, prof_cpu_mask);
  299. for (i = 0; i < 2; i++) {
  300. if (per_cpu(cpu_profile_hits, cpu)[i]) {
  301. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]);
  302. per_cpu(cpu_profile_hits, cpu)[i] = NULL;
  303. __free_page(page);
  304. }
  305. }
  306. return 0;
  307. }
  308. static int profile_prepare_cpu(unsigned int cpu)
  309. {
  310. int i, node = cpu_to_mem(cpu);
  311. struct page *page;
  312. per_cpu(cpu_profile_flip, cpu) = 0;
  313. for (i = 0; i < 2; i++) {
  314. if (per_cpu(cpu_profile_hits, cpu)[i])
  315. continue;
  316. page = __alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  317. if (!page) {
  318. profile_dead_cpu(cpu);
  319. return -ENOMEM;
  320. }
  321. per_cpu(cpu_profile_hits, cpu)[i] = page_address(page);
  322. }
  323. return 0;
  324. }
  325. static int profile_online_cpu(unsigned int cpu)
  326. {
  327. if (prof_cpu_mask != NULL)
  328. cpumask_set_cpu(cpu, prof_cpu_mask);
  329. return 0;
  330. }
  331. #else /* !CONFIG_SMP */
  332. #define profile_flip_buffers() do { } while (0)
  333. #define profile_discard_flip_buffers() do { } while (0)
  334. static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
  335. {
  336. unsigned long pc;
  337. pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
  338. atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
  339. }
  340. #endif /* !CONFIG_SMP */
  341. void profile_hits(int type, void *__pc, unsigned int nr_hits)
  342. {
  343. if (prof_on != type || !prof_buffer)
  344. return;
  345. do_profile_hits(type, __pc, nr_hits);
  346. }
  347. EXPORT_SYMBOL_GPL(profile_hits);
  348. void profile_tick(int type)
  349. {
  350. struct pt_regs *regs = get_irq_regs();
  351. if (!user_mode(regs) && prof_cpu_mask != NULL &&
  352. cpumask_test_cpu(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 <linux/seq_file.h>
  358. #include <linux/uaccess.h>
  359. static int prof_cpu_mask_proc_show(struct seq_file *m, void *v)
  360. {
  361. seq_printf(m, "%*pb\n", cpumask_pr_args(prof_cpu_mask));
  362. return 0;
  363. }
  364. static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file)
  365. {
  366. return single_open(file, prof_cpu_mask_proc_show, NULL);
  367. }
  368. static ssize_t prof_cpu_mask_proc_write(struct file *file,
  369. const char __user *buffer, size_t count, loff_t *pos)
  370. {
  371. cpumask_var_t new_value;
  372. int err;
  373. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  374. return -ENOMEM;
  375. err = cpumask_parse_user(buffer, count, new_value);
  376. if (!err) {
  377. cpumask_copy(prof_cpu_mask, new_value);
  378. err = count;
  379. }
  380. free_cpumask_var(new_value);
  381. return err;
  382. }
  383. static const struct file_operations prof_cpu_mask_proc_fops = {
  384. .open = prof_cpu_mask_proc_open,
  385. .read = seq_read,
  386. .llseek = seq_lseek,
  387. .release = single_release,
  388. .write = prof_cpu_mask_proc_write,
  389. };
  390. void create_prof_cpu_mask(void)
  391. {
  392. /* create /proc/irq/prof_cpu_mask */
  393. proc_create("irq/prof_cpu_mask", 0600, NULL, &prof_cpu_mask_proc_fops);
  394. }
  395. /*
  396. * This function accesses profiling information. The returned data is
  397. * binary: the sampling step and the actual contents of the profile
  398. * buffer. Use of the program readprofile is recommended in order to
  399. * get meaningful info out of these data.
  400. */
  401. static ssize_t
  402. read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  403. {
  404. unsigned long p = *ppos;
  405. ssize_t read;
  406. char *pnt;
  407. unsigned int sample_step = 1 << prof_shift;
  408. profile_flip_buffers();
  409. if (p >= (prof_len+1)*sizeof(unsigned int))
  410. return 0;
  411. if (count > (prof_len+1)*sizeof(unsigned int) - p)
  412. count = (prof_len+1)*sizeof(unsigned int) - p;
  413. read = 0;
  414. while (p < sizeof(unsigned int) && count > 0) {
  415. if (put_user(*((char *)(&sample_step)+p), buf))
  416. return -EFAULT;
  417. buf++; p++; count--; read++;
  418. }
  419. pnt = (char *)prof_buffer + p - sizeof(atomic_t);
  420. if (copy_to_user(buf, (void *)pnt, count))
  421. return -EFAULT;
  422. read += count;
  423. *ppos += read;
  424. return read;
  425. }
  426. /*
  427. * Writing to /proc/profile resets the counters
  428. *
  429. * Writing a 'profiling multiplier' value into it also re-sets the profiling
  430. * interrupt frequency, on architectures that support this.
  431. */
  432. static ssize_t write_profile(struct file *file, const char __user *buf,
  433. size_t count, loff_t *ppos)
  434. {
  435. #ifdef CONFIG_SMP
  436. extern int setup_profiling_timer(unsigned int multiplier);
  437. if (count == sizeof(int)) {
  438. unsigned int multiplier;
  439. if (copy_from_user(&multiplier, buf, sizeof(int)))
  440. return -EFAULT;
  441. if (setup_profiling_timer(multiplier))
  442. return -EINVAL;
  443. }
  444. #endif
  445. profile_discard_flip_buffers();
  446. memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
  447. return count;
  448. }
  449. static const struct file_operations proc_profile_operations = {
  450. .read = read_profile,
  451. .write = write_profile,
  452. .llseek = default_llseek,
  453. };
  454. int __ref create_proc_profile(void)
  455. {
  456. struct proc_dir_entry *entry;
  457. #ifdef CONFIG_SMP
  458. enum cpuhp_state online_state;
  459. #endif
  460. int err = 0;
  461. if (!prof_on)
  462. return 0;
  463. #ifdef CONFIG_SMP
  464. err = cpuhp_setup_state(CPUHP_PROFILE_PREPARE, "PROFILE_PREPARE",
  465. profile_prepare_cpu, profile_dead_cpu);
  466. if (err)
  467. return err;
  468. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE",
  469. profile_online_cpu, NULL);
  470. if (err < 0)
  471. goto err_state_prep;
  472. online_state = err;
  473. err = 0;
  474. #endif
  475. entry = proc_create("profile", S_IWUSR | S_IRUGO,
  476. NULL, &proc_profile_operations);
  477. if (!entry)
  478. goto err_state_onl;
  479. proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
  480. return err;
  481. err_state_onl:
  482. #ifdef CONFIG_SMP
  483. cpuhp_remove_state(online_state);
  484. err_state_prep:
  485. cpuhp_remove_state(CPUHP_PROFILE_PREPARE);
  486. #endif
  487. return err;
  488. }
  489. subsys_initcall(create_proc_profile);
  490. #endif /* CONFIG_PROC_FS */