Mercurial > vim
comparison src/ex_cmds2.c @ 17381:8f44c630c366 v8.1.1689
patch 8.1.1689: profiling code is spread out
commit https://github.com/vim/vim/commit/660a10ad41c14363326f83451c3c425201923119
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jul 14 15:48:38 2019 +0200
patch 8.1.1689: profiling code is spread out
Problem: Profiling code is spread out.
Solution: Move more profiling code to profiler.c. (Yegappan Lakshmanan,
closes #4668)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 14 Jul 2019 16:00:06 +0200 |
parents | ba06a1c42274 |
children | 892b4ea3bad6 |
comparison
equal
deleted
inserted
replaced
17380:9edf684600d7 | 17381:8f44c630c366 |
---|---|
393 } | 393 } |
394 } | 394 } |
395 # endif | 395 # endif |
396 # endif | 396 # endif |
397 | 397 |
398 # if defined(FEAT_PROFILE) || defined(PROTO) | |
399 /* | |
400 * Start profiling script "fp". | |
401 */ | |
402 void | |
403 script_do_profile(scriptitem_T *si) | |
404 { | |
405 si->sn_pr_count = 0; | |
406 profile_zero(&si->sn_pr_total); | |
407 profile_zero(&si->sn_pr_self); | |
408 | |
409 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100); | |
410 si->sn_prl_idx = -1; | |
411 si->sn_prof_on = TRUE; | |
412 si->sn_pr_nest = 0; | |
413 } | |
414 | |
415 /* | |
416 * Save time when starting to invoke another script or function. | |
417 */ | |
418 void | |
419 script_prof_save( | |
420 proftime_T *tm) /* place to store wait time */ | |
421 { | |
422 scriptitem_T *si; | |
423 | |
424 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len) | |
425 { | |
426 si = &SCRIPT_ITEM(current_sctx.sc_sid); | |
427 if (si->sn_prof_on && si->sn_pr_nest++ == 0) | |
428 profile_start(&si->sn_pr_child); | |
429 } | |
430 profile_get_wait(tm); | |
431 } | |
432 | |
433 /* | |
434 * Count time spent in children after invoking another script or function. | |
435 */ | |
436 void | |
437 script_prof_restore(proftime_T *tm) | |
438 { | |
439 scriptitem_T *si; | |
440 | |
441 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len) | |
442 { | |
443 si = &SCRIPT_ITEM(current_sctx.sc_sid); | |
444 if (si->sn_prof_on && --si->sn_pr_nest == 0) | |
445 { | |
446 profile_end(&si->sn_pr_child); | |
447 profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */ | |
448 profile_add(&si->sn_pr_children, &si->sn_pr_child); | |
449 profile_add(&si->sn_prl_children, &si->sn_pr_child); | |
450 } | |
451 } | |
452 } | |
453 | |
454 /* | |
455 * Dump the profiling results for all scripts in file "fd". | |
456 */ | |
457 void | |
458 script_dump_profile(FILE *fd) | |
459 { | |
460 int id; | |
461 scriptitem_T *si; | |
462 int i; | |
463 FILE *sfd; | |
464 sn_prl_T *pp; | |
465 | |
466 for (id = 1; id <= script_items.ga_len; ++id) | |
467 { | |
468 si = &SCRIPT_ITEM(id); | |
469 if (si->sn_prof_on) | |
470 { | |
471 fprintf(fd, "SCRIPT %s\n", si->sn_name); | |
472 if (si->sn_pr_count == 1) | |
473 fprintf(fd, "Sourced 1 time\n"); | |
474 else | |
475 fprintf(fd, "Sourced %d times\n", si->sn_pr_count); | |
476 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total)); | |
477 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self)); | |
478 fprintf(fd, "\n"); | |
479 fprintf(fd, "count total (s) self (s)\n"); | |
480 | |
481 sfd = mch_fopen((char *)si->sn_name, "r"); | |
482 if (sfd == NULL) | |
483 fprintf(fd, "Cannot open file!\n"); | |
484 else | |
485 { | |
486 /* Keep going till the end of file, so that trailing | |
487 * continuation lines are listed. */ | |
488 for (i = 0; ; ++i) | |
489 { | |
490 if (vim_fgets(IObuff, IOSIZE, sfd)) | |
491 break; | |
492 /* When a line has been truncated, append NL, taking care | |
493 * of multi-byte characters . */ | |
494 if (IObuff[IOSIZE - 2] != NUL && IObuff[IOSIZE - 2] != NL) | |
495 { | |
496 int n = IOSIZE - 2; | |
497 | |
498 if (enc_utf8) | |
499 { | |
500 /* Move to the first byte of this char. | |
501 * utf_head_off() doesn't work, because it checks | |
502 * for a truncated character. */ | |
503 while (n > 0 && (IObuff[n] & 0xc0) == 0x80) | |
504 --n; | |
505 } | |
506 else if (has_mbyte) | |
507 n -= mb_head_off(IObuff, IObuff + n); | |
508 IObuff[n] = NL; | |
509 IObuff[n + 1] = NUL; | |
510 } | |
511 if (i < si->sn_prl_ga.ga_len | |
512 && (pp = &PRL_ITEM(si, i))->snp_count > 0) | |
513 { | |
514 fprintf(fd, "%5d ", pp->snp_count); | |
515 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self)) | |
516 fprintf(fd, " "); | |
517 else | |
518 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total)); | |
519 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self)); | |
520 } | |
521 else | |
522 fprintf(fd, " "); | |
523 fprintf(fd, "%s", IObuff); | |
524 } | |
525 fclose(sfd); | |
526 } | |
527 fprintf(fd, "\n"); | |
528 } | |
529 } | |
530 } | |
531 # endif | |
532 #endif | 398 #endif |
533 | 399 |
534 /* | 400 /* |
535 * If 'autowrite' option set, try to write the file. | 401 * If 'autowrite' option set, try to write the file. |
536 * Careful: autocommands may make "buf" invalid! | 402 * Careful: autocommands may make "buf" invalid! |
3532 | 3398 |
3533 vim_free(ga.ga_data); | 3399 vim_free(ga.ga_data); |
3534 return NULL; | 3400 return NULL; |
3535 } | 3401 } |
3536 | 3402 |
3537 #if defined(FEAT_PROFILE) || defined(PROTO) | |
3538 /* | |
3539 * Called when starting to read a script line. | |
3540 * "sourcing_lnum" must be correct! | |
3541 * When skipping lines it may not actually be executed, but we won't find out | |
3542 * until later and we need to store the time now. | |
3543 */ | |
3544 void | |
3545 script_line_start(void) | |
3546 { | |
3547 scriptitem_T *si; | |
3548 sn_prl_T *pp; | |
3549 | |
3550 if (current_sctx.sc_sid <= 0 || current_sctx.sc_sid > script_items.ga_len) | |
3551 return; | |
3552 si = &SCRIPT_ITEM(current_sctx.sc_sid); | |
3553 if (si->sn_prof_on && sourcing_lnum >= 1) | |
3554 { | |
3555 /* Grow the array before starting the timer, so that the time spent | |
3556 * here isn't counted. */ | |
3557 (void)ga_grow(&si->sn_prl_ga, | |
3558 (int)(sourcing_lnum - si->sn_prl_ga.ga_len)); | |
3559 si->sn_prl_idx = sourcing_lnum - 1; | |
3560 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx | |
3561 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen) | |
3562 { | |
3563 /* Zero counters for a line that was not used before. */ | |
3564 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len); | |
3565 pp->snp_count = 0; | |
3566 profile_zero(&pp->sn_prl_total); | |
3567 profile_zero(&pp->sn_prl_self); | |
3568 ++si->sn_prl_ga.ga_len; | |
3569 } | |
3570 si->sn_prl_execed = FALSE; | |
3571 profile_start(&si->sn_prl_start); | |
3572 profile_zero(&si->sn_prl_children); | |
3573 profile_get_wait(&si->sn_prl_wait); | |
3574 } | |
3575 } | |
3576 | |
3577 /* | |
3578 * Called when actually executing a function line. | |
3579 */ | |
3580 void | |
3581 script_line_exec(void) | |
3582 { | |
3583 scriptitem_T *si; | |
3584 | |
3585 if (current_sctx.sc_sid <= 0 || current_sctx.sc_sid > script_items.ga_len) | |
3586 return; | |
3587 si = &SCRIPT_ITEM(current_sctx.sc_sid); | |
3588 if (si->sn_prof_on && si->sn_prl_idx >= 0) | |
3589 si->sn_prl_execed = TRUE; | |
3590 } | |
3591 | |
3592 /* | |
3593 * Called when done with a script line. | |
3594 */ | |
3595 void | |
3596 script_line_end(void) | |
3597 { | |
3598 scriptitem_T *si; | |
3599 sn_prl_T *pp; | |
3600 | |
3601 if (current_sctx.sc_sid <= 0 || current_sctx.sc_sid > script_items.ga_len) | |
3602 return; | |
3603 si = &SCRIPT_ITEM(current_sctx.sc_sid); | |
3604 if (si->sn_prof_on && si->sn_prl_idx >= 0 | |
3605 && si->sn_prl_idx < si->sn_prl_ga.ga_len) | |
3606 { | |
3607 if (si->sn_prl_execed) | |
3608 { | |
3609 pp = &PRL_ITEM(si, si->sn_prl_idx); | |
3610 ++pp->snp_count; | |
3611 profile_end(&si->sn_prl_start); | |
3612 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start); | |
3613 profile_add(&pp->sn_prl_total, &si->sn_prl_start); | |
3614 profile_self(&pp->sn_prl_self, &si->sn_prl_start, | |
3615 &si->sn_prl_children); | |
3616 } | |
3617 si->sn_prl_idx = -1; | |
3618 } | |
3619 } | |
3620 #endif | |
3621 | |
3622 /* | 3403 /* |
3623 * ":scriptencoding": Set encoding conversion for a sourced script. | 3404 * ":scriptencoding": Set encoding conversion for a sourced script. |
3624 */ | 3405 */ |
3625 void | 3406 void |
3626 ex_scriptencoding(exarg_T *eap) | 3407 ex_scriptencoding(exarg_T *eap) |