1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Execution Server
3 * ---------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief TestProcess implementation for Win32.
22 *//*--------------------------------------------------------------------*/
24 #include "xsWin32TestProcess.hpp"
25 #include "deFilePath.hpp"
42 MAX_OLD_LOGFILE_DELETE_ATTEMPTS = 20, //!< How many times execserver tries to delete old log file
43 LOGFILE_DELETE_SLEEP_MS = 50 //!< Sleep time (in ms) between log file delete attempts
51 static std::string formatErrMsg (DWORD error, const char* msg)
53 std::ostringstream str;
57 # error Unicode not supported.
60 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
61 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msgBuf, 0, DE_NULL) > 0)
62 str << msg << ", error " << error << ": " << msgBuf;
64 str << msg << ", error " << error;
69 Error::Error (DWORD error, const char* msg)
70 : std::runtime_error(formatErrMsg(error, msg))
77 Event::Event (bool manualReset, bool initialState)
80 m_handle = CreateEvent(NULL, manualReset ? TRUE : FALSE, initialState ? TRUE : FALSE, NULL);
82 throw Error(GetLastError(), "CreateEvent() failed");
87 CloseHandle(m_handle);
90 void Event::setSignaled (void)
92 if (!SetEvent(m_handle))
93 throw Error(GetLastError(), "SetEvent() failed");
96 void Event::reset (void)
98 if (!ResetEvent(m_handle))
99 throw Error(GetLastError(), "ResetEvent() failed");
104 CaseListWriter::CaseListWriter (void)
105 : m_dst (INVALID_HANDLE_VALUE)
106 , m_cancelEvent (true, false)
110 CaseListWriter::~CaseListWriter (void)
114 void CaseListWriter::start (const char* caseList, HANDLE dst)
116 DE_ASSERT(!isStarted());
120 int caseListSize = (int)strlen(caseList)+1;
121 m_caseList.resize(caseListSize);
122 std::copy(caseList, caseList+caseListSize, m_caseList.begin());
127 void CaseListWriter::run (void)
131 Event ioEvent (true, false); // Manual reset, non-signaled state.
132 HANDLE waitHandles[] = { ioEvent.getHandle(), m_cancelEvent.getHandle() };
133 OVERLAPPED overlapped;
136 deMemset(&overlapped, 0, sizeof(overlapped));
137 overlapped.hEvent = ioEvent.getHandle();
139 while (curPos < (int)m_caseList.size())
141 const int maxWriteSize = 4096;
142 const int numToWrite = de::min(maxWriteSize, (int)m_caseList.size() - curPos);
145 if (!WriteFile(m_dst, &m_caseList[curPos], (DWORD)numToWrite, NULL, &overlapped))
147 DWORD err = GetLastError();
148 if (err != ERROR_IO_PENDING)
149 throw Error(err, "WriteFile() failed");
152 waitRes = WaitForMultipleObjects(DE_LENGTH_OF_ARRAY(waitHandles), &waitHandles[0], FALSE, INFINITE);
154 if (waitRes == WAIT_OBJECT_0)
156 DWORD numBytesWritten = 0;
158 // \note GetOverlappedResult() will fail with ERROR_IO_INCOMPLETE if IO event is not complete (should be).
159 if (!GetOverlappedResult(m_dst, &overlapped, &numBytesWritten, FALSE))
160 throw Error(GetLastError(), "GetOverlappedResult() failed");
162 if (numBytesWritten == 0)
163 throw Error(GetLastError(), "Writing to pipe failed (pipe closed?)");
165 curPos += (int)numBytesWritten;
167 else if (waitRes == WAIT_OBJECT_0 + 1)
170 if (!CancelIo(m_dst))
171 throw Error(GetLastError(), "CancelIo() failed");
175 throw Error(GetLastError(), "WaitForMultipleObjects() failed");
178 catch (const std::exception& e)
180 // \todo [2013-08-13 pyry] What to do about this?
181 printf("win32::CaseListWriter::run(): %s\n", e.what());
185 void CaseListWriter::stop (void)
188 return; // Nothing to do.
190 m_cancelEvent.setSignaled();
195 m_cancelEvent.reset();
197 m_dst = INVALID_HANDLE_VALUE;
202 FileReader::FileReader (ThreadedByteBuffer* dst)
204 , m_handle (INVALID_HANDLE_VALUE)
205 , m_cancelEvent (false, false)
209 FileReader::~FileReader (void)
213 void FileReader::start (HANDLE file)
215 DE_ASSERT(!isStarted());
222 void FileReader::run (void)
226 Event ioEvent (true, false); // Manual reset, not signaled state.
227 HANDLE waitHandles[] = { ioEvent.getHandle(), m_cancelEvent.getHandle() };
228 OVERLAPPED overlapped;
229 std::vector<deUint8> tmpBuf (FILEREADER_TMP_BUFFER_SIZE);
230 deUint64 offset = 0; // Overlapped IO requires manual offset keeping.
232 deMemset(&overlapped, 0, sizeof(overlapped));
233 overlapped.hEvent = ioEvent.getHandle();
237 DWORD numBytesRead = 0;
240 overlapped.Offset = (DWORD)(offset & 0xffffffffu);
241 overlapped.OffsetHigh = (DWORD)(offset >> 32);
243 if (!ReadFile(m_handle, &tmpBuf[0], (DWORD)tmpBuf.size(), NULL, &overlapped))
245 DWORD err = GetLastError();
247 if (err == ERROR_BROKEN_PIPE)
249 else if (err == ERROR_HANDLE_EOF)
251 if (m_dstBuf->isCanceled())
254 deSleep(FILEREADER_IDLE_SLEEP);
256 if (m_dstBuf->isCanceled())
261 else if (err != ERROR_IO_PENDING)
262 throw Error(err, "ReadFile() failed");
265 waitRes = WaitForMultipleObjects(DE_LENGTH_OF_ARRAY(waitHandles), &waitHandles[0], FALSE, INFINITE);
267 if (waitRes == WAIT_OBJECT_0)
269 // \note GetOverlappedResult() will fail with ERROR_IO_INCOMPLETE if IO event is not complete (should be).
270 if (!GetOverlappedResult(m_handle, &overlapped, &numBytesRead, FALSE))
272 DWORD err = GetLastError();
274 if (err == ERROR_HANDLE_EOF)
276 // End of file - for now.
277 // \note Should check for end of buffer here, or otherwise may end up in infinite loop.
278 if (m_dstBuf->isCanceled())
281 deSleep(FILEREADER_IDLE_SLEEP);
283 if (m_dstBuf->isCanceled())
288 else if (err == ERROR_BROKEN_PIPE)
291 throw Error(err, "GetOverlappedResult() failed");
294 if (numBytesRead == 0)
295 throw Error(GetLastError(), "Reading from file failed");
297 offset += (deUint64)numBytesRead;
299 else if (waitRes == WAIT_OBJECT_0 + 1)
302 if (!CancelIo(m_handle))
303 throw Error(GetLastError(), "CancelIo() failed");
307 throw Error(GetLastError(), "WaitForMultipleObjects() failed");
311 m_dstBuf->write((int)numBytesRead, &tmpBuf[0]);
314 catch (const ThreadedByteBuffer::CanceledException&)
321 catch (const std::exception& e)
323 // \todo [2013-08-13 pyry] What to do?
324 printf("win32::FileReader::run(): %s\n", e.what());
328 void FileReader::stop (void)
331 return; // Nothing to do.
333 m_cancelEvent.setSignaled();
338 m_cancelEvent.reset();
340 m_handle = INVALID_HANDLE_VALUE;
345 TestLogReader::TestLogReader (void)
346 : m_logBuffer (LOG_BUFFER_BLOCK_SIZE, LOG_BUFFER_NUM_BLOCKS)
347 , m_logFile (INVALID_HANDLE_VALUE)
348 , m_reader (&m_logBuffer)
352 TestLogReader::~TestLogReader (void)
354 if (m_logFile != INVALID_HANDLE_VALUE)
355 CloseHandle(m_logFile);
358 void TestLogReader::start (const char* filename)
360 DE_ASSERT(m_logFile == INVALID_HANDLE_VALUE && !m_reader.isStarted());
362 m_logFile = CreateFile(filename,
364 FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
367 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
370 if (m_logFile == INVALID_HANDLE_VALUE)
371 throw Error(GetLastError(), "Failed to open log file");
373 m_reader.start(m_logFile);
376 void TestLogReader::stop (void)
378 if (!m_reader.isStarted())
379 return; // Nothing to do.
381 m_logBuffer.cancel();
384 CloseHandle(m_logFile);
385 m_logFile = INVALID_HANDLE_VALUE;
392 Process::Process (void)
393 : m_state (STATE_NOT_STARTED)
395 , m_standardIn (INVALID_HANDLE_VALUE)
396 , m_standardOut (INVALID_HANDLE_VALUE)
397 , m_standardErr (INVALID_HANDLE_VALUE)
399 deMemset(&m_procInfo, 0, sizeof(m_procInfo));
402 Process::~Process (void)
419 void Process::cleanupHandles (void)
421 DE_ASSERT(!isRunning());
423 if (m_standardErr != INVALID_HANDLE_VALUE)
424 CloseHandle(m_standardErr);
426 if (m_standardOut != INVALID_HANDLE_VALUE)
427 CloseHandle(m_standardOut);
429 if (m_standardIn != INVALID_HANDLE_VALUE)
430 CloseHandle(m_standardIn);
432 if (m_procInfo.hProcess)
433 CloseHandle(m_procInfo.hProcess);
435 if (m_procInfo.hThread)
436 CloseHandle(m_procInfo.hThread);
438 m_standardErr = INVALID_HANDLE_VALUE;
439 m_standardOut = INVALID_HANDLE_VALUE;
440 m_standardIn = INVALID_HANDLE_VALUE;
442 deMemset(&m_procInfo, 0, sizeof(m_procInfo));
445 __declspec(thread) static int t_pipeNdx = 0;
447 static void createPipeWithOverlappedIO (HANDLE* readHandleOut, HANDLE* writeHandleOut, deUint32 readMode, deUint32 writeMode, SECURITY_ATTRIBUTES* securityAttr)
449 const int defaultBufSize = 4096;
454 DE_ASSERT(((readMode | writeMode) & ~FILE_FLAG_OVERLAPPED) == 0);
456 deSprintf(pipeName, sizeof(pipeName), "\\\\.\\Pipe\\dEQP-ExecServer-%08x-%08x-%08x",
457 GetCurrentProcessId(),
458 GetCurrentThreadId(),
461 readHandle = CreateNamedPipe(pipeName, /* Pipe name. */
462 PIPE_ACCESS_INBOUND|readMode, /* Open mode. */
463 PIPE_TYPE_BYTE|PIPE_WAIT, /* Pipe flags. */
464 1, /* Max number of instances. */
465 defaultBufSize, /* Output buffer size. */
466 defaultBufSize, /* Input buffer size. */
467 0, /* Use default timeout. */
470 if (readHandle == INVALID_HANDLE_VALUE)
471 throw Error(GetLastError(), "CreateNamedPipe() failed");
473 writeHandle = CreateFile(pipeName,
474 GENERIC_WRITE, /* Access mode. */
477 OPEN_EXISTING, /* Assume existing object. */
478 FILE_ATTRIBUTE_NORMAL|writeMode, /* Open mode / flags. */
479 DE_NULL /* Template file. */);
481 if (writeHandle == INVALID_HANDLE_VALUE)
483 DWORD openErr = GetLastError();
484 CloseHandle(readHandle);
485 throw Error(openErr, "Failed to open created pipe, CreateFile() failed");
488 *readHandleOut = readHandle;
489 *writeHandleOut = writeHandle;
492 void Process::start (const char* commandLine, const char* workingDirectory)
495 HANDLE stdInRead = INVALID_HANDLE_VALUE;
496 HANDLE stdInWrite = INVALID_HANDLE_VALUE;
497 HANDLE stdOutRead = INVALID_HANDLE_VALUE;
498 HANDLE stdOutWrite = INVALID_HANDLE_VALUE;
499 HANDLE stdErrRead = INVALID_HANDLE_VALUE;
500 HANDLE stdErrWrite = INVALID_HANDLE_VALUE;
502 if (m_state == STATE_RUNNING)
503 throw std::runtime_error("Process already running");
504 else if (m_state == STATE_FINISHED)
506 // Process finished, clean up old cruft.
508 m_state = STATE_NOT_STARTED;
514 SECURITY_ATTRIBUTES securityAttr;
515 STARTUPINFO startInfo;
517 deMemset(&startInfo, 0, sizeof(startInfo));
518 deMemset(&securityAttr, 0, sizeof(securityAttr));
520 // Security attributes for inheriting handle.
521 securityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
522 securityAttr.bInheritHandle = TRUE;
523 securityAttr.lpSecurityDescriptor = DE_NULL;
525 createPipeWithOverlappedIO(&stdInRead, &stdInWrite, 0, FILE_FLAG_OVERLAPPED, &securityAttr);
526 createPipeWithOverlappedIO(&stdOutRead, &stdOutWrite, FILE_FLAG_OVERLAPPED, 0, &securityAttr);
527 createPipeWithOverlappedIO(&stdErrRead, &stdErrWrite, FILE_FLAG_OVERLAPPED, 0, &securityAttr);
529 if (!SetHandleInformation(stdInWrite, HANDLE_FLAG_INHERIT, 0) ||
530 !SetHandleInformation(stdOutRead, HANDLE_FLAG_INHERIT, 0) ||
531 !SetHandleInformation(stdErrRead, HANDLE_FLAG_INHERIT, 0))
532 throw Error(GetLastError(), "SetHandleInformation() failed");
534 // Startup info for process.
535 startInfo.cb = sizeof(startInfo);
536 startInfo.hStdError = stdErrWrite;
537 startInfo.hStdOutput = stdOutWrite;
538 startInfo.hStdInput = stdInRead;
539 startInfo.dwFlags |= STARTF_USESTDHANDLES;
541 if (!CreateProcess(DE_NULL, (LPTSTR)commandLine, DE_NULL, DE_NULL, TRUE /* inherit handles */, 0, DE_NULL, workingDirectory, &startInfo, &m_procInfo))
542 throw Error(GetLastError(), "CreateProcess() failed");
546 if (stdInRead != INVALID_HANDLE_VALUE) CloseHandle(stdInRead);
547 if (stdInWrite != INVALID_HANDLE_VALUE) CloseHandle(stdInWrite);
548 if (stdOutRead != INVALID_HANDLE_VALUE) CloseHandle(stdOutRead);
549 if (stdOutWrite != INVALID_HANDLE_VALUE) CloseHandle(stdOutWrite);
550 if (stdErrRead != INVALID_HANDLE_VALUE) CloseHandle(stdErrRead);
551 if (stdErrWrite != INVALID_HANDLE_VALUE) CloseHandle(stdErrWrite);
555 // Store handles to be kept.
556 m_standardIn = stdInWrite;
557 m_standardOut = stdOutRead;
558 m_standardErr = stdErrRead;
560 // Close other ends of handles.
561 CloseHandle(stdErrWrite);
562 CloseHandle(stdOutWrite);
563 CloseHandle(stdInRead);
565 m_state = STATE_RUNNING;
568 bool Process::isRunning (void)
570 if (m_state == STATE_RUNNING)
573 BOOL result = GetExitCodeProcess(m_procInfo.hProcess, (LPDWORD)&exitCode);
576 throw Error(GetLastError(), "GetExitCodeProcess() failed");
578 if (exitCode == STILL_ACTIVE)
583 m_exitCode = exitCode;
584 m_state = STATE_FINISHED;
592 void Process::waitForFinish (void)
594 if (m_state == STATE_RUNNING)
596 if (WaitForSingleObject(m_procInfo.hProcess, INFINITE) != WAIT_OBJECT_0)
597 throw Error(GetLastError(), "Waiting for process failed, WaitForSingleObject() failed");
600 throw std::runtime_error("Process is still alive");
603 throw std::runtime_error("Process is not running");
606 void Process::stopProcess (bool kill)
608 if (m_state == STATE_RUNNING)
610 if (!TerminateProcess(m_procInfo.hProcess, kill ? -1 : 0))
611 throw Error(GetLastError(), "TerminateProcess() failed");
614 throw std::runtime_error("Process is not running");
617 void Process::terminate (void)
622 void Process::kill (void)
629 Win32TestProcess::Win32TestProcess (void)
630 : m_process (DE_NULL)
631 , m_processStartTime (0)
632 , m_infoBuffer (INFO_BUFFER_BLOCK_SIZE, INFO_BUFFER_NUM_BLOCKS)
633 , m_stdOutReader (&m_infoBuffer)
634 , m_stdErrReader (&m_infoBuffer)
638 Win32TestProcess::~Win32TestProcess (void)
643 void Win32TestProcess::start (const char* name, const char* params, const char* workingDir, const char* caseList)
645 bool hasCaseList = strlen(caseList) > 0;
647 XS_CHECK(!m_process);
649 de::FilePath logFilePath = de::FilePath::join(workingDir, "TestResults.qpa");
650 m_logFileName = logFilePath.getPath();
652 // Remove old file if such exists.
653 // \note Sometimes on Windows the test process dies slowly and may not release handle to log file
654 // until a bit later.
655 // \todo [2013-07-15 pyry] This should be solved by improving deProcess and killing all child processes as well.
658 while (tryNdx < MAX_OLD_LOGFILE_DELETE_ATTEMPTS && deFileExists(m_logFileName.c_str()))
660 if (deDeleteFile(m_logFileName.c_str()))
662 deSleep(LOGFILE_DELETE_SLEEP_MS);
666 if (deFileExists(m_logFileName.c_str()))
667 throw TestProcessException(string("Failed to remove '") + m_logFileName + "'");
670 // Construct command line.
671 string cmdLine = de::FilePath(name).isAbsolutePath() ? name : de::FilePath::join(workingDir, name).normalize().getPath();
672 cmdLine += string(" --deqp-log-filename=") + logFilePath.getBaseName();
675 cmdLine += " --deqp-stdin-caselist";
677 if (strlen(params) > 0)
678 cmdLine += string(" ") + params;
680 DE_ASSERT(!m_process);
681 m_process = new win32::Process();
685 m_process->start(cmdLine.c_str(), strlen(workingDir) > 0 ? workingDir : DE_NULL);
687 catch (const std::exception& e)
691 throw TestProcessException(e.what());
694 m_processStartTime = deGetMicroseconds();
696 // Create stdout & stderr readers.
697 m_stdOutReader.start(m_process->getStdOut());
698 m_stdErrReader.start(m_process->getStdErr());
700 // Start case list writer.
702 m_caseListWriter.start(caseList, m_process->getStdIn());
705 void Win32TestProcess::terminate (void)
713 catch (const std::exception& e)
715 printf("Win32TestProcess::terminate(): Failed to kill process: %s\n", e.what());
720 void Win32TestProcess::cleanup (void)
722 m_caseListWriter.stop();
724 // \note Buffers must be canceled before stopping readers.
725 m_infoBuffer.cancel();
727 m_stdErrReader.stop();
728 m_stdOutReader.stop();
729 m_testLogReader.stop();
732 m_infoBuffer.clear();
738 if (m_process->isRunning())
741 m_process->waitForFinish();
744 catch (const std::exception& e)
746 printf("Win32TestProcess::cleanup(): Failed to kill process: %s\n", e.what());
754 int Win32TestProcess::readTestLog (deUint8* dst, int numBytes)
756 if (!m_testLogReader.isRunning())
758 if (deGetMicroseconds() - m_processStartTime > LOG_FILE_TIMEOUT*1000)
760 // Timeout, kill process.
762 return 0; // \todo [2013-08-13 pyry] Throw exception?
765 if (!deFileExists(m_logFileName.c_str()))
769 m_testLogReader.start(m_logFileName.c_str());
772 DE_ASSERT(m_testLogReader.isRunning());
773 return m_testLogReader.read(dst, numBytes);
776 bool Win32TestProcess::isRunning (void)
779 return m_process->isRunning();
784 int Win32TestProcess::getExitCode (void) const
787 return m_process->getExitCode();