1 /* Profiler implementation.
3 Copyright (C) 2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28 /* True if sampling profiler is running. */
30 bool sample_profiler_running
;
32 /* True if memory profiler is running. */
34 bool memory_profiler_running
;
36 /* True during tracing. */
40 /* Tag for GC entry. */
44 static void sigprof_handler (int, siginfo_t
*, void *);
45 static void block_sigprof (void);
46 static void unblock_sigprof (void);
49 /* Pattern matching. */
53 pattern_exact
, /* foo */
54 pattern_body_exact
, /* *foo* */
55 pattern_pre_any
, /* *foo */
56 pattern_post_any
, /* foo* */
57 pattern_body_any
/* foo*bar */
62 enum pattern_type type
;
69 static struct pattern
*
70 parse_pattern (const char *pattern
)
72 int length
= strlen (pattern
);
73 enum pattern_type type
;
77 (struct pattern
*) xmalloc (sizeof (struct pattern
));
81 && pattern
[length
- 1] == '*')
83 type
= pattern_body_exact
;
84 exact
= xstrdup (pattern
+ 1);
85 exact
[length
- 2] = 0;
87 else if (*pattern
== '*')
89 type
= pattern_pre_any
;
90 exact
= xstrdup (pattern
+ 1);
92 else if (pattern
[length
- 1] == '*')
94 type
= pattern_post_any
;
95 exact
= xstrdup (pattern
);
96 exact
[length
- 1] = 0;
98 else if (strchr (pattern
, '*'))
100 type
= pattern_body_any
;
101 exact
= xstrdup (pattern
);
102 extra
= strchr (exact
, '*');
107 type
= pattern_exact
;
108 exact
= xstrdup (pattern
);
114 pat
->exact_length
= strlen (exact
);
115 pat
->extra_length
= extra
? strlen (extra
) : 0;
121 free_pattern (struct pattern
*pattern
)
123 xfree (pattern
->exact
);
128 pattern_match_1 (enum pattern_type type
,
134 if (exact_length
> length
)
139 return exact_length
== length
&& !strncmp (exact
, string
, length
);
140 case pattern_body_exact
:
141 return strstr (string
, exact
) != 0;
142 case pattern_pre_any
:
143 return !strncmp (exact
, string
+ (length
- exact_length
), exact_length
);
144 case pattern_post_any
:
145 return !strncmp (exact
, string
, exact_length
);
146 case pattern_body_any
:
152 pattern_match (struct pattern
*pattern
, const char *string
)
154 int length
= strlen (string
);
155 switch (pattern
->type
)
157 case pattern_body_any
:
158 if (pattern
->exact_length
+ pattern
->extra_length
> length
)
160 return pattern_match_1 (pattern_post_any
,
162 pattern
->exact_length
,
164 && pattern_match_1 (pattern_pre_any
,
166 pattern
->extra_length
,
169 return pattern_match_1 (pattern
->type
,
171 pattern
->exact_length
,
178 match (const char *pattern
, const char *string
)
181 struct pattern
*pat
= parse_pattern (pattern
);
182 res
= pattern_match (pat
, string
);
188 should_match (const char *pattern
, const char *string
)
190 putchar (match (pattern
, string
) ? '.' : 'F');
194 should_not_match (const char *pattern
, const char *string
)
196 putchar (match (pattern
, string
) ? 'F' : '.');
200 pattern_match_tests (void)
202 should_match ("", "");
203 should_not_match ("", "a");
204 should_match ("a", "a");
205 should_not_match ("a", "ab");
206 should_not_match ("ab", "a");
207 should_match ("*a*", "a");
208 should_match ("*a*", "ab");
209 should_match ("*a*", "ba");
210 should_match ("*a*", "bac");
211 should_not_match ("*a*", "");
212 should_not_match ("*a*", "b");
213 should_match ("*", "");
214 should_match ("*", "a");
215 should_match ("a*", "a");
216 should_match ("a*", "ab");
217 should_not_match ("a*", "");
218 should_not_match ("a*", "ba");
219 should_match ("*a", "a");
220 should_match ("*a", "ba");
221 should_not_match ("*a", "");
222 should_not_match ("*a", "ab");
223 should_match ("a*b", "ab");
224 should_match ("a*b", "acb");
225 should_match ("a*b", "aab");
226 should_match ("a*b", "abb");
227 should_not_match ("a*b", "");
228 should_not_match ("a*b", "");
229 should_not_match ("a*b", "abc");
237 static struct pattern
*filter_pattern
;
239 /* Set the current filter pattern. If PATTERN is null, unset the
240 current filter pattern instead. */
243 set_filter_pattern (const char *pattern
)
245 if (sample_profiler_running
)
250 free_pattern (filter_pattern
);
254 filter_pattern
= parse_pattern (pattern
);
256 if (sample_profiler_running
)
260 /* Return true if the current filter pattern is matched with FUNCTION.
261 FUNCTION should be a symbol or a subroutine, otherwise return
265 apply_filter_1 (Lisp_Object function
)
272 if (SYMBOLP (function
))
273 name
= SDATA (SYMBOL_NAME (function
));
274 else if (SUBRP (function
))
275 name
= XSUBR (function
)->symbol_name
;
279 return pattern_match (filter_pattern
, name
);
282 /* Return true if the current filter pattern is matched with at least
283 one entry in BACKLIST. */
286 apply_filter (struct backtrace
*backlist
)
290 if (apply_filter_1 (*backlist
->function
))
292 backlist
= backlist
->next
;
297 DEFUN ("profiler-set-filter-pattern",
298 Fprofiler_set_filter_pattern
, Sprofiler_set_filter_pattern
,
300 doc
: /* Set the current filter pattern. PATTERN can contain
301 one or two wildcards (*) as follows:
309 If PATTERN is nil or an empty string, then unset the current filter
311 (Lisp_Object pattern
)
314 || (STRINGP (pattern
) && !SREF (pattern
, 0)))
316 set_filter_pattern (0);
317 message ("Profiler filter pattern unset");
320 else if (!STRINGP (pattern
))
321 error ("Invalid type of profiler filter pattern");
323 set_filter_pattern (SDATA (pattern
));
333 make_backtrace (int size
)
335 return Fmake_vector (make_number (size
), Qnil
);
339 backtrace_hash (Lisp_Object backtrace
)
343 for (i
= 0; i
< ASIZE (backtrace
); i
++)
345 hash
= SXHASH_COMBINE (XUINT (AREF (backtrace
, i
)), hash
);
350 backtrace_equal (Lisp_Object a
, Lisp_Object b
)
354 for (i
= 0, j
= 0;; i
++, j
++)
356 Lisp_Object x
= i
< ASIZE (a
) ? AREF (a
, i
) : Qnil
;
357 Lisp_Object y
= j
< ASIZE (b
) ? AREF (b
, j
) : Qnil
;
358 if (NILP (x
) && NILP (y
))
368 backtrace_object_1 (Lisp_Object backtrace
, int i
)
370 if (i
>= ASIZE (backtrace
) || NILP (AREF (backtrace
, i
)))
373 return Fcons (AREF (backtrace
, i
), backtrace_object_1 (backtrace
, i
+ 1));
376 /* Convert BACKTRACE to a list. */
379 backtrace_object (Lisp_Object backtrace
)
381 backtrace_object_1 (backtrace
, 0);
387 /* Slot data structure. */
391 /* Point to next free slot or next hash table link. */
393 /* Point to previous hash table link. */
395 /* Backtrace object with fixed size. */
396 Lisp_Object backtrace
;
397 /* How many times a profiler sees the slot, or how much resouce
398 allocated during profiling. */
400 /* How long the slot takes to execute. */
403 unsigned char used
: 1;
407 mark_slot (struct slot
*slot
)
409 mark_object (slot
->backtrace
);
412 /* Convert SLOT to a list. */
415 slot_object (struct slot
*slot
)
417 return list3 (backtrace_object (slot
->backtrace
),
418 make_number (slot
->count
),
419 make_number (slot
->elapsed
));
428 /* Number of slots allocated to the heap. */
430 /* Actual data area. */
433 struct slot
*free_list
;
437 clear_slot_heap (struct slot_heap
*heap
)
441 struct slot
*free_list
;
445 /* Mark all slots unsused. */
446 for (i
= 0; i
< heap
->size
; i
++)
449 /* Rebuild a free list. */
450 free_list
= heap
->free_list
= heap
->data
;
451 for (i
= 1; i
< heap
->size
; i
++)
453 free_list
->next
= &data
[i
];
454 free_list
= free_list
->next
;
459 /* Make a slot heap with SIZE. MAX_STACK_DEPTH is a fixed size of
462 static struct slot_heap
*
463 make_slot_heap (unsigned int size
, int max_stack_depth
)
466 struct slot_heap
*heap
;
469 data
= (struct slot
*) xmalloc (sizeof (struct slot
) * size
);
470 for (i
= 0; i
< size
; i
++)
471 data
[i
].backtrace
= make_backtrace (max_stack_depth
);
473 heap
= (struct slot_heap
*) xmalloc (sizeof (struct slot_heap
));
476 clear_slot_heap (heap
);
482 free_slot_heap (struct slot_heap
*heap
)
485 struct slot
*data
= heap
->data
;
486 for (i
= 0; i
< heap
->size
; i
++)
487 data
[i
].backtrace
= Qnil
;
493 mark_slot_heap (struct slot_heap
*heap
)
496 for (i
= 0; i
< heap
->size
; i
++)
497 mark_slot (&heap
->data
[i
]);
500 /* Allocate one slot from HEAP. Return 0 if no free slot in HEAP. */
503 allocate_slot (struct slot_heap
*heap
)
506 if (!heap
->free_list
)
508 slot
= heap
->free_list
;
512 heap
->free_list
= heap
->free_list
->next
;
517 free_slot (struct slot_heap
*heap
, struct slot
*slot
)
519 eassert (slot
->used
);
521 slot
->next
= heap
->free_list
;
522 heap
->free_list
= slot
;
525 /* Return a minimal slot from HEAP. "Minimal" means that such a slot
526 is meaningless for profiling. */
529 min_slot (struct slot_heap
*heap
)
532 struct slot
*min
= 0;
533 for (i
= 0; i
< heap
->size
; i
++)
535 struct slot
*slot
= &heap
->data
[i
];
536 if (!min
|| (slot
->used
&& slot
->count
< min
->count
))
543 /* Slot hash tables. */
547 /* Number of slot buckets. */
549 /* Buckets data area. */
554 clear_slot_table (struct slot_table
*table
)
557 for (i
= 0; i
< table
->size
; i
++)
561 static struct slot_table
*
562 make_slot_table (int size
)
564 struct slot_table
*table
565 = (struct slot_table
*) xmalloc (sizeof (struct slot_table
));
567 table
->data
= (struct slot
**) xmalloc (sizeof (struct slot
*) * size
);
568 clear_slot_table (table
);
573 free_slot_table (struct slot_table
*table
)
580 remove_slot (struct slot_table
*table
, struct slot
*slot
)
583 slot
->prev
->next
= slot
->next
;
586 EMACS_UINT hash
= backtrace_hash (slot
->backtrace
);
587 table
->data
[hash
% table
->size
] = slot
->next
;
590 slot
->next
->prev
= slot
->prev
;
598 /* Type of log in symbol. `sample' or `memory'. */
600 /* Backtrace for working. */
601 Lisp_Object backtrace
;
602 struct slot_heap
*slot_heap
;
603 struct slot_table
*slot_table
;
605 size_t others_elapsed
;
609 make_log (const char *type
, int heap_size
, int max_stack_depth
)
612 (struct log
*) xmalloc (sizeof (struct log
));
613 log
->type
= intern (type
);
614 log
->backtrace
= make_backtrace (max_stack_depth
);
615 log
->slot_heap
= make_slot_heap (heap_size
, max_stack_depth
);
616 /* Number of buckets of hash table will be 10% of HEAP_SIZE. */
617 log
->slot_table
= make_slot_table (max (256, heap_size
) / 10);
618 log
->others_count
= 0;
619 log
->others_elapsed
= 0;
624 free_log (struct log
*log
)
626 log
->backtrace
= Qnil
;
627 free_slot_heap (log
->slot_heap
);
628 free_slot_table (log
->slot_table
);
632 mark_log (struct log
*log
)
634 mark_object (log
->type
);
635 mark_object (log
->backtrace
);
636 mark_slot_heap (log
->slot_heap
);
640 clear_log (struct log
*log
)
642 clear_slot_heap (log
->slot_heap
);
643 clear_slot_table (log
->slot_table
);
644 log
->others_count
= 0;
645 log
->others_elapsed
= 0;
648 /* Evint SLOT from LOG and accumulate the slot counts into others
652 evict_slot (struct log
*log
, struct slot
*slot
)
654 log
->others_count
+= slot
->count
;
655 log
->others_elapsed
+= slot
->elapsed
;
656 remove_slot (log
->slot_table
, slot
);
657 free_slot (log
->slot_heap
, slot
);
660 /* Evict a minimal slot from LOG. */
663 evict_min_slot (struct log
*log
)
665 struct slot
*min
= min_slot (log
->slot_heap
);
667 evict_slot (log
, min
);
670 /* Allocate a new slot for BACKTRACE from LOG. The returen value must
671 be a valid pointer to the slot. */
674 new_slot (struct log
*log
, Lisp_Object backtrace
)
677 struct slot
*slot
= allocate_slot (log
->slot_heap
);
679 /* If failed to allocate a slot, free some slots to make a room in
683 evict_min_slot (log
);
684 slot
= allocate_slot (log
->slot_heap
);
685 /* Must be allocated. */
692 /* Assign BACKTRACE to the slot. */
693 for (i
= 0; i
< ASIZE (backtrace
); i
++)
694 ASET (slot
->backtrace
, i
, AREF (backtrace
, i
));
699 /* Make sure that a slot for BACKTRACE is in LOG and return the
700 slot. The return value must be a valid pointer to the slot. */
703 ensure_slot (struct log
*log
, Lisp_Object backtrace
)
705 EMACS_UINT hash
= backtrace_hash (backtrace
);
706 int index
= hash
% log
->slot_table
->size
;
707 struct slot
*slot
= log
->slot_table
->data
[index
];
708 struct slot
*prev
= slot
;
710 /* Looking up in hash table bucket. */
713 if (backtrace_equal (backtrace
, slot
->backtrace
))
719 /* If not found, allocate a new slot for BACKTRACE from LOG and link
720 it with bucket chain. */
721 slot
= new_slot (log
, backtrace
);
728 log
->slot_table
->data
[index
] = slot
;
734 /* Record the current backtrace in LOG. BASE is a special name for
735 describing which the backtrace come from. BASE can be nil. COUNT is
736 a number how many times the profiler sees the backtrace at the
737 time. ELAPSED is a elapsed time in millisecond that the backtrace
741 record_backtrace_under (struct log
*log
, Lisp_Object base
,
742 size_t count
, size_t elapsed
)
745 Lisp_Object backtrace
= log
->backtrace
;
746 struct backtrace
*backlist
= backtrace_list
;
748 /* First of all, apply filter on the bactkrace. */
749 if (!apply_filter (backlist
)) return;
751 /* Record BASE if necessary. */
752 if (!NILP (base
) && ASIZE (backtrace
) > 0)
753 ASET (backtrace
, i
++, base
);
755 /* Copy the backtrace contents into working memory. */
756 for (; i
< ASIZE (backtrace
) && backlist
; backlist
= backlist
->next
)
758 Lisp_Object function
= *backlist
->function
;
759 if (FUNCTIONP (function
))
760 ASET (backtrace
, i
++, function
);
762 /* Make sure that unused space of working memory is filled with
764 for (; i
< ASIZE (backtrace
); i
++)
765 ASET (backtrace
, i
, Qnil
);
767 /* If the backtrace is not empty, */
768 if (!NILP (AREF (backtrace
, 0)))
770 /* then record counts. */
771 struct slot
*slot
= ensure_slot (log
, backtrace
);
772 slot
->count
+= count
;
773 slot
->elapsed
+= elapsed
;
778 record_backtrace (struct log
*log
, size_t count
, size_t elapsed
)
780 record_backtrace_under (log
, Qnil
, count
, elapsed
);
783 /* Convert LOG to a list. */
786 log_object (struct log
*log
)
789 Lisp_Object slots
= Qnil
;
791 if (log
->others_count
!= 0 || log
->others_elapsed
!= 0)
793 /* Add others slot. */
794 Lisp_Object others_slot
796 make_number (log
->others_count
),
797 make_number (log
->others_elapsed
));
798 slots
= list1 (others_slot
);
801 for (i
= 0; i
< log
->slot_heap
->size
; i
++)
803 struct slot
*s
= &log
->slot_heap
->data
[i
];
806 Lisp_Object slot
= slot_object (s
);
807 slots
= Fcons (slot
, slots
);
811 return list4 (log
->type
, Qnil
, Fcurrent_time (), slots
);
815 /* Sample profiler. */
817 static struct log
*sample_log
;
819 /* The current sample interval in millisecond. */
821 static int current_sample_interval
;
823 DEFUN ("sample-profiler-start", Fsample_profiler_start
, Ssample_profiler_start
,
825 doc
: /* Start or restart sample profiler. Sample profiler will
826 take samples each SAMPLE-INTERVAL in millisecond. See also
827 `profiler-slot-heap-size' and `profiler-max-stack-depth'. */)
828 (Lisp_Object sample_interval
)
831 struct itimerval timer
;
833 if (sample_profiler_running
)
834 error ("Sample profiler is already running");
837 sample_log
= make_log ("sample",
838 profiler_slot_heap_size
,
839 profiler_max_stack_depth
);
841 current_sample_interval
= XINT (sample_interval
);
843 sa
.sa_sigaction
= sigprof_handler
;
844 sa
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
845 sigemptyset (&sa
.sa_mask
);
846 sigaction (SIGPROF
, &sa
, 0);
848 timer
.it_interval
.tv_sec
= 0;
849 timer
.it_interval
.tv_usec
= current_sample_interval
* 1000;
850 timer
.it_value
= timer
.it_interval
;
851 setitimer (ITIMER_PROF
, &timer
, 0);
853 sample_profiler_running
= 1;
858 DEFUN ("sample-profiler-stop", Fsample_profiler_stop
, Ssample_profiler_stop
,
860 doc
: /* Stop sample profiler. Profiler log will be kept. */)
863 if (!sample_profiler_running
)
864 error ("Sample profiler is not running");
865 sample_profiler_running
= 0;
867 setitimer (ITIMER_PROF
, 0, 0);
872 DEFUN ("sample-profiler-reset", Fsample_profiler_reset
, Ssample_profiler_reset
,
874 doc
: /* Clear sample profiler log. */)
879 if (sample_profiler_running
)
882 clear_log (sample_log
);
887 free_log (sample_log
);
893 DEFUN ("sample-profiler-running-p",
894 Fsample_profiler_running_p
, Ssample_profiler_running_p
,
896 doc
: /* Return t if sample profiler is running. */)
899 return sample_profiler_running
? Qt
: Qnil
;
902 DEFUN ("sample-profiler-log",
903 Fsample_profiler_log
, Ssample_profiler_log
,
905 doc
: /* Return sample profiler log. The data is a list of
906 (sample nil TIMESTAMP SLOTS), where TIMESTAMP is a timestamp when the
907 log is collected and SLOTS is a list of slots. */)
911 Lisp_Object result
= Qnil
;
915 if (sample_profiler_running
)
918 result
= log_object (sample_log
);
922 result
= log_object (sample_log
);
929 /* Memory profiler. */
931 static struct log
*memory_log
;
933 DEFUN ("memory-profiler-start", Fmemory_profiler_start
, Smemory_profiler_start
,
935 doc
: /* Start/restart memory profiler. See also
936 `profiler-slot-heap-size' and `profiler-max-stack-depth'. */)
939 if (memory_profiler_running
)
940 error ("Memory profiler is already running");
943 memory_log
= make_log ("memory",
944 profiler_slot_heap_size
,
945 profiler_max_stack_depth
);
947 memory_profiler_running
= 1;
952 DEFUN ("memory-profiler-stop",
953 Fmemory_profiler_stop
, Smemory_profiler_stop
,
955 doc
: /* Stop memory profiler. Profiler log will be kept. */)
958 if (!memory_profiler_running
)
959 error ("Memory profiler is not running");
960 memory_profiler_running
= 0;
965 DEFUN ("memory-profiler-reset",
966 Fmemory_profiler_reset
, Smemory_profiler_reset
,
968 doc
: /* Clear memory profiler log. */)
973 if (memory_profiler_running
)
974 clear_log (memory_log
);
977 free_log (memory_log
);
983 DEFUN ("memory-profiler-running-p",
984 Fmemory_profiler_running_p
, Smemory_profiler_running_p
,
986 doc
: /* Return t if memory profiler is running. */)
989 return memory_profiler_running
? Qt
: Qnil
;
992 DEFUN ("memory-profiler-log",
993 Fmemory_profiler_log
, Smemory_profiler_log
,
995 doc
: /* Return memory profiler log. The data is a list of
996 (memory nil TIMESTAMP SLOTS), where TIMESTAMP is a timestamp when the
997 log is collected and SLOTS is a list of slots. */)
1000 Lisp_Object result
= Qnil
;
1003 result
= log_object (memory_log
);
1009 /* Signals and probes. */
1011 /* Signal handler for sample profiler. */
1014 sigprof_handler (int signal
, siginfo_t
*info
, void *ctx
)
1016 if (!is_in_trace
&& sample_log
)
1017 record_backtrace (sample_log
, 1, current_sample_interval
);
1021 block_sigprof (void)
1024 sigemptyset (&sigset
);
1025 sigaddset (&sigset
, SIGPROF
);
1026 sigprocmask (SIG_BLOCK
, &sigset
, 0);
1030 unblock_sigprof (void)
1033 sigemptyset (&sigset
);
1034 sigaddset (&sigset
, SIGPROF
);
1035 sigprocmask (SIG_UNBLOCK
, &sigset
, 0);
1038 /* Record that the current backtrace allocated SIZE bytes. */
1041 malloc_probe (size_t size
)
1044 record_backtrace (memory_log
, size
, 0);
1047 /* Record that GC happened in the current backtrace. */
1050 gc_probe (size_t size
, size_t elapsed
)
1053 record_backtrace_under (sample_log
, Qgc
, 1, elapsed
);
1055 record_backtrace_under (memory_log
, Qgc
, size
, elapsed
);
1061 mark_profiler (void)
1065 if (sample_profiler_running
)
1068 mark_log (sample_log
);
1072 mark_log (sample_log
);
1075 mark_log (memory_log
);
1079 syms_of_profiler (void)
1083 DEFVAR_INT ("profiler-max-stack-depth", profiler_max_stack_depth
,
1085 profiler_max_stack_depth
= 16;
1086 DEFVAR_INT ("profiler-slot-heap-size", profiler_slot_heap_size
,
1088 profiler_slot_heap_size
= 10000;
1090 defsubr (&Sprofiler_set_filter_pattern
);
1092 defsubr (&Ssample_profiler_start
);
1093 defsubr (&Ssample_profiler_stop
);
1094 defsubr (&Ssample_profiler_reset
);
1095 defsubr (&Ssample_profiler_running_p
);
1096 defsubr (&Ssample_profiler_log
);
1098 defsubr (&Smemory_profiler_start
);
1099 defsubr (&Smemory_profiler_stop
);
1100 defsubr (&Smemory_profiler_reset
);
1101 defsubr (&Smemory_profiler_running_p
);
1102 defsubr (&Smemory_profiler_log
);