#!/bin/sh -e _distfile=topo-12212009.tar _srcurl=http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/ if [ $# -ne 1 ]; then echo "Usage: $0 " echo echo "${_distfile} is downloadable from:" echo echo "${_srcurl}" exit 0 fi _srctar=$1 shift if [ ! -f ${_srctar} ]; then echo "${_srctar} not found" exit 1 fi _arch=`uname -p` case ${_arch} in amd64) _target=64 ;; i386) _target=32 ;; *) echo "Unsupported platform ${_arch}." exit 1 ;; esac _blddir=`realpath .` _srcdir="${_blddir}/cpu-topology" if [ `md5 -q ${_srctar}` != 2eb4cc3fe03572e56f18467c78ce13b6 ]; then echo "Bad checksum." exit 1 fi tar -x -f ${_srctar} -C ${_blddir} for f in ${_srcdir}/*.[chs]; do mv ${f} ${f}.orig tr -d '\r' < ${f}.orig > ${f} done patch -p0 -s << EOF --- ${_srcdir}/cpu_topo.c 2009-12-21 17:58:58.000000000 -0500 +++ ${_srcdir}/cpu_topo.c 2010-07-14 13:20:49.000000000 -0400 @@ -45,7 +45,7 @@ #include "cputopology.h" -#ifdef __linux__ +#if defined(__FreeBSD__) || defined(__linux__) #define _GNU_SOURCE @@ -53,7 +53,9 @@ #include #include #include +#ifdef __linux__ #include +#endif #include #define __cdecl --- ${_srcdir}/cputopology.h 2009-12-21 17:58:58.000000000 -0500 +++ ${_srcdir}/cputopology.h 2010-07-14 13:27:13.000000000 -0400 @@ -15,7 +15,7 @@ #ifndef TOPOLOGY_H__ #define TOPOLOGY_H__ -#ifdef __linux__ +#if defined(__FreeBSD__) || defined(__linux__) #ifndef __int64 #define __int64 long long #endif @@ -268,7 +268,7 @@ unsigned GetCoreCount(unsigned long pac unsigned GetThreadCount(unsigned long package_ordinal, unsigned long core_ordinal); void InitCpuTopology(); -unsigned BindContext(unsigned cpu); +int BindContext(unsigned cpu); void SetChkProcessAffinityConsistency(unsigned lcl_OSProcessorCount); void SetGenericAffinityBit(GenericAffinityMask *pAffinityMap, unsigned cpu); unsigned GetMaxCPUSupportedByOS(); --- ${_srcdir}/util_os.c 2009-12-21 17:58:58.000000000 -0500 +++ ${_srcdir}/util_os.c 2010-07-14 13:35:24.000000000 -0400 @@ -8,7 +8,7 @@ */ #include "cputopology.h" -#ifdef __linux__ +#if defined(__FreeBSD__) || defined(__linux__) #ifndef _GNU_SOURCE #define _GNU_SOURCE @@ -18,9 +18,16 @@ #include #include #include +#ifdef __linux__ #include +#endif #include +#ifdef __FreeBSD__ +#include +#include +#endif + #ifdef __CPU_ISSET #define MY_CPU_SET __CPU_SET #define MY_CPU_ZERO __CPU_ZERO @@ -104,7 +111,16 @@ static char scratch[BLOCKSIZE_4K]; // s */ int BindContext(unsigned int cpu) { int ret = -1; -#ifdef __linux__ +#if defined(__FreeBSD__) + cpuset_t currentCPU; + // add check for size of cpumask_t. + MY_CPU_ZERO(¤tCPU); + // turn on the equivalent bit inside the bitmap corresponding to affinitymask + MY_CPU_SET(cpu, ¤tCPU); + if ( !cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(currentCPU), ¤tCPU) ) + { ret = 0; + } +#elif defined(__linux__) cpu_set_t currentCPU; // add check for size of cpumask_t. MY_CPU_ZERO(¤tCPU); @@ -197,7 +213,7 @@ static char scratch[BLOCKSIZE_4K]; // s */ unsigned int GetMaxCPUSupportedByOS() {unsigned int lcl_OSProcessorCount = 0; -#ifdef __linux__ +#if defined(__FreeBSD__) || defined(__linux__) lcl_OSProcessorCount = sysconf(_SC_NPROCESSORS_CONF); //This will tell us how many CPUs are currently enabled. @@ -240,10 +256,17 @@ unsigned int GetMaxCPUSupportedByOS() */ void SetChkProcessAffinityConsistency(unsigned int lcl_OSProcessorCount) {unsigned int i, sum = 0; -#ifdef __linux__ +#if defined(__FreeBSD__) || defined(__linux__) +#ifdef __FreeBSD__ + cpuset_t allowedCPUs; + + cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, sizeof(allowedCPUs), &allowedCPUs); +#else cpu_set_t allowedCPUs; sched_getaffinity(0, sizeof(allowedCPUs), &allowedCPUs); +#endif + for (i = 0; i < lcl_OSProcessorCount; i++ ) { if ( MY_CPU_ISSET(i, &allowedCPUs) == 0 ) EOF cc -g -c ${_srcdir}/util_os.c -o ${_blddir}/util_os.o cc -g -c ${_srcdir}/get_cpuid_lix${_target}.s -o ${_blddir}/get_cpuid_lix${_target}.o cc -g -DBUILD_MAIN ${_srcdir}/cpu_topo.c -o ${_blddir}/cpu_topology${_target}.out ${_blddir}/get_cpuid_lix${_target}.o ${_blddir}/util_os.o rm -fr ${_srcdir} ${_blddir}/get_cpuid_lix${_target}.o ${_blddir}/util_os.o ${_blddir}/cpu_topology${_target}.out echo echo "You may delete ${_blddir}/cpu_topology${_target}.out now." exit 0