profile.c 2.6 KB

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