[libcamera-devel,v2,1/1] Use tracing with perfetto in ChromeOS
diff mbox series

Message ID 20220722120028.3155712-2-chenghaoyang@google.com
State New
Headers show
Series
  • Use tracing with perfetto in ChromeOS
Related show

Commit Message

Cheng-Hao Yang July 22, 2022, noon UTC
As ChromeOS is using perfetto (project named as CrOSetto). When ChromeOS
uses libcamera, it should use perfetto to collect traces instead of
lttng.

Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
---
 Documentation/guides/tracing.rst              | 114 ++++++++++++++----
 include/libcamera/internal/tracepoints.h.in   |  37 +++++-
 .../internal/tracepoints/meson.build          |  25 ++--
 .../internal/tracepoints/pipeline.perfetto    |  10 ++
 .../internal/tracepoints/request.perfetto     |  30 +++++
 meson.build                                   |   7 +-
 src/android/cros/camera3_hal.cpp              |   5 +
 src/android/cros/meson.build                  |   1 +
 src/libcamera/meson.build                     |  14 ++-
 src/libcamera/pipeline_perfetto.cpp           |  24 ++++
 src/libcamera/request_perfetto.cpp            |  73 +++++++++++
 src/libcamera/tracepoints.cpp                 |  11 ++
 12 files changed, 312 insertions(+), 39 deletions(-)
 create mode 100644 include/libcamera/internal/tracepoints/pipeline.perfetto
 create mode 100644 include/libcamera/internal/tracepoints/request.perfetto
 create mode 100644 src/libcamera/pipeline_perfetto.cpp
 create mode 100644 src/libcamera/request_perfetto.cpp

Comments

Paul Elder Dec. 14, 2022, 6:48 a.m. UTC | #1
Hi Harvey,

Sorry for the delay.

On Fri, Jul 22, 2022 at 12:00:28PM +0000, Harvey Yang via libcamera-devel wrote:
> As ChromeOS is using perfetto (project named as CrOSetto). When ChromeOS
> uses libcamera, it should use perfetto to collect traces instead of
> lttng.
> 
> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
> ---
>  Documentation/guides/tracing.rst              | 114 ++++++++++++++----
>  include/libcamera/internal/tracepoints.h.in   |  37 +++++-
>  .../internal/tracepoints/meson.build          |  25 ++--
>  .../internal/tracepoints/pipeline.perfetto    |  10 ++
>  .../internal/tracepoints/request.perfetto     |  30 +++++
>  meson.build                                   |   7 +-
>  src/android/cros/camera3_hal.cpp              |   5 +
>  src/android/cros/meson.build                  |   1 +
>  src/libcamera/meson.build                     |  14 ++-
>  src/libcamera/pipeline_perfetto.cpp           |  24 ++++
>  src/libcamera/request_perfetto.cpp            |  73 +++++++++++
>  src/libcamera/tracepoints.cpp                 |  11 ++
>  12 files changed, 312 insertions(+), 39 deletions(-)
>  create mode 100644 include/libcamera/internal/tracepoints/pipeline.perfetto
>  create mode 100644 include/libcamera/internal/tracepoints/request.perfetto
>  create mode 100644 src/libcamera/pipeline_perfetto.cpp
>  create mode 100644 src/libcamera/request_perfetto.cpp
> 
> diff --git a/Documentation/guides/tracing.rst b/Documentation/guides/tracing.rst
> index ae960d85..53d6232e 100644
> --- a/Documentation/guides/tracing.rst
> +++ b/Documentation/guides/tracing.rst
> @@ -16,33 +16,50 @@ at periodic points in time. This can be done with other tools such as
>  callgrind, perf, gprof, etc., without modification to the application,
>  and is out of scope for this guide.
>  
> +Library: Perfetto vs lttng-ust
> +---------

The underline needs to be extended. (also in the other instances below)

> +
> +To integrate with CrOS tracing infrastructure, which uses perfetto (or called
> +CrOSetto in CrOS), we implement the tracepoint macros (will be described below)
> +with two different libraries: CrOS with perfetto, and the rest with lttng-ust.
> +
>  Compiling
>  ---------
>  
> -To compile libcamera with tracing support, it must be enabled through the
> -meson ``tracing`` option. It depends on the lttng-ust library (available in the
> -``liblttng-ust-dev`` package for Debian-based distributions).
> -By default the tracing option in meson is set to ``auto``, so if
> -liblttng is detected, it will be enabled by default. Conversely, if the option
> -is set to disabled, then libcamera will be compiled without tracing support.
> +To compile libcamera with perfetto support in CrOS, it must be enabled through

I'd rather the wording specify "tracing" generically first, then
elaborate between perfetto and lttng.

> +the meson ``tracing`` option. In CrOS, it depends on the perfetto library,
> +which is by default available in CrOS; In the rest, it depends on the lttng-ust
> +library (available in the ``liblttng-ust-dev`` package for Debian-based
> +distributions).
> +By default the tracing option in meson is set to ``auto``, so if the library is
> +detected, it will be enabled by default. Conversely, if the option is set to
> +disabled, then libcamera will be compiled without tracing support.

I'm wondering, does it make sense to allow both to be compiled in at the
same time? I guess we wouldn't use the same binary on CrOS and on
non-CrOS, and we wouldn't use perfetto on non-CrOS. So I guess the only
use case would be if lttng would ever be used on CrOS. Is that ever a
use case?

>  
>  Defining tracepoints
>  --------------------
>  
>  libcamera already contains a set of tracepoints. To define additional
> -tracepoints, create a file
> -``include/libcamera/internal/tracepoints/{file}.tp``, where ``file`` is a
> -reasonable name related to the category of tracepoints that you wish to
> -define. For example, the tracepoints file for the Request object is called
> -``request.tp``. An entry for this file must be added in
> -``include/libcamera/internal/tracepoints/meson.build``.
> -
> -In this tracepoints file, define your tracepoints `as mandated by lttng
> +tracepoints, create two files ``{file}.tp`` and ``{file}.perfetto``, for lttng
> +and perfetto respectively, under ``include/libcamera/internal/tracepoints/``,
> +and the perfetto implementation file ``src/libcamera/{file}_perfetto.cpp``,
> +where ``file`` is a reasonable name related to the category of tracepoints that
> +you wish to define. For example, the tracepoints files for the Request object is
> +called ``request.tp`` and ``request.perfetto``. Entries for the files must be
> +added in ``include/libcamera/internal/tracepoints/meson.build``.
> +
> +In the perfetto tracepoints files, declare the tracepoint functions in
> +``{file}.perfetto``, and define them in ``{file}_perfetto.cpp``, `as mandated
> +by perfetto <https://perfetto.dev/docs/instrumentation/track-events>`_.
> +Currently the only enabled perfetto::Category is ``libcamera``. If you intend to
> +use another category, remember to define it in

I'm wondering if category here in perfetto means something different
than lttng's? lttng only has the top-level category then the trace name
(afaik), so the top-level category is "libcamera" and after that we end
up using an underscore-separated prefix.

Does perfetto allow another level of catetories? I don't seem to see
any, in which case I think we should just remove this section, as all
the tracepoints that this section refers to are internal to libcamera,
so they'd all use the "libcamera" category.

> +``include/libcamera/internal/tracepoints.h.in``, and enable it when collecting
> +traces.

Do you think it's feasible to have just one definition and we can use it
to generate the other? I'd say use lttng's (because it's more specific,
like how it specifies the types of the fields) and then use that to
generate perfetto's.

Or is it better to just make sure that each tracepoint is defined twice?
I'm not fond of the duplicate work though, so tbh I'd rather one
definition that generates the other, if that's feasible. (Is it time for
me to write yet another code generator?)

> +
> +In the lttng tracepoints file, define your tracepoints `as mandated by lttng
>  <https://lttng.org/man/3/lttng-ust>`_. The header boilerplate must *not* be
>  included (as it will conflict with the rest of our infrastructure), and
>  only the tracepoint definitions (with the ``TRACEPOINT_*`` macros) should be
>  included.
> -
>  All tracepoint providers shall be ``libcamera``. According to lttng, the
>  tracepoint provider should be per-project; this is the rationale for this
>  decision. To group tracepoint events, we recommend using
> @@ -68,9 +85,9 @@ Then to use the tracepoint:
>  
>  ``LIBCAMERA_TRACEPOINT({tracepoint_event}, args...)``
>  
> -This macro must be used, as opposed to lttng's macros directly, because
> -lttng is an optional dependency of libcamera, so the code must compile and run
> -even when lttng is not present or when tracing is disabled.
> +This macro must be used, as opposed to perfetto's/lttng's macros directly,

s$perfetto's/lttng's$perfetto's or lttng's$

> +because tracing support is optional of libcamera, so the code must compile and
> +run even when perfetto/lttng is not present or when tracing is disabled.

s$perfetto/lttng is$perfetto or lttng are/

>  
>  The tracepoint provider name, as declared in the tracepoint definition, is not
>  included in the parameters of the tracepoint.
> @@ -86,7 +103,7 @@ and when the pipeline handler receives the corresponding response from the IPA,
>  respectively. These are the tracepoints that our sample analysis script
>  (see "Analyzing a trace") scans for when computing statistics on IPA call time.
>  
> -Using tracepoints (from an application)
> +Using lttng tracepoints (from an application)

I think this section applies regardless of if we're using lttng or
perfetto.

>  ---------------------------------------
>  
>  As applications are not part of libcamera, but rather users of libcamera,
> @@ -94,13 +111,66 @@ applications should seek their own tracing mechanisms. For ease of tracing
>  the application alongside tracing libcamera, it is recommended to also
>  `use lttng <https://lttng.org/docs/#doc-tracing-your-own-user-application>`_.
>  
> -Using tracepoints (from closed-source IPA)
> +Using lttng tracepoints (from closed-source IPA)

