Skip to content

Commit

Permalink
Started profiling stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mls-m5 committed Nov 2, 2023
1 parent ad647c2 commit 57905e3
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 41 deletions.
21 changes: 12 additions & 9 deletions src/core/jobqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ class JobQueue : public IJobQueue {
}

void work(bool shouldWait = true) override {
auto duration = ProfileDuration{};
if (shouldWait) {
_waitMutex.lock();
}
while (!_queue.empty() && _running) {
auto task = std::move(_queue.front());
_queue.pop();
task();
{
auto duration = ProfileDuration{};
if (shouldWait) {
_waitMutex.lock();
}
while (!_queue.empty() && _running) {
auto task = std::move(_queue.front());
_queue.pop();
task();
}
}

if (shouldWait) {
// auto duration = ProfileDuration{"JobQueue-wait"};
wait();
}
}
Expand All @@ -65,6 +68,7 @@ class JobQueue : public IJobQueue {
private:
//! Run and lock the current thread
void loop() {
// auto profileDuration = ProfileDuration{};
setThreadName("jobs");
_threadId = std::this_thread::get_id();
while (_running) {
Expand Down Expand Up @@ -93,5 +97,4 @@ class JobQueue : public IJobQueue {
bool _running = true;
std::thread::id _threadId = {};
std::thread _thread;
ProfileDuration _profileDuration{};
};
21 changes: 21 additions & 0 deletions src/core/profiler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "profiler.h"
#include "core/threadname.h"
#include <chrono>
#include <cstdint>
#include <fstream>
Expand Down Expand Up @@ -66,6 +67,7 @@ struct ProfiledData {
std::vector<DebugFrame> frames;
std::vector<InstantData> instants;
int id = generateProfilingThreadId();
std::string name;
};

struct GlobalData {
Expand Down Expand Up @@ -98,11 +100,23 @@ struct GlobalData {

auto file = std::ofstream{"medit_profile_log.json"};
file << "[\n";

for (auto &data : threadFrameDatas) {
// Output the thread's meta information

file << "{";
file << "\"name\": \"thread_name\", ";
file << "\"ph\": \"M\", ";
file << "\"pid\": 1, "; // Assuming process id is 1
file << "\"tid\": " << data->id << ", ";
file << "\"args\": {\"name\": \"" << data->name << "\"}";
file << "}";

for (auto &frame : data->frames) {
file << frame << ",\n";
}
}

file << "{}]\n";
threadFrameDatas.clear();
}
Expand Down Expand Up @@ -199,3 +213,10 @@ void profileInstant(std::string_view value) {
}
localProfilingThreadData.instant(value);
}

void setProfilerThreadName(std::string name) {
if (!shouldEnableProfiling || !isThreadInitialized) {
return;
}
localProfilingThreadData.data->name = std::move(name);
}
5 changes: 5 additions & 0 deletions src/core/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ void profileInstant(std::string_view value);

/// Just called once from main
void enableProfiling();

void setProfilerThreadName(std::string name);

#define PROFILE_FUNCTION() \
auto profileDurationScopeVariable = ProfileDuration {}
2 changes: 2 additions & 0 deletions src/core/threadname.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "threadname.h"
#include "core/profiler.h"
#include "os.h"
#include <array>
#include <string>
Expand All @@ -11,6 +12,7 @@ void setThreadName(const std::string &name) {
#ifdef MEDIT_USING_LINUX
pthread_setname_np(pthread_self(), name.c_str());
#endif
setProfilerThreadName(name);
}

std::string getThreadName() {
Expand Down
3 changes: 3 additions & 0 deletions src/core/timer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "core/timer.h"
#include "core/profiler.h"
#include "core/threadname.h"
#include <algorithm>
#include <thread>
Expand Down Expand Up @@ -52,12 +53,14 @@ void Timer::stop() {
}

void Timer::loop() {
// auto duration = ProfileDuration{};
setThreadName("timer");
_isRunning = true;
while (_isRunning) {
{
_mutex.lock();
while (!_triggers.empty()) {
auto duration = ProfileDuration{"Timer-task"};
auto nextTime = _triggers.front().time;
if (nextTime < std::chrono::system_clock::now()) {
auto next = std::move(_triggers.front());
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/lsp/lspplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "core/coreenvironment.h"
#include "core/ijobqueue.h"
#include "core/plugins.h"
#include "core/profiler.h"
#include "files/project.h"
#include "lsp/clientnotifications.h"
#include "lsp/lspclient.h"
Expand Down Expand Up @@ -230,6 +231,7 @@ LspPlugin::Instance *LspPlugin::createInstance(std::filesystem::path path) {

void LspPlugin::handleSemanticsTokens(std::shared_ptr<Buffer> buffer,
std::vector<long> data) {
auto duration = ProfileDuration{};

struct Item {
long *data;
Expand Down Expand Up @@ -318,6 +320,7 @@ void LspPlugin::handleSemanticsTokens(std::shared_ptr<Buffer> buffer,

void LspPlugin::requestSemanticsToken(std::shared_ptr<Buffer> buffer,
Instance &instance) {
auto duration = ProfileDuration{};
auto params = SemanticTokensParams{};
params.textDocument.uri = pathToUri(buffer->path());

Expand All @@ -342,6 +345,7 @@ void LspPlugin::requestSemanticsToken(std::shared_ptr<Buffer> buffer,

bool LspPlugin::updateBuffer(Buffer &buffer) {
// TODO: Only update buffers with changed revision
auto duration = ProfileDuration{};

auto i = instance(buffer.path());
if (!i) {
Expand Down
6 changes: 6 additions & 0 deletions src/screen/deserializescreen.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "deserializescreen.h"
#include "core/inarchive.h"
#include "core/outarchive.h"
#include "core/profiler.h"
#include "screen/cursorstyle.h"
#include "syntax/palette.h"
#include <sstream>
Expand All @@ -24,14 +25,17 @@ void DeserializeScreen::close() {
}

void DeserializeScreen::write(std::string_view data) {
PROFILE_FUNCTION();
handle(data);
}

void DeserializeScreen::unsubscribe() {
PROFILE_FUNCTION();
_callback = {};
}

void DeserializeScreen::handle(std::string_view str) {
PROFILE_FUNCTION();
auto ss = std::istringstream{std::string{str}};
auto arch = InArchive{ss};

Expand Down Expand Up @@ -121,6 +125,7 @@ void DeserializeScreen::handle(std::string_view str) {
}

void DeserializeScreen::send(std::string_view str) {
PROFILE_FUNCTION();
// auto ss = std::stringstream{};
// ss << data;
if (!_callback) {
Expand All @@ -136,6 +141,7 @@ void DeserializeScreen::send(std::string_view str) {
}

void DeserializeScreen::screenCallback(IScreen::EventListT list) {
PROFILE_FUNCTION();

auto ss = std::ostringstream{};
{
Expand Down
79 changes: 48 additions & 31 deletions src/screen/guiscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ struct GuiScreen::Buffer {

// Make sure that the bottom line aligns with the window border
void drawBottomLine(sdl::RendererView renderer) {
auto d = ProfileDuration{};
screen.render(renderer,
0,
pixelHeight - screen.cache.charHeight,
Expand All @@ -308,44 +309,58 @@ struct GuiScreen::Buffer {

auto l = std::lock_guard{refreshMutex};
_tv();
for (size_t y = 0; y < shownLines.size(); ++y) {
renderLine(y, shownLines.at(y));
}
{
auto d = ProfileDuration{"Draw"};

{
auto d = ProfileDuration{"Render Lines"};
for (size_t y = 0; y < shownLines.size(); ++y) {
renderLine(y, shownLines.at(y));
}
}

renderer.drawColor(_styles.front().bg);
renderer.fillRect();
renderer.drawColor(_styles.front().bg);
renderer.fillRect();

auto rect =
sdl::Rect{0, 0, screen.canvas.width, screen.canvas.height - 1};
screen.render(renderer, 0, 0, rect);
drawBottomLine(renderer);
auto rect =
sdl::Rect{0, 0, screen.canvas.width, screen.canvas.height - 1};
{
auto d = ProfileDuration{"RenderScreen"};
screen.render(renderer, 0, 0, rect);
}
drawBottomLine(renderer);

renderer.drawColor(sdl::White);
renderer.drawColor(sdl::White);

auto cellWidth = screen.cache.charWidth;
auto cellHeight = screen.cache.charHeight;
auto cellWidth = screen.cache.charWidth;
auto cellHeight = screen.cache.charHeight;

switch (cursorStyle) {
case CursorStyle::Beam:
renderer.fillRect(
sdl::Rect{static_cast<int>(cellWidth * cursorPos.x()),
static_cast<int>(cellHeight * cursorPos.y()),
1,
static_cast<int>(cellHeight)});
break;
default:
// renderer.fillRect(
// sdl::Rect{static_cast<int>(cellWidth *
// cursorPos.x()),
// static_cast<int>(cellHeight *
// cursorPos.y()),
// static_cast<int>(cellWidth),
// static_cast<int>(cellHeight)});
screen.renderCursor(renderer, rect, cursorPos.x(), cursorPos.y());
break;
switch (cursorStyle) {
case CursorStyle::Beam:
renderer.fillRect(
sdl::Rect{static_cast<int>(cellWidth * cursorPos.x()),
static_cast<int>(cellHeight * cursorPos.y()),
1,
static_cast<int>(cellHeight)});
break;
default:
// renderer.fillRect(
// sdl::Rect{static_cast<int>(cellWidth *
// cursorPos.x()),
// static_cast<int>(cellHeight *
// cursorPos.y()),
// static_cast<int>(cellWidth),
// static_cast<int>(cellHeight)});
screen.renderCursor(
renderer, rect, cursorPos.x(), cursorPos.y());
break;
}
}

renderer.present();
{
auto d = ProfileDuration{"Present"};
renderer.present();
}
}

size_t addStyle(const Color &fg, const Color &bg, size_t index) {
Expand Down Expand Up @@ -418,6 +433,8 @@ struct GuiScreen::Buffer {
}
}();

auto duration = ProfileDuration{};

if (!e) {
return NullEvent{};
}
Expand Down
Loading

0 comments on commit 57905e3

Please sign in to comment.