Skip to content

Commit c3a41aa

Browse files
Revert "[llvm-exegesis] Refactor parent code to separate function (#86232)"
This reverts commit bd49375. Causes build failures on non-X86 platforms. https://lab.llvm.org/buildbot/#/changes/128363
1 parent d2f8ba7 commit c3a41aa

File tree

1 file changed

+53
-63
lines changed

1 file changed

+53
-63
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -278,28 +278,67 @@ class SubProcessFunctionExecutorImpl
278278
return FD;
279279
}
280280

281-
Error
282-
runParentProcess(pid_t ChildPID, int WriteFD, StringRef CounterName,
283-
SmallVectorImpl<int64_t> &CounterValues,
284-
ArrayRef<const char *> ValidationCounters,
285-
SmallVectorImpl<int64_t> &ValidationCounterValues) const {
281+
Error createSubProcessAndRunBenchmark(
282+
StringRef CounterName, SmallVectorImpl<int64_t> &CounterValues,
283+
ArrayRef<const char *> ValidationCounters,
284+
SmallVectorImpl<int64_t> &ValidationCounterValues) const {
285+
int PipeFiles[2];
286+
int PipeSuccessOrErr = socketpair(AF_UNIX, SOCK_DGRAM, 0, PipeFiles);
287+
if (PipeSuccessOrErr != 0) {
288+
return make_error<Failure>(
289+
"Failed to create a pipe for interprocess communication between "
290+
"llvm-exegesis and the benchmarking subprocess: " +
291+
Twine(strerror(errno)));
292+
}
293+
294+
SubprocessMemory SPMemory;
295+
Error MemoryInitError = SPMemory.initializeSubprocessMemory(getpid());
296+
if (MemoryInitError)
297+
return MemoryInitError;
298+
299+
Error AddMemDefError =
300+
SPMemory.addMemoryDefinition(Key.MemoryValues, getpid());
301+
if (AddMemDefError)
302+
return AddMemDefError;
303+
304+
pid_t ParentOrChildPID = fork();
305+
306+
if (ParentOrChildPID == -1) {
307+
return make_error<Failure>("Failed to create child process: " +
308+
Twine(strerror(errno)));
309+
}
310+
311+
if (ParentOrChildPID == 0) {
312+
// We are in the child process, close the write end of the pipe.
313+
close(PipeFiles[1]);
314+
// Unregister handlers, signal handling is now handled through ptrace in
315+
// the host process.
316+
sys::unregisterHandlers();
317+
prepareAndRunBenchmark(PipeFiles[0], Key);
318+
// The child process terminates in the above function, so we should never
319+
// get to this point.
320+
llvm_unreachable("Child process didn't exit when expected.");
321+
}
322+
286323
const ExegesisTarget &ET = State.getExegesisTarget();
287-
auto CounterOrError =
288-
ET.createCounter(CounterName, State, ValidationCounters, ChildPID);
324+
auto CounterOrError = ET.createCounter(
325+
CounterName, State, ValidationCounters, ParentOrChildPID);
289326

290327
if (!CounterOrError)
291328
return CounterOrError.takeError();
292329

293330
pfm::CounterGroup *Counter = CounterOrError.get().get();
294331

332+
close(PipeFiles[0]);
333+
295334
// Make sure to attach to the process (and wait for the sigstop to be
296335
// delivered and for the process to continue) before we write to the counter
297336
// file descriptor. Attaching to the process before writing to the socket
298337
// ensures that the subprocess at most has blocked on the read call. If we
299338
// attach afterwards, the subprocess might exit before we get to the attach
300339
// call due to effects like scheduler contention, introducing transient
301340
// failures.
302-
if (ptrace(PTRACE_ATTACH, ChildPID, NULL, NULL) != 0)
341+
if (ptrace(PTRACE_ATTACH, ParentOrChildPID, NULL, NULL) != 0)
303342
return make_error<Failure>("Failed to attach to the child process: " +
304343
Twine(strerror(errno)));
305344

@@ -309,14 +348,14 @@ class SubProcessFunctionExecutorImpl
309348
Twine(strerror(errno)));
310349
}
311350

