Skip to content

Commit

Permalink
feat(capi+go): implement API for clearing profiling data.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Dec 10, 2024
1 parent d7487c8 commit 382fe31
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
13 changes: 12 additions & 1 deletion capi/include/yara_x.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,23 @@ enum YRX_RESULT yrx_scanner_set_global_float(struct YRX_SCANNER *scanner,
// Iterates over the slowest N rules, calling the callback for each rule.
//
// Requires the `rules-profiling` feature, otherwise returns
// [`YRX_RESULT::NOT_SUPPORTED`]
// [`YRX_RESULT::NOT_SUPPORTED`].
//
// See [`YRX_SLOWEST_RULES_CALLBACK`] for more details.
enum YRX_RESULT yrx_scanner_iter_slowest_rules(struct YRX_SCANNER *scanner,
size_t n,
YRX_SLOWEST_RULES_CALLBACK callback,
void *user_data);

// Clears all accumulated profiling data.
//
// This resets the profiling data collected during rule execution across
// scanned files. Use this to start a new profiling session, ensuring the
// results reflect only the data gathered after this method is called.
//
// Requires the `rules-profiling` feature, otherwise returns
// [`YRX_RESULT::NOT_SUPPORTED`].
//
enum YRX_RESULT yrx_scanner_clear_profiling_data(struct YRX_SCANNER *scanner);

#endif /* YARA_X */
30 changes: 29 additions & 1 deletion capi/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub type YRX_SLOWEST_RULES_CALLBACK = extern "C" fn(
/// Iterates over the slowest N rules, calling the callback for each rule.
///
/// Requires the `rules-profiling` feature, otherwise returns
/// [`YRX_RESULT::NOT_SUPPORTED`]
/// [`YRX_RESULT::NOT_SUPPORTED`].
///
/// See [`YRX_SLOWEST_RULES_CALLBACK`] for more details.
#[no_mangle]
Expand Down Expand Up @@ -362,3 +362,31 @@ pub unsafe extern "C" fn yrx_scanner_iter_slowest_rules(
YRX_RESULT::SUCCESS
}
}

/// Clears all accumulated profiling data.
///
/// This resets the profiling data collected during rule execution across
/// scanned files. Use this to start a new profiling session, ensuring the
/// results reflect only the data gathered after this method is called.
///
/// Requires the `rules-profiling` feature, otherwise returns
/// [`YRX_RESULT::NOT_SUPPORTED`].
///
#[no_mangle]
#[allow(unused_variables)]
pub unsafe extern "C" fn yrx_scanner_clear_profiling_data(
scanner: *mut YRX_SCANNER,
) -> YRX_RESULT {
#[cfg(not(feature = "rules-profiling"))]
return YRX_RESULT::NOT_SUPPORTED;

#[cfg(feature = "rules-profiling")]
{
match scanner.as_mut() {
Some(s) => s.inner.clear_profiling_data(),
None => return YRX_RESULT::INVALID_ARGUMENT,
};

YRX_RESULT::SUCCESS
}
}
19 changes: 16 additions & 3 deletions go/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ func slowestRulesCallback(
// SlowestRules returns information about the slowest rules and how much
// time they spent matching patterns and executing their conditions.
//
// In order to use this function the YARA-X C library must be built with
// support for rules profiling, which is done by enabling the `rules-profiling`
// feature. Otherwise, calling this function will cause a panic.
// In order to use this function, the YARA-X C library must be built with
// support for rules profiling by enabling the `rules-profiling` feature.
// Otherwise, calling this function will cause a panic.
func (s *Scanner) SlowestRules(n int) []ProfilingInfo {
profilingInfo := make([]ProfilingInfo, 0)
slowestRules := cgo.NewHandle(&profilingInfo)
Expand All @@ -311,6 +311,19 @@ func (s *Scanner) SlowestRules(n int) []ProfilingInfo {
return profilingInfo
}

/// ClearProfilingData resets the profiling data collected during rule execution
/// across scanned files. Use it to start a new profiling session, ensuring the
/// results reflect only the data gathered after this method is called.
//
// In order to use this function, the YARA-X C library must be built with
// support for rules profiling by enabling the `rules-profiling` feature.
// Otherwise, calling this function will cause a panic.
func (s *Scanner) ClearProfilingData() {
if C.yrx_scanner_clear_profiling_data(s.cScanner) == C.NOT_SUPPORTED {
panic("ClearProfilingData requires that the YARA-X C library is built with the `rules-profiling` feature")
}
}

// Destroy destroys the scanner.
//
// Calling this method directly is not necessary, it will be invoked by the
Expand Down

0 comments on commit 382fe31

Please sign in to comment.