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

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).