hammer2 - Increase bulkfree buffer size request and cap
[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, int now, char type)
55 {
56         info->ident = ident;
57         info->secs = 0;
58         info->count = 0;
59
60         if (tsc_frequency) {
61                 info->type = type;
62                 /* info->base = rdtsc(); (see indefinite_check()) */
63         } else {
64                 info->type = 0;
65                 info->base = 0;
66         }
67         if (now && info->ident) {
68                 mycpu->gd_cnt.v_lock_name[0] = info->type;
69                 strncpy(mycpu->gd_cnt.v_lock_name + 1, info->ident,
70                         sizeof(mycpu->gd_cnt.v_lock_name) - 2);
71         }
72 }
73
74 /*
75  * Update the state during any loop, record collision time in microseconds.
76  */
77 static __inline int
78 indefinite_check(indefinite_info_t *info)
79 {
80         tsc_uclock_t delta;
81         const char *str;
82
83 #ifdef _KERNEL_VIRTUAL
84         pthread_yield();
85 #else
86         cpu_pause();
87 #endif
88         if (info->type == 0)
89                 return FALSE;
90         if (info->count == INDEF_INFO_START)    /* start recording time */
91                 info->base = rdtsc();
92         if ((++info->count & 127) != 127)
93                 return FALSE;
94         info->count = 128;
95         delta = rdtsc() - info->base;
96
97 #if defined(INVARIANTS)
98         if (lock_test_mode > 0) {
99                 --lock_test_mode;
100                 print_backtrace(8);
101         }
102 #endif
103
104         /*
105          * Ignore minor one-second interval error accumulation in
106          * favor of ensuring that info->base is fully synchronized.
107          */
108         if (info->secs == 0 && delta > tsc_oneus_approx && info->ident) {
109                 mycpu->gd_cnt.v_lock_name[0] = info->type;
110                 strncpy(mycpu->gd_cnt.v_lock_name + 1, info->ident,
111                         sizeof(mycpu->gd_cnt.v_lock_name) - 2);
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
178         if (info->type && info->count > INDEF_INFO_START) {
179                 delta = rdtsc() - info->base;
180                 delta = delta * 1000000U / tsc_frequency;
181                 mycpu->gd_cnt.v_lock_colls += delta;
182         }
183         info->type = 0;
184 }
185
186 #endif