1 //
2 // Copyright 2015 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_posix.cpp: Implementation of OS-specific functions for Posix systems
8
9 #include "util/test_utils.h"
10
11 #include <dlfcn.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <sched.h>
15 #include <signal.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <cstdarg>
22 #include <cstring>
23 #include <iostream>
24
25 #include "common/debug.h"
26 #include "common/platform.h"
27 #include "common/system_utils.h"
28
29 #if !defined(ANGLE_PLATFORM_FUCHSIA)
30 # include <sys/resource.h>
31 #endif
32
33 #if defined(ANGLE_PLATFORM_MACOS)
34 # include <crt_externs.h>
35 #endif
36
37 namespace angle
38 {
39 namespace
40 {
41
42 #if defined(ANGLE_PLATFORM_MACOS)
43 // Argument to skip the file hooking step. Might be automatically added by InitMetalFileAPIHooking()
44 constexpr char kSkipFileHookingArg[] = "--skip-file-hooking";
45 #endif
46
47 struct ScopedPipe
48 {
~ScopedPipeangle::__anon19ef861f0111::ScopedPipe49 ~ScopedPipe()
50 {
51 closeEndPoint(0);
52 closeEndPoint(1);
53 }
54
closeEndPointangle::__anon19ef861f0111::ScopedPipe55 void closeEndPoint(int index)
56 {
57 if (fds[index] >= 0)
58 {
59 close(fds[index]);
60 fds[index] = -1;
61 }
62 }
63
validangle::__anon19ef861f0111::ScopedPipe64 bool valid() const { return fds[0] != -1 || fds[1] != -1; }
65
66 int fds[2] = {
67 -1,
68 -1,
69 };
70 };
71
72 enum class ReadResult
73 {
74 NoData,
75 GotData,
76 };
77
ReadFromFile(int fd,std::string * out)78 ReadResult ReadFromFile(int fd, std::string *out)
79 {
80 constexpr size_t kBufSize = 2048;
81 char buffer[kBufSize];
82 ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
83
84 if (bytesRead < 0 && errno == EINTR)
85 {
86 return ReadResult::GotData;
87 }
88
89 if (bytesRead <= 0)
90 {
91 return ReadResult::NoData;
92 }
93
94 out->append(buffer, bytesRead);
95 return ReadResult::GotData;
96 }
97
ReadEntireFile(int fd,std::string * out)98 void ReadEntireFile(int fd, std::string *out)
99 {
100 while (ReadFromFile(fd, out) == ReadResult::GotData)
101 {
102 }
103 }
104
105 class PosixProcess : public Process
106 {
107 public:
PosixProcess(const std::vector<const char * > & commandLineArgs,ProcessOutputCapture captureOutput)108 PosixProcess(const std::vector<const char *> &commandLineArgs,
109 ProcessOutputCapture captureOutput)
110 {
111 if (commandLineArgs.empty())
112 {
113 return;
114 }
115
116 const bool captureStdout = captureOutput != ProcessOutputCapture::Nothing;
117 const bool captureStderr =
118 captureOutput == ProcessOutputCapture::StdoutAndStderrInterleaved ||
119 captureOutput == ProcessOutputCapture::StdoutAndStderrSeparately;
120 const bool pipeStderrToStdout =
121 captureOutput == ProcessOutputCapture::StdoutAndStderrInterleaved;
122
123 // Create pipes for stdout and stderr.
124 if (captureStdout)
125 {
126 if (pipe(mStdoutPipe.fds) != 0)
127 {
128 std::cerr << "Error calling pipe: " << errno << "\n";
129 return;
130 }
131 if (fcntl(mStdoutPipe.fds[0], F_SETFL, O_NONBLOCK) == -1)
132 {
133 std::cerr << "Error calling fcntl: " << errno << "\n";
134 return;
135 }
136 }
137 if (captureStderr && !pipeStderrToStdout)
138 {
139 if (pipe(mStderrPipe.fds) != 0)
140 {
141 std::cerr << "Error calling pipe: " << errno << "\n";
142 return;
143 }
144 if (fcntl(mStderrPipe.fds[0], F_SETFL, O_NONBLOCK) == -1)
145 {
146 std::cerr << "Error calling fcntl: " << errno << "\n";
147 return;
148 }
149 }
150
151 mPID = fork();
152 if (mPID < 0)
153 {
154 return;
155 }
156
157 mStarted = true;
158 mTimer.start();
159
160 if (mPID == 0)
161 {
162 // Child. Execute the application.
163
164 // Redirect stdout and stderr to the pipe fds.
165 if (captureStdout)
166 {
167 if (dup2(mStdoutPipe.fds[1], STDOUT_FILENO) < 0)
168 {
169 _exit(errno);
170 }
171 mStdoutPipe.closeEndPoint(1);
172 }
173 if (pipeStderrToStdout)
174 {
175 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
176 {
177 _exit(errno);
178 }
179 }
180 else if (captureStderr)
181 {
182 if (dup2(mStderrPipe.fds[1], STDERR_FILENO) < 0)
183 {
184 _exit(errno);
185 }
186 mStderrPipe.closeEndPoint(1);
187 }
188
189 // Execute the application, which doesn't return unless failed. Note: execv takes argv
190 // as `char * const *` for historical reasons. It is safe to const_cast it:
191 //
192 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
193 //
194 // > The statement about argv[] and envp[] being constants is included to make explicit
195 // to future writers of language bindings that these objects are completely constant.
196 // Due to a limitation of the ISO C standard, it is not possible to state that idea in
197 // standard C. Specifying two levels of const- qualification for the argv[] and envp[]
198 // parameters for the exec functions may seem to be the natural choice, given that these
199 // functions do not modify either the array of pointers or the characters to which the
200 // function points, but this would disallow existing correct code. Instead, only the
201 // array of pointers is noted as constant.
202 std::vector<char *> args;
203 for (const char *arg : commandLineArgs)
204 {
205 args.push_back(const_cast<char *>(arg));
206 }
207 args.push_back(nullptr);
208
209 execv(commandLineArgs[0], args.data());
210 std::cerr << "Error calling evecv: " << errno;
211 _exit(errno);
212 }
213 // Parent continues execution.
214 mStdoutPipe.closeEndPoint(1);
215 mStderrPipe.closeEndPoint(1);
216 }
217
~PosixProcess()218 ~PosixProcess() override {}
219
started()220 bool started() override { return mStarted; }
221
finish()222 bool finish() override
223 {
224 if (!mStarted)
225 {
226 return false;
227 }
228
229 if (mFinished)
230 {
231 return true;
232 }
233
234 while (!finished())
235 {
236 angle::Sleep(1);
237 }
238
239 return true;
240 }
241
finished()242 bool finished() override
243 {
244 if (!mStarted)
245 {
246 return false;
247 }
248
249 if (mFinished)
250 {
251 return true;
252 }
253
254 int status = 0;
255 pid_t returnedPID = ::waitpid(mPID, &status, WNOHANG);
256
257 if (returnedPID == -1 && errno != ECHILD)
258 {
259 std::cerr << "Error calling waitpid: " << ::strerror(errno) << "\n";
260 return true;
261 }
262
263 if (returnedPID == mPID)
264 {
265 mFinished = true;
266 mTimer.stop();
267 readPipes();
268 mExitCode = WEXITSTATUS(status);
269 return true;
270 }
271
272 if (mStdoutPipe.valid())
273 {
274 ReadEntireFile(mStdoutPipe.fds[0], &mStdout);
275 }
276
277 if (mStderrPipe.valid())
278 {
279 ReadEntireFile(mStderrPipe.fds[0], &mStderr);
280 }
281
282 return false;
283 }
284
getExitCode()285 int getExitCode() override { return mExitCode; }
286
kill()287 bool kill() override
288 {
289 if (!mStarted)
290 {
291 return false;
292 }
293
294 if (finished())
295 {
296 return true;
297 }
298
299 return (::kill(mPID, SIGTERM) == 0);
300 }
301
302 private:
readPipes()303 void readPipes()
304 {
305 // Close the write end of the pipes, so EOF can be generated when child exits.
306 // Then read back the output of the child.
307 if (mStdoutPipe.valid())
308 {
309 ReadEntireFile(mStdoutPipe.fds[0], &mStdout);
310 }
311 if (mStderrPipe.valid())
312 {
313 ReadEntireFile(mStderrPipe.fds[0], &mStderr);
314 }
315 }
316
317 bool mStarted = false;
318 bool mFinished = false;
319 ScopedPipe mStdoutPipe;
320 ScopedPipe mStderrPipe;
321 int mExitCode = 0;
322 pid_t mPID = -1;
323 };
324 } // anonymous namespace
325
Sleep(unsigned int milliseconds)326 void Sleep(unsigned int milliseconds)
327 {
328 // On Windows Sleep(0) yields while it isn't guaranteed by Posix's sleep
329 // so we replicate Windows' behavior with an explicit yield.
330 if (milliseconds == 0)
331 {
332 sched_yield();
333 }
334 else
335 {
336 long milliseconds_long = milliseconds;
337 timespec sleepTime = {
338 .tv_sec = milliseconds_long / 1000,
339 .tv_nsec = (milliseconds_long % 1000) * 1000000,
340 };
341
342 nanosleep(&sleepTime, nullptr);
343 }
344 }
345
SetLowPriorityProcess()346 void SetLowPriorityProcess()
347 {
348 #if !defined(ANGLE_PLATFORM_FUCHSIA)
349 setpriority(PRIO_PROCESS, getpid(), 10);
350 #endif
351 }
352
WriteDebugMessage(const char * format,...)353 void WriteDebugMessage(const char *format, ...)
354 {
355 va_list vararg;
356 va_start(vararg, format);
357 vfprintf(stderr, format, vararg);
358 va_end(vararg);
359 }
360
StabilizeCPUForBenchmarking()361 bool StabilizeCPUForBenchmarking()
362 {
363 #if !defined(ANGLE_PLATFORM_FUCHSIA)
364 bool success = true;
365 errno = 0;
366 setpriority(PRIO_PROCESS, getpid(), -20);
367 if (errno)
368 {
369 // A friendly warning in case the test was run without appropriate permission.
370 perror(
371 "Warning: setpriority failed in StabilizeCPUForBenchmarking. Process will retain "
372 "default priority");
373 success = false;
374 }
375 # if ANGLE_PLATFORM_LINUX
376 cpu_set_t affinity;
377 CPU_SET(0, &affinity);
378 errno = 0;
379 if (sched_setaffinity(getpid(), sizeof(affinity), &affinity))
380 {
381 perror(
382 "Warning: sched_setaffinity failed in StabilizeCPUForBenchmarking. Process will retain "
383 "default affinity");
384 success = false;
385 }
386 # else
387 // TODO(jmadill): Implement for non-linux. http://anglebug.com/40096532
388 # endif
389
390 return success;
391 #else // defined(ANGLE_PLATFORM_FUCHSIA)
392 return false;
393 #endif
394 }
395
DeleteSystemFile(const char * path)396 bool DeleteSystemFile(const char *path)
397 {
398 return unlink(path) == 0;
399 }
400
LaunchProcess(const std::vector<const char * > & args,ProcessOutputCapture captureOutput)401 Process *LaunchProcess(const std::vector<const char *> &args, ProcessOutputCapture captureOutput)
402 {
403 return new PosixProcess(args, captureOutput);
404 }
405
NumberOfProcessors()406 int NumberOfProcessors()
407 {
408 // sysconf returns the number of "logical" (not "physical") processors on both
409 // Mac and Linux. So we get the number of max available "logical" processors.
410 //
411 // Note that the number of "currently online" processors may be fewer than the
412 // returned value of NumberOfProcessors(). On some platforms, the kernel may
413 // make some processors offline intermittently, to save power when system
414 // loading is low.
415 //
416 // One common use case that needs to know the processor count is to create
417 // optimal number of threads for optimization. It should make plan according
418 // to the number of "max available" processors instead of "currently online"
419 // ones. The kernel should be smart enough to make all processors online when
420 // it has sufficient number of threads waiting to run.
421 long res = sysconf(_SC_NPROCESSORS_CONF);
422 if (res == -1)
423 {
424 return 1;
425 }
426
427 return static_cast<int>(res);
428 }
429
GetNativeEGLLibraryNameWithExtension()430 const char *GetNativeEGLLibraryNameWithExtension()
431 {
432 #if defined(ANGLE_PLATFORM_ANDROID)
433 return "libEGL.so";
434 #elif defined(ANGLE_PLATFORM_LINUX)
435 return "libEGL.so.1";
436 #else
437 return "unknown_libegl";
438 #endif
439 }
440
441 #if defined(ANGLE_PLATFORM_MACOS)
InitMetalFileAPIHooking(int argc,char ** argv)442 void InitMetalFileAPIHooking(int argc, char **argv)
443 {
444 if (argc < 1)
445 {
446 return;
447 }
448
449 for (int i = 0; i < argc; ++i)
450 {
451 if (strncmp(argv[i], kSkipFileHookingArg, strlen(kSkipFileHookingArg)) == 0)
452 {
453 return;
454 }
455 }
456
457 constexpr char kInjectLibVarName[] = "DYLD_INSERT_LIBRARIES";
458 constexpr size_t kInjectLibVarNameLen = sizeof(kInjectLibVarName) - 1;
459
460 std::string exeDir = GetExecutableDirectory();
461 if (!exeDir.empty() && exeDir.back() != '/')
462 {
463 exeDir += "/";
464 }
465
466 // Intercept Metal shader cache access and return as if the cache doesn't exist.
467 // This is to avoid slow shader cache mechanism that caused the test timeout in the past.
468 // In order to do that, we need to hook the file API functions by making sure
469 // libmetal_shader_cache_file_hooking.dylib library is loaded first before any other libraries.
470 std::string injectLibsVar =
471 std::string(kInjectLibVarName) + "=" + exeDir + "libmetal_shader_cache_file_hooking.dylib";
472
473 char skipHookOption[sizeof(kSkipFileHookingArg)];
474 memcpy(skipHookOption, kSkipFileHookingArg, sizeof(kSkipFileHookingArg));
475
476 // Construct environment variables
477 std::vector<char *> newEnv;
478 char **environ = *_NSGetEnviron();
479 for (int i = 0; environ[i]; ++i)
480 {
481 if (strncmp(environ[i], kInjectLibVarName, kInjectLibVarNameLen) == 0)
482 {
483 injectLibsVar += ':';
484 injectLibsVar += environ[i] + kInjectLibVarNameLen + 1;
485 }
486 else
487 {
488 newEnv.push_back(environ[i]);
489 }
490 }
491 newEnv.push_back(strdup(injectLibsVar.data()));
492 newEnv.push_back(nullptr);
493
494 // Construct arguments with kSkipFileHookingArg flag to skip the hooking after re-launching.
495 std::vector<char *> newArgs;
496 newArgs.push_back(argv[0]);
497 newArgs.push_back(skipHookOption);
498 for (int i = 1; i < argc; ++i)
499 {
500 newArgs.push_back(argv[i]);
501 }
502 newArgs.push_back(nullptr);
503
504 // Re-launch the app with file API hooked.
505 ASSERT(-1 != execve(argv[0], newArgs.data(), newEnv.data()));
506 }
507 #endif
508
509 } // namespace angle
510