2020-01-18 05:38:21 -03:00
/*
2021-01-11 05:52:18 -03:00
* Copyright ( c ) 2018 - 2021 , Andreas Kling < kling @ serenityos . org >
2020-01-18 05:38:21 -03:00
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 05:38:21 -03:00
*/
2021-02-27 14:33:30 -03:00
# include "IndividualSampleModel.h"
2019-12-12 18:01:06 -03:00
# include "Profile.h"
2021-05-06 14:50:18 -03:00
# include "TimelineContainer.h"
# include "TimelineHeader.h"
2021-05-06 14:00:48 -03:00
# include "TimelineTrack.h"
2021-05-06 13:59:51 -03:00
# include "TimelineView.h"
2020-07-01 16:07:53 -03:00
# include <LibCore/ArgsParser.h>
2020-07-01 15:49:51 -03:00
# include <LibCore/ElapsedTimer.h>
# include <LibCore/EventLoop.h>
2020-12-26 08:58:50 -03:00
# include <LibCore/ProcessStatisticsReader.h>
2020-07-01 15:49:51 -03:00
# include <LibCore/Timer.h>
2021-02-24 12:40:36 -03:00
# include <LibDesktop/Launcher.h>
2020-02-06 16:33:02 -03:00
# include <LibGUI/Action.h>
# include <LibGUI/Application.h>
# include <LibGUI/BoxLayout.h>
2020-07-01 15:49:51 -03:00
# include <LibGUI/Button.h>
# include <LibGUI/Label.h>
2020-02-06 16:33:02 -03:00
# include <LibGUI/Menu.h>
2021-04-13 11:18:20 -03:00
# include <LibGUI/Menubar.h>
2020-07-01 15:49:51 -03:00
# include <LibGUI/MessageBox.h>
2020-02-16 05:17:49 -03:00
# include <LibGUI/Model.h>
2020-07-28 07:09:19 -03:00
# include <LibGUI/ProcessChooser.h>
2020-04-12 05:57:44 -03:00
# include <LibGUI/Splitter.h>
2021-05-06 14:12:58 -03:00
# include <LibGUI/Statusbar.h>
2021-02-27 14:33:30 -03:00
# include <LibGUI/TabWidget.h>
2020-04-11 13:46:11 -03:00
# include <LibGUI/TableView.h>
2020-02-06 16:33:02 -03:00
# include <LibGUI/TreeView.h>
# include <LibGUI/Window.h>
2020-07-01 15:49:51 -03:00
# include <serenity.h>
# include <string.h>
2021-05-04 12:45:02 -03:00
using namespace Profiler ;
2021-01-11 05:52:18 -03:00
static bool generate_profile ( pid_t & pid ) ;
2019-12-12 18:01:06 -03:00
int main ( int argc , char * * argv )
{
2020-07-01 16:07:53 -03:00
int pid = 0 ;
2021-03-04 19:07:38 -03:00
const char * perfcore_file_arg = nullptr ;
Core : : ArgsParser args_parser ;
2020-07-01 16:07:53 -03:00
args_parser . add_option ( pid , " PID to profile " , " pid " , ' p ' , " PID " ) ;
2021-03-04 19:07:38 -03:00
args_parser . add_positional_argument ( perfcore_file_arg , " Path of perfcore file " , " perfcore-file " , Core : : ArgsParser : : Required : : No ) ;
args_parser . parse ( argc , argv ) ;
if ( pid & & perfcore_file_arg ) {
warnln ( " -p/--pid option and perfcore-file argument must not be used together! " ) ;
return 1 ;
}
2020-07-01 16:07:53 -03:00
2020-07-04 09:05:19 -03:00
auto app = GUI : : Application : : construct ( argc , argv ) ;
2020-07-28 07:09:19 -03:00
auto app_icon = GUI : : Icon : : default_icon ( " app-profiler " ) ;
2020-07-01 15:49:51 -03:00
2021-03-04 19:07:38 -03:00
String perfcore_file ;
if ( ! perfcore_file_arg ) {
2020-07-01 16:07:53 -03:00
if ( ! generate_profile ( pid ) )
2020-07-01 15:49:51 -03:00
return 0 ;
2021-03-04 19:07:38 -03:00
perfcore_file = String : : formatted ( " /proc/{}/perf_events " , pid ) ;
2020-07-01 15:49:51 -03:00
} else {
2021-03-04 19:07:38 -03:00
perfcore_file = perfcore_file_arg ;
2019-12-12 18:01:06 -03:00
}
2021-03-04 19:07:38 -03:00
auto profile_or_error = Profile : : load_from_perfcore_file ( perfcore_file ) ;
2020-12-26 06:27:57 -03:00
if ( profile_or_error . is_error ( ) ) {
GUI : : MessageBox : : show ( nullptr , profile_or_error . error ( ) , " Profiler " , GUI : : MessageBox : : Type : : Error ) ;
return 0 ;
2019-12-12 18:01:06 -03:00
}
2020-12-26 06:27:57 -03:00
auto & profile = profile_or_error . value ( ) ;
2020-02-02 11:07:41 -03:00
auto window = GUI : : Window : : construct ( ) ;
2021-02-24 12:40:36 -03:00
if ( ! Desktop : : Launcher : : add_allowed_handler_with_only_specific_urls (
" /bin/Help " ,
{ URL : : create_with_file_protocol ( " /usr/share/man/man1/Profiler.md " ) } )
| | ! Desktop : : Launcher : : seal_allowlist ( ) ) {
warnln ( " Failed to set up allowed launch URLs " ) ;
return 1 ;
}
2020-07-01 14:43:17 -03:00
window - > set_title ( " Profiler " ) ;
2020-07-28 07:09:19 -03:00
window - > set_icon ( app_icon . bitmap_for_size ( 16 ) ) ;
Misc: Use automatic window positioning in more applications
This is a follow up to #2936 / d3e3b4ae56aa79d9bde12ca1f143dcf116f89a4c.
Affected programs:
- Applications: Browser (Download, View source, Inspect DOM tree, JS
console), Terminal (Settings)
- Demos: Cube, Eyes, Fire, HelloWorld, LibGfxDemo, WebView,
WidgetGallery
- DevTools: HackStudio, Inspector, Profiler
- Games: 2048, Minesweeper, Snake, Solitaire
- Userland: test-web
A few have been left out where manual positioning is done on purpose,
e.g. ClipboardManager (to be close to the menu bar) or VisualBuilder (to
preserve alignment of the multiple application windows).
2020-08-15 12:24:05 -03:00
window - > resize ( 800 , 600 ) ;
2019-12-12 18:01:06 -03:00
2020-03-04 05:46:23 -03:00
auto & main_widget = window - > set_main_widget < GUI : : Widget > ( ) ;
main_widget . set_fill_with_background_color ( true ) ;
main_widget . set_layout < GUI : : VerticalBoxLayout > ( ) ;
2019-12-14 14:44:29 -03:00
2021-05-06 14:50:18 -03:00
auto timeline_header_container = GUI : : Widget : : construct ( ) ;
timeline_header_container - > set_layout < GUI : : VerticalBoxLayout > ( ) ;
timeline_header_container - > set_fill_with_background_color ( true ) ;
timeline_header_container - > set_shrink_to_fit ( true ) ;
2021-05-06 13:59:51 -03:00
auto timeline_view = TimelineView : : construct ( ) ;
2021-05-06 13:44:31 -03:00
for ( auto & process : profile - > processes ( ) ) {
2021-05-07 06:11:45 -03:00
bool matching_event_found = false ;
2021-05-06 13:44:31 -03:00
for ( auto & event : profile - > events ( ) ) {
2021-05-07 06:11:45 -03:00
if ( event . pid = = process . pid & & process . valid_at ( event . timestamp ) ) {
matching_event_found = true ;
break ;
}
2021-05-06 13:44:31 -03:00
}
2021-05-07 06:11:45 -03:00
if ( ! matching_event_found )
2021-05-06 13:44:31 -03:00
continue ;
2021-05-07 08:47:12 -03:00
auto & timeline_header = timeline_header_container - > add < TimelineHeader > ( * profile , process ) ;
timeline_header . set_shrink_to_fit ( true ) ;
timeline_header . on_selection_change = [ & ] ( bool selected ) {
if ( selected ) {
auto end_valid = process . end_valid = = 0 ? profile - > last_timestamp ( ) : process . end_valid ;
profile - > set_process_filter ( process . pid , process . start_valid , end_valid ) ;
} else
profile - > clear_process_filter ( ) ;
timeline_header_container - > for_each_child_widget ( [ ] ( auto & other_timeline_header ) {
static_cast < TimelineHeader & > ( other_timeline_header ) . update_selection ( ) ;
return IterationDecision : : Continue ;
} ) ;
} ;
2021-05-06 14:00:48 -03:00
timeline_view - > add < TimelineTrack > ( * timeline_view , * profile , process ) ;
2021-05-06 13:44:31 -03:00
}
2021-05-06 14:50:18 -03:00
[[maybe_unused]] auto & timeline_container = main_widget . add < TimelineContainer > ( * timeline_header_container , * timeline_view ) ;
2021-05-06 13:44:31 -03:00
2021-02-27 14:33:30 -03:00
auto & tab_widget = main_widget . add < GUI : : TabWidget > ( ) ;
auto & tree_tab = tab_widget . add_tab < GUI : : Widget > ( " Call Tree " ) ;
tree_tab . set_layout < GUI : : VerticalBoxLayout > ( ) ;
tree_tab . layout ( ) - > set_margins ( { 4 , 4 , 4 , 4 } ) ;
auto & bottom_splitter = tree_tab . add < GUI : : VerticalSplitter > ( ) ;
2020-04-11 13:46:11 -03:00
2020-04-12 05:57:44 -03:00
auto & tree_view = bottom_splitter . add < GUI : : TreeView > ( ) ;
2020-10-27 16:33:30 -03:00
tree_view . set_should_fill_selected_rows ( true ) ;
2020-08-25 06:25:39 -03:00
tree_view . set_column_headers_visible ( true ) ;
2020-03-04 15:07:55 -03:00
tree_view . set_model ( profile - > model ( ) ) ;
2019-12-12 18:01:06 -03:00
2020-04-12 05:57:44 -03:00
auto & disassembly_view = bottom_splitter . add < GUI : : TableView > ( ) ;
2020-04-11 13:46:11 -03:00
tree_view . on_selection = [ & ] ( auto & index ) {
profile - > set_disassembly_index ( index ) ;
disassembly_view . set_model ( profile - > disassembly_model ( ) ) ;
} ;
2021-02-27 14:33:30 -03:00
auto & samples_tab = tab_widget . add_tab < GUI : : Widget > ( " Samples " ) ;
samples_tab . set_layout < GUI : : VerticalBoxLayout > ( ) ;
samples_tab . layout ( ) - > set_margins ( { 4 , 4 , 4 , 4 } ) ;
auto & samples_splitter = samples_tab . add < GUI : : HorizontalSplitter > ( ) ;
auto & samples_table_view = samples_splitter . add < GUI : : TableView > ( ) ;
samples_table_view . set_model ( profile - > samples_model ( ) ) ;
auto & individual_sample_view = samples_splitter . add < GUI : : TableView > ( ) ;
samples_table_view . on_selection = [ & ] ( const GUI : : ModelIndex & index ) {
auto model = IndividualSampleModel : : create ( * profile , index . data ( GUI : : ModelRole : : Custom ) . to_integer < size_t > ( ) ) ;
individual_sample_view . set_model ( move ( model ) ) ;
} ;
2021-05-06 14:12:58 -03:00
const u64 start_of_trace = profile - > first_timestamp ( ) ;
const u64 end_of_trace = start_of_trace + profile - > length_in_ms ( ) ;
const auto clamp_timestamp = [ start_of_trace , end_of_trace ] ( u64 timestamp ) - > u64 {
return min ( end_of_trace , max ( timestamp , start_of_trace ) ) ;
} ;
auto & statusbar = main_widget . add < GUI : : Statusbar > ( ) ;
timeline_view - > on_selection_change = [ & ] {
auto & view = * timeline_view ;
StringBuilder builder ;
u64 normalized_start_time = clamp_timestamp ( min ( view . select_start_time ( ) , view . select_end_time ( ) ) ) ;
u64 normalized_end_time = clamp_timestamp ( max ( view . select_start_time ( ) , view . select_end_time ( ) ) ) ;
u64 normalized_hover_time = clamp_timestamp ( view . hover_time ( ) ) ;
builder . appendff ( " Time: {} ms " , normalized_hover_time - start_of_trace ) ;
if ( normalized_start_time ! = normalized_end_time ) {
auto start = normalized_start_time - start_of_trace ;
auto end = normalized_end_time - start_of_trace ;
builder . appendff ( " , Selection: {} - {} ms " , start , end ) ;
}
statusbar . set_text ( builder . to_string ( ) ) ;
} ;
2021-04-13 11:18:20 -03:00
auto menubar = GUI : : Menubar : : construct ( ) ;
2021-05-01 05:45:39 -03:00
auto & file_menu = menubar - > add_menu ( " &File " ) ;
file_menu . add_action ( GUI : : CommonActions : : make_quit_action ( [ & ] ( auto & ) { app - > quit ( ) ; } ) ) ;
2019-12-15 18:55:11 -03:00
2021-04-10 10:55:34 -03:00
auto & view_menu = menubar - > add_menu ( " &View " ) ;
2020-10-19 10:04:54 -03:00
2021-04-10 10:55:34 -03:00
auto invert_action = GUI : : Action : : create_checkable ( " &Invert Tree " , { Mod_Ctrl , Key_I } , [ & ] ( auto & action ) {
2019-12-16 14:21:05 -03:00
profile - > set_inverted ( action . is_checked ( ) ) ;
} ) ;
2019-12-17 01:46:39 -03:00
invert_action - > set_checked ( false ) ;
2020-04-18 10:31:19 -03:00
view_menu . add_action ( invert_action ) ;
2019-12-16 14:21:05 -03:00
2021-04-10 10:55:34 -03:00
auto top_functions_action = GUI : : Action : : create_checkable ( " &Top Functions " , { Mod_Ctrl , Key_T } , [ & ] ( auto & action ) {
2020-10-19 10:04:54 -03:00
profile - > set_show_top_functions ( action . is_checked ( ) ) ;
} ) ;
top_functions_action - > set_checked ( false ) ;
view_menu . add_action ( top_functions_action ) ;
2021-04-10 10:55:34 -03:00
auto percent_action = GUI : : Action : : create_checkable ( " Show &Percentages " , { Mod_Ctrl , Key_P } , [ & ] ( auto & action ) {
2020-03-02 17:56:03 -03:00
profile - > set_show_percentages ( action . is_checked ( ) ) ;
2020-03-04 15:07:55 -03:00
tree_view . update ( ) ;
2020-04-12 10:22:17 -03:00
disassembly_view . update ( ) ;
2020-03-02 17:56:03 -03:00
} ) ;
percent_action - > set_checked ( false ) ;
2020-04-04 07:18:40 -03:00
view_menu . add_action ( percent_action ) ;
2019-12-16 14:21:05 -03:00
2021-04-10 10:55:34 -03:00
auto & help_menu = menubar - > add_menu ( " &Help " ) ;
2021-02-24 12:40:36 -03:00
help_menu . add_action ( GUI : : CommonActions : : make_help_action ( [ ] ( auto & ) {
Desktop : : Launcher : : open ( URL : : create_with_file_protocol ( " /usr/share/man/man1/Profiler.md " ) , " /bin/Help " ) ;
} ) ) ;
2021-01-04 19:51:49 -03:00
help_menu . add_action ( GUI : : CommonActions : : make_about_action ( " Profiler " , app_icon , window ) ) ;
2020-07-28 07:09:19 -03:00
2021-03-25 17:41:39 -03:00
window - > set_menubar ( move ( menubar ) ) ;
2019-12-12 18:01:06 -03:00
window - > show ( ) ;
2020-07-04 09:05:19 -03:00
return app - > exec ( ) ;
2019-12-12 18:01:06 -03:00
}
2020-07-01 15:49:51 -03:00
2020-12-26 08:58:50 -03:00
static bool prompt_to_stop_profiling ( pid_t pid , const String & process_name )
2020-07-01 15:49:51 -03:00
{
auto window = GUI : : Window : : construct ( ) ;
2020-12-26 08:58:50 -03:00
window - > set_title ( String : : formatted ( " Profiling {}({}) " , process_name , pid ) ) ;
window - > resize ( 240 , 100 ) ;
Misc: Use automatic window positioning in more applications
This is a follow up to #2936 / d3e3b4ae56aa79d9bde12ca1f143dcf116f89a4c.
Affected programs:
- Applications: Browser (Download, View source, Inspect DOM tree, JS
console), Terminal (Settings)
- Demos: Cube, Eyes, Fire, HelloWorld, LibGfxDemo, WebView,
WidgetGallery
- DevTools: HackStudio, Inspector, Profiler
- Games: 2048, Minesweeper, Snake, Solitaire
- Userland: test-web
A few have been left out where manual positioning is done on purpose,
e.g. ClipboardManager (to be close to the menu bar) or VisualBuilder (to
preserve alignment of the multiple application windows).
2020-08-15 12:24:05 -03:00
window - > set_icon ( Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-profiler.png " ) ) ;
2020-08-15 11:32:11 -03:00
window - > center_on_screen ( ) ;
2020-12-26 08:58:50 -03:00
2020-07-01 15:49:51 -03:00
auto & widget = window - > set_main_widget < GUI : : Widget > ( ) ;
widget . set_fill_with_background_color ( true ) ;
2020-12-26 08:58:50 -03:00
auto & layout = widget . set_layout < GUI : : VerticalBoxLayout > ( ) ;
layout . set_margins ( GUI : : Margins ( 0 , 0 , 0 , 16 ) ) ;
2020-07-01 15:49:51 -03:00
auto & timer_label = widget . add < GUI : : Label > ( " ... " ) ;
Core : : ElapsedTimer clock ;
clock . start ( ) ;
auto update_timer = Core : : Timer : : construct ( 100 , [ & ] {
2021-04-15 04:36:14 -03:00
timer_label . set_text ( String : : formatted ( " {:.1} seconds " , clock . elapsed ( ) / 1000.0f ) ) ;
2020-07-01 15:49:51 -03:00
} ) ;
auto & stop_button = widget . add < GUI : : Button > ( " Stop " ) ;
2020-12-29 21:23:32 -03:00
stop_button . set_fixed_size ( 140 , 22 ) ;
2020-07-01 15:49:51 -03:00
stop_button . on_click = [ & ] ( auto ) {
2020-07-04 11:52:01 -03:00
GUI : : Application : : the ( ) - > quit ( ) ;
2020-07-01 15:49:51 -03:00
} ;
window - > show ( ) ;
2020-07-04 11:52:01 -03:00
return GUI : : Application : : the ( ) - > exec ( ) = = 0 ;
2020-07-01 15:49:51 -03:00
}
2021-01-11 05:52:18 -03:00
bool generate_profile ( pid_t & pid )
2020-07-01 15:49:51 -03:00
{
2020-07-01 16:07:53 -03:00
if ( ! pid ) {
2020-07-28 07:09:19 -03:00
auto process_chooser = GUI : : ProcessChooser : : construct ( " Profiler " , " Profile " , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-profiler.png " ) ) ;
2020-07-02 07:12:16 -03:00
if ( process_chooser - > exec ( ) = = GUI : : Dialog : : ExecCancel )
2020-07-01 16:07:53 -03:00
return false ;
2020-07-02 07:12:16 -03:00
pid = process_chooser - > pid ( ) ;
2020-07-01 16:07:53 -03:00
}
2020-12-26 08:58:50 -03:00
String process_name ;
auto all_processes = Core : : ProcessStatisticsReader : : get_all ( ) ;
2021-01-02 00:58:47 -03:00
if ( all_processes . has_value ( ) ) {
if ( auto it = all_processes . value ( ) . find ( pid ) ; it ! = all_processes . value ( ) . end ( ) )
process_name = it - > value . name ;
else
process_name = " (unknown) " ;
} else {
2020-12-26 08:58:50 -03:00
process_name = " (unknown) " ;
2021-01-02 00:58:47 -03:00
}
2020-12-26 08:58:50 -03:00
2020-07-01 16:07:53 -03:00
if ( profiling_enable ( pid ) < 0 ) {
int saved_errno = errno ;
2020-12-26 08:58:50 -03:00
GUI : : MessageBox : : show ( nullptr , String : : formatted ( " Unable to profile process {}({}): {} " , process_name , pid , strerror ( saved_errno ) ) , " Profiler " , GUI : : MessageBox : : Type : : Error ) ;
2020-07-01 15:49:51 -03:00
return false ;
2020-07-01 16:07:53 -03:00
}
2020-07-01 15:49:51 -03:00
2020-12-26 08:58:50 -03:00
if ( ! prompt_to_stop_profiling ( pid , process_name ) )
2020-07-01 15:49:51 -03:00
return false ;
2020-11-14 07:13:59 -03:00
if ( profiling_disable ( pid ) < 0 ) {
return false ;
}
2020-07-01 15:49:51 -03:00
return true ;
}