profile.c 2.7 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
/* profile.c --- generate periodic events for profiling of Emacs Lisp code.
2
   Copyright (C) 1992, 1994, 1999, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
3
                 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6

 Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>

7 8 9 10
This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 3, or (at your option)
12 13 14 15 16 17 18 19 20
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
21 22
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
23 24 25 26 27 28 29 30 31 32 33


/**
 **  To be run as an emacs process. Input string that starts with:
 **    'z' -- resets the watch (to zero).
 **    'p' -- return time (on stdout) as string with format <sec>.<micro-sec>
 **    'q' -- exit.
 **
 **  abstraction : a stopwatch
 **  operations: reset_watch, get_time
 */
34
#include <config.h>
35
#include <stdio.h>
36
#include <systime.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
37

38
static EMACS_TIME TV1, TV2;
Richard M. Stallman's avatar
Richard M. Stallman committed
39 40 41 42 43
static int watch_not_started = 1; /* flag */
static char time_string[30];

/* Reset the stopwatch to zero.  */

44
void
Richard M. Stallman's avatar
Richard M. Stallman committed
45 46
reset_watch ()
{
47
  EMACS_GET_TIME (TV1);
Richard M. Stallman's avatar
Richard M. Stallman committed
48 49 50 51
  watch_not_started = 0;
}

/* 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.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
54 55 56 57 58

char *
get_time ()
{
  if (watch_not_started)
59
    exit (EXIT_FAILURE);  /* call reset_watch first ! */
60
  EMACS_GET_TIME (TV2);
61
  EMACS_SUB_TIME (TV2, TV2, TV1);
62
  sprintf (time_string, "%lu.%06lu", (unsigned long)EMACS_SECS (TV2), (unsigned long)EMACS_USECS (TV2));
Richard M. Stallman's avatar
Richard M. Stallman committed
63 64 65
  return time_string;
}

66
#if ! defined (HAVE_GETTIMEOFDAY) && defined (HAVE_TIMEVAL)
Pavel Janík's avatar
Pavel Janík committed
67

68 69 70 71 72 73 74
/* ARGSUSED */
gettimeofday (tp, tzp)
     struct timeval *tp;
     struct timezone *tzp;
{
  extern long time ();

Pavel Janík's avatar
Pavel Janík committed
75
  tp->tv_sec = time ((long *)0);
76 77 78 79
  tp->tv_usec = 0;
  if (tzp != 0)
    tzp->tz_minuteswest = -1;
}
Pavel Janík's avatar
Pavel Janík committed
80

81 82
#endif

Karl Heuer's avatar
Karl Heuer committed
83
int
Richard M. Stallman's avatar
Richard M. Stallman committed
84 85
main ()
{
86 87
  int c;
  while ((c = getchar ()) != EOF)
Richard M. Stallman's avatar
Richard M. Stallman committed
88
    {
89
      switch (c)
Richard M. Stallman's avatar
Richard M. Stallman committed
90 91 92 93 94 95 96 97
	{
	case 'z':
	  reset_watch ();
	  break;
	case 'p':
	  puts (get_time ());
	  break;
	case 'q':
98
	  exit (EXIT_SUCCESS);
Richard M. Stallman's avatar
Richard M. Stallman committed
99
	}
100 101 102
      /* Anything remaining on the line is ignored.  */
      while (c != '\n' && c != EOF)
	c = getchar ();
Richard M. Stallman's avatar
Richard M. Stallman committed
103
    }
104
  exit (EXIT_FAILURE);
Richard M. Stallman's avatar
Richard M. Stallman committed
105
}
Miles Bader's avatar
Miles Bader committed
106 107 108

/* arch-tag: 8db68f7e-2322-4944-a315-dba349bdbf39
   (do not change this comment) */
109 110

/* profile.c ends here */