From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 9E3BB15BB583; Tue, 11 Nov 2025 17:29:14 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9E3BB15BB583 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1762871354; bh=awSV+B5Ws/v4Rz0Xn/ryb7ynNHrBcuxAk1XMxy8JmTM=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=yPqZ0gWKxKtIyyJbeU7D01pnsJ958Mmx8gEQtZrck4FKD80c2K+hOSy7LrWLUYGwQ kZV2p0/sONvzY0htbVJ3ydtxnv+/n3krdakY+N/63O/fUF2bcGFLTrMpHGkMVWSVlE jAw9FcD3dYWDrBB/YzKwf/QykrE0UOnCWFlmkDrM= Received: from send149.i.mail.ru (send149.i.mail.ru [89.221.237.244]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A8D8115496A7 for ; Tue, 11 Nov 2025 17:28:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A8D8115496A7 Received: by exim-smtp-68c8686b96-tdghg with esmtpa (envelope-from ) id 1vIpMW-00000000WOp-2yno; Tue, 11 Nov 2025 17:28:49 +0300 Content-Type: multipart/alternative; boundary="------------aKZ4ukx4tyndt2H30vwopttp" Message-ID: <7696cc7b-12a5-4194-a28d-43f5810c93e3@tarantool.org> Date: Tue, 11 Nov 2025 17:28:48 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org References: <9c378a741df0ebcd8e56ec738436ff450c167d72.1761301736.git.skaplun@tarantool.org> In-Reply-To: <9c378a741df0ebcd8e56ec738436ff450c167d72.1761301736.git.skaplun@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9E2FEC05A616BBE39DBCE7D9BBE86FC7CEA9871AF525542C6182A05F5380850406AD356043E2F425D3DE06ABAFEAF6705E1769A6400E530287763F5021ED7DFF0AE87E38DD37F4ED5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77633BACAB33B9508C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE7353CFE866E745C13EA1F7E6F0F101C67CDEEF6D7F21E0D1D9295C2E9FA3191EE1B59CA4C82EFA65849D5AF5C37589E87B3BAE949C79DEA8AB95B472E06E54EDA117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18F04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE9647ADFADE5905B1A9539A8B24243104D8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE35E3BF8C76DC23F746136E347CC761E07C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006375F081080233BD282EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A56A8BAE84E7B710C25002B1117B3ED6960000068EF5987FFC9E040399BDE4761E823CB91A9FED034534781492E4B8EEAD17AEC49845D0B908 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF447057BC9C462C4EFAF1F94A4869D966950F1B9CC637BBAB9B03A683420D7452ACCA692FBCDC96C223BB165979B4CD0AF12384E9D823AED35E09A4654528C92CD365A6BA2D83CD06111DC66A97D0BFE2913E6812662D5F2AB9AF64DB4688768036DF5FE9C0001AF333F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVVMPnv1PZQG3pCFJJ+cIgMg= X-Mailru-Sender: 520A125C2F17F0B1A9638AD358559B593216A50261C118613DE06ABAFEAF6705E1769A6400E53028B7CBEF92542CD7C8795FA72BAB74744FC77752E0C033A69EA16A481184E8BB1C9B38E6EA4F046BE03A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 luajit 02/41] perf: introduce clock module X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This is a multi-part message in MIME format. --------------aKZ4ukx4tyndt2H30vwopttp Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, Sergey! thanks for the patch! Please see my comments. Sergey On 10/24/25 13:50, Sergey Kaplun wrote: > This module contains 2 functions: > - `realtime()` -- returns the time represented by the wall clock. > - `process_cputime()` -- returns the time consumed by all threads of > the process. I would rephrase second bullet: "to measure CPU time instead of elapsed time" Also, I would add this description to the Lua module as well. > > Both functions are implemented via FFI call to the `clock_gettime()`. > --- > perf/utils/clock.lua | 35 +++++++++++++++++++++++++++++++++++ > 1 file changed, 35 insertions(+) > create mode 100644 perf/utils/clock.lua > > diff --git a/perf/utils/clock.lua b/perf/utils/clock.lua > new file mode 100644 > index 00000000..57385967 > --- /dev/null > +++ b/perf/utils/clock.lua > @@ -0,0 +1,35 @@ > +local ffi = require('ffi') > + > +ffi.cdef[[ > +struct timespec { > + long tv_sec; /* Seconds. */ > + long tv_nsec; /* Nanoseconds. */ > +}; > + > +int clock_gettime(int clockid, struct timespec *tp); > +]] > + > +local C = ffi.C > + > +-- Wall clock. > +local CLOCK_REALTIME = 0  This clock is not a reliable source of the time. This clock can be adjusted by NTP or manually or by timezones. It is better to use CLOCK_MONOTONIC or even CLOCK_MONOTONIC_RAW (not portable, Linux-specific), it is more reliable and does not depend on things listed above. > +-- CPU time consumed by the process. > +local CLOCK_PROCESS_CPUTIME_ID = 2 > + > +-- All functions below returns the corresponding `clock_gettime()` s/`clock_gettime()`/elapsed time/ > +-- in seconds. > +local M = {} > + > +local timespec = ffi.new('struct timespec[1]') > + > +function M.realtime() > + C.clock_gettime(CLOCK_REALTIME, timespec) > + return tonumber(timespec[0].tv_sec) + tonumber(timespec[0].tv_nsec) / 1e9 > +end > + may be it is better to make conversion only once? @@ -24,7 +24,7 @@ local timespec = ffi.new('struct timespec[1]')  function M.realtime()    C.clock_gettime(CLOCK_REALTIME, timespec) -  return tonumber(timespec[0].tv_sec) + tonumber(timespec[0].tv_nsec) / 1e9 +  return tonumber(timespec[0].tv_sec + timespec[0].tv_nsec / 1e9)  end the same below > +function M.process_cputime() > + C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, timespec) > + return tonumber(timespec[0].tv_sec) + tonumber(timespec[0].tv_nsec) / 1e9 > +end > + > +return M --------------aKZ4ukx4tyndt2H30vwopttp Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi, Sergey!

