profile.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* profile.c --- generate periodic events for profiling of Emacs Lisp code.
  2. Copyright (C) 1992, 1994, 1999, 2001-2012 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 3 of the License, or
  8. (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
  15. /**
  16. ** To be run as an emacs process. Input string that starts with:
  17. ** 'z' -- resets the watch (to zero).
  18. ** 'p' -- return time (on stdout) as string with format <sec>.<micro-sec>
  19. ** 'q' -- exit.
  20. **
  21. ** abstraction : a stopwatch
  22. ** operations: reset_watch, get_time
  23. */
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <systime.h>
  27. static EMACS_TIME TV1, TV2;
  28. static int watch_not_started = 1; /* flag */
  29. static char time_string[30];
  30. /* Reset the stopwatch to zero. */
  31. static void
  32. reset_watch (void)
  33. {
  34. EMACS_GET_TIME (TV1);
  35. watch_not_started = 0;
  36. }
  37. /* This call returns the time since the last reset_watch call. The time
  38. is returned as a string with the format <seconds>.<micro-seconds>
  39. If reset_watch was not called yet, exit. */
  40. static char *
  41. get_time (void)
  42. {
  43. if (watch_not_started)
  44. exit (EXIT_FAILURE); /* call reset_watch first ! */
  45. EMACS_GET_TIME (TV2);
  46. EMACS_SUB_TIME (TV2, TV2, TV1);
  47. sprintf (time_string, "%lu.%06lu", (unsigned long)EMACS_SECS (TV2), (unsigned long)EMACS_USECS (TV2));
  48. return time_string;
  49. }
  50. #if ! defined (HAVE_GETTIMEOFDAY) && defined (HAVE_TIMEVAL)
  51. /* ARGSUSED */
  52. gettimeofday (tp, tzp)
  53. struct timeval *tp;
  54. struct timezone *tzp;
  55. {
  56. extern long time ();
  57. tp->tv_sec = time ((long *)0);
  58. tp->tv_usec = 0;
  59. if (tzp != 0)
  60. tzp->tz_minuteswest = -1;
  61. }
  62. #endif
  63. int
  64. main (void)
  65. {
  66. int c;
  67. while ((c = getchar ()) != EOF)
  68. {
  69. switch (c)
  70. {
  71. case 'z':
  72. reset_watch ();
  73. break;
  74. case 'p':
  75. puts (get_time ());
  76. break;
  77. case 'q':
  78. exit (EXIT_SUCCESS);
  79. }
  80. /* Anything remaining on the line is ignored. */
  81. while (c != '\n' && c != EOF)
  82. c = getchar ();
  83. }
  84. exit (EXIT_FAILURE);
  85. }
  86. /* profile.c ends here */