From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Lars Brinkhoff Newsgroups: gmane.emacs.devel Subject: get-internal-run-time Date: 01 May 2004 09:01:16 +0200 Organization: nocrew Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <85vfjgbphf.fsf_-_@junk.nocrew.org> References: <85k7036eqr.fsf@junk.nocrew.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1083395135 24342 80.91.224.253 (1 May 2004 07:05:35 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 1 May 2004 07:05:35 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Sat May 01 09:05:25 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BJoYv-0006RQ-00 for ; Sat, 01 May 2004 09:05:25 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BJoYv-0005bE-00 for ; Sat, 01 May 2004 09:05:25 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BJoXH-0002Fc-46 for emacs-devel@quimby.gnus.org; Sat, 01 May 2004 03:03:43 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BJoWo-0002EU-Ef for emacs-devel@gnu.org; Sat, 01 May 2004 03:03:14 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BJoWI-00023O-LI for emacs-devel@gnu.org; Sat, 01 May 2004 03:03:13 -0400 Original-Received: from [213.242.147.30] (helo=junk.nocrew.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BJoUx-0001bI-1M; Sat, 01 May 2004 03:01:19 -0400 Original-Received: from lars by junk.nocrew.org with local (Exim 3.35 #1 (Debian)) id 1BJoUu-0002Cu-00; Sat, 01 May 2004 09:01:16 +0200 Original-To: rms@gnu.org, emacs-devel@gnu.org In-Reply-To: Original-Lines: 103 User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:22482 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:22482 Richard Stallman 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. 2004-05-01 Lars Brinkhoff * editfns.c (Fget_internal_run_time): New function. (syms_of_data): Defsubr it. Index: editfns.c =================================================================== RCS file: /cvsroot/emacs/emacs/src/editfns.c,v retrieving revision 1.372 diff -c -r1.372 editfns.c *** editfns.c 27 Apr 2004 13:28:38 -0000 1.372 --- editfns.c 1 May 2004 06:57:46 -0000 *************** *** 39,44 **** --- 39,48 ---- #include #endif + #ifdef HAVE_SYS_RESOURCE_H + #include + #endif + #include #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);