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

improve falco files loading performance #2151

Merged
merged 5 commits into from
Sep 5, 2022

Conversation

VadimZy
Copy link
Contributor

@VadimZy VadimZy commented Aug 3, 2022

What type of PR is this?

Uncomment one (or more) /kind <> lines:

/kind design

/kind feature

Any specific area of the project related to this PR?

/area engine

What this PR does / why we need it:
Improve rules loading/parsing performance
Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

update(userspace/engine): improve falco files loading performance

Copy link
Contributor

@jasondellaluce jasondellaluce left a comment

Choose a reason for hiding this comment

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

Gave a first round of comments. This looks great, specially the evttype part! Can you remove the draft status, so that the CI and tests can run?

userspace/engine/filter_evttype_resolver.h Outdated Show resolved Hide resolved
@@ -21,6 +21,124 @@ limitations under the License.
#include <set>
#include <memory>

class falco_event_types
Copy link
Contributor

Choose a reason for hiding this comment

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

This is awesome!

userspace/engine/rule_loader.cpp Outdated Show resolved Hide resolved
userspace/engine/rule_loader.cpp Outdated Show resolved Hide resolved
@@ -347,8 +364,9 @@ static shared_ptr<ast::expr> parse_condition(
}
catch (const sinsp_exception& e)
{
throw falco_exception("Compilation error when compiling \""
+ condition + "\": " + to_string(p.get_pos().col) + ": " + e.what());
throw falco_exception("Compilation error when compiling \n"
Copy link
Contributor

Choose a reason for hiding this comment

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

Watch out for conflicts with #2098.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved

Copy link
Contributor

Choose a reason for hiding this comment

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

This is not solved, the changes of #2098 introduced a new form of error reposting. We should throw a rule_loader::rule_load_exception with proper err code here.

@mstemm
Copy link
Contributor

mstemm commented Aug 8, 2022

Should we bother changing any of the json_evt code at all, given that it's effectively dead with the transition of k8s audit support to a plugin?

@VadimZy
Copy link
Contributor Author

VadimZy commented Aug 10, 2022

Should we bother changing any of the json_evt code at all, given that it's effectively dead with the transition of k8s audit support to a plugin?

it will exist in the code for a while, sysdig might use it for some time as well.

@@ -347,8 +364,9 @@ static shared_ptr<ast::expr> parse_condition(
}
catch (const sinsp_exception& e)
{
throw falco_exception("Compilation error when compiling \""
+ condition + "\": " + to_string(p.get_pos().col) + ": " + e.what());
throw falco_exception("Compilation error when compiling \n"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not solved, the changes of #2098 introduced a new form of error reposting. We should throw a rule_loader::rule_load_exception with proper err code here.

@@ -21,7 +21,12 @@ limitations under the License.
#include "filter_evttype_resolver.h"
#include "filter_warning_resolver.h"
#include <version.h>
#include <sstream>

#ifndef _WIN32
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not enough and that's also the reason why the CI builds are failing. We have a very weird setup in the libraries for regex support that depends on:

  • old compiler versions, that don't support <regex>
  • inclusion or not of oniguruma in the build, which is a dependency of jq

To solve all the combinations, this trick worked for me:

Mind that this would also cause the regex standard used in the code to differ, depending on what regex library we are building with. So you need to write two copies of each regex, and also handle them in two different ways:

Good news is that the CI and the tests won't pass until this is addressed: we have different build types that help us test all the cases.

Bad news is that this is truly terrible, and I really hope we'll be able to do better than this as soon as possible. The bottleneck is the presence of oniguruma brought in by jq, we we manage to remove that we should be able to just stick to one regex standard (posix I guess).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dropping fix for the list inserter altogether - I don't have resources to fight regex portability here.
@jasondellaluce keep in mind that the current list expansion routine is quite inefficient - it keeps on looking for all available lists (100s) in the continuously growing string and does it from the string beginning each time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, that's what we had in the past with Lua as well. This part will need to be substituted with proper AST traversal+substitutions in the future, however that would introduce ruleset breaking changes so it's something that we'll tackle in the future.

userspace/engine/rule_loader.cpp Outdated Show resolved Hide resolved
@poiana poiana added size/XL and removed size/XXL labels Aug 11, 2022
jasondellaluce
jasondellaluce previously approved these changes Aug 22, 2022
@poiana
Copy link
Contributor

poiana commented Aug 22, 2022

LGTM label has been added.

Git tree hash: ecf2d1a1e3036d7189b2907fc8bffbfa36438a8b

@jasondellaluce
Copy link
Contributor

/milestone 0.33.0

@jasondellaluce
Copy link
Contributor

@VadimZy now we introduced RE2. Let me know if you intend to re-introduce your list_interter now that portable regexs are available. If not, this still needs to be rebased on top of master.

…rule sets

- replace std::set<uint16_t> with fixed size vector in event types propagation
- rework lists expansion by replacing repetitive string::find in constantly growing expansion string with regex tokenization
- improve json_event parsing by moving const initializations into static routines

Signed-off-by: VadimZy <vadim.zyarko@sysdig.com>
Signed-off-by: VadimZy <vadim.zyarko@sysdig.com>
Signed-off-by: VadimZy <vadim.zyarko@sysdig.com>
Signed-off-by: VadimZy <vadim.zyarko@sysdig.com>
reverting to the inefficient code.

Signed-off-by: VadimZy <vadim.zyarko@sysdig.com>
Copy link
Contributor

@jasondellaluce jasondellaluce left a comment

Choose a reason for hiding this comment

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

I rebased this on master for you. LGTM, thanks!

/approved

@poiana poiana added the lgtm label Sep 5, 2022
@poiana
Copy link
Contributor

poiana commented Sep 5, 2022

LGTM label has been added.

Git tree hash: 074920724c1a8793867fd771a65745ee5e6bfda1

@poiana poiana added the approved label Sep 5, 2022
@poiana
Copy link
Contributor

poiana commented Sep 5, 2022

[APPROVALNOTIFIER] This PR is APPROVED

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

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


void for_each(std::function<bool(uint16_t)> consumer) const
{
for(uint16_t i = 0; i < m_types.size(); ++i)

Check failure

Code scanning / CodeQL

Comparison of narrow type with wide type in loop condition

Comparison between [i](1) of type uint16_t and [call to size](2) of wider type size_type.
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.

5 participants