Debian Codegraph @

gcc/ada/libgnarl/s-stusta.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . S T A C K _ U S A G E . T A S K I N G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2009-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is why this package is part of GNARL:
33
34 with System.Tasking.Debug;
35 with System.Tasking.Stages;
36 with System.Task_Primitives.Operations;
37
38 with System.IO;
39
40 package body System.Stack_Usage.Tasking is
41 use System.IO;
42
43 procedure Report_For_Task (Id : System.Tasking.Task_Id);
44 -- A generic procedure calculating stack usage for a given task
45
46 procedure Compute_All_Tasks;
47 -- Compute the stack usage for all tasks and saves it in
48 -- System.Stack_Usage.Result_Array
49
50 procedure Compute_Current_Task;
51 -- Compute the stack usage for a given task and saves it in the precise
52 -- slot in System.Stack_Usage.Result_Array;
53
54 procedure Report_Impl (All_Tasks : Boolean; Do_Print : Boolean);
55 -- Report the stack usage of either all tasks (All_Tasks = True) or of the
56 -- current task (All_Task = False). If Print is True, then results are
57 -- printed on stderr
58
59 procedure Convert
60 (TS : System.Stack_Usage.Task_Result;
61 Res : out Stack_Usage_Result);
62 -- Convert an object of type System.Stack_Usage in a Stack_Usage_Result
63
64 -------------
65 -- Convert --
66 -------------
67
68 procedure Convert
69 (TS : System.Stack_Usage.Task_Result;
70 Res : out Stack_Usage_Result) is
71 begin
72 Res := TS;
73 end Convert;
74
75 ---------------------
76 -- Report_For_Task --
77 ---------------------
78
79 procedure Report_For_Task (Id : System.Tasking.Task_Id) is
80 begin
81 System.Stack_Usage.Compute_Result (Id.Common.Analyzer);
82 System.Stack_Usage.Report_Result (Id.Common.Analyzer);
83 end Report_For_Task;
84
85 -----------------------
86 -- Compute_All_Tasks --
87 -----------------------
88
89 procedure Compute_All_Tasks is
90 Id : System.Tasking.Task_Id;
91 use type System.Tasking.Task_Id;
92 begin
93 if not System.Stack_Usage.Is_Enabled then
94 Put_Line ("Stack Usage not enabled: bind with -uNNN switch");
95 else
96
97 -- Loop over all tasks
98
99 for J in System.Tasking.Debug.Known_Tasks'First + 1
100 .. System.Tasking.Debug.Known_Tasks'Last
101 loop
102 Id := System.Tasking.Debug.Known_Tasks (J);
103 exit when Id = null;
104
105 -- Calculate the task usage for a given task
106
107 if not System.Tasking.Stages.Terminated (Id) then
108 Report_For_Task (Id);
109 end if;
110 end loop;
111
112 end if;
113 end Compute_All_Tasks;
114
115 --------------------------
116 -- Compute_Current_Task --
117 --------------------------
118
119 procedure Compute_Current_Task is
120 begin
121 if not System.Stack_Usage.Is_Enabled then
122 Put_Line ("Stack Usage not enabled: bind with -uNNN switch");
123 else
124
125 -- The current task
126
127 Report_For_Task (System.Tasking.Self);
128
129 end if;
130 end Compute_Current_Task;
131
132 -----------------
133 -- Report_Impl --
134 -----------------
135
136 procedure Report_Impl (All_Tasks : Boolean; Do_Print : Boolean) is
137 begin
138
139 -- Lock the runtime
140
141 System.Task_Primitives.Operations.Lock_RTS;
142
143 -- Calculate results
144
145 if All_Tasks then
146 Compute_All_Tasks;
147 else
148 Compute_Current_Task;
149 end if;
150
151 -- Output results
152 if Do_Print then
153 System.Stack_Usage.Output_Results;
154 end if;
155
156 -- Unlock the runtime
157
158 System.Task_Primitives.Operations.Unlock_RTS;
159
160 end Report_Impl;
161
162 ----------------------
163 -- Report_All_Tasks --
164 ----------------------
165
166 procedure Report_All_Tasks is
167 begin
168 Report_Impl (True, True);
169 end Report_All_Tasks;
170
171 -------------------------
172 -- Report_Current_Task --
173 -------------------------
174
175 procedure Report_Current_Task is
176 Res : Stack_Usage_Result;
177 begin
178 Res := Get_Current_Task_Usage;
179 Print (Res);
180 end Report_Current_Task;
181
182 -------------------------
183 -- Get_All_Tasks_Usage --
184 -------------------------
185
186 function Get_All_Tasks_Usage return Stack_Usage_Result_Array is
187 Res : Stack_Usage_Result_Array
188 (1 .. System.Stack_Usage.Result_Array'Length);
189 begin
190 Report_Impl (True, False);
191
192 for J in Res'Range loop
193 Convert (System.Stack_Usage.Result_Array (J), Res (J));
194 end loop;
195
196 return Res;
197 end Get_All_Tasks_Usage;
198
199 ----------------------------
200 -- Get_Current_Task_Usage --
201 ----------------------------
202
203 function Get_Current_Task_Usage return Stack_Usage_Result is
204 Res : Stack_Usage_Result;
205 Original : System.Stack_Usage.Task_Result;
206 Found : Boolean := False;
207 begin
208
209 Report_Impl (False, False);
210
211 -- Look for the task info in System.Stack_Usage.Result_Array;
212 -- the search is based on task name
213
214 for T in System.Stack_Usage.Result_Array'Range loop
215 if System.Stack_Usage.Result_Array (T).Task_Name =
216 System.Tasking.Self.Common.Analyzer.Task_Name
217 then
218 Original := System.Stack_Usage.Result_Array (T);
219 Found := True;
220 exit;
221 end if;
222 end loop;
223
224 -- Be sure a task has been found
225
226 pragma Assert (Found);
227
228 Convert (Original, Res);
229 return Res;
230 end Get_Current_Task_Usage;
231
232 -----------
233 -- Print --
234 -----------
235
236 procedure Print (Obj : Stack_Usage_Result) is
237 Pos : Positive := Obj.Task_Name'Last;
238
239 begin
240 -- Simply trim the string containing the task name
241
242 for S in Obj.Task_Name'Range loop
243 if Obj.Task_Name (S) = ' ' then
244 Pos := S;
245 exit;
246 end if;
247 end loop;
248
249 declare
250 T_Name : constant String :=
251 Obj.Task_Name (Obj.Task_Name'First .. Pos);
252 begin
253 Put_Line
254 ("| " & T_Name & " | " & Natural'Image (Obj.Stack_Size) &
255 Natural'Image (Obj.Value));
256 end;
257 end Print;
258
259 end System.Stack_Usage.Tasking;