Skip to content

Commit

Permalink
Move timer to sys
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin committed Feb 25, 2021
1 parent 7324b4a commit 96662a4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 105 deletions.
105 changes: 0 additions & 105 deletions l3experimental/l3benchmark/l3benchmark.dtx
Expand Up @@ -52,26 +52,6 @@
%
% \begin{documentation}
%
% \section{Additions to \pkg{l3sys}: elapsed time}
%
% \begin{function}{\sys_gzero_timer:}
% \begin{syntax}
% \cs{sys_gzero_timer:}
% \end{syntax}
% Resets the timer to zero.
% \end{function}
%
% \begin{function}[EXP]{\sys_timer:}
% \begin{syntax}
% \cs{sys_timer:}
% \end{syntax}
% Expands to the current value of the engine's timer clock, a
% non-negative integer. In engines without clock support this expands
% to $0$ after an error. In \LuaTeX{} only the CPU time is measured,
% while in other engines real time is measured (including time waiting
% for user input).
% \end{function}
%
% \section{Benchmark}
%
% \begin{variable}{\g_benchmark_duration_target_fp}
Expand Down Expand Up @@ -133,91 +113,6 @@
{L3 Experimental benchmarking}
% \end{macrocode}
%
% \subsection{Additions to \pkg{l3sys}: elapsed time}
%
% \begin{macrocode}
%<@@=sys>
% \end{macrocode}
%
% \begin{macro}[EXP]{\sys_timer:, \@@_elapsedtime:, \@@_resettimer:}
% \begin{macro}[EXP,pTF]{\sys_if_timer_exist:}
% In \LuaTeX{}, use emulation (see \pkg{l3luatex}),
% otherwise try to locate the primitive. The
% elapsed time will be available if this succeeds.
% \begin{macrocode}
\sys_if_engine_luatex:TF
{
\cs_new:Npn \@@_elapsedtime: { }
\cs_new:Npn \@@_resettimer: { }
\lua_now:n
{
local~gettimeofday = os.gettimeofday~
local~epoch = gettimeofday() - os.clock()~
local~write = tex.write~
local~tointeger = math.tointeger~
local~id = luatexbase.new_luafunction'@@_elapsedtime:'
lua.get_functions_table()[id] = function()
write(tointeger((gettimeofday() - epoch)*65536 // 1))
end~
token.set_lua('@@_elapsedtime:', id, 'global')
id = luatexbase.new_luafunction'@@_resettimer:'
lua.get_functions_table()[id] = function()
epoch = gettimeofday()
end~
token.set_lua('@@_resettimer:', id, 'global', 'protected')
}
\cs_new:Npn \sys_timer:
{ \@@_elapsedtime: }
}
{
\cs_if_exist:NT \tex_elapsedtime:D
{
\cs_new:Npn \sys_timer:
{ \int_value:w \tex_elapsedtime:D }
}
}
\@@_const:nn { sys_if_timer_exist }
{ \cs_if_exist_p:N \sys_timer: }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sys_gzero_timer:}
% Three case, and in each case we define \cs{sys_gzero_timer:}.
% \begin{macrocode}
\cs_if_exist:NTF \sys_timer:
{
\sys_if_engine_luatex:TF
{
\cs_new_protected:Npn \sys_gzero_timer:
{ \@@_resettimer: }
}
{ \cs_new_protected:Npn \sys_gzero_timer: { \tex_resettimer:D } }
}
% \end{macrocode}
% If the elapsed time cannot be accessed then we define user
% commands to produce errors.
% \begin{macrocode}
{
\__kernel_msg_new:nnnn { kernel } { no-elapsed-time }
{ No~clock~detected~for~#1. }
{ The~current~engine~provides~no~way~to~access~the~system~time. }
\cs_new_protected:Npn \sys_gzero_timer:
{
\__kernel_msg_error:nnn { kernel } { no-elapsed-time }
{ \sys_gzero_timer: }
}
\cs_new:Npn \sys_timer:
{
\int_value:w
\__kernel_msg_expandable_error:nnn { kernel } { no-elapsed-time }
{ \sys_timer: }
\c_zero_int
}
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Benchmarking code}
%
% \begin{macrocode}
Expand Down
58 changes: 58 additions & 0 deletions l3kernel/l3sys.dtx
Expand Up @@ -121,6 +121,17 @@
% \tn{fmtname}).
% \end{variable}
%
% \begin{function}[added = 2020-09-24, EXP]{\sys_timer:}
% \begin{syntax}
% \cs{sys_timer:}
% \end{syntax}
% Expands to the current value of the engine's timer clock, a
% non-negative integer. This function is only defined for engines with
% timer support. This command measures not just CPU time but
% real time (including time waiting for user input). The unit are
% scaled seconds ($2^{-16}$ seconds).
% \end{function}
%
% \section{Output format}
%
% \begin{function}[added = 2015-09-19, EXP, pTF]
Expand Down Expand Up @@ -863,6 +874,53 @@ end
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\sys_timer:, \@@_elapsedtime:}
% \begin{macro}[EXP, pTF]{\sys_if_timer_exist:}
% In \LuaTeX{}, create a pseudo-primitve, otherwise try to
% locate the real primitive. The elapsed time will be
% available if this succeeds.
% \begin{macrocode}
%</tex>
%<*lua>
local gettimeofday = os.gettimeofday
local epoch = gettimeofday() - os.clock()
local write = tex.write
local tointeger = math.tointeger
luacmd('@@_elapsedtime:', function()
write(tointeger((gettimeofday() - epoch)*65536 // 1))
end, 'global')
%</lua>
%<*tex>
\sys_if_engine_luatex:TF
{
\cs_new:Npn \sys_timer:
{ \@@_elapsedtime: }
}
{
\cs_if_exist:NTF \tex_elapsedtime:D
{
\cs_new:Npn \sys_timer:
{ \int_value:w \tex_elapsedtime:D }
}
{
\__kernel_msg_new:nnnn { kernel } { no-elapsed-time }
{ No~clock~detected~for~#1. }
{ The~current~engine~provides~no~way~to~access~the~system~time. }
\cs_new:Npn \sys_timer:
{
\int_value:w
\__kernel_msg_expandable_error:nnn { kernel } { no-elapsed-time }
{ \sys_timer: }
\c_zero_int
}
}
}
\@@_const:nn { sys_if_timer_exist }
{ \cs_if_exist_p:N \tex_elapsedtime:D || \cs_if_exist_p:N \@@_elapsedtime: }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Access to the shell}
%
% \begin{variable}{\c_sys_shell_escape_int}
Expand Down

0 comments on commit 96662a4

Please sign in to comment.