Improve macos profiling with symbols

- Add size checks for resulting profile.trace.xml and symbols.pb files
- Delete unused merged_trace.zip file after succesufulll symbolisation

Change-Id: I2141ea94382b28dc2b95d4945b70f20f7f4fcc1e
Reviewed-on: https://chromium-review.googlesource.com/c/crossbench/+/7531943
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
diff --git a/crossbench/probes/profiling/context/linux.py b/crossbench/probes/profiling/context/linux.py
index 8dab3d2..fd774a1 100644
--- a/crossbench/probes/profiling/context/linux.py
+++ b/crossbench/probes/profiling/context/linux.py
@@ -238,7 +238,6 @@
 
 KB = 1024
 
-
 def linux_perf_probe_inject_v8_symbols(
     perf_data_file: pth.AnyPath,
     platform: Optional[plt.Platform] = None) -> Optional[pth.AnyPath]:
diff --git a/crossbench/probes/profiling/context/macos.py b/crossbench/probes/profiling/context/macos.py
index aeea604..899d9bd 100644
--- a/crossbench/probes/profiling/context/macos.py
+++ b/crossbench/probes/profiling/context/macos.py
@@ -28,6 +28,7 @@
     "//trace-toc/run/data/table["
     '@category="PointsOfInterest" and @schema="os-signpost"]|'
     '//trace-toc/run/data/table[@schema="cpu-profile"]')
+KB = 1024
 
 
 class MacOSProfilingContext(PosixProfilingContext):
@@ -93,6 +94,8 @@
       self.browser_platform.sh("xctrace", "export", "--input", self.result_path,
                                "--output", trace_xml_path, "--xpath",
                                _XPATH_EXPRESSION)
+      if self.browser_platform.file_size(trace_xml_path) < 100 * KB:
+        logging.error("Got empty %s file", trace_xml_path)
       return trace_xml_path
 
   def stop_process(self) -> None:
diff --git a/crossbench/probes/trace_processor/context/base.py b/crossbench/probes/trace_processor/context/base.py
index 4cb11c8..a5f4030 100644
--- a/crossbench/probes/trace_processor/context/base.py
+++ b/crossbench/probes/trace_processor/context/base.py
@@ -143,4 +143,4 @@
 
   @property
   def symbolized_trace_path(self) -> pth.LocalPath:
-    return self.local_result_path / "symbolized_trace_path.zip"
+    return self.local_result_path / "symbolized_trace.zip"
diff --git a/crossbench/probes/trace_processor/context/symbolizing.py b/crossbench/probes/trace_processor/context/symbolizing.py
index 7a6c8fd..7b4ab4e 100644
--- a/crossbench/probes/trace_processor/context/symbolizing.py
+++ b/crossbench/probes/trace_processor/context/symbolizing.py
@@ -19,6 +19,7 @@
 if TYPE_CHECKING:
   from crossbench.probes.results import LocalProbeResult
 
+KB = 1024
 
 class TraceProcessorSymbolizingProbeContext(TraceProcessorProbeContext):
 
@@ -44,6 +45,7 @@
     if not traceconv_bin:
       logging.error("Could not find traceconv binary")
       return result
+
     merged_file = result.get("zip")
     symbols_result = self.local_result_path / "symbols.pb"
     env = {
@@ -58,11 +60,22 @@
           traceconv_bin, "symbolize", merged_file, symbols_result, env=env)
     except SubprocessError as e:
       logging.error("Symbolization failed: %s", e)
-    if not self.host_platform.exists(symbols_result):
+
+    if not self.host_platform.exists(symbols_result) or (
+        self.host_platform.file_size(symbols_result) < 100 * KB):
       # Figure out why this regularly fails
-      logging.error("Could not generate symbols file: %s", symbols_result)
+      logging.error("Could not generate valid symbols file: %s", symbols_result)
       return result
+
     with zipfile.ZipFile(self.symbolized_trace_path, "w") as zip_file:
       for f in (*result.perfetto_list, symbols_result):
         zip_file.write(f, arcname=f.relative_to(self.run.out_dir))
+    self._cleanup()
     return self.local_result(perfetto=(self.symbolized_trace_path,))
+
+  def _cleanup(self) -> None:
+    # If we have a successfully symbolized trace file we don't need the merged
+    # one anymore.
+    if (self.host_platform.file_size(self.symbolized_trace_path)
+        > self.host_platform.file_size(self.merged_trace_path)):
+      self.host_platform.rm(self.merged_trace_path)