From 51765f4d0c19922293e3fc86a2f8ad2494e9813a Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Fri, 10 Mar 2023 09:24:32 +0100 Subject: [PATCH] Work on the profiler --- src/GC/include/event.hpp | 15 ++++------ src/GC/include/heap.hpp | 7 ++--- src/GC/include/profiler.hpp | 4 ++- src/GC/lib/event.cpp | 29 ++++++++++++++----- src/GC/lib/profiler.cpp | 39 ++++++++++++++++++++++---- src/GC/tests/advance.cpp | 56 +++++++++++++++++++++++-------------- 6 files changed, 101 insertions(+), 49 deletions(-) diff --git a/src/GC/include/event.hpp b/src/GC/include/event.hpp index 547e2aa..108e208 100644 --- a/src/GC/include/event.hpp +++ b/src/GC/include/event.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -20,34 +20,31 @@ namespace GC ReusedChunk }; - using TimeStamp = std::chrono::_V2::system_clock::time_point; - class GCEvent { private: // make const GCEventType m_type; - TimeStamp m_timestamp; + std::time_t m_timestamp; Chunk *m_chunk; public: GCEvent(GCEventType type) { m_type = type; - m_timestamp = std::chrono::system_clock::now(); + m_timestamp = std::time(NULL); } GCEvent(GCEventType type, Chunk *chunk) { m_type = type; - m_timestamp = std::chrono::system_clock::now(); + m_timestamp = std::time(NULL); m_chunk = chunk; } GCEventType getType(); - TimeStamp getTimeStamp(); + std::time_t getTimeStamp(); Chunk *getChunk(); - - void print(std::ostream &out); + const char *TypeToString(); }; } \ No newline at end of file diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 24b307a..5878e91 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -24,7 +24,6 @@ namespace GC { private: - // Private constructor according to the singleton pattern Heap() { m_heap = static_cast(malloc(HEAP_SIZE)); @@ -32,14 +31,13 @@ namespace GC m_allocated_size = 0; } - // BEWARE only for testing, this should be adressed ~Heap() { std::free((char *)m_heap); } inline static Heap *the() - { // TODO: make private + { if (m_instance) // if m_instance is not a nullptr return m_instance; m_instance = new Heap(); @@ -71,7 +69,6 @@ namespace GC uintptr_t *m_stack_top = nullptr; bool m_profiler_enable = false; - // maybe change to std::list std::vector m_allocated_chunks; std::vector m_freed_chunks; @@ -89,7 +86,7 @@ namespace GC // DEBUG ONLY static inline Heap *debug_the() - { // TODO: make private + { if (m_instance) // if m_instance is not a nullptr return m_instance; m_instance = new Heap(); diff --git a/src/GC/include/profiler.hpp b/src/GC/include/profiler.hpp index a253fa6..cc1b4c0 100644 --- a/src/GC/include/profiler.hpp +++ b/src/GC/include/profiler.hpp @@ -23,9 +23,11 @@ namespace GC { inline static Profiler *m_instance = nullptr; std::vector m_events; + std::ofstream createFileStream(); + public: static void record(GCEventType type); static void record(GCEventType type, Chunk *chunk); - static void printTrace(); + static void dumpTrace(); }; } \ No newline at end of file diff --git a/src/GC/lib/event.cpp b/src/GC/lib/event.cpp index 7c95a44..8fd633c 100644 --- a/src/GC/lib/event.cpp +++ b/src/GC/lib/event.cpp @@ -6,21 +6,36 @@ #include "event.hpp" #include "heap.hpp" -namespace GC { - - GCEventType GCEvent::getType() { +namespace GC +{ + + GCEventType GCEvent::getType() + { return m_type; } - TimeStamp GCEvent::getTimeStamp() { + std::time_t GCEvent::getTimeStamp() + { return m_timestamp; } - Chunk *GCEvent::getChunk() { + Chunk *GCEvent::getChunk() + { return m_chunk; } - void GCEvent::print(std::ostream &out) { - assert(false && "TODO: unimplemented"); + inline const char *GCEvent::TypeToString() + { + switch (m_type) + { + case CollectStart: return "CollectStart"; + case MarkStart: return "MarkStart"; + case ChunkMarked: return "ChunkMarked"; + case ChunkSwept: return "ChunkSwept"; + case ChunkFreed: return "ChunkFreed"; + case NewChunk: return "NewChunk"; + case ReusedChunk: return "ReusedChunk"; + default: return "[Unknown]"; + } } } \ No newline at end of file diff --git a/src/GC/lib/profiler.cpp b/src/GC/lib/profiler.cpp index 18b3810..d68f754 100644 --- a/src/GC/lib/profiler.cpp +++ b/src/GC/lib/profiler.cpp @@ -1,4 +1,8 @@ +#include +#include #include +#include +#include #include #include "chunk.hpp" @@ -6,26 +10,49 @@ #include "heap.hpp" #include "profiler.hpp" -namespace GC { - void Profiler::record(GCEventType type) { +namespace GC +{ + void Profiler::record(GCEventType type) + { auto event = new GCEvent(type); auto profiler = Profiler::the(); profiler->m_events.push_back(event); } - void Profiler::record(GCEventType type, Chunk *chunk) { + void Profiler::record(GCEventType type, Chunk *chunk) + { auto event = new GCEvent(type, chunk); auto profiler = Profiler::the(); profiler->m_events.push_back(event); } - void Profiler::printTrace() { + void Profiler::dumpTrace() + { auto profiler = Profiler::the(); auto start = profiler->m_events.begin(); auto end = profiler->m_events.end(); - while (start != end) { + + std::ofstream fstr = profiler->createFileStream(); + + while (start != end) + { auto event = *start++; - event->print(std::cout); + + fstr << "--------------------------------" + << event->TypeToString() + << event->getTimeStamp } } + + std::ofstream createFileStream() + { + 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::ofstream fstr(buffer); + return fstr; + } } \ No newline at end of file diff --git a/src/GC/tests/advance.cpp b/src/GC/tests/advance.cpp index 0a8a177..7fef9ac 100644 --- a/src/GC/tests/advance.cpp +++ b/src/GC/tests/advance.cpp @@ -1,34 +1,48 @@ +#include +#include #include #include +#include #include -using namespace std; - int main() { - list l; - char c = 'a'; - for (int i = 1; i <= 5; i++) { - l.push_back(c++); - } + // list l; + // char c = 'a'; + // for (int i = 1; i <= 5; i++) { + // l.push_back(c++); + // } - auto iter = l.begin(); - auto stop = l.end(); + // auto iter = l.begin(); + // auto stop = l.end(); - while (iter != stop) { - cout << *iter << " "; + // 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 << "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; + + return 0; } \ No newline at end of file