2020-01-18 08:38:21 +00:00
/*
2021-01-11 08:52:18 +00:00
* Copyright ( c ) 2018 - 2021 , Andreas Kling < kling @ serenityos . org >
2020-01-18 08:38:21 +00:00
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
*
* 1. Redistributions of source code must retain the above copyright notice , this
* list of conditions and the following disclaimer .
*
* 2. Redistributions in binary form must reproduce the above copyright notice ,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
* DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2021-02-27 17:33:30 +00:00
# include "IndividualSampleModel.h"
2019-12-12 21:01:06 +00:00
# include "Profile.h"
2019-12-14 17:44:29 +00:00
# include "ProfileTimelineWidget.h"
2020-07-01 19:07:53 +00:00
# include <LibCore/ArgsParser.h>
2020-07-01 18:49:51 +00:00
# include <LibCore/ElapsedTimer.h>
# include <LibCore/EventLoop.h>
2020-12-26 11:58:50 +00:00
# include <LibCore/ProcessStatisticsReader.h>
2020-07-01 18:49:51 +00:00
# include <LibCore/Timer.h>
2021-02-24 15:40:36 +00:00
# include <LibDesktop/Launcher.h>
2020-02-06 19:33:02 +00:00
# include <LibGUI/Action.h>
# include <LibGUI/Application.h>
# include <LibGUI/BoxLayout.h>
2020-07-01 18:49:51 +00:00
# include <LibGUI/Button.h>
# include <LibGUI/Label.h>
2020-02-06 19:33:02 +00:00
# include <LibGUI/Menu.h>
# include <LibGUI/MenuBar.h>
2020-07-01 18:49:51 +00:00
# include <LibGUI/MessageBox.h>
2020-02-16 08:17:49 +00:00
# include <LibGUI/Model.h>
2020-07-28 10:09:19 +00:00
# include <LibGUI/ProcessChooser.h>
2020-04-12 08:57:44 +00:00
# include <LibGUI/Splitter.h>
2021-02-27 17:33:30 +00:00
# include <LibGUI/TabWidget.h>
2020-04-11 16:46:11 +00:00
# include <LibGUI/TableView.h>
2020-02-06 19:33:02 +00:00
# include <LibGUI/TreeView.h>
# include <LibGUI/Window.h>
2020-07-01 18:49:51 +00:00
# include <serenity.h>
# include <string.h>
2021-01-11 08:52:18 +00:00
static bool generate_profile ( pid_t & pid ) ;
2019-12-12 21:01:06 +00:00
int main ( int argc , char * * argv )
{
2020-07-01 19:07:53 +00:00
int pid = 0 ;
2021-03-04 22:07:38 +00:00
const char * perfcore_file_arg = nullptr ;
Core : : ArgsParser args_parser ;
2020-07-01 19:07:53 +00:00
args_parser . add_option ( pid , " PID to profile " , " pid " , ' p ' , " PID " ) ;
2021-03-04 22:07:38 +00: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 19:07:53 +00:00
2020-07-04 12:05:19 +00:00
auto app = GUI : : Application : : construct ( argc , argv ) ;
2020-07-28 10:09:19 +00:00
auto app_icon = GUI : : Icon : : default_icon ( " app-profiler " ) ;
2020-07-01 18:49:51 +00:00
2021-03-04 22:07:38 +00:00
String perfcore_file ;
if ( ! perfcore_file_arg ) {
2020-07-01 19:07:53 +00:00
if ( ! generate_profile ( pid ) )
2020-07-01 18:49:51 +00:00
return 0 ;
2021-03-04 22:07:38 +00:00
perfcore_file = String : : formatted ( " /proc/{}/perf_events " , pid ) ;
2020-07-01 18:49:51 +00:00
} else {
2021-03-04 22:07:38 +00:00
perfcore_file = perfcore_file_arg ;
2019-12-12 21:01:06 +00:00
}
2021-03-04 22:07:38 +00:00
auto profile_or_error = Profile : : load_from_perfcore_file ( perfcore_file ) ;
2020-12-26 09:27:57 +00: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 21:01:06 +00:00
}
2020-12-26 09:27:57 +00:00
auto & profile = profile_or_error . value ( ) ;
2020-02-02 14:07:41 +00:00
auto window = GUI : : Window : : construct ( ) ;
2021-02-24 15:40:36 +00: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 17:43:17 +00:00
window - > set_title ( " Profiler " ) ;
2020-07-28 10:09:19 +00: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 15:24:05 +00:00
window - > resize ( 800 , 600 ) ;
2019-12-12 21:01:06 +00:00
2020-03-04 08:46:23 +00: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 17:44:29 +00:00
2020-03-04 18:07:55 +00:00
main_widget . add < ProfileTimelineWidget > ( * profile ) ;
2019-12-14 17:44:29 +00:00
2021-02-27 17:33:30 +00: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 16:46:11 +00:00
2020-04-12 08:57:44 +00:00
auto & tree_view = bottom_splitter . add < GUI : : TreeView > ( ) ;
2020-10-27 19:33:30 +00:00
tree_view . set_should_fill_selected_rows ( true ) ;
2020-08-25 09:25:39 +00:00
tree_view . set_column_headers_visible ( true ) ;
2020-03-04 18:07:55 +00:00
tree_view . set_model ( profile - > model ( ) ) ;
2019-12-12 21:01:06 +00:00
2020-04-12 08:57:44 +00:00
auto & disassembly_view = bottom_splitter . add < GUI : : TableView > ( ) ;
2020-04-11 16:46:11 +00:00
tree_view . on_selection = [ & ] ( auto & index ) {
profile - > set_disassembly_index ( index ) ;
disassembly_view . set_model ( profile - > disassembly_model ( ) ) ;
} ;
2021-02-27 17:33:30 +00: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 ) ) ;
} ;
2020-04-21 14:01:00 +00:00
auto menubar = GUI : : MenuBar : : construct ( ) ;
2021-03-25 20:41:39 +00:00
auto & app_menu = menubar - > add_menu ( " File " ) ;
2020-07-04 12:05:19 +00:00
app_menu . add_action ( GUI : : CommonActions : : make_quit_action ( [ & ] ( auto & ) { app - > quit ( ) ; } ) ) ;
2019-12-15 21:55:11 +00:00
2020-04-04 10:18:40 +00:00
auto & view_menu = menubar - > add_menu ( " View " ) ;
2020-10-19 13:04:54 +00:00
2020-04-21 15:19:27 +00:00
auto invert_action = GUI : : Action : : create_checkable ( " Invert tree " , { Mod_Ctrl , Key_I } , [ & ] ( auto & action ) {
2019-12-16 17:21:05 +00:00
profile - > set_inverted ( action . is_checked ( ) ) ;
} ) ;
2019-12-17 04:46:39 +00:00
invert_action - > set_checked ( false ) ;
2020-04-18 13:31:19 +00:00
view_menu . add_action ( invert_action ) ;
2019-12-16 17:21:05 +00:00
2020-10-19 13:04:54 +00:00
auto top_functions_action = GUI : : Action : : create_checkable ( " Top functions " , { Mod_Ctrl , Key_T } , [ & ] ( auto & action ) {
profile - > set_show_top_functions ( action . is_checked ( ) ) ;
} ) ;
top_functions_action - > set_checked ( false ) ;
view_menu . add_action ( top_functions_action ) ;
2020-04-21 15:19:27 +00:00
auto percent_action = GUI : : Action : : create_checkable ( " Show percentages " , { Mod_Ctrl , Key_P } , [ & ] ( auto & action ) {
2020-03-02 20:56:03 +00:00
profile - > set_show_percentages ( action . is_checked ( ) ) ;
2020-03-04 18:07:55 +00:00
tree_view . update ( ) ;
2020-04-12 13:22:17 +00:00
disassembly_view . update ( ) ;
2020-03-02 20:56:03 +00:00
} ) ;
percent_action - > set_checked ( false ) ;
2020-04-04 10:18:40 +00:00
view_menu . add_action ( percent_action ) ;
2019-12-16 17:21:05 +00:00
2020-07-28 10:09:19 +00:00
auto & help_menu = menubar - > add_menu ( " Help " ) ;
2021-02-24 15:40:36 +00: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 22:51:49 +00:00
help_menu . add_action ( GUI : : CommonActions : : make_about_action ( " Profiler " , app_icon , window ) ) ;
2020-07-28 10:09:19 +00:00
2021-03-25 20:41:39 +00:00
window - > set_menubar ( move ( menubar ) ) ;
2019-12-12 21:01:06 +00:00
window - > show ( ) ;
2020-07-04 12:05:19 +00:00
return app - > exec ( ) ;
2019-12-12 21:01:06 +00:00
}
2020-07-01 18:49:51 +00:00
2020-12-26 11:58:50 +00:00
static bool prompt_to_stop_profiling ( pid_t pid , const String & process_name )
2020-07-01 18:49:51 +00:00
{
auto window = GUI : : Window : : construct ( ) ;
2020-12-26 11:58:50 +00: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 15:24:05 +00:00
window - > set_icon ( Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-profiler.png " ) ) ;
2020-08-15 14:32:11 +00:00
window - > center_on_screen ( ) ;
2020-12-26 11:58:50 +00:00
2020-07-01 18:49:51 +00:00
auto & widget = window - > set_main_widget < GUI : : Widget > ( ) ;
widget . set_fill_with_background_color ( true ) ;
2020-12-26 11:58:50 +00:00
auto & layout = widget . set_layout < GUI : : VerticalBoxLayout > ( ) ;
layout . set_margins ( GUI : : Margins ( 0 , 0 , 0 , 16 ) ) ;
2020-07-01 18:49:51 +00:00
auto & timer_label = widget . add < GUI : : Label > ( " ... " ) ;
Core : : ElapsedTimer clock ;
clock . start ( ) ;
auto update_timer = Core : : Timer : : construct ( 100 , [ & ] {
timer_label . set_text ( String : : format ( " %.1f seconds " , ( float ) clock . elapsed ( ) / 1000.0f ) ) ;
} ) ;
auto & stop_button = widget . add < GUI : : Button > ( " Stop " ) ;
2020-12-30 00:23:32 +00:00
stop_button . set_fixed_size ( 140 , 22 ) ;
2020-07-01 18:49:51 +00:00
stop_button . on_click = [ & ] ( auto ) {
2020-07-04 14:52:01 +00:00
GUI : : Application : : the ( ) - > quit ( ) ;
2020-07-01 18:49:51 +00:00
} ;
window - > show ( ) ;
2020-07-04 14:52:01 +00:00
return GUI : : Application : : the ( ) - > exec ( ) = = 0 ;
2020-07-01 18:49:51 +00:00
}
2021-01-11 08:52:18 +00:00
bool generate_profile ( pid_t & pid )
2020-07-01 18:49:51 +00:00
{
2020-07-01 19:07:53 +00:00
if ( ! pid ) {
2020-07-28 10:09:19 +00:00
auto process_chooser = GUI : : ProcessChooser : : construct ( " Profiler " , " Profile " , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-profiler.png " ) ) ;
2020-07-02 10:12:16 +00:00
if ( process_chooser - > exec ( ) = = GUI : : Dialog : : ExecCancel )
2020-07-01 19:07:53 +00:00
return false ;
2020-07-02 10:12:16 +00:00
pid = process_chooser - > pid ( ) ;
2020-07-01 19:07:53 +00:00
}
2020-12-26 11:58:50 +00:00
String process_name ;
auto all_processes = Core : : ProcessStatisticsReader : : get_all ( ) ;
2021-01-02 03:58:47 +00: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 11:58:50 +00:00
process_name = " (unknown) " ;
2021-01-02 03:58:47 +00:00
}
2020-12-26 11:58:50 +00:00
2020-07-01 19:07:53 +00:00
if ( profiling_enable ( pid ) < 0 ) {
int saved_errno = errno ;
2020-12-26 11:58:50 +00: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 18:49:51 +00:00
return false ;
2020-07-01 19:07:53 +00:00
}
2020-07-01 18:49:51 +00:00
2020-12-26 11:58:50 +00:00
if ( ! prompt_to_stop_profiling ( pid , process_name ) )
2020-07-01 18:49:51 +00:00
return false ;
2020-11-14 10:13:59 +00:00
if ( profiling_disable ( pid ) < 0 ) {
return false ;
}
2020-07-01 18:49:51 +00:00
return true ;
}