kernel - Add fuwordadd32(), fuwordadd64()
[dragonfly.git] / sys / sys / indefinite2.h
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 #ifdef _KERNEL_VIRTUAL
47 #include <pthread.h>
48 #endif
49
50 /*
51  * Initialize the indefinite state (only if the TSC is supported)
52  */
53 static __inline void
54 indefinite_init(indefinite_info_t *info, const char *ident, char now, char type)
55 {
56         info->ident = ident;
57         info->secs = 0;
58         info->count = 0;
59         info->reported = now;
60
61         if (tsc_frequency) {
62                 info->type = type;
63                 /* info->base = rdtsc(); (see indefinite_check()) */
64         } else {
65                 info->type = 0;
66                 info->base = 0;
67         }
68         if (now && info->ident) {
69                 mycpu->gd_cnt.v_lock_name[0] = info->type;
70                 strncpy(mycpu->gd_cnt.v_lock_name + 1, info->ident,
71                         sizeof(mycpu->gd_cnt.v_lock_name) - 2);
72         }
73 }
74
75 /*
76  * Update the state during any loop, record collision time in microseconds.
77  */
78 static __inline int
79 indefinite_check(indefinite_info_t *info)
80 {
81         tsc_uclock_t delta;
82         const char *str;
83
84 #ifdef _KERNEL_VIRTUAL
85         pthread_yield();
86 #else
87         cpu_pause();
88 #endif
89         if (info->type == 0)
90                 return FALSE;
91         if (info->count == INDEF_INFO_START) {  /* start recording time */
92                 info->base = rdtsc();
93                 if (info->reported == 0 && info->ident) {
94                         mycpu->gd_cnt.v_lock_name[0] = info->type;
95                         strncpy(mycpu->gd_cnt.v_lock_name + 1, info->ident,
96                                 sizeof(mycpu->gd_cnt.v_lock_name) - 2);
97                         info->reported = 1;
98                 }
99         }
100         if ((++info->count & 127) != 127)
101                 return FALSE;
102         info->count = 128;
103         delta = rdtsc() - info->base;
104
105 #if defined(INVARIANTS)
106         if (lock_test_mode > 0) {
107                 --lock_test_mode;
108                 print_backtrace(8);
109         }
110 #endif
111
112         /*
113          * Ignore minor one-second interval error accumulation in
114          * favor of ensuring that info->base is fully synchronized.
115          */
116         if (delta >= tsc_frequency) {
117                 info->secs += delta / tsc_frequency;
118                 info->base += delta;
119                 mycpu->gd_cnt.v_lock_colls += delta / tsc_frequency * 1000000U;
120
121                 switch(info->type) {
122                 case 's':
123                         str = "spin_lock_sh";
124                         break;
125                 case 'S':
126                         str = "spin_lock_ex";
127                         break;
128                 case 'm':
129                         str = "mutex_sh";
130                         break;
131                 case 'M':
132                         str = "mutex_ex";
133                         break;
134                 case 'l':
135                         str = "lock_sh";
136                         break;
137                 case 'L':
138                         str = "lock_ex";
139                         break;
140                 case 't':
141                         str = "token";
142                         break;
143                 default:
144                         str = "lock(?)";
145                         break;
146                 }
147                 kprintf("%s: %s, indefinite wait (%d secs)!\n",
148                         str, info->ident, info->secs);
149                 if (panicstr)
150                         return TRUE;
151 #if defined(INVARIANTS)
152                 if (lock_test_mode) {
153                         print_backtrace(-1);
154                         return TRUE;
155                 }
156 #endif
157 #if defined(INVARIANTS)
158                 if (info->secs == 11 &&
159                     (info->type == 's' || info->type == 'S')) {
160                         print_backtrace(-1);
161                 }
162 #endif
163                 if (info->secs == 60 &&
164                     (info->type == 's' || info->type == 'S')) {
165                         panic("%s: %s, indefinite wait!", str, info->ident);
166                 }
167
168         }
169         return FALSE;
170 }
171
172 /*
173  * Finalize the state, record collision time in microseconds if
174  * we got past the initial load.
175  */
176 static __inline void
177 indefinite_done(indefinite_info_t *info)
178 {
179         tsc_uclock_t delta;
180         globaldata_t gd;
181
182         if (info->type && info->count > INDEF_INFO_START) {
183                 gd = mycpu;
184                 delta = rdtsc() - info->base;
185                 delta = delta * 1000000U / tsc_frequency;
186                 gd->gd_cnt.v_lock_colls += delta;
187         }
188         info->type = 0;
189 }
190
191 #endif