Work on the profiler

This commit is contained in:
Victor Olin 2023-03-10 09:24:32 +01:00
parent cdc802476d
commit 51765f4d0c
6 changed files with 101 additions and 49 deletions

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <chrono> #include <time.h>
#include <iostream> #include <iostream>
#include <list> #include <list>
@ -20,34 +20,31 @@ namespace GC
ReusedChunk ReusedChunk
}; };
using TimeStamp = std::chrono::_V2::system_clock::time_point;
class GCEvent class GCEvent
{ {
private: private:
// make const // make const
GCEventType m_type; GCEventType m_type;
TimeStamp m_timestamp; std::time_t m_timestamp;
Chunk *m_chunk; Chunk *m_chunk;
public: public:
GCEvent(GCEventType type) GCEvent(GCEventType type)
{ {
m_type = type; m_type = type;
m_timestamp = std::chrono::system_clock::now(); m_timestamp = std::time(NULL);
} }
GCEvent(GCEventType type, Chunk *chunk) GCEvent(GCEventType type, Chunk *chunk)
{ {
m_type = type; m_type = type;
m_timestamp = std::chrono::system_clock::now(); m_timestamp = std::time(NULL);
m_chunk = chunk; m_chunk = chunk;
} }
GCEventType getType(); GCEventType getType();
TimeStamp getTimeStamp(); std::time_t getTimeStamp();
Chunk *getChunk(); Chunk *getChunk();
const char *TypeToString();
void print(std::ostream &out);
}; };
} }

View file

@ -24,7 +24,6 @@ namespace GC
{ {
private: private:
// Private constructor according to the singleton pattern
Heap() Heap()
{ {
m_heap = static_cast<char *>(malloc(HEAP_SIZE)); m_heap = static_cast<char *>(malloc(HEAP_SIZE));
@ -32,14 +31,13 @@ namespace GC
m_allocated_size = 0; m_allocated_size = 0;
} }
// BEWARE only for testing, this should be adressed
~Heap() ~Heap()
{ {
std::free((char *)m_heap); std::free((char *)m_heap);
} }
inline static Heap *the() 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();
@ -71,7 +69,6 @@ namespace GC
uintptr_t *m_stack_top = nullptr; uintptr_t *m_stack_top = nullptr;
bool m_profiler_enable = false; bool m_profiler_enable = false;
// maybe change to std::list
std::vector<Chunk *> m_allocated_chunks; std::vector<Chunk *> m_allocated_chunks;
std::vector<Chunk *> m_freed_chunks; std::vector<Chunk *> m_freed_chunks;
@ -89,7 +86,7 @@ namespace GC
// DEBUG ONLY // DEBUG ONLY
static inline Heap *debug_the() static inline Heap *debug_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();

View file

@ -23,9 +23,11 @@ namespace GC {
inline static Profiler *m_instance = nullptr; inline static Profiler *m_instance = nullptr;
std::vector<GCEvent *> m_events; std::vector<GCEvent *> m_events;
std::ofstream createFileStream();
public: public:
static void record(GCEventType type); static void record(GCEventType type);
static void record(GCEventType type, Chunk *chunk); static void record(GCEventType type, Chunk *chunk);
static void printTrace(); static void dumpTrace();
}; };
} }

View file

@ -6,21 +6,36 @@
#include "event.hpp" #include "event.hpp"
#include "heap.hpp" #include "heap.hpp"
namespace GC { namespace GC
{
GCEventType GCEvent::getType() {
GCEventType GCEvent::getType()
{
return m_type; return m_type;
} }
TimeStamp GCEvent::getTimeStamp() { std::time_t GCEvent::getTimeStamp()
{
return m_timestamp; return m_timestamp;
} }
Chunk *GCEvent::getChunk() { Chunk *GCEvent::getChunk()
{
return m_chunk; return m_chunk;
} }
void GCEvent::print(std::ostream &out) { inline const char *GCEvent::TypeToString()
assert(false && "TODO: unimplemented"); {
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]";
}
} }
} }

View file

@ -1,4 +1,8 @@
#include <ctime>
#include <cstring>
#include <iostream> #include <iostream>
#include <fstream>
#include <time.h>
#include <vector> #include <vector>
#include "chunk.hpp" #include "chunk.hpp"
@ -6,26 +10,49 @@
#include "heap.hpp" #include "heap.hpp"
#include "profiler.hpp" #include "profiler.hpp"
namespace GC { namespace GC
void Profiler::record(GCEventType type) { {
void Profiler::record(GCEventType type)
{
auto event = new GCEvent(type); auto event = new GCEvent(type);
auto profiler = Profiler::the(); auto profiler = Profiler::the();
profiler->m_events.push_back(event); 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 event = new GCEvent(type, chunk);
auto profiler = Profiler::the(); auto profiler = Profiler::the();
profiler->m_events.push_back(event); profiler->m_events.push_back(event);
} }
void Profiler::printTrace() { void Profiler::dumpTrace()
{
auto profiler = Profiler::the(); auto profiler = Profiler::the();
auto start = profiler->m_events.begin(); auto start = profiler->m_events.begin();
auto end = profiler->m_events.end(); auto end = profiler->m_events.end();
while (start != end) {
std::ofstream fstr = profiler->createFileStream();
while (start != end)
{
auto event = *start++; 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;
}
} }

View file

@ -1,34 +1,48 @@
#include <chrono>
#include <cstring>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <time.h>
#include <stdlib.h> #include <stdlib.h>
using namespace std;
int main() { int main() {
list<char> l; // list<char> l;
char c = 'a'; // char c = 'a';
for (int i = 1; i <= 5; i++) { // for (int i = 1; i <= 5; i++) {
l.push_back(c++); // l.push_back(c++);
} // }
auto iter = l.begin(); // auto iter = l.begin();
auto stop = l.end(); // auto stop = l.end();
while (iter != stop) { // while (iter != stop) {
cout << *iter << " "; // cout << *iter << " ";
iter++; // iter++;
} // }
cout << endl; // cout << endl;
iter = l.begin(); // iter = l.begin();
while (*iter != *stop) { // while (*iter != *stop) {
cout << *iter << " "; // cout << *iter << " ";
iter++; // iter++;
} // }
cout << endl; // cout << endl;
cout << "rebased" << endl; // cout << "rebased" << endl;
// cout << "iter: " << *iter << "\nstop: " << *stop << 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; return 0;
} }