huangcm
2025-04-26 2868c607307b8de19383692485d1cbe1b64eb94d
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
/*-------------------------------------------------------------------------
 * drawElements Quality Program Execution Server
 * ---------------------------------------------
 *
 * 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 TestProcess implementation for Unix-like systems.
 *//*--------------------------------------------------------------------*/
 
#include "xsPosixTestProcess.hpp"
#include "deFilePath.hpp"
#include "deClock.h"
 
#include <string.h>
#include <stdio.h>
 
using std::string;
using std::vector;
 
namespace xs
{
 
namespace posix
{
 
CaseListWriter::CaseListWriter (void)
   : m_file    (DE_NULL)
   , m_run        (false)
{
}
 
CaseListWriter::~CaseListWriter (void)
{
}
 
void CaseListWriter::start (const char* caseList, deFile* dst)
{
   DE_ASSERT(!isStarted());
   m_file    = dst;
   m_run    = true;
 
   int caseListSize = (int)strlen(caseList)+1;
   m_caseList.resize(caseListSize);
   std::copy(caseList, caseList+caseListSize, m_caseList.begin());
 
   // Set to non-blocking mode.
   if (!deFile_setFlags(m_file, DE_FILE_NONBLOCKING))
       XS_FAIL("Failed to set non-blocking mode");
 
   de::Thread::start();
}
 
void CaseListWriter::run (void)
{
   deInt64 pos = 0;
 
   while (m_run && pos < (deInt64)m_caseList.size())
   {
       deInt64            numWritten    = 0;
       deFileResult    result        = deFile_write(m_file, &m_caseList[0] + pos, m_caseList.size()-pos, &numWritten);
 
       if (result == DE_FILERESULT_SUCCESS)
           pos += numWritten;
       else if (result == DE_FILERESULT_WOULD_BLOCK)
           deSleep(1); // Yield.
       else
           break; // Error.
   }
}
 
void CaseListWriter::stop (void)
{
   if (!isStarted())
       return; // Nothing to do.
 
   m_run = false;
 
   // Join thread.
   join();
 
   m_file = DE_NULL;
}
 
PipeReader::PipeReader (ThreadedByteBuffer* dst)
   : m_file    (DE_NULL)
   , m_buf        (dst)
{
}
 
PipeReader::~PipeReader (void)
{
}
 
void PipeReader::start (deFile* file)
{
   DE_ASSERT(!isStarted());
 
   // Set to non-blocking mode.
   if (!deFile_setFlags(file, DE_FILE_NONBLOCKING))
       XS_FAIL("Failed to set non-blocking mode");
 
   m_file = file;
 
   de::Thread::start();
}
 
void PipeReader::run (void)
{
   std::vector<deUint8>    tmpBuf        (FILEREADER_TMP_BUFFER_SIZE);
   deInt64                    numRead        = 0;
 
   while (!m_buf->isCanceled())
   {
       deFileResult result = deFile_read(m_file, &tmpBuf[0], (deInt64)tmpBuf.size(), &numRead);
 
       if (result == DE_FILERESULT_SUCCESS)
       {
           // Write to buffer.
           try
           {
               m_buf->write((int)numRead, &tmpBuf[0]);
               m_buf->flush();
           }
           catch (const ThreadedByteBuffer::CanceledException&)
           {
               // Canceled.
               break;
           }
       }
       else if (result == DE_FILERESULT_END_OF_FILE ||
                result == DE_FILERESULT_WOULD_BLOCK)
       {
           // Wait for more data.
           deSleep(FILEREADER_IDLE_SLEEP);
       }
       else
           break; // Error.
   }
}
 
void PipeReader::stop (void)
{
   if (!isStarted())
       return; // Nothing to do.
 
   // Buffer must be in canceled state or otherwise stopping reader might block.
   DE_ASSERT(m_buf->isCanceled());
 
   // Join thread.
   join();
 
   m_file = DE_NULL;
}
 
} // unix
 
PosixTestProcess::PosixTestProcess (void)
   : m_process                (DE_NULL)
   , m_processStartTime    (0)
   , m_infoBuffer            (INFO_BUFFER_BLOCK_SIZE, INFO_BUFFER_NUM_BLOCKS)
   , m_stdOutReader        (&m_infoBuffer)
   , m_stdErrReader        (&m_infoBuffer)
   , m_logReader            (LOG_BUFFER_BLOCK_SIZE, LOG_BUFFER_NUM_BLOCKS)
{
}
 
PosixTestProcess::~PosixTestProcess (void)
{
   delete m_process;
}
 
void PosixTestProcess::start (const char* name, const char* params, const char* workingDir, const char* caseList)
{
   bool hasCaseList = strlen(caseList) > 0;
 
   XS_CHECK(!m_process);
 
   de::FilePath logFilePath = de::FilePath::join(workingDir, "TestResults.qpa");
   m_logFileName = logFilePath.getPath();
 
   // Remove old file if such exists.
   if (deFileExists(m_logFileName.c_str()))
   {
       if (!deDeleteFile(m_logFileName.c_str()) || deFileExists(m_logFileName.c_str()))
           throw TestProcessException(string("Failed to remove '") + m_logFileName + "'");
   }
 
   // Construct command line.
   string cmdLine = de::FilePath(name).isAbsolutePath() ? name : de::FilePath::join(workingDir, name).getPath();
   cmdLine += string(" --deqp-log-filename=") + logFilePath.getBaseName();
 
   if (hasCaseList)
       cmdLine += " --deqp-stdin-caselist";
 
   if (strlen(params) > 0)
       cmdLine += string(" ") + params;
 
   DE_ASSERT(!m_process);
   m_process = new de::Process();
 
   try
   {
       m_process->start(cmdLine.c_str(), strlen(workingDir) > 0 ? workingDir : DE_NULL);
   }
   catch (const de::ProcessError& e)
   {
       delete m_process;
       m_process = DE_NULL;
       throw TestProcessException(e.what());
   }
 
   m_processStartTime = deGetMicroseconds();
 
   // Create stdout & stderr readers.
   if (m_process->getStdOut())
       m_stdOutReader.start(m_process->getStdOut());
 
   if (m_process->getStdErr())
       m_stdErrReader.start(m_process->getStdErr());
 
   // Start case list writer.
   if (hasCaseList)
   {
       deFile* dst = m_process->getStdIn();
       if (dst)
           m_caseListWriter.start(caseList, dst);
       else
       {
           cleanup();
           throw TestProcessException("Failed to write case list");
       }
   }
}
 
void PosixTestProcess::terminate (void)
{
   if (m_process)
   {
       try
       {
           m_process->kill();
       }
       catch (const std::exception& e)
       {
           printf("PosixTestProcess::terminate(): Failed to kill process: %s\n", e.what());
       }
   }
}
 
void PosixTestProcess::cleanup (void)
{
   m_caseListWriter.stop();
   m_logReader.stop();
 
   // \note Info buffer must be canceled before stopping pipe readers.
   m_infoBuffer.cancel();
 
   m_stdErrReader.stop();
   m_stdOutReader.stop();
 
   // Reset info buffer.
   m_infoBuffer.clear();
 
   if (m_process)
   {
       try
       {
           if (m_process->isRunning())
           {
               m_process->kill();
               m_process->waitForFinish();
           }
       }
       catch (const de::ProcessError& e)
       {
           printf("PosixTestProcess::stop(): Failed to kill process: %s\n", e.what());
       }
 
       delete m_process;
       m_process = DE_NULL;
   }
}
 
bool PosixTestProcess::isRunning (void)
{
   if (m_process)
       return m_process->isRunning();
   else
       return false;
}
 
int PosixTestProcess::getExitCode (void) const
{
   if (m_process)
       return m_process->getExitCode();
   else
       return -1;
}
 
int PosixTestProcess::readTestLog (deUint8* dst, int numBytes)
{
   if (!m_logReader.isRunning())
   {
       if (deGetMicroseconds() - m_processStartTime > LOG_FILE_TIMEOUT*1000)
       {
           // Timeout, kill process.
           terminate();
           return 0; // \todo [2013-08-13 pyry] Throw exception?
       }
 
       if (!deFileExists(m_logFileName.c_str()))
           return 0;
 
       // Start reader.
       m_logReader.start(m_logFileName.c_str());
   }
 
   DE_ASSERT(m_logReader.isRunning());
   return m_logReader.read(dst, numBytes);
}
 
} // xs