hooks for new --profile-scripts option

This commit is contained in:
Vern Paxson 2022-05-03 11:12:29 -07:00
parent bc3902fa79
commit 07f5e9cbe7
3 changed files with 30 additions and 24 deletions

View file

@ -52,10 +52,6 @@ void EventHandler::SetFunc(FuncPtr f)
void EventHandler::Call(Args* vl, bool no_remote) void EventHandler::Call(Args* vl, bool no_remote)
{ {
#ifdef PROFILE_BRO_FUNCTIONS
DEBUG_MSG("Event: %s\n", Name());
#endif
if ( new_event ) if ( new_event )
NewEvent(vl); NewEvent(vl);

View file

@ -5,18 +5,6 @@
#include "zeek/zeek-config.h" #include "zeek/zeek-config.h"
#include <sys/stat.h>
#include <sys/types.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#include <broker/error.hh> #include <broker/error.hh>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@ -25,7 +13,6 @@
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/resource.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm> #include <algorithm>
@ -42,6 +29,7 @@
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
#include "zeek/RunState.h" #include "zeek/RunState.h"
#include "zeek/Scope.h" #include "zeek/Scope.h"
#include "zeek/ScriptProfile.h"
#include "zeek/Stmt.h" #include "zeek/Stmt.h"
#include "zeek/Traverse.h" #include "zeek/Traverse.h"
#include "zeek/Var.h" #include "zeek/Var.h"
@ -348,9 +336,6 @@ bool ScriptFunc::IsPure() const
ValPtr ScriptFunc::Invoke(zeek::Args* args, Frame* parent) const ValPtr ScriptFunc::Invoke(zeek::Args* args, Frame* parent) const
{ {
#ifdef PROFILE_BRO_FUNCTIONS
DEBUG_MSG("Function: %s\n", Name());
#endif
SegmentProfiler prof(segment_logger, location); SegmentProfiler prof(segment_logger, location);
if ( sample_logger ) if ( sample_logger )
@ -417,6 +402,9 @@ ValPtr ScriptFunc::Invoke(zeek::Args* args, Frame* parent) const
f->SetElement(j, arg); f->SetElement(j, arg);
} }
if ( spm )
spm->StartInvocation(this, body.stmts);
f->Reset(args->size()); f->Reset(args->size());
try try
@ -439,6 +427,9 @@ ValPtr ScriptFunc::Invoke(zeek::Args* args, Frame* parent) const
continue; continue;
} }
if ( spm )
spm->EndInvocation();
if ( f->HasDelayed() ) if ( f->HasDelayed() )
{ {
assert(! result); assert(! result);
@ -758,9 +749,9 @@ bool BuiltinFunc::IsPure() const
ValPtr BuiltinFunc::Invoke(Args* args, Frame* parent) const ValPtr BuiltinFunc::Invoke(Args* args, Frame* parent) const
{ {
#ifdef PROFILE_BRO_FUNCTIONS if ( spm )
DEBUG_MSG("Function: %s\n", Name()); spm->StartInvocation(this);
#endif
SegmentProfiler prof(segment_logger, Name()); SegmentProfiler prof(segment_logger, Name());
if ( sample_logger ) if ( sample_logger )
@ -772,7 +763,11 @@ ValPtr BuiltinFunc::Invoke(Args* args, Frame* parent) const
CheckPluginResult(handled, hook_result, FUNC_FLAVOR_FUNCTION); CheckPluginResult(handled, hook_result, FUNC_FLAVOR_FUNCTION);
if ( handled ) if ( handled )
{
if ( spm )
spm->EndInvocation();
return hook_result; return hook_result;
}
if ( g_trace_state.DoTrace() ) if ( g_trace_state.DoTrace() )
{ {
@ -795,6 +790,9 @@ ValPtr BuiltinFunc::Invoke(Args* args, Frame* parent) const
g_trace_state.LogTrace("\tFunction return: %s\n", d.Description()); g_trace_state.LogTrace("\tFunction return: %s\n", d.Description());
} }
if ( spm )
spm->EndInvocation();
return result; return result;
} }

View file

@ -6,6 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include "zeek/ScriptProfile.h"
#include "zeek/script_opt/ScriptOpt.h" #include "zeek/script_opt/ScriptOpt.h"
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
@ -142,6 +143,9 @@ void usage(const char* prog, int code)
fprintf(stderr, " -m|--mem-leaks | show leaks [perftools]\n"); fprintf(stderr, " -m|--mem-leaks | show leaks [perftools]\n");
fprintf(stderr, " -M|--mem-profile | record heap [perftools]\n"); fprintf(stderr, " -M|--mem-profile | record heap [perftools]\n");
#endif #endif
fprintf(
stderr,
" --profile-scripts[=file] | profile scripts to given file (default stdout)\n");
fprintf(stderr, " --pseudo-realtime[=<speedup>] | enable pseudo-realtime for performance " fprintf(stderr, " --pseudo-realtime[=<speedup>] | enable pseudo-realtime for performance "
"evaluation (default 1)\n"); "evaluation (default 1)\n");
fprintf(stderr, " -j|--jobs | enable supervisor mode\n"); fprintf(stderr, " -j|--jobs | enable supervisor mode\n");
@ -362,7 +366,9 @@ Options parse_cmdline(int argc, char** argv)
} }
} }
constexpr struct option long_opts[] = { int profile_scripts = 0;
struct option long_opts[] = {
{"parse-only", no_argument, nullptr, 'a'}, {"parse-only", no_argument, nullptr, 'a'},
{"bare-mode", no_argument, nullptr, 'b'}, {"bare-mode", no_argument, nullptr, 'b'},
{"capture-unprocessed", required_argument, nullptr, 'c'}, {"capture-unprocessed", required_argument, nullptr, 'c'},
@ -403,6 +409,7 @@ Options parse_cmdline(int argc, char** argv)
{"mem-profile", no_argument, nullptr, 'M'}, {"mem-profile", no_argument, nullptr, 'M'},
#endif #endif
{"profile-scripts", optional_argument, &profile_scripts, 1},
{"pseudo-realtime", optional_argument, nullptr, '~'}, {"pseudo-realtime", optional_argument, nullptr, '~'},
{"jobs", optional_argument, nullptr, 'j'}, {"jobs", optional_argument, nullptr, 'j'},
{"test", no_argument, nullptr, '#'}, {"test", no_argument, nullptr, '#'},
@ -601,6 +608,11 @@ Options parse_cmdline(int argc, char** argv)
case 0: case 0:
// This happens for long options that don't have // This happens for long options that don't have
// a short-option equivalent. // a short-option equivalent.
if ( profile_scripts )
{
activate_script_profiling(optarg);
profile_scripts = 0;
}
break; break;
case '?': case '?':