Browse Source

profiler added

banana-cakes
Felix Brendel 6 years ago
commit
5d6da78c90
6 changed files with 161 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +29
    -0
      build.bat
  3. +54
    -0
      macros.h
  4. +40
    -0
      profiler.h
  5. +21
    -0
      test.cpp
  6. +16
    -0
      types.h

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
/bin/*

+ 29
- 0
build.bat View File

@@ -0,0 +1,29 @@
@echo off
pushd %~dp0

set exeName=main.exe
set binDir=bin

mkdir %bindir%
pushd %bindir%

cl^
../test.cpp^
/Fe%exeName% /MP /openmp /W3 /std:c++latest^
/nologo /EHsc /Z7^
/link /incremental /debug:fastlink

if %errorlevel% == 0 (
echo.
if not exist ..\%binDir% mkdir ..\%binDir%
move %exeName% ..\%binDir%\ > NUL
echo ---------- Output start ----------
%exeName%
echo ---------- Output end ----------
popd
) else (
echo.
echo Fucki'n 'ell
)

popd

+ 54
- 0
macros.h View File

@@ -0,0 +1,54 @@
#pragma once
#include <functional>

#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<typename F>
class defer_finalizer {
F f;
bool moved;
public:
template<typename T>
defer_finalizer(T && f_) : f(std::forward<T>(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<typename F>
defer_finalizer<F> operator<<(F && f) {
return defer_finalizer<F>(std::forward<F>(f));
}
} deferrer;

#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define defer auto TOKENPASTE2(__deferred_lambda_call, __COUNTER__) = deferrer << [&]

+ 40
- 0
profiler.h View File

@@ -0,0 +1,40 @@
#pragma once

#include <stdio.h>
#include <string.h>
#include <time.h>

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__)

+ 21
- 0
test.cpp View File

@@ -0,0 +1,21 @@
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>

#include "profiler.h"
#include "macros.h"

void test() {
profile_this;

printf("doing more work!\n");
}

int main(int argc, char* argv[]) {
profile_this;

printf("doing some work and sleeping!\n");
Sleep(1000); // Sleep a seconds
test();

return 0;
}

+ 16
- 0
types.h View File

@@ -0,0 +1,16 @@
#pragma once

#include <stdint.h>

typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef float f32;
typedef long double f64;

Loading…
Cancel
Save