1 | /* $Id: tstR0ThreadPreemption.cpp 29250 2010-05-09 17:53:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Thread Preemption.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/thread.h>
|
---|
31 |
|
---|
32 | #include <iprt/asm-amd64-x86.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <VBox/sup.h>
|
---|
37 | #include "tstR0ThreadPreemption.h"
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Service request callback function.
|
---|
43 | *
|
---|
44 | * @returns VBox status code.
|
---|
45 | * @param pSession The caller's session.
|
---|
46 | * @param u64Arg 64-bit integer argument.
|
---|
47 | * @param pReqHdr The request header. Input / Output. Optional.
|
---|
48 | */
|
---|
49 | DECLEXPORT(int) TSTR0ThreadPreemptionSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
|
---|
50 | uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
|
---|
51 | {
|
---|
52 | if (u64Arg)
|
---|
53 | return VERR_INVALID_PARAMETER;
|
---|
54 | if (!VALID_PTR(pReqHdr))
|
---|
55 | return VERR_INVALID_PARAMETER;
|
---|
56 | char *pszErr = (char *)(pReqHdr + 1);
|
---|
57 | size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
|
---|
58 | if (cchErr < 32 || cchErr >= 0x10000)
|
---|
59 | return VERR_INVALID_PARAMETER;
|
---|
60 | *pszErr = '\0';
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * The big switch.
|
---|
64 | */
|
---|
65 | switch (uOperation)
|
---|
66 | {
|
---|
67 | case TSTR0THREADPREMEPTION_SANITY_OK:
|
---|
68 | break;
|
---|
69 |
|
---|
70 | case TSTR0THREADPREMEPTION_SANITY_FAILURE:
|
---|
71 | RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
|
---|
72 | break;
|
---|
73 |
|
---|
74 | case TSTR0THREADPREMEPTION_BASIC:
|
---|
75 | {
|
---|
76 | if (!ASMIntAreEnabled())
|
---|
77 | RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
|
---|
78 | else if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
79 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false by default");
|
---|
80 | else
|
---|
81 | {
|
---|
82 | RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
83 | RTThreadPreemptDisable(&State);
|
---|
84 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
85 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
|
---|
86 | else if (!ASMIntAreEnabled())
|
---|
87 | RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
|
---|
88 | RTThreadPreemptRestore(&State);
|
---|
89 | }
|
---|
90 | break;
|
---|
91 | }
|
---|
92 |
|
---|
93 | case TSTR0THREADPREMEPTION_IS_PENDING:
|
---|
94 | {
|
---|
95 | RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
96 | RTThreadPreemptDisable(&State);
|
---|
97 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
98 | {
|
---|
99 | if (ASMIntAreEnabled())
|
---|
100 | {
|
---|
101 | uint64_t u64StartTS = RTTimeNanoTS();
|
---|
102 | uint64_t u64StartSysTS = RTTimeSystemNanoTS();
|
---|
103 | uint64_t cLoops = 0;
|
---|
104 | uint64_t cNanosSysElapsed;
|
---|
105 | uint64_t cNanosElapsed;
|
---|
106 | bool fPending;
|
---|
107 | do
|
---|
108 | {
|
---|
109 | fPending = RTThreadPreemptIsPending(NIL_RTTHREAD);
|
---|
110 | cNanosElapsed = RTTimeNanoTS() - u64StartTS;
|
---|
111 | cNanosSysElapsed = RTTimeSystemNanoTS() - u64StartSysTS;
|
---|
112 | cLoops++;
|
---|
113 | } while ( !fPending
|
---|
114 | && cNanosElapsed < UINT64_C(2)*1000U*1000U*1000U
|
---|
115 | && cNanosSysElapsed < UINT64_C(2)*1000U*1000U*1000U
|
---|
116 | && cLoops < 100U*_1M);
|
---|
117 | if (!fPending)
|
---|
118 | RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns / %'llu ns (sys)",
|
---|
119 | cLoops, cNanosElapsed, cNanosSysElapsed);
|
---|
120 | else if (cLoops == 1)
|
---|
121 | RTStrPrintf(pszErr, cchErr, "!cLoops=1\n");
|
---|
122 | else
|
---|
123 | RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns / %'llu ns (sys)",
|
---|
124 | cLoops, cNanosElapsed, cNanosSysElapsed);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
|
---|
128 | }
|
---|
129 | else
|
---|
130 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
|
---|
131 | RTThreadPreemptRestore(&State);
|
---|
132 | break;
|
---|
133 | }
|
---|
134 |
|
---|
135 | case TSTR0THREADPREMEPTION_NESTED:
|
---|
136 | {
|
---|
137 | bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
|
---|
138 | RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
139 | RTThreadPreemptDisable(&State1);
|
---|
140 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
141 | {
|
---|
142 | RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
143 | RTThreadPreemptDisable(&State2);
|
---|
144 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
145 | {
|
---|
146 | RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
147 | RTThreadPreemptDisable(&State3);
|
---|
148 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
149 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
|
---|
150 |
|
---|
151 | RTThreadPreemptRestore(&State3);
|
---|
152 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
|
---|
153 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
|
---|
154 | }
|
---|
155 | else
|
---|
156 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
|
---|
157 |
|
---|
158 | RTThreadPreemptRestore(&State2);
|
---|
159 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
|
---|
160 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
|
---|
161 | }
|
---|
162 | else
|
---|
163 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
|
---|
164 | RTThreadPreemptRestore(&State1);
|
---|
165 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
|
---|
166 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
|
---|
167 | break;
|
---|
168 | }
|
---|
169 |
|
---|
170 | default:
|
---|
171 | RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
|
---|
172 | break;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* The error indicator is the '!' in the message buffer. */
|
---|
176 | return VINF_SUCCESS;
|
---|
177 | }
|
---|
178 |
|
---|