From 5e84e270bc3930038b9924f99d6ab47c235ca813 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 9 Jul 2023 04:01:56 +0100 Subject: [PATCH] Added macros for profiling functions and made it possible to compile the profiling code out --- haversine_02/compile | 85 ++++++++++++++++++++++----- haversine_02/include/profiler/timer.h | 16 +++++ haversine_02/src/processor/main.cpp | 34 ++++++----- haversine_02/src/profiler/timer.c | 2 - 4 files changed, 105 insertions(+), 32 deletions(-) diff --git a/haversine_02/compile b/haversine_02/compile index 11e3807..6f16bca 100644 --- a/haversine_02/compile +++ b/haversine_02/compile @@ -2,32 +2,89 @@ CC=clang CXX=clang++ -CFLAGS="-Wall -Wextra -Iinclude " +CFLAGS="-Wall -Wextra -I$(realpath ./include) " -if [[ "$1" == "release" ]]; then +# PARSE ARGUMENTS +# From this StackOverflow answer https://stackoverflow.com/a/14203146 +while [[ $# > 0 ]];do + case $1 in + --release) + RELEASE=true + shift + ;; + --enable-profiling) + ENABLE_PROFILING=true + shift + ;; + *|-*|--*) + echo "Unknown option $1" + exit 1 + ;; + esac +done + + +# BUILD TYPE +if [[ $RELEASE == true ]]; then CFLAGS+="-O3" else CFLAGS+="-g" fi -# profiler -PROFSRC="./src/profiler/timer.c" -PROFFLAGS="-c" -# generator -GENSRC="./src/generator/gen_argparser.cpp ./src/generator/generator.cpp ./src/haversine.cpp ./src/point_types.cpp ./src/generator/main.cpp" +# GENERATOR +GENSRC="./src/generator/gen_argparser.cpp \ + ./src/generator/generator.cpp \ + ./src/haversine.cpp \ + ./src/point_types.cpp \ + ./src/generator/main.cpp" GENOUT=genhavr (set -x ; $CXX $CFLAGS $GENSRC -o $GENOUT) +echo -# processor -JSONSRC="./src/json/*.c" + +# PROFILER +PROFSRC="../src/profiler/timer.c" +PROFFLAGS="-c" +PROF_BUILD_DIR=prof_build + +# PROCESSOR +JSONSRC="../src/json/*.c" JSONFLAGS="-c" -PROCSRC="./*.o ./src/haversine.cpp ./src/point_types.cpp ./src/processor/proc_argparser.cpp ./src/processor/main.cpp" +JSON_BUILD_DIR=json_build + +PROCSRC="./$JSON_BUILD_DIR/*.o \ + ./src/haversine.cpp \ + ./src/point_types.cpp \ + ./src/processor/proc_argparser.cpp \ + ./src/processor/main.cpp " PROCOUT=prochavr -(set -x ; $CC $CFLAGS $PROFFLAGS $PROFSRC) -(set -x ; $CC $CFLAGS $JSONFLAGS $JSONSRC) -(set -x ; $CXX $CFLAGS $PROCSRC -o $PROCOUT) +if [[ $ENABLE_PROFILING == true ]]; then + PROCSRC+="./$PROF_BUILD_DIR/*.o" + PROCFLAGS="-DENABLE_PROFILING" -rm ./*.o + mkdir $PROF_BUILD_DIR + cd $PROF_BUILD_DIR + + (set -x ; $CC $CFLAGS $PROFFLAGS $PROFSRC) + echo + + cd ../ +fi + +mkdir $JSON_BUILD_DIR +cd $JSON_BUILD_DIR + +(set -x ; $CC $CFLAGS $JSONFLAGS $JSONSRC) +echo + +cd ../ + +(set -x ; $CXX $CFLAGS $PROCFLAGS $PROCSRC -o $PROCOUT) +echo + + +# CLEAR BUILD FILES +rm -rvf $JSON_BUILD_DIR $PROF_BUILD_DIR diff --git a/haversine_02/include/profiler/timer.h b/haversine_02/include/profiler/timer.h index 9c8f2d6..6531cea 100644 --- a/haversine_02/include/profiler/timer.h +++ b/haversine_02/include/profiler/timer.h @@ -3,6 +3,22 @@ #include "aliases.h" +#ifndef MAX_PROFILE_SAMPLES +#define MAX_PROFILE_SAMPLES 1024 +#endif // !MAX_PROFILE_SAMPLES + +#ifdef ENABLE_PROFILING +#define PROFILE_START profile_start() +#define PROFILE_END profile_end() +#define SAMPLE_START(ID, TITLE) profiler_sample_t ID = sample_start(TITLE) +#define SAMPLE_END(ID) sample_end(&ID) +#else +#define PROFILE_START +#define PROFILE_END +#define SAMPLE_START(ID, TITLE) +#define SAMPLE_END(ID) +#endif // ENABLE_PROFILING + #ifdef __cplusplus extern "C" { #endif diff --git a/haversine_02/src/processor/main.cpp b/haversine_02/src/processor/main.cpp index 0a055b0..e723deb 100644 --- a/haversine_02/src/processor/main.cpp +++ b/haversine_02/src/processor/main.cpp @@ -14,20 +14,22 @@ #include int main(int argc, char *argv[]) { - profile_start(); + PROFILE_START; - profiler_sample_t cli_parse = sample_start("CLI PARSING"); + SAMPLE_START(cli_parse, "CLI PARSING"); ProcessorArgs args = parse_args(argc, argv); - sample_end(&cli_parse); + SAMPLE_END(cli_parse); - profiler_sample_t json_parse = sample_start("JSON PARSING"); + SAMPLE_START(json_parse, "JSON PARSING"); jentity_t *root = load_json(args.filepath); assert(root->type == JENTITY_SINGLE && root->value.type == JVAL_COLLECTION); - sample_end(&json_parse); - profiler_sample_t load_pairs_json = sample_start("LOAD JSON PAIRS"); + SAMPLE_END(json_parse); + + SAMPLE_START(load_pairs_json, "LOAD JSON PAIRS"); + jentity_t *pairs = root->value.collection->begin; assert(pairs->type == JENTITY_PAIR && @@ -59,9 +61,9 @@ int main(int argc, char *argv[]) { point_pairs[index++] = p; } - sample_end(&load_pairs_json); + SAMPLE_END(load_pairs_json); - profiler_sample_t binary_file_read = sample_start("BINARY READ"); + SAMPLE_START(binary_file_read, "BINARY READ"); const char *filename = "count_and_distances"; @@ -73,9 +75,9 @@ int main(int argc, char *argv[]) { fseek(fp, sizeof(u64), SEEK_SET); } - sample_end(&binary_file_read); + SAMPLE_END(binary_file_read); - profiler_sample_t haversine_sum = sample_start("HAVERSINE SUM"); + SAMPLE_START(haversine_sum, "HAVERSINE SUM"); f64 sum = 0.0; f64 distance = 0.0; @@ -95,13 +97,13 @@ int main(int argc, char *argv[]) { sum += distance; } - sample_end(&haversine_sum); + SAMPLE_END(haversine_sum); - profiler_sample_t haversine_average = sample_start("HAVERSINE AVERAGE"); + SAMPLE_START(haversine_average, "HAVERSINE AVERAGE"); printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count); - sample_end(&haversine_average); + SAMPLE_END(haversine_average); - profiler_sample_t tear_down = sample_start("TEAR DOWN"); + SAMPLE_START(tear_down, "TEAR DOWN"); if (fp) { fclose(fp); @@ -109,9 +111,9 @@ int main(int argc, char *argv[]) { free(point_pairs); - sample_end(&tear_down); + SAMPLE_END(tear_down); - profile_end(); + PROFILE_END; return 0; } diff --git a/haversine_02/src/profiler/timer.c b/haversine_02/src/profiler/timer.c index 6ea895b..2a1006f 100644 --- a/haversine_02/src/profiler/timer.c +++ b/haversine_02/src/profiler/timer.c @@ -6,8 +6,6 @@ #include -#define MAX_PROFILE_SAMPLES 1024 - typedef struct { profiler_sample_t samples[MAX_PROFILE_SAMPLES]; u64 cpu_freq;