emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: get-internal-run-time


From: Lars Brinkhoff
Subject: Re: get-internal-run-time
Date: 01 May 2004 20:53:01 +0200
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

Lars Brinkhoff <address@hidden> writes:
> Richard Stallman <address@hidden> writes:
> > [Lars Brinkhoff wrote:]
> >     [I wish there were a] function that returns the amount of
> >     processor time used [by Emacs], for implementing [the Common
> >     Lisp function] get-internal-run-time.
> > 
> >     ANSI CL says:
> >         The intent is that the difference between the values of
> >         two calls to this function be the amount of time between
> >         the two calls during which computational effort was
> >         expended on behalf of the executing program.
> >     I haven't found a suitable function for this in Emacs.
> > 
> > Would you like to contribute such a function?
> 
> Here's a first attempt, submitted for comment.

Here's a more complete patch:

etc/NEWS:
** The new primitive `get-internal-run-time' returns the processor
run time used by Emacs so far.

src/ChangeLog:
2004-05-01  Lars Brinkhoff  <address@hidden>

        * editfns.c (Fget_internal_run_time): New function.
        (syms_of_data): Defsubr it.

lispref/ChangeLog:
2004-05-01  Lars Brinkhoff  <address@hidden>

        * os.texi (Processor Run Time): New section documenting
        get-internal-run-time.

Index: src/editfns.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/editfns.c,v
retrieving revision 1.372
diff -c -r1.372 editfns.c
*** src/editfns.c       27 Apr 2004 13:28:38 -0000      1.372
--- src/editfns.c       1 May 2004 07:26:07 -0000
***************
*** 39,44 ****
--- 39,48 ----
  #include <stdio.h>
  #endif
  
+ #ifdef HAVE_SYS_RESOURCE_H
+ #include <sys/resource.h>
+ #endif
+ 
  #include <ctype.h>
  
  #include "lisp.h"
***************
*** 1375,1380 ****
--- 1379,1425 ----
  
    return Flist (3, result);
  }
+ 
+ DEFUN ("get-internal-run-time", Fget_internal_run_time, 
Sget_internal_run_time,
+        0, 0, 0,
+        doc: /* Return the current run time used by Emacs.
+ The time is returned as a list of three integers.  The first has the
+ most significant 16 bits of the seconds, while the second has the
+ least significant 16 bits.  The third integer gives the microsecond
+ count.
+ 
+ On systems that can't determine the run time, get-internal-run-time
+ does the same thing as current-time.  The microsecond count is zero on
+ systems that do not provide resolution finer than a second.  */)
+      ()
+ {
+ #ifdef HAVE_SYS_RESOURCE_H
+   struct rusage usage;
+   Lisp_Object result[3];
+   int secs, usecs;
+ 
+   if (getrusage (RUSAGE_SELF, &usage) < 0)
+     /* This shouldn't happen.  What action is appropriate?  */
+     Fsignal (Qerror, Qnil);
+ 
+   /* Sum up user time and system time.  */
+   secs = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
+   usecs = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
+   if (usecs >= 1000000)
+     {
+       usecs -= 1000000;
+       secs++;
+     }
+ 
+   XSETINT (result[0], (secs >> 16) & 0xffff);
+   XSETINT (result[1], (secs >> 0)  & 0xffff);
+   XSETINT (result[2], usecs);
+ 
+   return Flist (3, result);
+ #else
+   return Fcurrent_time ();
+ #endif
+ }
  
  
  int
***************
*** 4280,4285 ****
--- 4325,4331 ----
    defsubr (&Suser_full_name);
    defsubr (&Semacs_pid);
    defsubr (&Scurrent_time);
+   defsubr (&Sget_internal_run_time);
    defsubr (&Sformat_time_string);
    defsubr (&Sfloat_time);
    defsubr (&Sdecode_time);
Index: lispref/os.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/lispref/os.texi,v
retrieving revision 1.62
diff -c -r1.62 os.texi
*** lispref/os.texi     17 Feb 2004 01:06:10 -0000      1.62
--- lispref/os.texi     1 May 2004 07:26:08 -0000
***************
*** 23,28 ****
--- 23,29 ----
  * Time of Day::               Getting the current time.
  * Time Conversion::     Converting a time from numeric form to a string, or
                            to calendrical data (or vice versa).
+ * Processor Run Time::  Getting the run time used by Emacs.
  * Time Calculations::   Adding, subtracting, comparing times, etc.
  * Timers::            Setting a timer to call a function at a certain time.
  * Terminal Input::      Recording terminal input for debugging.
***************
*** 1247,1252 ****
--- 1248,1275 ----
  if you try to encode a time that is out of range, an error results.
  For instance, years before 1970 do not work on some systems;
  on others, years as early as 1901 do work.
+ @end defun
+ 
+ @node Processor Run Time
+ @section Processor Run time
+ 
+ @defun get-internal-run-time
+ This function returns the processor run time used by Emacs as a list
+ of three integers: @code{(@var{high} @var{low} @var{microsec})}.  The
+ integers @var{high} and @var{low} combine to give the number of
+ seconds, which is
+ @ifnottex
+ @var{high} * 2**16 + @var{low}.
+ @end ifnottex
+ @tex
+ $high*2^{16}+low$.
+ @end tex
+ 
+ The third element, @var{microsec}, gives the microseconds (or 0 for
+ systems that return time with the resolution of only one second).
+ 
+ If the system doesn't provide a way to determine the processor run
+ time, get-internal-run-time returns the same time as current-time.
  @end defun
  
  @node Time Calculations





reply via email to

[Prev in Thread] Current Thread [Next in Thread]