#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2020, Google Inc.
#
# Author: Paul Elder <paul.elder@ideasonboard.com>
#
# analyze-ipa-trace.py - Example of how to extract information from libcamera lttng traces

import argparse
import bt2
import statistics as stats
import sys

# pipeline -> {function -> stack(timestamps)}
timestamps = {}

# pipeline:function -> samples[]
samples = {}

def main(argv):
    parser = argparse.ArgumentParser(
            description='A simple analysis script to get statistics on time taken for IPA calls')
    parser.add_argument('-p', '--pipeline', type=str,
                        help='Name of pipeline to filter for')
    parser.add_argument('trace_path', type=str,
                        help='Path to lttng trace (eg. ~/lttng-traces/demo-20201029-184003)')
    args = parser.parse_args(argv[1:])

    traces = bt2.TraceCollectionMessageIterator(args.trace_path)
    for msg in traces:
        if type(msg) is not bt2._EventMessageConst or \
           'pipeline_name' not in msg.event.payload_field or \
           (args.pipeline is not None and \
            msg.event.payload_field['pipeline_name'] != args.pipeline):
            continue

        pipeline = msg.event.payload_field['pipeline_name']
        event = msg.event.name
        func = msg.event.payload_field['function_name']
        timestamp_ns = msg.default_clock_snapshot.ns_from_origin

        if event == 'libcamera:ipa_call_begin':
            if pipeline not in timestamps:
                timestamps[pipeline] = {}
            if func not in timestamps[pipeline]:
                timestamps[pipeline][func] = []
            timestamps[pipeline][func].append(timestamp_ns)

        if event == 'libcamera:ipa_call_end':
            ts = timestamps[pipeline][func].pop()
            key = f'{pipeline}:{func}'
            if key not in samples:
                samples[key] = []
            samples[key].append(timestamp_ns - ts)

    # Compute stats
    rows = []
    rows.append(['pipeline:function', 'min', 'max', 'mean', 'stddev'])
    for k, v in samples.items():
        mean = int(stats.mean(v))
        stddev = int(stats.stdev(v))
        minv = min(v)
        maxv = max(v)
        rows.append([k, str(minv), str(maxv), str(mean), str(stddev)])

    # Get maximum string width for every column
    widths = []
    for i in range(len(rows[0])):
        widths.append(max([len(row[i]) for row in rows]))

    # Print stats table
    for row in rows:
        fmt = [row[i].rjust(widths[i]) for i in range(1, 5)]
        print('{} {} {} {} {}'.format(row[0].ljust(widths[0]), *fmt))

if __name__ == '__main__':
    sys.exit(main(sys.argv))
;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>src</a>/<a href='/libcamera/libcamera.git/tree/src/apps?h=v0.0.4&amp;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>apps</a>/<a href='/libcamera/libcamera.git/tree/src/apps/qcam?h=v0.0.4&amp;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>qcam</a>/<a href='/libcamera/libcamera.git/tree/src/apps/qcam/assets?h=v0.0.4&amp;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>assets</a>/<a href='/libcamera/libcamera.git/tree/src/apps/qcam/assets/feathericons?h=v0.0.4&amp;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>feathericons</a>/<a href='/libcamera/libcamera.git/tree/src/apps/qcam/assets/feathericons/camera.svg?h=v0.0.4&amp;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>camera.svg</a></div><div class='content'>blob: 0e7f06037ab0ee331040c5c0ada5dadafb9f4591 (<a href='/libcamera/libcamera.git/plain/src/apps/qcam/assets/feathericons/camera.svg?h=v0.0.4&amp;id=9c2ca46391ea2ba089d5d082a0dbdd3b15b28498'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
</pre></td>
<td class='lines'><pre><code><span class="hl kwa">&lt;svg</span> <span class="hl kwb">xmlns</span>=<span class="hl str">&quot;http://www.w3.org/2000/svg&quot;</span> <span class="hl kwb">width</span>=<span class="hl str">&quot;24&quot;</span> <span class="hl kwb">height</span>=<span class="hl str">&quot;24&quot;</span> <span class="hl kwb">viewBox</span>=<span class="hl str">&quot;0 0 24 24&quot;</span> <span class="hl kwb">fill</span>=<span class="hl str">&quot;none&quot;</span> <span class="hl kwb">stroke</span>=<span class="hl str">&quot;currentColor&quot;</span> <span class="hl kwb">stroke-width</span>=<span class="hl str">&quot;2&quot;</span> <span class="hl kwb">stroke-linecap</span>=<span class="hl str">&quot;round&quot;</span> <span class="hl kwb">stroke-linejoin</span>=<span class="hl str">&quot;round&quot;</span> <span class="hl kwb">class</span>=<span class="hl str">&quot;feather feather-camera&quot;</span><span class="hl kwa">&gt;&lt;path</span> <span class="hl kwb">d</span>=<span class="hl str">&quot;M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z&quot;</span><span class="hl kwa">&gt;&lt;/path&gt;&lt;circle</span> <span class="hl kwb">cx</span>=<span class="hl str">&quot;12&quot;</span> <span class="hl kwb">cy</span>=<span class="hl str">&quot;13&quot;</span> <span class="hl kwb">r</span>=<span class="hl str">&quot;4&quot;</span><span class="hl kwa">&gt;&lt;/circle&gt;&lt;/svg&gt;</span>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.1</a> (<a href='https://git-scm.com/'>git 2.18.0</a>) at 2025-03-04 00:19:39 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>