xref: /netbsd-src/sys/sys/syscall_stats.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: syscall_stats.h,v 1.2 2007/11/11 17:48:51 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by David Laight.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _SYS_SYCALL_STAT_H_
36 #define	_SYS_SYCALL_STAT_H_
37 
38 #ifndef _LKM
39 #include "opt_syscall_stats.h"
40 #endif
41 
42 #ifdef SYSCALL_STATS
43 #include <sys/syscall.h>
44 
45 extern uint64_t syscall_counts[SYS_NSYSENT];
46 extern uint64_t syscall_count_user, syscall_count_system, syscall_count_interrupt;
47 #define	SYSCALL_COUNT(table, code) ((table)[code]++)
48 
49 #if defined(SYSCALL_TIMES) && defined(__HAVE_CPU_COUNTER)
50 
51 #include <machine/cpu_counter.h>
52 extern uint64_t syscall_times[SYS_NSYSENT];
53 
54 #ifdef SYSCALL_TIMES_HASCOUNTER
55 /* Force use of cycle counter - needed for Soekris systems */
56 #define SYSCALL_TIME() (cpu_counter32())
57 #else
58 #define SYSCALL_TIME() (cpu_hascounter() ? cpu_counter32() : 0u)
59 #endif
60 
61 #ifdef SYSCALL_TIMES_PROCTIMES
62 #define SYSCALL_TIME_UPDATE_PROC(l, fld, delta) \
63 	(l)->l_proc->p_##fld##ticks += (delta)
64 #else
65 #define SYSCALL_TIME_UPDATE_PROC(l, fld, delta)
66 #endif
67 
68 /* lwp creation */
69 #define SYSCALL_TIME_LWP_INIT(l) do { \
70     (l)->l_syscall_counter = &syscall_count_system; \
71     SYSCALL_TIME_WAKEUP(l); \
72 } while (0)
73 
74 /* System call entry hook */
75 #define SYSCALL_TIME_SYS_ENTRY(l, table, code) do { \
76 	uint32_t now = SYSCALL_TIME(); \
77 	SYSCALL_TIME_UPDATE_PROC(l, u, elapsed = now - (l)->l_syscall_time); \
78 	(l)->l_syscall_counter = (table) + (code); \
79 	(l)->l_syscall_time = now; \
80 } while (0)
81 
82 /* System call - process sleep */
83 #define SYSCALL_TIME_SLEEP(l) do { \
84 	uint32_t now = SYSCALL_TIME(); \
85 	uint32_t elapsed = now - (l)->l_syscall_time; \
86 	(l)->l_syscall_time = now; \
87 	*(l)->l_syscall_counter += elapsed; \
88 	SYSCALL_TIME_UPDATE_PROC(l, s, elapsed); \
89 } while (0)
90 
91 /* Process wakeup */
92 #define SYSCALL_TIME_WAKEUP(l) \
93 	(l)->l_syscall_time = SYSCALL_TIME()
94 
95 /* System call exit */
96 #define SYSCALL_TIME_SYS_EXIT(l) do { \
97 	uint32_t now = SYSCALL_TIME(); \
98 	uint32_t elapsed = now - (l)->l_syscall_time; \
99 	(l)->l_syscall_time = now; \
100 	*(l)->l_syscall_counter += elapsed; \
101 	(l)->l_syscall_counter = &syscall_count_user; \
102 	SYSCALL_TIME_UPDATE_PROC(l, s, elapsed); \
103 } while (0)
104 
105 #ifdef _notyet
106 /* Interrupt entry hook */
107 #define SYSCALL_TIME_ISR_ENTRY(l, old) do { \
108 	uint32_t now = SYSCALL_TIME(); \
109 	uint32_t elapsed = now - (l)->l_syscall_time; \
110 	(l)->l_syscall_time = now; \
111 	old = (l)->l_syscall_counter; \
112 	if ((l)->l_syscall_counter != &syscall_count_interrupt) \
113 		if ((l)->l_syscall_counter == &syscall_count_user) \
114 			SYSCALL_TIME_UPDATE_PROC(l, u, elapsed); \
115 		else { \
116 			*(l)->l_syscall_counter += elapsed; \
117 			SYSCALL_TIME_UPDATE_PROC(l, s, elapsed); \
118 		} \
119 		(l)->l_syscall_counter = &syscall_count_interrupt; \
120 	} \
121 } while (0)
122 
123 /* Interrupt exit hook */
124 #define SYSCALL_TIME_ISR_EXIT(l, saved) do { \
125 	uint32_t now = SYSCALL_TIME(); \
126 	SYSCALL_TIME_UPDATE_PROC(l, i, now - (l)->l_syscall_time); \
127 	(l)->l_syscall_time = now; \
128 	(l)->l_syscall_counter = saved; \
129 } while (0)
130 #endif
131 
132 #endif
133 #endif
134 
135 #ifndef SYSCALL_TIME_SYS_ENTRY
136 #define SYSCALL_TIME_LWP_INIT(l)
137 #define SYSCALL_TIME_SYS_ENTRY(l,table,code)
138 #define SYSCALL_TIME_SLEEP(l)
139 #define SYSCALL_TIME_WAKEUP(l)
140 #define SYSCALL_TIME_SYS_EXIT(l)
141 #define SYSCALL_TIME_ISR_ENTRY(l,old)
142 #define SYSCALL_TIME_ISR_EXIT(l,saved)
143 #undef SYSCALL_TIMES
144 #endif
145 
146 #ifndef SYSCALL_COUNT
147 #define SYSCALL_COUNT(table, code)
148 #endif
149 
150 #endif /* !_SYS_SYCALL_STAT_H_ */
151