added Environment::processorCount()

This commit is contained in:
Guenter Obiltschnig 2009-06-01 08:50:54 +00:00
parent 589de05652
commit 60fc5b6e28
10 changed files with 103 additions and 20 deletions

View File

@ -1,7 +1,7 @@
//
// Environment.h
//
// $Id: //poco/1.3/Foundation/include/Poco/Environment.h#2 $
// $Id: //poco/1.3/Foundation/include/Poco/Environment.h#3 $
//
// Library: Foundation
// Package: Core
@ -94,6 +94,11 @@ public:
/// of the first Ethernet adapter found on the system.
///
/// Throws a SystemException if no Ethernet adapter is available.
static unsigned processorCount();
/// Returns the number of processors installed in the system.
///
/// If the number of processors cannot be determined, returns 1.
};

View File

@ -1,7 +1,7 @@
//
// Environment_UNIX.h
//
// $Id: //poco/1.3/Foundation/include/Poco/Environment_UNIX.h#2 $
// $Id: //poco/1.3/Foundation/include/Poco/Environment_UNIX.h#3 $
//
// Library: Foundation
// Package: Core
@ -61,6 +61,7 @@ public:
static std::string osArchitectureImpl();
static std::string nodeNameImpl();
static void nodeIdImpl(NodeId& id);
static unsigned processorCountImpl();
private:
typedef std::map<std::string, std::string> StringMap;

View File

@ -1,7 +1,7 @@
//
// Environment_VMS.h
//
// $Id: //poco/1.3/Foundation/include/Poco/Environment_VMS.h#2 $
// $Id: //poco/1.3/Foundation/include/Poco/Environment_VMS.h#3 $
//
// Library: Foundation
// Package: Core
@ -60,6 +60,7 @@ public:
static std::string osArchitectureImpl();
static std::string nodeNameImpl();
static void nodeIdImpl(NodeId& id);
static unsigned processorCountImpl();
static std::string getsyi(unsigned short code);
/// a wrapper for $GETSYIW

View File

@ -1,7 +1,7 @@
//
// Environment_WIN32.h
//
// $Id: //poco/1.3/Foundation/include/Poco/Environment_WIN32.h#2 $
// $Id: //poco/1.3/Foundation/include/Poco/Environment_WIN32.h#3 $
//
// Library: Foundation
// Package: Core
@ -59,6 +59,7 @@ public:
static std::string osArchitectureImpl();
static std::string nodeNameImpl();
static void nodeIdImpl(NodeId& id);
static unsigned processorCountImpl();
};

View File

@ -1,7 +1,7 @@
//
// Environment_WIN32U.h
//
// $Id: //poco/1.3/Foundation/include/Poco/Environment_WIN32U.h#2 $
// $Id: //poco/1.3/Foundation/include/Poco/Environment_WIN32U.h#3 $
//
// Library: Foundation
// Package: Core
@ -59,6 +59,7 @@ public:
static std::string osArchitectureImpl();
static std::string nodeNameImpl();
static void nodeIdImpl(NodeId& id);
static unsigned processorCountImpl();
};

View File

