+#include "../../lib/hweight.h"
+
+extern unsigned long mtime_since_now(struct timeval *);
+extern void fio_gettime(struct timeval *, void *);
+
+/* These aren't defined in the MinGW headers */
+HRESULT WINAPI StringCchCopyA(
+ char *pszDest,
+ size_t cchDest,
+ const char *pszSrc);
+
+HRESULT WINAPI StringCchPrintfA(
+ char *pszDest,
+ size_t cchDest,
+ const char *pszFormat,
+ ...);
+
+int vsprintf_s(
+ char *buffer,
+ size_t numberOfElements,
+ const char *format,
+ va_list argptr);
+
+int GetNumLogicalProcessors(void)
+{
+ SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
+ DWORD len = 0;
+ DWORD num_processors = 0;
+ DWORD error = 0;
+ DWORD i;
+
+ while (!GetLogicalProcessorInformation(processor_info, &len)) {
+ error = GetLastError();
+ if (error == ERROR_INSUFFICIENT_BUFFER)
+ processor_info = malloc(len);
+ else {
+ log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
+ return -1;
+ }
+
+ if (processor_info == NULL) {
+ log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
+ return -1;
+ }
+ }
+
+ for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++)
+ {
+ if (processor_info[i].Relationship == RelationProcessorCore)
+ num_processors += hweight64(processor_info[i].ProcessorMask);
+ }
+
+ free(processor_info);
+ return num_processors;
+}