MCPcopy Create free account
hub / github.com/cameron314/concurrentqueue / getCPUString

Function getCPUString

benchmarks/cpuid.cpp:150–213  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148namespace moodycamel
149{
150 const char* getCPUString()
151 {
152 // TODO: Support non-x86/-x64 architectures
153#ifdef MOODYCAMEL_X86_OR_X64
154 static char buf[128] = { 0 };
155 if (buf[0] != 0) {
156 return buf;
157 }
158
159 CPUIDInfo info = cpuid(0x80000000);
160 std::uint32_t ex = info.data[0];
161 for (std::uint32_t i = 0; i + 0x80000002 <= ex && i != 3; ++i) {
162 *(reinterpret_cast<CPUIDInfo*>(buf) + i) = cpuid(i + 0x80000002);
163 }
164
165 if (buf[0] == 0) {
166 strcpy(buf, UNKNOWN_CPU_STRING);
167 return buf;
168 }
169
170 info = cpuid(0);
171 if (info.data[0] < 1) {
172 // cpuid(1) not supported
173 return buf;
174 }
175
176 // Add number of CPUs, cores, HT, and GHz
177 info = cpuid(1);
178 bool ht = ((info.data[3] >> 28) & 1) == 1; // Note: This is also 1 on most multi-core systems, even if there's no HT
179 int cpus, cores, logicalCores;
180 double clockSpeed;
181 if (!getProcessorInfoFromOS(cpus, cores, logicalCores, clockSpeed)) {
182 return buf;
183 }
184 // Strip @ nGHz if any, since we re-calculate this ourselves
185 int atIndex;
186 for (atIndex = (int)std::strlen(buf) - 1; atIndex != -1; --atIndex) {
187 if (buf[atIndex] == '@') {
188 if (atIndex > 0 && buf[atIndex - 1] == ' ') {
189 --atIndex;
190 }
191 buf[atIndex] = '\0';
192 break;
193 }
194 }
195 // Strip trailing spaces if any
196 for (char* s = buf + std::strlen(buf); s != buf && s[-1] == ' '; --s)
197 s[-1] = '\0';
198 char* str = buf + std::strlen(buf);
199 if (cpus > 1) {
200 // Assume identical CPUs
201 logicalCores /= cpus;
202 cores /= cpus;
203 std::sprintf(str, " x%d", cpus);
204 str += strlen(str);
205 }
206 ht = ht && logicalCores != cores;
207 std::sprintf(str, " with %d core%s%s @ %.1fGHz%s", cores, cores == 1 ? "" : "s", ht ? " (HyperThreaded)" : "", clockSpeed, cpus > 1 ? " each" : "");

Callers 1

mainFunction · 0.85

Calls 2

cpuidFunction · 0.85
getProcessorInfoFromOSFunction · 0.85

Tested by

no test coverage detected