btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / system / libroot / os / system_info.cpp
blob8654ddf5cccbfb1e1726ae9da598d6d253547272
1 /*
2 * Copyright 2013, Paweł Dziepak, pdziepak@quarnos.org.
3 * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
5 */
8 #include <OS.h>
10 #include <string.h>
12 #include <algorithm>
14 #include <syscalls.h>
15 #include <system_info.h>
18 #if _BEOS_R5_COMPATIBLE_
21 #define LEGACY_B_CPU_X86 15
23 #define LEGACY_B_AT_CLONE_PLATFORM 2
25 typedef struct {
26 bigtime_t active_time; /* usec of doing useful work since boot */
27 } legacy_cpu_info;
29 typedef struct {
30 int32 id[2]; /* unique machine ID */
31 bigtime_t boot_time; /* time of boot (usecs since 1/1/1970) */
33 int32 cpu_count; /* number of cpus */
34 int32 cpu_type; /* type of cpu */
35 int32 cpu_revision; /* revision # of cpu */
36 legacy_cpu_info cpu_infos[8]; /* info about individual cpus */
37 int64 cpu_clock_speed; /* processor clock speed (Hz) */
38 int64 bus_clock_speed; /* bus clock speed (Hz) */
39 int32 platform_type; /* type of machine we're on */
41 int32 max_pages; /* total # of accessible pages */
42 int32 used_pages; /* # of accessible pages in use */
43 int32 page_faults; /* # of page faults */
44 int32 max_sems;
45 int32 used_sems;
46 int32 max_ports;
47 int32 used_ports;
48 int32 max_threads;
49 int32 used_threads;
50 int32 max_teams;
51 int32 used_teams;
53 char kernel_name[256];
54 char kernel_build_date[32];
55 char kernel_build_time[32];
56 int64 kernel_version;
58 bigtime_t _busy_wait_time; /* reserved for whatever */
60 int32 cached_pages;
61 uint32 abi; /* the system API */
62 int32 ignored_pages; /* # of ignored/inaccessible pages */
63 int32 pad;
64 } legacy_system_info;
67 extern "C" status_t
68 _get_system_info(legacy_system_info* info, size_t size)
70 if (info == NULL || size != sizeof(legacy_system_info))
71 return B_BAD_VALUE;
72 memset(info, 0, sizeof(legacy_system_info));
74 system_info systemInfo;
75 status_t error = _kern_get_system_info(&systemInfo);
76 if (error != B_OK)
77 return error;
79 cpu_info cpuInfos[8];
80 error = _kern_get_cpu_info(0, std::min(systemInfo.cpu_count, uint32(8)),
81 cpuInfos);
82 if (error != B_OK)
83 return error;
85 info->boot_time = systemInfo.boot_time;
86 info->cpu_count = std::min(systemInfo.cpu_count, uint32(8));
87 for (int32 i = 0; i < info->cpu_count; i++)
88 info->cpu_infos[i].active_time = cpuInfos[i].active_time;
90 info->platform_type = LEGACY_B_AT_CLONE_PLATFORM;
91 info->cpu_type = LEGACY_B_CPU_X86;
93 uint32 topologyNodeCount = 0;
94 cpu_topology_node_info* topology = NULL;
95 error = get_cpu_topology_info(NULL, &topologyNodeCount);
96 if (error != B_OK)
97 return B_OK;
98 if (topologyNodeCount != 0) {
99 topology = new(std::nothrow) cpu_topology_node_info[topologyNodeCount];
100 if (topology == NULL)
101 return B_NO_MEMORY;
103 error = get_cpu_topology_info(topology, &topologyNodeCount);
104 if (error != B_OK) {
105 delete[] topology;
106 return error;
109 for (uint32 i = 0; i < topologyNodeCount; i++) {
110 if (topology[i].type == B_TOPOLOGY_CORE) {
111 info->cpu_clock_speed = topology[i].data.core.default_frequency;
112 break;
115 info->bus_clock_speed = info->cpu_clock_speed;
116 delete[] topology;
118 info->max_pages = std::min(systemInfo.max_pages, uint64(INT32_MAX));
119 info->used_pages = std::min(systemInfo.used_pages, uint64(INT32_MAX));
120 info->cached_pages = std::min(systemInfo.cached_pages, uint64(INT32_MAX));
121 info->ignored_pages = std::min(systemInfo.ignored_pages, uint64(INT32_MAX));
122 info->page_faults = std::min(systemInfo.page_faults, uint32(INT32_MAX));
123 info->max_sems = std::min(systemInfo.max_sems, uint32(INT32_MAX));
124 info->used_sems = std::min(systemInfo.used_sems, uint32(INT32_MAX));
125 info->max_ports = std::min(systemInfo.max_ports, uint32(INT32_MAX));
126 info->used_ports = std::min(systemInfo.used_ports, uint32(INT32_MAX));
127 info->max_threads = std::min(systemInfo.max_threads, uint32(INT32_MAX));
128 info->used_threads = std::min(systemInfo.used_threads, uint32(INT32_MAX));
129 info->max_teams = std::min(systemInfo.max_teams, uint32(INT32_MAX));
130 info->used_teams = std::min(systemInfo.used_teams, uint32(INT32_MAX));
132 strlcpy(info->kernel_name, systemInfo.kernel_name,
133 sizeof(info->kernel_name));
134 strlcpy(info->kernel_build_date, systemInfo.kernel_build_date,
135 sizeof(info->kernel_build_date));
136 strlcpy(info->kernel_build_time, systemInfo.kernel_build_time,
137 sizeof(info->kernel_build_time));
138 info->kernel_version = systemInfo.kernel_version;
140 info->abi = systemInfo.abi;
142 return B_OK;
146 #endif // _BEOS_R5_COMPATIBLE_
149 status_t
150 __get_system_info(system_info* info)
152 return _kern_get_system_info(info);
156 status_t
157 __get_cpu_info(uint32 firstCPU, uint32 cpuCount, cpu_info* info)
159 return _kern_get_cpu_info(firstCPU, cpuCount, info);
163 status_t
164 __get_cpu_topology_info(cpu_topology_node_info* topologyInfos,
165 uint32* topologyInfoCount)
167 return _kern_get_cpu_topology_info(topologyInfos, topologyInfoCount);
171 status_t
172 __start_watching_system(int32 object, uint32 flags, port_id port, int32 token)
174 return _kern_start_watching_system(object, flags, port, token);
178 status_t
179 __stop_watching_system(int32 object, uint32 flags, port_id port, int32 token)
181 return _kern_stop_watching_system(object, flags, port, token);
185 int32
186 is_computer_on(void)
188 return _kern_is_computer_on();
192 double
193 is_computer_on_fire(void)
195 return 0.63739;
199 B_DEFINE_WEAK_ALIAS(__get_system_info, get_system_info);
200 B_DEFINE_WEAK_ALIAS(__get_cpu_info, get_cpu_info);
201 B_DEFINE_WEAK_ALIAS(__get_cpu_topology_info, get_cpu_topology_info);