{"id":17781,"url":"https://patchwork.libcamera.org/api/1.1/patches/17781/?format=json","web_url":"https://patchwork.libcamera.org/patch/17781/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20221110173154.488445-11-paul.elder@ideasonboard.com>","date":"2022-11-10T17:31:52","name":"[libcamera-devel,v3,10/12] utils: libtuning: generators: Add yaml output","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"63d8ea0e1f803c5c4f78e7cdfc8817076103e8ae","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17781/mbox/","series":[{"id":3603,"url":"https://patchwork.libcamera.org/api/1.1/series/3603/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3603","date":"2022-11-10T17:31:42","name":"utils: tuning: Add a new tuning infrastructure","version":3,"mbox":"https://patchwork.libcamera.org/series/3603/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17781/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17781/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id EBEFCBD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Nov 2022 17:32:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AAE8763085;\n\tThu, 10 Nov 2022 18:32:38 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B9B8663085\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Nov 2022 18:32:36 +0100 (CET)","from pyrite.tail37cf.ts.net (h175-177-042-159.catv02.itscom.jp\n\t[175.177.42.159])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4A7F1499;\n\tThu, 10 Nov 2022 18:32:35 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1668101558;\n\tbh=AqJwKyQZM0aJZ6Jh7VOQFZO9HCXxBT1jbI9j/OH8T+Q=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=DwPYcfnD8jbctl0Ud3ZEW0bnmeszgc1rBmz7t0MF487cy8z37iDLFLngx3sQucPsf\n\tx47JT0f+i24l5ibHVX0h/yoD6uroGbAPMBtJ7EiIpSOjMJ+TCTlFg5jsqiNw+6tYyv\n\tQ/GxHeSKYEHu3bxT5BxIN8zD8DOGxoIDkkbs3QjAKOaNqPv5WxjTD4dXlu5dZ8pVqt\n\tU3o/jpTFDCc+GuZBhieCYx8PcyNvaDETwBU7RMbMQVSMPADYVT0uSdwqxir0MHpmzx\n\tmmdnhPdeCWXoAzVMfrVt3lJwH5r3KXu0JufiYqpIZszOXPXRtHSP2ds3VfdrFluJ6R\n\tqhP4vInYUohGw==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1668101556;\n\tbh=AqJwKyQZM0aJZ6Jh7VOQFZO9HCXxBT1jbI9j/OH8T+Q=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Yy9Id8K9zHQXv9i5Jk9pYcmpCNcdHvDSGKrI/6Tbvbiq08sMJv2XAhyba3GL/Zx9w\n\tKfEgHp6cboQ57N6m+UGt3OPoCNYXvOvYSrcWS5cN2W2pGa3X1pcGK34ojg/aDRjbSd\n\tW/FfW0Nw/TbZoYUTJb7DQnzBgATdTgqMVhoo8lqo="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Yy9Id8K9\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 11 Nov 2022 02:31:52 +0900","Message-Id":"<20221110173154.488445-11-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20221110173154.488445-1-paul.elder@ideasonboard.com>","References":"<20221110173154.488445-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 10/12] utils: libtuning: generators:\n\tAdd yaml output","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Paul Elder via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Paul Elder <paul.elder@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a generator to libtuning for writing tuning output to a yaml file.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n\n---\nChanges in v3:\n- add file description\n- remove indirection from fake polymorphism\n\nNew in 2\n---\n utils/tuning/libtuning/generators/__init__.py |   1 +\n .../libtuning/generators/yaml_output.py       | 123 ++++++++++++++++++\n 2 files changed, 124 insertions(+)\n create mode 100644 utils/tuning/libtuning/generators/yaml_output.py","diff":"diff --git a/utils/tuning/libtuning/generators/__init__.py b/utils/tuning/libtuning/generators/__init__.py\nindex 937aff30..f28b6149 100644\n--- a/utils/tuning/libtuning/generators/__init__.py\n+++ b/utils/tuning/libtuning/generators/__init__.py\n@@ -3,3 +3,4 @@\n # Copyright (C) 2022, Paul Elder <paul.elder@ideasonboard.com>\n \n from libtuning.generators.raspberrypi_output import RaspberryPiOutput\n+from libtuning.generators.yaml_output import YamlOutput\ndiff --git a/utils/tuning/libtuning/generators/yaml_output.py b/utils/tuning/libtuning/generators/yaml_output.py\nnew file mode 100644\nindex 00000000..6398fc59\n--- /dev/null\n+++ b/utils/tuning/libtuning/generators/yaml_output.py\n@@ -0,0 +1,123 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+#\n+# Copyright 2022 Paul Elder <paul.elder@ideasonboard.com>\n+#\n+# yaml_output.py - Generate tuning file in YAML format\n+\n+from .generator import Generator\n+\n+from numbers import Number\n+from pathlib import Path\n+\n+import libtuning.utils as utils\n+\n+\n+class YamlOutput(Generator):\n+    def __init__(self):\n+        super().__init__()\n+\n+    def _stringify_number_list(self, listt: list):\n+        line_wrap = 80\n+\n+        line = '[ ' + ', '.join([str(x) for x in listt]) + ' ]'\n+        if len(line) <= line_wrap:\n+            return [line]\n+\n+        out_lines = ['[']\n+        line = ' '\n+        for x in listt:\n+            x_str = str(x)\n+            # If the first number is longer than line_wrap, it'll add an extra line\n+            if len(line) + len(x_str) > line_wrap:\n+                out_lines.append(line)\n+                line = f'  {x_str},'\n+                continue\n+            line += f' {x_str},'\n+        out_lines.append(line)\n+        out_lines.append(']')\n+\n+        return out_lines\n+\n+    # @return Array of lines, and boolean of if all elements were numbers\n+    def _stringify_list(self, listt: list):\n+        out_lines = []\n+\n+        all_numbers = set([isinstance(x, Number) for x in listt]).issubset({True})\n+\n+        if all_numbers:\n+            return self._stringify_number_list(listt), True\n+\n+        for value in listt:\n+            if isinstance(value, Number):\n+                out_lines.append(f'- {str(value)}')\n+            elif isinstance(value, str):\n+                out_lines.append(f'- \"{value}\"')\n+            elif isinstance(value, list):\n+                lines, all_numbers = self._stringify_list(value)\n+\n+                if all_numbers:\n+                    out_lines.append( f'- {lines[0]}')\n+                    out_lines +=     [f'  {line}' for line in lines[1:]]\n+                else:\n+                    out_lines.append( f'-')\n+                    out_lines += [f'  {line}' for line in lines]\n+            elif isinstance(value, dict):\n+                lines = self._stringify_dict(value)\n+                out_lines.append( f'- {lines[0]}')\n+                out_lines +=     [f'  {line}' for line in lines[1:]]\n+\n+        return out_lines, False\n+\n+    def _stringify_dict(self, dictt: dict):\n+        out_lines = []\n+\n+        for key in dictt:\n+            value = dictt[key]\n+\n+            if isinstance(value, Number):\n+                out_lines.append(f'{key}: {str(value)}')\n+            elif isinstance(value, str):\n+                out_lines.append(f'{key}: \"{value}\"')\n+            elif isinstance(value, list):\n+                lines, all_numbers = self._stringify_list(value)\n+\n+                if all_numbers:\n+                    out_lines.append( f'{key}: {lines[0]}')\n+                    out_lines +=     [f'{\" \" * (len(key) + 2)}{line}' for line in lines[1:]]\n+                else:\n+                    out_lines.append( f'{key}:')\n+                    out_lines +=     [f'  {line}' for line in lines]\n+            elif isinstance(value, dict):\n+                lines = self._stringify_dict(value)\n+                out_lines.append( f'{key}:')\n+                out_lines +=     [f'  {line}' for line in lines]\n+\n+        return out_lines\n+\n+    def write(self, output_file: Path, output_dict: dict, output_order: list):\n+        out_lines = [\n+            '%YAML 1.1',\n+            '---',\n+            'version: 1',\n+            # No need to condition this, as libtuning already guarantees that\n+            # we have at least one module. Even if the module has no output,\n+            # its prescence is sufficient.\n+            'algorithms:'\n+        ]\n+\n+        for module in output_order:\n+            out_lines.append(f'  - {module.out_name}:')\n+\n+            if len(output_dict[module]) == 0:\n+                continue\n+\n+            if not isinstance(output_dict[module], dict):\n+                utils.eprint(f'Error: Output of {module.type} is not a dictionary')\n+                continue\n+\n+            lines = self._stringify_dict(output_dict[module])\n+            out_lines += [f'      {line}' for line in lines]\n+\n+        with open(output_file, 'w') as f:\n+            for line in out_lines:\n+                f.write(f'{line}\\n')\n","prefixes":["libcamera-devel","v3","10/12"]}