Skip to content

Commit

Permalink
Fix _SC_NPROCESSORS_ONLN on Windows
Browse files Browse the repository at this point in the history
_SC_NPROCESSORS_ONLN was returning the wrong value on Windows because
GetLogicalProcessorInformation() returns data about caches, nodes and
packages in addition to processors.

Modified by Jens to use hweight32()

Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Bruce Cran authored and axboe committed Jan 21, 2013
1 parent 51aa2da commit 671b060
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions os/windows/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sys/poll.h>

#include "../os-windows.h"
#include "../../lib/hweight.h"

extern unsigned long mtime_since_now(struct timeval *);
extern void fio_gettime(struct timeval *, void *);
Expand All @@ -42,20 +43,52 @@ int vsprintf_s(
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 += hweight32(processor_info[i].ProcessorMask);
}

free(processor_info);
return num_processors;
}

long sysconf(int name)
{
long long val = -1;
DWORD len;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION processorInfo;
long val = -1;
SYSTEM_INFO sysInfo;
MEMORYSTATUSEX status;

switch (name)
{
case _SC_NPROCESSORS_ONLN:
len = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
GetLogicalProcessorInformation(&processorInfo, &len);
val = len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
val = GetNumLogicalProcessors();
if (val == -1)
log_err("_SC_NPROCESSORS_ONLN failed\n");

break;

case _SC_PAGESIZE:
Expand Down

0 comments on commit 671b060

Please sign in to comment.