thanks for the patch! Please see my comments.

Sergey

On 10/24/25 13:50, Sergey Kaplun wrote:
This module contains 2 functions:
- `realtime()` -- returns the time represented by the wall clock.
- `process_cputime()` -- returns the time consumed by all threads of
  the process.
I would rephrase second bullet: "to measure CPU time instead of elapsed time"
Also, I would add this description to the Lua module as well.

Both functions are implemented via FFI call to the `clock_gettime()`.
---
 perf/utils/clock.lua | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 perf/utils/clock.lua

diff --git a/perf/utils/clock.lua b/perf/utils/clock.lua
new file mode 100644
index 00000000..57385967
--- /dev/null
+++ b/perf/utils/clock.lua
@@ -0,0 +1,35 @@
+local ffi = require('ffi')
+
+ffi.cdef[[
+struct timespec {
+  long tv_sec; /* Seconds. */
+  long tv_nsec; /* Nanoseconds. */
+};
+
+int clock_gettime(int clockid, struct timespec *tp);
+]]
+
+local C = ffi.C
+
+-- Wall clock.
+local CLOCK_REALTIME = 0

 This clock is not a reliable source of the time. This clock can be adjusted by

NTP or manually or by timezones. It is better to use CLOCK_MONOTONIC or

even CLOCK_MONOTONIC_RAW (not portable, Linux-specific), it is more reliable

and does not depend on things listed above.

+-- CPU time consumed by the process.
+local CLOCK_PROCESS_CPUTIME_ID = 2
+
+-- All functions below returns the corresponding `clock_gettime()`
s/`clock_gettime()`/elapsed time/
+-- in seconds.
+local M = {}
+
+local timespec = ffi.new('struct timespec[1]')
+
+function M.realtime()
+  C.clock_gettime(CLOCK_REALTIME, timespec)
+  return tonumber(timespec[0].tv_sec) + tonumber(timespec[0].tv_nsec) / 1e9
+end
+

may be it is better to make conversion only once?

@@ -24,7 +24,7 @@ local timespec = ffi.new('struct timespec[1]')
 
 function M.realtime()
   C.clock_gettime(CLOCK_REALTIME, timespec)
-  return tonumber(timespec[0].tv_sec) + tonumber(timespec[0].tv_nsec) / 1e9
+  return tonumber(timespec[0].tv_sec + timespec[0].tv_nsec / 1e9)
 end
 
the same below

+function M.process_cputime()
+  C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, timespec)
+  return tonumber(timespec[0].tv_sec) + tonumber(timespec[0].tv_nsec) / 1e9
+end
+
+return M
--------------aKZ4ukx4tyndt2H30vwopttp--