1292
+ − 1 /* Profiling.
3025
+ − 2 Copyright (C) 2003, 2005 Ben Wing.
1292
+ − 3
+ − 4 This file is part of XEmacs.
+ − 5
+ − 6 XEmacs is free software; you can redistribute it and/or modify it
+ − 7 under the terms of the GNU General Public License as published by the
+ − 8 Free Software Foundation; either version 2, or (at your option) any
+ − 9 later version.
+ − 10
+ − 11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
+ − 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ − 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ − 14 for more details.
+ − 15
+ − 16 You should have received a copy of the GNU General Public License
+ − 17 along with XEmacs; see the file COPYING. If not, write to
+ − 18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ − 19 Boston, MA 02111-1307, USA. */
+ − 20
+ − 21 /* Synched up with: Not in FSF. */
+ − 22
+ − 23 /* Authorship:
+ − 24
+ − 25 Ben Wing: Feb 2003.
+ − 26 */
+ − 27
+ − 28 #include "backtrace.h"
+ − 29
+ − 30 void mark_profiling_info (void);
+ − 31 void profile_record_unwind (struct backtrace *);
+ − 32 void profile_record_about_to_call (struct backtrace *);
+ − 33 void profile_record_just_called (struct backtrace *);
+ − 34 void profile_record_consing (EMACS_INT size);
+ − 35 void profile_record_unconsing (EMACS_INT size);
+ − 36 extern int profiling_active;
+ − 37
+ − 38 /* We call about_to_call() and just_called() depending on the current
+ − 39 *dynamic* value of profiling_active (which could change as a result of
+ − 40 calling the function) but if we push a backtrace, we must pop it later,
+ − 41 so we need to remember the status of this. */
+ − 42 #define PROFILE_DECLARE() \
+ − 43 int do_backtrace = profiling_active || backtrace_with_internal_sections; \
+ − 44 struct backtrace backtrace
+ − 45
+ − 46 /* As just mentioned, we rely on the dynamic value of profiling_active.
+ − 47 This ensures correct behavior (e.g. we never modify the profiling info
+ − 48 when profiling is not active) because we seed and reap all functions
+ − 49 currently on the stack when starting and stopping. See
4162
+ − 50 `start-profiling'.
+ − 51
+ − 52 We check do_backtrace to make sure that the backtrace structure is
+ − 53 initialised. If it isn't, we can enter a function with profiling turned
+ − 54 off, and exit it with it turned on, with the consequence that an
+ − 55 unitialised backtrace structure is passed to
+ − 56 profile_record_just_called. Since do_backtrace is function-local (apart
+ − 57 from in the garbage collector) this avoids that. */
1292
+ − 58 #define PROFILE_ENTER_FUNCTION() \
+ − 59 do \
+ − 60 { \
4162
+ − 61 if (profiling_active && do_backtrace) \
1292
+ − 62 profile_record_about_to_call (&backtrace); \
+ − 63 } \
+ − 64 while (0)
+ − 65
+ − 66 #define PROFILE_EXIT_FUNCTION() \
+ − 67 do \
+ − 68 { \
4162
+ − 69 if (profiling_active && do_backtrace) \
1292
+ − 70 profile_record_just_called (&backtrace); \
+ − 71 } \
+ − 72 while (0)
+ − 73
+ − 74 /* We are entering a section that we would like to record profile information
+ − 75 about. We put this information into the backtrace list, just like
+ − 76 normal functions do. That is one easy way to make sure that we always
+ − 77 record info on the innermost section or function, whether section or
+ − 78 function. (To do this, we always need some sort of collusion between
+ − 79 profile and eval; this is one way.) */
+ − 80
3025
+ − 81 /* Or, we could call xzero() to zero the whole thing, and avoid four
+ − 82 of the statements below; or we could create a global backtrace object,
+ − 83 uninitialized (i.e. it will be initialized to all 0), and do structure
+ − 84 copy to initialize. It's not clear it will make much difference here,
+ − 85 but someone who really cared about counting cycles could implement it. */
1292
+ − 86 #define PROFILE_RECORD_ENTERING_SECTION(var) \
+ − 87 do \
+ − 88 { \
+ − 89 if (do_backtrace) \
+ − 90 { \
+ − 91 backtrace.function = &var; \
+ − 92 backtrace.args = NULL; \
+ − 93 backtrace.nargs = UNEVALLED; \
+ − 94 backtrace.evalargs = 0; \
+ − 95 backtrace.pdlcount = specpdl_depth (); \
+ − 96 backtrace.debug_on_exit = 0; \
+ − 97 backtrace.function_being_called = 0; \
+ − 98 PUSH_BACKTRACE (backtrace); \
+ − 99 } \
+ − 100 PROFILE_ENTER_FUNCTION (); \
+ − 101 } while (0)
+ − 102
+ − 103 #define PROFILE_RECORD_EXITING_SECTION(var) \
+ − 104 do \
+ − 105 { \
+ − 106 PROFILE_EXIT_FUNCTION (); \
+ − 107 if (do_backtrace) \
+ − 108 POP_BACKTRACE (backtrace); \
+ − 109 } while (0)
2514
+ − 110
+ − 111 #define RETURN_EXIT_PROFILING(tag, type, expr) \
+ − 112 do \
+ − 113 { \
+ − 114 type _ret_exitpr_ = (expr); \
+ − 115 PROFILE_RECORD_EXITING_SECTION (tag); \
+ − 116 RETURN_SANS_WARNINGS _ret_exitpr_; \
+ − 117 } while (0)
+ − 118
+ − 119 #define RETURN_LISP_EXIT_PROFILING(tag, expr) \
+ − 120 RETURN_EXIT_PROFILING (tag, Lisp_Object, expr)
+ − 121
+ − 122 #define RETURN_UNGCPRO_EXIT_PROFILING(tag, expr) \
3282
+ − 123 do \
2514
+ − 124 { \
+ − 125 Lisp_Object ret_ungc_val = (expr); \
+ − 126 UNGCPRO; \
+ − 127 PROFILE_RECORD_EXITING_SECTION (tag); \
+ − 128 RETURN_SANS_WARNINGS ret_ungc_val; \
+ − 129 } while (0)
+ − 130
+ − 131 #ifdef DEBUG_XEMACS
+ − 132 extern Lisp_Object QSin_temp_spot_1;
+ − 133 extern Lisp_Object QSin_temp_spot_2;
+ − 134 extern Lisp_Object QSin_temp_spot_3;
+ − 135 extern Lisp_Object QSin_temp_spot_4;
+ − 136 extern Lisp_Object QSin_temp_spot_5;
+ − 137 #endif /* DEBUG_XEMACS */