Same here.

>  ------------------------------------------
>  
>  Similar to applications, closed-source IPAs can simply use lttng on their own,
>  or any other tracing mechanism if desired.
>  
> -Collecting a trace
> +Collecting a perfetto trace
> +------------------
> +
> +A trace can be collected with the following steps:
> +
> +1. Start `traced` if it hasn't been started.
> +.. code-block:: bash
> +
> +   start traced
> +
> +2. Start a consumer that includes “track_event” data source in the trace
> +   config, and “libcamera” category.
> +.. code-block:: bash
> +
> +   perfetto -c - --txt -o /tmp/perfetto-trace \
> +   <<EOF
> +
> +   buffers: {
> +       size_kb: 63488
> +       fill_policy: DISCARD
> +   }
> +   buffers: {
> +       size_kb: 2048
> +       fill_policy: DISCARD
> +   }
> +   data_sources: {
> +        config {
> +            name: "track_event"
> +            track_event_config {
> +                enabled_categories: "libcamera"
> +            }
> +        }
> +   }
> +   duration_ms: 10000
> +
> +   EOF

Does this have to be fed through stdin or is there an option for
feeding it as a file?

> +
> +3. Execute the libcamera behavior you intend to trace during the set duration
> +   (10000 ms) in the above example.
> +
> +4. After the consumer (cmd `perfetto`) is done, you can find the trace result
> +   in ``/tmp/perfetto-trace``.
> +
> +Analyzing a perfetto trace
> +-----------------
> +
> +Follow the `guide <https://perfetto.dev/docs/visualization/perfetto-ui>`_ to
> +visualize the trace.
> +
> +In other words, upload the trace to `Perfetto UI <https://ui.perfetto.dev/>`_,
> +where you can check the timeline of tracepoints on each process/thread. You can
> +also run SQL queries to do analysis.
> +
> +Collecting a lttng trace

s/a/an/ (yeah, english sucks)

>  ------------------
>  
>  A trace can be collected fairly simply from lttng:
> @@ -123,7 +193,7 @@ viewed by: ``lttng view -t $PATH_TO_TRACE``, where ``$PATH_TO_TRACE`` is the
>  path that was printed when the session was created. This is the same path that
>  is used when analyzing traces programatically, as described in the next section.
>  
> -Analyzing a trace
> +Analyzing a lttng trace

Same here.

>  -----------------
>  
>  As mentioned above, while an lttng tracing session exists and the trace is not
> diff --git a/include/libcamera/internal/tracepoints.h.in b/include/libcamera/internal/tracepoints.h.in
> index d0fc1365..d91fadd7 100644
> --- a/include/libcamera/internal/tracepoints.h.in
> +++ b/include/libcamera/internal/tracepoints.h.in
> @@ -9,7 +9,24 @@
>  #ifndef __LIBCAMERA_INTERNAL_TRACEPOINTS_H__
>  #define __LIBCAMERA_INTERNAL_TRACEPOINTS_H__
>  
> -#if HAVE_TRACING
> +#if HAVE_PERFETTO
> +
> +#include <perfetto/perfetto.h>
> +
> +PERFETTO_DEFINE_CATEGORIES(
> +	perfetto::Category("libcamera")
> +		.SetDescription("Events from libcamera"));
> +
> +#define LIBCAMERA_TRACEPOINT(t_name, ...) \
> +LIBCAMERA_TRACE_EVENT_##t_name(__VA_ARGS__)
> +
> +#define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func) \
> +LIBCAMERA_TRACE_EVENT_ipa_call_begin(#pipe, #func)
> +
> +#define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func) \
> +LIBCAMERA_TRACE_EVENT_ipa_call_end(#pipe, #func)
> +
> +#elif HAVE_TRACING /* !HAVE_PERFETTO */
>  #define LIBCAMERA_TRACEPOINT(...) tracepoint(libcamera, __VA_ARGS__)
>  
>  #define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func) \
> @@ -18,7 +35,7 @@ tracepoint(libcamera, ipa_call_begin, #pipe, #func)
>  #define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func) \
>  tracepoint(libcamera, ipa_call_end, #pipe, #func)
>  
> -#else
> +#else /* HAVE_PERFETTO */

Should this be /* !HAVE_PERFETTO && !HAVE_TRACING */ ?

Also, probably we should rename TRACING to LTTNG everywhere. perfetto is
tracing too, so it sounds kind of weird to say "!HAVE_TRACING but
HAVE_PERFETTO", at least imo.

>  
>  namespace {
>  
> @@ -34,12 +51,15 @@ inline void unused([[maybe_unused]] Args&& ...args)
>  #define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func)
>  #define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func)
>  
> -#endif /* HAVE_TRACING */
> +#endif /* HAVE_PERFETTO */
>  
>  #endif /* __LIBCAMERA_INTERNAL_TRACEPOINTS_H__ */
>  
> +#if HAVE_PERFETTO
> +
> +#include <perfetto/perfetto.h>
>  
> -#if HAVE_TRACING
> +#elif HAVE_TRACING
>  
>  #undef TRACEPOINT_PROVIDER
>  #define TRACEPOINT_PROVIDER libcamera
> @@ -52,10 +72,15 @@ inline void unused([[maybe_unused]] Args&& ...args)
>  
>  #include <lttng/tracepoint.h>
>  
> -{{source}}

This won't work, as we need the header guard on the next line.

>  
>  #endif /* INCLUDE_LIBCAMERA_INTERNAL_TRACEPOINTS_TP_H */
>  
>  #include <lttng/tracepoint-event.h>
>  
> -#endif /* HAVE_TRACING */
> +#endif /* HAVE_PERFETTO */
> +
> +#if HAVE_PERFETTO || HAVE_TRACING
> +
> +{{source}}
> +
> +#endif /* HAVE_PERFETTO || HAVE_TRACING */

So this needs to be shuffled around.

> diff --git a/include/libcamera/internal/tracepoints/meson.build b/include/libcamera/internal/tracepoints/meson.build
> index d9b2fca5..ff5aece6 100644
> --- a/include/libcamera/internal/tracepoints/meson.build
> +++ b/include/libcamera/internal/tracepoints/meson.build
> @@ -1,12 +1,19 @@
>  # SPDX-License-Identifier: CC0-1.0
>  
> -# enum files must go first
> -tracepoint_files = files([
> -    'buffer_enums.tp',
> -    'request_enums.tp',
> -])
> +if get_option('android').enabled() and get_option('android_platform') == 'cros'
> +  tracepoint_files = files([
> +      'pipeline.perfetto',
> +      'request.perfetto',
> +  ])
> +else
> +  # enum files must go first
> +  tracepoint_files = files([
> +      'buffer_enums.tp',
> +      'request_enums.tp',
> +  ])
>  
> -tracepoint_files += files([
> -    'pipeline.tp',
> -    'request.tp',
> -])
> +  tracepoint_files += files([
> +      'pipeline.tp',
> +      'request.tp',
> +  ])
> +endif
> diff --git a/include/libcamera/internal/tracepoints/pipeline.perfetto b/include/libcamera/internal/tracepoints/pipeline.perfetto
> new file mode 100644
> index 00000000..5f45295e
> --- /dev/null
> +++ b/include/libcamera/internal/tracepoints/pipeline.perfetto
> @@ -0,0 +1,10 @@
> +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> +/*
> + * Copyright (C) 2022, Google Inc.
> + *
> + * pipeline.tp - Tracepoints for pipelines
> + */
> +
> +void LIBCAMERA_TRACE_EVENT_ipa_call_begin(const char *pipe, const char *func);
> +
> +void LIBCAMERA_TRACE_EVENT_ipa_call_end(const char *pipe, const char *func);
> diff --git a/include/libcamera/internal/tracepoints/request.perfetto b/include/libcamera/internal/tracepoints/request.perfetto
> new file mode 100644
> index 00000000..fd6a42a4
> --- /dev/null
> +++ b/include/libcamera/internal/tracepoints/request.perfetto
> @@ -0,0 +1,30 @@
> +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> +/*
> + * Copyright (C) 2022, Google Inc.
> + *
> + * request.tp - Tracepoints for the request object
> + */
> +
> +#include <libcamera/internal/request.h>
> +
> +#include <libcamera/framebuffer.h>
> +
> +void LIBCAMERA_TRACE_EVENT_request(libcamera::Request *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_construct(libcamera::Request *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_destroy(libcamera::Request *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_reuse(libcamera::Request *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_queue(libcamera::Request *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_device_queue(libcamera::Request *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_complete(libcamera::Request::Private *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_cancel(libcamera::Request::Private *req);
> +
> +void LIBCAMERA_TRACE_EVENT_request_complete_buffer(
> +		libcamera::Request::Private *req,
> +		libcamera::FrameBuffer * buf);
> diff --git a/meson.build b/meson.build
> index 29d8542d..434540e1 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -125,7 +125,11 @@ libcamera_includes = include_directories('include')
>  py_modules = []
>  
>  # Libraries used by multiple components
> -liblttng = cc.find_library('lttng-ust', required : get_option('tracing'))
> +libperfetto = dependency('perfetto', required : get_option('tracing').enabled()
> +    and get_option('android').enabled()
> +    and get_option('android_platform') == 'cros')
> +liblttng = cc.find_library('lttng-ust', required : get_option('tracing').enabled()
> +    and not libperfetto.found())
>  
>  # Pipeline handlers
>  #
> @@ -176,6 +180,7 @@ py_mod.find_installation('python3', modules: py_modules)
>  summary({
>              'Enabled pipelines': pipelines,
>              'Enabled IPA modules': ipa_modules,
> +            'Perfetto support': perfetto_enabled,
>              'Tracing support': tracing_enabled,
>              'Android support': android_enabled,
>              'GStreamer support': gst_enabled,
> diff --git a/src/android/cros/camera3_hal.cpp b/src/android/cros/camera3_hal.cpp
> index fb863b5f..2fbd7f14 100644
> --- a/src/android/cros/camera3_hal.cpp
> +++ b/src/android/cros/camera3_hal.cpp
> @@ -7,10 +7,15 @@
>  
>  #include <cros-camera/cros_camera_hal.h>
>  
> +#include "libcamera/internal/tracepoints.h"
>  #include "../camera_hal_manager.h"
>  
>  static void set_up([[maybe_unused]] cros::CameraMojoChannelManagerToken *token)
>  {
> +	perfetto::TracingInitArgs args;
> +	args.backends |= perfetto::kSystemBackend;
> +	perfetto::Tracing::Initialize(args);
> +	perfetto::TrackEvent::Register();
>  }
>  
>  static void tear_down()
> diff --git a/src/android/cros/meson.build b/src/android/cros/meson.build
> index 35995dd8..68f2bd9e 100644
> --- a/src/android/cros/meson.build
> +++ b/src/android/cros/meson.build
> @@ -9,5 +9,6 @@ android_hal_sources += files([
>  ])
>  
>  android_deps += dependency('libcros_camera')
> +android_deps += dependency('perfetto')
>  
>  android_cpp_args += ['-DOS_CHROMEOS']
> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
> index 26912ca1..39a55a17 100644
> --- a/src/libcamera/meson.build
> +++ b/src/libcamera/meson.build
> @@ -71,11 +71,22 @@ if libgnutls.found()
>      config_h.set('HAVE_GNUTLS', 1)
>  endif
>  
> -if liblttng.found()
> +if libperfetto.found()
> +    perfetto_enabled = true
> +    tracing_enabled = false
> +    config_h.set('HAVE_PERFETTO', 1)
> +    libcamera_sources += [
> +        'pipeline_perfetto.cpp',
> +        'request_perfetto.cpp',
> +        'tracepoints.cpp',
> +    ]
> +elif liblttng.found()
> +    perfetto_enabled = false
>      tracing_enabled = true
>      config_h.set('HAVE_TRACING', 1)
>      libcamera_sources += files(['tracepoints.cpp'])
>  else
> +    perfetto_enabled = false
>      tracing_enabled = false
>  endif
>  
> @@ -125,6 +136,7 @@ libcamera_deps = [
>      libdl,
>      libgnutls,
>      liblttng,
> +    libperfetto,
>      libudev,
>  ]
>  
> diff --git a/src/libcamera/pipeline_perfetto.cpp b/src/libcamera/pipeline_perfetto.cpp
> new file mode 100644
> index 00000000..07b82ffd
> --- /dev/null
> +++ b/src/libcamera/pipeline_perfetto.cpp

Is it not possible for the definitions to go in the .perfetto file? Or
is there some other reason that makes it strongly discouraged?

If it's required to be in a separate .cpp file, I'd at least want it
under src/libcamera/perfetto/ or src/libcamera/tracepoints/ or something
along those lines, and not in the top level src/libcamera/.

That or a code generator, so that we don't have to duplicate efforts of
tracepoint definitions in the first place, or risk tracepoints not being
defined for one but not the other.


Thanks,

Paul

> @@ -0,0 +1,24 @@
> +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> +/*
> + * Copyright (C) 2022, Google Inc.
> + *
> + * pipeline.tp - Tracepoints for pipelines
> + */
> +
> +#include "libcamera/internal/tracepoints.h"
> +
> +void LIBCAMERA_TRACE_EVENT_ipa_call_begin(const char *pipe, const char *func)
> +{
> +	// TODO: Consider TRACE_EVENT_BEGIN
> +	TRACE_EVENT("libcamera", "ipa_call_begin",
> +		    "pipeline_name", pipe,
> +		    "function_name", func);
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_ipa_call_end(const char *pipe, const char *func)
> +{
> +	// TODO: Consider TRACE_EVENT_END
> +	TRACE_EVENT("libcamera", "ipa_call_end",
> +		    "pipeline_name", pipe,
> +		    "function_name", func);
> +}
> diff --git a/src/libcamera/request_perfetto.cpp b/src/libcamera/request_perfetto.cpp
> new file mode 100644
> index 00000000..2cfff28e
> --- /dev/null
> +++ b/src/libcamera/request_perfetto.cpp
> @@ -0,0 +1,73 @@
> +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> +/*
> + * Copyright (C) 2022, Google Inc.
> + *
> + * request.tp - Tracepoints for the request object
> + */
> +
> +#include <libcamera/framebuffer.h>
> +
> +#include "libcamera/internal/request.h"
> +#include "libcamera/internal/tracepoints.h"
> +
> +void LIBCAMERA_TRACE_EVENT_request(libcamera::Request *req)
> +{
> +	TRACE_EVENT("libcamera", "request",
> +		    "request_ptr", reinterpret_cast<uintptr_t>(req),
> +		    "cookie", req->cookie(),
> +		    "status", req->status()); // TODO
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_construct(libcamera::Request *req)
> +{
> +	TRACE_EVENT("libcamera", "request_construct",
> +		    "request_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_destroy(libcamera::Request *req)
> +{
> +	TRACE_EVENT("libcamera", "request_destroy",
> +		    "request_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_reuse(libcamera::Request *req)
> +{
> +	TRACE_EVENT("libcamera", "request_reuse",
> +		    "request_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_queue(libcamera::Request *req)
> +{
> +	TRACE_EVENT("libcamera", "request_queue",
> +		    "request_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_device_queue(libcamera::Request *req)
> +{
> +	TRACE_EVENT("libcamera", "request_device_queue",
> +		    "request_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_complete(libcamera::Request::Private *req)
> +{
> +	TRACE_EVENT("libcamera", "request_complete",
> +		    "request_private_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_cancel(libcamera::Request::Private *req)
> +{
> +	TRACE_EVENT("libcamera", "request_cancel",
> +		    "request_private_ptr", reinterpret_cast<uintptr_t>(req));
> +}
> +
> +void LIBCAMERA_TRACE_EVENT_request_complete_buffer(
> +	libcamera::Request::Private *req,
> +	libcamera::FrameBuffer *buf)
> +{
> +	TRACE_EVENT("libcamera", "request_complete_buffer",
> +		    "request_private_ptr", reinterpret_cast<uintptr_t>(req),
> +		    "cookie", req->_o<libcamera::Request>()->cookie(),
> +		    "status", req->_o<libcamera::Request>()->status(), // TODO
> +		    "buffer_ptr", reinterpret_cast<uintptr_t>(buf),
> +		    "buffer_status", buf->metadata().status); // TODO
> +}
> diff --git a/src/libcamera/tracepoints.cpp b/src/libcamera/tracepoints.cpp
> index 0173b75a..a07ea531 100644
> --- a/src/libcamera/tracepoints.cpp
> +++ b/src/libcamera/tracepoints.cpp
> @@ -4,7 +4,18 @@
>   *
>   * tracepoints.cpp - Tracepoints with lttng
>   */
> +
> +#if HAVE_PERFETTO
> +
> +#include "libcamera/internal/tracepoints.h"
> +
> +PERFETTO_TRACK_EVENT_STATIC_STORAGE();
> +
> +#else
> +
>  #define TRACEPOINT_CREATE_PROBES
>  #define TRACEPOINT_DEFINE
>  
>  #include "libcamera/internal/tracepoints.h"
> +
> +#endif /* HAVE_PERFETTO */
> -- 
> 2.37.1.359.gd136c6c3e2-goog
>
Cheng-Hao Yang Dec. 20, 2022, 7:06 a.m. UTC | #2
Thanks Paul! And sorry for the delay.

Please check the v3 patch, which should contain most of the fixes according
to your
comments.

On Wed, Dec 14, 2022 at 2:49 PM Paul Elder <paul.elder@ideasonboard.com>
wrote:

> Hi Harvey,
>
> Sorry for the delay.
>
> On Fri, Jul 22, 2022 at 12:00:28PM +0000, Harvey Yang via libcamera-devel
> wrote:
> > As ChromeOS is using perfetto (project named as CrOSetto). When ChromeOS
> > uses libcamera, it should use perfetto to collect traces instead of
> > lttng.
> >
> > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
> > ---
> >  Documentation/guides/tracing.rst              | 114 ++++++++++++++----
> >  include/libcamera/internal/tracepoints.h.in   |  37 +++++-
> >  .../internal/tracepoints/meson.build          |  25 ++--
> >  .../internal/tracepoints/pipeline.perfetto    |  10 ++
> >  .../internal/tracepoints/request.perfetto     |  30 +++++
> >  meson.build                                   |   7 +-
> >  src/android/cros/camera3_hal.cpp              |   5 +
> >  src/android/cros/meson.build                  |   1 +
> >  src/libcamera/meson.build                     |  14 ++-
> >  src/libcamera/pipeline_perfetto.cpp           |  24 ++++
> >  src/libcamera/request_perfetto.cpp            |  73 +++++++++++
> >  src/libcamera/tracepoints.cpp                 |  11 ++
> >  12 files changed, 312 insertions(+), 39 deletions(-)
> >  create mode 100644
> include/libcamera/internal/tracepoints/pipeline.perfetto
> >  create mode 100644
> include/libcamera/internal/tracepoints/request.perfetto
> >  create mode 100644 src/libcamera/pipeline_perfetto.cpp
> >  create mode 100644 src/libcamera/request_perfetto.cpp
> >
> > diff --git a/Documentation/guides/tracing.rst
> b/Documentation/guides/tracing.rst
> > index ae960d85..53d6232e 100644
> > --- a/Documentation/guides/tracing.rst
> > +++ b/Documentation/guides/tracing.rst
> > @@ -16,33 +16,50 @@ at periodic points in time. This can be done with
> other tools such as
> >  callgrind, perf, gprof, etc., without modification to the application,
> >  and is out of scope for this guide.
> >
> > +Library: Perfetto vs lttng-ust
> > +---------
>
> The underline needs to be extended. (also in the other instances below)
>
>
Done. Please check if I missed anything.


> > +
> > +To integrate with CrOS tracing infrastructure, which uses perfetto (or
> called
> > +CrOSetto in CrOS), we implement the tracepoint macros (will be
> described below)
> > +with two different libraries: CrOS with perfetto, and the rest with
> lttng-ust.
> > +
> >  Compiling
> >  ---------
> >
> > -To compile libcamera with tracing support, it must be enabled through
> the
> > -meson ``tracing`` option. It depends on the lttng-ust library
> (available in the
> > -``liblttng-ust-dev`` package for Debian-based distributions).
> > -By default the tracing option in meson is set to ``auto``, so if
> > -liblttng is detected, it will be enabled by default. Conversely, if the
> option
> > -is set to disabled, then libcamera will be compiled without tracing
> support.
> > +To compile libcamera with perfetto support in CrOS, it must be enabled
> through
>
> I'd rather the wording specify "tracing" generically first, then
> elaborate between perfetto and lttng.
>
>
Right, updated. Please check.


> > +the meson ``tracing`` option. In CrOS, it depends on the perfetto
> library,
> > +which is by default available in CrOS; In the rest, it depends on the
> lttng-ust
> > +library (available in the ``liblttng-ust-dev`` package for Debian-based
> > +distributions).
> > +By default the tracing option in meson is set to ``auto``, so if the
> library is
> > +detected, it will be enabled by default. Conversely, if the option is
> set to
> > +disabled, then libcamera will be compiled without tracing support.
>
> I'm wondering, does it make sense to allow both to be compiled in at the
> same time? I guess we wouldn't use the same binary on CrOS and on
> non-CrOS, and we wouldn't use perfetto on non-CrOS. So I guess the only
> use case would be if lttng would ever be used on CrOS. Is that ever a
> use case?
>
>
I don't think so. CrOS Camera already supports perfetto, and it's the
performance
tracing tool being used across the OS and the VMs. So I believe we don't
have
the use case to compile them both.


> >
> >  Defining tracepoints
> >  --------------------
> >
> >  libcamera already contains a set of tracepoints. To define additional
> > -tracepoints, create a file
> > -``include/libcamera/internal/tracepoints/{file}.tp``, where ``file`` is
> a
> > -reasonable name related to the category of tracepoints that you wish to
> > -define. For example, the tracepoints file for the Request object is
> called
> > -``request.tp``. An entry for this file must be added in
> > -``include/libcamera/internal/tracepoints/meson.build``.
> > -
> > -In this tracepoints file, define your tracepoints `as mandated by lttng
> > +tracepoints, create two files ``{file}.tp`` and ``{file}.perfetto``,
> for lttng
> > +and perfetto respectively, under
> ``include/libcamera/internal/tracepoints/``,
> > +and the perfetto implementation file
> ``src/libcamera/{file}_perfetto.cpp``,
> > +where ``file`` is a reasonable name related to the category of
> tracepoints that
> > +you wish to define. For example, the tracepoints files for the Request
> object is
> > +called ``request.tp`` and ``request.perfetto``. Entries for the files
> must be
> > +added in ``include/libcamera/internal/tracepoints/meson.build``.
> > +
> > +In the perfetto tracepoints files, declare the tracepoint functions in
> > +``{file}.perfetto``, and define them in ``{file}_perfetto.cpp``, `as
> mandated
> > +by perfetto <https://perfetto.dev/docs/instrumentation/track-events>`_.
> > +Currently the only enabled perfetto::Category is ``libcamera``. If you
> intend to
> > +use another category, remember to define it in
>
> I'm wondering if category here in perfetto means something different
> than lttng's? lttng only has the top-level category then the trace name
> (afaik), so the top-level category is "libcamera" and after that we end
> up using an underscore-separated prefix.
>
> Does perfetto allow another level of catetories? I don't seem to see
> any, in which case I think we should just remove this section, as all
> the tracepoints that this section refers to are internal to libcamera,
> so they'd all use the "libcamera" category.
>
>
So perfetto would allow multiple categories. Currently yes, all tracepoints
belong to the same category "libcamera". But I just wanted to mention
here that it's possible for developers to add other categories to filter
tracepoints in the future.

Do you think that's worth mentioning here?


> > +``include/libcamera/internal/tracepoints.h.in``, and enable it when
> collecting
> > +traces.
>
> Do you think it's feasible to have just one definition and we can use it
> to generate the other? I'd say use lttng's (because it's more specific,
> like how it specifies the types of the fields) and then use that to
> generate perfetto's.
>
> Or is it better to just make sure that each tracepoint is defined twice?
> I'm not fond of the duplicate work though, so tbh I'd rather one
> definition that generates the other, if that's feasible. (Is it time for
> me to write yet another code generator?)
>
>
Sorry, I'm not sure what's in your picture, and I don't really understand
how lttng works: is it to have only one set of macro definitions
(LIBCAMERA_TRACEPOINT, LIBCAMERA_TRACEPOINT_IPA_BEGIN,
LIBCAMERA_TRACEPOINT_IPA_END)? Into the macro `tracepoint`?

Could you give a simple POC that covers one macro? Thanks!


> > +
> > +In the lttng tracepoints file, define your tracepoints `as mandated by
> lttng
> >  <https://lttng.org/man/3/lttng-ust>`_. The header boilerplate must
> *not* be
> >  included (as it will conflict with the rest of our infrastructure), and
> >  only the tracepoint definitions (with the ``TRACEPOINT_*`` macros)
> should be
> >  included.
> > -
> >  All tracepoint providers shall be ``libcamera``. According to lttng, the
> >  tracepoint provider should be per-project; this is the rationale for
> this
> >  decision. To group tracepoint events, we recommend using
> > @@ -68,9 +85,9 @@ Then to use the tracepoint:
> >
> >  ``LIBCAMERA_TRACEPOINT({tracepoint_event}, args...)``
> >
> > -This macro must be used, as opposed to lttng's macros directly, because
> > -lttng is an optional dependency of libcamera, so the code must compile
> and run
> > -even when lttng is not present or when tracing is disabled.
> > +This macro must be used, as opposed to perfetto's/lttng's macros
> directly,
>
> s$perfetto's/lttng's$perfetto's or lttng's$
>
>
Done.


> > +because tracing support is optional of libcamera, so the code must
> compile and
> > +run even when perfetto/lttng is not present or when tracing is disabled.
>
> s$perfetto/lttng is$perfetto or lttng are/
>
>
Done.


> >
> >  The tracepoint provider name, as declared in the tracepoint definition,
> is not
> >  included in the parameters of the tracepoint.
> > @@ -86,7 +103,7 @@ and when the pipeline handler receives the
> corresponding response from the IPA,
> >  respectively. These are the tracepoints that our sample analysis script
> >  (see "Analyzing a trace") scans for when computing statistics on IPA
> call time.
> >
> > -Using tracepoints (from an application)
> > +Using lttng tracepoints (from an application)
>
> I think this section applies regardless of if we're using lttng or
> perfetto.
>
>
Right, updated. I also added a link to perfetto's doc. Please check :)


> >  ---------------------------------------
> >
> >  As applications are not part of libcamera, but rather users of
> libcamera,
> > @@ -94,13 +111,66 @@ applications should seek their own tracing
> mechanisms. For ease of tracing
> >  the application alongside tracing libcamera, it is recommended to also
> >  `use lttng <
> https://lttng.org/docs/#doc-tracing-your-own-user-application>`_.
> >
> > -Using tracepoints (from closed-source IPA)
> > +Using lttng tracepoints (from closed-source IPA)
>
> Same here.
>
>
Done.


> >  ------------------------------------------
> >
> >  Similar to applications, closed-source IPAs can simply use lttng on
> their own,
> >  or any other tracing mechanism if desired.
> >
> > -Collecting a trace
> > +Collecting a perfetto trace
> > +------------------
> > +
> > +A trace can be collected with the following steps:
> > +
> > +1. Start `traced` if it hasn't been started.
> > +.. code-block:: bash
> > +
> > +   start traced
> > +
> > +2. Start a consumer that includes “track_event” data source in the trace
> > +   config, and “libcamera” category.
> > +.. code-block:: bash
> > +
> > +   perfetto -c - --txt -o /tmp/perfetto-trace \
> > +   <<EOF
> > +
> > +   buffers: {
> > +       size_kb: 63488
> > +       fill_policy: DISCARD
> > +   }
> > +   buffers: {
> > +       size_kb: 2048
> > +       fill_policy: DISCARD
> > +   }
> > +   data_sources: {
> > +        config {
> > +            name: "track_event"
> > +            track_event_config {
> > +                enabled_categories: "libcamera"
> > +            }
> > +        }
> > +   }
> > +   duration_ms: 10000
> > +
> > +   EOF
>
> Does this have to be fed through stdin or is there an option for
> feeding it as a file?
>
>
Both stdin or a file work, `perfetto --help` shows:
```--config         -c      : /path/to/trace/config/file or - for stdin```

Do you think I should put this instead of instructing developers to use
stdin?


> > +
> > +3. Execute the libcamera behavior you intend to trace during the set
> duration
> > +   (10000 ms) in the above example.
> > +
> > +4. After the consumer (cmd `perfetto`) is done, you can find the trace
> result
> > +   in ``/tmp/perfetto-trace``.
> > +
> > +Analyzing a perfetto trace
> > +-----------------
> > +
> > +Follow the `guide <https://perfetto.dev/docs/visualization/perfetto-ui>`_
> to
> > +visualize the trace.
> > +
> > +In other words, upload the trace to `Perfetto UI <
> https://ui.perfetto.dev/>`_,
> > +where you can check the timeline of tracepoints on each process/thread.
> You can
> > +also run SQL queries to do analysis.
> > +
> > +Collecting a lttng trace
>
> s/a/an/ (yeah, english sucks)
>
>
Ah right. Thanks!


> >  ------------------
> >
> >  A trace can be collected fairly simply from lttng:
> > @@ -123,7 +193,7 @@ viewed by: ``lttng view -t $PATH_TO_TRACE``, where
> ``$PATH_TO_TRACE`` is the
> >  path that was printed when the session was created. This is the same
> path that
> >  is used when analyzing traces programatically, as described in the next
> section.
> >
> > -Analyzing a trace
> > +Analyzing a lttng trace
>
> Same here.
>
>
Done.


> >  -----------------
> >
> >  As mentioned above, while an lttng tracing session exists and the trace
> is not
> > diff --git a/include/libcamera/internal/tracepoints.h.in
> b/include/libcamera/internal/tracepoints.h.in
> > index d0fc1365..d91fadd7 100644
> > --- a/include/libcamera/internal/tracepoints.h.in
> > +++ b/include/libcamera/internal/tracepoints.h.in
> > @@ -9,7 +9,24 @@
> >  #ifndef __LIBCAMERA_INTERNAL_TRACEPOINTS_H__
> >  #define __LIBCAMERA_INTERNAL_TRACEPOINTS_H__
> >
> > -#if HAVE_TRACING
> > +#if HAVE_PERFETTO
> > +
> > +#include <perfetto/perfetto.h>
> > +
> > +PERFETTO_DEFINE_CATEGORIES(
> > +     perfetto::Category("libcamera")
> > +             .SetDescription("Events from libcamera"));
> > +
> > +#define LIBCAMERA_TRACEPOINT(t_name, ...) \
> > +LIBCAMERA_TRACE_EVENT_##t_name(__VA_ARGS__)
> > +
> > +#define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func) \
> > +LIBCAMERA_TRACE_EVENT_ipa_call_begin(#pipe, #func)
> > +
> > +#define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func) \
> > +LIBCAMERA_TRACE_EVENT_ipa_call_end(#pipe, #func)
> > +
> > +#elif HAVE_TRACING /* !HAVE_PERFETTO */
> >  #define LIBCAMERA_TRACEPOINT(...) tracepoint(libcamera, __VA_ARGS__)
> >
> >  #define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func) \
> > @@ -18,7 +35,7 @@ tracepoint(libcamera, ipa_call_begin, #pipe, #func)
> >  #define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func) \
> >  tracepoint(libcamera, ipa_call_end, #pipe, #func)
> >
> > -#else
> > +#else /* HAVE_PERFETTO */
>
> Should this be /* !HAVE_PERFETTO && !HAVE_TRACING */ ?
>
>
Right, updated.


> Also, probably we should rename TRACING to LTTNG everywhere. perfetto is
> tracing too, so it sounds kind of weird to say "!HAVE_TRACING but
> HAVE_PERFETTO", at least imo.
>
>
Yes, I've also considered that. Updated.


> >
> >  namespace {
> >
> > @@ -34,12 +51,15 @@ inline void unused([[maybe_unused]] Args&& ...args)
> >  #define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func)
> >  #define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func)
> >
> > -#endif /* HAVE_TRACING */
> > +#endif /* HAVE_PERFETTO */
> >
> >  #endif /* __LIBCAMERA_INTERNAL_TRACEPOINTS_H__ */
> >
> > +#if HAVE_PERFETTO
> > +
> > +#include <perfetto/perfetto.h>
> >
> > -#if HAVE_TRACING
> > +#elif HAVE_TRACING
> >
> >  #undef TRACEPOINT_PROVIDER
> >  #define TRACEPOINT_PROVIDER libcamera
> > @@ -52,10 +72,15 @@ inline void unused([[maybe_unused]] Args&& ...args)
> >
> >  #include <lttng/tracepoint.h>
> >
> > -{{source}}
>
> This won't work, as we need the header guard on the next line.
>
>
Updated. Please check again.


> >
> >  #endif /* INCLUDE_LIBCAMERA_INTERNAL_TRACEPOINTS_TP_H */
> >
> >  #include <lttng/tracepoint-event.h>
> >
> > -#endif /* HAVE_TRACING */
> > +#endif /* HAVE_PERFETTO */
> > +
> > +#if HAVE_PERFETTO || HAVE_TRACING
> > +
> > +{{source}}
> > +
> > +#endif /* HAVE_PERFETTO || HAVE_TRACING */
>
> So this needs to be shuffled around.
>
>
Updated. Please check again.


> > diff --git a/include/libcamera/internal/tracepoints/meson.build
> b/include/libcamera/internal/tracepoints/meson.build
> > index d9b2fca5..ff5aece6 100644
> > --- a/include/libcamera/internal/tracepoints/meson.build
> > +++ b/include/libcamera/internal/tracepoints/meson.build
> > @@ -1,12 +1,19 @@
> >  # SPDX-License-Identifier: CC0-1.0
> >
> > -# enum files must go first
> > -tracepoint_files = files([
> > -    'buffer_enums.tp',
> > -    'request_enums.tp',
> > -])
> > +if get_option('android').enabled() and get_option('android_platform')
> == 'cros'
> > +  tracepoint_files = files([
> > +      'pipeline.perfetto',
> > +      'request.perfetto',
> > +  ])
> > +else
> > +  # enum files must go first
> > +  tracepoint_files = files([
> > +      'buffer_enums.tp',
> > +      'request_enums.tp',
> > +  ])
> >
> > -tracepoint_files += files([
> > -    'pipeline.tp',
> > -    'request.tp',
> > -])
> > +  tracepoint_files += files([
> > +      'pipeline.tp',
> > +      'request.tp',
> > +  ])
> > +endif
> > diff --git a/include/libcamera/internal/tracepoints/pipeline.perfetto
> b/include/libcamera/internal/tracepoints/pipeline.perfetto
> > new file mode 100644
> > index 00000000..5f45295e
> > --- /dev/null
> > +++ b/include/libcamera/internal/tracepoints/pipeline.perfetto
> > @@ -0,0 +1,10 @@
> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> > +/*
> > + * Copyright (C) 2022, Google Inc.
> > + *
> > + * pipeline.tp - Tracepoints for pipelines
> > + */
> > +
> > +void LIBCAMERA_TRACE_EVENT_ipa_call_begin(const char *pipe, const char
> *func);
> > +
> > +void LIBCAMERA_TRACE_EVENT_ipa_call_end(const char *pipe, const char
> *func);
> > diff --git a/include/libcamera/internal/tracepoints/request.perfetto
> b/include/libcamera/internal/tracepoints/request.perfetto
> > new file mode 100644
> > index 00000000..fd6a42a4
> > --- /dev/null
> > +++ b/include/libcamera/internal/tracepoints/request.perfetto
> > @@ -0,0 +1,30 @@
> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> > +/*
> > + * Copyright (C) 2022, Google Inc.
> > + *
> > + * request.tp - Tracepoints for the request object
> > + */
> > +
> > +#include <libcamera/internal/request.h>
> > +
> > +#include <libcamera/framebuffer.h>
> > +
> > +void LIBCAMERA_TRACE_EVENT_request(libcamera::Request *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_construct(libcamera::Request *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_destroy(libcamera::Request *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_reuse(libcamera::Request *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_queue(libcamera::Request *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_device_queue(libcamera::Request
> *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_complete(libcamera::Request::Private
> *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_cancel(libcamera::Request::Private
> *req);
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_complete_buffer(
> > +             libcamera::Request::Private *req,
> > +             libcamera::FrameBuffer * buf);
> > diff --git a/meson.build b/meson.build
> > index 29d8542d..434540e1 100644
> > --- a/meson.build
> > +++ b/meson.build
> > @@ -125,7 +125,11 @@ libcamera_includes = include_directories('include')
> >  py_modules = []
> >
> >  # Libraries used by multiple components
> > -liblttng = cc.find_library('lttng-ust', required :
> get_option('tracing'))
> > +libperfetto = dependency('perfetto', required :
> get_option('tracing').enabled()
> > +    and get_option('android').enabled()
> > +    and get_option('android_platform') == 'cros')
> > +liblttng = cc.find_library('lttng-ust', required :
> get_option('tracing').enabled()
> > +    and not libperfetto.found())
> >
> >  # Pipeline handlers
> >  #
> > @@ -176,6 +180,7 @@ py_mod.find_installation('python3', modules:
> py_modules)
> >  summary({
> >              'Enabled pipelines': pipelines,
> >              'Enabled IPA modules': ipa_modules,
> > +            'Perfetto support': perfetto_enabled,
> >              'Tracing support': tracing_enabled,
> >              'Android support': android_enabled,
> >              'GStreamer support': gst_enabled,
> > diff --git a/src/android/cros/camera3_hal.cpp
> b/src/android/cros/camera3_hal.cpp
> > index fb863b5f..2fbd7f14 100644
> > --- a/src/android/cros/camera3_hal.cpp
> > +++ b/src/android/cros/camera3_hal.cpp
> > @@ -7,10 +7,15 @@
> >
> >  #include <cros-camera/cros_camera_hal.h>
> >
> > +#include "libcamera/internal/tracepoints.h"
> >  #include "../camera_hal_manager.h"
> >
> >  static void set_up([[maybe_unused]] cros::CameraMojoChannelManagerToken
> *token)
> >  {
> > +     perfetto::TracingInitArgs args;
> > +     args.backends |= perfetto::kSystemBackend;
> > +     perfetto::Tracing::Initialize(args);
> > +     perfetto::TrackEvent::Register();
> >  }
> >
> >  static void tear_down()
> > diff --git a/src/android/cros/meson.build b/src/android/cros/meson.build
> > index 35995dd8..68f2bd9e 100644
> > --- a/src/android/cros/meson.build
> > +++ b/src/android/cros/meson.build
> > @@ -9,5 +9,6 @@ android_hal_sources += files([
> >  ])
> >
> >  android_deps += dependency('libcros_camera')
> > +android_deps += dependency('perfetto')
> >
> >  android_cpp_args += ['-DOS_CHROMEOS']
> > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
> > index 26912ca1..39a55a17 100644
> > --- a/src/libcamera/meson.build
> > +++ b/src/libcamera/meson.build
> > @@ -71,11 +71,22 @@ if libgnutls.found()
> >      config_h.set('HAVE_GNUTLS', 1)
> >  endif
> >
> > -if liblttng.found()
> > +if libperfetto.found()
> > +    perfetto_enabled = true
> > +    tracing_enabled = false
> > +    config_h.set('HAVE_PERFETTO', 1)
> > +    libcamera_sources += [
> > +        'pipeline_perfetto.cpp',
> > +        'request_perfetto.cpp',
> > +        'tracepoints.cpp',
> > +    ]
> > +elif liblttng.found()
> > +    perfetto_enabled = false
> >      tracing_enabled = true
> >      config_h.set('HAVE_TRACING', 1)
> >      libcamera_sources += files(['tracepoints.cpp'])
> >  else
> > +    perfetto_enabled = false
> >      tracing_enabled = false
> >  endif
> >
> > @@ -125,6 +136,7 @@ libcamera_deps = [
> >      libdl,
> >      libgnutls,
> >      liblttng,
> > +    libperfetto,
> >      libudev,
> >  ]
> >
> > diff --git a/src/libcamera/pipeline_perfetto.cpp
> b/src/libcamera/pipeline_perfetto.cpp
> > new file mode 100644
> > index 00000000..07b82ffd
> > --- /dev/null
> > +++ b/src/libcamera/pipeline_perfetto.cpp
>
> Is it not possible for the definitions to go in the .perfetto file? Or
> is there some other reason that makes it strongly discouraged?
>
>
So when I put them into the .perfetto file, which will be put in the
generated tracepoints.h with `{{source}}`, even with the header
guard INCLUDE_LIBCAMERA_INTERNAL_TRACEPOINTS_TP_H,
it still won't compile:
```
ld.lld: error: duplicate symbol:
LIBCAMERA_TRACE_EVENT_request(libcamera::Request*)
>>> defined at tracepoints.h:105
(include/libcamera/internal/tracepoints.h:105)
>>>
 src/libcamera/libcamera.so.0.0.2.p/pipeline_handler.cpp.o:(LIBCAMERA_TRACE_EVENT_request(libcamera::Request*))
>>> defined at tracepoints.h:105
(include/libcamera/internal/tracepoints.h:105)
>>>
 src/libcamera/libcamera.so.0.0.2.p/request.cpp.o:(.text._Z29LIBCAMERA_TRACE_EVENT_requestPN9libcamera7RequestE+0x0)
```

I wonder if it's because of the header guard with another condition:
`|| defined(TRACEPOINT_HEADER_MULTI_READ)`

WDYT?


> If it's required to be in a separate .cpp file, I'd at least want it
> under src/libcamera/perfetto/ or src/libcamera/tracepoints/ or something
> along those lines, and not in the top level src/libcamera/.
>
>
Put them under `src/libcamera/perfetto` now.


> That or a code generator, so that we don't have to duplicate efforts of
> tracepoint definitions in the first place, or risk tracepoints not being
> defined for one but not the other.
>
>
I'm not sure how to add a code generator that handles some complicated
macros (like LIBCAMERA_TRACE_EVENT_request_complete_buffer)
without great effort. Let me know if you have a great idea. Thanks!


>
> Thanks,
>
> Paul
>
> > @@ -0,0 +1,24 @@
> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> > +/*
> > + * Copyright (C) 2022, Google Inc.
> > + *
> > + * pipeline.tp - Tracepoints for pipelines
> > + */
> > +
> > +#include "libcamera/internal/tracepoints.h"
> > +
> > +void LIBCAMERA_TRACE_EVENT_ipa_call_begin(const char *pipe, const char
> *func)
> > +{
> > +     // TODO: Consider TRACE_EVENT_BEGIN
> > +     TRACE_EVENT("libcamera", "ipa_call_begin",
> > +                 "pipeline_name", pipe,
> > +                 "function_name", func);
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_ipa_call_end(const char *pipe, const char
> *func)
> > +{
> > +     // TODO: Consider TRACE_EVENT_END
> > +     TRACE_EVENT("libcamera", "ipa_call_end",
> > +                 "pipeline_name", pipe,
> > +                 "function_name", func);
> > +}
> > diff --git a/src/libcamera/request_perfetto.cpp
> b/src/libcamera/request_perfetto.cpp
> > new file mode 100644
> > index 00000000..2cfff28e
> > --- /dev/null
> > +++ b/src/libcamera/request_perfetto.cpp
> > @@ -0,0 +1,73 @@
> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */
> > +/*
> > + * Copyright (C) 2022, Google Inc.
> > + *
> > + * request.tp - Tracepoints for the request object
> > + */
> > +
> > +#include <libcamera/framebuffer.h>
> > +
> > +#include "libcamera/internal/request.h"
> > +#include "libcamera/internal/tracepoints.h"
> > +
> > +void LIBCAMERA_TRACE_EVENT_request(libcamera::Request *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request",
> > +                 "request_ptr", reinterpret_cast<uintptr_t>(req),
> > +                 "cookie", req->cookie(),
> > +                 "status", req->status()); // TODO
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_construct(libcamera::Request *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_construct",
> > +                 "request_ptr", reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_destroy(libcamera::Request *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_destroy",
> > +                 "request_ptr", reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_reuse(libcamera::Request *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_reuse",
> > +                 "request_ptr", reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_queue(libcamera::Request *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_queue",
> > +                 "request_ptr", reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_device_queue(libcamera::Request *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_device_queue",
> > +                 "request_ptr", reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_complete(libcamera::Request::Private
> *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_complete",
> > +                 "request_private_ptr",
> reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_cancel(libcamera::Request::Private
> *req)
> > +{
> > +     TRACE_EVENT("libcamera", "request_cancel",
> > +                 "request_private_ptr",
> reinterpret_cast<uintptr_t>(req));
> > +}
> > +
> > +void LIBCAMERA_TRACE_EVENT_request_complete_buffer(
> > +     libcamera::Request::Private *req,
> > +     libcamera::FrameBuffer *buf)
> > +{
> > +     TRACE_EVENT("libcamera", "request_complete_buffer",
> > +                 "request_private_ptr",
> reinterpret_cast<uintptr_t>(req),
> > +                 "cookie", req->_o<libcamera::Request>()->cookie(),
> > +                 "status", req->_o<libcamera::Request>()->status(), //
> TODO
> > +                 "buffer_ptr", reinterpret_cast<uintptr_t>(buf),
> > +                 "buffer_status", buf->metadata().status); // TODO
> > +}
> > diff --git a/src/libcamera/tracepoints.cpp
> b/src/libcamera/tracepoints.cpp
> > index 0173b75a..a07ea531 100644
> > --- a/src/libcamera/tracepoints.cpp
> > +++ b/src/libcamera/tracepoints.cpp
> > @@ -4,7 +4,18 @@
> >   *
> >   * tracepoints.cpp - Tracepoints with lttng
> >   */
> > +
> > +#if HAVE_PERFETTO
> > +
> > +#include "libcamera/internal/tracepoints.h"
> > +
> > +PERFETTO_TRACK_EVENT_STATIC_STORAGE();
> > +
> > +#else
> > +
> >  #define TRACEPOINT_CREATE_PROBES
> >  #define TRACEPOINT_DEFINE
> >
> >  #include "libcamera/internal/tracepoints.h"
> > +
> > +#endif /* HAVE_PERFETTO */
> > --
> > 2.37.1.359.gd136c6c3e2-goog
> >
>

Patch
diff mbox series

diff --git a/Documentation/guides/tracing.rst b/Documentation/guides/tracing.rst
index ae960d85..53d6232e 100644
--- a/Documentation/guides/tracing.rst
+++ b/Documentation/guides/tracing.rst
@@ -16,33 +16,50 @@  at periodic points in time. This can be done with other tools such as
 callgrind, perf, gprof, etc., without modification to the application,
 and is out of scope for this guide.
 
+Library: Perfetto vs lttng-ust
+---------
+
+To integrate with CrOS tracing infrastructure, which uses perfetto (or called
+CrOSetto in CrOS), we implement the tracepoint macros (will be described below)
+with two different libraries: CrOS with perfetto, and the rest with lttng-ust.
+
 Compiling
 ---------
 
-To compile libcamera with tracing support, it must be enabled through the
-meson ``tracing`` option. It depends on the lttng-ust library (available in the
-``liblttng-ust-dev`` package for Debian-based distributions).
-By default the tracing option in meson is set to ``auto``, so if
-liblttng is detected, it will be enabled by default. Conversely, if the option
-is set to disabled, then libcamera will be compiled without tracing support.
+To compile libcamera with perfetto support in CrOS, it must be enabled through
+the meson ``tracing`` option. In CrOS, it depends on the perfetto library,
+which is by default available in CrOS; In the rest, it depends on the lttng-ust
+library (available in the ``liblttng-ust-dev`` package for Debian-based
+distributions).
+By default the tracing option in meson is set to ``auto``, so if the library is
+detected, it will be enabled by default. Conversely, if the option is set to
+disabled, then libcamera will be compiled without tracing support.
 
 Defining tracepoints
 --------------------
 
 libcamera already contains a set of tracepoints. To define additional
-tracepoints, create a file
-``include/libcamera/internal/tracepoints/{file}.tp``, where ``file`` is a
-reasonable name related to the category of tracepoints that you wish to
-define. For example, the tracepoints file for the Request object is called
-``request.tp``. An entry for this file must be added in
-``include/libcamera/internal/tracepoints/meson.build``.
-
-In this tracepoints file, define your tracepoints `as mandated by lttng
+tracepoints, create two files ``{file}.tp`` and ``{file}.perfetto``, for lttng
+and perfetto respectively, under ``include/libcamera/internal/tracepoints/``,
+and the perfetto implementation file ``src/libcamera/{file}_perfetto.cpp``,
+where ``file`` is a reasonable name related to the category of tracepoints that
+you wish to define. For example, the tracepoints files for the Request object is
+called ``request.tp`` and ``request.perfetto``. Entries for the files must be
+added in ``include/libcamera/internal/tracepoints/meson.build``.
+
+In the perfetto tracepoints files, declare the tracepoint functions in
+``{file}.perfetto``, and define them in ``{file}_perfetto.cpp``, `as mandated
+by perfetto <https://perfetto.dev/docs/instrumentation/track-events>`_.
+Currently the only enabled perfetto::Category is ``libcamera``. If you intend to
+use another category, remember to define it in
+``include/libcamera/internal/tracepoints.h.in``, and enable it when collecting
+traces.
+
+In the lttng tracepoints file, define your tracepoints `as mandated by lttng
 <https://lttng.org/man/3/lttng-ust>`_. The header boilerplate must *not* be
 included (as it will conflict with the rest of our infrastructure), and
 only the tracepoint definitions (with the ``TRACEPOINT_*`` macros) should be
 included.
-
 All tracepoint providers shall be ``libcamera``. According to lttng, the
 tracepoint provider should be per-project; this is the rationale for this
 decision. To group tracepoint events, we recommend using
@@ -68,9 +85,9 @@  Then to use the tracepoint:
 
 ``LIBCAMERA_TRACEPOINT({tracepoint_event}, args...)``
 
-This macro must be used, as opposed to lttng's macros directly, because
-lttng is an optional dependency of libcamera, so the code must compile and run
-even when lttng is not present or when tracing is disabled.
+This macro must be used, as opposed to perfetto's/lttng's macros directly,
+because tracing support is optional of libcamera, so the code must compile and
+run even when perfetto/lttng is not present or when tracing is disabled.
 
 The tracepoint provider name, as declared in the tracepoint definition, is not
 included in the parameters of the tracepoint.
@@ -86,7 +103,7 @@  and when the pipeline handler receives the corresponding response from the IPA,
 respectively. These are the tracepoints that our sample analysis script
 (see "Analyzing a trace") scans for when computing statistics on IPA call time.
 
-Using tracepoints (from an application)
+Using lttng tracepoints (from an application)
 ---------------------------------------
 
 As applications are not part of libcamera, but rather users of libcamera,
@@ -94,13 +111,66 @@  applications should seek their own tracing mechanisms. For ease of tracing
 the application alongside tracing libcamera, it is recommended to also
 `use lttng <https://lttng.org/docs/#doc-tracing-your-own-user-application>`_.
 
-Using tracepoints (from closed-source IPA)
+Using lttng tracepoints (from closed-source IPA)
 ------------------------------------------
 
 Similar to applications, closed-source IPAs can simply use lttng on their own,
 or any other tracing mechanism if desired.
 
-Collecting a trace
+Collecting a perfetto trace
+------------------
+
+A trace can be collected with the following steps:
+
+1. Start `traced` if it hasn't been started.
+.. code-block:: bash
+
+   start traced
+
+2. Start a consumer that includes “track_event” data source in the trace
+   config, and “libcamera” category.
+.. code-block:: bash
+
+   perfetto -c - --txt -o /tmp/perfetto-trace \
+   <<EOF
+
+   buffers: {
+       size_kb: 63488
+       fill_policy: DISCARD
+   }
+   buffers: {
+       size_kb: 2048
+       fill_policy: DISCARD
+   }
+   data_sources: {
+        config {
+            name: "track_event"
+            track_event_config {
+                enabled_categories: "libcamera"
+            }
+        }
+   }
+   duration_ms: 10000
+
+   EOF
+
+3. Execute the libcamera behavior you intend to trace during the set duration
+   (10000 ms) in the above example.
+
+4. After the consumer (cmd `perfetto`) is done, you can find the trace result
+   in ``/tmp/perfetto-trace``.
+
+Analyzing a perfetto trace
+-----------------
+
+Follow the `guide <https://perfetto.dev/docs/visualization/perfetto-ui>`_ to
+visualize the trace.
+
+In other words, upload the trace to `Perfetto UI <https://ui.perfetto.dev/>`_,
+where you can check the timeline of tracepoints on each process/thread. You can
+also run SQL queries to do analysis.
+
+Collecting a lttng trace
 ------------------
 
 A trace can be collected fairly simply from lttng:
@@ -123,7 +193,7 @@  viewed by: ``lttng view -t $PATH_TO_TRACE``, where ``$PATH_TO_TRACE`` is the
 path that was printed when the session was created. This is the same path that
 is used when analyzing traces programatically, as described in the next section.
 
-Analyzing a trace
+Analyzing a lttng trace
 -----------------
 
 As mentioned above, while an lttng tracing session exists and the trace is not
diff --git a/include/libcamera/internal/tracepoints.h.in b/include/libcamera/internal/tracepoints.h.in
index d0fc1365..d91fadd7 100644
--- a/include/libcamera/internal/tracepoints.h.in
+++ b/include/libcamera/internal/tracepoints.h.in
@@ -9,7 +9,24 @@ 
 #ifndef __LIBCAMERA_INTERNAL_TRACEPOINTS_H__
 #define __LIBCAMERA_INTERNAL_TRACEPOINTS_H__
 
-#if HAVE_TRACING
+#if HAVE_PERFETTO
+
+#include <perfetto/perfetto.h>
+
+PERFETTO_DEFINE_CATEGORIES(
+	perfetto::Category("libcamera")
+		.SetDescription("Events from libcamera"));
+
+#define LIBCAMERA_TRACEPOINT(t_name, ...) \
+LIBCAMERA_TRACE_EVENT_##t_name(__VA_ARGS__)
+
+#define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func) \
+LIBCAMERA_TRACE_EVENT_ipa_call_begin(#pipe, #func)
+
+#define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func) \
+LIBCAMERA_TRACE_EVENT_ipa_call_end(#pipe, #func)
+
+#elif HAVE_TRACING /* !HAVE_PERFETTO */
 #define LIBCAMERA_TRACEPOINT(...) tracepoint(libcamera, __VA_ARGS__)
 
 #define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func) \
@@ -18,7 +35,7 @@  tracepoint(libcamera, ipa_call_begin, #pipe, #func)
 #define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func) \
 tracepoint(libcamera, ipa_call_end, #pipe, #func)
 
-#else
+#else /* HAVE_PERFETTO */
 
 namespace {
 
@@ -34,12 +51,15 @@  inline void unused([[maybe_unused]] Args&& ...args)
 #define LIBCAMERA_TRACEPOINT_IPA_BEGIN(pipe, func)
 #define LIBCAMERA_TRACEPOINT_IPA_END(pipe, func)
 
-#endif /* HAVE_TRACING */
+#endif /* HAVE_PERFETTO */
 
 #endif /* __LIBCAMERA_INTERNAL_TRACEPOINTS_H__ */
 
+#if HAVE_PERFETTO
+
+#include <perfetto/perfetto.h>
 
-#if HAVE_TRACING
+#elif HAVE_TRACING
 
 #undef TRACEPOINT_PROVIDER
 #define TRACEPOINT_PROVIDER libcamera
@@ -52,10 +72,15 @@  inline void unused([[maybe_unused]] Args&& ...args)
 
 #include <lttng/tracepoint.h>
 
-{{source}}
 
 #endif /* INCLUDE_LIBCAMERA_INTERNAL_TRACEPOINTS_TP_H */
 
 #include <lttng/tracepoint-event.h>
 
-#endif /* HAVE_TRACING */
+#endif /* HAVE_PERFETTO */
+
+#if HAVE_PERFETTO || HAVE_TRACING
+
+{{source}}
+
+#endif /* HAVE_PERFETTO || HAVE_TRACING */
diff --git a/include/libcamera/internal/tracepoints/meson.build b/include/libcamera/internal/tracepoints/meson.build
index d9b2fca5..ff5aece6 100644
--- a/include/libcamera/internal/tracepoints/meson.build
+++ b/include/libcamera/internal/tracepoints/meson.build
@@ -1,12 +1,19 @@ 
 # SPDX-License-Identifier: CC0-1.0
 
-# enum files must go first
-tracepoint_files = files([
-    'buffer_enums.tp',
-    'request_enums.tp',
-])
+if get_option('android').enabled() and get_option('android_platform') == 'cros'
+  tracepoint_files = files([
+      'pipeline.perfetto',
+      'request.perfetto',
+  ])
+else
+  # enum files must go first
+  tracepoint_files = files([
+      'buffer_enums.tp',
+      'request_enums.tp',
+  ])
 
-tracepoint_files += files([
-    'pipeline.tp',
-    'request.tp',
-])
+  tracepoint_files += files([
+      'pipeline.tp',
+      'request.tp',
+  ])
+endif
diff --git a/include/libcamera/internal/tracepoints/pipeline.perfetto b/include/libcamera/internal/tracepoints/pipeline.perfetto
new file mode 100644
index 00000000..5f45295e
--- /dev/null
+++ b/include/libcamera/internal/tracepoints/pipeline.perfetto
@@ -0,0 +1,10 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2022, Google Inc.
+ *
+ * pipeline.tp - Tracepoints for pipelines
+ */
+
+void LIBCAMERA_TRACE_EVENT_ipa_call_begin(const char *pipe, const char *func);
+
+void LIBCAMERA_TRACE_EVENT_ipa_call_end(const char *pipe, const char *func);
diff --git a/include/libcamera/internal/tracepoints/request.perfetto b/include/libcamera/internal/tracepoints/request.perfetto
new file mode 100644
index 00000000..fd6a42a4
--- /dev/null
+++ b/include/libcamera/internal/tracepoints/request.perfetto
@@ -0,0 +1,30 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2022, Google Inc.
+ *
+ * request.tp - Tracepoints for the request object
+ */
+
+#include <libcamera/internal/request.h>
+
+#include <libcamera/framebuffer.h>
+
+void LIBCAMERA_TRACE_EVENT_request(libcamera::Request *req);
+
+void LIBCAMERA_TRACE_EVENT_request_construct(libcamera::Request *req);
+
+void LIBCAMERA_TRACE_EVENT_request_destroy(libcamera::Request *req);
+
+void LIBCAMERA_TRACE_EVENT_request_reuse(libcamera::Request *req);
+
+void LIBCAMERA_TRACE_EVENT_request_queue(libcamera::Request *req);
+
+void LIBCAMERA_TRACE_EVENT_request_device_queue(libcamera::Request *req);
+
+void LIBCAMERA_TRACE_EVENT_request_complete(libcamera::Request::Private *req);
+
+void LIBCAMERA_TRACE_EVENT_request_cancel(libcamera::Request::Private *req);
+
+void LIBCAMERA_TRACE_EVENT_request_complete_buffer(
+		libcamera::Request::Private *req,
+		libcamera::FrameBuffer * buf);
diff --git a/meson.build b/meson.build
index 29d8542d..434540e1 100644
--- a/meson.build
+++ b/meson.build
@@ -125,7 +125,11 @@  libcamera_includes = include_directories('include')
 py_modules = []
 
 # Libraries used by multiple components
-liblttng = cc.find_library('lttng-ust', required : get_option('tracing'))
+libperfetto = dependency('perfetto', required : get_option('tracing').enabled()
+    and get_option('android').enabled()
+    and get_option('android_platform') == 'cros')
+liblttng = cc.find_library('lttng-ust', required : get_option('tracing').enabled()
+    and not libperfetto.found())
 
 # Pipeline handlers
 #
@@ -176,6 +180,7 @@  py_mod.find_installation('python3', modules: py_modules)
 summary({
             'Enabled pipelines': pipelines,
             'Enabled IPA modules': ipa_modules,
+            'Perfetto support': perfetto_enabled,
             'Tracing support': tracing_enabled,
             'Android support': android_enabled,
             'GStreamer support': gst_enabled,
diff --git a/src/android/cros/camera3_hal.cpp b/src/android/cros/camera3_hal.cpp
index fb863b5f..2fbd7f14 100644
--- a/src/android/cros/camera3_hal.cpp
+++ b/src/android/cros/camera3_hal.cpp
@@ -7,10 +7,15 @@ 
 
 #include <cros-camera/cros_camera_hal.h>
 
+#include "libcamera/internal/tracepoints.h"
 #include "../camera_hal_manager.h"
 
 static void set_up([[maybe_unused]] cros::CameraMojoChannelManagerToken *token)
 {
+	perfetto::TracingInitArgs args;
+	args.backends |= perfetto::kSystemBackend;
+	perfetto::Tracing::Initialize(args);
+	perfetto::TrackEvent::Register();
 }
 
 static void tear_down()
diff --git a/src/android/cros/meson.build b/src/android/cros/meson.build
index 35995dd8..68f2bd9e 100644
--- a/src/android/cros/meson.build
+++ b/src/android/cros/meson.build
@@ -9,5 +9,6 @@  android_hal_sources += files([
 ])
 
 android_deps += dependency('libcros_camera')
+android_deps += dependency('perfetto')
 
 android_cpp_args += ['-DOS_CHROMEOS']
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index 26912ca1..39a55a17 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -71,11 +71,22 @@  if libgnutls.found()
     config_h.set('HAVE_GNUTLS', 1)
 endif
 
-if liblttng.found()
+if libperfetto.found()
+    perfetto_enabled = true
+    tracing_enabled = false
+    config_h.set('HAVE_PERFETTO', 1)
+    libcamera_sources += [
+        'pipeline_perfetto.cpp',
+        'request_perfetto.cpp',
+        'tracepoints.cpp',
+    ]
+elif liblttng.found()
+    perfetto_enabled = false
     tracing_enabled = true
     config_h.set('HAVE_TRACING', 1)
     libcamera_sources += files(['tracepoints.cpp'])
 else
+    perfetto_enabled = false
     tracing_enabled = false
 endif
 
@@ -125,6 +136,7 @@  libcamera_deps = [
     libdl,
     libgnutls,
     liblttng,
+    libperfetto,
     libudev,
 ]
 
diff --git a/src/libcamera/pipeline_perfetto.cpp b/src/libcamera/pipeline_perfetto.cpp
new file mode 100644
index 00000000..07b82ffd
--- /dev/null
+++ b/src/libcamera/pipeline_perfetto.cpp
@@ -0,0 +1,24 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2022, Google Inc.
+ *
+ * pipeline.tp - Tracepoints for pipelines
+ */
+
+#include "libcamera/internal/tracepoints.h"
+
+void LIBCAMERA_TRACE_EVENT_ipa_call_begin(const char *pipe, const char *func)
+{
+	// TODO: Consider TRACE_EVENT_BEGIN
+	TRACE_EVENT("libcamera", "ipa_call_begin",
+		    "pipeline_name", pipe,
+		    "function_name", func);
+}
+
+void LIBCAMERA_TRACE_EVENT_ipa_call_end(const char *pipe, const char *func)
+{
+	// TODO: Consider TRACE_EVENT_END
+	TRACE_EVENT("libcamera", "ipa_call_end",
+		    "pipeline_name", pipe,
+		    "function_name", func);
+}
diff --git a/src/libcamera/request_perfetto.cpp b/src/libcamera/request_perfetto.cpp
new file mode 100644
index 00000000..2cfff28e
--- /dev/null
+++ b/src/libcamera/request_perfetto.cpp
@@ -0,0 +1,73 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2022, Google Inc.
+ *
+ * request.tp - Tracepoints for the request object
+ */
+
+#include <libcamera/framebuffer.h>
+
+#include "libcamera/internal/request.h"
+#include "libcamera/internal/tracepoints.h"
+
+void LIBCAMERA_TRACE_EVENT_request(libcamera::Request *req)
+{
+	TRACE_EVENT("libcamera", "request",
+		    "request_ptr", reinterpret_cast<uintptr_t>(req),
+		    "cookie", req->cookie(),
+		    "status", req->status()); // TODO
+}
+
+void LIBCAMERA_TRACE_EVENT_request_construct(libcamera::Request *req)
+{
+	TRACE_EVENT("libcamera", "request_construct",
+		    "request_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_destroy(libcamera::Request *req)
+{
+	TRACE_EVENT("libcamera", "request_destroy",
+		    "request_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_reuse(libcamera::Request *req)
+{
+	TRACE_EVENT("libcamera", "request_reuse",
+		    "request_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_queue(libcamera::Request *req)
+{
+	TRACE_EVENT("libcamera", "request_queue",
+		    "request_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_device_queue(libcamera::Request *req)
+{
+	TRACE_EVENT("libcamera", "request_device_queue",
+		    "request_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_complete(libcamera::Request::Private *req)
+{
+	TRACE_EVENT("libcamera", "request_complete",
+		    "request_private_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_cancel(libcamera::Request::Private *req)
+{
+	TRACE_EVENT("libcamera", "request_cancel",
+		    "request_private_ptr", reinterpret_cast<uintptr_t>(req));
+}
+
+void LIBCAMERA_TRACE_EVENT_request_complete_buffer(
+	libcamera::Request::Private *req,
+	libcamera::FrameBuffer *buf)
+{
+	TRACE_EVENT("libcamera", "request_complete_buffer",
+		    "request_private_ptr", reinterpret_cast<uintptr_t>(req),
+		    "cookie", req->_o<libcamera::Request>()->cookie(),
+		    "status", req->_o<libcamera::Request>()->status(), // TODO
+		    "buffer_ptr", reinterpret_cast<uintptr_t>(buf),
+		    "buffer_status", buf->metadata().status); // TODO
+}
diff --git a/src/libcamera/tracepoints.cpp b/src/libcamera/tracepoints.cpp
index 0173b75a..a07ea531 100644
--- a/src/libcamera/tracepoints.cpp
+++ b/src/libcamera/tracepoints.cpp
@@ -4,7 +4,18 @@ 
  *
  * tracepoints.cpp - Tracepoints with lttng
  */
+
+#if HAVE_PERFETTO
+
+#include "libcamera/internal/tracepoints.h"
+
+PERFETTO_TRACK_EVENT_STATIC_STORAGE();
+
+#else
+
 #define TRACEPOINT_CREATE_PROBES
 #define TRACEPOINT_DEFINE
 
 #include "libcamera/internal/tracepoints.h"
+
+#endif /* HAVE_PERFETTO */