blob: f1b9a40699d4152f13793c0cf00dd4994590b8b1 [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
27 gettimeofday(tm, NULL);
28# 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
43 gettimeofday(&now, NULL);
44 tm->tv_usec = now.tv_usec - tm->tv_usec;
45 tm->tv_sec = now.tv_sec - tm->tv_sec;
46 if (tm->tv_usec < 0)
47 {
48 tm->tv_usec += 1000000;
49 --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
63 tm->tv_usec -= tm2->tv_usec;
64 tm->tv_sec -= tm2->tv_sec;
65 if (tm->tv_usec < 0)
66 {
67 tm->tv_usec += 1000000;
68 --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
88 sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec);
89# endif
90 return buf;
91}
92
93# if defined(FEAT_FLOAT) || defined(PROTO)
94/*
95 * Return a float that represents the time in "tm".
96 */
97 float_T
98profile_float(proftime_T *tm)
99{
100# ifdef MSWIN
101 LARGE_INTEGER fr;
102
103 QueryPerformanceFrequency(&fr);
104 return (float_T)tm->QuadPart / (float_T)fr.QuadPart;
105# else
106 return (float_T)tm->tv_sec + (float_T)tm->tv_usec / 1000000.0;
107# endif
108}
109# endif
110
111/*
112 * Put the time "msec" past now in "tm".
113 */
114 void
115profile_setlimit(long msec, proftime_T *tm)
116{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200117 if (msec <= 0) // no limit
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200118 profile_zero(tm);
119 else
120 {
121# ifdef MSWIN
122 LARGE_INTEGER fr;
123
124 QueryPerformanceCounter(tm);
125 QueryPerformanceFrequency(&fr);
126 tm->QuadPart += (LONGLONG)((double)msec / 1000.0 * (double)fr.QuadPart);
127# else
128 long usec;
129
130 gettimeofday(tm, NULL);
131 usec = (long)tm->tv_usec + (long)msec * 1000;
132 tm->tv_usec = usec % 1000000L;
133 tm->tv_sec += usec / 1000000L;
134# endif
135 }
136}
137
138/*
139 * Return TRUE if the current time is past "tm".
140 */
141 int
142profile_passed_limit(proftime_T *tm)
143{
144 proftime_T now;
145
146# ifdef MSWIN
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200147 if (tm->QuadPart == 0) // timer was not set
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200148 return FALSE;
149 QueryPerformanceCounter(&now);
150 return (now.QuadPart > tm->QuadPart);
151# else
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200152 if (tm->tv_sec == 0) // timer was not set
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200153 return FALSE;
154 gettimeofday(&now, NULL);
155 return (now.tv_sec > tm->tv_sec
156 || (now.tv_sec == tm->tv_sec && now.tv_usec > tm->tv_usec));
157# endif
158}
159
160/*
161 * Set the time in "tm" to zero.
162 */
163 void
164profile_zero(proftime_T *tm)
165{
166# ifdef MSWIN
167 tm->QuadPart = 0;
168# else
169 tm->tv_usec = 0;
170 tm->tv_sec = 0;
171# endif
172}
173
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200174# endif // FEAT_PROFILE || FEAT_RELTIME
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200175
176#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME) && defined(FEAT_FLOAT) && defined(FEAT_PROFILE)
177# if defined(HAVE_MATH_H)
178# include <math.h>
179# endif
180
181/*
182 * Divide the time "tm" by "count" and store in "tm2".
183 */
184 void
185profile_divide(proftime_T *tm, int count, proftime_T *tm2)
186{
187 if (count == 0)
188 profile_zero(tm2);
189 else
190 {
191# ifdef MSWIN
192 tm2->QuadPart = tm->QuadPart / count;
193# else
194 double usec = (tm->tv_sec * 1000000.0 + tm->tv_usec) / count;
195
196 tm2->tv_sec = floor(usec / 1000000.0);
197 tm2->tv_usec = vim_round(usec - (tm2->tv_sec * 1000000.0));
198# endif
199 }
200}
201#endif
202
203# if defined(FEAT_PROFILE) || defined(PROTO)
204/*
205 * Functions for profiling.
206 */
207static proftime_T prof_wait_time;
208
209/*
210 * Add the time "tm2" to "tm".
211 */
212 void
213profile_add(proftime_T *tm, proftime_T *tm2)
214{
215# ifdef MSWIN
216 tm->QuadPart += tm2->QuadPart;
217# else
218 tm->tv_usec += tm2->tv_usec;
219 tm->tv_sec += tm2->tv_sec;
220 if (tm->tv_usec >= 1000000)
221 {
222 tm->tv_usec -= 1000000;
223 ++tm->tv_sec;
224 }
225# endif
226}
227
228/*
229 * Add the "self" time from the total time and the children's time.
230 */
231 void
232profile_self(proftime_T *self, proftime_T *total, proftime_T *children)
233{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200234 // Check that the result won't be negative. Can happen with recursive
235 // calls.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200236#ifdef MSWIN
237 if (total->QuadPart <= children->QuadPart)
238 return;
239#else
240 if (total->tv_sec < children->tv_sec
241 || (total->tv_sec == children->tv_sec
242 && total->tv_usec <= children->tv_usec))
243 return;
244#endif
245 profile_add(self, total);
246 profile_sub(self, children);
247}
248
249/*
250 * Get the current waittime.
251 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200252 static void
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200253profile_get_wait(proftime_T *tm)
254{
255 *tm = prof_wait_time;
256}
257
258/*
259 * Subtract the passed waittime since "tm" from "tma".
260 */
261 void
262profile_sub_wait(proftime_T *tm, proftime_T *tma)
263{
264 proftime_T tm3 = prof_wait_time;
265
266 profile_sub(&tm3, tm);
267 profile_sub(tma, &tm3);
268}
269
270/*
271 * Return TRUE if "tm1" and "tm2" are equal.
272 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200273 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200274profile_equal(proftime_T *tm1, proftime_T *tm2)
275{
276# ifdef MSWIN
277 return (tm1->QuadPart == tm2->QuadPart);
278# else
279 return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec);
280# endif
281}
282
283/*
284 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
285 */
286 int
287profile_cmp(const proftime_T *tm1, const proftime_T *tm2)
288{
289# ifdef MSWIN
290 return (int)(tm2->QuadPart - tm1->QuadPart);
291# else
292 if (tm1->tv_sec == tm2->tv_sec)
293 return tm2->tv_usec - tm1->tv_usec;
294 return tm2->tv_sec - tm1->tv_sec;
295# endif
296}
297
298static char_u *profile_fname = NULL;
299static proftime_T pause_time;
300
301/*
302 * ":profile cmd args"
303 */
304 void
305ex_profile(exarg_T *eap)
306{
307 char_u *e;
308 int len;
309
310 e = skiptowhite(eap->arg);
311 len = (int)(e - eap->arg);
312 e = skipwhite(e);
313
314 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
315 {
316 vim_free(profile_fname);
317 profile_fname = expand_env_save_opt(e, TRUE);
318 do_profiling = PROF_YES;
319 profile_zero(&prof_wait_time);
320 set_vim_var_nr(VV_PROFILING, 1L);
321 }
322 else if (do_profiling == PROF_NONE)
Bram Moolenaar677658a2022-01-05 16:09:06 +0000323 emsg(_(e_first_use_profile_start_fname));
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200324 else if (STRCMP(eap->arg, "pause") == 0)
325 {
326 if (do_profiling == PROF_YES)
327 profile_start(&pause_time);
328 do_profiling = PROF_PAUSED;
329 }
330 else if (STRCMP(eap->arg, "continue") == 0)
331 {
332 if (do_profiling == PROF_PAUSED)
333 {
334 profile_end(&pause_time);
335 profile_add(&prof_wait_time, &pause_time);
336 }
337 do_profiling = PROF_YES;
338 }
339 else
340 {
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200341 // The rest is similar to ":breakadd".
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200342 ex_breakadd(eap);
343 }
344}
345
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200346// Command line expansion for :profile.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200347static enum
348{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200349 PEXP_SUBCMD, // expand :profile sub-commands
350 PEXP_FUNC // expand :profile func {funcname}
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200351} pexpand_what;
352
353static char *pexpand_cmds[] = {
354 "start",
355#define PROFCMD_START 0
356 "pause",
357#define PROFCMD_PAUSE 1
358 "continue",
359#define PROFCMD_CONTINUE 2
360 "func",
361#define PROFCMD_FUNC 3
362 "file",
363#define PROFCMD_FILE 4
364 NULL
365#define PROFCMD_LAST 5
366};
367
368/*
369 * Function given to ExpandGeneric() to obtain the profile command
370 * specific expansion.
371 */
372 char_u *
373get_profile_name(expand_T *xp UNUSED, int idx)
374{
375 switch (pexpand_what)
376 {
377 case PEXP_SUBCMD:
378 return (char_u *)pexpand_cmds[idx];
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200379 default:
380 return NULL;
381 }
382}
383
384/*
385 * Handle command line completion for :profile command.
386 */
387 void
388set_context_in_profile_cmd(expand_T *xp, char_u *arg)
389{
390 char_u *end_subcmd;
391
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200392 // Default: expand subcommands.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200393 xp->xp_context = EXPAND_PROFILE;
394 pexpand_what = PEXP_SUBCMD;
395 xp->xp_pattern = arg;
396
397 end_subcmd = skiptowhite(arg);
398 if (*end_subcmd == NUL)
399 return;
400
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000401 if ((end_subcmd - arg == 5 && STRNCMP(arg, "start", 5) == 0)
402 || (end_subcmd - arg == 4 && STRNCMP(arg, "file", 4) == 0))
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200403 {
404 xp->xp_context = EXPAND_FILES;
405 xp->xp_pattern = skipwhite(end_subcmd);
406 return;
407 }
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000408 else if (end_subcmd - arg == 4 && STRNCMP(arg, "func", 4) == 0)
409 {
410 xp->xp_context = EXPAND_USER_FUNC;
411 xp->xp_pattern = skipwhite(end_subcmd);
412 return;
413 }
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200414
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200415 xp->xp_context = EXPAND_NOTHING;
416}
417
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200418static proftime_T inchar_time;
419
420/*
421 * Called when starting to wait for the user to type a character.
422 */
423 void
424prof_inchar_enter(void)
425{
426 profile_start(&inchar_time);
427}
428
429/*
430 * Called when finished waiting for the user to type a character.
431 */
432 void
433prof_inchar_exit(void)
434{
435 profile_end(&inchar_time);
436 profile_add(&prof_wait_time, &inchar_time);
437}
438
439
440/*
441 * Return TRUE when a function defined in the current script should be
442 * profiled.
443 */
444 int
445prof_def_func(void)
446{
447 if (current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100448 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_pr_force;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200449 return FALSE;
450}
451
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200452/*
453 * Print the count and times for one function or function line.
454 */
455 static void
456prof_func_line(
457 FILE *fd,
458 int count,
459 proftime_T *total,
460 proftime_T *self,
461 int prefer_self) // when equal print only self time
462{
463 if (count > 0)
464 {
465 fprintf(fd, "%5d ", count);
466 if (prefer_self && profile_equal(total, self))
467 fprintf(fd, " ");
468 else
469 fprintf(fd, "%s ", profile_msg(total));
470 if (!prefer_self && profile_equal(total, self))
471 fprintf(fd, " ");
472 else
473 fprintf(fd, "%s ", profile_msg(self));
474 }
475 else
476 fprintf(fd, " ");
477}
478
479 static void
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200480prof_sort_list(
481 FILE *fd,
482 ufunc_T **sorttab,
483 int st_len,
484 char *title,
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200485 int prefer_self) // when equal print only self time
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200486{
487 int i;
488 ufunc_T *fp;
489
490 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
491 fprintf(fd, "count total (s) self (s) function\n");
492 for (i = 0; i < 20 && i < st_len; ++i)
493 {
494 fp = sorttab[i];
495 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
496 prefer_self);
497 if (fp->uf_name[0] == K_SPECIAL)
498 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
499 else
500 fprintf(fd, " %s()\n", fp->uf_name);
501 }
502 fprintf(fd, "\n");
503}
504
505/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200506 * Compare function for total time sorting.
507 */
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200508 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200509prof_total_cmp(const void *s1, const void *s2)
510{
511 ufunc_T *p1, *p2;
512
513 p1 = *(ufunc_T **)s1;
514 p2 = *(ufunc_T **)s2;
515 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
516}
517
518/*
519 * Compare function for self time sorting.
520 */
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200521 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200522prof_self_cmp(const void *s1, const void *s2)
523{
524 ufunc_T *p1, *p2;
525
526 p1 = *(ufunc_T **)s1;
527 p2 = *(ufunc_T **)s2;
528 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
529}
530
531/*
532 * Start profiling function "fp".
533 */
534 void
535func_do_profile(ufunc_T *fp)
536{
537 int len = fp->uf_lines.ga_len;
538
539 if (!fp->uf_prof_initialized)
540 {
541 if (len == 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200542 len = 1; // avoid getting error for allocating zero bytes
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200543 fp->uf_tm_count = 0;
544 profile_zero(&fp->uf_tm_self);
545 profile_zero(&fp->uf_tm_total);
546 if (fp->uf_tml_count == NULL)
547 fp->uf_tml_count = ALLOC_CLEAR_MULT(int, len);
548 if (fp->uf_tml_total == NULL)
549 fp->uf_tml_total = ALLOC_CLEAR_MULT(proftime_T, len);
550 if (fp->uf_tml_self == NULL)
551 fp->uf_tml_self = ALLOC_CLEAR_MULT(proftime_T, len);
552 fp->uf_tml_idx = -1;
553 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
554 || fp->uf_tml_self == NULL)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200555 return; // out of memory
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200556 fp->uf_prof_initialized = TRUE;
557 }
558
559 fp->uf_profiling = TRUE;
560}
561
562/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200563 * Save time when starting to invoke another script or function.
564 */
565 static void
566script_prof_save(
567 proftime_T *tm) // place to store wait time
568{
569 scriptitem_T *si;
570
571 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
572 {
573 si = SCRIPT_ITEM(current_sctx.sc_sid);
574 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
575 profile_start(&si->sn_pr_child);
576 }
577 profile_get_wait(tm);
578}
579
580/*
Bram Moolenaarb2049902021-01-24 12:53:53 +0100581 * When calling a function: may initialize for profiling.
582 */
583 void
Bram Moolenaar12d26532021-02-19 19:13:21 +0100584profile_may_start_func(profinfo_T *info, ufunc_T *fp, ufunc_T *caller)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100585{
Bram Moolenaar12d26532021-02-19 19:13:21 +0100586 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarb2049902021-01-24 12:53:53 +0100587 {
Bram Moolenaar12d26532021-02-19 19:13:21 +0100588 info->pi_started_profiling = TRUE;
589 func_do_profile(fp);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100590 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100591 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
592 {
593 ++fp->uf_tm_count;
594 profile_start(&info->pi_call_start);
595 profile_zero(&fp->uf_tm_children);
596 }
597 script_prof_save(&info->pi_wait_start);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100598}
599
600/*
601 * After calling a function: may handle profiling. profile_may_start_func()
602 * must have been called previously.
603 */
604 void
Bram Moolenaar12d26532021-02-19 19:13:21 +0100605profile_may_end_func(profinfo_T *info, ufunc_T *fp, ufunc_T *caller)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100606{
607 profile_end(&info->pi_call_start);
608 profile_sub_wait(&info->pi_wait_start, &info->pi_call_start);
609 profile_add(&fp->uf_tm_total, &info->pi_call_start);
610 profile_self(&fp->uf_tm_self, &info->pi_call_start, &fp->uf_tm_children);
Bram Moolenaar12d26532021-02-19 19:13:21 +0100611 if (caller != NULL && caller->uf_profiling)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100612 {
Bram Moolenaar12d26532021-02-19 19:13:21 +0100613 profile_add(&caller->uf_tm_children, &info->pi_call_start);
614 profile_add(&caller->uf_tml_children, &info->pi_call_start);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100615 }
616 if (info->pi_started_profiling)
617 // make a ":profdel func" stop profiling the function
618 fp->uf_profiling = FALSE;
619}
620
621/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200622 * Prepare profiling for entering a child or something else that is not
623 * counted for the script/function itself.
624 * Should always be called in pair with prof_child_exit().
625 */
626 void
627prof_child_enter(
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200628 proftime_T *tm) // place to store waittime
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200629{
630 funccall_T *fc = get_current_funccal();
631
632 if (fc != NULL && fc->func->uf_profiling)
633 profile_start(&fc->prof_child);
634 script_prof_save(tm);
635}
636
637/*
638 * Take care of time spent in a child.
639 * Should always be called after prof_child_enter().
640 */
641 void
642prof_child_exit(
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200643 proftime_T *tm) // where waittime was stored
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200644{
645 funccall_T *fc = get_current_funccal();
646
647 if (fc != NULL && fc->func->uf_profiling)
648 {
649 profile_end(&fc->prof_child);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200650 profile_sub_wait(tm, &fc->prof_child); // don't count waiting time
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200651 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
652 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
653 }
654 script_prof_restore(tm);
655}
656
657/*
658 * Called when starting to read a function line.
659 * "sourcing_lnum" must be correct!
660 * When skipping lines it may not actually be executed, but we won't find out
661 * until later and we need to store the time now.
662 */
663 void
Bram Moolenaarb2049902021-01-24 12:53:53 +0100664func_line_start(void *cookie, long lnum)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200665{
666 funccall_T *fcp = (funccall_T *)cookie;
667 ufunc_T *fp = fcp->func;
668
Bram Moolenaarb2049902021-01-24 12:53:53 +0100669 if (fp->uf_profiling && lnum >= 1 && lnum <= fp->uf_lines.ga_len)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200670 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100671 fp->uf_tml_idx = lnum - 1;
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200672 // Skip continuation lines.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200673 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
674 --fp->uf_tml_idx;
675 fp->uf_tml_execed = FALSE;
676 profile_start(&fp->uf_tml_start);
677 profile_zero(&fp->uf_tml_children);
678 profile_get_wait(&fp->uf_tml_wait);
679 }
680}
681
682/*
683 * Called when actually executing a function line.
684 */
685 void
686func_line_exec(void *cookie)
687{
688 funccall_T *fcp = (funccall_T *)cookie;
689 ufunc_T *fp = fcp->func;
690
691 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
692 fp->uf_tml_execed = TRUE;
693}
694
695/*
696 * Called when done with a function line.
697 */
698 void
699func_line_end(void *cookie)
700{
701 funccall_T *fcp = (funccall_T *)cookie;
702 ufunc_T *fp = fcp->func;
703
704 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
705 {
706 if (fp->uf_tml_execed)
707 {
708 ++fp->uf_tml_count[fp->uf_tml_idx];
709 profile_end(&fp->uf_tml_start);
710 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
711 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
712 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
713 &fp->uf_tml_children);
714 }
715 fp->uf_tml_idx = -1;
716 }
717}
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200718
719/*
720 * Dump the profiling results for all functions in file "fd".
721 */
722 static void
723func_dump_profile(FILE *fd)
724{
725 hashtab_T *functbl;
726 hashitem_T *hi;
727 int todo;
728 ufunc_T *fp;
729 int i;
730 ufunc_T **sorttab;
731 int st_len = 0;
732 char_u *p;
733
734 functbl = func_tbl_get();
735 todo = (int)functbl->ht_used;
736 if (todo == 0)
737 return; // nothing to dump
738
739 sorttab = ALLOC_MULT(ufunc_T *, todo);
740
741 for (hi = functbl->ht_array; todo > 0; ++hi)
742 {
743 if (!HASHITEM_EMPTY(hi))
744 {
745 --todo;
746 fp = HI2UF(hi);
747 if (fp->uf_prof_initialized)
748 {
749 if (sorttab != NULL)
750 sorttab[st_len++] = fp;
751
752 if (fp->uf_name[0] == K_SPECIAL)
753 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
754 else
755 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
Bram Moolenaar16358802019-08-30 18:37:26 +0200756 if (fp->uf_script_ctx.sc_sid > 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200757 {
Bram Moolenaar16358802019-08-30 18:37:26 +0200758 p = home_replace_save(NULL,
759 get_scriptname(fp->uf_script_ctx.sc_sid));
760 if (p != NULL)
761 {
Bram Moolenaar181d4f52019-09-18 22:04:56 +0200762 fprintf(fd, " Defined: %s:%ld\n",
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200763 p, (long)fp->uf_script_ctx.sc_lnum);
Bram Moolenaar16358802019-08-30 18:37:26 +0200764 vim_free(p);
765 }
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200766 }
767 if (fp->uf_tm_count == 1)
768 fprintf(fd, "Called 1 time\n");
769 else
770 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
771 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
772 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
773 fprintf(fd, "\n");
774 fprintf(fd, "count total (s) self (s)\n");
775
776 for (i = 0; i < fp->uf_lines.ga_len; ++i)
777 {
778 if (FUNCLINE(fp, i) == NULL)
779 continue;
780 prof_func_line(fd, fp->uf_tml_count[i],
781 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
782 fprintf(fd, "%s\n", FUNCLINE(fp, i));
783 }
784 fprintf(fd, "\n");
785 }
786 }
787 }
788
789 if (sorttab != NULL && st_len > 0)
790 {
791 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
792 prof_total_cmp);
793 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
794 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
795 prof_self_cmp);
796 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
797 }
798
799 vim_free(sorttab);
800}
801
802/*
803 * Start profiling script "fp".
804 */
805 void
806script_do_profile(scriptitem_T *si)
807{
808 si->sn_pr_count = 0;
809 profile_zero(&si->sn_pr_total);
810 profile_zero(&si->sn_pr_self);
811
812 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
813 si->sn_prl_idx = -1;
814 si->sn_prof_on = TRUE;
815 si->sn_pr_nest = 0;
816}
817
818/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200819 * Count time spent in children after invoking another script or function.
820 */
821 void
822script_prof_restore(proftime_T *tm)
823{
824 scriptitem_T *si;
825
Bram Moolenaare3d46852020-08-29 13:39:17 +0200826 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200827 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100828 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200829 if (si->sn_prof_on && --si->sn_pr_nest == 0)
830 {
831 profile_end(&si->sn_pr_child);
832 profile_sub_wait(tm, &si->sn_pr_child); // don't count wait time
833 profile_add(&si->sn_pr_children, &si->sn_pr_child);
834 profile_add(&si->sn_prl_children, &si->sn_pr_child);
835 }
836 }
837}
838
839/*
840 * Dump the profiling results for all scripts in file "fd".
841 */
842 static void
843script_dump_profile(FILE *fd)
844{
845 int id;
846 scriptitem_T *si;
847 int i;
848 FILE *sfd;
849 sn_prl_T *pp;
850
851 for (id = 1; id <= script_items.ga_len; ++id)
852 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100853 si = SCRIPT_ITEM(id);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200854 if (si->sn_prof_on)
855 {
856 fprintf(fd, "SCRIPT %s\n", si->sn_name);
857 if (si->sn_pr_count == 1)
858 fprintf(fd, "Sourced 1 time\n");
859 else
860 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
861 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
862 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
863 fprintf(fd, "\n");
864 fprintf(fd, "count total (s) self (s)\n");
865
866 sfd = mch_fopen((char *)si->sn_name, "r");
867 if (sfd == NULL)
868 fprintf(fd, "Cannot open file!\n");
869 else
870 {
871 // Keep going till the end of file, so that trailing
872 // continuation lines are listed.
873 for (i = 0; ; ++i)
874 {
875 if (vim_fgets(IObuff, IOSIZE, sfd))
876 break;
877 // When a line has been truncated, append NL, taking care
878 // of multi-byte characters .
879 if (IObuff[IOSIZE - 2] != NUL && IObuff[IOSIZE - 2] != NL)
880 {
881 int n = IOSIZE - 2;
882
883 if (enc_utf8)
884 {
885 // Move to the first byte of this char.
886 // utf_head_off() doesn't work, because it checks
887 // for a truncated character.
888 while (n > 0 && (IObuff[n] & 0xc0) == 0x80)
889 --n;
890 }
891 else if (has_mbyte)
892 n -= mb_head_off(IObuff, IObuff + n);
893 IObuff[n] = NL;
894 IObuff[n + 1] = NUL;
895 }
896 if (i < si->sn_prl_ga.ga_len
897 && (pp = &PRL_ITEM(si, i))->snp_count > 0)
898 {
899 fprintf(fd, "%5d ", pp->snp_count);
900 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
901 fprintf(fd, " ");
902 else
903 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
904 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
905 }
906 else
907 fprintf(fd, " ");
908 fprintf(fd, "%s", IObuff);
909 }
910 fclose(sfd);
911 }
912 fprintf(fd, "\n");
913 }
914 }
915}
916
917/*
918 * Dump the profiling info.
919 */
920 void
921profile_dump(void)
922{
923 FILE *fd;
924
925 if (profile_fname != NULL)
926 {
927 fd = mch_fopen((char *)profile_fname, "w");
928 if (fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000929 semsg(_(e_cant_open_file_str), profile_fname);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200930 else
931 {
932 script_dump_profile(fd);
933 func_dump_profile(fd);
934 fclose(fd);
935 }
936 }
937}
938
939/*
940 * Called when starting to read a script line.
941 * "sourcing_lnum" must be correct!
942 * When skipping lines it may not actually be executed, but we won't find out
943 * until later and we need to store the time now.
944 */
945 void
946script_line_start(void)
947{
948 scriptitem_T *si;
949 sn_prl_T *pp;
950
Bram Moolenaare3d46852020-08-29 13:39:17 +0200951 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200952 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100953 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100954 if (si->sn_prof_on && SOURCING_LNUM >= 1)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200955 {
956 // Grow the array before starting the timer, so that the time spent
957 // here isn't counted.
958 (void)ga_grow(&si->sn_prl_ga,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100959 (int)(SOURCING_LNUM - si->sn_prl_ga.ga_len));
960 si->sn_prl_idx = SOURCING_LNUM - 1;
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200961 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
962 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
963 {
964 // Zero counters for a line that was not used before.
965 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
966 pp->snp_count = 0;
967 profile_zero(&pp->sn_prl_total);
968 profile_zero(&pp->sn_prl_self);
969 ++si->sn_prl_ga.ga_len;
970 }
971 si->sn_prl_execed = FALSE;
972 profile_start(&si->sn_prl_start);
973 profile_zero(&si->sn_prl_children);
974 profile_get_wait(&si->sn_prl_wait);
975 }
976}
977
978/*
979 * Called when actually executing a function line.
980 */
981 void
982script_line_exec(void)
983{
984 scriptitem_T *si;
985
Bram Moolenaare3d46852020-08-29 13:39:17 +0200986 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200987 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100988 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200989 if (si->sn_prof_on && si->sn_prl_idx >= 0)
990 si->sn_prl_execed = TRUE;
991}
992
993/*
994 * Called when done with a script line.
995 */
996 void
997script_line_end(void)
998{
999 scriptitem_T *si;
1000 sn_prl_T *pp;
1001
Bram Moolenaare3d46852020-08-29 13:39:17 +02001002 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001003 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001004 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001005 if (si->sn_prof_on && si->sn_prl_idx >= 0
1006 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
1007 {
1008 if (si->sn_prl_execed)
1009 {
1010 pp = &PRL_ITEM(si, si->sn_prl_idx);
1011 ++pp->snp_count;
1012 profile_end(&si->sn_prl_start);
1013 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
1014 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
1015 profile_self(&pp->sn_prl_self, &si->sn_prl_start,
1016 &si->sn_prl_children);
1017 }
1018 si->sn_prl_idx = -1;
1019 }
1020}
1021# endif // FEAT_PROFILE
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001022
1023#endif