Skip to content

Commit

Permalink
Add OS Info script for macos
Browse files Browse the repository at this point in the history
 * Add a `cpu_info_mac.sh` that gets hardware info on macos
 * Modify `run.sh` and `run-mac-arm-efficient-cores.sh` to call
   `cpu_info_mac.sh` if run on macos
 * Add a simple untility (`utilities/cpu_freq.c`) to attempt to detect
   current CPU Frequency if OS doesn't expose that information at all or
   without root privilegies (e.x. M1 chips on mac). Currently only
   tested on arm64@macos and amd64@linux.
  • Loading branch information
Civil committed Jan 2, 2022
1 parent fee0e2b commit c1818db
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
**/memSpeed.txt
**/whets.txt
**/pi*.txt
**/cpu_freq
152 changes: 76 additions & 76 deletions src/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

current_arch=$(uname -m)
current_uname=$(uname)
cpus_count=$(getconf _NPROCESSORS_ONLN)
cpus_count=$(getconf "_NPROCESSORS_ONLN")

if [[ ${BASH_VERSINFO[0]} -lt 5 ]]; then
echo "WARNING! Using Bash older than 5.0 is not supported!!!!"
echo "This script might fail to run"
sleep 30
echo "WARNING! Using Bash older than 5.0 is not supported!!!!"
echo "This script might fail to run"
sleep 30
fi

# List of files to compile, with binary specific flags
declare -A binaryCompileOptions
extraLinkerOptions=""
coremarkPlatform="posix"
case ${current_uname} in
Darwin)
;;
*)
extraLinkerOptions="-lrt"
Darwin) ;;

*)
extraLinkerOptions="-lrt"
;;
esac
CPUID_FLAGS="benchmarks/common/cpuidc64.c -Ibenchmarks/common/"
binaryCompileOptions["dhrystone"]="benchmarks/generic/dhrystone/dhry_1.c benchmarks/generic/dhrystone/dhry_2.c ${CPUID_FLAGS} ${extraLinkerOptions}"
Expand All @@ -36,26 +37,26 @@ binaryCompileOptions["SuperPI"]="benchmarks/generic/SuperPI/pi_fftcs.c benchmark
coremarkCompileOptions="-Ibenchmarks/generic/coremark/${coremarkPlatform} -Ibenchmarks/generic/coremark -DPERFORMANCE_RUN=1 -DUSE_FORK=1 ${extraLinkerOptions} benchmarks/generic/coremark/core_list_join.c benchmarks/generic/coremark/core_main.c benchmarks/generic/coremark/core_matrix.c benchmarks/generic/coremark/core_state.c benchmarks/generic/coremark/core_util.c benchmarks/generic/coremark/${coremarkPlatform}/core_portme.c"

for cores in 1 2 4 8; do
binaryCompileOptions["coremark_mp${cores}"]="-DMULTITHREAD=${cores} ${coremarkCompileOptions}"
binaryCompileOptions["coremark_mp${cores}"]="-DMULTITHREAD=${cores} ${coremarkCompileOptions}"
done

if (( cpus_count > 8 )); then
binaryCompileOptions["coremark_mp${cpus_count}"]="-DMULTITHREAD=${cpus_count} ${coremarkCompileOptions}"
if ((cpus_count > 8)); then
binaryCompileOptions["coremark_mp${cpus_count}"]="-DMULTITHREAD=${cpus_count} ${coremarkCompileOptions}"
fi

binaryCompileOptions["scimark2"]="-Ibenchmarks/generic/scimark2 benchmarks/generic/scimark2/*.c -lm"

if [[ ${current_arch} == "x86_64" ]]; then
current_arch="amd64"
current_arch="amd64"
fi

if [[ ${current_arch} == "i386" ]]; then
current_arch="i386"
current_arch="i386"
fi

os_name="${current_uname}"
if [[ ${current_uname} == "Darwin" ]]; then
os_name="mac"
os_name="mac"
fi

