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
, int now
, char type
)
62 /* info->base = rdtsc(); (see indefinite_check()) */
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);
75 * Update the state during any loop, record collision time in microseconds.
78 indefinite_check(indefinite_info_t
*info
)
83 #ifdef _KERNEL_VIRTUAL
90 if (info
->count
== INDEF_INFO_START
) /* start recording time */
92 if ((++info
->count
& 127) != 127)
95 delta
= rdtsc() - info
->base
;
97 #if defined(INVARIANTS)
98 if (lock_test_mode
> 0) {
105 * Ignore minor one-second interval error accumulation in
106 * favor of ensuring that info->base is fully synchronized.
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);
113 if (delta
>= tsc_frequency
) {
114 info
->secs
+= delta
/ tsc_frequency
;
116 mycpu
->gd_cnt
.v_lock_colls
+= delta
/ tsc_frequency
* 1000000U;
120 str
= "spin_lock_sh";
123 str
= "spin_lock_ex";
144 kprintf("%s: %s, indefinite wait (%d secs)!\n",
145 str
, info
->ident
, info
->secs
);
148 #if defined(INVARIANTS)
149 if (lock_test_mode
) {
154 #if defined(INVARIANTS)
155 if (info
->secs
== 11 &&
156 (info
->type
== 's' || info
->type
== 'S')) {
160 if (info
->secs
== 60 &&
161 (info
->type
== 's' || info
->type
== 'S')) {
162 panic("%s: %s, indefinite wait!", str
, info
->ident
);
170 * Finalize the state, record collision time in microseconds if
171 * we got past the initial load.
174 indefinite_done(indefinite_info_t
*info
)
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
;