zsh-workers
 help / color / mirror / code / Atom feed
* Exporting REPORTTIME data to ENVVARs instead of printing.
@ 2015-10-05 11:23 Slava Barinov
  2015-10-05 18:31 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Slava Barinov @ 2015-10-05 11:23 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 844 bytes --]

Hello,

  A while ago I tried to add process timing reports to my ZSH and found that
  REPORTTIME feature only can print timings to terminal but I have a great
  prompt with much information embedded and still having some space to add this
  info.

  My suggestion is to add possibility of exporting timings to envvars instead
  of printing them to terminal.

  My straightforward implementation is attached: while REPORTTIME is switched
  on if REPORTTIME_TO_VAR is defined then four variables are set up:
  REPORTTIME_USER, REPORTTIME_SYSTEM, REPORTTIME_TOTAL and REPORTTIME_CPU. The
  numbers are just summed up for all procs over the job. Don't know if it's a
  good idea to extract timing computation to a new function, so I just copied
  it from `printtime'.

  Will this feature be useful for someone else?

Best Regards,
Slava Barinov.

[-- Attachment #2: reporttime_value.patch --]
[-- Type: text/x-diff, Size: 2442 bytes --]

diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index ba2856b..b9fafab 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -1373,6 +1373,12 @@ executed within the line editor, including completion; commands
 explicitly marked with the tt(time) keyword still cause the summary
 to be printed in this case.
 )
+vindex(REPORTTIME_TO_VAR)
+item(tt(REPORTTIME_TO_VAR))(
+If defined, the values of tt(REPORTTIME) are not printed for processes
+but placed into tt(REPORTTIME_USER), tt(REPORTTIME_SYSTEM),
+tt(REPORTTIME_TOTAL) and tt(REPORTTIME_CPU) for the job.
+)
 vindex(REPLY)
 item(tt(REPLY))(
 This parameter is reserved by convention to pass string values between
diff --git a/Src/jobs.c b/Src/jobs.c
index b47ba8c..72c71dc 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -864,12 +864,52 @@ dumptime(Job jn)
 {
     Process pn;
     struct timeval dtimeval;
+    char *s = "REPORTTIME_TO_VAR";
 
     if (!jn->procs)
 	return;
-    for (pn = jn->procs; pn; pn = pn->next)
+    if (NULL == getsparam(s))
+      for (pn = jn->procs; pn; pn = pn->next)
 	printtime(dtime(&dtimeval, &pn->bgtime, &pn->endtime), &pn->ti,
 		  pn->text);
+    else
+      {
+	double user_time = 0.0, system_time = 0.0;
+	double percent = 0.0, total_time = 0.0;
+	mnumber mnval;
+	mnval.type = MN_FLOAT;
+
+	for (pn = jn->procs; pn; pn = pn->next)
+	  {
+	    const child_times_t *ti = &pn->ti;
+	    const struct timeval *real = dtime(&dtimeval, &pn->bgtime,
+					       &pn->endtime);
+#ifdef HAVE_GETRUSAGE
+	  user_time += ti->ru_utime.tv_sec + ti->ru_utime.tv_usec / 1000000.0;
+	  system_time += ti->ru_stime.tv_sec + ti->ru_stime.tv_usec / 1000000.0;
+	  total_time += user_time + system_time;
+	  percent += 100.0 * total_time
+	    / (real->tv_sec + real->tv_usec / 1000000.0);
+#else
+	  {
+	    long clktck = get_clktck();
+	    user_time    += ti->ut / (double) clktck;
+	    system_time  += ti->st / (double) clktck;
+	    total_time   += user_time + system_time;
+	    percent      += 100.0 * (ti->ut + ti->st)
+	      / (clktck * real->tv_sec + clktck * real->tv_usec / 1000000.0);
+	  }
+#endif
+	  }
+	mnval.u.d = user_time;
+	setnparam("REPORTTIME_USER", mnval);
+	mnval.u.d = system_time;
+	setnparam("REPORTTIME_SYSTEM", mnval);
+	mnval.u.d = total_time;
+	setnparam("REPORTTIME_TOTAL", mnval);
+	mnval.u.d = percent;
+	setnparam("REPORTTIME_CPU", mnval);
+      }
 }
 
 /* Check whether shell should report the amount of time consumed   *

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Exporting REPORTTIME data to ENVVARs instead of printing.
  2015-10-05 11:23 Exporting REPORTTIME data to ENVVARs instead of printing Slava Barinov
@ 2015-10-05 18:31 ` Bart Schaefer
  2015-10-06  0:41   ` Jan Larres
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2015-10-05 18:31 UTC (permalink / raw)
  To: zsh-workers

On Oct 5,  2:23pm, Slava Barinov wrote:
} Subject: Exporting REPORTTIME data to ENVVARs instead of printing.
}
}   REPORTTIME feature only can print timings to terminal but I have a
}   great prompt with much information embedded and still having some
}   space to add this info.
}
}   My suggestion is to add possibility of exporting timings to envvars
}   instead of printing them to terminal.

It would be more in keeping with similar features if the values were
placed in an array or hash rather than in four separate variables.
Or in a single scalar but formatted with $TIMEFMT.

However, if the primary motivation is to include them in the prompt
string, a prompt substitution escape might be more appropriate.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Exporting REPORTTIME data to ENVVARs instead of printing.
  2015-10-05 18:31 ` Bart Schaefer
