]>
Commit | Line | Data |
---|---|---|
e9607dbe | 1 | /* Low-level statistical profiling support function. Mostly POSIX.1 version. |
de7e6366 | 2 | Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. |
478b92f0 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library 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 GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
e9607dbe RM |
19 | |
20 | #include <sys/types.h> | |
21 | #include <unistd.h> | |
22 | #include <errno.h> | |
23 | #include <signal.h> | |
24 | #include <sys/time.h> | |
25 | ||
26 | #ifndef SIGPROF | |
27 | ||
3d42e04d | 28 | #include <sysdeps/generic/profil.c> |
e9607dbe RM |
29 | |
30 | #else | |
31 | ||
32 | static u_short *samples; | |
33 | static size_t nsamples; | |
34 | static size_t pc_offset; | |
35 | static u_int pc_scale; | |
36 | ||
37 | static inline void | |
38 | profil_count (void *pc) | |
39 | { | |
b236e99d UD |
40 | size_t i = (pc - pc_offset - (void *) 0) / 2; |
41 | ||
42 | if (sizeof (unsigned long long int) > sizeof (size_t)) | |
43 | i = (unsigned long long int) i * pc_scale / 65536; | |
44 | else | |
45 | i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536; | |
46 | ||
e9607dbe RM |
47 | if (i < nsamples) |
48 | ++samples[i]; | |
49 | } | |
50 | ||
51 | /* Get the machine-dependent definition of `profil_counter', the signal | |
52 | handler for SIGPROF. It calls `profil_count' (above) with the PC of the | |
53 | interrupted code. */ | |
54 | #include "profil-counter.h" | |
55 | ||
56 | /* Enable statistical profiling, writing samples of the PC into at most | |
57 | SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling | |
58 | is enabled, the system examines the user PC and increments | |
59 | SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero, | |
60 | disable profiling. Returns zero on success, -1 on error. */ | |
61 | ||
62 | int | |
9a0a462c | 63 | __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale) |
e9607dbe | 64 | { |
de7e6366 UD |
65 | static struct sigaction oact; |
66 | static struct itimerval otimer; | |
67 | struct sigaction act; | |
68 | struct itimerval timer; | |
e9607dbe RM |
69 | |
70 | if (sample_buffer == NULL) | |
71 | { | |
72 | /* Disable profiling. */ | |
73 | if (samples == NULL) | |
74 | /* Wasn't turned on. */ | |
75 | return 0; | |
e9607dbe | 76 | |
50304ef0 | 77 | if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0) |
e9607dbe | 78 | return -1; |
b236e99d | 79 | samples = NULL; |
c0b50509 | 80 | return __sigaction (SIGPROF, &oact, NULL); |
e9607dbe RM |
81 | } |
82 | ||
57ba7bb4 UD |
83 | if (samples) |
84 | { | |
85 | /* Was already turned on. Restore old timer and signal handler | |
86 | first. */ | |
50304ef0 | 87 | if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0 |
c0b50509 | 88 | || __sigaction (SIGPROF, &oact, NULL) < 0) |
57ba7bb4 UD |
89 | return -1; |
90 | } | |
91 | ||
e9607dbe RM |
92 | samples = sample_buffer; |
93 | nsamples = size / sizeof *samples; | |
94 | pc_offset = offset; | |
95 | pc_scale = scale; | |
96 | ||
97 | act.sa_handler = (sighandler_t) &profil_counter; | |
98 | act.sa_flags = SA_RESTART; | |
99 | sigfillset (&act.sa_mask); | |
c0b50509 | 100 | if (__sigaction (SIGPROF, &act, &oact) < 0) |
e9607dbe RM |
101 | return -1; |
102 | ||
103 | timer.it_value.tv_sec = 0; | |
104 | timer.it_value.tv_usec = 1; | |
105 | timer.it_interval = timer.it_value; | |
50304ef0 | 106 | return __setitimer (ITIMER_PROF, &timer, &otimer); |
e9607dbe | 107 | } |
9a0a462c | 108 | weak_alias (__profil, profil) |
e9607dbe RM |
109 | |
110 | #endif |