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

refactor(userspace/engine): reduce memory usage for resolving evttypes #1965

Merged
merged 7 commits into from
Apr 19, 2022

Conversation

jasondellaluce
Copy link
Contributor

@jasondellaluce jasondellaluce commented Apr 6, 2022

What type of PR is this?

/kind bug

/kind cleanup

Any specific area of the project related to this PR?

/area engine

/area tests

What this PR does / why we need it:

In falcosecurity/libs#74 we added a feature in libsinsp for searching the event types for a given filtercheck. The set of event types is used internally by the Falco rule engine to index each rule filter by event type to improve performance at runtime. Before this, we used to calculate the set of event types right from Falco.

In the implementation of falcosecurity/libs#74, filterchecks maintain the set of evttypes as part of the internal class state. Since this information is used only at initialization time, maintaining it at runtime causes a memory abuse that ended up in Falco 0.31+ using way more memory than needed, with the limit increasing depending on how many rules are loaded in the engine.

This PR fixes this by re-introducing the evttype search back into Falco, this time leveraging the new AST definitions introduced in falcosecurity/libs#217. We do so by implementing a simple AST visitor class that populates the set of evttypes for a given filter condition. I think this is optimal because this use case is only related to Falco and I don't think the logic should be part of libsinsp. Moreover, this allows a better control (for example, we want to find evttypes only for the syscall source whereas this is handled differently for k8s_audit and plugins).

This also deprecates the changes of falcosecurity/libs#74, that can be removed after this PR gets merged (see falcosecurity/libs#280). The unit tests related to falcosecurity/libs#74 have been ported from libsinp to Falco and adapted to the Catch2 framework.

Here's some data about Falco's memory usage. The following was measured at runtime by after launching Falco by simply loading the falco_rules.yaml default ruleset:

Which issue(s) this PR fixes:

Not sure this is a direct fix, but this can help for the following issues that reported Falco consuming more memory at runtime:

Special notes for your reviewer:

Of the 650+ LOC added in this PR, but the real contribution is userspace/engine/filter_evttype_resolver.cpp with only ~160 LOC, the rest are just renamings and unit tests ported from libsinsp.

Does this PR introduce a user-facing change?:

refactor(userspace/engine): reduce memory usage for resolving evttypes

Copy link
Contributor

@mstemm mstemm left a comment

Choose a reason for hiding this comment

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

Thanks for making this change! One comment and 2 questions.

tests/engine/test_filter_evttype_resolver.cpp Show resolved Hide resolved
userspace/engine/ruleset.cpp Outdated Show resolved Hide resolved
userspace/engine/ruleset.h Show resolved Hide resolved
@jasondellaluce
Copy link
Contributor Author

I reverted the refactorings of the add_filter method signatures. I think that will require deeper attention and must be part of other future PRs.

@jasondellaluce
Copy link
Contributor Author

/milestone 0.32.0

@poiana poiana added this to the 0.32.0 milestone Apr 8, 2022
@jasondellaluce jasondellaluce force-pushed the refactor/evttypes branch 2 times, most recently from 4478ebb to a727440 Compare April 11, 2022 11:41
Copy link
Contributor

@mstemm mstemm left a comment

Choose a reason for hiding this comment

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

Just the question about the fields checksum changing now.

userspace/engine/falco_engine_version.h Show resolved Hide resolved
…ilters and event names

Signed-off-by: Jason Dellaluce <[email protected]>
At the same time, this also simplifies the unit test cases by using the SCENARIO construct of catch2,
which allows sharing a setup phases between different unit tests, and removes a bunch of repeated LOC in our case.

Signed-off-by: Jason Dellaluce <[email protected]>
Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

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

LGTM!

Thank you

/approve

@poiana poiana added the lgtm label Apr 19, 2022
@leogr leogr requested a review from mstemm April 19, 2022 13:51
@poiana
Copy link
Contributor

poiana commented Apr 19, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jasondellaluce, leogr

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:
  • OWNERS [jasondellaluce,leogr]

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

cmake/modules/plugins.cmake Show resolved Hide resolved
@poiana poiana removed the lgtm label Apr 19, 2022
@poiana poiana requested review from leogr and mstemm April 19, 2022 14:28
@poiana poiana added the lgtm label Apr 19, 2022
@poiana
Copy link
Contributor

poiana commented Apr 19, 2022

LGTM label has been added.

Git tree hash: e081a992c33c2d418293c2bd72a3cb0ee8bf9958

@poiana poiana merged commit 13256fb into master Apr 19, 2022
@poiana poiana deleted the refactor/evttypes branch April 19, 2022 14:29
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