gnat_arm_elf_13.2.1_db1e9283/arm-eabi/lib/gnat/embedded-stm32f4/gnarl/s-bbexti.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
------------------------------------------------------------------------------
--                                                                          --
--                  GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                --
--                                                                          --
--              S Y S T E M . B B . E X E C U T I O N _ T I M E             --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2011-2023, AdaCore                     --
--                                                                          --
-- GNARL is free software; you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with System.BB.Parameters;
with System.BB.Protection;

with System.Multiprocessors;
with System.Multiprocessors.Fair_Locks;
with System.Multiprocessors.Spin_Locks;

with System.OS_Interface;

------------------------------
-- System.BB.Execution_Time --
------------------------------

package body System.BB.Execution_Time is

   use type System.BB.Time.Time;
   use System.BB.Interrupts;
   use System.BB.Threads;
   use System.Multiprocessors;
   use System.Multiprocessors.Fair_Locks;
   use System.Multiprocessors.Spin_Locks;

   Interrupts_Execution_Time : array (Interrupt_ID) of System.BB.Time.Time :=
                                 (others => System.BB.Time.Time'First);
   --  Time counter for each interrupt

   Interrupt_Exec_Time_Lock : Fair_Lock := (Spinning => (others => False),
                                            Lock     => (Flag   => Unlocked));
   --  Protect access to interrupt time counters on multiprocessor systems

   CPU_Clock : array (CPU) of System.BB.Time.Time;
   --  Date of the last Interrupt

   procedure Scheduling_Event;
   --  Assign elapsed time to the executing Task/Interrupt and reset CPU clock.
   --  This must be called at the end of an execution period:
   --
   --    When the run-time switches from a task to another task
   --                                    a task to an interrupt
   --                                    an interrupt to a task
   --                                    an interrupt to another interrupt

   function Elapsed_Time return System.BB.Time.Time;
   --  Function returning the time elapsed since the last scheduling event,
   --  i.e. the execution time of the currently executing entity (thread or
   --  interrupt) that has not yet been added to the global counters.

   function Read_Execution_Time_Atomic
     (Th : System.BB.Threads.Thread_Id) return System.BB.Time.Time;
   --  Read the execution time of thread Th. The result is a coherent value:
   --  if the execution time has changed from A to B (while being read by this
   --  function), the result will be between A and B. The error is less
   --  than 2**32 and less than the increment.

   function To_Time (High, Low : Time.Word) return Time.Time;
   --  Low level routine to convert a composite execution time to type Time

   function To_Composite_Execution_Time
     (T : Time.Time) return Time.Composite_Execution_Time;
   --  Low level routine to convert a time to a composite execution time

   ------------------
   -- Elapsed_Time --
   ------------------

   function Elapsed_Time return System.BB.Time.Time is
      CPU_Id : constant CPU := System.OS_Interface.Current_CPU;

      Now  : constant BB.Time.Time := System.BB.Time.Clock;
      pragma Assert (Now >= CPU_Clock (CPU_Id));

   begin
      return Now - CPU_Clock (CPU_Id);
   end Elapsed_Time;

   ----------------------------
   -- Global_Interrupt_Clock --
   ----------------------------

   function Global_Interrupt_Clock return System.BB.Time.Time is
      Sum : System.BB.Time.Time := System.BB.Time.Time'First;

   begin
      --  Protect against interruption the addition of all
      --  Interrupt_Execution_Time and Elapsed_Time.

      System.BB.Protection.Enter_Kernel;

      --  Protect shared access on multiprocessor systems

      if System.BB.Parameters.Multiprocessor then
         Lock (Interrupt_Exec_Time_Lock);
      end if;

      for Interrupt in Interrupt_ID loop
         Sum := Sum + Interrupts_Execution_Time (Interrupt);
      end loop;

      --  If any interrupt is executing, we need to add the elapsed time
      --  between the last scheduling and now.

      if System.BB.Interrupts.Current_Interrupt /= No_Interrupt then
         Sum := Sum + Elapsed_Time;
      end if;

      if System.BB.Parameters.Multiprocessor then
         Unlock (Interrupt_Exec_Time_Lock);
      end if;

      System.BB.Protection.Leave_Kernel;

      return Sum;
   end Global_Interrupt_Clock;

   ---------------------
   -- Interrupt_Clock --
   ---------------------

   function Interrupt_Clock
     (Interrupt : Interrupt_ID) return System.BB.Time.Time
   is
      Value : System.BB.Time.Time;

   begin
      --  Protect against interruption the addition between Execution_Time
      --  and Elapsed_Time.

      System.BB.Protection.Enter_Kernel;

      --  Protect shared access on multiprocessor systems

      if System.BB.Parameters.Multiprocessor then
         Lock (Interrupt_Exec_Time_Lock);
      end if;

      Value := Interrupts_Execution_Time (Interrupt);

      --  If the interrupt is executing (i.e., if we are requesting the
      --  interrupt clock from the interrupt handler), we need to add the
      --  elapsed time between the last scheduling and now.

      if System.BB.Interrupts.Current_Interrupt = Interrupt then
         Value := Value + Elapsed_Time;
      end if;

      if System.BB.Parameters.Multiprocessor then
         Unlock (Interrupt_Exec_Time_Lock);
      end if;

      System.BB.Protection.Leave_Kernel;

      return Value;
   end Interrupt_Clock;

   --------------------------------
   -- Read_Execution_Time_Atomic --
   --------------------------------

   function Read_Execution_Time_Atomic
     (Th : System.BB.Threads.Thread_Id) return System.BB.Time.Time
   is
      use Time;
      H1 : Word;
      L  : Word;
      H2 : Word;

   begin
      --  Read parts in that order

      H1 := Th.Execution_Time.High;
      L  := Th.Execution_Time.Low;
      H2 := Th.Execution_Time.High;

      --  If value has changed while being read, keep the latest high part,
      --  but use 0 for the low part. So the result will be greater than
      --  the old value and less than the new value.

      if H1 /= H2 then
         L := 0;
      end if;

      return Time.Time (H2) * 2 ** 32 + Time.Time (L);
   end Read_Execution_Time_Atomic;

   ----------------------
   -- Scheduling_Event --
   ----------------------

   procedure Scheduling_Event is
      Now            : constant System.BB.Time.Time := System.BB.Time.Clock;
      CPU_Id         : constant CPU                 :=
                         System.OS_Interface.Current_CPU;
      Last_CPU_Clock : constant System.BB.Time.Time := CPU_Clock (CPU_Id);
      Elapsed_Time   : System.BB.Time.Time;

   begin
      pragma Assert (Now >= Last_CPU_Clock);
      Elapsed_Time := Now - Last_CPU_Clock;

      --  Reset the clock

      CPU_Clock (CPU_Id) := Now;

      --  Case of CPU currently executing an interrupt

      if Current_Interrupt /= No_Interrupt then

         --  Protect shared access on multiprocessor systems

         if System.BB.Parameters.Multiprocessor then
            Lock (Interrupt_Exec_Time_Lock);
         end if;

         Interrupts_Execution_Time (Current_Interrupt) :=
           Interrupts_Execution_Time (Current_Interrupt) + Elapsed_Time;

         if System.BB.Parameters.Multiprocessor then
            Unlock (Interrupt_Exec_Time_Lock);
         end if;

      --  This CPU is currently executing a task

      else
         declare
            T : Time.Time;
         begin
            T := To_Time (Thread_Self.Execution_Time.High,
                          Thread_Self.Execution_Time.Low);
            T := T + Elapsed_Time;
            Thread_Self.Execution_Time := To_Composite_Execution_Time (T);
         end;
      end if;
   end Scheduling_Event;

   ------------------
   -- Thread_Clock --
   ------------------

   function Thread_Clock
     (Th : System.BB.Threads.Thread_Id) return System.BB.Time.Time
   is
      Res : System.BB.Time.Time;

      Sec : System.BB.Time.Time;
      --  Second read of execution time

      ET : System.BB.Time.Time;
      --  Elapsed time

   begin
      pragma Assert (Th /= Null_Thread_Id);

      Res := Read_Execution_Time_Atomic (Th);

      --  If the thread Th is running, we need to add the elapsed time between
      --  the last scheduling and now. The thread Th is running if it is the
      --  current one and no interrupt is executed

      if Th = Thread_Self
        and then System.BB.Interrupts.Current_Interrupt = No_Interrupt
      then

         ET := Elapsed_Time;

         Sec := Read_Execution_Time_Atomic (Th);

         if Res /= Sec then

            --  The whole set of values (Res, ET and Sec) isn't coherent, as
            --  the execution time has been updated (might happen in case of
            --  interrupt). Unfortunately, the error in Sec might be as large
            --  as ET. So lets read again. The error will be small, as the time
            --  spent between this third read and the second one is small.

            Res := Read_Execution_Time_Atomic (Th);

         else
            Res := Res + ET;
         end if;
      end if;

      return Res;
   end Thread_Clock;

   ---------------------------------
   -- To_Composite_Execution_Time --
   ---------------------------------

   function To_Composite_Execution_Time
     (T : Time.Time) return Time.Composite_Execution_Time is
   begin
      return (High => Time.Word (T / 2 ** 32),
              Low  => Time.Word (T mod 2 ** 32));
   end To_Composite_Execution_Time;

   -------------
   -- To_Time --
   -------------

   function To_Time (High, Low : Time.Word) return Time.Time is
   begin
      return Time.Time (High) * 2 ** 32 + Time.Time (Low);
   end To_Time;

begin
   --  Set the hooks to enable computation

   System.BB.Time.Scheduling_Event_Hook       := Scheduling_Event'Access;

   --  Initialize CPU_Clock

   declare
      Now : constant BB.Time.Time := System.BB.Time.Epoch;
      --  Calling Clock here is tricky because we may be doing so before timer
      --  initialization. Hence, we simply get the startup time.

   begin
      CPU_Clock := (others => Now);
   end;
end System.BB.Execution_Time;