hvCall_inst.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2006 Mike Kravetz IBM Corporation
  3. *
  4. * Hypervisor Call Instrumentation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/percpu.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/cpumask.h>
  25. #include <asm/hvcall.h>
  26. #include <asm/firmware.h>
  27. #include <asm/cputable.h>
  28. #include <asm/trace.h>
  29. #include <asm/machdep.h>
  30. /* For hcall instrumentation. One structure per-hcall, per-CPU */
  31. struct hcall_stats {
  32. unsigned long num_calls; /* number of calls (on this CPU) */
  33. unsigned long tb_total; /* total wall time (mftb) of calls. */
  34. unsigned long purr_total; /* total cpu time (PURR) of calls. */
  35. unsigned long tb_start;
  36. unsigned long purr_start;
  37. };
  38. #define HCALL_STAT_ARRAY_SIZE ((MAX_HCALL_OPCODE >> 2) + 1)
  39. DEFINE_PER_CPU(struct hcall_stats[HCALL_STAT_ARRAY_SIZE], hcall_stats);
  40. /*
  41. * Routines for displaying the statistics in debugfs
  42. */
  43. static void *hc_start(struct seq_file *m, loff_t *pos)
  44. {
  45. if ((int)*pos < (HCALL_STAT_ARRAY_SIZE-1))
  46. return (void *)(unsigned long)(*pos + 1);
  47. return NULL;
  48. }
  49. static void *hc_next(struct seq_file *m, void *p, loff_t * pos)
  50. {
  51. ++*pos;
  52. return hc_start(m, pos);
  53. }
  54. static void hc_stop(struct seq_file *m, void *p)
  55. {
  56. }
  57. static int hc_show(struct seq_file *m, void *p)
  58. {
  59. unsigned long h_num = (unsigned long)p;
  60. struct hcall_stats *hs = m->private;
  61. if (hs[h_num].num_calls) {
  62. if (cpu_has_feature(CPU_FTR_PURR))
  63. seq_printf(m, "%lu %lu %lu %lu\n", h_num<<2,
  64. hs[h_num].num_calls,
  65. hs[h_num].tb_total,
  66. hs[h_num].purr_total);
  67. else
  68. seq_printf(m, "%lu %lu %lu\n", h_num<<2,
  69. hs[h_num].num_calls,
  70. hs[h_num].tb_total);
  71. }
  72. return 0;
  73. }
  74. static const struct seq_operations hcall_inst_seq_ops = {
  75. .start = hc_start,
  76. .next = hc_next,
  77. .stop = hc_stop,
  78. .show = hc_show
  79. };
  80. static int hcall_inst_seq_open(struct inode *inode, struct file *file)
  81. {
  82. int rc;
  83. struct seq_file *seq;
  84. rc = seq_open(file, &hcall_inst_seq_ops);
  85. seq = file->private_data;
  86. seq->private = file_inode(file)->i_private;
  87. return rc;
  88. }
  89. static const struct file_operations hcall_inst_seq_fops = {
  90. .open = hcall_inst_seq_open,
  91. .read = seq_read,
  92. .llseek = seq_lseek,
  93. .release = seq_release,
  94. };
  95. #define HCALL_ROOT_DIR "hcall_inst"
  96. #define CPU_NAME_BUF_SIZE 32
  97. static void probe_hcall_entry(void *ignored, unsigned long opcode, unsigned long *args)
  98. {
  99. struct hcall_stats *h;
  100. if (opcode > MAX_HCALL_OPCODE)
  101. return;
  102. h = this_cpu_ptr(&hcall_stats[opcode / 4]);
  103. h->tb_start = mftb();
  104. h->purr_start = mfspr(SPRN_PURR);
  105. }
  106. static void probe_hcall_exit(void *ignored, unsigned long opcode, long retval,
  107. unsigned long *retbuf)
  108. {
  109. struct hcall_stats *h;
  110. if (opcode > MAX_HCALL_OPCODE)
  111. return;
  112. h = this_cpu_ptr(&hcall_stats[opcode / 4]);
  113. h->num_calls++;
  114. h->tb_total += mftb() - h->tb_start;
  115. h->purr_total += mfspr(SPRN_PURR) - h->purr_start;
  116. }
  117. static int __init hcall_inst_init(void)
  118. {
  119. struct dentry *hcall_root;
  120. struct dentry *hcall_file;
  121. char cpu_name_buf[CPU_NAME_BUF_SIZE];
  122. int cpu;
  123. if (!firmware_has_feature(FW_FEATURE_LPAR))
  124. return 0;
  125. if (register_trace_hcall_entry(probe_hcall_entry, NULL))
  126. return -EINVAL;
  127. if (register_trace_hcall_exit(probe_hcall_exit, NULL)) {
  128. unregister_trace_hcall_entry(probe_hcall_entry, NULL);
  129. return -EINVAL;
  130. }
  131. hcall_root = debugfs_create_dir(HCALL_ROOT_DIR, NULL);
  132. if (!hcall_root)
  133. return -ENOMEM;
  134. for_each_possible_cpu(cpu) {
  135. snprintf(cpu_name_buf, CPU_NAME_BUF_SIZE, "cpu%d", cpu);
  136. hcall_file = debugfs_create_file(cpu_name_buf, 0444,
  137. hcall_root,
  138. per_cpu(hcall_stats, cpu),
  139. &hcall_inst_seq_fops);
  140. if (!hcall_file)
  141. return -ENOMEM;
  142. }
  143. return 0;
  144. }
  145. machine_device_initcall(pseries, hcall_inst_init);