2 * Copyright (c) 2017 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 #ifndef _SYS_INDEFINITE2_H_
35 #define _SYS_INDEFINITE2_H_
38 * Indefinite info collection and handling code for contention loops
40 #ifndef _SYS_INDEFINITE_H_
41 #include <sys/indefinite.h>
43 #ifndef _SYS_GLOBALDATA_H_
44 #include <sys/globaldata.h>
46 #ifdef _KERNEL_VIRTUAL
51 * Initialize the indefinite state (only if the TSC is supported)
54 indefinite_init(indefinite_info_t
*info
, const char *ident
, char now
, char type
)
63 /* info->base = rdtsc(); (see indefinite_check()) */
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);
76 * Update the state during any loop, record collision time in microseconds.
79 indefinite_check(indefinite_info_t
*info
)
84 #ifdef _KERNEL_VIRTUAL
91 if (info
->count
== INDEF_INFO_START
) { /* start recording time */
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);
100 if ((++info
->count
& 127) != 127)
103 delta
= rdtsc() - info
->base
;
105 #if defined(INVARIANTS)
106 if (lock_test_mode
> 0) {
113 * Ignore minor one-second interval error accumulation in
114 * favor of ensuring that info->base is fully synchronized.
116 if (delta
>= tsc_frequency
) {
117 info
->secs
+= delta
/ tsc_frequency
;
119 mycpu
->gd_cnt
.v_lock_colls
+= delta
/ tsc_frequency
* 1000000U;
123 str
= "spin_lock_sh";
126 str
= "spin_lock_ex";
147 kprintf("%s: %s, indefinite wait (%d secs)!\n",
148 str
, info
->ident
, info
->secs
);
151 #if defined(INVARIANTS)
152 if (lock_test_mode
) {
157 #if defined(INVARIANTS)
158 if (info
->secs
== 11 &&
159 (info
->type
== 's' || info
->type
== 'S')) {
163 if (info
->secs
== 60 &&
164 (info
->type
== 's' || info
->type
== 'S')) {
165 panic("%s: %s, indefinite wait!", str
, info
->ident
);
173 * Finalize the state, record collision time in microseconds if
174 * we got past the initial load.
177 indefinite_done(indefinite_info_t
*info
)
182 if (info
->type
&& info
->count
> INDEF_INFO_START
) {
184 delta
= rdtsc() - info
->base
;
185 delta
= delta
* 1000000U / tsc_frequency
;
186 gd
->gd_cnt
.v_lock_colls
+= delta
;