[{"id":2656,"web_url":"https://patchwork.libcamera.org/comment/2656/","msgid":"<20190914114407.GI12549@bigcity.dyn.berto.se>","date":"2019-09-14T11:44:07","subject":"Re: [libcamera-devel] [PATCH v2 1/2] libcamera: utils: Add clock\n\thelpers","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your work.\n\nOn 2019-09-14 14:06:19 +0300, Laurent Pinchart wrote:\n> In preparation for standardisation of the std::chrono::steady_clock as\n> the libcamera default clock, define aliases for the clock, duration and\n> time point, and add helper functions to convert a duration to a timespec\n> and a time point to a string. More helpers will be added later as\n> needed.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  src/libcamera/include/utils.h | 10 +++++++\n>  src/libcamera/utils.cpp       | 50 +++++++++++++++++++++++++++++++++++\n>  2 files changed, 60 insertions(+)\n> \n> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\n> index 47a422587338..52eee8ac2804 100644\n> --- a/src/libcamera/include/utils.h\n> +++ b/src/libcamera/include/utils.h\n> @@ -8,7 +8,10 @@\n>  #define __LIBCAMERA_UTILS_H__\n>  \n>  #include <algorithm>\n> +#include <chrono>\n>  #include <memory>\n> +#include <string>\n> +#include <sys/time.h>\n>  \n>  #define ARRAY_SIZE(a)\t(sizeof(a) / sizeof(a[0]))\n>  \n> @@ -53,6 +56,13 @@ const T& clamp(const T& v, const T& lo, const T& hi)\n>  \treturn std::max(lo, std::min(v, hi));\n>  }\n>  \n> +using clock = std::chrono::steady_clock;\n> +using duration = std::chrono::steady_clock::duration;\n> +using time_point = std::chrono::steady_clock::time_point;\n> +\n> +struct timespec duration_to_timespec(const duration &value);\n> +std::string time_point_to_string(const time_point &time);\n> +\n>  } /* namespace utils */\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> index afbdd8349790..fe7ec53302a3 100644\n> --- a/src/libcamera/utils.cpp\n> +++ b/src/libcamera/utils.cpp\n> @@ -7,6 +7,8 @@\n>  \n>  #include \"utils.h\"\n>  \n> +#include <iomanip>\n> +#include <sstream>\n>  #include <stdlib.h>\n>  #include <string.h>\n>  #include <unistd.h>\n> @@ -93,6 +95,54 @@ char *secure_getenv(const char *name)\n>   * \\return lo if v is less than lo, hi if v is greater than hi, otherwise v\n>   */\n>  \n> +/**\n> + * \\typedef clock\n> + * \\brief The libcamera clock (monotonous)\n\nI would s/monotonous/monotonic/ but it's just bike shedding ;-)\n\nWith or without this addressed,\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> + */\n> +\n> +/**\n> + * \\typedef duration\n> + * \\brief The libcamera duration related to libcamera::utils::clock\n> + */\n> +\n> +/**\n> + * \\typedef time_point\n> + * \\brief The libcamera time point related to libcamera::utils::clock\n> + */\n> +\n> +/**\n> + * \\brief Convert a duration to a timespec\n> + * \\param[in] value The duration\n> + * \\return A timespec expressing the duration\n> + */\n> +struct timespec duration_to_timespec(const duration &value)\n> +{\n> +\tuint64_t nsecs = std::chrono::duration_cast<std::chrono::nanoseconds>(value).count();\n> +\tstruct timespec ts;\n> +\tts.tv_sec = nsecs / 1000000000ULL;\n> +\tts.tv_nsec = nsecs % 1000000000ULL;\n> +\treturn ts;\n> +}\n> +\n> +/**\n> + * \\brief Convert a time point to a string representation\n> + * \\param[in] time The time point\n> + * \\return A string representing the time point in hh:mm:ss.nanoseconds format\n> + */\n> +std::string time_point_to_string(const time_point &time)\n> +{\n> +\tuint64_t nsecs = std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch()).count();\n> +\tunsigned int secs = nsecs / 1000000000ULL;\n> +\n> +\tstd::ostringstream ossTimestamp;\n> +\tossTimestamp.fill('0');\n> +\tossTimestamp << secs / (60 * 60) << \":\"\n> +\t\t     << std::setw(2) << (secs / 60) % 60 << \":\"\n> +\t\t     << std::setw(2) << secs % 60 << \".\"\n> +\t\t     << std::setw(9) << nsecs % 1000000000ULL;\n> +\treturn ossTimestamp.str();\n> +}\n> +\n>  } /* namespace utils */\n>  \n>  } /* namespace libcamera */\n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lf1-x142.google.com (mail-lf1-x142.google.com\n\t[IPv6:2a00:1450:4864:20::142])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7602660BB0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 14 Sep 2019 13:44:09 +0200 (CEST)","by mail-lf1-x142.google.com with SMTP id u26so6300539lfg.6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 14 Sep 2019 04:44:09 -0700 (PDT)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\t77sm7119043ljf.85.2019.09.14.04.44.07\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tSat, 14 Sep 2019 04:44:07 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=JH9uSFsCWAYrRFdnhMsUOlvoJUzi/ZKgxRhMnZbd1XE=;\n\tb=z0YQwGvNTAus5FkZ0IffUnVu7/p6egxOyC+wJfCu6tnuqyk39T2YaDfd+13r27hrz1\n\td524sFpbekCK2ZIm86BsXQdgqOpKXVfeEND0CBHgt+5HjvGIh00f+e/iHbJEyu8qhW0L\n\t6xLCEIsN2CmvmcmgjxUU3k6krQab8/oNCd/4eHsWiVPYKYDY9qJIG8Dfx6R9FzWhoQG4\n\td3HH29Lkj81xzgbHcKx97Xa68WJjQQzGMQE6ZN2EEFTcmsz4dr9hARPQuMwTeBPCKjll\n\t51Da93JGljfb84MN9nEolYEdQUBqQODz3FqwLR9+NcIdpz9vNZFqfO32Qa4eyibpMQVv\n\tQNdQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=JH9uSFsCWAYrRFdnhMsUOlvoJUzi/ZKgxRhMnZbd1XE=;\n\tb=jYLGegT9ltnYwjTQSkV7inwRuRnF2btrJNlnhy4shqmXUq7ysSYg2VSOTJR85t5Dj4\n\t0x+pU4GFrlFMgtt7xk03RSY6eZFAK4Dhmed0qQ8BtOTn/1OCecCufIsoGABHK8EO6O//\n\tIm3008gGryKkLsaPST7forJ4nnaZI5RpWhC5j21OB7LBuUIC7dwSdO77TUqjMogF/HGv\n\tKD7+Ls3FZd+JCqBwjBf4yCwzkSwZV9ajc0K0ppgJ+FCCPGpn91yw+1UPKhnQ2XLmKl9q\n\t7LGdVI9/1/Mv8kR9bj4Ki4mj0tcxDbr2VuTw+TWR06o5BNg8UHRKKwWbEkbyXBZsyPqU\n\tWQ9Q==","X-Gm-Message-State":"APjAAAUlsfhYQarKX71y2ywEaf/rg1dQRlBSNksb33XmaXiCiEWOGAKI\n\tHX1DwnS9s2O9hjYOz3AHuwi4LGKAMs4=","X-Google-Smtp-Source":"APXvYqzp4NyzfmW/8UFRDd38YqfuePHsgMl2fnXcoOfkjATCuqgRo4pjPGQVUYll9Y65WI8P0K4OIg==","X-Received":"by 2002:a19:c396:: with SMTP id\n\tt144mr34238080lff.14.1568461448658; \n\tSat, 14 Sep 2019 04:44:08 -0700 (PDT)","Date":"Sat, 14 Sep 2019 13:44:07 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190914114407.GI12549@bigcity.dyn.berto.se>","References":"<20190914110620.23290-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190914110620.23290-1-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [PATCH v2 1/2] libcamera: utils: Add clock\n\thelpers","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Sat, 14 Sep 2019 11:44:09 -0000"}}]