@ 2015-10-06  0:41   ` Jan Larres
  0 siblings, 0 replies; 3+ messages in thread
From: Jan Larres @ 2015-10-06  0:41 UTC (permalink / raw)
  To: zsh-workers

On 06/10/15 07:31, Bart Schaefer wrote:
> On Oct 5,  2:23pm, Slava Barinov wrote:
> } Subject: Exporting REPORTTIME data to ENVVARs instead of printing.
> }
> }   REPORTTIME feature only can print timings to terminal but I have a
> }   great prompt with much information embedded and still having some
> }   space to add this info.
> }
> }   My suggestion is to add possibility of exporting timings to envvars
> }   instead of printing them to terminal.
>
> It would be more in keeping with similar features if the values were
> placed in an array or hash rather than in four separate variables.
> Or in a single scalar but formatted with $TIMEFMT.
>
> However, if the primary motivation is to include them in the prompt
> string, a prompt substitution escape might be more appropriate.

I recently wrote some functionality that reimplements REPORTTIME as a zsh
script to make it more flexible. It can show the duration of the last 10
commands together with their start and stop times and the exit code:

  $ timerecords
         0  13:36:14 - 13:36:14  0  echo foo
         5  13:36:16 - 13:36:21  0  sleep 5
         0  13:36:28 - 13:36:28  0  echo bar
  $

It could easily be adapted to also provide the information in a way that could
be used in a prompt.

-Jan


zmodload zsh/datetime

typeset -g -a __last_cmds
typeset -g __last_cmd
typeset -g __last_cmd_time

_record_cmd() {
    __last_cmd="$1"
    __last_cmd_time=$(print -P "%D{%s}")
}
add-zsh-hook preexec _record_cmd

_process_cmd_time() {
    [[ -z "$__last_cmd" ]] && return
    local curtime=$(print -P "%D{%s}")
    local timedelta=$(( curtime - __last_cmd_time ))

    local hours=$(( timedelta / 3600 ))
    local minutes=$(( timedelta / 60 % 60 ))
    local seconds=$(( timedelta % 60 ))

    local entry=""
    if (( $hours == 0 )) && (( $minutes == 0 )); then
        entry+="${(l:8:)seconds}"
    elif (( $hours == 0 )); then
        entry+="${(l:5:)minutes}:${(l:2::0:)seconds}"
    else
        entry+="${(l:2:)hours}:${(l:2::0:)minutes}:${(l:2::0:)seconds}"
    fi

    entry+="  $(strftime '%T' $__last_cmd_time) - $(strftime '%T' $curtime)  $__last_cmd_exitstatus  $__last_cmd"

    __last_cmds+=( $entry )
    (( ${#__last_cmds} > 10 )) && shift __last_cmds
}
add-zsh-hook precmd _process_cmd_time

_clear_last_cmd() {
    __last_cmd=""
}
add-zsh-hook precmd _clear_last_cmd

timerecords() {
    print -l ${__last_cmds}
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-10-06  0:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-05 11:23 Exporting REPORTTIME data to ENVVARs instead of printing Slava Barinov
2015-10-05 18:31 ` Bart Schaefer
2015-10-06  0:41   ` Jan Larres

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).