1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . E X E C U T I O N _ T I M E --
9 -- Copyright (C) 2007-2017, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This is the Darwin version of this package
34 with Ada
.Task_Identification
; use Ada
.Task_Identification
;
35 with Ada
.Unchecked_Conversion
;
38 with System
.OS_Interface
; use System
.OS_Interface
;
39 with System
.Task_Primitives
.Operations
; use System
.Task_Primitives
.Operations
;
41 with Interfaces
.C
; use Interfaces
.C
;
43 package body Ada
.Execution_Time
is
51 Right
: Ada
.Real_Time
.Time_Span
) return CPU_Time
53 use type Ada
.Real_Time
.Time
;
55 return CPU_Time
(Ada
.Real_Time
.Time
(Left
) + Right
);
59 (Left
: Ada
.Real_Time
.Time_Span
;
60 Right
: CPU_Time
) return CPU_Time
62 use type Ada
.Real_Time
.Time
;
64 return CPU_Time
(Left
+ Ada
.Real_Time
.Time
(Right
));
73 Right
: Ada
.Real_Time
.Time_Span
) return CPU_Time
75 use type Ada
.Real_Time
.Time
;
77 return CPU_Time
(Ada
.Real_Time
.Time
(Left
) - Right
);
82 Right
: CPU_Time
) return Ada
.Real_Time
.Time_Span
84 use type Ada
.Real_Time
.Time
;
86 return (Ada
.Real_Time
.Time
(Left
) - Ada
.Real_Time
.Time
(Right
));
94 (T
: Ada
.Task_Identification
.Task_Id
:=
95 Ada
.Task_Identification
.Current_Task
) return CPU_Time
97 function Convert_Ids
is new
98 Ada
.Unchecked_Conversion
(Task_Id
, System
.Tasking
.Task_Id
);
100 function To_CPU_Time
is
101 new Ada
.Unchecked_Conversion
(Duration, CPU_Time
);
102 -- Time is equal to Duration (although it is a private type) and
103 -- CPU_Time is equal to Time.
105 subtype integer_t
is Interfaces
.C
.int
;
106 subtype mach_port_t
is integer_t
;
107 -- Type definition for Mach.
109 type time_value_t
is record
111 microseconds
: integer_t
;
113 pragma Convention
(C
, time_value_t
);
116 type thread_basic_info_t
is record
117 user_time
: time_value_t
;
118 system_time
: time_value_t
;
119 cpu_usage
: integer_t
;
121 run_state
: integer_t
;
123 suspend_count
: integer_t
;
124 sleep_time
: integer_t
;
126 pragma Convention
(C
, thread_basic_info_t
);
127 -- Mach structure from thread_info.h
129 THREAD_BASIC_INFO
: constant := 3;
130 THREAD_BASIC_INFO_COUNT
: constant := 10;
131 -- Flavors for basic info
133 function thread_info
(Target
: mach_port_t
;
135 Thread_Info
: System
.Address
;
136 Count
: System
.Address
) return integer_t
;
137 pragma Import
(C
, thread_info
);
138 -- Mach call to get info on a thread
140 function pthread_mach_thread_np
(Thread
: pthread_t
) return mach_port_t
;
141 pragma Import
(C
, pthread_mach_thread_np
);
142 -- Get Mach thread from posix thread
144 Result
: Interfaces
.C
.int
;
147 Ti
: thread_basic_info_t
;
150 if T
= Ada
.Task_Identification
.Null_Task_Id
then
154 Thread
:= Get_Thread_Id
(Convert_Ids
(T
));
155 Port
:= pthread_mach_thread_np
(Thread
);
156 pragma Assert
(Port
> 0);
158 Count
:= THREAD_BASIC_INFO_COUNT
;
159 Result
:= thread_info
(Port
, THREAD_BASIC_INFO
,
160 Ti
'Address, Count
'Address);
161 pragma Assert
(Result
= 0);
162 pragma Assert
(Count
= THREAD_BASIC_INFO_COUNT
);
165 (Duration (Ti
.user_time
.seconds
+ Ti
.system_time
.seconds
)
166 + Duration (Ti
.user_time
.microseconds
167 + Ti
.system_time
.microseconds
) / 1E6
);
170 --------------------------
171 -- Clock_For_Interrupts --
172 --------------------------
174 function Clock_For_Interrupts
return CPU_Time
is
176 -- According to AI 0170-1, D.14(18.1/3), if Interrupt_Clocks_Supported
177 -- is set to False the function raises Program_Error.
180 return CPU_Time_First
;
181 end Clock_For_Interrupts
;
189 SC
: out Ada
.Real_Time
.Seconds_Count
;
190 TS
: out Ada
.Real_Time
.Time_Span
)
193 Ada
.Real_Time
.Split
(Ada
.Real_Time
.Time
(T
), SC
, TS
);
201 (SC
: Ada
.Real_Time
.Seconds_Count
;
202 TS
: Ada
.Real_Time
.Time_Span
:= Ada
.Real_Time
.Time_Span_Zero
)
206 return CPU_Time
(Ada
.Real_Time
.Time_Of
(SC
, TS
));
209 end Ada
.Execution_Time
;