Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(userspace/libsinsp): call sinsp_observer methods after an event has been processed by all parsers #2222

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Jan 3, 2025

What type of PR is this?

/kind cleanup

Any specific area of the project related to this PR?

/area libsinsp

Does this PR require a change in the driver versions?

What this PR does / why we need it:

The PR contains 2 distinct commits:

  • first of all, properly call sinsp_observer methods once all parsers (plugins too!) processed the event; in this way, we can be sure that libsinsp state has been fully processed and is consistent
  • secondly, drops 2 unused fields in threadinfo: m_program_hash and m_program_hash_scripts, and their compute_program_hash method. They were completely unused and their implementation can be eventually offloaded to libs consumers (by either attaching a parse plugin or using the sinsp_observer.

IMHO in the future sinsp_observer class should go away and consumers should rely upon parse plugins.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

cleanup(userspace/libsinsp): call sinsp_observer methods after an event has been processed by all parsers
cleanup(userspace/libsinsp)!: drop m_program_hash and m_program_hash_scripts from threadinfo

@poiana
Copy link
Contributor

poiana commented Jan 3, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

github-actions bot commented Jan 3, 2025

Perf diff from master - unit tests

     4.70%     -0.98%  [.] gzfile_read
    10.23%     +0.70%  [.] sinsp_parser::reset
     3.30%     +0.46%  [.] sinsp_evt::load_params
     1.53%     +0.37%  [.] next
     0.80%     +0.34%  [.] libsinsp::events::is_unknown_event
     8.42%     -0.31%  [.] sinsp_evt::get_type
     2.74%     -0.29%  [.] sinsp_parser::process_event
     0.48%     +0.26%  [.] 0x00000000000fc5b0
     0.68%     +0.26%  [.] sinsp_parser::parse_context_switch
     1.66%     +0.25%  [.] sinsp_evt_filter::sinsp_evt_filter

Heap diff from master - unit tests

peak heap memory consumption: 158.77K
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Heap diff from master - scap file

peak heap memory consumption: 3.46K
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Benchmarks diff from master

Comparing gbench_data.json to /root/actions-runner/_work/libs/libs/build/gbench_data.json
Benchmark                                                         Time             CPU      Time Old      Time New       CPU Old       CPU New
----------------------------------------------------------------------------------------------------------------------------------------------
BM_sinsp_split_mean                                            -0.0076         -0.0077           151           150           151           150
BM_sinsp_split_median                                          -0.0060         -0.0061           151           150           151           150
BM_sinsp_split_stddev                                          -0.3321         -0.3321             2             1             2             1
BM_sinsp_split_cv                                              -0.3270         -0.3269             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_mean                  +0.1105         +0.1104            56            62            56            62
BM_sinsp_concatenate_paths_relative_path_median                +0.1167         +0.1166            56            63            56            63
BM_sinsp_concatenate_paths_relative_path_stddev                +6.7958         +6.7881             0             1             0             1
BM_sinsp_concatenate_paths_relative_path_cv                    +6.0199         +6.0138             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_mean                     +0.0133         +0.0133            25            25            25            25
BM_sinsp_concatenate_paths_empty_path_median                   +0.0175         +0.0174            25            25            25            25
BM_sinsp_concatenate_paths_empty_path_stddev                   -0.1391         -0.1384             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_cv                       -0.1504         -0.1497             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_mean                  +0.1032         +0.1032            55            61            55            61
BM_sinsp_concatenate_paths_absolute_path_median                +0.0571         +0.0570            55            58            55            58
BM_sinsp_concatenate_paths_absolute_path_stddev                +3.7824         +3.7846             1             4             1             4
BM_sinsp_concatenate_paths_absolute_path_cv                    +3.3349         +3.3371             0             0             0             0
BM_sinsp_split_container_image_mean                            +0.0155         +0.0154           384           390           384           390
BM_sinsp_split_container_image_median                          +0.0209         +0.0208           384           392           384           392
BM_sinsp_split_container_image_stddev                          +0.0009         +0.0011             3             3             3             3
BM_sinsp_split_container_image_cv                              -0.0144         -0.0141             0             0             0             0

Copy link

codecov bot commented Jan 3, 2025

Codecov Report

Attention: Patch coverage is 72.46377% with 38 lines in your changes missing coverage. Please review.

Project coverage is 75.10%. Comparing base (f535e22) to head (e871b4d).
Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
userspace/libsinsp/parsers.cpp 46.93% 26 Missing ⚠️
userspace/libsinsp/test/classes/sinsp_observer.cpp 85.71% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2222      +/-   ##
==========================================
+ Coverage   75.06%   75.10%   +0.04%     
==========================================
  Files         267      276       +9     
  Lines       34259    34390     +131     
  Branches     5930     5930              
==========================================
+ Hits        25715    25830     +115     
- Misses       8544     8560      +16     
Flag Coverage Δ
libsinsp 75.10% <72.46%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@FedeDP FedeDP force-pushed the cleanup/post_process_callbacks branch from 4aa6d10 to aecb8af Compare January 3, 2025 14:28
@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 3, 2025

/milestone 0.20.0

@poiana poiana added this to the 0.20.0 milestone Jan 3, 2025
@FedeDP FedeDP changed the title wip: cleanup(userspace/libsinsp): call sinsp_observer methods after an event has been processed by all parsers cleanup(userspace/libsinsp): call sinsp_observer methods after an event has been processed by all parsers Jan 7, 2025
@LucaGuerra
Copy link
Contributor

LGTM, just a minor comment

@LucaGuerra
Copy link
Contributor

would also be nice to add a couple tests, since this patch is lowering coverage for libsinsp

@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 8, 2025

It seems a little bit too hard to add tests around it; i don't even know where i could start. I mean, of course i could create an inspector, add a sinsp_observer, create an event and check that my observer gets called; is this what you mean?

@LucaGuerra
Copy link
Contributor

LucaGuerra commented Jan 8, 2025

Yes, I believe you can simply add a test based on the usual sinsp_with_test_input fixture which already comes with an inspector, add a relevant observer to it, generate an event that should fire the observer and check that all was good.

@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 8, 2025

Yep i'll do it!

FedeDP added 3 commits January 8, 2025 17:18
…ine during parsing.

Instead, push them onto a queue owned by the inspector to be later called,
1 by 1, as requested.

This ensures that the whole libsinsp state has been processed,
even by plugins, before sinsp_observer methods are called.

Signed-off-by: Federico Di Pierro <[email protected]>
…program_hashscript` fields from threadinfo.

They are unused and can be eventually directly implemented by consumers, if needed.

Signed-off-by: Federico Di Pierro <[email protected]>
@FedeDP FedeDP force-pushed the cleanup/post_process_callbacks branch from aecb8af to 6ba4e77 Compare January 8, 2025 16:47
@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 8, 2025

@LucaGuerra pushed a small test file!

@poiana poiana removed the size/L label Jan 8, 2025
@poiana poiana added the size/XL label Jan 8, 2025
LucaGuerra
LucaGuerra previously approved these changes Jan 8, 2025
Copy link
Contributor

@LucaGuerra LucaGuerra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 8, 2025

/cc @jasondellaluce

@poiana poiana requested a review from jasondellaluce January 8, 2025 16:53
LucaGuerra
LucaGuerra previously approved these changes Jan 9, 2025
if(m_inspector->get_observer()) {
m_inspector->get_observer()->on_clone(evt, new_child.get(), tid_collision);
m_inspector->m_post_process_cbs.emplace(
[&new_child, tid_collision](sinsp_observer *observer, sinsp_evt *evt) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for passing new_child by reference here? I see this being applied to other parameters below. I would need to double check, but I think we risk this pointing to potential garbage memory as new_child is a pointer variable on the stack, and the reference would get treated as a pointer to it. If it's about raw pointers, can't we just pass them by copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, great catch, damnit!
/hold

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be because new_child is already a reference to a shared_ptr (!) which is not owned by the caller and we don't want to increase the refcount in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hopeful that we did not actually read deallocated stack memory there because normally those cases are covered by ASan, once a test is introduced we should get a stack-use-after-return error if we're trying to read garbage from the stack

Copy link
Contributor Author

@FedeDP FedeDP Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that the added test covers all cases, thus i agree with Jason, we need the fix.
Also, i am not sure how to fix the sinsp_parser::erase_fd() case, since the function takes a stack pointer to an ad-hoc structure (https://github.com/falcosecurity/libs/pull/2222/files#diff-b1e2f413bae0df2c1d50354a44678c2350f5ce108a3609fe14896be2529873f8R3221) and, even when captured as a normal C pointer, its address will be lost once we finally call our callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i think i fixed everything; also i improved the new test so that it checks that listener params are ok.

Moreover, improved sinsp_observer tests.

Signed-off-by: Federico Di Pierro <[email protected]>
Comment on lines 38 to 39
std::cerr << "[ ] read = " << evt->get_name() << " fd " << fdinfo->m_fd << " data "
<< data << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a bit more work, but instead of printing out text could we save this into a field and then expect or assert that the saved string is right?

Copy link
Contributor Author

@FedeDP FedeDP Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants