ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Cpu.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "Cpu.h"
9 
10 #include "CVPlatform.h"
11 
12 #ifdef CV_WINDOWS
13 #include <windows.h>
14 namespace cloudViewer {
15 namespace system {
16 
17 int cpu_clock_by_os(void) {
18  HKEY key;
19  DWORD result;
20  DWORD size = 4;
21 
22  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
23  TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
24  0, KEY_READ, &key) != ERROR_SUCCESS)
25  return -1;
26 
27  if (RegQueryValueEx(key, TEXT("~MHz"), NULL, NULL, (LPBYTE)&result,
28  (LPDWORD)&size) != ERROR_SUCCESS) {
29  RegCloseKey(key);
30  return -1;
31  }
32  RegCloseKey(key);
33 
34  return (int)result;
35 }
36 } // namespace system
37 } // namespace cloudViewer
38 #else
39 #ifdef CV_MAC_OS
40 #include <sys/sysctl.h>
41 #include <sys/types.h>
42 /* Assuming Mac OS X with hw.cpufrequency sysctl */
43 namespace cloudViewer {
44 namespace system {
45 
46 int cpu_clock_by_os(void) {
47  long long result = -1;
48  size_t size = sizeof(result);
49  if (sysctlbyname("hw.cpufrequency", &result, &size, NULL, 0)) return -1;
50  return (int)(result / (long long)1000000);
51 }
52 } // namespace system
53 } // namespace cloudViewer
54 #else
55 #include <cstdio>
56 #include <cstdlib>
57 #include <cstring>
58 namespace cloudViewer {
59 namespace system {
60 
61 /* Assuming Linux with /proc/cpuinfo */
62 int cpu_clock_by_os(void) {
63  FILE *f;
64  char line[1024], *s;
65  int result;
66 
67  f = fopen("/proc/cpuinfo", "rt");
68  if (!f) return -1;
69 
70  while (fgets(line, sizeof(line), f)) {
71  if (!strncmp(line, "cpu MHz", 7)) {
72  s = strchr(line, ':');
73  if (s && 1 == sscanf(s, ":%d.", &result)) {
74  fclose(f);
75  return result;
76  }
77  }
78  }
79  fclose(f);
80  return -1;
81 }
82 } // namespace system
83 } // namespace cloudViewer
84 #endif /* CV_MAC_OS */
85 #endif /* CV_WINDOWS */
86 
87 /* get_total_cpus() system specific code: uses OS routines to determine total
88  * number of CPUs */
89 #ifdef CV_MAC_OS
90 #include <mach/clock.h>
91 #include <mach/clock_types.h>
92 #include <mach/mach.h>
93 #include <unistd.h>
94 namespace cloudViewer {
95 namespace system {
96 
97 int get_total_cpus(void) {
98  kern_return_t kr;
99  host_basic_info_data_t basic_info;
100  host_info_t info = (host_info_t)&basic_info;
101  host_flavor_t flavor = HOST_BASIC_INFO;
102  mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
103  kr = host_info(mach_host_self(), flavor, info, &count);
104  if (kr != KERN_SUCCESS) return 1;
105  return basic_info.avail_cpus;
106 }
107 } // namespace system
108 } // namespace cloudViewer
109 #define GET_TOTAL_CPUS_DEFINED
110 #endif
111 
112 #ifdef CV_WINDOWS
113 #include <windows.h>
114 namespace cloudViewer {
115 namespace system {
116 
117 int get_total_cpus(void) {
118  SYSTEM_INFO system_info;
119  GetSystemInfo(&system_info);
120  return system_info.dwNumberOfProcessors;
121 }
122 } // namespace system
123 } // namespace cloudViewer
124 #define GET_TOTAL_CPUS_DEFINED
125 #endif
126 
127 #if defined CV_LINUX
128 #include <sys/sysinfo.h>
129 #include <unistd.h>
130 namespace cloudViewer {
131 namespace system {
132 
133 int get_total_cpus(void) { return sysconf(_SC_NPROCESSORS_ONLN); }
134 } // namespace system
135 } // namespace cloudViewer
136 #define GET_TOTAL_CPUS_DEFINED
137 #endif
138 
139 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || \
140  defined __bsdi__ || defined __QNX__
141 #include <sys/sysctl.h>
142 #include <sys/types.h>
143 namespace cloudViewer {
144 namespace system {
145 
146 int get_total_cpus(void) {
147  int mib[2] = {CTL_HW, HW_NCPU};
148  int ncpus;
149  size_t len = sizeof(ncpus);
150  if (sysctl(mib, 2, &ncpus, &len, (void *)0, 0) != 0) return 1;
151  return ncpus;
152 }
153 } // namespace system
154 } // namespace cloudViewer
155 #define GET_TOTAL_CPUS_DEFINED
156 #endif
157 
158 #ifndef GET_TOTAL_CPUS_DEFINED
159 namespace cloudViewer {
160 namespace system {
161 
162 int get_total_cpus(void) { return 1; }
163 } // namespace system
164 } // namespace cloudViewer
165 
166 #endif /* GET_TOTAL_CPUS_DEFINED */
int size
int count
#define NULL
core::Tensor result
Definition: VtkUtils.cpp:76
int get_total_cpus()
Returns the total number of CPUs.
Definition: Cpu.cpp:133
int cpu_clock_by_os()
Returns the CPU clock, as reported by the OS.
Definition: Cpu.cpp:62
Generic file read and write utility for python interface.