profile.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* profile.c --- generate periodic events for profiling of Emacs Lisp code.
  2. Copyright (C) 1992, 1994, 1999 Free Software Foundation, Inc.
  3. Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>
  4. This file is part of GNU Emacs.
  5. GNU Emacs is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Emacs is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Emacs; see the file COPYING. If not, write to
  15. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. /**
  18. ** To be run as an emacs process. Input string that starts with:
  19. ** 'z' -- resets the watch (to zero).
  20. ** 'p' -- return time (on stdout) as string with format <sec>.<micro-sec>
  21. ** 'q' -- exit.
  22. **
  23. ** abstraction : a stopwatch
  24. ** operations: reset_watch, get_time
  25. */
  26. #include <config.h>
  27. #include <stdio.h>
  28. #include <systime.h>
  29. static EMACS_TIME TV1, TV2;
  30. static int watch_not_started = 1; /* flag */
  31. static char time_string[30];
  32. /* Reset the stopwatch to zero. */
  33. void
  34. reset_watch ()
  35. {
  36. EMACS_GET_TIME (TV1);
  37. watch_not_started = 0;
  38. }
  39. /* This call returns the time since the last reset_watch call. The time
  40. is returned as a string with the format <seconds>.<micro-seconds>
  41. If reset_watch was not called yet, exit. */
  42. char *
  43. get_time ()
  44. {
  45. if (watch_not_started)
  46. exit (1); /* call reset_watch first ! */
  47. EMACS_GET_TIME (TV2);
  48. EMACS_SUB_TIME (TV2, TV2, TV1);
  49. sprintf (time_string, "%lu.%06lu", (unsigned long)EMACS_SECS (TV2), (unsigned long)EMACS_USECS (TV2));
  50. return time_string;
  51. }
  52. #if ! defined (HAVE_GETTIMEOFDAY) && defined (HAVE_TIMEVAL)
  53. /* ARGSUSED */
  54. gettimeofday (tp, tzp)
  55. struct timeval *tp;
  56. struct timezone *tzp;
  57. {
  58. extern long time ();
  59. tp->tv_sec = time ((long *)0);
  60. tp->tv_usec = 0;
  61. if (tzp != 0)
  62. tzp->tz_minuteswest = -1;
  63. }
  64. #endif
  65. int
  66. main ()
  67. {
  68. int c;
  69. while ((c = getchar ()) != EOF)
  70. {
  71. switch (c)
  72. {
  73. case 'z':
  74. reset_watch ();
  75. break;
  76. case 'p':
  77. puts (get_time ());
  78. break;
  79. case 'q':
  80. exit (0);
  81. }
  82. /* Anything remaining on the line is ignored. */
  83. while (c != '\n' && c != EOF)
  84. c = getchar ();
  85. }
  86. exit (1);
  87. }