robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
64a301184e
master
1#include <assert.h> 2#include <math.h> 3#include <stdbool.h> 4#include <stdint.h> 5#include <stdio.h> 6#include <stdlib.h> 7#include <string.h> 8#include <uv.h> 9 10#include "nvim/ascii_defs.h" 11#include "nvim/charset.h" 12#include "nvim/cmdexpand_defs.h" 13#include "nvim/debugger.h" 14#include "nvim/errors.h" 15#include "nvim/eval/typval_defs.h" 16#include "nvim/eval/userfunc.h" 17#include "nvim/eval/vars.h" 18#include "nvim/ex_cmds_defs.h" 19#include "nvim/fileio.h" 20#include "nvim/garray.h" 21#include "nvim/gettext_defs.h" 22#include "nvim/globals.h" 23#include "nvim/hashtab.h" 24#include "nvim/hashtab_defs.h" 25#include "nvim/keycodes.h" 26#include "nvim/memory.h" 27#include "nvim/message.h" 28#include "nvim/os/fs.h" 29#include "nvim/os/os.h" 30#include "nvim/os/time.h" 31#include "nvim/pos_defs.h" 32#include "nvim/profile.h" 33#include "nvim/runtime.h" 34#include "nvim/types_defs.h" 35 36#include "profile.c.generated.h" 37 38/// Struct used in sn_prl_ga for every line of a script. 39typedef struct { 40int snp_count ;///< nr of times line was executed 41proftime_T sn_prl_total ;///< time spent in a line + children 42proftime_T sn_prl_self ;///< time spent in a line itself 43}sn_prl_T ; 44 45#define PRL_ITEM (si ,idx ) (((sn_prl_T *)(si)->sn_prl_ga.ga_data)[(idx)]) 46 47static proftime_T prof_wait_time ; 48static char * startuptime_buf = NULL ;// --startuptime buffer 49 50/// Gets the current time. 51/// 52/// @return the current time 53proftime_T profile_start (void )FUNC_ATTR_WARN_UNUSED_RESULT 54{ 55return os_hrtime (); 56} 57 58/// Computes the time elapsed. 59/// 60/// @return Elapsed time from `tm` until now. 61proftime_T profile_end (proftime_T tm )FUNC_ATTR_WARN_UNUSED_RESULT 62{ 63return profile_sub (os_hrtime (),tm ); 64} 65 66/// Gets a string representing time `tm`. 67/// 68/// @warning Do not modify or free this string, not multithread-safe. 69/// 70/// @param tm Time 71/// @return Static string representing `tm` in the form "seconds.microseconds". 72const char * profile_msg (proftime_T tm )FUNC_ATTR_WARN_UNUSED_RESULT 73{ 74static char buf [50 ]; 75snprintf (buf ,sizeof (buf ),"%10.6lf" , 76 (double )profile_signed (tm ) /NS_PER_SEC ); 77return buf ; 78} 79 80/// Gets the time `msec` into the future. 81/// 82/// @param msec milliseconds, the maximum number of milliseconds is 83/// (2^63 / 10^6) - 1 = 9.223372e+12. 84/// @return if msec > 0, returns the time msec past now. Otherwise returns 85/// the zero time. 86proftime_T profile_setlimit (int64_t msec )FUNC_ATTR_WARN_UNUSED_RESULT 87{ 88if (msec <=0 ) { 89// no limit 90return profile_zero (); 91 } 92assert (msec <= (INT64_MAX /1000000LL )- 1 ); 93proftime_T nsec = (proftime_T )msec * 1000000ULL ; 94return os_hrtime ()+ nsec ; 95} 96 97/// Checks if current time has passed `tm`. 98/// 99/// @return true if the current time is past `tm`, false if not or if the 100/// timer was not set. 101bool profile_passed_limit (proftime_T tm )FUNC_ATTR_WARN_UNUSED_RESULT 102{ 103if (tm == 0 ) { 104// timer was not set 105return false; 106 } 107 108return profile_cmp (os_hrtime (),tm )< 0 ; 109} 110 111/// Gets the zero time. 112/// 113/// @return the zero time 114proftime_T profile_zero (void )FUNC_ATTR_CONST 115{ 116return 0 ; 117} 118 119/// Divides time `tm` by `count`. 120/// 121/// @return 0 if count <= 0, otherwise tm / count 122proftime_T profile_divide (proftime_T tm ,int count )FUNC_ATTR_CONST 123{ 124if (count <=0 ) { 125return profile_zero (); 126 } 127 128return (proftime_T )round ((double )tm / (double )count ); 129} 130 131/// Adds time `tm2` to `tm1`. 132/// 133/// @return `tm1` + `tm2` 134proftime_T profile_add (proftime_T tm1 ,proftime_T tm2 )FUNC_ATTR_CONST 135{ 136return tm1 + tm2 ; 137} 138 139/// Subtracts time `tm2` from `tm1`. 140/// 141/// Unsigned overflow (wraparound) occurs if `tm2` is greater than `tm1`. 142/// Use `profile_signed()` to get the signed integer value. 143/// 144/// @see profile_signed 145/// 146/// @return `tm1` - `tm2` 147proftime_T profile_sub (proftime_T tm1 ,proftime_T tm2 )FUNC_ATTR_CONST 148{ 149return tm1 - tm2 ; 150} 151 152/// Adds the `self` time from the total time and the `children` time. 153/// 154/// @return if `total` <= `children`, then self, otherwise `self` + `total` - 155/// `children` 156proftime_T profile_self (proftime_T self ,proftime_T total ,proftime_T children ) 157FUNC_ATTR_CONST 158{ 159// check that the result won't be negative, which can happen with 160// recursive calls. 161if (total <=children ) { 162return self ; 163 } 164 165// add the total time to self and subtract the children's time from self 166return profile_sub (profile_add (self ,total ),children ); 167} 168 169/// Gets the current waittime. 170/// 171/// @return the current waittime 172static proftime_T profile_get_wait (void )FUNC_ATTR_PURE 173{ 174return prof_wait_time ; 175} 176 177/// Sets the current waittime. 178void profile_set_wait (proftime_T wait ) 179{ 180prof_wait_time = wait ; 181} 182 183/// Subtracts the passed waittime since `tm`. 184/// 185/// @return `tma` - (waittime - `tm`) 186proftime_T profile_sub_wait (proftime_T tm ,proftime_T tma )FUNC_ATTR_PURE 187{ 188proftime_T tm3 = profile_sub (profile_get_wait (),tm ); 189return profile_sub (tma ,tm3 ); 190} 191 192/// Checks if time `tm1` is equal to `tm2`. 193/// 194/// @return true if `tm1` == `tm2` 195static bool profile_equal (proftime_T tm1 ,proftime_T tm2 )FUNC_ATTR_CONST 196{ 197return tm1 == tm2 ; 198} 199 200/// Converts time duration `tm` (`profile_sub` result) to a signed integer. 201/// 202/// @return signed representation of the given time value 203int64_t profile_signed (proftime_T tm ) 204FUNC_ATTR_CONST 205{ 206// (tm > INT64_MAX) is >=150 years, so we can assume it was produced by 207// arithmetic of two proftime_T values. For human-readable representation 208// (and Vim-compat) we want the difference after unsigned wraparound. #10452 209return (tm <=INT64_MAX ) ? (int64_t )tm :- (int64_t )(UINT64_MAX - tm ); 210} 211 212/// Compares profiling times. 213/// 214/// Times `tm1` and `tm2` must be less than 150 years apart. 215/// 216/// @return <0: `tm2` < `tm1` 217/// 0: `tm2` == `tm1` 218/// >0: `tm2` > `tm1` 219int profile_cmp (proftime_T tm1 ,proftime_T tm2 )FUNC_ATTR_CONST 220{ 221if (tm1 == tm2 ) { 222return 0 ; 223 } 224return profile_signed (tm2 - tm1 )< 0 ?-1 :1 ; 225} 226 227static char * profile_fname = NULL ; 228 229/// Reset all profiling information. 230void profile_reset (void ) 231{ 232// Reset sourced files. 233for (int id = 1 ;id <=script_items .ga_len ;id ++ ) { 234scriptitem_T * si = SCRIPT_ITEM (id ); 235if (si -> sn_prof_on ) { 236si -> sn_prof_on = false; 237si -> sn_pr_force = false; 238si -> sn_pr_child = profile_zero (); 239si -> sn_pr_nest = 0 ; 240si -> sn_pr_count = 0 ; 241si -> sn_pr_total = profile_zero (); 242si -> sn_pr_self = profile_zero (); 243si -> sn_pr_start = profile_zero (); 244si -> sn_pr_children = profile_zero (); 245ga_clear (& si -> sn_prl_ga ); 246si -> sn_prl_start = profile_zero (); 247si -> sn_prl_children = profile_zero (); 248si -> sn_prl_wait = profile_zero (); 249si -> sn_prl_idx = -1 ; 250si -> sn_prl_execed = 0 ; 251 } 252 } 253 254// Reset functions. 255hashtab_T * const functbl = func_tbl_get (); 256size_t todo = functbl -> ht_used ; 257hashitem_T * hi = functbl -> ht_array ; 258 259for (;todo > 0 ;hi ++ ) { 260if (!HASHITEM_EMPTY (hi )) { 261todo -- ; 262ufunc_T * uf = HI2UF (hi ); 263if (uf -> uf_prof_initialized ) { 264uf -> uf_profiling = 0 ; 265uf -> uf_tm_count = 0 ; 266uf -> uf_tm_total = profile_zero (); 267uf -> uf_tm_self = profile_zero (); 268uf -> uf_tm_children = profile_zero (); 269 270for (int i = 0 ;i < uf -> uf_lines .ga_len ;i ++ ) { 271uf -> uf_tml_count [i ]= 0 ; 272uf -> uf_tml_total [i ]= uf -> uf_tml_self [i ]= 0 ; 273 } 274 275uf -> uf_tml_start = profile_zero (); 276uf -> uf_tml_children = profile_zero (); 277uf -> uf_tml_wait = profile_zero (); 278uf -> uf_tml_idx = -1 ; 279uf -> uf_tml_execed = 0 ; 280 } 281 } 282 } 283 284XFREE_CLEAR (profile_fname ); 285} 286 287/// ":profile cmd args" 288void ex_profile (exarg_T * eap ) 289{ 290static proftime_T pause_time ; 291 292char * e = skiptowhite (eap -> arg ); 293int len = (int )(e - eap -> arg ); 294e = skipwhite (e ); 295 296if (len == 5 && strncmp (eap -> arg ,"start" ,5 )== 0 && * e != NUL ) { 297xfree (profile_fname ); 298profile_fname = expand_env_save_opt (e , true,NULL ); 299do_profiling = PROF_YES ; 300profile_set_wait (profile_zero ()); 301set_vim_var_nr (VV_PROFILING ,1 ); 302 }else if (do_profiling == PROF_NONE ) { 303emsg (_ ("E750: First use \":profile start {fname}\"" )); 304 }else if (strcmp (eap -> arg ,"stop" )== 0 ) { 305profile_dump (); 306do_profiling = PROF_NONE ; 307set_vim_var_nr (VV_PROFILING ,0 ); 308profile_reset (); 309 }else if (strcmp (eap -> arg ,"pause" )== 0 ) { 310if (do_profiling == PROF_YES ) { 311pause_time = profile_start (); 312 } 313do_profiling = PROF_PAUSED ; 314 }else if (strcmp (eap -> arg ,"continue" )== 0 ) { 315if (do_profiling == PROF_PAUSED ) { 316pause_time = profile_end (pause_time ); 317profile_set_wait (profile_add (profile_get_wait (),pause_time )); 318 } 319do_profiling = PROF_YES ; 320 }else if (strcmp (eap -> arg ,"dump" )== 0 ) { 321profile_dump (); 322 }else { 323// The rest is similar to ":breakadd". 324ex_breakadd (eap ); 325 } 326} 327 328/// Command line expansion for :profile. 329static enum { 330PEXP_SUBCMD ,///< expand :profile sub-commands 331PEXP_FUNC ,///< expand :profile func {funcname} 332}pexpand_what ; 333 334static char * pexpand_cmds []= { 335"continue" , 336"dump" , 337"file" , 338"func" , 339"pause" , 340"start" , 341"stop" , 342NULL 343}; 344 345/// Function given to ExpandGeneric() to obtain the profile command 346/// specific expansion. 347char * get_profile_name (expand_T * xp ,int idx ) 348FUNC_ATTR_PURE 349{ 350switch (pexpand_what ) { 351case PEXP_SUBCMD : 352return pexpand_cmds [idx ]; 353default : 354return NULL ; 355 } 356} 357 358/// Handle command line completion for :profile command. 359void set_context_in_profile_cmd (expand_T * xp ,const char * arg ) 360{ 361// Default: expand subcommands. 362xp -> xp_context = EXPAND_PROFILE ; 363pexpand_what = PEXP_SUBCMD ; 364xp -> xp_pattern = (char * )arg ; 365 366char * const end_subcmd = skiptowhite (arg ); 367if (* end_subcmd == NUL ) { 368return ; 369 } 370 371if ((end_subcmd - arg == 5 && strncmp (arg ,"start" ,5 )== 0 ) 372|| (end_subcmd - arg == 4 && strncmp (arg ,"file" ,4 )== 0 )) { 373xp -> xp_context = EXPAND_FILES ; 374xp -> xp_pattern = skipwhite (end_subcmd ); 375return ; 376 }else if (end_subcmd - arg == 4 && strncmp (arg ,"func" ,4 )== 0 ) { 377xp -> xp_context = EXPAND_USER_FUNC ; 378xp -> xp_pattern = skipwhite (end_subcmd ); 379return ; 380 } 381 382xp -> xp_context = EXPAND_NOTHING ; 383} 384 385static proftime_T wait_time ; 386 387/// Called when starting to wait for the user to type a character. 388void prof_input_start (void ) 389{ 390wait_time = profile_start (); 391} 392 393/// Called when finished waiting for the user to type a character. 394void prof_input_end (void ) 395{ 396wait_time = profile_end (wait_time ); 397profile_set_wait (profile_add (profile_get_wait (),wait_time )); 398} 399 400/// @return true when a function defined in the current script should be 401/// profiled. 402bool prof_def_func (void ) 403FUNC_ATTR_PURE 404{ 405if (current_sctx .sc_sid > 0 ) { 406return SCRIPT_ITEM (current_sctx .sc_sid )-> sn_pr_force ; 407 } 408return false; 409} 410 411/// Print the count and times for one function or function line. 412/// 413/// @param prefer_self when equal print only self time 414static void prof_func_line (FILE * fd ,int count ,const proftime_T * total ,const proftime_T * self , 415bool prefer_self ) 416{ 417if (count > 0 ) { 418fprintf (fd ,"%5d " ,count ); 419if (prefer_self && profile_equal (* total ,* self )) { 420fprintf (fd ," " ); 421 }else { 422fprintf (fd ,"%s " ,profile_msg (* total )); 423 } 424if (!prefer_self && profile_equal (* total ,* self )) { 425fprintf (fd ," " ); 426 }else { 427fprintf (fd ,"%s " ,profile_msg (* self )); 428 } 429 }else { 430fprintf (fd ," " ); 431 } 432} 433 434/// @param prefer_self when equal print only self time 435static void prof_sort_list (FILE * fd ,ufunc_T ** sorttab ,int st_len ,char * title ,bool prefer_self ) 436{ 437fprintf (fd ,"FUNCTIONS SORTED ON %s TIME\n" ,title ); 438fprintf (fd ,"count total (s) self (s) function\n" ); 439for (int i = 0 ;i < 20 && i < st_len ;i ++ ) { 440ufunc_T * fp = sorttab [i ]; 441prof_func_line (fd ,fp -> uf_tm_count ,& fp -> uf_tm_total ,& fp -> uf_tm_self , 442prefer_self ); 443if ((uint8_t )fp -> uf_name [0 ]== K_SPECIAL ) { 444fprintf (fd ," <SNR>%s()\n" ,fp -> uf_name + 3 ); 445 }else { 446fprintf (fd ," %s()\n" ,fp -> uf_name ); 447 } 448 } 449fprintf (fd ,"\n" ); 450} 451 452/// Compare function for total time sorting. 453static int prof_total_cmp (const void * s1 ,const void * s2 ) 454{ 455ufunc_T * p1 = * (ufunc_T ** )s1 ; 456ufunc_T * p2 = * (ufunc_T ** )s2 ; 457return profile_cmp (p1 -> uf_tm_total ,p2 -> uf_tm_total ); 458} 459 460/// Compare function for self time sorting. 461static int prof_self_cmp (const void * s1 ,const void * s2 ) 462{ 463ufunc_T * p1 = * (ufunc_T ** )s1 ; 464ufunc_T * p2 = * (ufunc_T ** )s2 ; 465return profile_cmp (p1 -> uf_tm_self ,p2 -> uf_tm_self ); 466} 467 468/// Start profiling function "fp". 469void func_do_profile (ufunc_T * fp ) 470{ 471int len = fp -> uf_lines .ga_len ; 472 473if (!fp -> uf_prof_initialized ) { 474if (len == 0 ) { 475len = 1 ;// avoid getting error for allocating zero bytes 476 } 477fp -> uf_tm_count = 0 ; 478fp -> uf_tm_self = profile_zero (); 479fp -> uf_tm_total = profile_zero (); 480 481if (fp -> uf_tml_count == NULL ) { 482fp -> uf_tml_count = xcalloc ((size_t )len ,sizeof (int )); 483 } 484 485if (fp -> uf_tml_total == NULL ) { 486fp -> uf_tml_total = xcalloc ((size_t )len ,sizeof (proftime_T )); 487 } 488 489if (fp -> uf_tml_self == NULL ) { 490fp -> uf_tml_self = xcalloc ((size_t )len ,sizeof (proftime_T )); 491 } 492 493fp -> uf_tml_idx = -1 ; 494fp -> uf_prof_initialized = true; 495 } 496 497fp -> uf_profiling = true; 498} 499 500/// Prepare profiling for entering a child or something else that is not 501/// counted for the script/function itself. 502/// Should always be called in pair with prof_child_exit(). 503/// 504/// @param tm place to store waittime 505void prof_child_enter (proftime_T * tm ) 506{ 507funccall_T * fc = get_current_funccal (); 508 509if (fc != NULL && fc -> fc_func -> uf_profiling ) { 510fc -> fc_prof_child = profile_start (); 511 } 512 513script_prof_save (tm ); 514} 515 516/// Take care of time spent in a child. 517/// Should always be called after prof_child_enter(). 518/// 519/// @param tm where waittime was stored 520void prof_child_exit (proftime_T * tm ) 521{ 522funccall_T * fc = get_current_funccal (); 523 524if (fc != NULL && fc -> fc_func -> uf_profiling ) { 525fc -> fc_prof_child = profile_end (fc -> fc_prof_child ); 526// don't count waiting time 527fc -> fc_prof_child = profile_sub_wait (* tm ,fc -> fc_prof_child ); 528fc -> fc_func -> uf_tm_children = 529profile_add (fc -> fc_func -> uf_tm_children ,fc -> fc_prof_child ); 530fc -> fc_func -> uf_tml_children = 531profile_add (fc -> fc_func -> uf_tml_children ,fc -> fc_prof_child ); 532 } 533script_prof_restore (tm ); 534} 535 536/// Called when starting to read a function line. 537/// "sourcing_lnum" must be correct! 538/// When skipping lines it may not actually be executed, but we won't find out 539/// until later and we need to store the time now. 540void func_line_start (void * cookie ) 541{ 542funccall_T * fcp = (funccall_T * )cookie ; 543ufunc_T * fp = fcp -> fc_func ; 544 545if (fp -> uf_profiling && SOURCING_LNUM >=1 && SOURCING_LNUM <=fp -> uf_lines .ga_len ) { 546fp -> uf_tml_idx = SOURCING_LNUM - 1 ; 547// Skip continuation lines. 548while (fp -> uf_tml_idx > 0 && FUNCLINE (fp ,fp -> uf_tml_idx )== NULL ) { 549fp -> uf_tml_idx -- ; 550 } 551fp -> uf_tml_execed = false; 552fp -> uf_tml_start = profile_start (); 553fp -> uf_tml_children = profile_zero (); 554fp -> uf_tml_wait = profile_get_wait (); 555 } 556} 557 558/// Called when actually executing a function line. 559void func_line_exec (void * cookie ) 560{ 561funccall_T * fcp = (funccall_T * )cookie ; 562ufunc_T * fp = fcp -> fc_func ; 563 564if (fp -> uf_profiling && fp -> uf_tml_idx >=0 ) { 565fp -> uf_tml_execed = true; 566 } 567} 568 569/// Called when done with a function line. 570void func_line_end (void * cookie ) 571{ 572funccall_T * fcp = (funccall_T * )cookie ; 573ufunc_T * fp = fcp -> fc_func ; 574 575if (fp -> uf_profiling && fp -> uf_tml_idx >=0 ) { 576if (fp -> uf_tml_execed ) { 577fp -> uf_tml_count [fp -> uf_tml_idx ]++ ; 578fp -> uf_tml_start = profile_end (fp -> uf_tml_start ); 579fp -> uf_tml_start = profile_sub_wait (fp -> uf_tml_wait ,fp -> uf_tml_start ); 580fp -> uf_tml_total [fp -> uf_tml_idx ]= 581profile_add (fp -> uf_tml_total [fp -> uf_tml_idx ],fp -> uf_tml_start ); 582fp -> uf_tml_self [fp -> uf_tml_idx ]= 583profile_self (fp -> uf_tml_self [fp -> uf_tml_idx ],fp -> uf_tml_start , 584fp -> uf_tml_children ); 585 } 586fp -> uf_tml_idx = -1 ; 587 } 588} 589 590/// Dump the profiling results for all functions in file "fd". 591static void func_dump_profile (FILE * fd ) 592{ 593hashtab_T * const functbl = func_tbl_get (); 594int st_len = 0 ; 595 596int todo = (int )functbl -> ht_used ; 597if (todo == 0 ) { 598return ;// nothing to dump 599 } 600 601ufunc_T ** sorttab = xmalloc (sizeof (ufunc_T * )* (size_t )todo ); 602 603for (hashitem_T * hi = functbl -> ht_array ;todo > 0 ;hi ++ ) { 604if (!HASHITEM_EMPTY (hi )) { 605todo -- ; 606ufunc_T * fp = HI2UF (hi ); 607if (fp -> uf_prof_initialized ) { 608sorttab [st_len ++ ]= fp ; 609 610if ((uint8_t )fp -> uf_name [0 ]== K_SPECIAL ) { 611fprintf (fd ,"FUNCTION <SNR>%s()\n" ,fp -> uf_name + 3 ); 612 }else { 613fprintf (fd ,"FUNCTION %s()\n" ,fp -> uf_name ); 614 } 615if (fp -> uf_script_ctx .sc_sid != 0 ) { 616bool should_free ; 617char * p = get_scriptname (fp -> uf_script_ctx ,& should_free ); 618fprintf (fd ," Defined: %s:%" PRIdLINENR "\n" , 619p ,fp -> uf_script_ctx .sc_lnum ); 620if (should_free ) { 621xfree (p ); 622 } 623 } 624if (fp -> uf_tm_count == 1 ) { 625fprintf (fd ,"Called 1 time\n" ); 626 }else { 627fprintf (fd ,"Called %d times\n" ,fp -> uf_tm_count ); 628 } 629fprintf (fd ,"Total time: %s\n" ,profile_msg (fp -> uf_tm_total )); 630fprintf (fd ," Self time: %s\n" ,profile_msg (fp -> uf_tm_self )); 631fprintf (fd ,"\n" ); 632fprintf (fd ,"count total (s) self (s)\n" ); 633 634for (int i = 0 ;i < fp -> uf_lines .ga_len ;i ++ ) { 635if (FUNCLINE (fp ,i )== NULL ) { 636continue ; 637 } 638prof_func_line (fd ,fp -> uf_tml_count [i ], 639& fp -> uf_tml_total [i ],& fp -> uf_tml_self [i ], true); 640fprintf (fd ,"%s\n" ,FUNCLINE (fp ,i )); 641 } 642fprintf (fd ,"\n" ); 643 } 644 } 645 } 646 647if (st_len > 0 ) { 648qsort ((void * )sorttab , (size_t )st_len ,sizeof (ufunc_T * ), 649prof_total_cmp ); 650prof_sort_list (fd ,sorttab ,st_len ,"TOTAL" , false); 651qsort ((void * )sorttab , (size_t )st_len ,sizeof (ufunc_T * ), 652prof_self_cmp ); 653prof_sort_list (fd ,sorttab ,st_len ,"SELF" , true); 654 } 655 656xfree (sorttab ); 657} 658 659/// Start profiling a script. 660void profile_init (scriptitem_T * si ) 661{ 662si -> sn_pr_count = 0 ; 663si -> sn_pr_total = profile_zero (); 664si -> sn_pr_self = profile_zero (); 665 666ga_init (& si -> sn_prl_ga ,sizeof (sn_prl_T ),100 ); 667si -> sn_prl_idx = -1 ; 668si -> sn_prof_on = true; 669si -> sn_pr_nest = 0 ; 670} 671 672/// Save time when starting to invoke another script or function. 673/// 674/// @param tm place to store wait time 675void script_prof_save (proftime_T * tm ) 676{ 677if (current_sctx .sc_sid > 0 && current_sctx .sc_sid <=script_items .ga_len ) { 678scriptitem_T * si = SCRIPT_ITEM (current_sctx .sc_sid ); 679if (si -> sn_prof_on && si -> sn_pr_nest ++ == 0 ) { 680si -> sn_pr_child = profile_start (); 681 } 682 } 683* tm = profile_get_wait (); 684} 685 686/// Count time spent in children after invoking another script or function. 687void script_prof_restore (const proftime_T * tm ) 688{ 689if (!SCRIPT_ID_VALID (current_sctx .sc_sid )) { 690return ; 691 } 692 693scriptitem_T * si = SCRIPT_ITEM (current_sctx .sc_sid ); 694if (si -> sn_prof_on && -- si -> sn_pr_nest == 0 ) { 695si -> sn_pr_child = profile_end (si -> sn_pr_child ); 696// don't count wait time 697si -> sn_pr_child = profile_sub_wait (* tm ,si -> sn_pr_child ); 698si -> sn_pr_children = profile_add (si -> sn_pr_children ,si -> sn_pr_child ); 699si -> sn_prl_children = profile_add (si -> sn_prl_children ,si -> sn_pr_child ); 700 } 701} 702 703/// Dump the profiling results for all scripts in file "fd". 704static void script_dump_profile (FILE * fd ) 705{ 706sn_prl_T * pp ; 707 708for (int id = 1 ;id <=script_items .ga_len ;id ++ ) { 709scriptitem_T * si = SCRIPT_ITEM (id ); 710if (si -> sn_prof_on ) { 711fprintf (fd ,"SCRIPT %s\n" ,si -> sn_name ); 712if (si -> sn_pr_count == 1 ) { 713fprintf (fd ,"Sourced 1 time\n" ); 714 }else { 715fprintf (fd ,"Sourced %d times\n" ,si -> sn_pr_count ); 716 } 717fprintf (fd ,"Total time: %s\n" ,profile_msg (si -> sn_pr_total )); 718fprintf (fd ," Self time: %s\n" ,profile_msg (si -> sn_pr_self )); 719fprintf (fd ,"\n" ); 720fprintf (fd ,"count total (s) self (s)\n" ); 721 722FILE * sfd = os_fopen (si -> sn_name ,"r" ); 723if (sfd == NULL ) { 724fprintf (fd ,"Cannot open file!\n" ); 725 }else { 726// Keep going till the end of file, so that trailing 727// continuation lines are listed. 728for (int i = 0 ;;i ++ ) { 729if (vim_fgets (IObuff ,IOSIZE ,sfd )) { 730break ; 731 } 732// When a line has been truncated, append NL, taking care 733// of multi-byte characters . 734if (IObuff [IOSIZE - 2 ]!= NUL && IObuff [IOSIZE - 2 ]!= NL ) { 735int n = IOSIZE - 2 ; 736 737// Move to the first byte of this char. 738// utf_head_off() doesn't work, because it checks 739// for a truncated character. 740while (n > 0 && (IObuff [n ]& 0xc0 )== 0x80 ) { 741n -- ; 742 } 743 744IObuff [n ]= NL ; 745IObuff [n + 1 ]= NUL ; 746 } 747if (i < si -> sn_prl_ga .ga_len 748&& (pp = & PRL_ITEM (si ,i ))-> snp_count > 0 ) { 749fprintf (fd ,"%5d " ,pp -> snp_count ); 750if (profile_equal (pp -> sn_prl_total ,pp -> sn_prl_self )) { 751fprintf (fd ," " ); 752 }else { 753fprintf (fd ,"%s " ,profile_msg (pp -> sn_prl_total )); 754 } 755fprintf (fd ,"%s " ,profile_msg (pp -> sn_prl_self )); 756 }else { 757fprintf (fd ," " ); 758 } 759fprintf (fd ,"%s" ,IObuff ); 760 } 761fclose (sfd ); 762 } 763fprintf (fd ,"\n" ); 764 } 765 } 766} 767 768/// Dump the profiling info. 769void profile_dump (void ) 770{ 771if (profile_fname == NULL ) { 772return ; 773 } 774 775FILE * fd = os_fopen (profile_fname ,"w" ); 776if (fd == NULL ) { 777semsg (_ (e_notopen ),profile_fname ); 778 }else { 779script_dump_profile (fd ); 780func_dump_profile (fd ); 781fclose (fd ); 782 } 783} 784 785/// Called when starting to read a script line. 786/// "sourcing_lnum" must be correct! 787/// When skipping lines it may not actually be executed, but we won't find out 788/// until later and we need to store the time now. 789void script_line_start (void ) 790{ 791if (current_sctx .sc_sid <=0 || current_sctx .sc_sid > script_items .ga_len ) { 792return ; 793 } 794scriptitem_T * si = SCRIPT_ITEM (current_sctx .sc_sid ); 795if (si -> sn_prof_on && SOURCING_LNUM >=1 ) { 796// Grow the array before starting the timer, so that the time spent 797// here isn't counted. 798ga_grow (& si -> sn_prl_ga ,SOURCING_LNUM - si -> sn_prl_ga .ga_len ); 799si -> sn_prl_idx = SOURCING_LNUM - 1 ; 800while (si -> sn_prl_ga .ga_len <=si -> sn_prl_idx 801&& si -> sn_prl_ga .ga_len < si -> sn_prl_ga .ga_maxlen ) { 802// Zero counters for a line that was not used before. 803sn_prl_T * pp = & PRL_ITEM (si ,si -> sn_prl_ga .ga_len ); 804pp -> snp_count = 0 ; 805pp -> sn_prl_total = profile_zero (); 806pp -> sn_prl_self = profile_zero (); 807si -> sn_prl_ga .ga_len ++ ; 808 } 809si -> sn_prl_execed = false; 810si -> sn_prl_start = profile_start (); 811si -> sn_prl_children = profile_zero (); 812si -> sn_prl_wait = profile_get_wait (); 813 } 814} 815 816/// Called when actually executing a function line. 817void script_line_exec (void ) 818{ 819if (current_sctx .sc_sid <=0 || current_sctx .sc_sid > script_items .ga_len ) { 820return ; 821 } 822scriptitem_T * si = SCRIPT_ITEM (current_sctx .sc_sid ); 823if (si -> sn_prof_on && si -> sn_prl_idx >=0 ) { 824si -> sn_prl_execed = true; 825 } 826} 827 828/// Called when done with a function line. 829void script_line_end (void ) 830{ 831if (current_sctx .sc_sid <=0 || current_sctx .sc_sid > script_items .ga_len ) { 832return ; 833 } 834scriptitem_T * si = SCRIPT_ITEM (current_sctx .sc_sid ); 835if (si -> sn_prof_on && si -> sn_prl_idx >=0 836&& si -> sn_prl_idx < si -> sn_prl_ga .ga_len ) { 837if (si -> sn_prl_execed ) { 838sn_prl_T * pp = & PRL_ITEM (si ,si -> sn_prl_idx ); 839pp -> snp_count ++ ; 840si -> sn_prl_start = profile_end (si -> sn_prl_start ); 841si -> sn_prl_start = profile_sub_wait (si -> sn_prl_wait ,si -> sn_prl_start ); 842pp -> sn_prl_total = profile_add (pp -> sn_prl_total ,si -> sn_prl_start ); 843pp -> sn_prl_self = profile_self (pp -> sn_prl_self ,si -> sn_prl_start , 844si -> sn_prl_children ); 845 } 846si -> sn_prl_idx = -1 ; 847 } 848} 849 850/// globals for use in the startuptime related functionality (time_*). 851static proftime_T g_start_time ; 852static proftime_T g_prev_time ; 853 854/// Saves the previous time before doing something that could nest. 855/// 856/// After calling this function, the static global `g_prev_time` will 857/// contain the current time. 858/// 859/// @param[out] rel to the time elapsed so far 860/// @param[out] start the current time 861void time_push (proftime_T * rel ,proftime_T * start ) 862{ 863proftime_T now = profile_start (); 864 865// subtract the previous time from now, store it in `rel` 866* rel = profile_sub (now ,g_prev_time ); 867* start = now ; 868 869// reset global `g_prev_time` for the next call 870g_prev_time = now ; 871} 872 873/// Computes the prev time after doing something that could nest. 874/// 875/// Subtracts `tp` from the static global `g_prev_time`. 876/// 877/// @param tp the time to subtract 878void time_pop (proftime_T tp ) 879{ 880g_prev_time -= tp ; 881} 882 883/// Prints the difference between `then` and `now`. 884/// 885/// the format is "msec.usec". 886static void time_diff (proftime_T then ,proftime_T now ) 887{ 888proftime_T diff = profile_sub (now ,then ); 889fprintf (time_fd ,"%07.3lf" , (double )diff /1.0E6 ); 890} 891 892/// Initializes the startuptime code. 893/// 894/// Must be called once before calling other startuptime code (such as 895/// time_{push,pop,msg,...}). 896/// 897/// @param message the message that will be displayed 898void time_start (const char * message ) 899{ 900if (time_fd == NULL ) { 901return ; 902 } 903 904// initialize the global variables 905g_prev_time = g_start_time = profile_start (); 906 907fprintf (time_fd ,"\ntimes in msec\n" ); 908fprintf (time_fd ," clock self+sourced self: sourced script\n" ); 909fprintf (time_fd ," clock elapsed: other lines\n\n" ); 910 911time_msg (message ,NULL ); 912} 913 914/// Prints out timing info. 915/// 916/// @warning don't forget to call `time_start()` once before calling this. 917/// 918/// @param mesg the message to display next to the timing information 919/// @param start only for do_source: start time 920void time_msg (const char * mesg ,const proftime_T * start ) 921{ 922if (time_fd == NULL ) { 923return ; 924 } 925 926// print out the difference between `start` (init earlier) and `now` 927proftime_T now = profile_start (); 928time_diff (g_start_time ,now ); 929 930// if `start` was supplied, print the diff between `start` and `now` 931if (start != NULL ) { 932fprintf (time_fd ," " ); 933time_diff (* start ,now ); 934 } 935 936// print the difference between the global `g_prev_time` and `now` 937fprintf (time_fd ," " ); 938time_diff (g_prev_time ,now ); 939 940// reset `g_prev_time` and print the message 941g_prev_time = now ; 942fprintf (time_fd ,": %s\n" ,mesg ); 943} 944 945/// Initializes the `time_fd` stream for the --startuptime report. 946/// 947/// @param fname startuptime report file path 948/// @param proc_name name of the current Nvim process to write in the report. 949void time_init (const char * fname ,const char * proc_name ) 950{ 951const size_t bufsize = 8192 ;// Big enough for the entire --startuptime report. 952time_fd = fopen (fname ,"a" ); 953if (time_fd == NULL ) { 954fprintf (stderr ,_ (e_notopen ),fname ); 955return ; 956 } 957startuptime_buf = xmalloc (sizeof (char )* (bufsize + 1 )); 958// The startuptime file is (potentially) written by multiple Nvim processes concurrently. So each 959// report is buffered, and flushed to disk (`time_finish`) once after startup. `_IOFBF` mode 960// ensures the buffer is not auto-flushed ("controlled buffering"). 961int r = setvbuf (time_fd ,startuptime_buf ,_IOFBF ,bufsize + 1 ); 962if (r != 0 ) { 963XFREE_CLEAR (startuptime_buf ); 964fclose (time_fd ); 965time_fd = NULL ; 966fprintf (stderr ,"time_init: setvbuf failed: %d %s" ,r ,uv_err_name (r )); 967return ; 968 } 969fprintf (time_fd ,"--- Startup times for process: %s ---\n" ,proc_name ); 970} 971 972/// Flushes the startuptimes to disk for the current process 973void time_finish (void ) 974{ 975if (time_fd == NULL ) { 976return ; 977 } 978assert (startuptime_buf != NULL ); 979TIME_MSG ("--- NVIM STARTED ---\n" ); 980 981// flush buffer to disk 982fclose (time_fd ); 983time_fd = NULL ; 984 985XFREE_CLEAR (startuptime_buf ); 986}