Started working on a custom profiler

This commit is contained in:
Victor Olin 2023-02-28 18:44:07 +01:00
parent 4f9aaaf8b4
commit 137687e446
8 changed files with 158 additions and 28 deletions

48
src/GC/include/event.hpp Normal file
View file

@ -0,0 +1,48 @@
#pragma once
#include <chrono>
#include <iostream>
#include <list>
#include "chunk.hpp"
using namespace std;
namespace GC {
enum GCEventType {
CollectStart,
MarkStart,
ChunkMarked,
ChunkSwept,
ChunkFreed
};
using TimeStamp = chrono::_V2::system_clock::time_point;
class GCEvent {
private:
// make const
GCEventType m_type;
TimeStamp m_timestamp;
Chunk *m_chunk;
public:
GCEvent(GCEventType type) {
m_type = type;
m_timestamp = chrono::system_clock::now();
}
GCEvent(GCEventType type, Chunk *chunk) {
m_type = type;
m_timestamp = chrono::system_clock::now();
m_chunk = chunk;
}
GCEventType getType();
TimeStamp getTimeStamp();
Chunk *getChunk();
void printShort();
void printFull();
};
}

View file

@ -35,14 +35,14 @@ namespace GC {
std::free((char *)m_heap); std::free((char *)m_heap);
} }
static inline Heap *the() { // TODO: make private inline static Heap *the() { // TODO: make private
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;
m_instance = new Heap(); m_instance = new Heap();
return m_instance; return m_instance;
} }
static inline Chunk *getAt(std::vector<Chunk *> list, size_t n) { inline static Chunk *getAt(std::vector<Chunk *> list, size_t n) {
auto iter = list.begin(); auto iter = list.begin();
if (!n) if (!n)
return *iter; return *iter;

32
src/GC/lib/event.cpp Normal file
View file

@ -0,0 +1,32 @@
#include <chrono>
#include <iostream>
#include <list>
#include "chunk.hpp"
#include "event.hpp"
#include "heap.hpp"
using namespace std;
namespace GC {
GCEventType GCEvent::getType() {
return m_type;
}
TimeStamp GCEvent::getTimeStamp() {
return m_timestamp;
}
Chunk *GCEvent::getChunk() {
return m_chunk;
}
void GCEvent::printShort() {
assert(false && "TODO: unimplemented");
}
void GCEvent::printFull() {
assert(false && "TODO: unimplemented");
}
}

View file

@ -141,12 +141,7 @@ namespace GC {
// get current stack // get current stack
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
// fix this block, it's nästy uintptr_t *stack_top = heap->m_stack_top != nullptr ? heap->m_stack_top : (uintptr_t *)0;
uintptr_t *stack_top;
if (heap->m_stack_top != nullptr)
stack_top = heap->m_stack_top;
else
stack_top = (uintptr_t *)0; // temporary
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);

34
src/GC/lib/profiler.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include "chunk.hpp"
#include "event.hpp"
#include "heap.hpp"
#include "profiler.hpp"
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) {
auto event = new GCEvent(type, chunk);
auto profiler = Profiler::the();
profiler->m_events.push_back(event);
}
void Profiler::printTrace(PrintTraceOptions opt) {
auto profiler = Profiler::the();
auto start = profiler->m_events.begin();
auto end = profiler->m_events.end();
while (start != end) {
auto event = *start++;
if (opt == Short)
event->printShort();
else
event->printFull();
}
}
}

View file

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.h_test.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

View file

@ -0,0 +1,41 @@
#include <iostream>
#include "heap.hpp"
using namespace std;
struct Node {
int value;
Node *left;
Node *right;
};
int getValue();
Node *createNode();
void insert();
int main() {
GC::Heap::init();
Node *node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
return 0;
}
int getValue() {
cout << "Enter a value to insert: ";
int value;
cin >> value;
return value;
}
Node *createNode() {
Node *node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
node->value = getValue();
return node;
}
void insert(Node *root) {
Node *node = createNode();
Node *curr = root;
while (curr)
}