From 02c9ae0ab414078dcb4f94bb47b16630de6749d0 Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Fri, 10 Mar 2023 11:08:15 +0100 Subject: [PATCH] Game test! Co-authored-by: ValterMiari --- src/GC/Makefile | 7 +++++ src/GC/include/event.hpp | 5 ++-- src/GC/include/heap.hpp | 37 +++++++++++++---------- src/GC/include/profiler.hpp | 1 + src/GC/lib/heap.cpp | 9 +++++- src/GC/lib/profiler.cpp | 30 +++++++++++++++---- src/GC/tests/advance.cpp | 60 +++++++++++++++++-------------------- src/GC/tests/file.cpp | 26 ++++++++++++++++ src/GC/tests/game.cpp | 35 +++++++++++++++++++--- src/GC/tests/player.hpp | 21 ++++++------- 10 files changed, 162 insertions(+), 69 deletions(-) create mode 100644 src/GC/tests/file.cpp diff --git a/src/GC/Makefile b/src/GC/Makefile index 92b02e8..57800c4 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -12,6 +12,9 @@ DBGFLAGS = -g advance: $(CC) $(WFLAGS) $(STDFLAGS) tests/advance.cpp -o tests/advance.out +file: + $(CC) $(WFLAGS) $(STDFLAGS) tests/file.cpp -o tests/file.out + heap: $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp @@ -35,6 +38,10 @@ linker_vg: make linker valgrind $(VGFLAGS) tests/linker.out +game: + rm -f tests/game.out + $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) tests/game.cpp lib/heap.cpp -o tests/game.out + extern_lib: rm -f lib/heap.o lib/libheap.so tests/extern_lib.out $(CC) $(STDFLAGS) -c -fPIC -o lib/heap.o lib/heap.cpp diff --git a/src/GC/include/event.hpp b/src/GC/include/event.hpp index 108e208..ac215b3 100644 --- a/src/GC/include/event.hpp +++ b/src/GC/include/event.hpp @@ -1,8 +1,9 @@ #pragma once -#include +#include #include #include +#include #include "chunk.hpp" @@ -26,7 +27,7 @@ namespace GC // make const GCEventType m_type; std::time_t m_timestamp; - Chunk *m_chunk; + Chunk *m_chunk = nullptr; public: GCEvent(GCEventType type) diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 5878e91..cad2773 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -7,19 +7,21 @@ #include #include "chunk.hpp" +#include "profiler.hpp" #define HEAP_SIZE 65536 - -#define MARK (uint)0x1 -#define SWEEP (uint)0x2 -#define FREE (uint)0x4 -#define COLLECT_ALL (uint)0x7 - #define FREE_THRESH (uint)20 namespace GC { + enum CollectOption { + MARK=0x1, + SWEEP=0x2, + FREE=0x4, + COLLECT_ALL=0x7 + }; + class Heap { @@ -53,14 +55,10 @@ namespace GC return *iter; } - void collect(); - void sweep(Heap *heap); - uintptr_t *try_recycle_chunks(size_t size); - void free(Heap *heap); - void free_overlap(Heap *heap); - void mark(uintptr_t *start, const uintptr_t *end, std::vector &worklist); - void print_line(Chunk *chunk); - void print_worklist(std::vector &list); + inline static bool getProfilerMode() { + auto heap = Heap::the(); + return heap->m_profiler_enable; + } inline static Heap *m_instance = nullptr; const char *m_heap; @@ -72,6 +70,15 @@ namespace GC std::vector m_allocated_chunks; std::vector m_freed_chunks; + void collect(); + void sweep(Heap *heap); + uintptr_t *try_recycle_chunks(size_t size); + void free(Heap *heap); + void free_overlap(Heap *heap); + void mark(uintptr_t *start, const uintptr_t *end, std::vector &worklist); + void print_line(Chunk *chunk); + void print_worklist(std::vector &list); + public: /** * These are the only two functions which are exposed @@ -92,7 +99,7 @@ namespace GC m_instance = new Heap(); return m_instance; } - void collect(uint flags); // conditional collection + void collect(CollectOption flags); // conditional collection void check_init(); // print dummy things void print_contents(); // print dummy things void set_profiler(bool mode); diff --git a/src/GC/include/profiler.hpp b/src/GC/include/profiler.hpp index cc1b4c0..4567082 100644 --- a/src/GC/include/profiler.hpp +++ b/src/GC/include/profiler.hpp @@ -24,6 +24,7 @@ namespace GC { std::vector m_events; std::ofstream createFileStream(); + public: static void record(GCEventType type); diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index 9618415..a7d37d0 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -175,6 +175,8 @@ namespace GC */ void Heap::mark(uintptr_t *start, const uintptr_t *end, vector &worklist) { + if (getProfilerMode()) + Profiler::record(MarkStart); int counter = 0; // To find adresses thats in the worklist for (; start < end; start++) @@ -202,6 +204,8 @@ namespace GC if (!chunk->marked) { + if (getProfilerMode()) + Profiler::record(ChunkMarked, chunk); chunk->marked = true; // Remove the marked chunk from the worklist it = worklist.erase(it); @@ -247,6 +251,7 @@ namespace GC { // Add the unmarked chunks to freed chunks and remove from // the list of allocated chunks + Profiler::record(ChunkSwept, chunk); heap->m_freed_chunks.push_back(chunk); iter = heap->m_allocated_chunks.erase(iter); } @@ -336,8 +341,10 @@ namespace GC * * @param flags Bitmap of flags */ - void Heap::collect(uint flags) + void Heap::collect(CollectOption flags) { + if (getProfilerMode()) + Profiler::record(CollectStart); cout << "DEBUG COLLECT\nFLAGS: "; if (flags & MARK) diff --git a/src/GC/lib/profiler.cpp b/src/GC/lib/profiler.cpp index d68f754..2b7f5b8 100644 --- a/src/GC/lib/profiler.cpp +++ b/src/GC/lib/profiler.cpp @@ -33,14 +33,31 @@ namespace GC auto end = profiler->m_events.end(); std::ofstream fstr = profiler->createFileStream(); + char buffer[22]; + std::tm *btm; + std::time_t tt; + Chunk *chunk; while (start != end) { auto event = *start++; - fstr << "--------------------------------" - << event->TypeToString() - << event->getTimeStamp + tt = event->getTimeStamp(); + btm = std::localtime(&tt); + std::strftime(buffer, 22, "%a %T", btm); + + fstr << "--------------------------------\n" + << buffer + << "\nEvent:\t" << event->TypeToString(); + + chunk = event->getChunk(); + + if (chunk) { + fstr << "\nChunk:\t" << chunk->start + << "\n Size: " << chunk->size + << "\n Mark: " << chunk->marked; + } + fstr << "--------------------------------\n" << std::endl; } } @@ -48,11 +65,14 @@ namespace GC { std::time_t tt = std::time(NULL); std::tm *ptm = std::localtime(&tt); - char buffer[32]; std::strftime(buffer, 32, "/profiler/log_%a_%H_%M_%S.txt", ptm); + std::string filename(buffer); + + const std::string ABS_PATH = "/home/virre/dev/systemF/org/language/src/GC/"; + std::string fullpath = ABS_PATH + filename; - std::ofstream fstr(buffer); + std::ofstream fstr(fullpath); return fstr; } } \ No newline at end of file diff --git a/src/GC/tests/advance.cpp b/src/GC/tests/advance.cpp index 7fef9ac..92ce506 100644 --- a/src/GC/tests/advance.cpp +++ b/src/GC/tests/advance.cpp @@ -6,43 +6,39 @@ #include int main() { - // list l; - // char c = 'a'; - // for (int i = 1; i <= 5; i++) { - // l.push_back(c++); - // } + using namespace std; + using TimeStamp = std::chrono::_V2::system_clock::time_point; - // auto iter = l.begin(); - // auto stop = l.end(); + list l; + char c = 'a'; + for (int i = 1; i <= 5; i++) { + l.push_back(c++); + } - // while (iter != stop) { - // cout << *iter << " "; + auto iter = l.begin(); + auto stop = l.end(); + + while (iter != stop) { + cout << *iter << " "; - // iter++; - // } - // cout << endl; - // iter = l.begin(); - // while (*iter != *stop) { - // cout << *iter << " "; - // iter++; - // } - // cout << endl; + iter++; + } + cout << endl; + iter = l.begin(); + while (*iter != *stop) { + cout << *iter << " "; + iter++; + } + cout << endl; - // cout << "rebased" << endl; - // cout << "iter: " << *iter << "\nstop: " << *stop << endl; - - // TimeStamp ts = std::chrono::system_clock::now(); - // std::time_t tt = std::chrono::system_clock::to_time_t(ts); - // std::string tstr = std::ctime(&tt); - // tstr.resize(tstr.size()-1); - // std::cout << tstr << std::endl; - - char buffer[31]; - std::time_t tt = std::time(NULL); - std::tm *ptm = std::localtime(&tt); - std::strftime(buffer, 31, "/profiler/log_%a_%H_%M_%S.txt", ptm); - std::cout << buffer << std::endl; + cout << "rebased" << endl; + cout << "iter: " << *iter << "\nstop: " << *stop << endl; + TimeStamp ts = std::chrono::system_clock::now(); + std::time_t tt = std::chrono::system_clock::to_time_t(ts); + std::string tstr = std::ctime(&tt); + tstr.resize(tstr.size()-1); + std::cout << tstr << std::endl; return 0; } \ No newline at end of file diff --git a/src/GC/tests/file.cpp b/src/GC/tests/file.cpp new file mode 100644 index 0000000..92c9af2 --- /dev/null +++ b/src/GC/tests/file.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +int main() +{ + char buffer[31]; + std::time_t tt = std::time(NULL); + std::tm *ptm = std::localtime(&tt); + std::strftime(buffer, 31, "/logs/log_%a_%H_%M_%S.txt", ptm); + std::cout << buffer << std::endl; + + const std::string TESTS_PATH = "/home/virre/dev/systemF/org/language/src/GC/tests/"; + + std::string path = TESTS_PATH + "/testlog.txt"; + + std::ofstream testF(path); + + testF << "hellow yorld"; + + testF.close(); + + return 0; +} \ No newline at end of file diff --git a/src/GC/tests/game.cpp b/src/GC/tests/game.cpp index 4bf2d02..5ff8787 100644 --- a/src/GC/tests/game.cpp +++ b/src/GC/tests/game.cpp @@ -1,6 +1,7 @@ #include #include "player.hpp" +#include "heap.hpp" #define X_LENGTH 1000; #define Y_LENGTH 500; @@ -14,12 +15,38 @@ private: public: - Game() { - dimensions = {X_LENGTH, Y_LENGTH}; - } + Game() : dimensions(1000, 500) {} void add_player(Player& p) { players.push_back(p); } -}; \ No newline at end of file + Player* create_player(string s, Point pos, Point size, Point dir) { + Player *p = static_cast(GC::Heap::alloc(sizeof(Player))); + p->Player(s, pos, size, dir); // This will probably both be allocated on "our" heap as well as the heap for this program + return p; + } + + std::vector create_players(int nr) { + for (int i = 0; i < nr; i++) { + Player *p = create_player(std::to_string(i), Point(i, i), Point(2, 2), Point(0, 0)); + add_player(*p); + } + } + +}; + + +int main() { + GC::Heap::init(); + GC::Heap *gc = GC::Heap::debug_the(); + gc->check_init(); + + Game *game = static_cast(gc->alloc(sizeof(Game))); + game->create_players(100); + + gc->collect(GC::MARK); + gc->print_contents(); + + return 0; +} \ No newline at end of file diff --git a/src/GC/tests/player.hpp b/src/GC/tests/player.hpp index 97d2a0d..d3b8fec 100644 --- a/src/GC/tests/player.hpp +++ b/src/GC/tests/player.hpp @@ -2,6 +2,14 @@ using std::string; +class Point { + +public: + + int x, y; + Point(int _x, int _y) : x(_x), y(_y) {} +}; + class Player { private: @@ -13,12 +21,9 @@ private: public: - Player(string n, Point pos, Point s, Point dir) { - name = n; - position = pos; - size = s; - direction = dir; - } + Player(string n, Point pos, Point s, Point dir) + : name(n), position(pos.x, pos.y), size(s.x, s.y), direction(dir.x, dir.y) + {} void move() { position.x += direction.x; @@ -31,7 +36,3 @@ public: } }; - -struct Point { - int x, y; -}; \ No newline at end of file