Code cleanup

This commit is contained in:
Victor Olin 2023-03-21 17:06:10 +01:00
parent 8081bc5d67
commit 7105c570d9
9 changed files with 106 additions and 138 deletions

View file

@ -1,27 +1,18 @@
#pragma once #pragma once
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
namespace GC namespace GC
{ {
struct Chunk struct Chunk
{ {
bool marked; bool m_marked {false};
uintptr_t *start; uintptr_t *const m_start {nullptr};
size_t size; const size_t m_size {0};
// Default constructor Chunk(size_t size, uintptr_t *start) : m_size(size), m_start(start) {}
Chunk() {} Chunk(const Chunk *const c) : m_marked(c->m_marked), m_start(c->m_start), m_size(c->m_size) {}
Chunk(const Chunk *const c) : marked(c->marked), start(c->start), size(c->size) {} Chunk(const Chunk& c) : m_marked(c.m_marked), m_start(c.m_start), m_size(c.m_size) {}
// -- Temporary --
// A copy constructor, keep track of how many times the vectors that hold chunks
// are copied.
// Shouldn't be all that relevant if we use vectors with Chunk-pointers.
Chunk(const Chunk& c) : marked(c.marked), start(c.start), size(c.size)
{
// std::cout << "Chunk was copied" << std::endl;
}
}; };
} }

View file

