Skip to content

Instantly share code, notes, and snippets.

@timo

timo/moar.patch Secret

Created April 29, 2021 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/c15757c54db23ddadec34c7657c949bb to your computer and use it in GitHub Desktop.
Save timo/c15757c54db23ddadec34c7657c949bb to your computer and use it in GitHub Desktop.
add some tracy stuff to moarvm
diff --git a/src/core/fixedsizealloc.c b/src/core/fixedsizealloc.c
index e1a080ea2..4a60b1cdd 100644
--- a/src/core/fixedsizealloc.c
+++ b/src/core/fixedsizealloc.c
@@ -1,4 +1,5 @@
#include "moar.h"
+#include "../3rdparty/tracy/TracyC.h"
#include "memdebug.h"
@@ -196,9 +197,12 @@ void * MVM_fixed_size_alloc(MVMThreadContext *tc, MVMFixedSizeAlloc *al, size_t
if (fle) {
bin_ptr->free_list = fle->next;
bin_ptr->items--;
+ TracyCAllocNS(fle, bytes, 10, "fsa");
return (void *)fle;
}
- return alloc_from_global(tc, al, bin);
+ void * result = alloc_from_global(tc, al, bin);
+ TracyCAllocNS(result, bytes, 10, "fsa");
+ return result;
}
return MVM_malloc(bytes);
#endif
@@ -277,6 +281,7 @@ static void add_to_global_bin_freelist(MVMThreadContext *tc, MVMFixedSizeAlloc *
static void add_to_bin_freelist(MVMThreadContext *tc, MVMFixedSizeAlloc *al,
MVMint32 bin, void *to_free) {
MVMFixedSizeAllocThreadSizeClass *bin_ptr = &(tc->thread_fsa->size_classes[bin]);
+ TracyCFreeNS(to_free, 10, "fsa");
if (bin_ptr->items < MVM_FSA_THREAD_FREELIST_LIMIT) {
MVMFixedSizeAllocFreeListEntry *to_add = (MVMFixedSizeAllocFreeListEntry *)to_free;
to_add->next = bin_ptr->free_list;
diff --git a/src/core/loadbytecode.c b/src/core/loadbytecode.c
index 42b3cee1f..3105e217b 100644
--- a/src/core/loadbytecode.c
+++ b/src/core/loadbytecode.c
@@ -1,4 +1,5 @@
#include "moar.h"
+#include "../../3rdparty/tracy/TracyC.h"
/* Handles loading of bytecode, including triggering the deserialize and load
* special frames. Takes place in two steps, with a callback between them which
@@ -30,6 +31,8 @@ void MVM_load_bytecode_buffer(MVMThreadContext *tc, MVMObject *buf) {
MVMuint8 *data_start;
MVMuint32 data_size;
+ TracyCZone(load_bytecode_buffer, 1);
+
/* Ensure the source is in the correct form. */
if (
!IS_CONCRETE(buf)
@@ -48,6 +51,8 @@ void MVM_load_bytecode_buffer(MVMThreadContext *tc, MVMObject *buf) {
cu = MVM_cu_from_bytes(tc, data_start, data_size);
run_comp_unit(tc, cu);
+
+ TracyCZoneEnd(load_bytecode_buffer);
}
void MVM_load_bytecode_buffer_to_cu(MVMThreadContext *tc, MVMObject *buf, MVMRegister *res) {
MVMCompUnit *cu;
@@ -86,6 +91,7 @@ void MVM_load_bytecode_buffer_to_cu(MVMThreadContext *tc, MVMObject *buf, MVMReg
}
}
void MVM_load_bytecode(MVMThreadContext *tc, MVMString *filename) {
+ TracyCZone(load_bytecode_filename, 1);
/* Work out actual filename to use, taking --libpath into account. */
filename = MVM_file_in_libpath(tc, filename);
@@ -122,6 +128,7 @@ void MVM_load_bytecode(MVMThreadContext *tc, MVMString *filename) {
LEAVE:
MVM_tc_clear_ex_release_mutex(tc);
uv_mutex_unlock(&tc->instance->mutex_loaded_compunits);
+ TracyCZoneEnd(load_bytecode_filename);
}
void MVM_load_bytecode_fh(MVMThreadContext *tc, MVMObject *oshandle, MVMString *filename) {
MVMCompUnit *cu;
diff --git a/src/core/nativecall.c b/src/core/nativecall.c
index 35796a564..bd3f18c7b 100644
--- a/src/core/nativecall.c
+++ b/src/core/nativecall.c
@@ -3,6 +3,7 @@
#include <dlfcn.h>
#endif
#include <platform/time.h>
+#include "../../3rdparty/tracy/TracyC.h"
/* Grabs a NativeCall body. */
MVMNativeCallBody * MVM_nativecall_get_nc_body(MVMThreadContext *tc, MVMObject *obj) {
@@ -1188,6 +1189,8 @@ void MVM_nativecall_restore_library(MVMThreadContext *tc, MVMNativeCallBody *bod
void MVM_nativecall_invoke_jit(MVMThreadContext *tc, MVMObject *site) {
MVMNativeCallBody *body = MVM_nativecall_get_nc_body(tc, site);
+ TracyCZone(nativecall_invoke_jit, 1);
+
if (MVM_UNLIKELY(!body->lib_handle)) {
MVMROOT(tc, site, {
MVM_nativecall_restore_library(tc, body, site);
@@ -1199,4 +1202,5 @@ void MVM_nativecall_invoke_jit(MVMThreadContext *tc, MVMObject *site) {
assert(jitcode);
assert(jitcode->func_ptr);
jitcode->func_ptr(tc, *tc->interp_cu, jitcode->labels[0]);
+ TracyCZoneEnd(nativecall_invoke_jit);
}
diff --git a/src/core/nativecall_dyncall.c b/src/core/nativecall_dyncall.c
index 742e53208..c70494eda 100644
--- a/src/core/nativecall_dyncall.c
+++ b/src/core/nativecall_dyncall.c
@@ -2,6 +2,7 @@
#ifndef _WIN32
#include <dlfcn.h>
#endif
+#include "../../3rdparty/tracy/TracyC.h"
/* Maps a calling convention name to an ID. */
MVMint16 MVM_nativecall_get_calling_convention(MVMThreadContext *tc, MVMString *name) {
@@ -465,6 +466,8 @@ MVMObject * MVM_nativecall_invoke(MVMThreadContext *tc, MVMObject *res_type,
MVMint16 num_rws = 0;
MVMint16 i;
+ TracyCZone(nativecall_invoke, 1);
+
/* Get native call body, so we can locate the call info. Read out all we
* shall need, since later we may allocate a result and and move it. */
MVMNativeCallBody *body = MVM_nativecall_get_nc_body(tc, site);
@@ -480,6 +483,8 @@ MVMObject * MVM_nativecall_invoke(MVMThreadContext *tc, MVMObject *res_type,
void *entry_point = body->entry_point;
void *ptr = NULL;
+ TracyCZoneName(nativecall_invoke, body->sym_name, strlen(body->sym_name));
+
unsigned int interval_id;
DCCallVM *vm;
@@ -809,6 +814,7 @@ MVMObject * MVM_nativecall_invoke(MVMThreadContext *tc, MVMObject *res_type,
/* Finally, free call VM. */
dcFree(vm);
+ TracyCZoneEnd(nativecall_invoke);
MVM_telemetry_interval_stop(tc, interval_id, "nativecall invoke");
return result;
}
diff --git a/src/core/threads.c b/src/core/threads.c
index b199d84c2..f5982d361 100644
--- a/src/core/threads.c
+++ b/src/core/threads.c
@@ -1,4 +1,5 @@
#include "moar.h"
+#include "../../3rdparty/tracy/TracyC.h"
/* Temporary structure for passing data to thread start. */
typedef struct {
@@ -359,6 +360,7 @@ void MVM_thread_set_self_name(MVMThreadContext *tc, MVMString *name) {
char *c_name = MVM_string_utf8_c8_encode_C_string(tc, substring);
/* pthread man page says names are allowed to be 15 bytes long... */
if (strlen(c_name) > 0 && pthread_setname_np(pthread_self(), c_name) == 0) {
+ TracyCSetThreadName(c_name);
success = 1;
}
if (strlen(c_name) == 0) {
diff --git a/src/gc/orchestrate.c b/src/gc/orchestrate.c
index ae5cf9377..508137c26 100644
--- a/src/gc/orchestrate.c
+++ b/src/gc/orchestrate.c
@@ -1,5 +1,6 @@
#include "moar.h"
#include "platform/malloc_trim.h"
+#include "../../3rdparty/tracy/TracyC.h"
/* If we have the job of doing GC for a thread, we add it to our work
* list. */
@@ -422,10 +423,13 @@ static void run_gc(MVMThreadContext *tc, MVMuint8 what_to_do) {
/* Decide nursery or full collection. */
gen = tc->instance->gc_full_collect ? MVMGCGenerations_Both : MVMGCGenerations_Nursery;
+ TracyCZoneS(gcrun, 15, 1);
if (tc->instance->gc_full_collect) {
interval_id = MVM_telemetry_interval_start(tc, "start full collection");
+ TracyCZoneName(gcrun, "full_gcrun", strlen("full_gcrun"));
} else {
interval_id = MVM_telemetry_interval_start(tc, "start minor collection");
+ TracyCZoneName(gcrun, "minor_gcrun", strlen("minor_gcrun"));
}
if (is_coordinator)
@@ -484,6 +488,7 @@ static void run_gc(MVMThreadContext *tc, MVMuint8 what_to_do) {
MVM_repr_push_o(tc, tc->instance->subscriptions.subscription_queue, instance);
}
+ TracyCZoneEnd(gcrun);
MVM_telemetry_interval_stop(tc, interval_id, "finished run_gc");
}
diff --git a/src/io/asyncsocket.c b/src/io/asyncsocket.c
index 642925d29..5c42abad5 100644
--- a/src/io/asyncsocket.c
+++ b/src/io/asyncsocket.c
@@ -1,4 +1,5 @@
#include "moar.h"
+#include "../../3rdparty/tracy/TracyC.h"
/* Data that we keep for an asynchronous socket handle. */
typedef struct {
@@ -34,6 +35,10 @@ static void on_read(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
MVMObject *arr = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTArray);
MVMAsyncTask *t = MVM_io_eventloop_get_active_work(tc, ri->work_idx);
MVM_repr_push_o(tc, arr, t->body.schedulee);
+
+ TracyCZone(on_read, 1);
+ TracyCZoneValue(on_read, nread);
+
if (nread >= 0) {
MVMROOT2(tc, t, arr, {
MVMArray *res_buf;
@@ -87,6 +92,7 @@ static void on_read(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
}
}
MVM_repr_push_o(tc, t->body.queue, arr);
+ TracyCZoneEnd(on_read);
}
/* Does setup work for setting up asynchronous reads. */
@@ -235,6 +241,8 @@ static void on_write(uv_write_t *req, int status) {
MVMObject *arr = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTArray);
MVMAsyncTask *t = MVM_io_eventloop_get_active_work(tc, wi->work_idx);
MVM_repr_push_o(tc, arr, t->body.schedulee);
+ TracyCZone(on_write, 1);
+ TracyCZoneValue(on_write, wi->buf.len);
if (status >= 0) {
MVMROOT2(tc, arr, t, {
MVMObject *bytes_box = MVM_repr_box_int(tc,
@@ -257,6 +265,7 @@ static void on_write(uv_write_t *req, int status) {
MVM_repr_push_o(tc, t->body.queue, arr);
MVM_free(wi->req);
MVM_io_eventloop_remove_active_work(tc, &(wi->work_idx));
+ TracyCZoneEnd(on_write);
}
/* Does setup work for an asynchronous write. */
diff --git a/src/io/eventloop.c b/src/io/eventloop.c
index b6edadbdd..5f9ea4a23 100644
--- a/src/io/eventloop.c
+++ b/src/io/eventloop.c
@@ -1,4 +1,5 @@
#include "moar.h"
+#include "../../3rdparty/tracy/TracyC.h"
/* Asynchronous I/O, timers, file system notifications and signal handlers
* have their callbacks processed by this event loop. Its job is mostly to
@@ -72,10 +73,12 @@ static void cancel_work(MVMThreadContext *tc) {
* cancellation for the event loop to process. */
static void async_handler(uv_async_t *handle) {
MVMThreadContext *tc = (MVMThreadContext *)handle->data;
+ TracyCZone(eventloop_handler, 1);
GC_SYNC_POINT(tc);
setup_work(tc);
permit_work(tc);
cancel_work(tc);
+ TracyCZoneEnd(eventloop_handler);
}
/* Enters the event loop. */
@@ -85,6 +88,7 @@ static void enter_loop(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister
#ifdef MVM_HAS_PTHREAD_SETNAME_NP
pthread_setname_np(pthread_self(), "async io thread");
+ TracyCSetThreadName("async io thread");
#endif
/* Bind the thread context for the wakeup signal */
diff --git a/src/io/syncfile.c b/src/io/syncfile.c
index f049a992b..fb1454742 100644
--- a/src/io/syncfile.c
+++ b/src/io/syncfile.c
@@ -1,5 +1,6 @@
#include "moar.h"
#include "platform/io.h"
+#include "../../3rdparty/tracy/TracyC.h"
#ifndef _WIN32
#include <sys/types.h>
@@ -71,6 +72,7 @@ static MVMint64 mvm_fileno(MVMThreadContext *tc, MVMOSHandle *h) {
static void perform_write(MVMThreadContext *tc, MVMIOFileData *data, char *buf, MVMint64 bytes) {
MVMint64 bytes_written = 0;
MVM_gc_mark_thread_blocked(tc);
+ TracyCZoneS(perform_write, 5, 1);
while (bytes > 0) {
int r;
do {
@@ -86,6 +88,7 @@ static void perform_write(MVMThreadContext *tc, MVMIOFileData *data, char *buf,
buf += r;
bytes -= r;
}
+ TracyCZoneEnd(perform_write);
MVM_gc_mark_thread_unblocked(tc);
data->byte_position += bytes_written;
data->known_writable = 1;
@@ -130,6 +133,7 @@ static MVMint64 read_bytes(MVMThreadContext *tc, MVMOSHandle *h, char **buf_out,
MVMIOFileData *data = (MVMIOFileData *)h->body.data;
char *buf = MVM_malloc(bytes);
unsigned int interval_id = MVM_telemetry_interval_start(tc, "syncfile.read_to_buffer");
+ TracyCZoneS(read_bytes, 5, 1);
MVMint32 bytes_read;
#ifdef _WIN32
/* Can only perform relatively small reads from a Windows console;
@@ -153,6 +157,7 @@ static MVMint64 read_bytes(MVMThreadContext *tc, MVMOSHandle *h, char **buf_out,
*buf_out = buf;
MVM_telemetry_interval_annotate(bytes_read, interval_id, "read this many bytes");
MVM_telemetry_interval_stop(tc, interval_id, "syncfile.read_to_buffer");
+ TracyCZoneEnd(read_bytes);
data->byte_position += bytes_read;
if (bytes_read == 0 && bytes != 0)
data->eof_reported = 1;
diff --git a/src/main.c b/src/main.c
index 784dc60ad..ab4d3c01d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <moar.h>
#include "platform/io.h"
+#include "../3rdparty/tracy/TracyC.h"
#if MVM_TRACING
# define TRACING_OPT "[--tracing] "
@@ -277,6 +278,8 @@ int wmain(int argc, wchar_t *wargv[])
return EXIT_FAILURE;
}
+ TracyCZone(ctx, 1);
+
instance = MVM_vm_create_instance();
input_file = argv[argi++];
@@ -311,6 +314,8 @@ int wmain(int argc, wchar_t *wargv[])
}
#endif
+ TracyCZoneEnd(ctx);
+
if (full_cleanup) {
MVM_vm_destroy_instance(instance);
return EXIT_SUCCESS;
diff --git a/src/spesh/worker.c b/src/spesh/worker.c
index 1808b3d77..f006c66f9 100644
--- a/src/spesh/worker.c
+++ b/src/spesh/worker.c
@@ -1,4 +1,5 @@
#include "moar.h"
+#include "../../3rdparty/tracy/TracyC.h"
/* The specialization worker thread receives logs from other threads about
* calls and types that showed up at runtime. It uses this to produce
@@ -17,6 +18,7 @@ static void worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *arg
#ifdef MVM_HAS_PTHREAD_SETNAME_NP
pthread_setname_np(pthread_self(), "spesh optimizer");
+ TracyCSetThreadName("spesh optimizer");
#endif
tc->instance->speshworker_thread_id = tc->thread_obj->body.thread_id;
@@ -65,6 +67,7 @@ static void worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *arg
if (tc->instance->main_thread->prof_data)
MVM_profiler_log_spesh_start(tc);
+ TracyCZone(speshwork, 1);
interval_id = MVM_telemetry_interval_start(tc, "spesh worker consuming a log");
@@ -208,11 +211,13 @@ static void worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *arg
}
else if (MVM_is_null(tc, log_obj)) {
/* This is a stop signal, so quit processing */
+ TracyCZoneEnd(speshwork);
break;
} else {
MVM_panic(1, "Unexpected object sent to specialization worker");
}
+ TracyCZoneEnd(speshwork);
MVM_telemetry_interval_stop(tc, interval_id, "spesh worker finished");
if (overview_data) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment