zsh-workers
 help / color / mirror / code / Atom feed
From: Slava Barinov <rayslava@gmail.com>
To: zsh-workers@zsh.org
Subject: Exporting REPORTTIME data to ENVVARs instead of printing.
Date: Mon, 5 Oct 2015 14:23:34 +0300	[thread overview]
Message-ID: <CADef55pgC+ym697KLegbZeOM9sMq6j2s3H=R6Te=PozUStJZ8Q@mail.gmail.com> (raw)

[-- 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   *

             reply	other threads:[~2015-10-05 11:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-05 11:23 Slava Barinov [this message]
2015-10-05 18:31 ` Bart Schaefer
2015-10-06  0:41   ` Jan Larres

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADef55pgC+ym697KLegbZeOM9sMq6j2s3H=R6Te=PozUStJZ8Q@mail.gmail.com' \
    --to=rayslava@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).