312-
if (ptrace(PTRACE_CONT, ChildPID, NULL, NULL) != 0)
351+
if (ptrace(PTRACE_CONT, ParentOrChildPID, NULL, NULL) != 0)
313352
return make_error<Failure>(
314353
"Failed to continue execution of the child process: " +
315354
Twine(strerror(errno)));
316355

317356
int CounterFileDescriptor = Counter->getFileDescriptor();
318357
Error SendError =
319-
sendFileDescriptorThroughSocket(WriteFD, CounterFileDescriptor);
358+
sendFileDescriptorThroughSocket(PipeFiles[1], CounterFileDescriptor);
320359

321360
if (SendError)
322361
return SendError;
@@ -356,7 +395,8 @@ class SubProcessFunctionExecutorImpl
356395

357396
// An error was encountered running the snippet, process it
358397
siginfo_t ChildSignalInfo;
359-
if (ptrace(PTRACE_GETSIGINFO, ChildPID, NULL, &ChildSignalInfo) == -1) {
398+
if (ptrace(PTRACE_GETSIGINFO, ParentOrChildPID, NULL, &ChildSignalInfo) ==
399+
-1) {
360400
return make_error<Failure>("Getting signal info from the child failed: " +
361401
Twine(strerror(errno)));
362402
}
@@ -382,65 +422,15 @@ class SubProcessFunctionExecutorImpl
382422
return make_error<SnippetSignal>(ChildSignalInfo.si_signo);
383423
}
384424

385-
Error createSubProcessAndRunBenchmark(
386-
StringRef CounterName, SmallVectorImpl<int64_t> &CounterValues,
387-
ArrayRef<const char *> ValidationCounters,
388-
SmallVectorImpl<int64_t> &ValidationCounterValues) const {
389-
int PipeFiles[2];
390-
int PipeSuccessOrErr = socketpair(AF_UNIX, SOCK_DGRAM, 0, PipeFiles);
391-
if (PipeSuccessOrErr != 0) {
392-
return make_error<Failure>(
393-
"Failed to create a pipe for interprocess communication between "
394-
"llvm-exegesis and the benchmarking subprocess: " +
395-
Twine(strerror(errno)));
396-
}
397-
398-
SubprocessMemory SPMemory;
399-
Error MemoryInitError = SPMemory.initializeSubprocessMemory(getpid());
400-
if (MemoryInitError)
401-
return MemoryInitError;
402-
403-
Error AddMemDefError =
404-
SPMemory.addMemoryDefinition(Key.MemoryValues, getpid());
405-
if (AddMemDefError)
406-
return AddMemDefError;
407-
408-
pid_t ParentOrChildPID = fork();
409-
410-
if (ParentOrChildPID == -1) {
411-
return make_error<Failure>("Failed to create child process: " +
412-
Twine(strerror(errno)));
413-
}
414-
415-
if (ParentOrChildPID == 0) {
416-
// We are in the child process, close the write end of the pipe.
417-
close(PipeFiles[1]);
418-
// Unregister handlers, signal handling is now handled through ptrace in
419-
// the host process.
420-
sys::unregisterHandlers();
421-
runChildSubprocess(PipeFiles[0], Key);
422-
// The child process terminates in the above function, so we should never
423-
// get to this point.
424-
llvm_unreachable("Child process didn't exit when expected.");
425-
}
426-
427-
// Close the read end of the pipe as we only need to write to the subprocess
428-
// from the parent process.
429-
close(PipeFiles[0]);
430-
return runParentProcess(ParentOrChildPID, PipeFiles[1], CounterName,
431-
CounterValues, ValidationCounters,
432-
ValidationCounterValues);
433-
}
434-
435425
void disableCoreDumps() const {
436426
struct rlimit rlim;
437427

438428
rlim.rlim_cur = 0;
439429
setrlimit(RLIMIT_CORE, &rlim);
440430
}
441431

442-
[[noreturn]] void runChildSubprocess(int Pipe,
443-
const BenchmarkKey &Key) const {
432+
[[noreturn]] void prepareAndRunBenchmark(int Pipe,
433+
const BenchmarkKey &Key) const {
444434
// Disable core dumps in the child process as otherwise everytime we
445435
// encounter an execution failure like a segmentation fault, we will create
446436
// a core dump. We report the information directly rather than require the

0 commit comments

Comments
 (0)