1 /* profile.c --- generate periodic events for profiling of Emacs Lisp code.
2 Copyright (C) 1992, 1994 Free Software Foundation, Inc.
4 Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>
6 This file is part of SXEmacs.
8 SXEmacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 SXEmacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with: FSF 19.28. */
22 /* #### Not sure if this is needed for XEmacs. */
25 ** To be run as an emacs process. Input string that starts with:
26 ** 'z' -- resets the watch (to zero).
27 ** 'p' -- return time (on stdout) as string with format <sec>.<micro-sec>
30 ** abstraction : a stopwatch
31 ** operations: reset_watch, get_time
37 #include "../src/systime.h"
39 static struct timeval TV1, TV2;
40 static int watch_not_started = 1; /* flag */
41 static char time_string[64];
43 /* Reset the stopwatch to zero. */
45 static void reset_watch(void)
48 watch_not_started = 0;
51 /* This call returns the time since the last reset_watch call. The time
52 is returned as a string with the format <seconds>.<micro-seconds>
53 If reset_watch was not called yet, exit. */
55 static char *get_time(void)
58 if (watch_not_started)
59 exit(1); /* call reset_watch first ! */
61 if (TV1.tv_usec > TV2.tv_usec) {
62 TV2.tv_usec += 1000000;
65 sz = snprintf(time_string, sizeof(time_string), "%lu.%06lu",
66 (unsigned long)TV2.tv_sec - TV1.tv_sec,
67 (unsigned long)TV2.tv_usec - TV1.tv_usec);
68 assert(sz >= 0 && (size_t)sz < sizeof(time_string));
72 int main(int argc, char *argv[])
75 while ((c = getchar()) != EOF) {
88 /* Anything remaining on the line is ignored. */
89 while (c != '\n' && c != EOF)