Skip to content

Commit 772a1ad

Browse files
committed
Fix CPU metrics and fmt integration
1 parent 0476bca commit 772a1ad

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

include/string/format.inl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ inline std::string format(fmt::format_string<T...> pattern, T&&... args)
1717
template <typename... T>
1818
inline std::wstring format(fmt::wformat_string<T...> pattern, T&&... args)
1919
{
20-
return fmt::vformat<wchar_t>(pattern, fmt::make_format_args<fmt::wformat_context>(args...));
20+
return fmt::vformat(pattern, fmt::make_format_args<fmt::wformat_context>(args...));
2121
}
2222

2323
template <typename... T>
@@ -29,7 +29,7 @@ inline void print(fmt::format_string<T...> pattern, T&&... args)
2929
template <typename... T>
3030
inline void print(fmt::wformat_string<T...> pattern, T&&... args)
3131
{
32-
return fmt::vprint<wchar_t>(pattern, fmt::make_format_args<fmt::wformat_context>(args...));
32+
return fmt::vprint(pattern, fmt::make_format_args<fmt::wformat_context>(args...));
3333
}
3434

3535
template <typename TOutputStream, typename... T>
@@ -41,7 +41,7 @@ inline void print(TOutputStream& stream, fmt::format_string<T...> pattern, T&&..
4141
template <typename TOutputStream, typename... T>
4242
inline void print(TOutputStream& stream, fmt::wformat_string<T...> pattern, T&&... args)
4343
{
44-
return fmt::vprint<wchar_t>(stream, pattern, fmt::make_format_args<fmt::wformat_context>(args...));
44+
return fmt::vprint(stream, pattern, fmt::make_format_args<fmt::wformat_context>(args...));
4545
}
4646

4747
} // namespace CppCommon

source/system/cpu.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
#if defined(__APPLE__)
1313
#include <sys/sysctl.h>
1414
#elif defined(unix) || defined(__unix) || defined(__unix__)
15+
#include <sched.h>
1516
#include <unistd.h>
1617
#include <fstream>
1718
#include <regex>
19+
#include <set>
1820
#elif defined(_WIN32) || defined(_WIN64)
1921
#include <windows.h>
2022
#endif
@@ -100,8 +102,12 @@ int CPU::Affinity()
100102

101103
return logical;
102104
#elif defined(unix) || defined(__unix) || defined(__unix__)
103-
long processors = sysconf(_SC_NPROCESSORS_ONLN);
104-
return processors;
105+
cpu_set_t cs;
106+
CPU_ZERO(&cs);
107+
if (sched_getaffinity(0, sizeof(cs), &cs) != 0)
108+
return -1;
109+
110+
return CPU_COUNT(&cs);
105111
#elif defined(_WIN32) || defined(_WIN64)
106112
SYSTEM_INFO si;
107113
GetSystemInfo(&si);
@@ -136,8 +142,22 @@ std::pair<int, int> CPU::TotalCores()
136142

137143
return std::make_pair(logical, physical);
138144
#elif defined(unix) || defined(__unix) || defined(__unix__)
139-
long processors = sysconf(_SC_NPROCESSORS_ONLN);
140-
return std::make_pair(processors, processors);
145+
static std::regex pattern("core id(.*): (.*)");
146+
147+
std::set<int> cores;
148+
149+
std::string line;
150+
std::ifstream stream("/proc/cpuinfo");
151+
while (getline(stream, line))
152+
{
153+
std::smatch matches;
154+
if (std::regex_match(line, matches, pattern))
155+
cores.insert(atoi(matches[2].str().c_str()));
156+
}
157+
158+
size_t logical = cores.size();
159+
long physical = sysconf(_SC_NPROCESSORS_ONLN);
160+
return std::make_pair(logical, physical);
141161
#elif defined(_WIN32) || defined(_WIN64)
142162
BOOL allocated = FALSE;
143163
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBuffer = nullptr;
@@ -202,6 +222,12 @@ int64_t CPU::ClockSpeed()
202222
if (sysctlbyname("hw.cpufrequency", &frequency, &size, nullptr, 0) == 0)
203223
return frequency;
204224

225+
// On Apple Silicon fallback to hw.tbfrequency and kern.clockrate.hz
226+
struct clockinfo clockrate;
227+
size_t clockrate_size = sizeof(clockrate);
228+
if ((sysctlbyname("hw.tbfrequency", &frequency, &frequency_size, NULL, 0) == 0) && (sysctlbyname("kern.clockrate", &clockrate, &clockrate_size, NULL, 0) == 0))
229+
return frequency * clockrate.hz;
230+
205231
return -1;
206232
#elif defined(unix) || defined(__unix) || defined(__unix__)
207233
static std::regex pattern("cpu MHz(.*): (.*)");

0 commit comments

Comments
 (0)