annotate gcc/ada/libgnat/s-stausa.ads @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S Y S T E M - S T A C K _ U S A G E --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2004-2017, Free Software Foundation, Inc. --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNARL is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNARL was developed by the GNARL team at Florida State University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 with System;
kono
parents:
diff changeset
33 with System.Storage_Elements;
kono
parents:
diff changeset
34 with System.Address_To_Access_Conversions;
kono
parents:
diff changeset
35 with Interfaces;
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 package System.Stack_Usage is
kono
parents:
diff changeset
38 pragma Preelaborate;
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 package SSE renames System.Storage_Elements;
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 subtype Stack_Address is SSE.Integer_Address;
kono
parents:
diff changeset
43 -- Address on the stack
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 function To_Stack_Address
kono
parents:
diff changeset
46 (Value : System.Address) return Stack_Address
kono
parents:
diff changeset
47 renames System.Storage_Elements.To_Integer;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 Task_Name_Length : constant := 32;
kono
parents:
diff changeset
50 -- The maximum length of task name displayed.
kono
parents:
diff changeset
51 -- ??? Consider merging this variable with Max_Task_Image_Length.
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 type Task_Result is record
kono
parents:
diff changeset
54 Task_Name : String (1 .. Task_Name_Length);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 Value : Natural;
kono
parents:
diff changeset
57 -- Amount of stack used. The value is calculated on the basis of the
kono
parents:
diff changeset
58 -- mechanism used by GNAT to allocate it, and it is NOT a precise value.
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 Stack_Size : Natural;
kono
parents:
diff changeset
61 -- Size of the stack
kono
parents:
diff changeset
62 end record;
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 type Result_Array_Type is array (Positive range <>) of Task_Result;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 type Stack_Analyzer is private;
kono
parents:
diff changeset
67 -- Type of the stack analyzer tool. It is used to fill a portion of the
kono
parents:
diff changeset
68 -- stack with Pattern, and to compute the stack used after some execution.
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 -- Usage:
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 -- A typical use of the package is something like:
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 -- A : Stack_Analyzer;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 -- task T is
kono
parents:
diff changeset
77 -- pragma Storage_Size (A_Storage_Size);
kono
parents:
diff changeset
78 -- end T;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 -- [...]
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 -- Bottom_Of_Stack : aliased Integer;
kono
parents:
diff changeset
83 -- -- Bottom_Of_Stack'Address will be used as an approximation of
kono
parents:
diff changeset
84 -- -- the bottom of stack. A good practise is to avoid allocating
kono
parents:
diff changeset
85 -- -- other local variables on this stack, as it would degrade
kono
parents:
diff changeset
86 -- -- the quality of this approximation.
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 -- begin
kono
parents:
diff changeset
89 -- Initialize_Analyzer (A,
kono
parents:
diff changeset
90 -- "Task t",
kono
parents:
diff changeset
91 -- A_Storage_Size,
kono
parents:
diff changeset
92 -- 0,
kono
parents:
diff changeset
93 -- A_Storage_Size - A_Guard,
kono
parents:
diff changeset
94 -- To_Stack_Address (Bottom_Of_Stack'Address));
kono
parents:
diff changeset
95 -- Fill_Stack (A);
kono
parents:
diff changeset
96 -- Some_User_Code;
kono
parents:
diff changeset
97 -- Compute_Result (A);
kono
parents:
diff changeset
98 -- Report_Result (A);
kono
parents:
diff changeset
99 -- end T;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 -- Errors:
kono
parents:
diff changeset
102 --
kono
parents:
diff changeset
103 -- We are instrumenting the code to measure the stack used by the user
kono
parents:
diff changeset
104 -- code. This method has a number of systematic errors, but several methods
kono
parents:
diff changeset
105 -- can be used to evaluate or reduce those errors. Here are those errors
kono
parents:
diff changeset
106 -- and the strategy that we use to deal with them:
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 -- Bottom offset:
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 -- Description: The procedure used to fill the stack with a given
kono
parents:
diff changeset
111 -- pattern will itself have a stack frame. The value of the stack
kono
parents:
diff changeset
112 -- pointer in this procedure is, therefore, different from the value
kono
parents:
diff changeset
113 -- before the call to the instrumentation procedure.
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 -- Strategy: The user of this package should measure the bottom of stack
kono
parents:
diff changeset
116 -- before the call to Fill_Stack and pass it in parameter. The impact
kono
parents:
diff changeset
117 -- is very minor unless the stack used is very small, but in this case
kono
parents:
diff changeset
118 -- you aren't very interested by the figure.
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 -- Instrumentation threshold at writing:
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 -- Description: The procedure used to fill the stack with a given
kono
parents:
diff changeset
123 -- pattern will itself have a stack frame. Therefore, it will
kono
parents:
diff changeset
124 -- fill the stack after this stack frame. This part of the stack will
kono
parents:
diff changeset
125 -- appear as used in the final measure.
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 -- Strategy: As the user passes the value of the bottom of stack to
kono
parents:
diff changeset
128 -- the instrumentation to deal with the bottom offset error, and as
kono
parents:
diff changeset
129 -- the instrumentation procedure knows where the pattern filling start
kono
parents:
diff changeset
130 -- on the stack, the difference between the two values is the minimum
kono
parents:
diff changeset
131 -- stack usage that the method can measure. If, when the results are
kono
parents:
diff changeset
132 -- computed, the pattern zone has been left untouched, we conclude
kono
parents:
diff changeset
133 -- that the stack usage is inferior to this minimum stack usage.
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 -- Instrumentation threshold at reading:
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 -- Description: The procedure used to read the stack at the end of the
kono
parents:
diff changeset
138 -- execution clobbers the stack by allocating its stack frame. If this
kono
parents:
diff changeset
139 -- stack frame is bigger than the total stack used by the user code at
kono
parents:
diff changeset
140 -- this point, it will increase the measured stack size.
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 -- Strategy: We could augment this stack frame and see if it changes the
kono
parents:
diff changeset
143 -- measure. However, this error should be negligible.
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 -- Pattern zone overflow:
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 -- Description: The stack grows outer than the topmost bound of the
kono
parents:
diff changeset
148 -- pattern zone. In that case, the topmost region modified in the
kono
parents:
diff changeset
149 -- pattern is not the maximum value of the stack pointer during the
kono
parents:
diff changeset
150 -- execution.
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 -- Strategy: At the end of the execution, the difference between the
kono
parents:
diff changeset
153 -- topmost memory region modified in the pattern zone and the
kono
parents:
diff changeset
154 -- topmost bound of the pattern zone can be understood as the
kono
parents:
diff changeset
155 -- biggest allocation that the method could have detect, provided
kono
parents:
diff changeset
156 -- that there is no "Untouched allocated zone" error and no "Pattern
kono
parents:
diff changeset
157 -- usage in user code" error. If no object in the user code is likely
kono
parents:
diff changeset
158 -- to have this size, this is not likely to happen.
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 -- Pattern usage in user code:
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 -- Description: The pattern can be found in the object of the user code.
kono
parents:
diff changeset
163 -- Therefore, the address space where this object has been allocated
kono
parents:
diff changeset
164 -- will appear as untouched.
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 -- Strategy: Choose a pattern that is uncommon. 16#0000_0000# is the
kono
parents:
diff changeset
167 -- worst choice; 16#DEAD_BEEF# can be a good one. A good choice is an
kono
parents:
diff changeset
168 -- address which is not a multiple of 2, and which is not in the
kono
parents:
diff changeset
169 -- target address space. You can also change the pattern to see if it
kono
parents:
diff changeset
170 -- changes the measure. Note that this error *very* rarely influence
kono
parents:
diff changeset
171 -- the measure of the total stack usage: to have some influence, the
kono
parents:
diff changeset
172 -- pattern has to be used in the object that has been allocated on the
kono
parents:
diff changeset
173 -- topmost address of the used stack.
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 -- Stack overflow:
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 -- Description: The pattern zone does not fit on the stack. This may
kono
parents:
diff changeset
178 -- lead to an erroneous execution.
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 -- Strategy: Specify a storage size that is bigger than the size of the
kono
parents:
diff changeset
181 -- pattern. 2 times bigger should be enough.
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 -- Augmentation of the user stack frames:
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 -- Description: The use of instrumentation object or procedure may
kono
parents:
diff changeset
186 -- augment the stack frame of the caller.
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 -- Strategy: Do *not* inline the instrumentation procedures. Do *not*
kono
parents:
diff changeset
189 -- allocate the Stack_Analyzer object on the stack.
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 -- Untouched allocated zone:
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 -- Description: The user code may allocate objects that it will never
kono
parents:
diff changeset
194 -- touch. In that case, the pattern will not be changed.
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 -- Strategy: There are no way to detect this error. Fortunately, this
kono
parents:
diff changeset
197 -- error is really rare, and it is most probably a bug in the user
kono
parents:
diff changeset
198 -- code, e.g. some uninitialized variable. It is (most of the time)
kono
parents:
diff changeset
199 -- harmless: it influences the measure only if the untouched allocated
kono
parents:
diff changeset
200 -- zone happens to be located at the topmost value of the stack
kono
parents:
diff changeset
201 -- pointer for the whole execution.
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 procedure Initialize (Buffer_Size : Natural);
kono
parents:
diff changeset
204 pragma Export (C, Initialize, "__gnat_stack_usage_initialize");
kono
parents:
diff changeset
205 -- Initializes the size of the buffer that stores the results. Only the
kono
parents:
diff changeset
206 -- first Buffer_Size results are stored. Any results that do not fit in
kono
parents:
diff changeset
207 -- this buffer will be displayed on the fly.
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 procedure Fill_Stack (Analyzer : in out Stack_Analyzer);
kono
parents:
diff changeset
210 -- Fill an area of the stack with the pattern Analyzer.Pattern. The size
kono
parents:
diff changeset
211 -- of this area is Analyzer.Size. After the call to this procedure,
kono
parents:
diff changeset
212 -- the memory will look like that:
kono
parents:
diff changeset
213 --
kono
parents:
diff changeset
214 -- Stack growing
kono
parents:
diff changeset
215 -- ---------------------------------------------------------------------->
kono
parents:
diff changeset
216 -- |<--------------------->|<----------------------------------->|
kono
parents:
diff changeset
217 -- | Stack frames to | Memory filled with Analyzer.Pattern |
kono
parents:
diff changeset
218 -- | Fill_Stack | |
kono
parents:
diff changeset
219 -- ^ | ^
kono
parents:
diff changeset
220 -- Analyzer.Stack_Base | Analyzer.Pattern_Limit
kono
parents:
diff changeset
221 -- ^
kono
parents:
diff changeset
222 -- Analyzer.Pattern_Limit +/- Analyzer.Pattern_Size
kono
parents:
diff changeset
223 --
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 procedure Initialize_Analyzer
kono
parents:
diff changeset
226 (Analyzer : in out Stack_Analyzer;
kono
parents:
diff changeset
227 Task_Name : String;
kono
parents:
diff changeset
228 Stack_Size : Natural;
kono
parents:
diff changeset
229 Stack_Base : Stack_Address;
kono
parents:
diff changeset
230 Pattern_Size : Natural;
kono
parents:
diff changeset
231 Pattern : Interfaces.Unsigned_32 := 16#DEAD_BEEF#);
kono
parents:
diff changeset
232 -- Should be called before any use of a Stack_Analyzer, to initialize it.
kono
parents:
diff changeset
233 -- Max_Pattern_Size is the size of the pattern zone, might be smaller than
kono
parents:
diff changeset
234 -- the full stack size Stack_Size in order to take into account e.g. the
kono
parents:
diff changeset
235 -- secondary stack and a guard against overflow. The actual size taken
kono
parents:
diff changeset
236 -- will be readjusted with data already used at the time the stack is
kono
parents:
diff changeset
237 -- actually filled.
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 Is_Enabled : Boolean := False;
kono
parents:
diff changeset
240 -- When this flag is true, then stack analysis is enabled
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 procedure Compute_Result (Analyzer : in out Stack_Analyzer);
kono
parents:
diff changeset
243 -- Read the pattern zone and deduce the stack usage. It should be called
kono
parents:
diff changeset
244 -- from the same frame as Fill_Stack. If Analyzer.Probe is not null, an
kono
parents:
diff changeset
245 -- array of Unsigned_32 with Analyzer.Probe elements is allocated on
kono
parents:
diff changeset
246 -- Compute_Result's stack frame. Probe can be used to detect the error:
kono
parents:
diff changeset
247 -- "instrumentation threshold at reading". See above. After the call
kono
parents:
diff changeset
248 -- to this procedure, the memory will look like:
kono
parents:
diff changeset
249 --
kono
parents:
diff changeset
250 -- Stack growing
kono
parents:
diff changeset
251 -- ----------------------------------------------------------------------->
kono
parents:
diff changeset
252 -- |<---------------------->|<-------------->|<--------->|<--------->|
kono
parents:
diff changeset
253 -- | Stack frames | Array of | used | Memory |
kono
parents:
diff changeset
254 -- | to Compute_Result | Analyzer.Probe | during | filled |
kono
parents:
diff changeset
255 -- | | elements | the | with |
kono
parents:
diff changeset
256 -- | | | execution | pattern |
kono
parents:
diff changeset
257 -- | | |
kono
parents:
diff changeset
258 -- |<----------------------------------------------------> |
kono
parents:
diff changeset
259 -- Stack used ^
kono
parents:
diff changeset
260 -- Pattern_Limit
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 procedure Report_Result (Analyzer : Stack_Analyzer);
kono
parents:
diff changeset
263 -- Store the results of the computation in memory, at the address
kono
parents:
diff changeset
264 -- corresponding to the symbol __gnat_stack_usage_results. This is not
kono
parents:
diff changeset
265 -- done inside Compute_Result in order to use as less stack as possible
kono
parents:
diff changeset
266 -- within a task.
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 procedure Output_Results;
kono
parents:
diff changeset
269 -- Print the results computed so far on the standard output. Should be
kono
parents:
diff changeset
270 -- called when all tasks are dead.
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 pragma Export (C, Output_Results, "__gnat_stack_usage_output_results");
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 private
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 package Unsigned_32_Addr is
kono
parents:
diff changeset
277 new System.Address_To_Access_Conversions (Interfaces.Unsigned_32);
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 subtype Pattern_Type is Interfaces.Unsigned_32;
kono
parents:
diff changeset
280 Bytes_Per_Pattern : constant := Pattern_Type'Object_Size / Storage_Unit;
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 type Stack_Analyzer is record
kono
parents:
diff changeset
283 Task_Name : String (1 .. Task_Name_Length);
kono
parents:
diff changeset
284 -- Name of the task
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 Stack_Base : Stack_Address;
kono
parents:
diff changeset
287 -- Address of the base of the stack, as given by the caller of
kono
parents:
diff changeset
288 -- Initialize_Analyzer.
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 Stack_Size : Natural;
kono
parents:
diff changeset
291 -- Entire size of the analyzed stack
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 Pattern_Size : Natural;
kono
parents:
diff changeset
294 -- Size of the pattern zone
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 Pattern : Pattern_Type;
kono
parents:
diff changeset
297 -- Pattern used to recognize untouched memory
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 Pattern_Limit : Stack_Address;
kono
parents:
diff changeset
300 -- Bound of the pattern area farthest to the base
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 Topmost_Touched_Mark : Stack_Address;
kono
parents:
diff changeset
303 -- Topmost address of the pattern area whose value it is pointing
kono
parents:
diff changeset
304 -- at has been modified during execution. If the systematic error are
kono
parents:
diff changeset
305 -- compensated, it is the topmost value of the stack pointer during
kono
parents:
diff changeset
306 -- the execution.
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 Pattern_Overlay_Address : System.Address;
kono
parents:
diff changeset
309 -- Address of the stack abstraction object we overlay over a
kono
parents:
diff changeset
310 -- task's real stack, typically a pattern-initialized array.
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 Result_Id : Positive;
kono
parents:
diff changeset
313 -- Id of the result. If less than value given to gnatbind -u corresponds
kono
parents:
diff changeset
314 -- to the location in the result array of result for the current task.
kono
parents:
diff changeset
315 end record;
kono
parents:
diff changeset
316
kono
parents:
diff changeset
317 Environment_Task_Analyzer : Stack_Analyzer;
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 Compute_Environment_Task : Boolean;
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 type Result_Array_Ptr is access all Result_Array_Type;
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 Result_Array : Result_Array_Ptr;
kono
parents:
diff changeset
324 pragma Export (C, Result_Array, "__gnat_stack_usage_results");
kono
parents:
diff changeset
325 -- Exported in order to have an easy accessible symbol in when debugging
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 Next_Id : Positive := 1;
kono
parents:
diff changeset
328 -- Id of the next stack analyzer
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 function Stack_Size
kono
parents:
diff changeset
331 (SP_Low : Stack_Address;
kono
parents:
diff changeset
332 SP_High : Stack_Address) return Natural;
kono
parents:
diff changeset
333 pragma Inline (Stack_Size);
kono
parents:
diff changeset
334 -- Return the size of a portion of stack delimited by SP_High and SP_Low
kono
parents:
diff changeset
335 -- (), i.e. the difference between SP_High and SP_Low. The storage element
kono
parents:
diff changeset
336 -- pointed by SP_Low is not included in the size. Inlined to reduce the
kono
parents:
diff changeset
337 -- size of the stack used by the instrumentation code.
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 end System.Stack_Usage;