2.9
[glibc/nacl-glibc.git] / sysdeps / posix / profil.c
blob281b53bf6a958ed88a46d0be1dcd5a765e8fd6a2
1 /* Low-level statistical profiling support function. Mostly POSIX.1 version.
2 Copyright (C) 1996,1997,1998,2002,2004,2005,2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <signal.h>
25 #include <sys/time.h>
26 #include <libc-internal.h>
28 #ifndef SIGPROF
30 #include <gmon/profil.c>
32 #else
34 static u_short *samples;
35 static size_t nsamples;
36 static size_t pc_offset;
37 static u_int pc_scale;
39 static inline void
40 profil_count (void *pc)
42 size_t i = (pc - pc_offset - (void *) 0) / 2;
44 if (sizeof (unsigned long long int) > sizeof (size_t))
45 i = (unsigned long long int) i * pc_scale / 65536;
46 else
47 i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536;
49 if (i < nsamples)
50 ++samples[i];
53 /* Get the machine-dependent definition of `profil_counter', the signal
54 handler for SIGPROF. It calls `profil_count' (above) with the PC of the
55 interrupted code. */
56 #include "profil-counter.h"
58 /* Enable statistical profiling, writing samples of the PC into at most
59 SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
60 is enabled, the system examines the user PC and increments
61 SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
62 disable profiling. Returns zero on success, -1 on error. */
64 int
65 __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
67 struct sigaction act;
68 struct itimerval timer;
69 #ifndef IS_IN_rtld
70 static struct sigaction oact;
71 static struct itimerval otimer;
72 # define oact_ptr &oact
73 # define otimer_ptr &otimer
75 if (sample_buffer == NULL)
77 /* Disable profiling. */
78 if (samples == NULL)
79 /* Wasn't turned on. */
80 return 0;
82 if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0)
83 return -1;
84 samples = NULL;
85 return __sigaction (SIGPROF, &oact, NULL);
88 if (samples)
90 /* Was already turned on. Restore old timer and signal handler
91 first. */
92 if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0
93 || __sigaction (SIGPROF, &oact, NULL) < 0)
94 return -1;
96 #else
97 /* In ld.so profiling should never be disabled once it runs. */
98 //assert (sample_buffer != NULL);
99 # define oact_ptr NULL
100 # define otimer_ptr NULL
101 #endif
103 samples = sample_buffer;
104 nsamples = size / sizeof *samples;
105 pc_offset = offset;
106 pc_scale = scale;
108 act.sa_handler = (sighandler_t) &profil_counter;
109 act.sa_flags = SA_RESTART;
110 __sigfillset (&act.sa_mask);
111 if (__sigaction (SIGPROF, &act, oact_ptr) < 0)
112 return -1;
114 timer.it_value.tv_sec = 0;
115 timer.it_value.tv_usec = 1000000 / __profile_frequency ();
116 timer.it_interval = timer.it_value;
117 return __setitimer (ITIMER_PROF, &timer, otimer_ptr);
119 weak_alias (__profil, profil)
121 #endif