blob: 4fc772d00eea1f65f50253046b40ed58143b3ec1 [file] [log] [blame]
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * profiler.c: vim script profiler
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17# if defined(FEAT_PROFILE) || defined(FEAT_RELTIME) || defined(PROTO)
18/*
19 * Store the current time in "tm".
20 */
21 void
22profile_start(proftime_T *tm)
23{
24# ifdef MSWIN
25 QueryPerformanceCounter(tm);
26# else
Ernie Rael076de792023-03-16 21:43:15 +000027 PROF_GET_TIME(tm);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020028# endif
29}
30
31/*
32 * Compute the elapsed time from "tm" till now and store in "tm".
33 */
34 void
35profile_end(proftime_T *tm)
36{
37 proftime_T now;
38
39# ifdef MSWIN
40 QueryPerformanceCounter(&now);
41 tm->QuadPart = now.QuadPart - tm->QuadPart;
42# else
Ernie Rael076de792023-03-16 21:43:15 +000043 PROF_GET_TIME(&now);
44 tm->tv_fsec = now.tv_fsec - tm->tv_fsec;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020045 tm->tv_sec = now.tv_sec - tm->tv_sec;
Ernie Rael076de792023-03-16 21:43:15 +000046 if (tm->tv_fsec < 0)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020047 {
Ernie Rael076de792023-03-16 21:43:15 +000048 tm->tv_fsec += TV_FSEC_SEC;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020049 --tm->tv_sec;
50 }
51# endif
52}
53
54/*
55 * Subtract the time "tm2" from "tm".
56 */
57 void
58profile_sub(proftime_T *tm, proftime_T *tm2)
59{
60# ifdef MSWIN
61 tm->QuadPart -= tm2->QuadPart;
62# else
Ernie Rael076de792023-03-16 21:43:15 +000063 tm->tv_fsec -= tm2->tv_fsec;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020064 tm->tv_sec -= tm2->tv_sec;
Ernie Rael076de792023-03-16 21:43:15 +000065 if (tm->tv_fsec < 0)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020066 {
Ernie Rael076de792023-03-16 21:43:15 +000067 tm->tv_fsec += TV_FSEC_SEC;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020068 --tm->tv_sec;
69 }
70# endif
71}
72
73/*
74 * Return a string that represents the time in "tm".
75 * Uses a static buffer!
76 */
77 char *
78profile_msg(proftime_T *tm)
79{
80 static char buf[50];
81
82# ifdef MSWIN
83 LARGE_INTEGER fr;
84
85 QueryPerformanceFrequency(&fr);
86 sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart);
87# else
Ernie Rael076de792023-03-16 21:43:15 +000088 sprintf(buf, PROF_TIME_FORMAT, (long)tm->tv_sec, (long)tm->tv_fsec);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020089# endif
90 return buf;
91}
92
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020093/*
94 * Return a float that represents the time in "tm".
95 */
96 float_T
97profile_float(proftime_T *tm)
98{
Bram Moolenaar73e28dc2022-09-17 21:08:33 +010099# ifdef MSWIN
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200100 LARGE_INTEGER fr;
101
102 QueryPerformanceFrequency(&fr);
103 return (float_T)tm->QuadPart / (float_T)fr.QuadPart;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100104# else
Ernie Rael076de792023-03-16 21:43:15 +0000105 return (float_T)tm->tv_sec + (float_T)tm->tv_fsec / (float_T)TV_FSEC_SEC;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200106# endif
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100107}
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200108
109/*
110 * Put the time "msec" past now in "tm".
111 */
112 void
113profile_setlimit(long msec, proftime_T *tm)
114{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200115 if (msec <= 0) // no limit
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200116 profile_zero(tm);
117 else
118 {
119# ifdef MSWIN
120 LARGE_INTEGER fr;
121
122 QueryPerformanceCounter(tm);
123 QueryPerformanceFrequency(&fr);
124 tm->QuadPart += (LONGLONG)((double)msec / 1000.0 * (double)fr.QuadPart);
125# else
Ernie Rael076de792023-03-16 21:43:15 +0000126 long fsec;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200127
Ernie Rael076de792023-03-16 21:43:15 +0000128 PROF_GET_TIME(tm);
129 fsec = (long)tm->tv_fsec + (long)msec * (TV_FSEC_SEC / 1000);
130 tm->tv_fsec = fsec % (long)TV_FSEC_SEC;
131 tm->tv_sec += fsec / (long)TV_FSEC_SEC;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200132# endif
133 }
134}
135
136/*
137 * Return TRUE if the current time is past "tm".
138 */
139 int
140profile_passed_limit(proftime_T *tm)
141{
142 proftime_T now;
143
144# ifdef MSWIN
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200145 if (tm->QuadPart == 0) // timer was not set
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200146 return FALSE;
147 QueryPerformanceCounter(&now);
148 return (now.QuadPart > tm->QuadPart);
149# else
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200150 if (tm->tv_sec == 0) // timer was not set
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200151 return FALSE;
Ernie Rael076de792023-03-16 21:43:15 +0000152 PROF_GET_TIME(&now);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200153 return (now.tv_sec > tm->tv_sec
Ernie Rael076de792023-03-16 21:43:15 +0000154 || (now.tv_sec == tm->tv_sec && now.tv_fsec > tm->tv_fsec));
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200155# endif
156}
157
158/*
159 * Set the time in "tm" to zero.
160 */
161 void
162profile_zero(proftime_T *tm)
163{
164# ifdef MSWIN
165 tm->QuadPart = 0;
166# else
Ernie Rael076de792023-03-16 21:43:15 +0000167 tm->tv_fsec = 0;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200168 tm->tv_sec = 0;
169# endif
170}
171
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200172# endif // FEAT_PROFILE || FEAT_RELTIME
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200173
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100174#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME) && defined(FEAT_PROFILE)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200175# if defined(HAVE_MATH_H)
176# include <math.h>
177# endif
178
179/*
180 * Divide the time "tm" by "count" and store in "tm2".
181 */
182 void
183profile_divide(proftime_T *tm, int count, proftime_T *tm2)
184{
185 if (count == 0)
186 profile_zero(tm2);
187 else
188 {
189# ifdef MSWIN
190 tm2->QuadPart = tm->QuadPart / count;
191# else
Ernie Rael076de792023-03-16 21:43:15 +0000192 double fsec = (tm->tv_sec * (float_T)TV_FSEC_SEC + tm->tv_fsec)
193 / count;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200194
Ernie Rael076de792023-03-16 21:43:15 +0000195 tm2->tv_sec = floor(fsec / (float_T)TV_FSEC_SEC);
196 tm2->tv_fsec = vim_round(fsec - (tm2->tv_sec * (float_T)TV_FSEC_SEC));
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200197# endif
198 }
199}
200#endif
201
202# if defined(FEAT_PROFILE) || defined(PROTO)
203/*
204 * Functions for profiling.
205 */
206static proftime_T prof_wait_time;
207
208/*
209 * Add the time "tm2" to "tm".
210 */
211 void
212profile_add(proftime_T *tm, proftime_T *tm2)
213{
214# ifdef MSWIN
215 tm->QuadPart += tm2->QuadPart;
216# else
Ernie Rael076de792023-03-16 21:43:15 +0000217 tm->tv_fsec += tm2->tv_fsec;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200218 tm->tv_sec += tm2->tv_sec;
Ernie Rael076de792023-03-16 21:43:15 +0000219 if (tm->tv_fsec >= TV_FSEC_SEC)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200220 {
Ernie Rael076de792023-03-16 21:43:15 +0000221 tm->tv_fsec -= TV_FSEC_SEC;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200222 ++tm->tv_sec;
223 }
224# endif
225}
226
227/*
228 * Add the "self" time from the total time and the children's time.
229 */
230 void
231profile_self(proftime_T *self, proftime_T *total, proftime_T *children)
232{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200233 // Check that the result won't be negative. Can happen with recursive
234 // calls.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200235#ifdef MSWIN
236 if (total->QuadPart <= children->QuadPart)
237 return;
238#else
239 if (total->tv_sec < children->tv_sec
240 || (total->tv_sec == children->tv_sec
Ernie Rael076de792023-03-16 21:43:15 +0000241 && total->tv_fsec <= children->tv_fsec))
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200242 return;
243#endif
244 profile_add(self, total);
245 profile_sub(self, children);
246}
247
248/*
249 * Get the current waittime.
250 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200251 static void
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200252profile_get_wait(proftime_T *tm)
253{
254 *tm = prof_wait_time;
255}
256
257/*
258 * Subtract the passed waittime since "tm" from "tma".
259 */
260 void
261profile_sub_wait(proftime_T *tm, proftime_T *tma)
262{
263 proftime_T tm3 = prof_wait_time;
264
265 profile_sub(&tm3, tm);
266 profile_sub(tma, &tm3);
267}
268
269/*
270 * Return TRUE if "tm1" and "tm2" are equal.
271 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200272 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200273profile_equal(proftime_T *tm1, proftime_T *tm2)
274{
275# ifdef MSWIN
276 return (tm1->QuadPart == tm2->QuadPart);
277# else
Ernie Rael076de792023-03-16 21:43:15 +0000278 return (tm1->tv_fsec == tm2->tv_fsec && tm1->tv_sec == tm2->tv_sec);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200279# endif
280}
281
282/*
283 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
284 */
285 int
286profile_cmp(const proftime_T *tm1, const proftime_T *tm2)
287{
288# ifdef MSWIN
289 return (int)(tm2->QuadPart - tm1->QuadPart);
290# else
291 if (tm1->tv_sec == tm2->tv_sec)
Ernie Rael076de792023-03-16 21:43:15 +0000292 return tm2->tv_fsec - tm1->tv_fsec;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200293 return tm2->tv_sec - tm1->tv_sec;
294# endif
295}
296
297static char_u *profile_fname = NULL;
298static proftime_T pause_time;
299
300/*
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100301 * Reset all profiling information.
302 */
303 static void
304profile_reset(void)
305{
306 int id;
307 int todo;
308 hashtab_T *functbl;
309 hashitem_T *hi;
310
311 // Reset sourced files.
312 for (id = 1; id <= script_items.ga_len; id++)
313 {
314 scriptitem_T *si = SCRIPT_ITEM(id);
315 if (si->sn_prof_on)
316 {
317 si->sn_prof_on = FALSE;
318 si->sn_pr_force = FALSE;
319 profile_zero(&si->sn_pr_child);
320 si->sn_pr_nest = 0;
321 si->sn_pr_count = 0;
322 profile_zero(&si->sn_pr_total);
323 profile_zero(&si->sn_pr_self);
324 profile_zero(&si->sn_pr_start);
325 profile_zero(&si->sn_pr_children);
326 ga_clear(&si->sn_prl_ga);
327 profile_zero(&si->sn_prl_start);
328 profile_zero(&si->sn_prl_children);
329 profile_zero(&si->sn_prl_wait);
330 si->sn_prl_idx = -1;
331 si->sn_prl_execed = 0;
332 }
333 }
334
335 // Reset functions.
336 functbl = func_tbl_get();
337 todo = (int)functbl->ht_used;
338
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000339 FOR_ALL_HASHTAB_ITEMS(functbl, hi, todo)
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100340 {
341 ufunc_T *fp;
342 int i;
343
344 if (HASHITEM_EMPTY(hi))
345 continue;
346
347 todo--;
348 fp = HI2UF(hi);
349 if (fp->uf_prof_initialized)
350 {
351 fp->uf_profiling = 0;
352 fp->uf_prof_initialized = FALSE;
353 fp->uf_tm_count = 0;
354 profile_zero(&fp->uf_tm_total);
355 profile_zero(&fp->uf_tm_self);
356 profile_zero(&fp->uf_tm_children);
357
358 for (i = 0; i < fp->uf_lines.ga_len; i++)
359 {
360 fp->uf_tml_count[i] = 0;
361 profile_zero(&fp->uf_tml_total[i]);
362 profile_zero(&fp->uf_tml_self[i]);
363 }
364
365 profile_zero(&fp->uf_tml_start);
366 profile_zero(&fp->uf_tml_children);
367 profile_zero(&fp->uf_tml_wait);
368 fp->uf_tml_idx = -1;
369 fp->uf_tml_execed = 0;
370 }
371 }
372
373 VIM_CLEAR(profile_fname);
374}
375
376/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200377 * ":profile cmd args"
378 */
379 void
380ex_profile(exarg_T *eap)
381{
382 char_u *e;
383 int len;
384
385 e = skiptowhite(eap->arg);
386 len = (int)(e - eap->arg);
387 e = skipwhite(e);
388
389 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
390 {
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100391 VIM_CLEAR(profile_fname);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200392 profile_fname = expand_env_save_opt(e, TRUE);
393 do_profiling = PROF_YES;
394 profile_zero(&prof_wait_time);
395 set_vim_var_nr(VV_PROFILING, 1L);
396 }
397 else if (do_profiling == PROF_NONE)
Bram Moolenaar677658a2022-01-05 16:09:06 +0000398 emsg(_(e_first_use_profile_start_fname));
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100399 else if (STRCMP(eap->arg, "stop") == 0)
400 {
401 profile_dump();
402 do_profiling = PROF_NONE;
403 set_vim_var_nr(VV_PROFILING, 0L);
404 profile_reset();
405 }
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200406 else if (STRCMP(eap->arg, "pause") == 0)
407 {
408 if (do_profiling == PROF_YES)
409 profile_start(&pause_time);
410 do_profiling = PROF_PAUSED;
411 }
412 else if (STRCMP(eap->arg, "continue") == 0)
413 {
414 if (do_profiling == PROF_PAUSED)
415 {
416 profile_end(&pause_time);
417 profile_add(&prof_wait_time, &pause_time);
418 }
419 do_profiling = PROF_YES;
420 }
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100421 else if (STRCMP(eap->arg, "dump") == 0)
422 profile_dump();
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200423 else
424 {
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200425 // The rest is similar to ":breakadd".
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200426 ex_breakadd(eap);
427 }
428}
429
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200430// Command line expansion for :profile.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200431static enum
432{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200433 PEXP_SUBCMD, // expand :profile sub-commands
434 PEXP_FUNC // expand :profile func {funcname}
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200435} pexpand_what;
436
437static char *pexpand_cmds[] = {
438 "start",
439#define PROFCMD_START 0
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100440 "stop",
441#define PROFCMD_STOP 1
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200442 "pause",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100443#define PROFCMD_PAUSE 2
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200444 "continue",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100445#define PROFCMD_CONTINUE 3
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200446 "func",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100447#define PROFCMD_FUNC 4
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200448 "file",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100449#define PROFCMD_DUMP 5
450 "dump",
451#define PROFCMD_FILE 6
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200452 NULL
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100453#define PROFCMD_LAST 7
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200454};
455
456/*
457 * Function given to ExpandGeneric() to obtain the profile command
458 * specific expansion.
459 */
460 char_u *
461get_profile_name(expand_T *xp UNUSED, int idx)
462{
463 switch (pexpand_what)
464 {
465 case PEXP_SUBCMD:
466 return (char_u *)pexpand_cmds[idx];
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200467 default:
468 return NULL;
469 }
470}
471
472/*
473 * Handle command line completion for :profile command.
474 */
475 void
476set_context_in_profile_cmd(expand_T *xp, char_u *arg)
477{
478 char_u *end_subcmd;
479
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200480 // Default: expand subcommands.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200481 xp->xp_context = EXPAND_PROFILE;
482 pexpand_what = PEXP_SUBCMD;
483 xp->xp_pattern = arg;
484
485 end_subcmd = skiptowhite(arg);
486 if (*end_subcmd == NUL)
487 return;
488
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000489 if ((end_subcmd - arg == 5 && STRNCMP(arg, "start", 5) == 0)
490 || (end_subcmd - arg == 4 && STRNCMP(arg, "file", 4) == 0))
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200491 {
492 xp->xp_context = EXPAND_FILES;
493 xp->xp_pattern = skipwhite(end_subcmd);
494 return;
495 }
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000496 else if (end_subcmd - arg == 4 && STRNCMP(arg, "func", 4) == 0)
497 {
498 xp->xp_context = EXPAND_USER_FUNC;
499 xp->xp_pattern = skipwhite(end_subcmd);
500 return;
501 }
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200502
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200503 xp->xp_context = EXPAND_NOTHING;
504}
505
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200506static proftime_T inchar_time;
507
508/*
509 * Called when starting to wait for the user to type a character.
510 */
511 void
512prof_inchar_enter(void)
513{
514 profile_start(&inchar_time);
515}
516
517/*
518 * Called when finished waiting for the user to type a character.
519 */
520 void
521prof_inchar_exit(void)
522{
523 profile_end(&inchar_time);
524 profile_add(&prof_wait_time, &inchar_time);
525}
526
527
528/*
529 * Return TRUE when a function defined in the current script should be
530 * profiled.
531 */
532 int
533prof_def_func(void)
534{
535 if (current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100536 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_pr_force;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200537 return FALSE;
538}
539
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200540/*
541 * Print the count and times for one function or function line.
542 */
543 static void
544prof_func_line(
545 FILE *fd,
546 int count,
547 proftime_T *total,
548 proftime_T *self,
549 int prefer_self) // when equal print only self time
550{
551 if (count > 0)
552 {
553 fprintf(fd, "%5d ", count);
554 if (prefer_self && profile_equal(total, self))
Ernie Rael076de792023-03-16 21:43:15 +0000555 fprintf(fd, PROF_TIME_BLANK);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200556 else
557 fprintf(fd, "%s ", profile_msg(total));
558 if (!prefer_self && profile_equal(total, self))
Ernie Rael076de792023-03-16 21:43:15 +0000559 fprintf(fd, PROF_TIME_BLANK);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200560 else
561 fprintf(fd, "%s ", profile_msg(self));
562 }
563 else
Ernie Rael076de792023-03-16 21:43:15 +0000564 fprintf(fd, " %s%s", PROF_TIME_BLANK, PROF_TIME_BLANK);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200565}
566
567 static void
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200568prof_sort_list(
569 FILE *fd,
570 ufunc_T **sorttab,
571 int st_len,
572 char *title,
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200573 int prefer_self) // when equal print only self time
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200574{
575 int i;
576 ufunc_T *fp;
577
578 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
Ernie Rael076de792023-03-16 21:43:15 +0000579 fprintf(fd, "%s function\n", PROF_TOTALS_HEADER);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200580 for (i = 0; i < 20 && i < st_len; ++i)
581 {
582 fp = sorttab[i];
583 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
584 prefer_self);
585 if (fp->uf_name[0] == K_SPECIAL)
586 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
587 else
588 fprintf(fd, " %s()\n", fp->uf_name);
589 }
590 fprintf(fd, "\n");
591}
592
593/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200594 * Compare function for total time sorting.
595 */
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200596 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200597prof_total_cmp(const void *s1, const void *s2)
598{
599 ufunc_T *p1, *p2;
600
601 p1 = *(ufunc_T **)s1;
602 p2 = *(ufunc_T **)s2;
603 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
604}
605
606/*
607 * Compare function for self time sorting.
608 */
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200609 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200610prof_self_cmp(const void *s1, const void *s2)
611{
612 ufunc_T *p1, *p2;
613
614 p1 = *(ufunc_T **)s1;
615 p2 = *(ufunc_T **)s2;
616 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
617}
618
619/*
620 * Start profiling function "fp".
621 */
622 void
623func_do_profile(ufunc_T *fp)
624{
625 int len = fp->uf_lines.ga_len;
626
627 if (!fp->uf_prof_initialized)
628 {
629 if (len == 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200630 len = 1; // avoid getting error for allocating zero bytes
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200631 fp->uf_tm_count = 0;
632 profile_zero(&fp->uf_tm_self);
633 profile_zero(&fp->uf_tm_total);
634 if (fp->uf_tml_count == NULL)
635 fp->uf_tml_count = ALLOC_CLEAR_MULT(int, len);
636 if (fp->uf_tml_total == NULL)
637 fp->uf_tml_total = ALLOC_CLEAR_MULT(proftime_T, len);
638 if (fp->uf_tml_self == NULL)
639 fp->uf_tml_self = ALLOC_CLEAR_MULT(proftime_T, len);
640 fp->uf_tml_idx = -1;
641 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
642 || fp->uf_tml_self == NULL)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200643 return; // out of memory
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200644 fp->uf_prof_initialized = TRUE;
645 }
646
647 fp->uf_profiling = TRUE;
648}
649
650/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200651 * Save time when starting to invoke another script or function.
652 */
653 static void
654script_prof_save(
655 proftime_T *tm) // place to store wait time
656{
657 scriptitem_T *si;
658
659 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
660 {
661 si = SCRIPT_ITEM(current_sctx.sc_sid);
662 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
663 profile_start(&si->sn_pr_child);
664 }
665 profile_get_wait(tm);
666}
667
668/*
Bram Moolenaarb2049902021-01-24 12:53:53 +0100669 * When calling a function: may initialize for profiling.
670 */
671 void
Bram Moolenaar12d26532021-02-19 19:13:21 +0100672profile_may_start_func(profinfo_T *info, ufunc_T *fp, ufunc_T *caller)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100673{
Bram Moolenaar12d26532021-02-19 19:13:21 +0100674 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarb2049902021-01-24 12:53:53 +0100675 {
Bram Moolenaar12d26532021-02-19 19:13:21 +0100676 info->pi_started_profiling = TRUE;
677 func_do_profile(fp);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100678 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100679 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
680 {
681 ++fp->uf_tm_count;
682 profile_start(&info->pi_call_start);
683 profile_zero(&fp->uf_tm_children);
684 }
685 script_prof_save(&info->pi_wait_start);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100686}
687
688/*
689 * After calling a function: may handle profiling. profile_may_start_func()
690 * must have been called previously.
691 */
692 void
Bram Moolenaar12d26532021-02-19 19:13:21 +0100693profile_may_end_func(profinfo_T *info, ufunc_T *fp, ufunc_T *caller)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100694{
695 profile_end(&info->pi_call_start);
696 profile_sub_wait(&info->pi_wait_start, &info->pi_call_start);
697 profile_add(&fp->uf_tm_total, &info->pi_call_start);
698 profile_self(&fp->uf_tm_self, &info->pi_call_start, &fp->uf_tm_children);
Bram Moolenaar12d26532021-02-19 19:13:21 +0100699 if (caller != NULL && caller->uf_profiling)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100700 {
Bram Moolenaar12d26532021-02-19 19:13:21 +0100701 profile_add(&caller->uf_tm_children, &info->pi_call_start);
702 profile_add(&caller->uf_tml_children, &info->pi_call_start);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100703 }
704 if (info->pi_started_profiling)
705 // make a ":profdel func" stop profiling the function
706 fp->uf_profiling = FALSE;
707}
708
709/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200710 * Prepare profiling for entering a child or something else that is not
711 * counted for the script/function itself.
712 * Should always be called in pair with prof_child_exit().
713 */
714 void
715prof_child_enter(
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200716 proftime_T *tm) // place to store waittime
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200717{
718 funccall_T *fc = get_current_funccal();
719
Bram Moolenaarca16c602022-09-06 18:57:08 +0100720 if (fc != NULL && fc->fc_func->uf_profiling)
721 profile_start(&fc->fc_prof_child);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200722 script_prof_save(tm);
723}
724
725/*
726 * Take care of time spent in a child.
727 * Should always be called after prof_child_enter().
728 */
729 void
730prof_child_exit(
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200731 proftime_T *tm) // where waittime was stored
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200732{
733 funccall_T *fc = get_current_funccal();
734
Bram Moolenaarca16c602022-09-06 18:57:08 +0100735 if (fc != NULL && fc->fc_func->uf_profiling)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200736 {
Bram Moolenaarca16c602022-09-06 18:57:08 +0100737 profile_end(&fc->fc_prof_child);
738 profile_sub_wait(tm, &fc->fc_prof_child); // don't count waiting time
739 profile_add(&fc->fc_func->uf_tm_children, &fc->fc_prof_child);
740 profile_add(&fc->fc_func->uf_tml_children, &fc->fc_prof_child);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200741 }
742 script_prof_restore(tm);
743}
744
745/*
746 * Called when starting to read a function line.
747 * "sourcing_lnum" must be correct!
748 * When skipping lines it may not actually be executed, but we won't find out
749 * until later and we need to store the time now.
750 */
751 void
Bram Moolenaarb2049902021-01-24 12:53:53 +0100752func_line_start(void *cookie, long lnum)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200753{
754 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100755 ufunc_T *fp = fcp->fc_func;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200756
Bram Moolenaarb2049902021-01-24 12:53:53 +0100757 if (fp->uf_profiling && lnum >= 1 && lnum <= fp->uf_lines.ga_len)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200758 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100759 fp->uf_tml_idx = lnum - 1;
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200760 // Skip continuation lines.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200761 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
762 --fp->uf_tml_idx;
763 fp->uf_tml_execed = FALSE;
764 profile_start(&fp->uf_tml_start);
765 profile_zero(&fp->uf_tml_children);
766 profile_get_wait(&fp->uf_tml_wait);
767 }
768}
769
770/*
771 * Called when actually executing a function line.
772 */
773 void
774func_line_exec(void *cookie)
775{
776 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100777 ufunc_T *fp = fcp->fc_func;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200778
779 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
780 fp->uf_tml_execed = TRUE;
781}
782
783/*
784 * Called when done with a function line.
785 */
786 void
787func_line_end(void *cookie)
788{
789 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100790 ufunc_T *fp = fcp->fc_func;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200791
792 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
793 {
794 if (fp->uf_tml_execed)
795 {
796 ++fp->uf_tml_count[fp->uf_tml_idx];
797 profile_end(&fp->uf_tml_start);
798 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
799 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
800 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
801 &fp->uf_tml_children);
802 }
803 fp->uf_tml_idx = -1;
804 }
805}
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200806
807/*
808 * Dump the profiling results for all functions in file "fd".
809 */
810 static void
811func_dump_profile(FILE *fd)
812{
813 hashtab_T *functbl;
814 hashitem_T *hi;
815 int todo;
816 ufunc_T *fp;
817 int i;
818 ufunc_T **sorttab;
819 int st_len = 0;
820 char_u *p;
821
822 functbl = func_tbl_get();
823 todo = (int)functbl->ht_used;
824 if (todo == 0)
825 return; // nothing to dump
826
827 sorttab = ALLOC_MULT(ufunc_T *, todo);
828
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000829 FOR_ALL_HASHTAB_ITEMS(functbl, hi, todo)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200830 {
831 if (!HASHITEM_EMPTY(hi))
832 {
833 --todo;
834 fp = HI2UF(hi);
835 if (fp->uf_prof_initialized)
836 {
837 if (sorttab != NULL)
838 sorttab[st_len++] = fp;
839
840 if (fp->uf_name[0] == K_SPECIAL)
841 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
842 else
843 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
Bram Moolenaar16358802019-08-30 18:37:26 +0200844 if (fp->uf_script_ctx.sc_sid > 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200845 {
Bram Moolenaar16358802019-08-30 18:37:26 +0200846 p = home_replace_save(NULL,
847 get_scriptname(fp->uf_script_ctx.sc_sid));
848 if (p != NULL)
849 {
Bram Moolenaar181d4f52019-09-18 22:04:56 +0200850 fprintf(fd, " Defined: %s:%ld\n",
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200851 p, (long)fp->uf_script_ctx.sc_lnum);
Bram Moolenaar16358802019-08-30 18:37:26 +0200852 vim_free(p);
853 }
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200854 }
855 if (fp->uf_tm_count == 1)
856 fprintf(fd, "Called 1 time\n");
857 else
858 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
859 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
860 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
861 fprintf(fd, "\n");
Ernie Rael076de792023-03-16 21:43:15 +0000862 fprintf(fd, "%s\n", PROF_TOTALS_HEADER);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200863
864 for (i = 0; i < fp->uf_lines.ga_len; ++i)
865 {
866 if (FUNCLINE(fp, i) == NULL)
867 continue;
868 prof_func_line(fd, fp->uf_tml_count[i],
869 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
870 fprintf(fd, "%s\n", FUNCLINE(fp, i));
871 }
872 fprintf(fd, "\n");
873 }
874 }
875 }
876
877 if (sorttab != NULL && st_len > 0)
878 {
879 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
880 prof_total_cmp);
881 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
882 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
883 prof_self_cmp);
884 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
885 }
886
887 vim_free(sorttab);
888}
889
890/*
891 * Start profiling script "fp".
892 */
893 void
894script_do_profile(scriptitem_T *si)
895{
896 si->sn_pr_count = 0;
897 profile_zero(&si->sn_pr_total);
898 profile_zero(&si->sn_pr_self);
899
900 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
901 si->sn_prl_idx = -1;
902 si->sn_prof_on = TRUE;
903 si->sn_pr_nest = 0;
904}
905
906/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200907 * Count time spent in children after invoking another script or function.
908 */
909 void
910script_prof_restore(proftime_T *tm)
911{
912 scriptitem_T *si;
913
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000914 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
915 return;
916
917 si = SCRIPT_ITEM(current_sctx.sc_sid);
918 if (si->sn_prof_on && --si->sn_pr_nest == 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200919 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000920 profile_end(&si->sn_pr_child);
921 profile_sub_wait(tm, &si->sn_pr_child); // don't count wait time
922 profile_add(&si->sn_pr_children, &si->sn_pr_child);
923 profile_add(&si->sn_prl_children, &si->sn_pr_child);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200924 }
925}
926
927/*
928 * Dump the profiling results for all scripts in file "fd".
929 */
930 static void
931script_dump_profile(FILE *fd)
932{
933 int id;
934 scriptitem_T *si;
935 int i;
936 FILE *sfd;
937 sn_prl_T *pp;
938
939 for (id = 1; id <= script_items.ga_len; ++id)
940 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100941 si = SCRIPT_ITEM(id);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200942 if (si->sn_prof_on)
943 {
944 fprintf(fd, "SCRIPT %s\n", si->sn_name);
945 if (si->sn_pr_count == 1)
946 fprintf(fd, "Sourced 1 time\n");
947 else
948 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
949 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
950 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
951 fprintf(fd, "\n");
Ernie Rael076de792023-03-16 21:43:15 +0000952 fprintf(fd, "%s\n", PROF_TOTALS_HEADER);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200953
954 sfd = mch_fopen((char *)si->sn_name, "r");
955 if (sfd == NULL)
956 fprintf(fd, "Cannot open file!\n");
957 else
958 {
959 // Keep going till the end of file, so that trailing
960 // continuation lines are listed.
961 for (i = 0; ; ++i)
962 {
963 if (vim_fgets(IObuff, IOSIZE, sfd))
964 break;
965 // When a line has been truncated, append NL, taking care
966 // of multi-byte characters .
967 if (IObuff[IOSIZE - 2] != NUL && IObuff[IOSIZE - 2] != NL)
968 {
969 int n = IOSIZE - 2;
970
971 if (enc_utf8)
972 {
973 // Move to the first byte of this char.
974 // utf_head_off() doesn't work, because it checks
975 // for a truncated character.
976 while (n > 0 && (IObuff[n] & 0xc0) == 0x80)
977 --n;
978 }
979 else if (has_mbyte)
980 n -= mb_head_off(IObuff, IObuff + n);
981 IObuff[n] = NL;
982 IObuff[n + 1] = NUL;
983 }
984 if (i < si->sn_prl_ga.ga_len
985 && (pp = &PRL_ITEM(si, i))->snp_count > 0)
986 {
987 fprintf(fd, "%5d ", pp->snp_count);
988 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
989 fprintf(fd, " ");
990 else
991 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
992 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
993 }
994 else
995 fprintf(fd, " ");
996 fprintf(fd, "%s", IObuff);
997 }
998 fclose(sfd);
999 }
1000 fprintf(fd, "\n");
1001 }
1002 }
1003}
1004
1005/*
1006 * Dump the profiling info.
1007 */
1008 void
1009profile_dump(void)
1010{
1011 FILE *fd;
1012
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001013 if (profile_fname == NULL)
1014 return;
1015
1016 fd = mch_fopen((char *)profile_fname, "w");
1017 if (fd == NULL)
1018 semsg(_(e_cant_open_file_str), profile_fname);
1019 else
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001020 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001021 script_dump_profile(fd);
1022 func_dump_profile(fd);
1023 fclose(fd);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001024 }
1025}
1026
1027/*
1028 * Called when starting to read a script line.
1029 * "sourcing_lnum" must be correct!
1030 * When skipping lines it may not actually be executed, but we won't find out
1031 * until later and we need to store the time now.
1032 */
1033 void
1034script_line_start(void)
1035{
1036 scriptitem_T *si;
1037 sn_prl_T *pp;
1038
Bram Moolenaare3d46852020-08-29 13:39:17 +02001039 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001040 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001041 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001042 if (si->sn_prof_on && SOURCING_LNUM >= 1)
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001043 {
1044 // Grow the array before starting the timer, so that the time spent
1045 // here isn't counted.
1046 (void)ga_grow(&si->sn_prl_ga,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001047 (int)(SOURCING_LNUM - si->sn_prl_ga.ga_len));
1048 si->sn_prl_idx = SOURCING_LNUM - 1;
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001049 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
1050 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
1051 {
1052 // Zero counters for a line that was not used before.
1053 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
1054 pp->snp_count = 0;
1055 profile_zero(&pp->sn_prl_total);
1056 profile_zero(&pp->sn_prl_self);
1057 ++si->sn_prl_ga.ga_len;
1058 }
1059 si->sn_prl_execed = FALSE;
1060 profile_start(&si->sn_prl_start);
1061 profile_zero(&si->sn_prl_children);
1062 profile_get_wait(&si->sn_prl_wait);
1063 }
1064}
1065
1066/*
1067 * Called when actually executing a function line.
1068 */
1069 void
1070script_line_exec(void)
1071{
1072 scriptitem_T *si;
1073
Bram Moolenaare3d46852020-08-29 13:39:17 +02001074 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001075 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001076 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001077 if (si->sn_prof_on && si->sn_prl_idx >= 0)
1078 si->sn_prl_execed = TRUE;
1079}
1080
1081/*
1082 * Called when done with a script line.
1083 */
1084 void
1085script_line_end(void)
1086{
1087 scriptitem_T *si;
1088 sn_prl_T *pp;
1089
Bram Moolenaare3d46852020-08-29 13:39:17 +02001090 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001091 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001092 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001093 if (si->sn_prof_on && si->sn_prl_idx >= 0
1094 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
1095 {
1096 if (si->sn_prl_execed)
1097 {
1098 pp = &PRL_ITEM(si, si->sn_prl_idx);
1099 ++pp->snp_count;
1100 profile_end(&si->sn_prl_start);
1101 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
1102 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
1103 profile_self(&pp->sn_prl_self, &si->sn_prl_start,
1104 &si->sn_prl_children);
1105 }
1106 si->sn_prl_idx = -1;
1107 }
1108}
1109# endif // FEAT_PROFILE
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001110
1111#endif