Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux build script and some platform code fixes for Linux. #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions perfaware/part2/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

if [ -z "${CXX}" ]; then
echo "WARNING: Environment variable CXX not set."
exit
fi
# Check if `${CXX}` is in the PATH
if ! command -v ${CXX} &> /dev/null; then
echo "WARNING: ${CXX} not found -- executables will not be built"
echo "Set CXX environment variable to a working C++ compiler."
exit
fi

# Set buildpat based on the first script argument
if [ -z "$1" ]; then
for g in ./*_main.cpp ; do
./build_single.sh "$g"
done
else
for g in ./*$1*_main.cpp ; do
./build_single.sh "$g"
done
fi
19 changes: 19 additions & 0 deletions perfaware/part2/build_asm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Check if `nasm` is in the PATH
if ! command -v nasm &> /dev/null; then
echo "WARNING: nasm not found -- ASM libs will not be built"
fi

# Set buildpat based on the first script argument
if [ -z "$1" ]; then
buildpat="*.asm"
for g in ./*asm ; do
./build_single_asm.sh "$g"
done
else
for g in ./*$1*.asm ; do
./build_single_asm.sh "$g"
done
fi

22 changes: 22 additions & 0 deletions perfaware/part2/build_single.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -x

# Create 'build' directory if it does not exist
if [ ! -d build ]; then
mkdir build
fi
pushd build

# Check if `clang++` is in the PATH and compile with `clang++` if available
if command -v ${CXX} &> /dev/null; then
# Extract the filename without extension from the first argument
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"

# Compile with clang++
${CXX} -mavx2 -g -Wall -fuse-ld=lld ../"$1" -L . -o "${filename}_dc"
${CXX} -mavx2 -O3 -g -Wall -fuse-ld=lld ../"$1" -L . -o "${filename}_rc"
fi

popd

24 changes: 24 additions & 0 deletions perfaware/part2/build_single_asm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Create 'build' directory if it does not exist
if [ ! -d build ]; then
mkdir build
fi
pushd build

# Check if `nasm` is in the PATH and compile with `nasm` if available
if command -v nasm &> /dev/null; then
# Extract the filename without extension from the first argument
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"

# Compile with nasm
nasm -f elf64 -o "${filename}.o" ../"$1"

# Create a static library from the object file, using 'ar' as a likely substitute for 'lib'
ar rcs "${filename}.a" "${filename}.o"
fi

popd

23 changes: 23 additions & 0 deletions perfaware/part3/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

if [ -z "${CXX}" ]; then
echo "WARNING: Environment variable CXX not set."
exit
fi
# Check if `${CXX}` is in the PATH
if ! command -v ${CXX} &> /dev/null; then
echo "WARNING: ${CXX} not found -- executables will not be built"
echo "Set CXX environment variable to a working C++ compiler."
exit
fi

# Set buildpat based on the first script argument
if [ -z "$1" ]; then
for g in ./*_main.cpp ; do
./build_single.sh "$g"
done
else
for g in ./*$1*_main.cpp ; do
./build_single.sh "$g"
done
fi
19 changes: 19 additions & 0 deletions perfaware/part3/build_asm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Check if `nasm` is in the PATH
if ! command -v nasm &> /dev/null; then
echo "WARNING: nasm not found -- ASM libs will not be built"
fi

# Set buildpat based on the first script argument
if [ -z "$1" ]; then
buildpat="*.asm"
for g in ./*asm ; do
./build_single_asm.sh "$g"
done
else
for g in ./*$1*.asm ; do
./build_single_asm.sh "$g"
done
fi

22 changes: 22 additions & 0 deletions perfaware/part3/build_single.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -x

# Create 'build' directory if it does not exist
if [ ! -d build ]; then
mkdir build
fi
pushd build

# Check if `clang++` is in the PATH and compile with `clang++` if available
if command -v ${CXX} &> /dev/null; then
# Extract the filename without extension from the first argument
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"

# Compile with clang++
${CXX} -mavx2 -g -Wall -fuse-ld=lld ../"$1" -L . -o "${filename}_dc"
${CXX} -mavx2 -O3 -g -Wall -fuse-ld=lld ../"$1" -L . -o "${filename}_rc"
fi

popd

24 changes: 24 additions & 0 deletions perfaware/part3/build_single_asm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Create 'build' directory if it does not exist
if [ ! -d build ]; then
mkdir build
fi
pushd build

# Check if `nasm` is in the PATH and compile with `nasm` if available
if command -v nasm &> /dev/null; then
# Extract the filename without extension from the first argument
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"

# Compile with nasm
nasm -f elf64 -o "${filename}.o" ../"$1"

# Create a static library from the object file, using 'ar' as a likely substitute for 'lib'
ar rcs "${filename}.a" "${filename}.o"
fi

popd

7 changes: 6 additions & 1 deletion perfaware/part3/listing_0102_read_overhead_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
LISTING 102
======================================================================== */

#if _WIN32
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#endif
#include <fcntl.h>