echo "Arch: ${current_arch}"
Expand All @@ -69,73 +70,72 @@ optFlags="-O2 -O3 -Ofast"
declare -A targetToFlags
declare -A targetToFPU
declare -A binaryExtraArgs
declare -A platformSpecificCompileOptions

binaryExtraArgs["SuperPI"]="$((1<<22))"
binaryExtraArgs["SuperPI"]="$((1 << 22))"

if [[ ${#} -eq 1 ]]; then
if [[ ${1} == "list" ]] || [[ ${1} == "ls" ]]; then
echo "Available benchmarks: ${!binaryCompileOptions[@]}"
exit 0;
fi
if [[ ${1} == "list" ]] || [[ ${1} == "ls" ]]; then
echo "Available benchmarks: " "${!binaryCompileOptions[@]}"
exit 0
fi
fi



compile_binary() {
ARCH=${1}
BINARY=${2}
SUFFIX=${3}
CC=${4:-cc}

# Special case to preserve the name of original dir name for amd64
output_dir="../bin/${os_name}/${current_arch}"
if [[ ${CC} != "cc" ]]; then
output_dir="${output_dir}_${CC}"
fi
mkdir -p ${output_dir}

echo -e "\033[0;32mCompiling ${BINARY} for ${ARCH} (${current_arch})\033[0m\n"
FPUS="${targetToFPU[${ARCH}]}"
MTUNES="${targetToFlags[${ARCH}]}"
for FPU in "" ${FPUS}; do
for MTUNE in "" ${MTUNES}; do
EXTRA_CFLAGS="${MTUNE} ${FPU}"
for OPT in ${optFlags}; do
BINARY_NAME="${BINARY}_${SUFFIX}-${ARCH}${OPT//-/_}${FPU//-mfpu=/_}$(sed 's/^-m[a-z]\+=//' <<< ${MTUNE})"
echo -e "\033[0;36mCompiling ${BINARY_NAME}\033[0m"
declare -a BINARY_EXTRA_FLAGS
if [[ ${BINARY_NAME} =~ ^coremark ]]; then
BINARY_EXTRA_FLAGS[0]="-DFLAGS_STR=\"${OPT} ${EXTRA_CFLAGS} -DPERFORMANCE_RUN=1 -DUSE_FORK=1 -lrt\""
fi
case "${BINARY_NAME}" in
STREAM*)
if [[ -z ${OPENMP_CC} ]]; then
echo "WARNING!!!! building STREAM benchmark might fail unless OPENMP_CC environment variable"
echo "points to openmp compatible compiler. In most Linux distros that should be ok to use default"
echo "compiler, howerever on MacOS clang must be installed from somewhere else, e.x. brew nix or "
echo "other package manager. Please note you must also provide OPENMP_LINK variable that would "
echo "point to library dir where openmp libs are on OSX it should point to where libomp is "
echo "installed, e.x. /opt/homebrew/Cellar/libomp/13.0.0"
echo
echo "Example:"
echo 'export OPENMP_CC="/opt/homebrew/Cellar/llvm/13.0.0_2/bin/clang"'
echo 'export OPENMP_LINK="-L/opt/homebrew/Cellar/libomp/13.0.0/lib"'
sleep 10
echo "Will try to compile using ${CC} anyway..."
OPENMP_CC="${CC}"
fi
${OPENMP_CC} ${OPENMP_LINK} ${platformSpecificCompileOptions[${BINARY}]} -Ibenchmarks/common ${binaryCompileOptions[${BINARY}]} "${BINARY_EXTRA_FLAGS[@]}" -o ${output_dir}/${BINARY_NAME} ${OPT} ${EXTRA_CFLAGS} -D options="\"${current_arch} ${ARCH} optimized\"" || continue
;;
gsynth*)
"${CC}" ${binaryCompileOptions[${BINARY}]} "${BINARY_EXTRA_FLAGS[@]}" -o "${output_dir}/${BINARY_NAME}" ${OPT} ${EXTRA_CFLAGS}
;;
*)
"${CC}" ${binaryCompileOptions[${BINARY}]} "${BINARY_EXTRA_FLAGS[@]}" -o "${output_dir}/${BINARY_NAME}" ${OPT} ${EXTRA_CFLAGS} -D options="\"${current_arch} ${ARCH} optimized\""
;;
esac
chmod +x ${output_dir}/${BINARY_NAME}
done
done
ARCH=${1}
BINARY=${2}
SUFFIX=${3}
CC=${4:-cc}

# Special case to preserve the name of original dir name for amd64
output_dir="../bin/${os_name}/${current_arch}"
if [[ ${CC} != "cc" ]]; then
output_dir="${output_dir}_${CC}"
fi
mkdir -p "${output_dir}"

echo -e "\033[0;32mCompiling ${BINARY} for ${ARCH} (${current_arch})\033[0m\n"
FPUS="${targetToFPU[${ARCH}]}"
MTUNES="${targetToFlags[${ARCH}]}"
for FPU in "" ${FPUS}; do
for MTUNE in "" ${MTUNES}; do
EXTRA_CFLAGS="${MTUNE} ${FPU}"
for OPT in ${optFlags}; do
BINARY_NAME="${BINARY}_${SUFFIX}-${ARCH}${OPT//-/_}${FPU//-mfpu=/_}$(sed 's/^-m[a-z]\+=//' <<< ${MTUNE})"
echo -e "\033[0;36mCompiling ${BINARY_NAME}\033[0m"
declare -a BINARY_EXTRA_FLAGS
if [[ ${BINARY_NAME} =~ ^coremark ]]; then
BINARY_EXTRA_FLAGS[0]="-DFLAGS_STR=\"${OPT} ${EXTRA_CFLAGS} -DPERFORMANCE_RUN=1 -DUSE_FORK=1 -lrt\""
fi
case "${BINARY_NAME}" in
STREAM*)
if [[ -z ${OPENMP_CC} ]]; then
echo "WARNING!!!! building STREAM benchmark might fail unless OPENMP_CC environment variable"
echo "points to openmp compatible compiler. In most Linux distros that should be ok to use default"
echo "compiler, howerever on MacOS clang must be installed from somewhere else, e.x. brew nix or "
echo "other package manager. Please note you must also provide OPENMP_LINK variable that would "
echo "point to library dir where openmp libs are on OSX it should point to where libomp is "
echo "installed, e.x. /opt/homebrew/Cellar/libomp/13.0.0"
echo
echo "Example:"
echo 'export OPENMP_CC="/opt/homebrew/Cellar/llvm/13.0.0_2/bin/clang"'
echo 'export OPENMP_LINK="-L/opt/homebrew/Cellar/libomp/13.0.0/lib"'
sleep 10
echo "Will try to compile using ${CC} anyway..."
OPENMP_CC="${CC}"
fi
${OPENMP_CC} ${OPENMP_LINK} ${platformSpecificCompileOptions[${BINARY}]} -Ibenchmarks/common ${binaryCompileOptions[${BINARY}]} "${BINARY_EXTRA_FLAGS[@]}" -o ${output_dir}/${BINARY_NAME} ${OPT} ${EXTRA_CFLAGS} -D options="\"${current_arch} ${ARCH} optimized\"" || continue
;;
gsynth*)
"${CC}" ${binaryCompileOptions[${BINARY}]} "${BINARY_EXTRA_FLAGS[@]}" -o "${output_dir}/${BINARY_NAME}" ${OPT} ${EXTRA_CFLAGS}
;;
*)
"${CC}" ${binaryCompileOptions[${BINARY}]} "${BINARY_EXTRA_FLAGS[@]}" -o "${output_dir}/${BINARY_NAME}" ${OPT} ${EXTRA_CFLAGS} -D options="\"${current_arch} ${ARCH} optimized\""
;;
esac
chmod +x "${output_dir}/${BINARY_NAME}"
done
done
echo
done
echo
}
57 changes: 57 additions & 0 deletions src/cpu_info_mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

