diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-05-21 21:18:16 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-05-21 21:18:16 -0300 |
commit | a75f8963380a1f6ae8501f21a1d3f3bef8a89d91 (patch) | |
tree | ae561ded247f81565c8287b6fd4b816f6ec762e6 /tools | |
parent | c195c0df6b36debc870216dd42e49fbda70fa861 (diff) |
First version of Profiler
It is now possible to profile GDScript as well as some parts of Godot
internals.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/editor/editor_node.cpp | 17 | ||||
-rw-r--r-- | tools/editor/editor_node.h | 4 | ||||
-rw-r--r-- | tools/editor/editor_profiler.cpp | 753 | ||||
-rw-r--r-- | tools/editor/editor_profiler.h | 143 | ||||
-rw-r--r-- | tools/editor/script_editor_debugger.cpp | 231 | ||||
-rw-r--r-- | tools/editor/script_editor_debugger.h | 12 |
6 files changed, 1147 insertions, 13 deletions
diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index fc676bda7e..25c854ce56 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -187,7 +187,7 @@ void EditorNode::_unhandled_input(const InputEvent& p_event) { break; case KEY_F5: _menu_option_confirm((p_event.key.mod.control&&p_event.key.mod.shift)?RUN_PLAY_CUSTOM_SCENE:RUN_PLAY,true); break; case KEY_F6: _menu_option_confirm(RUN_PLAY_SCENE,true); break; - case KEY_F7: _menu_option_confirm(RUN_PAUSE,true); break; + //case KEY_F7: _menu_option_confirm(RUN_PAUSE,true); break; case KEY_F8: _menu_option_confirm(RUN_STOP,true); break; } @@ -2636,11 +2636,6 @@ void EditorNode::_menu_option_confirm(int p_option,bool p_confirmed) { } } break; - case RUN_PAUSE: { - - emit_signal("pause_pressed"); - - } break; case RUN_STOP: { if (editor_run.get_status()==EditorRun::STATUS_STOP) @@ -5597,14 +5592,16 @@ EditorNode::EditorNode() { - /*pause_button = memnew( ToolButton ); + pause_button = memnew( ToolButton ); //menu_panel->add_child(pause_button); - not needed for now? pause_button->set_toggle_mode(true); pause_button->set_icon(gui_base->get_icon("Pause","EditorIcons")); pause_button->set_focus_mode(Control::FOCUS_NONE); - pause_button->connect("pressed", this,"_menu_option",make_binds(RUN_PAUSE)); - pause_button->set_tooltip(TTR("Pause the scene (F7).")); -*/ + //pause_button->connect("pressed", this,"_menu_option",make_binds(RUN_PAUSE)); + pause_button->set_tooltip(TTR("Pause the scene")); + pause_button->set_disabled(true); + play_hb->add_child(pause_button); + stop_button = memnew( ToolButton ); play_hb->add_child(stop_button); //stop_button->set_toggle_mode(true); diff --git a/tools/editor/editor_node.h b/tools/editor/editor_node.h index 8fa6cfefcf..5bd6ef0bdf 100644 --- a/tools/editor/editor_node.h +++ b/tools/editor/editor_node.h @@ -159,7 +159,7 @@ private: OBJECT_CALL_METHOD, OBJECT_REQUEST_HELP, RUN_PLAY, - RUN_PAUSE, + RUN_STOP, RUN_PLAY_SCENE, RUN_PLAY_NATIVE, @@ -692,6 +692,8 @@ public: void update_keying(); + ToolButton *get_pause_button() { return pause_button; } + ToolButton* add_bottom_panel_item(String p_text,Control *p_item); bool are_bottom_panels_hidden() const; diff --git a/tools/editor/editor_profiler.cpp b/tools/editor/editor_profiler.cpp new file mode 100644 index 0000000000..955af6b603 --- /dev/null +++ b/tools/editor/editor_profiler.cpp @@ -0,0 +1,753 @@ +#include "editor_profiler.h" +#include "editor_settings.h" +#include "os/os.h" + +void EditorProfiler::_make_metric_ptrs(Metric& m) { + + for(int i=0;i<m.categories.size();i++) { + m.category_ptrs[m.categories[i].signature]=&m.categories[i]; + for(int j=0;j<m.categories[i].items.size();j++) { + m.item_ptrs[m.categories[i].items[j].signature]=&m.categories[i].items[j]; + } + } +} + +void EditorProfiler::add_frame_metric(const Metric& p_metric,bool p_final) { + + ++last_metric; + if (last_metric>=frame_metrics.size()) + last_metric=0; + + + frame_metrics[last_metric]=p_metric; + _make_metric_ptrs(frame_metrics[last_metric]); + + updating_frame=true; + cursor_metric_edit->set_max(frame_metrics[last_metric].frame_number); + cursor_metric_edit->set_min(MAX(frame_metrics[last_metric].frame_number-frame_metrics.size(),0)); + + + if (!seeking) { + cursor_metric_edit->set_val(frame_metrics[last_metric].frame_number); + if (hover_metric!=-1) { + hover_metric++; + if (hover_metric>=frame_metrics.size()) { + hover_metric=0; + } + } + + } + updating_frame=false; + + if (!frame_delay->is_processing()) { + + frame_delay->set_wait_time(p_final?0.1:1); + frame_delay->start(); + } + + if (!plot_delay->is_processing()) { + plot_delay->set_wait_time(0.1); + plot_delay->start(); + } + +} + + + +void EditorProfiler::clear() { + + int metric_size=EditorSettings::get_singleton()->get("debugger/profiler_frame_history_size"); + metric_size = CLAMP(metric_size,60,1024); + frame_metrics.clear(); + frame_metrics.resize(metric_size); + last_metric=-1; + variables->clear(); + //activate->set_pressed(false); + plot_sigs.clear(); + plot_sigs.insert("fixed_frame_time"); + plot_sigs.insert("category_frame_time"); + + updating_frame=true; + cursor_metric_edit->set_min(0); + cursor_metric_edit->set_max(0); + cursor_metric_edit->set_val(0); + updating_frame=false; + hover_metric=-1; + seeking=false; +} + +static String _get_percent_txt(float p_value,float p_total) { + if (p_total==0) + p_total=0.00001; + return String::num((p_value/p_total)*100,1)+"%"; +} + + +String EditorProfiler::_get_time_as_text(Metric &m,float p_time,int p_calls) { + + int dmode = display_mode->get_selected(); + + + if (dmode==DISPLAY_FRAME_TIME) { + return rtos(p_time); + } else if (dmode==DISPLAY_AVERAGE_TIME) { + if (p_calls==0) + return "0"; + else + return rtos(p_time/p_calls); + } else if (dmode==DISPLAY_FRAME_PERCENT) { + return _get_percent_txt(p_time,m.frame_time); + } else if (dmode==DISPLAY_FIXED_FRAME_PERCENT) { + + return _get_percent_txt(p_time,m.fixed_frame_time); + } + + return "err"; +} + +Color EditorProfiler::_get_color_from_signature(const StringName& p_signature) const { + + double rot = ABS(double(p_signature.hash())/double(0x7FFFFFFF)); + Color c; + c.set_hsv(rot,1,1); + return c; + +} + +void EditorProfiler::_item_edited() { + + if (updating_frame) + return; + + TreeItem *item=variables->get_edited(); + if (!item) + return; + StringName signature=item->get_metadata(0); + bool checked=item->is_checked(0); + + + if (checked) + plot_sigs.insert(signature); + else + plot_sigs.erase(signature); + + if (!frame_delay->is_processing()) { + frame_delay->set_wait_time(0.1); + frame_delay->start(); + } +} + +void EditorProfiler::_update_plot() { + + int w = graph->get_size().width; + int h = graph->get_size().height; + + bool reset_texture=false; + + int desired_len = w * h * 4; + + if (graph_image.size()!=desired_len) { + reset_texture=true; + graph_image.resize(desired_len); + } + + + DVector<uint8_t>::Write wr = graph_image.write(); + + + + //clear + for(int i=0;i<desired_len;i+=4) { + wr[i+0]=0; + wr[i+1]=0; + wr[i+2]=0; + wr[i+3]=255; + } + + + //find highest value + + bool use_self = display_time->get_selected()==DISPLAY_SELF_TIME; + float highest=0; + + for(int i=0;i<frame_metrics.size();i++) { + Metric &m = frame_metrics[i]; + if (!m.valid) + continue; + + for (Set<StringName>::Element *E=plot_sigs.front();E;E=E->next()) { + + Map<StringName,Metric::Category*>::Element *F=m.category_ptrs.find(E->get()); + if (F) { + highest=MAX(F->get()->total_time,highest); + } + + Map<StringName,Metric::Category::Item*>::Element *G=m.item_ptrs.find(E->get()); + if (G) { + if (use_self) { + highest=MAX(G->get()->self,highest); + } else { + highest=MAX(G->get()->total,highest); + } + } + } + } + + if (highest>0) { + //means some data exists.. + highest*=1.2; //leave some upper room + graph_height=highest; + + Vector<int> columnv; + columnv.resize(h*4); + + int *column = columnv.ptr(); + + Map<StringName,int> plot_prev; + //Map<StringName,int> plot_max; + + uint64_t time = OS::get_singleton()->get_ticks_usec(); + + for(int i=0;i<w;i++) { + + + for(int j=0;j<h*4;j++) { + column[j]=0; + } + + int current = i*frame_metrics.size()/w; + int next = (i+1)*frame_metrics.size()/w; + if (next>frame_metrics.size()) { + next=frame_metrics.size(); + } + if (next==current) + next=current+1; //just because for loop must work + + for (Set<StringName>::Element *E=plot_sigs.front();E;E=E->next()) { + + int plot_pos=-1; + + for(int j=current;j<next;j++) { + + //wrap + int idx = last_metric+1+j; + while( idx >= frame_metrics.size() ) { + idx-=frame_metrics.size(); + } + + //get + Metric &m = frame_metrics[idx]; + if (m.valid==false) + continue; //skip because invalid + + + float value=0; + + Map<StringName,Metric::Category*>::Element *F=m.category_ptrs.find(E->get()); + if (F) { + value=F->get()->total_time; + } + + Map<StringName,Metric::Category::Item*>::Element *G=m.item_ptrs.find(E->get()); + if (G) { + if (use_self) { + value=G->get()->self; + } else { + value=G->get()->total; + } + } + + + plot_pos = MAX( CLAMP(int(value*h/highest),0,h-1), plot_pos ); + + + } + + int prev_plot=plot_pos; + Map<StringName,int>::Element *H=plot_prev.find(E->get()); + if (H) { + prev_plot=H->get(); + H->get()=plot_pos; + } else { + plot_prev[E->get()]=plot_pos; + } + + if (plot_pos==-1 && prev_plot==-1) { + //don't bother drawing + continue; + } + + if (prev_plot!=-1 && plot_pos==-1) { + + plot_pos=prev_plot; + } + + if (prev_plot==-1 && plot_pos!=-1) { + prev_plot=plot_pos; + } + + plot_pos = h- plot_pos -1; + prev_plot = h- prev_plot -1; + + if (prev_plot > plot_pos) { + SWAP(prev_plot,plot_pos); + } + + Color col = _get_color_from_signature(E->get()); + + for(int j=prev_plot;j<=plot_pos;j++) { + + column[j*4+0]+=Math::fast_ftoi(CLAMP(col.r*255,0,255)); + column[j*4+1]+=Math::fast_ftoi(CLAMP(col.g*255,0,255)); + column[j*4+2]+=Math::fast_ftoi(CLAMP(col.b*255,0,255)); + column[j*4+3]+=1; + + } + } + + + for(int j=0;j<h*4;j+=4) { + + int a = column[j+3]; + if (a>0) { + column[j+0]/=a; + column[j+1]/=a; + column[j+2]/=a; + + } + + uint8_t r = uint8_t(column[j+0]); + uint8_t g = uint8_t(column[j+1]); + uint8_t b = uint8_t(column[j+2]); + + int widx = ((j>>2)*w+i)*4; + wr[widx+0]=r; + wr[widx+1]=g; + wr[widx+2]=b; + wr[widx+3]=255; + } + } + + time = OS::get_singleton()->get_ticks_usec() - time; + //print_line("Taken: "+rtos(USEC_TO_SEC(time))); + + } + + + wr = DVector<uint8_t>::Write(); + + Image img(w,h,0,Image::FORMAT_RGBA,graph_image); + + if (reset_texture) { + + if (graph_texture.is_null()) { + graph_texture.instance(); + } + graph_texture->create(img.get_width(),img.get_height(),img.get_format(),Texture::FLAG_VIDEO_SURFACE); + + } + + graph_texture->set_data(img);; + + + graph->set_texture(graph_texture); + graph->update(); + +} + +void EditorProfiler::_update_frame() { + + int cursor_metric = _get_cursor_index(); + + + ERR_FAIL_INDEX(cursor_metric,frame_metrics.size()); + + updating_frame=true; + variables->clear(); + + TreeItem* root = variables->create_item(); + Metric &m = frame_metrics[cursor_metric]; + + + int dtime = display_time->get_selected(); + + + for(int i=0;i<m.categories.size();i++) { + + TreeItem *category = variables->create_item(root); + category->set_cell_mode(0,TreeItem::CELL_MODE_CHECK); + category->set_editable(0,true); + category->set_metadata(0,m.categories[i].signature); + category->set_text(0,String(m.categories[i].name)); + category->set_text(1,_get_time_as_text(m,m.categories[i].total_time,1)); + + if (plot_sigs.has(m.categories[i].signature)) { + category->set_checked(0,true); + category->set_custom_bg_color(0,Color(0,0,0)); + category->set_custom_color(0,_get_color_from_signature(m.categories[i].signature)); + } + + + for(int j=0;j<m.categories[i].items.size();j++) { + Metric::Category::Item &it = m.categories[i].items[j]; + + TreeItem *item = variables->create_item(category); + item->set_cell_mode(0,TreeItem::CELL_MODE_CHECK); + item->set_editable(0,true); + item->set_text(0,it.name); + item->set_metadata(0,it.signature); + item->set_metadata(1,it.script); + item->set_metadata(2,it.line); + item->set_tooltip(0,it.script+":"+itos(it.line)); + + float time = dtime == DISPLAY_SELF_TIME ? it.self : it.total; + + item->set_text(1,_get_time_as_text(m,time,it.calls)); + + item->set_text(2,itos(it.calls)); + + if (plot_sigs.has(it.signature)) { + item->set_checked(0,true); + item->set_custom_bg_color(0,Color(0,0,0)); + item->set_custom_color(0,_get_color_from_signature(it.signature)); + } + + } + } + + updating_frame=false; + +} + + +void EditorProfiler::_activate_pressed() { + + if (activate->is_pressed()) { + clear(); + activate->set_icon(get_icon("Stop","EditorIcons")); + activate->set_text(TTR("Stop Profilinng")); + } else { + activate->set_icon(get_icon("Play","EditorIcons")); + activate->set_text(TTR("Start Profilinng")); + + + } + emit_signal("enable_profiling",activate->is_pressed()); + +} + + +void EditorProfiler::_notification(int p_what) { + + if (p_what==NOTIFICATION_ENTER_TREE) { + activate->set_icon(get_icon("Play","EditorIcons")); + } +} + +void EditorProfiler::_graph_tex_draw() { + + if (last_metric<0) + return; + if (seeking) { + + int max_frames = frame_metrics.size(); + int frame = cursor_metric_edit->get_val() - (frame_metrics[last_metric].frame_number-max_frames+1); + if (frame<0) + frame=0; + + int cur_x = frame * graph->get_size().x / max_frames; + + + graph->draw_line(Vector2(cur_x,0),Vector2(cur_x,graph->get_size().y),Color(1,1,1,0.8)); + } + + + if (hover_metric!=-1 && frame_metrics[hover_metric].valid) { + + + + int max_frames = frame_metrics.size(); + int frame = frame_metrics[hover_metric].frame_number - (frame_metrics[last_metric].frame_number-max_frames+1); + if (frame<0) + frame=0; + + int cur_x = frame * graph->get_size().x / max_frames; + + graph->draw_line(Vector2(cur_x,0),Vector2(cur_x,graph->get_size().y),Color(1,1,1,0.4)); + + + } + +} + +void EditorProfiler::_graph_tex_mouse_exit() { + + hover_metric=-1; + graph->update(); +} + + +void EditorProfiler::_cursor_metric_changed(double) { + if (updating_frame) + return; + + + graph->update(); + _update_frame(); + +} + +void EditorProfiler::_graph_tex_input(const InputEvent& p_ev){ + + if (last_metric<0) + return; + + if ( + (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && p_ev.mouse_button.pressed) || + (p_ev.type==InputEvent::MOUSE_MOTION) ) { + + int x = p_ev.mouse_button.x; + x=x*frame_metrics.size()/graph->get_size().width; + + bool show_hover = x>=0 && x<frame_metrics.size(); + + if (x<0) { + x=0; + } + + if (x>=frame_metrics.size()) { + x=frame_metrics.size()-1; + } + + + + int metric=frame_metrics.size()-x-1; + metric = last_metric-metric; + while(metric<0) { + metric+=frame_metrics.size(); + } + + + + if (show_hover) { + + hover_metric=metric; + + } else { + hover_metric=-1; + } + + + if (p_ev.type==InputEvent::MOUSE_BUTTON || p_ev.mouse_motion.button_mask&BUTTON_MASK_LEFT) { + //cursor_metric=x; + updating_frame=true; + + //metric may be invalid, so look for closest metric that is valid, this makes snap feel better + bool valid=false; + for(int i=0;i<frame_metrics.size();i++) { + + if (frame_metrics[metric].valid) { + valid=true; + break; + } + + metric++; + if (metric>=frame_metrics.size()) + metric=0; + } + + if (valid) + cursor_metric_edit->set_val(frame_metrics[metric].frame_number); + + updating_frame=false; + + if (activate->is_pressed()) { + if (!seeking) { + emit_signal("break_request"); + } + } + + seeking=true; + + if (!frame_delay->is_processing()) { + frame_delay->set_wait_time(0.1); + frame_delay->start(); + } + } + + graph->update(); + + } + +} + +int EditorProfiler::_get_cursor_index() const { + + if (last_metric<0) + return 0; + if (!frame_metrics[last_metric].valid) + return 0; + + int diff = (frame_metrics[last_metric].frame_number-cursor_metric_edit->get_val()); + + int idx = last_metric - diff; + while (idx<0) { + idx+=frame_metrics.size(); + } + + + return idx; + +} + +void EditorProfiler::disable_seeking() { + + seeking=false; + graph->update(); + +} + +void EditorProfiler::_combo_changed(int) { + + _update_frame(); + _update_plot(); +} + +void EditorProfiler::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("_update_frame"),&EditorProfiler::_update_frame); + ObjectTypeDB::bind_method(_MD("_update_plot"),&EditorProfiler::_update_plot); + ObjectTypeDB::bind_method(_MD("_activate_pressed"),&EditorProfiler::_activate_pressed); + ObjectTypeDB::bind_method(_MD("_graph_tex_draw"),&EditorProfiler::_graph_tex_draw); + ObjectTypeDB::bind_method(_MD("_graph_tex_input"),&EditorProfiler::_graph_tex_input); + ObjectTypeDB::bind_method(_MD("_graph_tex_mouse_exit"),&EditorProfiler::_graph_tex_mouse_exit); + ObjectTypeDB::bind_method(_MD("_cursor_metric_changed"),&EditorProfiler::_cursor_metric_changed); + ObjectTypeDB::bind_method(_MD("_combo_changed"),&EditorProfiler::_combo_changed); + + ObjectTypeDB::bind_method(_MD("_item_edited"),&EditorProfiler::_item_edited); + ADD_SIGNAL( MethodInfo("enable_profiling",PropertyInfo(Variant::BOOL,"enable"))); + ADD_SIGNAL( MethodInfo("break_request")); + +} + +void EditorProfiler::set_enabled(bool p_enable) { + + activate->set_disabled(!p_enable); +} + +bool EditorProfiler::is_profiling() { + return activate->is_pressed(); +} + +EditorProfiler::EditorProfiler() +{ + + HBoxContainer *hb = memnew( HBoxContainer ); + add_child(hb); + activate = memnew( Button ); + activate->set_toggle_mode(true); + activate->set_text(TTR("Start Profiling")); + activate->connect("pressed",this,"_activate_pressed"); + hb->add_child(activate); + + hb->add_child( memnew( Label(TTR("Measure:") ) ) ); + + display_mode = memnew( OptionButton ); + display_mode->add_item(TTR("Frame Time (sec)")); + display_mode->add_item(TTR("Average Time (sec)")); + display_mode->add_item(TTR("Frame %")); + display_mode->add_item(TTR("Fixed Frame %")); + display_mode->connect("item_selected",this,"_combo_changed"); + + hb->add_child( display_mode ); + + hb->add_child( memnew( Label(TTR("Time:") ) ) ); + + display_time = memnew( OptionButton ); + display_time->add_item(TTR("Inclusive")); + display_time->add_item(TTR("Self")); + display_time->connect("item_selected",this,"_combo_changed"); + + hb->add_child(display_time); + + hb->add_spacer(); + + hb->add_child( memnew( Label(TTR("Frame#:") ) ) ); + + cursor_metric_edit = memnew( SpinBox ); + cursor_metric_edit->set_h_size_flags(SIZE_FILL); + hb->add_child(cursor_metric_edit); + cursor_metric_edit->connect("value_changed",this,"_cursor_metric_changed"); + + hb->add_constant_override("separation",8); + + + + h_split = memnew( HSplitContainer ); + add_child(h_split); + h_split->set_v_size_flags(SIZE_EXPAND_FILL); + + variables = memnew( Tree ); + variables->set_custom_minimum_size(Size2(300,0)); + variables->set_hide_folding(true); + h_split->add_child(variables); + variables->set_hide_root(true); + variables->set_columns(3); + variables->set_column_titles_visible(true); + variables->set_column_title(0,"Name"); + variables->set_column_expand(0,true); + variables->set_column_min_width(0,60); + variables->set_column_title(1,"Time"); + variables->set_column_expand(1,false); + variables->set_column_min_width(1,60); + variables->set_column_title(2,"Calls"); + variables->set_column_expand(2,false); + variables->set_column_min_width(2,60); + variables->connect("item_edited",this,"_item_edited"); + + + graph = memnew( TextureFrame ); + graph->set_expand(true); + graph->set_stop_mouse(true); + graph->set_ignore_mouse(false); + graph->connect("draw",this,"_graph_tex_draw"); + graph->connect("input_event",this,"_graph_tex_input"); + graph->connect("mouse_exit",this,"_graph_tex_mouse_exit"); + + h_split->add_child(graph); + graph->set_h_size_flags(SIZE_EXPAND_FILL); + + add_constant_override("separation",3); + + int metric_size=CLAMP(int(EDITOR_DEF("debugger/profiler_frame_history_size",600)),60,1024); + frame_metrics.resize(metric_size); + last_metric=-1; +// cursor_metric=-1; + hover_metric=-1; + + + EDITOR_DEF("debugger/profiler_frame_max_functions",64); + + //display_mode=DISPLAY_FRAME_TIME; + + frame_delay = memnew( Timer ); + frame_delay->set_wait_time(0.1); + frame_delay->set_one_shot(true); + add_child(frame_delay); + frame_delay->connect("timeout",this,"_update_frame"); + + plot_delay = memnew( Timer ); + plot_delay->set_wait_time(0.1); + plot_delay->set_one_shot(true); + add_child(plot_delay); + plot_delay->connect("timeout",this,"_update_plot"); + + plot_sigs.insert("fixed_frame_time"); + plot_sigs.insert("category_frame_time"); + + seeking=false; + graph_height=1; + +// activate->set_disabled(true); + +} diff --git a/tools/editor/editor_profiler.h b/tools/editor/editor_profiler.h new file mode 100644 index 0000000000..aca3220717 --- /dev/null +++ b/tools/editor/editor_profiler.h @@ -0,0 +1,143 @@ +#ifndef EDITORPROFILER_H +#define EDITORPROFILER_H + + +#include "scene/gui/box_container.h" +#include "scene/gui/texture_frame.h" +#include "scene/gui/button.h" +#include "scene/gui/label.h" +#include "scene/gui/tree.h" +#include "scene/gui/split_container.h" +#include "scene/gui/option_button.h" +#include "scene/gui/spin_box.h" + + +class EditorProfiler : public VBoxContainer { + + OBJ_TYPE(EditorProfiler,VBoxContainer) + +public: + + struct Metric { + + bool valid; + + int frame_number; + float frame_time; + float idle_time; + float fixed_time; + float fixed_frame_time; + + struct Category { + + StringName signature; + String name; + float total_time; //total for category + + struct Item { + + StringName signature; + String name; + String script; + int line; + float self; + float total; + int calls; + }; + + Vector<Item> items; + }; + + Vector<Category> categories; + + Map<StringName,Category*> category_ptrs; + Map<StringName,Category::Item*> item_ptrs; + + + Metric() { valid=false; frame_number=0; } + }; + + enum DisplayMode { + DISPLAY_FRAME_TIME, + DISPLAY_AVERAGE_TIME, + DISPLAY_FRAME_PERCENT, + DISPLAY_FIXED_FRAME_PERCENT, + }; + + enum DisplayTime { + DISPLAY_TOTAL_TIME, + DISPLAY_SELF_TIME, + }; + +private: + Button *activate; + TextureFrame *graph; + Ref<ImageTexture> graph_texture; + DVector<uint8_t> graph_image; + Tree *variables; + HSplitContainer *h_split; + + Set<StringName> plot_sigs; + + OptionButton *display_mode; + OptionButton *display_time; + + SpinBox * cursor_metric_edit; + + Vector<Metric> frame_metrics; + int last_metric; + + int max_functions; + + bool updating_frame; + + //int cursor_metric; + int hover_metric; + + bool seeking; + + Timer *frame_delay; + Timer *plot_delay; + + void _update_frame(); + + void _activate_pressed(); + + String _get_time_as_text(Metric &m,float p_time,int p_calls); + + void _make_metric_ptrs(Metric& m); + void _item_edited(); + + void _update_plot(); + + void _graph_tex_mouse_exit(); + + void _graph_tex_draw(); + void _graph_tex_input(const InputEvent& p_ev); + + int _get_cursor_index() const; + + Color _get_color_from_signature(const StringName& p_signature) const; + + void _cursor_metric_changed(double); + + void _combo_changed(int); + +protected: + + void _notification(int p_what); + static void _bind_methods(); +public: + + void add_frame_metric(const Metric& p_metric, bool p_final=false); + void set_enabled(bool p_enable); + bool is_profiling(); + bool is_seeking() { return seeking; } + void disable_seeking(); + + void clear(); + + EditorProfiler(); +}; + +#endif // EDITORPROFILER_H diff --git a/tools/editor/script_editor_debugger.cpp b/tools/editor/script_editor_debugger.cpp index 325eadc1e5..b0cd2e925d 100644 --- a/tools/editor/script_editor_debugger.cpp +++ b/tools/editor/script_editor_debugger.cpp @@ -41,6 +41,8 @@ #include "globals.h" #include "editor_node.h" #include "main/performance.h" +#include "editor_profiler.h" +#include "editor_settings.h" class ScriptEditorDebuggerVariables : public Object { @@ -208,7 +210,13 @@ void ScriptEditorDebugger::_parse_message(const String& p_msg,const Array& p_dat docontinue->set_disabled(false); emit_signal("breaked",true,can_continue); OS::get_singleton()->move_window_to_foreground(); - tabs->set_current_tab(0); + if (!profiler->is_seeking()) + tabs->set_current_tab(0); + + profiler->set_enabled(false); + + EditorNode::get_singleton()->get_pause_button()->set_pressed(true); + EditorNode::get_singleton()->make_bottom_panel_item_visible(this); @@ -225,6 +233,11 @@ void ScriptEditorDebugger::_parse_message(const String& p_msg,const Array& p_dat docontinue->set_disabled(true); emit_signal("breaked",false,false); //tabs->set_current_tab(0); + profiler->set_enabled(true); + profiler->disable_seeking(); + + EditorNode::get_singleton()->get_pause_button()->set_pressed(false); + } else if (p_msg=="message:click_ctrl") { @@ -441,6 +454,137 @@ void ScriptEditorDebugger::_parse_message(const String& p_msg,const Array& p_dat packet_peer_stream->put_var(oe.warning); packet_peer_stream->put_var(oe.callstack); */ + + } else if (p_msg=="profile_sig") { + //cache a signature + print_line("SIG: "+String(Variant(p_data))); + profiler_signature[p_data[1]]=p_data[0]; + + } else if (p_msg=="profile_frame" || p_msg=="profile_total") { + + EditorProfiler::Metric metric; + metric.valid=true; + metric.frame_number=p_data[0]; + metric.frame_time=p_data[1]; + metric.idle_time=p_data[2]; + metric.fixed_time=p_data[3]; + metric.fixed_frame_time=p_data[4]; + int frame_data_amount = p_data[6]; + int frame_function_amount = p_data[7]; + + + if (frame_data_amount) { + EditorProfiler::Metric::Category frame_time; + frame_time.signature="category_frame_time"; + frame_time.name="Frame Time"; + frame_time.total_time=metric.frame_time; + + EditorProfiler::Metric::Category::Item item; + item.calls=1; + item.line=0; + item.name="Fixed Time"; + item.total=metric.fixed_time; + item.self=item.total; + item.signature="fixed_time"; + + + frame_time.items.push_back(item); + + item.name="Idle Time"; + item.total=metric.idle_time; + item.self=item.total; + item.signature="idle_time"; + + frame_time.items.push_back(item); + + item.name="Fixed Frame Time"; + item.total=metric.fixed_frame_time; + item.self=item.total; + item.signature="fixed_frame_time"; + + frame_time.items.push_back(item); + + metric.categories.push_back(frame_time); + + } + + + + int idx=8; + for(int i=0;i<frame_data_amount;i++) { + + EditorProfiler::Metric::Category c; + String name=p_data[idx++]; + Array values=p_data[idx++]; + c.name=name.capitalize(); + c.items.resize(values.size()/2); + c.total_time=0; + c.signature="categ::"+name; + for(int i=0;i<values.size();i+=2) { + + EditorProfiler::Metric::Category::Item item; + item.name=values[i]; + item.calls=1; + item.self=values[i+1]; + item.total=item.self; + item.signature="categ::"+name+"::"+item.name; + item.name=item.name.capitalize(); + c.total_time+=item.total; + c.items[i/2]=item; + + + } + metric.categories.push_back(c); + } + + EditorProfiler::Metric::Category funcs; + funcs.total_time=p_data[5]; //script time + funcs.items.resize(frame_function_amount); + funcs.name="Script Functions"; + funcs.signature="script_functions"; + for(int i=0;i<frame_function_amount;i++) { + + int signature = p_data[idx++]; + int calls = p_data[idx++]; + float total = p_data[idx++]; + float self = p_data[idx++]; + + + + EditorProfiler::Metric::Category::Item item; + if (profiler_signature.has(signature)) { + + item.signature=profiler_signature[signature]; + + String name = profiler_signature[signature]; + Vector<String> strings = name.split("::"); + if (strings.size()==3) { + item.name=strings[2]; + item.script=strings[0]; + item.line=strings[1].to_int(); + } + + } else { + item.name="SigErr "+itos(signature); + } + + + + + item.calls=calls; + item.self=self; + item.total=total; + funcs.items[i]=item; + + } + + metric.categories.push_back(funcs); + + if (p_msg=="profile_frame") + profiler->add_frame_metric(metric,false); + else + profiler->add_frame_metric(metric,true); + } else if (p_msg=="kill_me") { editor->call_deferred("stop_child_process"); @@ -586,15 +730,25 @@ void ScriptEditorDebugger::_notification(int p_what) { reason->set_text(TTR("Child Process Connected")); reason->set_tooltip(TTR("Child Process Connected")); + profiler->clear(); + scene_tree->clear(); le_set->set_disabled(true); le_clear->set_disabled(false); error_list->clear(); error_stack->clear(); error_count=0; + profiler_signature.clear(); //live_edit_root->set_text("/root"); + EditorNode::get_singleton()->get_pause_button()->set_pressed(false); + EditorNode::get_singleton()->get_pause_button()->set_disabled(false); + update_live_edit_root(); + if (profiler->is_profiling()) { + _profiler_activate(true); + } + } else { @@ -656,6 +810,7 @@ void ScriptEditorDebugger::_notification(int p_what) { } message_type=cmd; + //print_line("GOT: "+message_type); ret = ppeer->get_var(cmd); if (ret!=OK) { @@ -744,8 +899,14 @@ void ScriptEditorDebugger::stop(){ node_path_cache.clear(); res_path_cache.clear(); + profiler_signature.clear(); le_clear->set_disabled(false); le_set->set_disabled(true); + profiler->set_enabled(true); + + EditorNode::get_singleton()->get_pause_button()->set_pressed(false); + EditorNode::get_singleton()->get_pause_button()->set_disabled(true); + if (hide_on_stop) { @@ -756,6 +917,44 @@ void ScriptEditorDebugger::stop(){ } +void ScriptEditorDebugger::_profiler_activate(bool p_enable) { + + if (!connection.is_valid()) + return; + + + if (p_enable) { + profiler_signature.clear(); + Array msg; + msg.push_back("start_profiling"); + int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions"); + max_funcs = CLAMP(max_funcs,16,512); + msg.push_back(max_funcs); + ppeer->put_var(msg); + + print_line("BEGIN PROFILING!"); + + } else { + Array msg; + msg.push_back("stop_profiling"); + ppeer->put_var(msg); + + print_line("END PROFILING!"); + + } + +} + +void ScriptEditorDebugger::_profiler_seeked() { + + if (!connection.is_valid() || !connection->is_connected()) + return; + + if (breaked) + return; + debug_break();; +} + void ScriptEditorDebugger::_stack_dump_frame_selected() { @@ -1172,6 +1371,21 @@ void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) { hide_on_stop=p_hide; } +void ScriptEditorDebugger::_paused() { + + ERR_FAIL_COND(connection.is_null()); + ERR_FAIL_COND(!connection->is_connected()); + + if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) { + debug_break(); + } + + if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) { + debug_continue(); + } + +} + void ScriptEditorDebugger::_bind_methods() { ObjectTypeDB::bind_method(_MD("_stack_dump_frame_selected"),&ScriptEditorDebugger::_stack_dump_frame_selected); @@ -1189,6 +1403,11 @@ void ScriptEditorDebugger::_bind_methods() { ObjectTypeDB::bind_method(_MD("_error_selected"),&ScriptEditorDebugger::_error_selected); ObjectTypeDB::bind_method(_MD("_error_stack_selected"),&ScriptEditorDebugger::_error_stack_selected); + ObjectTypeDB::bind_method(_MD("_profiler_activate"),&ScriptEditorDebugger::_profiler_activate); + ObjectTypeDB::bind_method(_MD("_profiler_seeked"),&ScriptEditorDebugger::_profiler_seeked); + + ObjectTypeDB::bind_method(_MD("_paused"),&ScriptEditorDebugger::_paused); + ObjectTypeDB::bind_method(_MD("live_debug_create_node"),&ScriptEditorDebugger::live_debug_create_node); ObjectTypeDB::bind_method(_MD("live_debug_instance_node"),&ScriptEditorDebugger::live_debug_instance_node); @@ -1320,6 +1539,12 @@ ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor){ error_split->set_name(TTR("Errors")); tabs->add_child(error_split); + profiler = memnew( EditorProfiler ); + profiler->set_name("Profiler"); + tabs->add_child(profiler); + profiler->connect("enable_profiling",this,"_profiler_activate"); + profiler->connect("break_request",this,"_profiler_seeked"); + HSplitContainer *hsp = memnew( HSplitContainer ); @@ -1334,7 +1559,7 @@ ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor){ perf_draw = memnew( Control ); perf_draw->connect("draw",this,"_performance_draw"); hsp->add_child(perf_draw); - hsp->set_name("Performance"); + hsp->set_name("Metrics"); hsp->set_split_offset(300); tabs->add_child(hsp); perf_max.resize(Performance::MONITOR_MAX); @@ -1468,6 +1693,8 @@ ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor){ hide_on_stop=true; last_error_count=0; + EditorNode::get_singleton()->get_pause_button()->connect("pressed",this,"_paused"); + } diff --git a/tools/editor/script_editor_debugger.h b/tools/editor/script_editor_debugger.h index ecd0494955..1288349884 100644 --- a/tools/editor/script_editor_debugger.h +++ b/tools/editor/script_editor_debugger.h @@ -34,6 +34,7 @@ #include "core/io/tcp_server.h" #include "core/io/packet_peer.h" + class Tree; class PropertyEditor; class EditorNode; @@ -46,6 +47,7 @@ class AcceptDialog; class TreeItem; class HSplitContainer; class ItemList; +class EditorProfiler; class ScriptEditorDebugger : public Control { @@ -93,6 +95,8 @@ class ScriptEditorDebugger : public Control { Vector<float> perf_max; Vector<TreeItem*> perf_items; + Map<int,String> profiler_signature; + Tree *perf_monitors; Control *perf_draw; @@ -115,6 +119,7 @@ class ScriptEditorDebugger : public Control { int last_path_id; Map<String,int> res_path_cache; + EditorProfiler *profiler; EditorNode *editor; @@ -148,6 +153,13 @@ class ScriptEditorDebugger : public Control { void _error_selected(int p_idx); void _error_stack_selected(int p_idx); + void _profiler_activate(bool p_enable); + void _profiler_seeked(); + + + + void _paused(); + protected: void _notification(int p_what); |