[compiler-rt][Profile] Wait for child threads in set-file-object test
authorDavid Spickett <david.spickett@linaro.org>
Fri, 3 Sep 2021 18:48:06 +0000 (11:48 -0700)
committerZequan Wu <zequanwu@google.com>
Fri, 3 Sep 2021 18:48:50 +0000 (11:48 -0700)
We've been seeing this test return 31 instead of 32 for the "functions"
line in this test on our AArch64 bots.

One possible cause is some of the children not finishing in time
before the llvm-profdata commands are run, if the machine is heavily loaded.

Wait for all the children to finish before exiting the parent.

Reviewed By: zequanwu

Differential Revision: https://reviews.llvm.org/D109222

compiler-rt/test/profile/ContinuousSyncMode/set-file-object.c

index d32dd57..d710b39 100644 (file)
@@ -41,6 +41,9 @@
 #include <stdio.h>
 #include <string.h>
 
+#include <sys/types.h>
+#include <sys/wait.h>
+
 const int num_child_procs_to_spawn = 32;
 
 extern int __llvm_profile_is_continuous_mode_enabled(void);
@@ -70,6 +73,24 @@ int main(int argc, char **argv) {
         return 1;
       }
     }
+    for (I = 0; I < num_child_procs_to_spawn; ++I) {
+      int status;
+      pid_t waited_pid = waitpid(child_pids[I], &status, 0);
+      if (waited_pid != child_pids[I]) {
+        fprintf(stderr, "Failed to wait on child %d\n", I);
+        return 1;
+      }
+      if (!WIFEXITED(status)) {
+        fprintf(stderr, "Child %d did not terminate normally\n", I);
+        return 1;
+      }
+      int return_status = WEXITSTATUS(status);
+      if (return_status != 0) {
+        fprintf(stderr, "Child %d exited with non zero status %d\n", I,
+                return_status);
+        return 1;
+      }
+    }
   } else if (strcmp(argv[1], "set") == 0) {
     // Child processes.
     if (!__llvm_profile_is_continuous_mode_enabled()) {