2 * Copyright (C) 2006 Mike Kravetz IBM Corporation
4 * Hypervisor Call Instrumentation
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.
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.
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
21 #include <linux/kernel.h>
22 #include <linux/percpu.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/cpumask.h>
26 #include <asm/hvcall.h>
27 #include <asm/firmware.h>
28 #include <asm/cputable.h>
29 #include <asm/trace.h>
30 #include <asm/machdep.h>
32 /* For hcall instrumentation. One structure per-hcall, per-CPU */
34 unsigned long num_calls
; /* number of calls (on this CPU) */
35 unsigned long tb_total
; /* total wall time (mftb) of calls. */
36 unsigned long purr_total
; /* total cpu time (PURR) of calls. */
37 unsigned long tb_start
;
38 unsigned long purr_start
;
40 #define HCALL_STAT_ARRAY_SIZE ((MAX_HCALL_OPCODE >> 2) + 1)
42 DEFINE_PER_CPU(struct hcall_stats
[HCALL_STAT_ARRAY_SIZE
], hcall_stats
);
45 * Routines for displaying the statistics in debugfs
47 static void *hc_start(struct seq_file
*m
, loff_t
*pos
)
49 if ((int)*pos
< (HCALL_STAT_ARRAY_SIZE
-1))
50 return (void *)(unsigned long)(*pos
+ 1);
55 static void *hc_next(struct seq_file
*m
, void *p
, loff_t
* pos
)
59 return hc_start(m
, pos
);
62 static void hc_stop(struct seq_file
*m
, void *p
)
66 static int hc_show(struct seq_file
*m
, void *p
)
68 unsigned long h_num
= (unsigned long)p
;
69 struct hcall_stats
*hs
= m
->private;
71 if (hs
[h_num
].num_calls
) {
72 if (cpu_has_feature(CPU_FTR_PURR
))
73 seq_printf(m
, "%lu %lu %lu %lu\n", h_num
<<2,
76 hs
[h_num
].purr_total
);
78 seq_printf(m
, "%lu %lu %lu\n", h_num
<<2,
86 static const struct seq_operations hcall_inst_seq_ops
= {
93 static int hcall_inst_seq_open(struct inode
*inode
, struct file
*file
)
98 rc
= seq_open(file
, &hcall_inst_seq_ops
);
99 seq
= file
->private_data
;
100 seq
->private = file_inode(file
)->i_private
;
105 static const struct file_operations hcall_inst_seq_fops
= {
106 .open
= hcall_inst_seq_open
,
109 .release
= seq_release
,
112 #define HCALL_ROOT_DIR "hcall_inst"
113 #define CPU_NAME_BUF_SIZE 32
116 static void probe_hcall_entry(void *ignored
, unsigned long opcode
, unsigned long *args
)
118 struct hcall_stats
*h
;
120 if (opcode
> MAX_HCALL_OPCODE
)
123 h
= this_cpu_ptr(&hcall_stats
[opcode
/ 4]);
124 h
->tb_start
= mftb();
125 h
->purr_start
= mfspr(SPRN_PURR
);
128 static void probe_hcall_exit(void *ignored
, unsigned long opcode
, unsigned long retval
,
129 unsigned long *retbuf
)
131 struct hcall_stats
*h
;
133 if (opcode
> MAX_HCALL_OPCODE
)
136 h
= this_cpu_ptr(&hcall_stats
[opcode
/ 4]);
138 h
->tb_total
+= mftb() - h
->tb_start
;
139 h
->purr_total
+= mfspr(SPRN_PURR
) - h
->purr_start
;
142 static int __init
hcall_inst_init(void)
144 struct dentry
*hcall_root
;
145 struct dentry
*hcall_file
;
146 char cpu_name_buf
[CPU_NAME_BUF_SIZE
];
149 if (!firmware_has_feature(FW_FEATURE_LPAR
))
152 if (register_trace_hcall_entry(probe_hcall_entry
, NULL
))
155 if (register_trace_hcall_exit(probe_hcall_exit
, NULL
)) {
156 unregister_trace_hcall_entry(probe_hcall_entry
, NULL
);
160 hcall_root
= debugfs_create_dir(HCALL_ROOT_DIR
, NULL
);
164 for_each_possible_cpu(cpu
) {
165 snprintf(cpu_name_buf
, CPU_NAME_BUF_SIZE
, "cpu%d", cpu
);
166 hcall_file
= debugfs_create_file(cpu_name_buf
, S_IRUGO
,
168 per_cpu(hcall_stats
, cpu
),
169 &hcall_inst_seq_fops
);
176 machine_device_initcall(pseries
, hcall_inst_init
);