huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Android ExecServer.
 *//*--------------------------------------------------------------------*/
 
#include "tcuAndroidExecService.hpp"
#include "deFile.h"
#include "deClock.h"
 
#if 0
#    define DBG_PRINT(ARGS) print ARGS
#else
#    define DBG_PRINT(ARGS)
#endif
 
namespace tcu
{
namespace Android
{
 
static const char* LOG_FILE_NAME = "/sdcard/dEQP-log.qpa";
 
enum
{
   PROCESS_START_TIMEOUT    = 5000*1000,    //!< Timeout in usec.
   PROCESS_QUERY_INTERVAL    = 1000*1000        //!< Running query interval limit in usec.
};
 
static void checkJniException (JNIEnv* env, const char* file, int line)
{
   if (env->ExceptionOccurred())
   {
       env->ExceptionDescribe();
       env->ExceptionClear();
       throw InternalError("JNI Exception", DE_NULL, file, line);
   }
}
 
#define JNI_CHECK(EXPR) do { checkJniException(env, __FILE__, __LINE__); TCU_CHECK_INTERNAL(EXPR); } while (deGetFalse())
 
// TestProcess
 
TestProcess::TestProcess (JavaVM* vm, jobject context)
   : m_vm                    (vm)
   , m_remoteCls            (0)
   , m_remote                (0)
   , m_start                (0)
   , m_kill                (0)
   , m_isRunning            (0)
   , m_launchTime            (0)
   , m_lastQueryTime        (0)
   , m_lastRunningStatus    (false)
   , m_logReader            (xs::LOG_BUFFER_BLOCK_SIZE, xs::LOG_BUFFER_NUM_BLOCKS)
{
   DBG_PRINT(("TestProcess::TestProcess(%p, %p)", vm, context));
 
   JNIEnv* env            = getCurrentThreadEnv();
   jobject    remote        = 0;
   jstring    logFileName    = 0;
 
   try
   {
       jclass        remoteCls    = 0;
       jmethodID    ctorId        = 0;
 
       remoteCls = env->FindClass("com/drawelements/deqp/testercore/RemoteAPI");
       JNI_CHECK(remoteCls);
 
       // Acquire global reference to RemoteAPI class.
       m_remoteCls = reinterpret_cast<jclass>(env->NewGlobalRef(remoteCls));
       JNI_CHECK(m_remoteCls);
       env->DeleteLocalRef(remoteCls);
       remoteCls = 0;
 
       ctorId = env->GetMethodID(m_remoteCls, "<init>", "(Landroid/content/Context;Ljava/lang/String;)V");
       JNI_CHECK(ctorId);
 
       logFileName = env->NewStringUTF(LOG_FILE_NAME);
       JNI_CHECK(logFileName);
 
       // Create RemoteAPI instance.
       remote = env->NewObject(m_remoteCls, ctorId, context, logFileName);
       JNI_CHECK(remote);
 
       env->DeleteLocalRef(logFileName);
       logFileName = 0;
 
       // Acquire global reference to remote.
       m_remote = env->NewGlobalRef(remote);
       JNI_CHECK(m_remote);
       env->DeleteLocalRef(remote);
       remote = 0;
 
       m_start    = env->GetMethodID(m_remoteCls, "start", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z");
       JNI_CHECK(m_start);
 
       m_kill = env->GetMethodID(m_remoteCls, "kill", "()Z");
       JNI_CHECK(m_kill);
 
       m_isRunning = env->GetMethodID(m_remoteCls, "isRunning", "()Z");
       JNI_CHECK(m_isRunning);
   }
   catch (...)
   {
       if (logFileName)
           env->DeleteLocalRef(logFileName);
       if (remote)
           env->DeleteLocalRef(remote);
       if (m_remoteCls)
           env->DeleteGlobalRef(reinterpret_cast<jobject>(m_remoteCls));
       if (m_remote)
           env->DeleteGlobalRef(m_remote);
       throw;
   }
}
 
TestProcess::~TestProcess (void)
{
   DBG_PRINT(("TestProcess::~TestProcess()"));
 
   try
   {
       JNIEnv* env = getCurrentThreadEnv();
       env->DeleteGlobalRef(m_remote);
       env->DeleteGlobalRef(m_remoteCls);
   }
   catch (...)
   {
   }
}
 
void TestProcess::start (const char* name, const char* params, const char* workingDir, const char* caseList)
{
   DBG_PRINT(("TestProcess::start(%s, %s, %s, ...)", name, params, workingDir));
 
   JNIEnv* env            = getCurrentThreadEnv();
   jstring    nameStr        = 0;
   jstring    paramsStr    = 0;
   jstring caseListStr    = 0;
 
   DE_UNREF(workingDir);
 
   // Remove old log file if such exists.
   if (deFileExists(LOG_FILE_NAME))
   {
       if (!deDeleteFile(LOG_FILE_NAME) || deFileExists(LOG_FILE_NAME))
           throw xs::TestProcessException(std::string("Failed to remove '") + LOG_FILE_NAME + "'");
   }
 
   try
   {
       nameStr = env->NewStringUTF(name);
       JNI_CHECK(nameStr);
 
       paramsStr = env->NewStringUTF(params);
       JNI_CHECK(paramsStr);
 
       caseListStr = env->NewStringUTF(caseList);
       JNI_CHECK(caseListStr);
 
       jboolean res = env->CallBooleanMethod(m_remote, m_start, nameStr, paramsStr, caseListStr);
       checkJniException(env, __FILE__, __LINE__);
 
       if (res == JNI_FALSE)
           throw xs::TestProcessException("Failed to launch activity");
 
       m_launchTime        = deGetMicroseconds();
       m_lastQueryTime        = m_launchTime;
       m_lastRunningStatus    = true;
   }
   catch (...)
   {
       if (nameStr)
           env->DeleteLocalRef(nameStr);
       if (paramsStr)
           env->DeleteLocalRef(paramsStr);
       if (caseListStr)
           env->DeleteLocalRef(caseListStr);
       throw;
   }
 
   env->DeleteLocalRef(nameStr);
   env->DeleteLocalRef(paramsStr);
   env->DeleteLocalRef(caseListStr);
}
 
void TestProcess::terminate (void)
{
   DBG_PRINT(("TestProcess::terminate()"));
 
   JNIEnv*        env        = getCurrentThreadEnv();
   jboolean    res        = env->CallBooleanMethod(m_remote, m_kill);
   checkJniException(env, __FILE__, __LINE__);
   DE_UNREF(res); // Failure to kill process is ignored.
}
 
void TestProcess::cleanup (void)
{
   DBG_PRINT(("TestProcess::cleanup()"));
 
   terminate();
   m_logReader.stop();
}
 
bool TestProcess::isRunning (void)
{
   deUint64 curTime = deGetMicroseconds();
 
   // On Android process launch is asynchronous so we don't want to poll for process until after some time.
   if (curTime-m_launchTime < PROCESS_START_TIMEOUT ||
       curTime-m_lastQueryTime < PROCESS_QUERY_INTERVAL)
       return m_lastRunningStatus;
 
   JNIEnv*        env        = getCurrentThreadEnv();
   jboolean    res        = env->CallBooleanMethod(m_remote, m_isRunning);
   checkJniException(env, __FILE__, __LINE__);
 
   DBG_PRINT(("TestProcess::isRunning(): %s", res == JNI_TRUE ? "true" : "false"));
   m_lastQueryTime        = curTime;
   m_lastRunningStatus    = res == JNI_TRUE;
 
   return m_lastRunningStatus;
}
 
JNIEnv* TestProcess::getCurrentThreadEnv (void)
{
   JNIEnv* env = DE_NULL;
   jint    ret    = m_vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
 
   if (ret == JNI_OK)
       return env;
   else
       throw InternalError("GetEnv() failed");
}
 
int TestProcess::readTestLog (deUint8* dst, int numBytes)
{
   if (!m_logReader.isRunning())
   {
       if (deGetMicroseconds() - m_launchTime > xs::LOG_FILE_TIMEOUT*1000)
       {
           // Timeout, kill process.
           terminate();
           DBG_PRINT(("TestProcess:readTestLog(): Log file timeout occurred!"));
           return 0; // \todo [2013-08-13 pyry] Throw exception?
       }
 
       if (!deFileExists(LOG_FILE_NAME))
           return 0;
 
       // Start reader.
       m_logReader.start(LOG_FILE_NAME);
   }
 
   DE_ASSERT(m_logReader.isRunning());
   return m_logReader.read(dst, numBytes);
}
 
int    TestProcess::getExitCode (void) const
{
   return 0;
}
 
int TestProcess::readInfoLog (deUint8* dst, int numBytes)
{
   // \todo [2012-11-12 pyry] Read device log.
   DE_UNREF(dst && numBytes);
   return 0;
}
 
// ExecutionServer
 
ExecutionServer::ExecutionServer (JavaVM* vm, xs::TestProcess* testProcess, deSocketFamily family, int port, RunMode runMode)
   : xs::ExecutionServer    (testProcess, family, port, runMode)
   , m_vm                    (vm)
{
}
 
xs::ConnectionHandler* ExecutionServer::createHandler (de::Socket* socket, const de::SocketAddress& clientAddress)
{
   DE_UNREF(clientAddress);
   return new ConnectionHandler(m_vm, this, socket);
}
 
// ConnectionHandler
 
ConnectionHandler::ConnectionHandler (JavaVM* vm, xs::ExecutionServer* server, de::Socket* socket)
   : xs::ExecutionRequestHandler    (server, socket)
   , m_vm                            (vm)
{
}
 
void ConnectionHandler::run (void)
{
   JNIEnv* env = DE_NULL;
   if (m_vm->AttachCurrentThread(&env, DE_NULL) != 0)
   {
       print("AttachCurrentThread() failed");
       return;
   }
 
   xs::ExecutionRequestHandler::run();
 
   if (m_vm->DetachCurrentThread() != 0)
       print("DetachCurrentThread() failed");
}
 
// ServerThread
 
ServerThread::ServerThread (JavaVM* vm, xs::TestProcess* process, deSocketFamily family, int port)
   : m_server(vm, process, family, port, xs::ExecutionServer::RUNMODE_FOREVER)
{
}
 
void ServerThread::run (void)
{
   try
   {
       m_server.runServer();
   }
   catch (const std::exception& e)
   {
       die("ServerThread::run(): %s", e.what());
   }
}
 
void ServerThread::stop (void)
{
   m_server.stopServer();
   join();
}
 
// ExecService
 
ExecService::ExecService (JavaVM* vm, jobject context, int port, deSocketFamily family)
   : m_process        (vm, context)
   , m_thread        (vm, &m_process, family, port)
{
}
 
ExecService::~ExecService (void)
{
}
 
void ExecService::start (void)
{
   m_thread.start();
}
 
void ExecService::stop (void)
{
   m_thread.stop();
}
 
} // Android
} // tcu