1 // 2 // Copyright 2014 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // test_utils.h: declaration of OS-specific utility functions 8 9 #ifndef UTIL_TEST_UTILS_H_ 10 #define UTIL_TEST_UTILS_H_ 11 12 #include <functional> 13 #include <string> 14 #include <vector> 15 16 #include "common/angleutils.h" 17 #include "util/Timer.h" 18 19 // DeleteFile is defined in the Windows headers to either DeleteFileA or DeleteFileW. Make sure 20 // there are no conflicts. 21 #undef DeleteFile 22 23 namespace angle 24 { 25 // Cross platform equivalent of the Windows Sleep function 26 void Sleep(unsigned int milliseconds); 27 28 void SetLowPriorityProcess(); 29 30 // Write a debug message, either to a standard output or Debug window. 31 void WriteDebugMessage(const char *format, ...); 32 33 // Set thread affinity and priority. 34 bool StabilizeCPUForBenchmarking(); 35 36 // Set a crash handler to print stack traces. 37 using CrashCallback = std::function<void()>; 38 void InitCrashHandler(CrashCallback *callback); 39 void TerminateCrashHandler(); 40 41 // Print a stack back trace. 42 void PrintStackBacktrace(); 43 44 // Get temporary directory. 45 bool GetTempDir(char *tempDirOut, uint32_t maxDirNameLen); 46 47 // Creates a temporary file. The full path is placed in |path|, and the 48 // function returns true if was successful in creating the file. The file will 49 // be empty and all handles closed after this function returns. 50 bool CreateTemporaryFile(char *tempFileNameOut, uint32_t maxFileNameLen); 51 52 // Same as CreateTemporaryFile but the file is created in |dir|. 53 bool CreateTemporaryFileInDir(const char *dir, char *tempFileNameOut, uint32_t maxFileNameLen); 54 55 // Deletes a file or directory. 56 bool DeleteFile(const char *path); 57 58 // Reads a file contents into a string. 59 bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen); 60 61 // Compute a file's size. 62 bool GetFileSize(const char *filePath, uint32_t *sizeOut); 63 64 class ProcessHandle; 65 66 class Process : angle::NonCopyable 67 { 68 public: 69 virtual bool started() = 0; 70 virtual bool finished() = 0; 71 virtual bool finish() = 0; 72 virtual bool kill() = 0; 73 virtual int getExitCode() = 0; 74 getElapsedTimeSeconds()75 double getElapsedTimeSeconds() const { return mTimer.getElapsedTime(); } getStdout()76 const std::string &getStdout() const { return mStdout; } getStderr()77 const std::string &getStderr() const { return mStderr; } 78 79 protected: 80 friend class ProcessHandle; 81 virtual ~Process(); 82 83 Timer mTimer; 84 std::string mStdout; 85 std::string mStderr; 86 }; 87 88 enum class ProcessOutputCapture 89 { 90 Nothing, 91 // Capture stdout only 92 StdoutOnly, 93 // Capture stdout, and pipe stderr to stdout 94 StdoutAndStderrInterleaved, 95 // Capture stdout and stderr separately 96 StdoutAndStderrSeparately, 97 }; 98 99 class ProcessHandle final : angle::NonCopyable 100 { 101 public: 102 ProcessHandle(); 103 ProcessHandle(Process *process); 104 ProcessHandle(const std::vector<const char *> &args, ProcessOutputCapture captureOutput); 105 ~ProcessHandle(); 106 ProcessHandle(ProcessHandle &&other); 107 ProcessHandle &operator=(ProcessHandle &&rhs); 108 109 Process *operator->() { return mProcess; } 110 const Process *operator->() const { return mProcess; } 111 112 operator bool() const { return mProcess != nullptr; } 113 114 void reset(); 115 116 private: 117 Process *mProcess; 118 }; 119 120 // Launch a process and optionally get the output. Uses a vector of c strings as command line 121 // arguments to the child process. Returns a Process handle which can be used to retrieve 122 // the stdout and stderr outputs as well as the exit code. 123 // 124 // Pass false for stdoutOut/stderrOut if you don't need to capture them. 125 // 126 // On success, returns a Process pointer with started() == true. 127 // On failure, returns a Process pointer with started() == false. 128 Process *LaunchProcess(const std::vector<const char *> &args, ProcessOutputCapture captureOutput); 129 130 int NumberOfProcessors(); 131 132 const char *GetNativeEGLLibraryNameWithExtension(); 133 134 // Intercept Metal shader cache access to avoid slow caching mechanism that caused the test timeout 135 // in the past. Note: 136 // - If there is NO "--skip-file-hooking" switch in the argument list: 137 // - This function will re-launch the app with additional argument "--skip-file-hooking". 138 // - The running process's image & memory will be re-created. 139 // - If there is "--skip-file-hooking" switch in the argument list, this function will do nothing. 140 #if defined(ANGLE_PLATFORM_APPLE) 141 void InitMetalFileAPIHooking(int argc, char **argv); 142 #endif 143 } // namespace angle 144 145 #endif // UTIL_TEST_UTILS_H_ 146