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