source common.sh
RESULT_DIR="../results/${current_arch}"
mkdir -p "${RESULT_DIR}"

echo "Recording output of uname"
uname -mrsv > "${RESULT_DIR}/osx_uname.log"

echo "Obtaining sysctl information (hw and machdep only)"
sysctl -a | grep -E '^(hw|machdep)' > "${RESULT_DIR}/osx_sysctl.log"

echo "Obtaining sw_vers"
sw_vers > "${RESULT_DIR}/osx_swvers.log"

echo "Getting system_profiler information (serial numbers should be redacted out)"

system_profiler SPHardwareDataType SPDisplaysDataType SPMemoryDataType | awk '
{
if ($0 ~ "^$") {next}
key=$1" "$2
if (key ~ "Serial Number|Hardware UUID|Provisioning UDID") {next}
print $0
}' > "${RESULT_DIR}/osx_hwinfo.log"

echo "Getting information from current compiler"
cc --version >> "${RESULT_DIR}/osx_compiler.log"

# compile test code and do heruistics to detect cpu frequency
echo "Compiling code to detect CPU frequency..."
cc -ocpu_freq -std=gnu99 -Wall -Wextra -O2 utilities/cpu_freq.c -lpthread
echo "Running tool to attempt to detect real sustained frequency... This might take a while..."
echo "To make results more accurate please don't touch your computer while tools are running"
if [[ ${current_arch} == "arm64" ]]; then
EFFECTIVE_CORES=$(sysctl hw.perflevel1.physicalcpu | awk '{print $2}')
PERFORMANCE_CORES=$(sysctl hw.perflevel0.physicalcpu | awk '{print $2}')
rm -f "${RESULT_DIR}/osx_freq_effective_cores.log"
for i in $(seq 1 "${EFFECTIVE_CORES}"); do
echo "Running code to detect CPU for ${i} effective cores"
taskpolicy -b -t 0 -l 0 ./cpu_freq "${i}" >> "${RESULT_DIR}/osx_freq_effective_cores.log"
echo >> "${RESULT_DIR}/osx_freq_effective_cores.log"
done
rm -f "${RESULT_DIR}/osx_freq_performance_cores.log"
for i in $(seq 1 "${PERFORMANCE_CORES}"); do
echo "Running code to detect CPU for ${i} performance cores"
taskpolicy -t 0 -l 0 ./cpu_freq "${i}" >> "${RESULT_DIR}/osx_freq_performance_cores.log"
echo >> "${RESULT_DIR}/osx_freq_performance_cores.log"
done
else
CORES=$(sysctl hw.activecpu | awk '{print $2}')
rm -f "${RESULT_DIR}/osx_freq.log"
for i in $(seq 1 "${CORES}"); do
echo "Running code to detect CPU frequency for ${i} cores"
taskpolicy -t 0 -l 0 ./cpu_freq "${i}" >> "${RESULT_DIR}/osx_freq.log"
echo >> "${RESULT_DIR}/osx_freq.log"
done
fi
2 changes: 2 additions & 0 deletions src/run-mac-arm-efficient-cores.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ source common.sh
RESULT_DIR="../results/${current_arch}"
mkdir -p "${RESULT_DIR}"

./cpu_info_mac.sh

BIN_DIR="../bin/${os_name}/${current_arch}"
for BINARY in $(ls ${BIN_DIR}/); do
[[ ! -x "${BIN_DIR}/${BINARY}" ]] && continue ||:
Expand Down
4 changes: 4 additions & 0 deletions src/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ source common.sh
RESULT_DIR="../results/${current_arch}"
mkdir -p "${RESULT_DIR}"

if [[ ${os_name} == "mac" ]]; then
./cpu_info_mac.sh
fi

BIN_DIR="../bin/${os_name}/${current_arch}"
for BINARY in $(ls ${BIN_DIR}/); do
[[ ! -x "${BIN_DIR}/${BINARY}" ]] && continue ||:
Expand Down
Loading

0 comments on commit c1818db

Please sign in to comment.