Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger | | ||
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 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/system/os.hpp" | ||
30 | |||
31 | namespace gate | ||
32 | { | ||
33 | namespace sys | ||
34 | { | ||
35 | |||
36 | 1 | OperatingSystem::PlatformEnum OperatingSystem::getPlatform() | |
37 | { | ||
38 | 1 | uint32_t id = 0; | |
39 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_get_platform(&id); |
40 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(res); |
41 | 1 | return static_cast<PlatformEnum>(id); | |
42 | } | ||
43 | 1 | String OperatingSystem::getPlatformLabel(PlatformEnum id) | |
44 | { | ||
45 | 1 | char const* static_text = gate_os_get_platform_label(static_cast<uint32_t>(id)); | |
46 | 1 | return String::createStatic(static_text); | |
47 | } | ||
48 | 1 | uint32_t OperatingSystem::getAddressSpace() | |
49 | { | ||
50 | 1 | return gate_os_address_space(); | |
51 | } | ||
52 | 1 | uint32_t OperatingSystem::getUpTimeSeconds() | |
53 | { | ||
54 | 1 | return gate_os_up_time_seconds(); | |
55 | } | ||
56 | 1 | Time OperatingSystem::getBootTime() | |
57 | { | ||
58 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | Time bootTime = Time::now(); |
59 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | uint64_t up_time = gate_os_up_time_seconds(); |
60 | 1 | up_time *= 1000000; | |
61 | 1 | bootTime.timestamp -= up_time; | |
62 | 1 | return bootTime; | |
63 | } | ||
64 | |||
65 | 1 | String OperatingSystem::getOSName() | |
66 | { | ||
67 | char buffer[1024]; | ||
68 | 1 | size_t buffer_used = 0; | |
69 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_print_osname(buffer, sizeof(buffer), &buffer_used); |
70 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(res); |
71 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return String(buffer, buffer_used); |
72 | } | ||
73 | 1 | String OperatingSystem::getProductName() | |
74 | { | ||
75 | char buffer[1024]; | ||
76 | 1 | size_t buffer_used = 0; | |
77 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_print_productname(buffer, sizeof(buffer), &buffer_used); |
78 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
79 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return String(buffer, buffer_used); |
80 | } | ||
81 | 1 | Version OperatingSystem::getVersion() | |
82 | { | ||
83 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | Version ver; |
84 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_get_version(&ver); |
85 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
86 | 1 | return ver; | |
87 | } | ||
88 | |||
89 | 1 | String OperatingSystem::getHostName() | |
90 | { | ||
91 | char buffer[1024]; | ||
92 | 1 | size_t buffer_used = 0; | |
93 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_get_hostname_str(buffer, sizeof(buffer), &buffer_used); |
94 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
95 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return String(buffer, buffer_used); |
96 | } | ||
97 | 1 | String OperatingSystem::getHostDomain() | |
98 | { | ||
99 | char buffer[1024]; | ||
100 | 1 | size_t buffer_used = 0; | |
101 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_get_hostdomainname_str(buffer, sizeof(buffer), &buffer_used); |
102 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
103 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return String(buffer, buffer_used); |
104 | } | ||
105 | 1 | Array<uint8_t> OperatingSystem::getUid() | |
106 | { | ||
107 | gate_uint8_t buffer[1024]; | ||
108 | 1 | size_t buffer_used = 0; | |
109 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t res = gate_os_get_uid(buffer, sizeof(buffer), &buffer_used); |
110 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
111 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return Array<uint8_t>(buffer, buffer_used); |
112 | } | ||
113 | |||
114 | ✗ | void OperatingSystem::shutdown() | |
115 | { | ||
116 | ✗ | result_t res = gate_os_shutdown(); | |
117 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
118 | ✗ | } | |
119 | ✗ | void OperatingSystem::reboot() | |
120 | { | ||
121 | ✗ | result_t res = gate_os_reboot(); | |
122 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
123 | ✗ | } | |
124 | |||
125 | |||
126 | |||
127 | |||
128 | ✗ | SystemCpuInfo::SystemCpuInfo() | |
129 | { | ||
130 | ✗ | gate_os_cpuinfo_t* ptr = static_cast<gate_os_cpuinfo_t*>(this); | |
131 | ✗ | gate_mem_clear(ptr, sizeof(gate_os_cpuinfo_t)); | |
132 | ✗ | } | |
133 | ✗ | uint32_t SystemCpuInfo::getCores() const | |
134 | { | ||
135 | ✗ | return this->cpu_cores; | |
136 | } | ||
137 | ✗ | uint32_t SystemCpuInfo::getVersion() const | |
138 | { | ||
139 | ✗ | return this->cpu_version; | |
140 | } | ||
141 | ✗ | uint32_t SystemCpuInfo::getSpeedMHz() const | |
142 | { | ||
143 | ✗ | return this->cpu_speed_mhz; | |
144 | } | ||
145 | ✗ | uint32_t SystemCpuInfo::getCacheSize() const | |
146 | { | ||
147 | ✗ | return this->cpu_cache_bytes; | |
148 | } | ||
149 | ✗ | String SystemCpuInfo::getName() const | |
150 | { | ||
151 | ✗ | return String(this->cpu_name); | |
152 | } | ||
153 | ✗ | String SystemCpuInfo::getVendor() const | |
154 | { | ||
155 | ✗ | return String(this->cpu_vendor); | |
156 | } | ||
157 | ✗ | String SystemCpuInfo::getDescription() const | |
158 | { | ||
159 | ✗ | return String(this->cpu_description); | |
160 | } | ||
161 | |||
162 | |||
163 | |||
164 | ✗ | SystemCpuActivation::SystemCpuActivation() | |
165 | { | ||
166 | ✗ | gate_os_cpu_activation_t* ptr = static_cast<gate_os_cpu_activation_t*>(this); | |
167 | ✗ | gate_mem_clear(ptr, sizeof(gate_os_cpu_activation_t)); | |
168 | ✗ | } | |
169 | |||
170 | ✗ | bool_t SystemCpuActivation::get(size_t coreIndex) | |
171 | { | ||
172 | ✗ | return gate_os_cpu_get_activation(this, coreIndex); | |
173 | } | ||
174 | ✗ | void SystemCpuActivation::set(size_t coreIndex, bool_t enabled) | |
175 | { | ||
176 | ✗ | result_t res = gate_os_cpu_set_activation(this, coreIndex, enabled); | |
177 | ✗ | GATEXX_CHECK_ERROR(res); | |
178 | ✗ | } | |
179 | |||
180 | |||
181 | |||
182 | ✗ | SystemCpu::ArchEnum SystemCpu::getArchitecture() | |
183 | { | ||
184 | ✗ | enumint_t arch = 0; | |
185 | ✗ | result_t res = gate_os_get_cpu_architecture(&arch); | |
186 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
187 | ✗ | return static_cast<SystemCpu::ArchEnum>(arch); | |
188 | } | ||
189 | |||
190 | ✗ | String SystemCpu::getArchitectureLabel(ArchEnum arch) | |
191 | { | ||
192 | ✗ | char const* static_text = gate_os_get_cpu_architecture_label(static_cast<gate_uint8_t>(arch)); | |
193 | ✗ | return String::createStatic(static_text); | |
194 | } | ||
195 | |||
196 | ✗ | SystemCpuInfo SystemCpu::getCpuInfo() | |
197 | { | ||
198 | ✗ | SystemCpuInfo info; | |
199 | ✗ | result_t res = gate_os_get_cpu_info(&info); | |
200 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
201 | ✗ | return info; | |
202 | } | ||
203 | |||
204 | ✗ | static gate_bool_t gate_os_cpu_feature_callback(char const* feature_name, void* param) | |
205 | { | ||
206 | ✗ | ArrayList<String>* ptr_features = static_cast<ArrayList<String>*>(param); | |
207 | try | ||
208 | { | ||
209 | ✗ | String text(feature_name); | |
210 | ✗ | ptr_features->add(text); | |
211 | } | ||
212 | ✗ | catch (...) | |
213 | { | ||
214 | } | ||
215 | ✗ | return true; | |
216 | } | ||
217 | |||
218 | ✗ | Array<String> SystemCpu::getCpuFeatures() | |
219 | { | ||
220 | ✗ | ArrayList<String> features; | |
221 | ✗ | result_t res = gate_os_enum_cpu_features(&gate_os_cpu_feature_callback, &features); | |
222 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
223 | ✗ | return features.toArray(); | |
224 | } | ||
225 | |||
226 | |||
227 | ✗ | SystemCpuActivation SystemCpu::getProcessAffinity() | |
228 | { | ||
229 | ✗ | SystemCpuActivation affinity; | |
230 | ✗ | result_t res = gate_os_get_process_cpu_affinity(&affinity); | |
231 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
232 | ✗ | return affinity; | |
233 | } | ||
234 | |||
235 | ✗ | void SystemCpu::setProcessAffinity(SystemCpuActivation const& affinity) | |
236 | { | ||
237 | ✗ | result_t res = gate_os_set_process_cpu_affinity(&affinity); | |
238 | ✗ | GATEXX_CHECK_EXCEPTION(res); | |
239 | ✗ | } | |
240 | |||
241 | |||
242 | |||
243 | |||
244 | ✗ | SystemCpuLoad::SystemCpuLoad() | |
245 | { | ||
246 | ✗ | result_t result = gate_os_cpu_load_init(&this->impl); | |
247 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
248 | ✗ | } | |
249 | ✗ | SystemCpuLoad::~SystemCpuLoad() noexcept | |
250 | { | ||
251 | ✗ | gate_os_cpu_load_uninit(&this->impl); | |
252 | ✗ | } | |
253 | |||
254 | ✗ | uint16_t SystemCpuLoad::updateState(real32_t& percent) | |
255 | { | ||
256 | ✗ | uint16_t current_load = 0; | |
257 | ✗ | result_t result = gate_os_cpu_load_update(&this->impl, ¤t_load); | |
258 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
259 | ✗ | percent = (static_cast<real32_t>(current_load) / 65535.0f) * 100.0f; | |
260 | ✗ | return current_load; | |
261 | } | ||
262 | |||
263 | |||
264 | ✗ | void SystemMemory::getPhysicalLoad(uint64_t& totalBytes, uint64_t& availableBytes) | |
265 | { | ||
266 | ✗ | result_t result = gate_os_get_physical_memory(&totalBytes, &availableBytes); | |
267 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
268 | ✗ | } | |
269 | ✗ | void SystemMemory::getVirtualLoad(uint64_t& totalBytes, uint64_t& availableBytes) | |
270 | { | ||
271 | ✗ | result_t result = gate_os_get_virtual_memory(&totalBytes, &availableBytes); | |
272 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
273 | ✗ | } | |
274 | |||
275 | } // end of namespace sys | ||
276 | } // end of namespace gate | ||
277 |