Use C99-style 'extern inline' if available.
[bpt/emacs.git] / lib-src / profile.c
CommitLineData
f9356baa 1/* profile.c --- generate periodic events for profiling of Emacs Lisp code.
acaf905b 2 Copyright (C) 1992, 1994, 1999, 2001-2012 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/**
0d23c240 23 ** To be run as an emacs subprocess. Input string that starts with:
f9356baa
RS
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>
d35af63c 32
f162bcc3
PE
33#define SYSTIME_INLINE EXTERN_INLINE
34
d35af63c 35#include <inttypes.h>
4ee9629e 36#include <stdio.h>
d35af63c
PE
37
38#include <intprops.h>
f98d41f5 39#include <systime.h>
f9356baa 40
e9a9ae03 41static EMACS_TIME TV1;
f9356baa 42static int watch_not_started = 1; /* flag */
d35af63c
PE
43static char time_string[INT_STRLEN_BOUND (uintmax_t) + sizeof "."
44 + LOG10_EMACS_TIME_RESOLUTION];
f9356baa
RS
45
46/* Reset the stopwatch to zero. */
47
b23b5a5b 48static void
873fbd0b 49reset_watch (void)
f9356baa 50{
e9a9ae03 51 TV1 = current_emacs_time ();
f9356baa
RS
52 watch_not_started = 0;
53}
54
55/* This call returns the time since the last reset_watch call. The time
d35af63c 56 is returned as a string with the format <seconds>.<nanoseconds>
1c1fce3f 57 If reset_watch was not called yet, exit. */
f9356baa 58
b23b5a5b 59static char *
873fbd0b 60get_time (void)
f9356baa 61{
e9a9ae03
PE
62 EMACS_TIME TV2 = sub_emacs_time (current_emacs_time (), TV1);
63 uintmax_t s = EMACS_SECS (TV2);
64 int ns = EMACS_NSECS (TV2);
f9356baa 65 if (watch_not_started)
65396510 66 exit (EXIT_FAILURE); /* call reset_watch first ! */
d35af63c 67 sprintf (time_string, "%"PRIuMAX".%0*d", s, LOG10_EMACS_TIME_RESOLUTION, ns);
f9356baa
RS
68 return time_string;
69}
79dbff47 70\f
d0dff6e5 71int
873fbd0b 72main (void)
f9356baa 73{
1c1fce3f
KH
74 int c;
75 while ((c = getchar ()) != EOF)
f9356baa 76 {
1c1fce3f 77 switch (c)
f9356baa
RS
78 {
79 case 'z':
80 reset_watch ();
81 break;
82 case 'p':
83 puts (get_time ());
84 break;
85 case 'q':
65396510 86 exit (EXIT_SUCCESS);
f9356baa 87 }
1c1fce3f
KH
88 /* Anything remaining on the line is ignored. */
89 while (c != '\n' && c != EOF)
90 c = getchar ();
f9356baa 91 }
65396510 92 exit (EXIT_FAILURE);
f9356baa 93}
ab5796a9 94
65396510
TTN
95
96/* profile.c ends here */