Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Exporting REPORTTIME data to ENVVARs instead of printing.



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


Messages sorted by: Reverse Date, Date, Thread, Author