1 /* SPDX-License-Identifier: (BSD-2-Clause AND BSD-3-Clause) */
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * This file is adapted from glibc' gmon/sys/gmon.h.
31  *-
32  * Copyright (c) 1982, 1986, 1992, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 4. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 /*
61  * See gmon_out.h for gmon.out format.
62  */
63 
64 #ifndef GMON_H
65 #define GMON_H
66 
67 #include <stdint.h>
68 #include <util.h>
69 
70 /* Exported by the TA linker script */
71 extern uint8_t __text_start[];
72 extern uint8_t __text_end[];
73 
74 void __mcount_internal(unsigned long frompc, unsigned long selfpc);
75 
76 
77 /*
78  * Histogram counters are unsigned shorts (according to the kernel).
79  */
80 #define	HISTCOUNTER	unsigned short
81 
82 /*
83  * Fraction of text space to allocate for histogram counters here, 1/2
84  */
85 #define	HISTFRACTION	2
86 
87 /*
88  * Fraction of text space to allocate for from hash buckets.
89  * The value of HASHFRACTION is based on the minimum number of bytes
90  * of separation between two subroutine call points in the object code.
91  * Given MIN_SUBR_SEPARATION bytes of separation the value of
92  * HASHFRACTION is calculated as:
93  *
94  *	HASHFRACTION = MIN_SUBR_SEPARATION / (2 * sizeof(short) - 1);
95  *
96  * For example, on the VAX, the shortest two call sequence is:
97  *
98  *	calls	$0,(r0)
99  *	calls	$0,(r0)
100  *
101  * which is separated by only three bytes, thus HASHFRACTION is
102  * calculated as:
103  *
104  *	HASHFRACTION = 3 / (2 * 2 - 1) = 1
105  *
106  * Note that the division above rounds down, thus if MIN_SUBR_FRACTION
107  * is less than three, this algorithm will not work!
108  *
109  * In practice, however, call instructions are rarely at a minimal
110  * distance.  Hence, we will define HASHFRACTION to be 2 across all
111  * architectures.  This saves a reasonable amount of space for
112  * profiling data structures without (in practice) sacrificing
113  * any granularity.
114  */
115 #define	HASHFRACTION	2
116 
117 /*
118  * Percent of text space to allocate for tostructs.
119  * This is a heuristic; we will fail with a warning when profiling programs
120  * with a very large number of very small functions, but that's
121  * normally OK.
122  * 2 is probably still a good value for normal programs.
123  * Profiling a test case with 64000 small functions will work if
124  * you raise this value to 3 and link statically (which bloats the
125  * text size, thus raising the number of arcs expected by the heuristic).
126  */
127 #define ARCDENSITY	3
128 
129 /*
130  * Always allocate at least this many tostructs.  This
131  * hides the inadequacy of the ARCDENSITY heuristic, at least
132  * for small programs.
133  */
134 #define MINARCS		50
135 
136 /*
137  * The type used to represent indices into gmonparam.tos[].
138  */
139 #define	ARCINDEX	unsigned long
140 
141 /*
142  * Maximum number of arcs we want to allow.
143  * Used to be max representable value of ARCINDEX minus 2, but now
144  * that ARCINDEX is a long, that's too large; we don't really want
145  * to allow a 48 gigabyte table.
146  * The old value of 1<<16 wasn't high enough in practice for large C++
147  * programs; will 1<<20 be adequate for long?  FIXME
148  */
149 #define MAXARCS		(1 << 20)
150 
151 struct tostruct {
152 	unsigned long selfpc;
153 	long count;
154 	ARCINDEX link;
155 };
156 
157 /*
158  * A raw arc, with pointers to the calling site and the called site and a
159  * count.
160  */
161 struct rawarc {
162 	unsigned long	raw_frompc;
163 	unsigned long	raw_selfpc;
164 	long		raw_count;
165 };
166 
167 /*
168  * The profiling data structures are housed in this structure.
169  */
170 struct gmonparam {
171 	long int	state;
172 	unsigned short	*kcount;
173 	unsigned long	kcountsize;
174 	ARCINDEX	*froms;
175 	unsigned long	fromssize;
176 	struct tostruct	*tos;
177 	unsigned long	tossize;
178 	unsigned long	tolimit;
179 	unsigned long	lowpc;
180 	unsigned long	highpc;
181 	unsigned long	textsize;
182 	unsigned long	hashfraction;
183 	long		log_hashfraction;
184 	/* */
185 	uint32_t	prof_rate; /* PC sampling frequency */
186 };
187 
188 /*
189  * Possible states of profiling.
190  */
191 #define	GMON_PROF_ON		0
192 #define	GMON_PROF_BUSY		1
193 #define	GMON_PROF_ERROR		2
194 #define	GMON_PROF_OFF		3
195 #define	GMON_PROF_OFF_EXITING	4
196 
197 #endif /* GMON_H */
198