xref: /dragonfly/sys/sys/indefinite2.h (revision 207ba670)
1 /*
2  * Copyright (c) 2017 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #ifndef _SYS_INDEFINITE2_H_
35 #define _SYS_INDEFINITE2_H_
36 
37 /*
38  * Indefinite info collection and handling code for contention loops
39  */
40 #ifndef _SYS_INDEFINITE_H_
41 #include <sys/indefinite.h>
42 #endif
43 #ifndef _SYS_GLOBALDATA_H_
44 #include <sys/globaldata.h>
45 #endif
46 
47 /*
48  * Initialize the indefinite state (only if the TSC is supported)
49  */
50 static __inline void
51 indefinite_init(indefinite_info_t *info, const char *ident, char now, char type)
52 {
53 	info->ident = ident;
54 	info->secs = 0;
55 	info->count = 0;
56 	info->reported = now;
57 
58 	if (tsc_frequency) {
59 		info->type = type;
60 		/* info->base = rdtsc(); (see indefinite_check()) */
61 	} else {
62 		info->type = 0;
63 		info->base = 0;
64 	}
65 	if (now && info->ident) {
66 		mycpu->gd_cnt.v_lock_name[0] = info->type;
67 		strncpy(mycpu->gd_cnt.v_lock_name + 1, info->ident,
68 			sizeof(mycpu->gd_cnt.v_lock_name) - 2);
69 	}
70 }
71 
72 /*
73  * Update the state during any loop, record collision time in microseconds.
74  */
75 static __inline int
76 indefinite_check(indefinite_info_t *info)
77 {
78 	tsc_uclock_t delta;
79 	const char *str;
80 
81 #ifdef _KERNEL_VIRTUAL
82 	vkernel_yield();
83 #else
84 	cpu_pause();
85 #endif
86 	if (info->type == 0)
87 		return FALSE;
88 	if (info->count == INDEF_INFO_START) {	/* start recording time */
89 		info->base = rdtsc();
90 		if (info->reported == 0 && info->ident) {
91 			mycpu->gd_cnt.v_lock_name[0] = info->type;
92 			strncpy(mycpu->gd_cnt.v_lock_name + 1, info->ident,
93 				sizeof(mycpu->gd_cnt.v_lock_name) - 2);
94 			info->reported = 1;
95 		}
96 	}
97 	if ((++info->count & 127) != 127)
98 		return FALSE;
99 	info->count = 128;
100 	delta = rdtsc() - info->base;
101 
102 #if defined(INVARIANTS)
103 	if (lock_test_mode > 0) {
104 		--lock_test_mode;
105 		print_backtrace(8);
106 	}
107 #endif
108 
109 	/*
110 	 * Ignore minor one-second interval error accumulation in
111 	 * favor of ensuring that info->base is fully synchronized.
112 	 */
113 	if (delta >= tsc_frequency) {
114 		info->secs += delta / tsc_frequency;
115 		info->base += delta;
116 		mycpu->gd_cnt.v_lock_colls += delta / tsc_frequency * 1000000U;
117 
118 		switch(info->type) {
119 		case 's':
120 			str = "spin_lock_sh";
121 			break;
122 		case 'S':
123 			str = "spin_lock_ex";
124 			break;
125 		case 'm':
126 			str = "mutex_sh";
127 			break;
128 		case 'M':
129 			str = "mutex_ex";
130 			break;
131 		case 'l':
132 			str = "lock_sh";
133 			break;
134 		case 'L':
135 			str = "lock_ex";
136 			break;
137 		case 't':
138 			str = "token";
139 			break;
140 		default:
141 			str = "lock(?)";
142 			break;
143 		}
144 		kprintf("%s: %s, indefinite wait (%d secs)!\n",
145 			str, info->ident, info->secs);
146 		if (panicstr)
147 			return TRUE;
148 #if defined(INVARIANTS)
149 		if (lock_test_mode) {
150 			print_backtrace(-1);
151 			return TRUE;
152 		}
153 #endif
154 #if defined(INVARIANTS)
155 		if (info->secs == 11 &&
156 		    (info->type == 's' || info->type == 'S')) {
157 			print_backtrace(-1);
158 		}
159 #endif
160 		if (info->secs == 60 &&
161 		    (info->type == 's' || info->type == 'S')) {
162 			panic("%s: %s, indefinite wait!", str, info->ident);
163 		}
164 
165 	}
166 	return FALSE;
167 }
168 
169 /*
170  * Finalize the state, record collision time in microseconds if
171  * we got past the initial load.
172  */
173 static __inline void
174 indefinite_done(indefinite_info_t *info)
175 {
176 	tsc_uclock_t delta;
177 	globaldata_t gd;
178 
179 	if (info->type && info->count > INDEF_INFO_START) {
180 		gd = mycpu;
181 		delta = rdtsc() - info->base;
182 		delta = delta * 1000000U / tsc_frequency;
183 		gd->gd_cnt.v_lock_colls += delta;
184 	}
185 	info->type = 0;
186 }
187 
188 #endif
189