From 41aee03c17ecd6958cc21e48b833ad1c6945ca13 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Tue, 3 May 2022 10:38:32 -0700 Subject: [PATCH] make curr_CPU_time() broadly available rather than just isolated to ZAM --- src/script_opt/ZAM/ZBody.cc | 15 ++++----------- src/util.cc | 7 +++++++ src/util.h | 3 +++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/script_opt/ZAM/ZBody.cc b/src/script_opt/ZAM/ZBody.cc index d4804f16d7..d011736049 100644 --- a/src/script_opt/ZAM/ZBody.cc +++ b/src/script_opt/ZAM/ZBody.cc @@ -124,13 +124,6 @@ VEC_COERCE(UD, TYPE_COUNT, bro_uint_t, AsDouble(), double_to_count_would_overflo "double to unsigned") VEC_COERCE(UI, TYPE_COUNT, bro_int_t, AsInt(), int_to_count_would_overflow, "signed to unsigned") -double curr_CPU_time() - { - struct timespec ts; - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return double(ts.tv_sec) + double(ts.tv_nsec) / 1e9; - } - ZBody::ZBody(const char* _func_name, const ZAMCompiler* zc) : Stmt(STMT_ZAM) { func_name = _func_name; @@ -240,14 +233,14 @@ void ZBody::InitProfile() ValPtr ZBody::Exec(Frame* f, StmtFlowType& flow) { #ifdef DEBUG - double t = analysis_options.profile_ZAM ? curr_CPU_time() : 0.0; + double t = analysis_options.profile_ZAM ? util::curr_CPU_time() : 0.0; #endif auto val = DoExec(f, 0, flow); #ifdef DEBUG if ( analysis_options.profile_ZAM ) - *CPU_time += curr_CPU_time() - t; + *CPU_time += util::curr_CPU_time() - t; #endif return val; @@ -305,7 +298,7 @@ ValPtr ZBody::DoExec(Frame* f, int start_pc, StmtFlowType& flow) ++(*inst_count)[pc]; profile_pc = pc; - profile_CPU = curr_CPU_time(); + profile_CPU = util::curr_CPU_time(); } #endif @@ -327,7 +320,7 @@ ValPtr ZBody::DoExec(Frame* f, int start_pc, StmtFlowType& flow) #ifdef DEBUG if ( do_profile ) { - double dt = curr_CPU_time() - profile_CPU; + double dt = util::curr_CPU_time() - profile_CPU; inst_CPU->at(profile_pc) += dt; ZOP_CPU[z.op] += dt; } diff --git a/src/util.cc b/src/util.cc index 17fc590886..830ceaf81b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2058,6 +2058,13 @@ int time_compare(struct timeval* tv_a, struct timeval* tv_b) return tv_a->tv_sec - tv_b->tv_sec; } +double curr_CPU_time() + { + struct timespec ts; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + return double(ts.tv_sec) + double(ts.tv_nsec) / 1e9; + } + struct UIDEntry { UIDEntry() : key(0, 0), needs_init(true) { } diff --git a/src/util.h b/src/util.h index e715a665ec..936f7424d7 100644 --- a/src/util.h +++ b/src/util.h @@ -424,6 +424,9 @@ extern struct timeval double_to_timeval(double t); // Return > 0 if tv_a > tv_b, 0 if equal, < 0 if tv_a < tv_b. extern int time_compare(struct timeval* tv_a, struct timeval* tv_b); +// Returns the CPU time consumed to date. +extern double curr_CPU_time(); + // Returns an integer that's very likely to be unique, even across Zeek // instances. The integer can be drawn from different pools, which is helpful // when the random number generator is seeded to be deterministic. In that