From c532aecc2127f485081526a3dacfde1676eca847 Mon Sep 17 00:00:00 2001 From: DepressedTWM Date: Tue, 23 Jun 2026 13:51:17 +0400 Subject: [PATCH] Aded profiler efkdlgm --- CMakeLists.txt | 2 +- binding-mri/binding-mri.cpp | 74 ++++++++++++++++++++++++++++---- binding-mri/sunshine-binding.cpp | 6 +-- niga.rb | 9 ++++ scripts/Game_Party.rb | 4 +- scripts/Main.rb | 27 ++++++++++++ src/meow.cpp | 1 - 7 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 niga.rb diff --git a/CMakeLists.txt b/CMakeLists.txt index 417f824..48dfb22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ else() if (MSVC) add_compile_options(/O2 /fp:fast /GL /GF /GA) else() - add_compile_options(-Ofast -funroll-loops -fipa-pta -ftree-vectorize -fstdarg-opt -fomit-frame-pointer -frename-registers) + add_compile_options(-O2 -ffast-math -fipa-pta -ftree-vectorize -fstdarg-opt -fomit-frame-pointer -frename-registers) if(!WIN32) add_compile_options(-flto) endif() diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index aeaa3c7..daa1207 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -33,14 +33,67 @@ #include "meow.h" #include +#include #include #undef inline #include #include #include - +#include +#include +#include #include +typedef struct { + uint64_t start_ns; +} ud_t; + +//TODO rewrtire profiler bcz ChatGPT bcz WHERE IS DOCS ABOUT RUBY C API TRACEPOINT +static uint64_t now_ns(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; +} + +static const char *val_to_cstr(VALUE v, const char *fallback) { + if (NIL_P(v)) return fallback; + VALUE s = rb_funcall(v, rb_intern("to_s"), 0); + if (!RB_TYPE_P(s, T_STRING)) return fallback; + return RSTRING_PTR(s); +} + +static void tp_cb(VALUE tpval, void *data) { + ud_t *ud = (ud_t *)data; + + rb_trace_arg_t *trace_arg = rb_tracearg_from_tracepoint(tpval); + if (!trace_arg) return; + + VALUE path = rb_tracearg_path(trace_arg); + VALUE lineno = rb_tracearg_lineno(trace_arg); + VALUE defined_class = rb_tracearg_defined_class(trace_arg); + VALUE method_id = rb_tracearg_method_id(trace_arg); + rb_event_flag_t ev = rb_tracearg_event_flag(trace_arg); + + const char *path_s = val_to_cstr(path, ""); + const char *class_s = val_to_cstr(defined_class, ""); + + const char *meth_s = ""; + if (!NIL_P(method_id)) { + const char *mn = rb_id2name(SYM2ID(method_id)); + if (mn) meth_s = mn; + } + + long line = NIL_P(lineno) ? 0 : FIX2LONG(lineno); + + if (ev == RUBY_EVENT_CALL) { + ud->start_ns = now_ns(); + } else if (ev == RUBY_EVENT_RETURN) { + uint64_t end_ns = now_ns(); + uint64_t dur_ns = end_ns - ud->start_ns; + printf("[PROFILER] %s %s:%ld class=%s dur_ns=%" PRIu64 "\n", meth_s, path_s, line, class_s, dur_ns); + } +} + extern const char binding_mri_module_rpg1_rb[]; static void mriBindingExecute(); @@ -355,12 +408,8 @@ static void runRMXPScripts(BacktraceData &btData){ crash(Exception::MEOW, "Failed to read script data"); return; } - - /* Set the debug flag */ - rb_gv_set("$debug", conf.debugMode ? Qtrue : Qfalse); - - rb_gv_set("$RGSS_SCRIPTS", scriptArray); - + + rb_gv_set("$RGSS_SCRIPTS", scriptArray); long scriptCount = RARRAY_LEN(scriptArray); std::string decodeBuffer; decodeBuffer.resize(0x1000); @@ -507,8 +556,17 @@ static void mriBindingExecute(){ ruby_init(); ruby_init_loadpath(); rb_enc_set_default_external(rb_enc_from_encoding(rb_utf8_encoding())); - Config &conf = shState->rtData().config; + if(conf.debugMode){ + setvbuf(stdout, NULL, _IOFBF, 1<<20); + ud_t ud; + rb_event_flag_t flags = 0; + flags |= RUBY_EVENT_CALL; + flags |= RUBY_EVENT_RETURN; + flags |= RUBY_EVENT_RAISE; + VALUE tp = rb_tracepoint_new(Qnil, flags, tp_cb, &ud); + rb_tracepoint_enable(tp); + } if (!conf.rubyLoadpaths.empty()){ /* Setup custom load paths */ diff --git a/binding-mri/sunshine-binding.cpp b/binding-mri/sunshine-binding.cpp index 98cd0ea..d056461 100644 --- a/binding-mri/sunshine-binding.cpp +++ b/binding-mri/sunshine-binding.cpp @@ -2,7 +2,7 @@ #include #include #include "security.h" - +#include "config.h" //Просто на C реализуем методы мне в падлу ебаться со статической линковкой и прочим дерьмом. //Аминь. @@ -40,7 +40,6 @@ void SunshineBindingInit(){ rb_const_set(module, rb_intern("SDLVersion_minor"), INT2NUM(SDL_MINOR_VERSION)); rb_const_set(module, rb_intern("SDLVersion_micro"), INT2NUM(SDL_MICRO_VERSION)); rb_const_set(module, rb_intern("SECURITYSTATE"), rb_str_new_cstr(securitystate)); - //если методы доступны то просто не перезаписываем их if (!rb_respond_to(rb_cObject, rb_intern("class"))) { rb_define_method(rb_cObject, "class", rb_obj_class, 0); @@ -53,7 +52,4 @@ void SunshineBindingInit(){ if (!rb_respond_to(rb_cInteger, rb_intern("times"))) { rb_define_method(rb_cInteger, "times", RUBY_METHOD_FUNC(int_times), 0); } - if (!rb_respond_to(rb_cArray, rb_intern("last"))) { - rb_define_method(rb_cArray, "last", RUBY_METHOD_FUNC(a_last), 0); - } } diff --git a/niga.rb b/niga.rb new file mode 100644 index 0000000..c1e65d4 --- /dev/null +++ b/niga.rb @@ -0,0 +1,9 @@ +tp = TracePoint.new(:call) do |t| + # Для Ruby-методов: + next unless t.defined_class.is_a?(Module) # обычно всегда + puts "CALL #{t.defined_class}##{t.method_id} (line #{t.lineno})" +end + +tp.enable +p "idi naxui" +tp.disable diff --git a/scripts/Game_Party.rb b/scripts/Game_Party.rb index e4f2455..2bb78d9 100644 --- a/scripts/Game_Party.rb +++ b/scripts/Game_Party.rb @@ -114,9 +114,9 @@ class Game_Party if $game_followers.empty? $game_followers.push(Game_Follower.new($game_player, actor)) else - $game_followers.push(Game_Follower.new($game_followers.last, actor)) + $game_followers.push(Game_Follower.new($game_followers[-1], actor)) end - $scene.add_follower($game_followers.last) + $scene.add_follower($game_followers[-1]) end # Add actor @actors.push(actor) diff --git a/scripts/Main.rb b/scripts/Main.rb index 49e1326..e9f5d5b 100644 --- a/scripts/Main.rb +++ b/scripts/Main.rb @@ -11,6 +11,26 @@ at_exit do end begin + p RUBY_VERSION + if $debug + trace = TracePoint.new(:raise) do |tp| + p [tp.lineno, tp.event, tp.raised_exception] + end + trace.enable + + 0 / 0 + #tp = TracePoint.new(:call, :return) do |t| + # case t.event + # when :call + # puts "WRAP START (#{start_code}) -> #{t.defined_class}##{t.method_id}" + # when :return + # puts "WRAP END -> #{t.defined_class}##{t.method_id}" + # end + #end + #tp.enable + end + + RPG::Mod.exec_hooks("hooks/Main/start", binding) $console = Graphics.fullscreen Graphics.frame_rate = 60 @@ -51,9 +71,16 @@ begin if Journal.active? Journal.set '' end + + if $debug + tp.disable + end Oneshot.allow_exit true rescue Errno::ENOENT + if $debug + tp.disable + end # Supplement Errno::ENOENT exception # If unable to open file, display message and end filename = $!.message.sub("No such file or directory - ", "") diff --git a/src/meow.cpp b/src/meow.cpp index 43cdbb4..fec34aa 100644 --- a/src/meow.cpp +++ b/src/meow.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include