@ -1,7 +1,7 @@
//
// Environment.cpp
//
// $Id: //poco/1.3/Foundation/src/Environment.cpp#2 $
// $Id: //poco/1.3/Foundation/src/Environment.cpp#4 $
//
// Library: Foundation
// Package: Core
@ -36,7 +36,8 @@
#include "Poco/Environment.h"
#include <cstdlib>
#include <cstdio>
#include <cstdio> // sprintf()
#if defined(POCO_OS_FAMILY_VMS)
#include "Environment_VMS.cpp"
@ -125,4 +126,10 @@ void Environment::nodeId(NodeId& id)
}
unsigned Environment::processorCount()
{
return EnvironmentImpl::processorCountImpl();
}
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// Environment_UNIX.cpp
//
// $Id: //poco/1.3/Foundation/src/Environment_UNIX.cpp#4 $
// $Id: //poco/1.3/Foundation/src/Environment_UNIX.cpp#7 $
//
// Library: Foundation
// Package: Core
@ -36,10 +36,18 @@
#include "Poco/Environment_UNIX.h"
#include "Poco/Exception.h"
#include <cstring>
#include <unistd.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <sys/param.h>
#include <cstring>
#if defined(POCO_OS_FAMILY_BSD)
#include <sys/sysctl.h>
#elif POCO_OS == POCO_OS_HPUX
#include <pthread.h>
#endif
namespace Poco {
@ -117,6 +125,27 @@ std::string EnvironmentImpl::nodeNameImpl()
}
unsigned EnvironmentImpl::processorCountImpl()
{
#if defined(POCO_OS_FAMILY_BSD)
unsigned count;
std::size_t size = sizeof(count);
if (sysctlbyname("hw.ncpu", &count, &size, 0, 0))
return 1;
else
return count;
#elif POCO_OS == POCO_OS_HPUX
return pthread_num_processors_np();
#elif defined(_SC_NPROCESSORS_ONLN)
int count = sysconf(_SC_NPROCESSORS_ONLN);
if (count <= 0) count = 1;
return static_cast<int>(count);
#else
return 1;
#endif
}
} // namespace Poco
@ -188,7 +217,7 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
int s = socket(PF_INET, SOCK_DGRAM, 0);
if (s == -1) throw SystemException("cannot open socket");
strcpy(ifr.ifr_name, "eth0");
std::strcpy(ifr.ifr_name, "eth0");
int rc = ioctl(s, SIOCGIFHWADDR, &ifr);
close(s);
if (rc < 0) throw SystemException("cannot get MAC address");
@ -205,23 +234,20 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
// General Unix
//
#include <sys/ioctl.h>
#if defined(sun) || defined(__sun) || defined(__sun__)
# ifndef __EXTENSIONS__
# define __EXTENSIONS__
# endif
# include <net/if_arp.h>
# include <sys/sockio.h>
# include <stropts.h>
#if defined(sun) || defined(__sun)
#include <sys/sockio.h>
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <unistd.h>
namespace Poco {

View File

@ -1,7 +1,7 @@
//
// Environment_VMS.cpp
//
// $Id: //poco/1.3/Foundation/src/Environment_VMS.cpp#2 $
// $Id: //poco/1.3/Foundation/src/Environment_VMS.cpp#3 $
//
// Library: Foundation
// Package: Core
@ -145,6 +145,31 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
}
unsigned EnvironmentImpl::processorCountImpl()
{
#pragma pointer_size save
#pragma pointer_size 32
Poco::UInt32 count;
unsigned short length;
ILE3 items[2];
items[0].ile3$w_code = SYI$_ACTIVECPU_CNT;
items[0].ile3$w_length = sizeof(count);
items[0].ile3$ps_bufaddr = &count;
items[0].ile3$ps_retlen_addr = &length;
items[1].ile3$w_code = 0;
items[1].ile3$w_length = 0;
if (sys$getsyiw(0, 0, 0, items, 0, 0, 0) == 1)
return count;
else
throw SystemException("$GETSYI failed");
#pragma pointer_size restore
}
std::string EnvironmentImpl::getsyi(unsigned short code)
{
#pragma pointer_size save

View File

@ -1,7 +1,7 @@
//
// Environment_WIN32.cpp
//
// $Id: //poco/Main/Foundation/src/Environment_WIN32.cpp#15 $
// $Id: //poco/1.3/Foundation/src/Environment_WIN32.cpp#6 $
//
// Library: Foundation
// Package: Core
@ -192,4 +192,12 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
}
unsigned EnvironmentImpl::processorCountImpl()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// Environment_WIN32U.cpp
//
// $Id: //poco/1.3/Foundation/src/Environment_WIN32U.cpp#4 $
// $Id: //poco/1.3/Foundation/src/Environment_WIN32U.cpp#5 $
//
// Library: Foundation
// Package: Core
@ -206,4 +206,12 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
}
unsigned EnvironmentImpl::processorCountImpl()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}
} // namespace Poco