• 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 // 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 class ProcessHandle final : angle::NonCopyable
89 {
90   public:
91     ProcessHandle();
92     ProcessHandle(Process *process);
93     ProcessHandle(const std::vector<const char *> &args, bool captureStdout, bool captureStderr);
94     ~ProcessHandle();
95     ProcessHandle(ProcessHandle &&other);
96     ProcessHandle &operator=(ProcessHandle &&rhs);
97 
98     Process *operator->() { return mProcess; }
99     const Process *operator->() const { return mProcess; }
100 
101     operator bool() const { return mProcess != nullptr; }
102 
103     void reset();
104 
105   private:
106     Process *mProcess;
107 };
108 
109 // Launch a process and optionally get the output. Uses a vector of c strings as command line
110 // arguments to the child process. Returns a Process handle which can be used to retrieve
111 // the stdout and stderr outputs as well as the exit code.
112 //
113 // Pass false for stdoutOut/stderrOut if you don't need to capture them.
114 //
115 // On success, returns a Process pointer with started() == true.
116 // On failure, returns a Process pointer with started() == false.
117 Process *LaunchProcess(const std::vector<const char *> &args,
118                        bool captureStdout,
119                        bool captureStderr);
120 
121 int NumberOfProcessors();
122 
123 }  // namespace angle
124 
125 #endif  // UTIL_TEST_UTILS_H_
126