//1// Copyright 2014 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// test_utils.h: declaration of OS-specific utility functions78#ifndef UTIL_TEST_UTILS_H_9#define UTIL_TEST_UTILS_H_1011#include <functional>12#include <string>13#include <vector>1415#include "common/angleutils.h"16#include "util/Timer.h"1718// DeleteFile is defined in the Windows headers to either DeleteFileA or DeleteFileW. Make sure19// there are no conflicts.20#undef DeleteFile2122namespace angle23{24// Cross platform equivalent of the Windows Sleep function25void Sleep(unsigned int milliseconds);2627void SetLowPriorityProcess();2829// Write a debug message, either to a standard output or Debug window.30void WriteDebugMessage(const char *format, ...);3132// Set thread affinity and priority.33bool StabilizeCPUForBenchmarking();3435// Set a crash handler to print stack traces.36using CrashCallback = std::function<void()>;37void InitCrashHandler(CrashCallback *callback);38void TerminateCrashHandler();3940// Print a stack back trace.41void PrintStackBacktrace();4243// Get temporary directory.44bool GetTempDir(char *tempDirOut, uint32_t maxDirNameLen);4546// Creates a temporary file. The full path is placed in |tempFileNameOut|, and the47// function returns true if was successful in creating the file. The file will48// be empty and all handles closed after this function returns.49bool CreateTemporaryFile(char *tempFileNameOut, uint32_t maxFileNameLen);5051// Same as CreateTemporaryFile but the file is created in |dir|.52bool CreateTemporaryFileInDir(const char *dir, char *tempFileNameOut, uint32_t maxFileNameLen);5354// Deletes a file or directory.55bool DeleteFile(const char *path);5657// Reads a file contents into a string.58bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen);5960// Compute a file's size.61bool GetFileSize(const char *filePath, uint32_t *sizeOut);6263class ProcessHandle;6465class Process : angle::NonCopyable66{67public:68virtual bool started() = 0;69virtual bool finished() = 0;70virtual bool finish() = 0;71virtual bool kill() = 0;72virtual int getExitCode() = 0;7374double getElapsedTimeSeconds() const { return mTimer.getElapsedTime(); }75const std::string &getStdout() const { return mStdout; }76const std::string &getStderr() const { return mStderr; }7778protected:79friend class ProcessHandle;80virtual ~Process();8182Timer mTimer;83std::string mStdout;84std::string mStderr;85};8687enum class ProcessOutputCapture88{89Nothing,90// Capture stdout only91StdoutOnly,92// Capture stdout, and pipe stderr to stdout93StdoutAndStderrInterleaved,94// Capture stdout and stderr separately95StdoutAndStderrSeparately,96};9798class ProcessHandle final : angle::NonCopyable99{100public:101ProcessHandle();102ProcessHandle(Process *process);103ProcessHandle(const std::vector<const char *> &args, ProcessOutputCapture captureOutput);104~ProcessHandle();105ProcessHandle(ProcessHandle &&other);106ProcessHandle &operator=(ProcessHandle &&rhs);107108Process *operator->() { return mProcess; }109const Process *operator->() const { return mProcess; }110111operator bool() const { return mProcess != nullptr; }112113void reset();114115private:116Process *mProcess;117};118119// Launch a process and optionally get the output. Uses a vector of c strings as command line120// arguments to the child process. Returns a Process handle which can be used to retrieve121// the stdout and stderr outputs as well as the exit code.122//123// Pass false for stdoutOut/stderrOut if you don't need to capture them.124//125// On success, returns a Process pointer with started() == true.126// On failure, returns a Process pointer with started() == false.127Process *LaunchProcess(const std::vector<const char *> &args, ProcessOutputCapture captureOutput);128129int NumberOfProcessors();130131const char *GetNativeEGLLibraryNameWithExtension();132133// Intercept Metal shader cache access to avoid slow caching mechanism that caused the test timeout134// in the past. Note:135// - If there is NO "--skip-file-hooking" switch in the argument list:136// - This function will re-launch the app with additional argument "--skip-file-hooking".137// - The running process's image & memory will be re-created.138// - If there is "--skip-file-hooking" switch in the argument list, this function will do nothing.139#if defined(ANGLE_PLATFORM_APPLE)140void InitMetalFileAPIHooking(int argc, char **argv);141#endif142} // namespace angle143144#endif // UTIL_TEST_UTILS_H_145146147