xref: /aosp_15_r20/external/deqp/framework/qphelper/qpWatchDog.c (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
2*35238bceSAndroid Build Coastguard Worker  * drawElements Quality Program Helper Library
3*35238bceSAndroid Build Coastguard Worker  * -------------------------------------------
4*35238bceSAndroid Build Coastguard Worker  *
5*35238bceSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
6*35238bceSAndroid Build Coastguard Worker  *
7*35238bceSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
8*35238bceSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
9*35238bceSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
10*35238bceSAndroid Build Coastguard Worker  *
11*35238bceSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
12*35238bceSAndroid Build Coastguard Worker  *
13*35238bceSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
14*35238bceSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
15*35238bceSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*35238bceSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
17*35238bceSAndroid Build Coastguard Worker  * limitations under the License.
18*35238bceSAndroid Build Coastguard Worker  *
19*35238bceSAndroid Build Coastguard Worker  *//*!
20*35238bceSAndroid Build Coastguard Worker  * \file
21*35238bceSAndroid Build Coastguard Worker  * \brief Watch dog for detecting timeouts
22*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker 
24*35238bceSAndroid Build Coastguard Worker #include "qpWatchDog.h"
25*35238bceSAndroid Build Coastguard Worker 
26*35238bceSAndroid Build Coastguard Worker #include "deThread.h"
27*35238bceSAndroid Build Coastguard Worker #include "deClock.h"
28*35238bceSAndroid Build Coastguard Worker #include "deMemory.h"
29*35238bceSAndroid Build Coastguard Worker 
30*35238bceSAndroid Build Coastguard Worker #include <stdio.h>
31*35238bceSAndroid Build Coastguard Worker 
32*35238bceSAndroid Build Coastguard Worker #if 0
33*35238bceSAndroid Build Coastguard Worker #define DBGPRINT(X) qpPrintf X
34*35238bceSAndroid Build Coastguard Worker #else
35*35238bceSAndroid Build Coastguard Worker #define DBGPRINT(X)
36*35238bceSAndroid Build Coastguard Worker #endif
37*35238bceSAndroid Build Coastguard Worker 
38*35238bceSAndroid Build Coastguard Worker typedef enum Status_e
39*35238bceSAndroid Build Coastguard Worker {
40*35238bceSAndroid Build Coastguard Worker     STATUS_THREAD_RUNNING = 0,
41*35238bceSAndroid Build Coastguard Worker     STATUS_STOP_THREAD,
42*35238bceSAndroid Build Coastguard Worker 
43*35238bceSAndroid Build Coastguard Worker     STATUS_LAST
44*35238bceSAndroid Build Coastguard Worker } Status;
45*35238bceSAndroid Build Coastguard Worker 
46*35238bceSAndroid Build Coastguard Worker struct qpWatchDog_s
47*35238bceSAndroid Build Coastguard Worker {
48*35238bceSAndroid Build Coastguard Worker     qpWatchDogFunc timeOutFunc;
49*35238bceSAndroid Build Coastguard Worker     void *timeOutUserPtr;
50*35238bceSAndroid Build Coastguard Worker     int totalTimeLimit;    /* Total test case time limit in seconds    */
51*35238bceSAndroid Build Coastguard Worker     int intervalTimeLimit; /* Iteration length limit in seconds        */
52*35238bceSAndroid Build Coastguard Worker     /*
53*35238bceSAndroid Build Coastguard Worker         Iteration time limit in seconds specified to the constructor. This is stored so that
54*35238bceSAndroid Build Coastguard Worker         intervalTimeLimit can be restored after qpWatchDog_touchAndDisableIntervalTimeLimit
55*35238bceSAndroid Build Coastguard Worker         is called.
56*35238bceSAndroid Build Coastguard Worker     */
57*35238bceSAndroid Build Coastguard Worker     int defaultIntervalTimeLimit;
58*35238bceSAndroid Build Coastguard Worker 
59*35238bceSAndroid Build Coastguard Worker     volatile uint64_t resetTime;
60*35238bceSAndroid Build Coastguard Worker     volatile uint64_t lastTouchTime;
61*35238bceSAndroid Build Coastguard Worker 
62*35238bceSAndroid Build Coastguard Worker     deThread watchDogThread;
63*35238bceSAndroid Build Coastguard Worker     volatile Status status;
64*35238bceSAndroid Build Coastguard Worker };
65*35238bceSAndroid Build Coastguard Worker 
watchDogThreadFunc(void * arg)66*35238bceSAndroid Build Coastguard Worker static void watchDogThreadFunc(void *arg)
67*35238bceSAndroid Build Coastguard Worker {
68*35238bceSAndroid Build Coastguard Worker     qpWatchDog *dog = (qpWatchDog *)arg;
69*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(dog);
70*35238bceSAndroid Build Coastguard Worker 
71*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("watchDogThreadFunc(): start\n"));
72*35238bceSAndroid Build Coastguard Worker 
73*35238bceSAndroid Build Coastguard Worker     while (dog->status == STATUS_THREAD_RUNNING)
74*35238bceSAndroid Build Coastguard Worker     {
75*35238bceSAndroid Build Coastguard Worker         uint64_t curTime          = deGetMicroseconds();
76*35238bceSAndroid Build Coastguard Worker         int totalSecondsPassed    = (int)((curTime - dog->resetTime) / 1000000ull);
77*35238bceSAndroid Build Coastguard Worker         int secondsSinceLastTouch = (int)((curTime - dog->lastTouchTime) / 1000000ull);
78*35238bceSAndroid Build Coastguard Worker         bool overIntervalLimit    = secondsSinceLastTouch > dog->intervalTimeLimit;
79*35238bceSAndroid Build Coastguard Worker         bool overTotalLimit       = totalSecondsPassed > dog->totalTimeLimit;
80*35238bceSAndroid Build Coastguard Worker 
81*35238bceSAndroid Build Coastguard Worker         if (overIntervalLimit || overTotalLimit)
82*35238bceSAndroid Build Coastguard Worker         {
83*35238bceSAndroid Build Coastguard Worker             qpTimeoutReason reason = overTotalLimit ? QP_TIMEOUT_REASON_TOTAL_LIMIT : QP_TIMEOUT_REASON_INTERVAL_LIMIT;
84*35238bceSAndroid Build Coastguard Worker             DBGPRINT(("watchDogThreadFunc(): call timeout func\n"));
85*35238bceSAndroid Build Coastguard Worker             dog->timeOutFunc(dog, dog->timeOutUserPtr, reason);
86*35238bceSAndroid Build Coastguard Worker             break;
87*35238bceSAndroid Build Coastguard Worker         }
88*35238bceSAndroid Build Coastguard Worker 
89*35238bceSAndroid Build Coastguard Worker         deSleep(100);
90*35238bceSAndroid Build Coastguard Worker     }
91*35238bceSAndroid Build Coastguard Worker 
92*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("watchDogThreadFunc(): stop\n"));
93*35238bceSAndroid Build Coastguard Worker }
94*35238bceSAndroid Build Coastguard Worker 
qpWatchDog_create(qpWatchDogFunc timeOutFunc,void * userPtr,int totalTimeLimitSecs,int intervalTimeLimitSecs)95*35238bceSAndroid Build Coastguard Worker qpWatchDog *qpWatchDog_create(qpWatchDogFunc timeOutFunc, void *userPtr, int totalTimeLimitSecs,
96*35238bceSAndroid Build Coastguard Worker                               int intervalTimeLimitSecs)
97*35238bceSAndroid Build Coastguard Worker {
98*35238bceSAndroid Build Coastguard Worker     /* Allocate & initialize. */
99*35238bceSAndroid Build Coastguard Worker     qpWatchDog *dog = (qpWatchDog *)deCalloc(sizeof(qpWatchDog));
100*35238bceSAndroid Build Coastguard Worker     if (!dog)
101*35238bceSAndroid Build Coastguard Worker         return dog;
102*35238bceSAndroid Build Coastguard Worker 
103*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(timeOutFunc);
104*35238bceSAndroid Build Coastguard Worker     DE_ASSERT((totalTimeLimitSecs > 0) && (intervalTimeLimitSecs > 0));
105*35238bceSAndroid Build Coastguard Worker 
106*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("qpWatchDog::create(%ds, %ds)\n", totalTimeLimitSecs, intervalTimeLimitSecs));
107*35238bceSAndroid Build Coastguard Worker 
108*35238bceSAndroid Build Coastguard Worker     dog->timeOutFunc              = timeOutFunc;
109*35238bceSAndroid Build Coastguard Worker     dog->timeOutUserPtr           = userPtr;
110*35238bceSAndroid Build Coastguard Worker     dog->totalTimeLimit           = totalTimeLimitSecs;
111*35238bceSAndroid Build Coastguard Worker     dog->intervalTimeLimit        = intervalTimeLimitSecs;
112*35238bceSAndroid Build Coastguard Worker     dog->defaultIntervalTimeLimit = intervalTimeLimitSecs;
113*35238bceSAndroid Build Coastguard Worker 
114*35238bceSAndroid Build Coastguard Worker     /* Reset (sets time values). */
115*35238bceSAndroid Build Coastguard Worker     qpWatchDog_reset(dog);
116*35238bceSAndroid Build Coastguard Worker 
117*35238bceSAndroid Build Coastguard Worker     /* Initialize watchdog thread. */
118*35238bceSAndroid Build Coastguard Worker     dog->status         = STATUS_THREAD_RUNNING;
119*35238bceSAndroid Build Coastguard Worker     dog->watchDogThread = deThread_create(watchDogThreadFunc, dog, DE_NULL);
120*35238bceSAndroid Build Coastguard Worker     if (!dog->watchDogThread)
121*35238bceSAndroid Build Coastguard Worker     {
122*35238bceSAndroid Build Coastguard Worker         deFree(dog);
123*35238bceSAndroid Build Coastguard Worker         return DE_NULL;
124*35238bceSAndroid Build Coastguard Worker     }
125*35238bceSAndroid Build Coastguard Worker 
126*35238bceSAndroid Build Coastguard Worker     return dog;
127*35238bceSAndroid Build Coastguard Worker }
128*35238bceSAndroid Build Coastguard Worker 
qpWatchDog_reset(qpWatchDog * dog)129*35238bceSAndroid Build Coastguard Worker void qpWatchDog_reset(qpWatchDog *dog)
130*35238bceSAndroid Build Coastguard Worker {
131*35238bceSAndroid Build Coastguard Worker     uint64_t curTime = deGetMicroseconds();
132*35238bceSAndroid Build Coastguard Worker 
133*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(dog);
134*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("qpWatchDog::reset()\n"));
135*35238bceSAndroid Build Coastguard Worker 
136*35238bceSAndroid Build Coastguard Worker     dog->resetTime     = curTime;
137*35238bceSAndroid Build Coastguard Worker     dog->lastTouchTime = curTime;
138*35238bceSAndroid Build Coastguard Worker }
139*35238bceSAndroid Build Coastguard Worker 
qpWatchDog_destroy(qpWatchDog * dog)140*35238bceSAndroid Build Coastguard Worker void qpWatchDog_destroy(qpWatchDog *dog)
141*35238bceSAndroid Build Coastguard Worker {
142*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(dog);
143*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("qpWatchDog::destroy()\n"));
144*35238bceSAndroid Build Coastguard Worker 
145*35238bceSAndroid Build Coastguard Worker     /* Finish the watchdog thread. */
146*35238bceSAndroid Build Coastguard Worker     dog->status = STATUS_STOP_THREAD;
147*35238bceSAndroid Build Coastguard Worker     deThread_join(dog->watchDogThread);
148*35238bceSAndroid Build Coastguard Worker     deThread_destroy(dog->watchDogThread);
149*35238bceSAndroid Build Coastguard Worker 
150*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("qpWatchDog::destroy() finished\n"));
151*35238bceSAndroid Build Coastguard Worker     deFree(dog);
152*35238bceSAndroid Build Coastguard Worker }
153*35238bceSAndroid Build Coastguard Worker 
qpWatchDog_touch(qpWatchDog * dog)154*35238bceSAndroid Build Coastguard Worker void qpWatchDog_touch(qpWatchDog *dog)
155*35238bceSAndroid Build Coastguard Worker {
156*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(dog);
157*35238bceSAndroid Build Coastguard Worker     DBGPRINT(("qpWatchDog::touch()\n"));
158*35238bceSAndroid Build Coastguard Worker     dog->lastTouchTime = deGetMicroseconds();
159*35238bceSAndroid Build Coastguard Worker }
160*35238bceSAndroid Build Coastguard Worker 
161*35238bceSAndroid Build Coastguard Worker /*
162*35238bceSAndroid Build Coastguard Worker     These function exists to allow the interval timer to be disabled for special cases
163*35238bceSAndroid Build Coastguard Worker     like very long shader compilations. Heavy code can be put between calls
164*35238bceSAndroid Build Coastguard Worker     to qpWatchDog_touchAndDisableIntervalTimeLimit and qpWatchDog_touchAndEnableIntervalTimeLimit
165*35238bceSAndroid Build Coastguard Worker     and during that period the interval time limit will become the same as the total
166*35238bceSAndroid Build Coastguard Worker     time limit. Afterwards, the interval timer is set back to its default.
167*35238bceSAndroid Build Coastguard Worker */
qpWatchDog_touchAndDisableIntervalTimeLimit(qpWatchDog * dog)168*35238bceSAndroid Build Coastguard Worker void qpWatchDog_touchAndDisableIntervalTimeLimit(qpWatchDog *dog)
169*35238bceSAndroid Build Coastguard Worker {
170*35238bceSAndroid Build Coastguard Worker     dog->intervalTimeLimit = dog->totalTimeLimit;
171*35238bceSAndroid Build Coastguard Worker     qpWatchDog_touch(dog);
172*35238bceSAndroid Build Coastguard Worker }
173*35238bceSAndroid Build Coastguard Worker 
qpWatchDog_touchAndEnableIntervalTimeLimit(qpWatchDog * dog)174*35238bceSAndroid Build Coastguard Worker void qpWatchDog_touchAndEnableIntervalTimeLimit(qpWatchDog *dog)
175*35238bceSAndroid Build Coastguard Worker {
176*35238bceSAndroid Build Coastguard Worker     dog->intervalTimeLimit = dog->defaultIntervalTimeLimit;
177*35238bceSAndroid Build Coastguard Worker     qpWatchDog_touch(dog);
178*35238bceSAndroid Build Coastguard Worker }
179