traced: make CloneReadOnly() copy only used portion of the buffer

Before this change CloneReadOnly() would memcpy the whole buffer
when cloning. This has two side-effects:
1. Causes a read-only page-fault on the original buffer. This is
   typically not a huge deal because both Linux and Window will
   map all that memory to the zero-page until a RW fault happens.
2. The cloned buffer will be filled with zeros up to the declared
   size. This can cause memory spikes.

The memory spikes wouldn't be a huge deal normally, because we
expect the buffer to be eventually fully used.
However, recently we introduced edge use cases where we declare
extremely large (~500MB) buffers and expect to use them only in
the case of a bugreport (But not fully). In this case the memory
spike is undesirable.

Bug: 307542353
Bug: 296227911
Test: perfetto_unittests --gtest_filter=TraceBufferTest.*
(cherry picked from commit 88060ce0324ec8dec6a1f9a9084033b4b7049a29)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:133f99fc7a967313509db6ddbb9c86400f5c18a0)
Merged-In: Ie53ee2b854cf2da488d115623dbdcf959fbacade
Change-Id: Ie53ee2b854cf2da488d115623dbdcf959fbacade
3 files changed
tree: 718394b40b40b76718b22f2f66ed1ae1853926a7
  1. .github/
  2. bazel/
  3. build_overrides/
  4. buildtools/
  5. debian/
  6. docs/
  7. examples/
  8. gn/
  9. include/
  10. infra/
  11. protos/
  12. python/
  13. src/
  14. test/
  15. third_party/
  16. tools/
  17. ui/
  18. .clang-format
  19. .clang-tidy
  20. .git-blame-ignore-revs
  21. .gitattributes
  22. .gitignore
  23. .gn
  24. .style.yapf
  25. Android.bp
  26. Android.bp.extras
  27. BUILD
  28. BUILD.extras
  29. BUILD.gn
  30. CHANGELOG
  31. codereview.settings
  32. DIR_METADATA
  33. heapprofd.rc
  34. LICENSE
  35. meson.build
  36. METADATA
  37. MODULE_LICENSE_APACHE2
  38. OWNERS
  39. perfetto.rc
  40. PerfettoIntegrationTests.xml
  41. PRESUBMIT.py
  42. README.chromium
  43. README.md
  44. TEST_MAPPING
  45. traced_perf.rc
  46. WORKSPACE
README.md

Perfetto - System profiling, app tracing and trace analysis

Perfetto is a production-grade open-source stack for performance instrumentation and trace analysis. It offers services and libraries and for recording system-level and app-level traces, native + java heap profiling, a library for analyzing traces using SQL and a web-based UI to visualize and explore multi-GB traces.

See https://perfetto.dev/docs or the /docs/ directory for documentation.