Merge from emacs-23; up to 2010-06-15T03:34:12Z!rgm@gnu.org.
[bpt/emacs.git] / lib-src / profile.c
CommitLineData
f9356baa 1/* profile.c --- generate periodic events for profiling of Emacs Lisp code.
73b0cd50 2 Copyright (C) 1992, 1994, 1999, 2001-2011 Free Software Foundation, Inc.
f9356baa 3
294981c7 4Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>
f9356baa 5
3b7ad313
EN
6This file is part of GNU Emacs.
7
294981c7 8GNU Emacs is free software: you can redistribute it and/or modify
3b7ad313 9it under the terms of the GNU General Public License as published by
294981c7
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
3b7ad313
EN
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
294981c7 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f9356baa
RS
20
21
22/**
23 ** To be run as an emacs process. Input string that starts with:
24 ** 'z' -- resets the watch (to zero).
25 ** 'p' -- return time (on stdout) as string with format <sec>.<micro-sec>
26 ** 'q' -- exit.
27 **
28 ** abstraction : a stopwatch
29 ** operations: reset_watch, get_time
30 */
f98d41f5 31#include <config.h>
4ee9629e 32#include <stdio.h>
f98d41f5 33#include <systime.h>
f9356baa 34
db965a28 35static EMACS_TIME TV1, TV2;
f9356baa
RS
36static int watch_not_started = 1; /* flag */
37static char time_string[30];
38
39/* Reset the stopwatch to zero. */
40
b23b5a5b 41static void
873fbd0b 42reset_watch (void)
f9356baa 43{
72ddb82c 44 EMACS_GET_TIME (TV1);
f9356baa
RS
45 watch_not_started = 0;
46}
47
48/* This call returns the time since the last reset_watch call. The time
177c0ea7 49 is returned as a string with the format <seconds>.<micro-seconds>
1c1fce3f 50 If reset_watch was not called yet, exit. */
f9356baa 51
b23b5a5b 52static char *
873fbd0b 53get_time (void)
f9356baa 54{
f9356baa 55 if (watch_not_started)
65396510 56 exit (EXIT_FAILURE); /* call reset_watch first ! */
72ddb82c 57 EMACS_GET_TIME (TV2);
db965a28 58 EMACS_SUB_TIME (TV2, TV2, TV1);
9ae17778 59 sprintf (time_string, "%lu.%06lu", (unsigned long)EMACS_SECS (TV2), (unsigned long)EMACS_USECS (TV2));
f9356baa
RS
60 return time_string;
61}
62
79dbff47 63#if ! defined (HAVE_GETTIMEOFDAY) && defined (HAVE_TIMEVAL)
80b2cbf2 64
79dbff47
RS
65/* ARGSUSED */
66gettimeofday (tp, tzp)
67 struct timeval *tp;
68 struct timezone *tzp;
69{
70 extern long time ();
71
80b2cbf2 72 tp->tv_sec = time ((long *)0);
79dbff47
RS
73 tp->tv_usec = 0;
74 if (tzp != 0)
75 tzp->tz_minuteswest = -1;
76}
80b2cbf2 77
79dbff47
RS
78#endif
79\f
d0dff6e5 80int
873fbd0b 81main (void)
f9356baa 82{
1c1fce3f
KH
83 int c;
84 while ((c = getchar ()) != EOF)
f9356baa 85 {
1c1fce3f 86 switch (c)
f9356baa
RS
87 {
88 case 'z':
89 reset_watch ();
90 break;
91 case 'p':
92 puts (get_time ());
93 break;
94 case 'q':
65396510 95 exit (EXIT_SUCCESS);
f9356baa 96 }
1c1fce3f
KH
97 /* Anything remaining on the line is ignored. */
98 while (c != '\n' && c != EOF)
99 c = getchar ();
f9356baa 100 }
65396510 101 exit (EXIT_FAILURE);
f9356baa 102}
ab5796a9 103
65396510
TTN
104
105/* profile.c ends here */