blob: 9c9fa9dbc22e1b5eee89f51a9c6af0091901e61e [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/*
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100302 * Reset all profiling information.
303 */
304 static void
305profile_reset(void)
306{
307 int id;
308 int todo;
309 hashtab_T *functbl;
310 hashitem_T *hi;
311
312 // Reset sourced files.
313 for (id = 1; id <= script_items.ga_len; id++)
314 {
315 scriptitem_T *si = SCRIPT_ITEM(id);
316 if (si->sn_prof_on)
317 {
318 si->sn_prof_on = FALSE;
319 si->sn_pr_force = FALSE;
320 profile_zero(&si->sn_pr_child);
321 si->sn_pr_nest = 0;
322 si->sn_pr_count = 0;
323 profile_zero(&si->sn_pr_total);
324 profile_zero(&si->sn_pr_self);
325 profile_zero(&si->sn_pr_start);
326 profile_zero(&si->sn_pr_children);
327 ga_clear(&si->sn_prl_ga);
328 profile_zero(&si->sn_prl_start);
329 profile_zero(&si->sn_prl_children);
330 profile_zero(&si->sn_prl_wait);
331 si->sn_prl_idx = -1;
332 si->sn_prl_execed = 0;
333 }
334 }
335
336 // Reset functions.
337 functbl = func_tbl_get();
338 todo = (int)functbl->ht_used;
339
340 for (hi = functbl->ht_array; todo > 0; ++hi)
341 {
342 ufunc_T *fp;
343 int i;
344
345 if (HASHITEM_EMPTY(hi))
346 continue;
347
348 todo--;
349 fp = HI2UF(hi);
350 if (fp->uf_prof_initialized)
351 {
352 fp->uf_profiling = 0;
353 fp->uf_prof_initialized = FALSE;
354 fp->uf_tm_count = 0;
355 profile_zero(&fp->uf_tm_total);
356 profile_zero(&fp->uf_tm_self);
357 profile_zero(&fp->uf_tm_children);
358
359 for (i = 0; i < fp->uf_lines.ga_len; i++)
360 {
361 fp->uf_tml_count[i] = 0;
362 profile_zero(&fp->uf_tml_total[i]);
363 profile_zero(&fp->uf_tml_self[i]);
364 }
365
366 profile_zero(&fp->uf_tml_start);
367 profile_zero(&fp->uf_tml_children);
368 profile_zero(&fp->uf_tml_wait);
369 fp->uf_tml_idx = -1;
370 fp->uf_tml_execed = 0;
371 }
372 }
373
374 VIM_CLEAR(profile_fname);
375}
376
377/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200378 * ":profile cmd args"
379 */
380 void
381ex_profile(exarg_T *eap)
382{
383 char_u *e;
384 int len;
385
386 e = skiptowhite(eap->arg);
387 len = (int)(e - eap->arg);
388 e = skipwhite(e);
389
390 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
391 {
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100392 VIM_CLEAR(profile_fname);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200393 profile_fname = expand_env_save_opt(e, TRUE);
394 do_profiling = PROF_YES;
395 profile_zero(&prof_wait_time);
396 set_vim_var_nr(VV_PROFILING, 1L);
397 }
398 else if (do_profiling == PROF_NONE)
Bram Moolenaar677658a2022-01-05 16:09:06 +0000399 emsg(_(e_first_use_profile_start_fname));
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100400 else if (STRCMP(eap->arg, "stop") == 0)
401 {
402 profile_dump();
403 do_profiling = PROF_NONE;
404 set_vim_var_nr(VV_PROFILING, 0L);
405 profile_reset();
406 }
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200407 else if (STRCMP(eap->arg, "pause") == 0)
408 {
409 if (do_profiling == PROF_YES)
410 profile_start(&pause_time);
411 do_profiling = PROF_PAUSED;
412 }
413 else if (STRCMP(eap->arg, "continue") == 0)
414 {
415 if (do_profiling == PROF_PAUSED)
416 {
417 profile_end(&pause_time);
418 profile_add(&prof_wait_time, &pause_time);
419 }
420 do_profiling = PROF_YES;
421 }
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100422 else if (STRCMP(eap->arg, "dump") == 0)
423 profile_dump();
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200424 else
425 {
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200426 // The rest is similar to ":breakadd".
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200427 ex_breakadd(eap);
428 }
429}
430
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200431// Command line expansion for :profile.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200432static enum
433{
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200434 PEXP_SUBCMD, // expand :profile sub-commands
435 PEXP_FUNC // expand :profile func {funcname}
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200436} pexpand_what;
437
438static char *pexpand_cmds[] = {
439 "start",
440#define PROFCMD_START 0
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100441 "stop",
442#define PROFCMD_STOP 1
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200443 "pause",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100444#define PROFCMD_PAUSE 2
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200445 "continue",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100446#define PROFCMD_CONTINUE 3
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200447 "func",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100448#define PROFCMD_FUNC 4
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200449 "file",
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100450#define PROFCMD_DUMP 5
451 "dump",
452#define PROFCMD_FILE 6
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200453 NULL
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100454#define PROFCMD_LAST 7
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200455};
456
457/*
458 * Function given to ExpandGeneric() to obtain the profile command
459 * specific expansion.
460 */
461 char_u *
462get_profile_name(expand_T *xp UNUSED, int idx)
463{
464 switch (pexpand_what)
465 {
466 case PEXP_SUBCMD:
467 return (char_u *)pexpand_cmds[idx];
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200468 default:
469 return NULL;
470 }
471}
472
473/*
474 * Handle command line completion for :profile command.
475 */
476 void
477set_context_in_profile_cmd(expand_T *xp, char_u *arg)
478{
479 char_u *end_subcmd;
480
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200481 // Default: expand subcommands.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200482 xp->xp_context = EXPAND_PROFILE;
483 pexpand_what = PEXP_SUBCMD;
484 xp->xp_pattern = arg;
485
486 end_subcmd = skiptowhite(arg);
487 if (*end_subcmd == NUL)
488 return;
489
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000490 if ((end_subcmd - arg == 5 && STRNCMP(arg, "start", 5) == 0)
491 || (end_subcmd - arg == 4 && STRNCMP(arg, "file", 4) == 0))
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200492 {
493 xp->xp_context = EXPAND_FILES;
494 xp->xp_pattern = skipwhite(end_subcmd);
495 return;
496 }
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000497 else if (end_subcmd - arg == 4 && STRNCMP(arg, "func", 4) == 0)
498 {
499 xp->xp_context = EXPAND_USER_FUNC;
500 xp->xp_pattern = skipwhite(end_subcmd);
501 return;
502 }
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200503
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200504 xp->xp_context = EXPAND_NOTHING;
505}
506
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200507static proftime_T inchar_time;
508
509/*
510 * Called when starting to wait for the user to type a character.
511 */
512 void
513prof_inchar_enter(void)
514{
515 profile_start(&inchar_time);
516}
517
518/*
519 * Called when finished waiting for the user to type a character.
520 */
521 void
522prof_inchar_exit(void)
523{
524 profile_end(&inchar_time);
525 profile_add(&prof_wait_time, &inchar_time);
526}
527
528
529/*
530 * Return TRUE when a function defined in the current script should be
531 * profiled.
532 */
533 int
534prof_def_func(void)
535{
536 if (current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100537 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_pr_force;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200538 return FALSE;
539}
540
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200541/*
542 * Print the count and times for one function or function line.
543 */
544 static void
545prof_func_line(
546 FILE *fd,
547 int count,
548 proftime_T *total,
549 proftime_T *self,
550 int prefer_self) // when equal print only self time
551{
552 if (count > 0)
553 {
554 fprintf(fd, "%5d ", count);
555 if (prefer_self && profile_equal(total, self))
556 fprintf(fd, " ");
557 else
558 fprintf(fd, "%s ", profile_msg(total));
559 if (!prefer_self && profile_equal(total, self))
560 fprintf(fd, " ");
561 else
562 fprintf(fd, "%s ", profile_msg(self));
563 }
564 else
565 fprintf(fd, " ");
566}
567
568 static void
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200569prof_sort_list(
570 FILE *fd,
571 ufunc_T **sorttab,
572 int st_len,
573 char *title,
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200574 int prefer_self) // when equal print only self time
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200575{
576 int i;
577 ufunc_T *fp;
578
579 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
580 fprintf(fd, "count total (s) self (s) function\n");
581 for (i = 0; i < 20 && i < st_len; ++i)
582 {
583 fp = sorttab[i];
584 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
585 prefer_self);
586 if (fp->uf_name[0] == K_SPECIAL)
587 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
588 else
589 fprintf(fd, " %s()\n", fp->uf_name);
590 }
591 fprintf(fd, "\n");
592}
593
594/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200595 * Compare function for total time sorting.
596 */
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200597 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200598prof_total_cmp(const void *s1, const void *s2)
599{
600 ufunc_T *p1, *p2;
601
602 p1 = *(ufunc_T **)s1;
603 p2 = *(ufunc_T **)s2;
604 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
605}
606
607/*
608 * Compare function for self time sorting.
609 */
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200610 static int
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200611prof_self_cmp(const void *s1, const void *s2)
612{
613 ufunc_T *p1, *p2;
614
615 p1 = *(ufunc_T **)s1;
616 p2 = *(ufunc_T **)s2;
617 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
618}
619
620/*
621 * Start profiling function "fp".
622 */
623 void
624func_do_profile(ufunc_T *fp)
625{
626 int len = fp->uf_lines.ga_len;
627
628 if (!fp->uf_prof_initialized)
629 {
630 if (len == 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200631 len = 1; // avoid getting error for allocating zero bytes
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200632 fp->uf_tm_count = 0;
633 profile_zero(&fp->uf_tm_self);
634 profile_zero(&fp->uf_tm_total);
635 if (fp->uf_tml_count == NULL)
636 fp->uf_tml_count = ALLOC_CLEAR_MULT(int, len);
637 if (fp->uf_tml_total == NULL)
638 fp->uf_tml_total = ALLOC_CLEAR_MULT(proftime_T, len);
639 if (fp->uf_tml_self == NULL)
640 fp->uf_tml_self = ALLOC_CLEAR_MULT(proftime_T, len);
641 fp->uf_tml_idx = -1;
642 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
643 || fp->uf_tml_self == NULL)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200644 return; // out of memory
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200645 fp->uf_prof_initialized = TRUE;
646 }
647
648 fp->uf_profiling = TRUE;
649}
650
651/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200652 * Save time when starting to invoke another script or function.
653 */
654 static void
655script_prof_save(
656 proftime_T *tm) // place to store wait time
657{
658 scriptitem_T *si;
659
660 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
661 {
662 si = SCRIPT_ITEM(current_sctx.sc_sid);
663 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
664 profile_start(&si->sn_pr_child);
665 }
666 profile_get_wait(tm);
667}
668
669/*
Bram Moolenaarb2049902021-01-24 12:53:53 +0100670 * When calling a function: may initialize for profiling.
671 */
672 void
Bram Moolenaar12d26532021-02-19 19:13:21 +0100673profile_may_start_func(profinfo_T *info, ufunc_T *fp, ufunc_T *caller)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100674{
Bram Moolenaar12d26532021-02-19 19:13:21 +0100675 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarb2049902021-01-24 12:53:53 +0100676 {
Bram Moolenaar12d26532021-02-19 19:13:21 +0100677 info->pi_started_profiling = TRUE;
678 func_do_profile(fp);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100679 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100680 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
681 {
682 ++fp->uf_tm_count;
683 profile_start(&info->pi_call_start);
684 profile_zero(&fp->uf_tm_children);
685 }
686 script_prof_save(&info->pi_wait_start);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100687}
688
689/*
690 * After calling a function: may handle profiling. profile_may_start_func()
691 * must have been called previously.
692 */
693 void
Bram Moolenaar12d26532021-02-19 19:13:21 +0100694profile_may_end_func(profinfo_T *info, ufunc_T *fp, ufunc_T *caller)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100695{
696 profile_end(&info->pi_call_start);
697 profile_sub_wait(&info->pi_wait_start, &info->pi_call_start);
698 profile_add(&fp->uf_tm_total, &info->pi_call_start);
699 profile_self(&fp->uf_tm_self, &info->pi_call_start, &fp->uf_tm_children);
Bram Moolenaar12d26532021-02-19 19:13:21 +0100700 if (caller != NULL && caller->uf_profiling)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100701 {
Bram Moolenaar12d26532021-02-19 19:13:21 +0100702 profile_add(&caller->uf_tm_children, &info->pi_call_start);
703 profile_add(&caller->uf_tml_children, &info->pi_call_start);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100704 }
705 if (info->pi_started_profiling)
706 // make a ":profdel func" stop profiling the function
707 fp->uf_profiling = FALSE;
708}
709
710/*
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200711 * Prepare profiling for entering a child or something else that is not
712 * counted for the script/function itself.
713 * Should always be called in pair with prof_child_exit().
714 */
715 void
716prof_child_enter(
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200717 proftime_T *tm) // place to store waittime
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200718{
719 funccall_T *fc = get_current_funccal();
720
Bram Moolenaarca16c602022-09-06 18:57:08 +0100721 if (fc != NULL && fc->fc_func->uf_profiling)
722 profile_start(&fc->fc_prof_child);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200723 script_prof_save(tm);
724}
725
726/*
727 * Take care of time spent in a child.
728 * Should always be called after prof_child_enter().
729 */
730 void
731prof_child_exit(
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200732 proftime_T *tm) // where waittime was stored
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200733{
734 funccall_T *fc = get_current_funccal();
735
Bram Moolenaarca16c602022-09-06 18:57:08 +0100736 if (fc != NULL && fc->fc_func->uf_profiling)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200737 {
Bram Moolenaarca16c602022-09-06 18:57:08 +0100738 profile_end(&fc->fc_prof_child);
739 profile_sub_wait(tm, &fc->fc_prof_child); // don't count waiting time
740 profile_add(&fc->fc_func->uf_tm_children, &fc->fc_prof_child);
741 profile_add(&fc->fc_func->uf_tml_children, &fc->fc_prof_child);
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200742 }
743 script_prof_restore(tm);
744}
745
746/*
747 * Called when starting to read a function line.
748 * "sourcing_lnum" must be correct!
749 * When skipping lines it may not actually be executed, but we won't find out
750 * until later and we need to store the time now.
751 */
752 void
Bram Moolenaarb2049902021-01-24 12:53:53 +0100753func_line_start(void *cookie, long lnum)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200754{
755 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100756 ufunc_T *fp = fcp->fc_func;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200757
Bram Moolenaarb2049902021-01-24 12:53:53 +0100758 if (fp->uf_profiling && lnum >= 1 && lnum <= fp->uf_lines.ga_len)
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200759 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100760 fp->uf_tml_idx = lnum - 1;
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200761 // Skip continuation lines.
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200762 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
763 --fp->uf_tml_idx;
764 fp->uf_tml_execed = FALSE;
765 profile_start(&fp->uf_tml_start);
766 profile_zero(&fp->uf_tml_children);
767 profile_get_wait(&fp->uf_tml_wait);
768 }
769}
770
771/*
772 * Called when actually executing a function line.
773 */
774 void
775func_line_exec(void *cookie)
776{
777 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100778 ufunc_T *fp = fcp->fc_func;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200779
780 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
781 fp->uf_tml_execed = TRUE;
782}
783
784/*
785 * Called when done with a function line.
786 */
787 void
788func_line_end(void *cookie)
789{
790 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100791 ufunc_T *fp = fcp->fc_func;
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +0200792
793 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
794 {
795 if (fp->uf_tml_execed)
796 {
797 ++fp->uf_tml_count[fp->uf_tml_idx];
798 profile_end(&fp->uf_tml_start);
799 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
800 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
801 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
802 &fp->uf_tml_children);
803 }
804 fp->uf_tml_idx = -1;
805 }
806}
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200807
808/*
809 * Dump the profiling results for all functions in file "fd".
810 */
811 static void
812func_dump_profile(FILE *fd)
813{
814 hashtab_T *functbl;
815 hashitem_T *hi;
816 int todo;
817 ufunc_T *fp;
818 int i;
819 ufunc_T **sorttab;
820 int st_len = 0;
821 char_u *p;
822
823 functbl = func_tbl_get();
824 todo = (int)functbl->ht_used;
825 if (todo == 0)
826 return; // nothing to dump
827
828 sorttab = ALLOC_MULT(ufunc_T *, todo);
829
830 for (hi = functbl->ht_array; todo > 0; ++hi)
831 {
832 if (!HASHITEM_EMPTY(hi))
833 {
834 --todo;
835 fp = HI2UF(hi);
836 if (fp->uf_prof_initialized)
837 {
838 if (sorttab != NULL)
839 sorttab[st_len++] = fp;
840
841 if (fp->uf_name[0] == K_SPECIAL)
842 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
843 else
844 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
Bram Moolenaar16358802019-08-30 18:37:26 +0200845 if (fp->uf_script_ctx.sc_sid > 0)
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200846 {
Bram Moolenaar16358802019-08-30 18:37:26 +0200847 p = home_replace_save(NULL,
848 get_scriptname(fp->uf_script_ctx.sc_sid));
849 if (p != NULL)
850 {
Bram Moolenaar181d4f52019-09-18 22:04:56 +0200851 fprintf(fd, " Defined: %s:%ld\n",
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200852 p, (long)fp->uf_script_ctx.sc_lnum);
Bram Moolenaar16358802019-08-30 18:37:26 +0200853 vim_free(p);
854 }
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200855 }
856 if (fp->uf_tm_count == 1)
857 fprintf(fd, "Called 1 time\n");
858 else
859 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
860 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
861 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
862 fprintf(fd, "\n");
863 fprintf(fd, "count total (s) self (s)\n");
864
865 for (i = 0; i < fp->uf_lines.ga_len; ++i)
866 {
867 if (FUNCLINE(fp, i) == NULL)
868 continue;
869 prof_func_line(fd, fp->uf_tml_count[i],
870 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
871 fprintf(fd, "%s\n", FUNCLINE(fp, i));
872 }
873 fprintf(fd, "\n");
874 }
875 }
876 }
877
878 if (sorttab != NULL && st_len > 0)
879 {
880 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
881 prof_total_cmp);
882 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
883 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
884 prof_self_cmp);
885 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
886 }
887
888 vim_free(sorttab);
889}
890
891/*
892 * Start profiling script "fp".
893 */
894 void
895script_do_profile(scriptitem_T *si)
896{
897 si->sn_pr_count = 0;
898 profile_zero(&si->sn_pr_total);
899 profile_zero(&si->sn_pr_self);
900
901 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
902 si->sn_prl_idx = -1;
903 si->sn_prof_on = TRUE;
904 si->sn_pr_nest = 0;
905}
906
907/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200908 * Count time spent in children after invoking another script or function.
909 */
910 void
911script_prof_restore(proftime_T *tm)
912{
913 scriptitem_T *si;
914
Bram Moolenaare3d46852020-08-29 13:39:17 +0200915 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200916 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100917 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200918 if (si->sn_prof_on && --si->sn_pr_nest == 0)
919 {
920 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);
924 }
925 }
926}
927
928/*
929 * Dump the profiling results for all scripts in file "fd".
930 */
931 static void
932script_dump_profile(FILE *fd)
933{
934 int id;
935 scriptitem_T *si;
936 int i;
937 FILE *sfd;
938 sn_prl_T *pp;
939
940 for (id = 1; id <= script_items.ga_len; ++id)
941 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100942 si = SCRIPT_ITEM(id);
Bram Moolenaar660a10a2019-07-14 15:48:38 +0200943 if (si->sn_prof_on)
944 {
945 fprintf(fd, "SCRIPT %s\n", si->sn_name);
946 if (si->sn_pr_count == 1)
947 fprintf(fd, "Sourced 1 time\n");
948 else
949 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
950 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
951 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
952 fprintf(fd, "\n");
953 fprintf(fd, "count total (s) self (s)\n");
954
955 sfd = mch_fopen((char *)si->sn_name, "r");
956 if (sfd == NULL)
957 fprintf(fd, "Cannot open file!\n");
958 else
959 {
960 // Keep going till the end of file, so that trailing
961 // continuation lines are listed.
962 for (i = 0; ; ++i)
963 {
964 if (vim_fgets(IObuff, IOSIZE, sfd))
965 break;
966 // When a line has been truncated, append NL, taking care
967 // of multi-byte characters .
968 if (IObuff[IOSIZE - 2] != NUL && IObuff[IOSIZE - 2] != NL)
969 {
970 int n = IOSIZE - 2;
971
972 if (enc_utf8)
973 {
974 // Move to the first byte of this char.
975 // utf_head_off() doesn't work, because it checks
976 // for a truncated character.
977 while (n > 0 && (IObuff[n] & 0xc0) == 0x80)
978 --n;
979 }
980 else if (has_mbyte)
981 n -= mb_head_off(IObuff, IObuff + n);
982 IObuff[n] = NL;
983 IObuff[n + 1] = NUL;
984 }
985 if (i < si->sn_prl_ga.ga_len
986 && (pp = &PRL_ITEM(si, i))->snp_count > 0)
987 {
988 fprintf(fd, "%5d ", pp->snp_count);
989 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
990 fprintf(fd, " ");
991 else
992 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
993 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
994 }
995 else
996 fprintf(fd, " ");
997 fprintf(fd, "%s", IObuff);
998 }
999 fclose(sfd);
1000 }
1001 fprintf(fd, "\n");
1002 }
1003 }
1004}
1005
1006/*
1007 * Dump the profiling info.
1008 */
1009 void
1010profile_dump(void)
1011{
1012 FILE *fd;
1013
1014 if (profile_fname != NULL)
1015 {
1016 fd = mch_fopen((char *)profile_fname, "w");
1017 if (fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001018 semsg(_(e_cant_open_file_str), profile_fname);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001019 else
1020 {
1021 script_dump_profile(fd);
1022 func_dump_profile(fd);
1023 fclose(fd);
1024 }
1025 }
1026}
1027
1028/*
1029 * Called when starting to read a script line.
1030 * "sourcing_lnum" must be correct!
1031 * When skipping lines it may not actually be executed, but we won't find out
1032 * until later and we need to store the time now.
1033 */
1034 void
1035script_line_start(void)
1036{
1037 scriptitem_T *si;
1038 sn_prl_T *pp;
1039
Bram Moolenaare3d46852020-08-29 13:39:17 +02001040 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001041 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001042 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001043 if (si->sn_prof_on && SOURCING_LNUM >= 1)
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001044 {
1045 // Grow the array before starting the timer, so that the time spent
1046 // here isn't counted.
1047 (void)ga_grow(&si->sn_prl_ga,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001048 (int)(SOURCING_LNUM - si->sn_prl_ga.ga_len));
1049 si->sn_prl_idx = SOURCING_LNUM - 1;
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001050 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
1051 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
1052 {
1053 // Zero counters for a line that was not used before.
1054 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
1055 pp->snp_count = 0;
1056 profile_zero(&pp->sn_prl_total);
1057 profile_zero(&pp->sn_prl_self);
1058 ++si->sn_prl_ga.ga_len;
1059 }
1060 si->sn_prl_execed = FALSE;
1061 profile_start(&si->sn_prl_start);
1062 profile_zero(&si->sn_prl_children);
1063 profile_get_wait(&si->sn_prl_wait);
1064 }
1065}
1066
1067/*
1068 * Called when actually executing a function line.
1069 */
1070 void
1071script_line_exec(void)
1072{
1073 scriptitem_T *si;
1074
Bram Moolenaare3d46852020-08-29 13:39:17 +02001075 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001076 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001077 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001078 if (si->sn_prof_on && si->sn_prl_idx >= 0)
1079 si->sn_prl_execed = TRUE;
1080}
1081
1082/*
1083 * Called when done with a script line.
1084 */
1085 void
1086script_line_end(void)
1087{
1088 scriptitem_T *si;
1089 sn_prl_T *pp;
1090
Bram Moolenaare3d46852020-08-29 13:39:17 +02001091 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001092 return;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001093 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar660a10a2019-07-14 15:48:38 +02001094 if (si->sn_prof_on && si->sn_prl_idx >= 0
1095 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
1096 {
1097 if (si->sn_prl_execed)
1098 {
1099 pp = &PRL_ITEM(si, si->sn_prl_idx);
1100 ++pp->snp_count;
1101 profile_end(&si->sn_prl_start);
1102 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
1103 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
1104 profile_self(&pp->sn_prl_self, &si->sn_prl_start,
1105 &si->sn_prl_children);
1106 }
1107 si->sn_prl_idx = -1;
1108 }
1109}
1110# endif // FEAT_PROFILE
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001111
1112#endif