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