struct read_parameters
{
Expand Down Expand Up @@ -57,6 +59,8 @@ static void ReadViaFRead(repetition_tester *Tester, read_parameters *Params)
}
}

#if _WIN32

static void ReadViaRead(repetition_tester *Tester, read_parameters *Params)
{
while(IsTesting(Tester))
Expand Down Expand Up @@ -149,3 +153,4 @@ static void ReadViaReadFile(repetition_tester *Tester, read_parameters *Params)
}
}
}
#endif
2 changes: 2 additions & 0 deletions perfaware/part3/listing_0104_read_overhead_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ struct test_function
test_function TestFunctions[] =
{
{"fread", ReadViaFRead},
#if _WIN32
{"_read", ReadViaRead},
{"ReadFile", ReadViaReadFile},
#endif
};

int main(int ArgCount, char **Args)
Expand Down
1 change: 1 addition & 0 deletions perfaware/part3/listing_0108_platform_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static void InitializeOSMetrics(void)

#include <x86intrin.h>
#include <sys/time.h>
#include <sys/resource.h>

static u64 GetOSTimerFreq(void)
{
Expand Down
11 changes: 9 additions & 2 deletions perfaware/part3/listing_0109_pagefault_repetition_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ static void PrintValue(char const *Label, repetition_value Value, u64 CPUTimerFr
if(CPUTimerFreq)
{
f64 Seconds = SecondsFromCPUTime(E[RepValue_CPUTimer], CPUTimerFreq);
printf(" (%fms)", 1000.0f*Seconds);
printf(" (%8.3fms)", 1000.0f*Seconds);

if(E[RepValue_ByteCount] > 0)
{
f64 Gigabyte = (1024.0f * 1024.0f * 1024.0f);
f64 Bandwidth = E[RepValue_ByteCount] / (Gigabyte * Seconds);
printf(" %fgb/s", Bandwidth);
printf(" %9.4fgb/s", Bandwidth);
}
}

Expand Down Expand Up @@ -146,6 +146,7 @@ static void NewTestWave(repetition_tester *Tester, u64 TargetProcessedByteCount,

Tester->TryForTime = SecondsToTry*CPUTimerFreq;
Tester->TestsStartedAt = ReadCPUTimer();
//printf("\n"); // Move to the next line to start the live Min reporting.
}

static void BeginTime(repetition_tester *Tester)
Expand Down Expand Up @@ -215,8 +216,14 @@ static b32 IsTesting(repetition_tester *Tester)

if(Tester->PrintNewMinimums)
{
#if !_WIN32
printf("\033[2K\r");
#endif
PrintValue("Min", Results->Min, Tester->CPUTimerFreq);
#if _WIN32
printf(" \r");
#endif
fflush(stdout);
}
}

Expand Down
3 changes: 3 additions & 0 deletions perfaware/part3/listing_0126_os_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ static void InitializeOSPlatform(void)

#include <x86intrin.h>
#include <sys/time.h>
#include <sys/resource.h>

struct os_platform
{
b32 Initialized;
u64 CPUTimerFreq;
u64 LargePageSize; // NOTE(casey): This will be 0 when large pages are not supported (which is most of the time!)
};
static os_platform GlobalOSPlatform;

Expand Down Expand Up @@ -143,6 +145,7 @@ static void InitializeOSPlatform(void)
{
GlobalOSPlatform.Initialized = true;
GlobalOSPlatform.CPUTimerFreq = EstimateCPUTimerFreq();
GlobalOSPlatform.LargePageSize = 2048 * 1024;
}
}

Expand Down
12 changes: 10 additions & 2 deletions perfaware/part3/listing_0137_os_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,18 @@ static void InitializeOSPlatform(void)

#include <x86intrin.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>

struct os_platform
{
b32 Initialized;
u64 CPUTimerFreq;
u64 LargePageSize; // NOTE(casey): This will be 0 when large pages are not supported (which is most of the time!)
};
static os_platform GlobalOSPlatform;

Expand Down Expand Up @@ -167,7 +174,7 @@ static b32 ReadOSRandomBytes(u64 Count, void *Dest)
// and do multiple read()'s to make sure you filled the entire buffer.

int DevRandom = open("/dev/urandom", O_RDONLY);
b32 Result = (read(DevRandom, Dest.Data, Dest.Count) == Count);
b32 Result = (read(DevRandom, Dest, Count) == Count);
close(DevRandom);

return Result;
Expand All @@ -179,6 +186,7 @@ static void InitializeOSPlatform(void)
{
GlobalOSPlatform.Initialized = true;
GlobalOSPlatform.CPUTimerFreq = EstimateCPUTimerFreq();
GlobalOSPlatform.LargePageSize = 2048 * 1024;
}
}

Expand Down Expand Up @@ -252,4 +260,4 @@ inline void FillWithRandomBytes(buffer Dest)
ReadOSRandomBytes(ReadCount, Dest.Data + AtOffset);
AtOffset += ReadCount;
}
}
}