@ -27,30 +27,15 @@ namespace GC
class GCEvent class GCEvent
{ {
private: private:
// make const const GCEventType m_type;
GCEventType m_type; const std::time_t m_timestamp {std::time(NULL)};
std::time_t m_timestamp; const Chunk *m_chunk {nullptr};
Chunk *m_chunk; const size_t m_size {0};
size_t m_size;
public: public:
GCEvent(GCEventType type) GCEvent(GCEventType type) : m_type(type) {}
{ GCEvent(GCEventType type, Chunk *chunk) : m_type(type), m_chunk(chunk) {}
m_type = type; GCEvent(GCEventType type, size_t size) : m_type(type), m_size(size) {}
m_timestamp = std::time(NULL);
m_chunk = nullptr;
m_size = 0;
}
GCEvent(GCEventType type, Chunk *chunk) : GCEvent(type)
{
m_chunk = chunk;
}
GCEvent(GCEventType type, size_t size) : GCEvent(type)
{
m_size = size;
}
~GCEvent() { ~GCEvent() {
if (m_chunk != nullptr) if (m_chunk != nullptr)
@ -59,7 +44,7 @@ namespace GC
GCEventType get_type(); GCEventType get_type();
std::time_t get_time_stamp(); std::time_t get_time_stamp();
Chunk *get_chunk(); const Chunk *get_chunk();
size_t get_size(); size_t get_size();
const char *type_to_string(); const char *type_to_string();
}; };

View file

@ -11,7 +11,7 @@
#define HEAP_SIZE 65536 #define HEAP_SIZE 65536
#define FREE_THRESH (uint)20 #define FREE_THRESH (uint)20
#define DEBUG // #define DEBUG
namespace GC namespace GC
{ {
@ -28,19 +28,14 @@ namespace GC
{ {
private: private:
Heap() Heap() : m_heap(static_cast<char *>(malloc(HEAP_SIZE))) {}
{
m_heap = static_cast<char *>(malloc(HEAP_SIZE));
m_size = 0;
m_allocated_size = 0;
}
~Heap() ~Heap()
{ {
std::free((char *)m_heap); std::free((char *)m_heap);
} }
inline static Heap *the() static Heap *the()
{ {
if (m_instance) // if m_instance is not a nullptr if (m_instance) // if m_instance is not a nullptr
return m_instance; return m_instance;
@ -48,7 +43,7 @@ namespace GC
return m_instance; return m_instance;
} }
inline static Chunk *get_at(std::vector<Chunk *> &list, size_t n) static Chunk *get_at(std::vector<Chunk *> &list, size_t n)
{ {
auto iter = list.begin(); auto iter = list.begin();
if (!n) if (!n)
@ -62,12 +57,12 @@ namespace GC
return heap->m_profiler_enable; return heap->m_profiler_enable;
} }
inline static Heap *m_instance = nullptr; char *const m_heap;
char *m_heap; size_t m_size {0};
size_t m_size; inline static Heap *m_instance {nullptr};
size_t m_allocated_size; // size_t m_allocated_size {0};
uintptr_t *m_stack_top = nullptr; uintptr_t *m_stack_top {nullptr};
bool m_profiler_enable = false; bool m_profiler_enable {false};
std::vector<Chunk *> m_allocated_chunks; std::vector<Chunk *> m_allocated_chunks;
std::vector<Chunk *> m_freed_chunks; std::vector<Chunk *> m_freed_chunks;
@ -95,7 +90,7 @@ namespace GC
static void *alloc(size_t size); static void *alloc(size_t size);
#ifdef DEBUG #ifdef DEBUG
static inline Heap *debug_the() static Heap *debug_the()
{ {
if (m_instance) // if m_instance is not a nullptr if (m_instance) // if m_instance is not a nullptr
return m_instance; return m_instance;

View file

@ -10,29 +10,31 @@ namespace GC {
class Profiler { class Profiler {
private: private:
Profiler() {} Profiler() {}
~Profiler() { ~Profiler()
{
for (GCEvent *c : m_events) for (GCEvent *c : m_events)
delete c; delete c;
} }
inline static Profiler *the() { static Profiler *the()
{
if (m_instance) if (m_instance)
return m_instance; return m_instance;
m_instance = new Profiler(); m_instance = new Profiler();
return m_instance; return m_instance;
} }
inline static Profiler *m_instance = nullptr; inline static Profiler *m_instance {nullptr};
std::vector<GCEvent *> m_events; std::vector<GCEvent *> m_events;
std::ofstream create_file_stream(); std::ofstream create_file_stream();
std::string get_log_folder(); std::string get_log_folder();
static void dump_trace();
public: public:
static void record(GCEventType type); static void record(GCEventType type);
static void record(GCEventType type, size_t size); static void record(GCEventType type, size_t size);
static void record(GCEventType type, Chunk *chunk); static void record(GCEventType type, Chunk *chunk);
static void dump_trace();
static void dispose(); static void dispose();
}; };
} }

View file

@ -18,7 +18,7 @@ namespace GC
return m_timestamp; return m_timestamp;
} }
Chunk *GCEvent::get_chunk() const Chunk *GCEvent::get_chunk()
{ {
return m_chunk; return m_chunk;
} }

View file

@ -7,32 +7,32 @@
#include <stdlib.h> #include <stdlib.h>
#include <vector> #include <vector>
// #include "../include/heap.hpp"
// #include <heap.hpp>
#include "heap.hpp" #include "heap.hpp"
using std::cout, std::endl, std::vector, std::hex, std::dec; using std::cout, std::endl, std::vector, std::hex, std::dec;
namespace GC namespace GC
{ {
/** /**
* Initialises the heap singleton and saves the address * Initialises the heap singleton and saves the address
* of the calling stack frame as the stack_top. Presumeably * of the calling function's stack frame as the stack_top.
* this address points to the stack frame of the compiled * Presumeably this address points to the stack frame of
* LLVM executable after linking. * the compiled LLVM executable after linking.
*/ */
void Heap::init() void Heap::init()
{ {
Heap *heap = Heap::the(); Heap *heap = Heap::the();
if (heap->profiler_enabled()) if (heap->profiler_enabled())
Profiler::record(HeapInit); Profiler::record(HeapInit);
#pragma clang diagnostic ignored "-Wframe-address" // clang complains because arg for __b_f_a is not 0 // clang complains because arg for __b_f_a is not 0 which is unsafe
#pragma clang diagnostic ignored "-Wframe-address"
heap->m_stack_top = static_cast<uintptr_t *>(__builtin_frame_address(1)); heap->m_stack_top = static_cast<uintptr_t *>(__builtin_frame_address(1));
} }
/** /**
* Disposes the heap at program exit. * Disposes the heap and the profiler at program exit
* which also triggers a heap log file dumped if the
* profiler is enabled.
*/ */
void Heap::dispose() void Heap::dispose()
{ {
@ -60,16 +60,16 @@ namespace GC
if (profiler_enabled) if (profiler_enabled)
Profiler::record(AllocStart, size); Profiler::record(AllocStart, size);
if (size < 0) if (size == 0)
{ {
cout << "Heap: Cannot alloc less than 0B. No bytes allocated." << endl; cout << "Heap: Cannot alloc 0B. No bytes allocated." << endl;
return nullptr; return nullptr;
} }
if (heap->m_size + size > HEAP_SIZE) if (heap->m_size + size > HEAP_SIZE)
{ {
heap->collect(); heap->collect();
// If collect failed, crash with OOM error // If memory is not enough after collect, crash with OOM error
assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory"); assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory");
} }
@ -79,24 +79,21 @@ namespace GC
{ {
if (profiler_enabled) if (profiler_enabled)
Profiler::record(ReusedChunk, reused_chunk); Profiler::record(ReusedChunk, reused_chunk);
return static_cast<void *>(reused_chunk->start); return static_cast<void *>(reused_chunk->m_start);
} }
// If no free chunks was found (reused_chunk is a nullptr), // If no free chunks was found (reused_chunk is a nullptr),
// then create a new chunk // then create a new chunk
auto new_chunk = new Chunk; auto new_chunk = new Chunk(size, (uintptr_t *)(heap->m_heap + heap->m_size));
new_chunk->size = size;
new_chunk->start = (uintptr_t *)(heap->m_heap + heap->m_size);
heap->m_size += size; heap->m_size += size;
heap->m_allocated_chunks.push_back(new_chunk); heap->m_allocated_chunks.push_back(new_chunk);
if (profiler_enabled) if (profiler_enabled)
Profiler::record(NewChunk, new_chunk); Profiler::record(NewChunk, new_chunk);
// new_chunk should probably be a unique pointer, if that isn't implicit already // new_chunk should probably be a unique pointer, if that isn't implicit already
return new_chunk->start; return new_chunk->m_start;
} }
/** /**
@ -124,15 +121,12 @@ namespace GC
auto chunk = Heap::get_at(heap->m_freed_chunks, i); auto chunk = Heap::get_at(heap->m_freed_chunks, i);
auto iter = heap->m_freed_chunks.begin(); auto iter = heap->m_freed_chunks.begin();
advance(iter, i); advance(iter, i);
if (chunk->size > size) if (chunk->m_size > size)
{ {
// Split the chunk, use one part and add the remaining part to // Split the chunk, use one part and add the remaining part to
// the list of freed chunks // the list of freed chunks
size_t diff = chunk->size - size; size_t diff = chunk->m_size - size;
auto chunk_complement = new Chunk(diff, chunk->m_start + chunk->m_size);
auto chunk_complement = new Chunk;
chunk_complement->size = diff;
chunk_complement->start = chunk->start + chunk->size;
heap->m_freed_chunks.erase(iter); heap->m_freed_chunks.erase(iter);
heap->m_freed_chunks.push_back(chunk_complement); heap->m_freed_chunks.push_back(chunk_complement);
@ -140,7 +134,7 @@ namespace GC
return chunk; return chunk;
} }
else if (chunk->size == size) else if (chunk->m_size == size)
{ {
// Reuse the whole chunk // Reuse the whole chunk
heap->m_freed_chunks.erase(iter); heap->m_freed_chunks.erase(iter);
@ -148,6 +142,7 @@ namespace GC
return chunk; return chunk;
} }
} }
// If no chunk was found, return nullptr
return nullptr; return nullptr;
} }
@ -160,16 +155,18 @@ namespace GC
*/ */
void Heap::collect() void Heap::collect()
{ {
// Get instance
auto heap = Heap::the(); auto heap = Heap::the();
if (heap->profiler_enabled()) if (heap->profiler_enabled())
Profiler::record(CollectStart); Profiler::record(CollectStart);
// get current stack // get current stack frame
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
uintptr_t *stack_top = heap->m_stack_top != nullptr ? heap->m_stack_top : (uintptr_t *)0; if (heap->m_stack_top == nullptr)
assert(false && "Heap is not initialized, read the docs!");
uintptr_t *stack_top = heap->m_stack_top;
auto work_list = heap->m_allocated_chunks; auto work_list = heap->m_allocated_chunks;
mark(stack_bottom, stack_top, work_list); mark(stack_bottom, stack_top, work_list);
@ -197,39 +194,31 @@ namespace GC
bool profiler_enabled = heap->profiler_enabled(); bool profiler_enabled = heap->profiler_enabled();
if (profiler_enabled) if (profiler_enabled)
Profiler::record(MarkStart); Profiler::record(MarkStart);
// To find adresses thats in the worklist // To find adresses thats in the worklist
for (; start <= end; start++) for (; start <= end; start++)
{ {
auto it = worklist.begin(); auto it = worklist.begin();
auto stop = worklist.end(); auto stop = worklist.end();
// for (auto it = worklist.begin(); it != worklist.end();) {
while (it != stop) while (it != stop)
{ {
Chunk *chunk = *it; Chunk *chunk = *it;
auto c_start = reinterpret_cast<uintptr_t>(chunk->m_start);
auto c_start = reinterpret_cast<uintptr_t>(chunk->start); auto c_size = reinterpret_cast<uintptr_t>(chunk->m_size);
auto c_size = reinterpret_cast<uintptr_t>(chunk->size);
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size); auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
/* cout << "Value of Start:\t\t" << start << endl; // Check if the stack pointer points to something within the chunk
cout << "Start points to:\t" << hex << *start << endl;
cout << "Chunk start:\t\t" << hex << c_start << endl;
cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl; */
// Check if the stack pointer aligns with the chunk
if (c_start <= *start && *start < c_end) if (c_start <= *start && *start < c_end)
{ {
if (!chunk->marked) if (!chunk->m_marked)
{ {
if (profiler_enabled) if (profiler_enabled)
Profiler::record(ChunkMarked, chunk); Profiler::record(ChunkMarked, chunk);
chunk->marked = true; chunk->m_marked = true;
// Remove the marked chunk from the worklist
it = worklist.erase(it); it = worklist.erase(it);
// Recursively call mark, to see if the reachable chunk further points to another chunk // Recursively call mark, to see if the reachable chunk further points to another chunk
mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist); mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist);
//mark_step(c_start, c_end, worklist);
} }
else else
{ {
@ -261,9 +250,9 @@ namespace GC
Chunk *chunk = *iter; Chunk *chunk = *iter;
// Unmark the marked chunks for the next iteration. // Unmark the marked chunks for the next iteration.
if (chunk->marked) if (chunk->m_marked)
{ {
chunk->marked = false; chunk->m_marked = false;
++iter; ++iter;
} }
else else
@ -325,35 +314,32 @@ namespace GC
{ {
std::vector<Chunk *> filtered; std::vector<Chunk *> filtered;
size_t i = 0; size_t i = 0;
// filtered.push_back(heap->m_freed_chunks.at(i++));
// filtered.push_back(Heap::get_at(heap->m_freed_chunks, i++));
auto prev = Heap::get_at(heap->m_freed_chunks, i++); auto prev = Heap::get_at(heap->m_freed_chunks, i++);
prev->marked = true; prev->m_marked = true;
filtered.push_back(prev); filtered.push_back(prev);
cout << filtered.back()->start << endl; cout << filtered.back()->m_start << endl;
for (; i < heap->m_freed_chunks.size(); i++) for (; i < heap->m_freed_chunks.size(); i++)
{ {
prev = filtered.back(); prev = filtered.back();
// auto next = heap->m_freed_chunks.at(i);
auto next = Heap::get_at(heap->m_freed_chunks, i); auto next = Heap::get_at(heap->m_freed_chunks, i);
auto p_start = (uintptr_t)(prev->start); auto p_start = (uintptr_t)(prev->m_start);
auto p_size = (uintptr_t)(prev->size); auto p_size = (uintptr_t)(prev->m_size);
auto n_start = (uintptr_t)(next->start); auto n_start = (uintptr_t)(next->m_start);
if (n_start >= (p_start + p_size)) if (n_start >= (p_start + p_size))
{ {
next->marked = true; next->m_marked = true;
filtered.push_back(next); filtered.push_back(next);
} }
} }
heap->m_freed_chunks.swap(filtered); heap->m_freed_chunks.swap(filtered);
bool profiler_enabled = heap->profiler_enabled(); bool profiler_enabled = heap->profiler_enabled();
// after swap m_freed_chunks contains still available chunks // After swap m_freed_chunks contains still available chunks
// and filtered contains all the chunks, so delete unused chunks // and filtered contains all the chunks, so delete unused chunks
for (Chunk *chunk : filtered) for (Chunk *chunk : filtered)
{ {
// if chunk was filtered away, delete it // if chunk was filtered away, delete it
if (!chunk->marked) if (!chunk->m_marked)
{ {
if (profiler_enabled) if (profiler_enabled)
Profiler::record(ChunkFreed, chunk); Profiler::record(ChunkFreed, chunk);
@ -361,13 +347,12 @@ namespace GC
} }
else else
{ {
chunk->marked = false; chunk->m_marked = false;
} }
} }
} }
// ----- ONLY DEBUGGING ----------------------------------------------------------------------- #ifdef DEBUG
/** /**
* Prints the result of Heap::init() and a dummy value * Prints the result of Heap::init() and a dummy value
* for the current stack frame for reference. * for the current stack frame for reference.
@ -430,9 +415,9 @@ namespace GC
Chunk *ref = worklist.back(); Chunk *ref = worklist.back();
worklist.pop_back(); worklist.pop_back();
Chunk *child = (Chunk *)ref; // this is probably not correct Chunk *child = (Chunk *)ref; // this is probably not correct
if (child != nullptr && !child->marked) if (child != nullptr && !child->m_marked)
{ {
child->marked = true; child->m_marked = true;
worklist.push_back(child); worklist.push_back(child);
mark_test(worklist); mark_test(worklist);
} }
@ -448,9 +433,9 @@ namespace GC
if (*start % 8 == 0) if (*start % 8 == 0)
{ // all pointers must be aligned as double words { // all pointers must be aligned as double words
Chunk *ref = (Chunk *)*start; Chunk *ref = (Chunk *)*start;
if (ref != nullptr && !ref->marked) if (ref != nullptr && !ref->m_marked)
{ {
ref->marked = true; ref->m_marked = true;
worklist.push_back(ref); worklist.push_back(ref);
mark_test(worklist); mark_test(worklist);
} }
@ -461,14 +446,14 @@ namespace GC
// For testing purposes // For testing purposes
void Heap::print_line(Chunk *chunk) void Heap::print_line(Chunk *chunk)
{ {
cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n" cout << "Marked: " << chunk->m_marked << "\nStart adr: " << chunk->m_start << "\nSize: " << chunk->m_size << " B\n"
<< endl; << endl;
} }
void Heap::print_worklist(std::vector<Chunk *> &list) void Heap::print_worklist(std::vector<Chunk *> &list)
{ {
for (auto cp : list) for (auto cp : list)
cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << "\n"; cout << "Chunk at:\t" << cp->m_start << "\nSize:\t\t" << cp->m_size << "\n";
cout << endl; cout << endl;
} }
@ -509,4 +494,5 @@ namespace GC
print_line(chunk); print_line(chunk);
} }
} }
#endif
} }

View file

@ -29,6 +29,10 @@ namespace GC
void Profiler::record(GCEventType type, Chunk *chunk) void Profiler::record(GCEventType type, Chunk *chunk)
{ {
// Create a copy of chunk to store in the profiler
// because in free() chunks are deleted and cannot
// be referenced by the profiler. These copied
// chunks are deleted by the profiler on dispose().
auto chunk_copy = new Chunk(chunk); auto chunk_copy = new Chunk(chunk);
auto event = new GCEvent(type, chunk_copy); auto event = new GCEvent(type, chunk_copy);
auto profiler = Profiler::the(); auto profiler = Profiler::the();
@ -41,11 +45,14 @@ namespace GC
auto start = profiler->m_events.begin(); auto start = profiler->m_events.begin();
auto end = profiler->m_events.end(); auto end = profiler->m_events.end();
// File output stream
std::ofstream fstr = profiler->create_file_stream(); std::ofstream fstr = profiler->create_file_stream();
// Buffer for timestamp
char buffer[22]; char buffer[22];
// Time variables
std::tm *btm; std::tm *btm;
std::time_t tt; std::time_t tt;
Chunk *chunk; const Chunk *chunk;
while (start != end) while (start != end)
{ {
@ -63,18 +70,23 @@ namespace GC
chunk = event->get_chunk(); chunk = event->get_chunk();
if (chunk) { if (event->get_type() == AllocStart)
fstr << "\nChunk: " << chunk->start {
<< "\n Size: " << chunk->size fstr << "\nSize: " << event->get_size();
<< "\n Mark: " << chunk->marked; }
else if (chunk)
{
fstr << "\nChunk: " << chunk->m_start
<< "\n Size: " << chunk->m_size
<< "\n Mark: " << chunk->m_marked;
} }
// else if (event->get)
fstr << "\n"; fstr << "\n";
} }
fstr << "--------------------------------" << std::endl; fstr << "--------------------------------" << std::endl;
} }
void Profiler::dispose() { void Profiler::dispose()
{
Profiler::record(ProfilerDispose); Profiler::record(ProfilerDispose);
Profiler::dump_trace(); Profiler::dump_trace();
auto profiler = Profiler::the(); auto profiler = Profiler::the();

View file

@ -12,6 +12,3 @@ Deliver to samuel
## Tests TODO ## Tests TODO
- Write complex datastructures for tests with larger programs - Write complex datastructures for tests with larger programs
## Profiler grejer
- Fixa användning av `Profiler::record(GCEventType type, size_t size)` i både alloc och dump_trace