From 63f8554200fc6edd821818f531ce76fd8d7aeef8 Mon Sep 17 00:00:00 2001 From: Felix Brendel Date: Fri, 30 Aug 2019 01:29:46 +0200 Subject: [PATCH] cleanup --- build.sh | 13 ++++++++ macros.h | 54 ------------------------------- macros.hpp | 77 ++++++++++++++++++++++++++++++++++++++++++++ profiler.h | 40 ----------------------- profiler.hpp | 61 +++++++++++++++++++++++++++++++++++ test.cpp | 15 ++++++--- types.h => types.hpp | 0 7 files changed, 161 insertions(+), 99 deletions(-) create mode 100755 build.sh delete mode 100644 macros.h create mode 100644 macros.hpp delete mode 100644 profiler.h create mode 100644 profiler.hpp rename types.h => types.hpp (100%) diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..30eba47 --- /dev/null +++ b/build.sh @@ -0,0 +1,13 @@ +TIMEFORMAT=%3lR +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +pushd $SCRIPTPATH > /dev/null + +# _DEBUG +# time g++ -fpermissive src/main.cpp -g -o ./bin/slime --std=c++17 || exit 1 +time clang++ -D_DEBUG -D_PROFILING -fpermissive test.cpp -g -o ./ftb --std=c++17 || exit 1 + +echo "" +time ./ftb + +popd > /dev/null +unset TIMEFORMAT diff --git a/macros.h b/macros.h deleted file mode 100644 index fb147f8..0000000 --- a/macros.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once -#include - -#define proc auto - -#ifdef _DEBUG -# define if_debug if constexpr (true) -#else -# define if_debug if constexpr (false) -#endif - -#ifdef _MSC_VER -# define debug_break() if_debug __debugbreak() -# define if_windows if constexpr (true) -# define if_linux if constexpr (false) -#else -# define debug_break() if_debug raise(SIGTRAP) -# define if_windows if constexpr (false) -# define if_linux if constexpr (true) -#endif - -/** - * Defer * -*/ - -template -class defer_finalizer { - F f; - bool moved; -public: - template - defer_finalizer(T && f_) : f(std::forward(f_)), moved(false) { } - - defer_finalizer(const defer_finalizer &) = delete; - - defer_finalizer(defer_finalizer && other) : f(std::move(other.f)), moved(other.moved) { - other.moved = true; - } - - ~defer_finalizer() { - if (!moved) f(); - } -}; - -struct { - template - defer_finalizer operator<<(F && f) { - return defer_finalizer(std::forward(f)); - } -} deferrer; - -#define TOKENPASTE(x, y) x ## y -#define TOKENPASTE2(x, y) TOKENPASTE(x, y) -#define defer auto TOKENPASTE2(__deferred_lambda_call, __COUNTER__) = deferrer << [&] diff --git a/macros.hpp b/macros.hpp new file mode 100644 index 0000000..e94a07b --- /dev/null +++ b/macros.hpp @@ -0,0 +1,77 @@ +#pragma once +#include + +#define proc auto + +#ifdef _DEBUG +# define if_debug if constexpr (true) +#else +# define if_debug if constexpr (false) +#endif + +#ifdef _MSC_VER +# define debug_break() if_debug __debugbreak() +# define if_windows if constexpr (true) +# define if_linux if constexpr (false) +#else +# define debug_break() if_debug raise(SIGTRAP) +# define if_windows if constexpr (false) +# define if_linux if constexpr (true) +#endif + +#define TOKENPASTE(x, y) x ## y +#define TOKENPASTE2(x, y) TOKENPASTE(x, y) + + +/** + * Defer * + */ +template +class defer_finalizer { + F f; + bool moved; +public: + template + defer_finalizer(T && f_) : f(std::forward(f_)), moved(false) { } + + defer_finalizer(const defer_finalizer &) = delete; + + defer_finalizer(defer_finalizer && other) : f(std::move(other.f)), moved(other.moved) { + other.moved = true; + } + + ~defer_finalizer() { + if (!moved) f(); + } +}; + +struct { + template + defer_finalizer operator<<(F && f) { + return defer_finalizer(std::forward(f)); + } +} deferrer; + +#define defer auto TOKENPASTE2(__deferred_lambda_call, __COUNTER__) = deferrer << [&] + + +/** + * fluid-let * + */ + +#define fluid_let(var, val) \ + if (0) \ + TOKENPASTE2(finished,__LINE__): ; \ + else \ + for (auto TOKENPASTE2(fluid_let_, __LINE__) = var;;) \ + for(var = val;;) \ + if (1) { \ + goto TOKENPASTE2(body,__LINE__); \ + } \ + else \ + while (1) \ + if (1) { \ + var = TOKENPASTE2(fluid_let_, __LINE__); \ + goto TOKENPASTE2(finished, __LINE__); \ + } \ + else TOKENPASTE2(body,__LINE__): diff --git a/profiler.h b/profiler.h deleted file mode 100644 index 2f29085..0000000 --- a/profiler.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include - -struct Profiler { - inline thread_local static int thread_id = 0; - inline thread_local static FILE* out_file = nullptr; - inline static char file_template[40] = "\0"; - - Profiler(const char* file, const char* func, const int line) { - if (!out_file) { - time_t t = time(NULL); - tm tm_i; - localtime_s(&tm_i, &t); - - if (!file_template[0]) { - sprintf(file_template, "%02d.%02d.%04d-%02d.%02d.%02d-%%02d-profiler.report", - tm_i.tm_mday, tm_i.tm_mon+1, tm_i.tm_year+1900, - tm_i.tm_hour, tm_i.tm_min, tm_i.tm_sec); - } - - char file_name[38]; - sprintf(file_name, file_template, thread_id); - fopen_s(&out_file, file_name, "w"); - if (!out_file) { - printf("could not open %s\n", file_name); - } - - } - - fprintf(out_file, "-> %s %s %d\n", func, file, line); - }; - ~Profiler() { - fprintf(out_file, "<-\n"); - }; -}; - -#define profile_this Profiler profiler(__FILE__, __FUNCTION__, __LINE__) diff --git a/profiler.hpp b/profiler.hpp new file mode 100644 index 0000000..3266b4e --- /dev/null +++ b/profiler.hpp @@ -0,0 +1,61 @@ +#pragma once +#ifdef _PROFILING +# include +# include +# include +# include +// for syscall: +# include +# include + +struct Profiler { + // same for all threads + inline static char file_template[40] = "\0"; + + // thread local + inline thread_local static bool is_initialized = false; + inline thread_local static int thread_id = -1; + inline thread_local static int call_depth = 0; + inline thread_local static FILE* out_file = nullptr; + Profiler(const char* file, const char* func, const int line) { + call_depth += 1; + + // if we never used this thread before + if (!is_initialized) { + thread_id = syscall(__NR_gettid); + printf("Hello I am %d\n", thread_id); + time_t t = time(NULL); + tm* tm_i = localtime(&t); + + // if we never even created the shared file name template + if (!file_template[0]) { + sprintf(file_template, "%02d.%02d.%04d-%02d.%02d.%02d-%%02d-profiler.report", + tm_i->tm_mday, tm_i->tm_mon+1, tm_i->tm_year+1900, + tm_i->tm_hour, tm_i->tm_min, tm_i->tm_sec); + } + + + char file_name[38]; + sprintf(file_name, file_template, thread_id); + out_file = fopen(file_name, "w"); + if (!out_file) { + printf("could not open %s\n", file_name); + } + + is_initialized = true; + } + + fprintf(out_file, "-> %s %s %d\n", func, file, line); + }; + ~Profiler() { + call_depth -= 1; + fprintf(out_file, "<-\n"); + if (call_depth == 0) + fflush(out_file); + }; +}; + +# define profile_this Profiler profiler(__FILE__, __FUNCTION__, __LINE__) +#else +# define profile_this enum {} +#endif diff --git a/test.cpp b/test.cpp index 3e18e21..db9c4eb 100644 --- a/test.cpp +++ b/test.cpp @@ -1,20 +1,25 @@ #define _CRT_SECURE_NO_WARNINGS -#include -#include "profiler.h" +#include "profiler.hpp" #include "macros.h" + +int var = 100; + void test() { profile_this; + printf("var is %d\n", var); + + fluid_let (var, 200) { + printf("var is %d\n", var); + } - printf("doing more work!\n"); + printf("var is %d\n", var); } int main(int argc, char* argv[]) { profile_this; - printf("doing some work and sleeping!\n"); - Sleep(1000); // Sleep a seconds test(); return 0; diff --git a/types.h b/types.hpp similarity index 100% rename from types.h rename to types.hpp