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

chore: scaffolding for enabling code formatting #2038

Merged
merged 3 commits into from
Sep 6, 2024

Conversation

Andreagit97
Copy link
Member

What type of PR is this?

/kind feature

Any specific area of the project related to this PR?

/area CI

Does this PR require a change in the driver versions?

No

What this PR does / why we need it:

This PR is the first step to introduce code formatting in this repo.
These are the steps that I see to have code formatting ready and working on this repo.

  1. Merge this PR into master. the format CI job will fail but it won't be required.
  2. Wait until almost all PRs needed for the next libs/drivers tag are merged in master.
  3. Open a new PR that formats all the code in just one commit with pre-commit run --all-files (same check used in CI).
  4. Once the above PR is merged, open a new pull request to add the formatting commit to the .git-blame-ignore-revs file (this will ignore this commit in the git blame)
  5. Set the format job as required in test-infra
  6. [OPTIONAL] Add an extra CI job to print a pretty message when the formatting fails (see an example here TRIGGER_FORMAT_AND_LINT_FAILURE Andreagit97/tmp_libs#3 (comment))

This workstream is pretty delicate because it requires:

  • to be done before a git tag to avoid cherry-pick issues with release branches.
  • a rebase from all contributors

Since the clang-tidy support can be done at any point in time during the dev cycle, I removed its support from this PR, hoping to simplify this formatting workstream as much as possible. BTW you can see here a possible clang-tidy integration with our repo Andreagit97/tmp_libs#3 (review) (with the corresponding job https://github.com/Andreagit97/tmp_libs/blob/master/.github/workflows/lint.yml)

Which issue(s) this PR fixes:

Fixes #381

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@Andreagit97
Copy link
Member Author

As you can see the format job is failing, and it has uploaded the diff in the github summary https://github.com/falcosecurity/libs/actions/runs/10618227746/artifacts/1870627029

Copy link

github-actions bot commented Aug 29, 2024

Perf diff from master - unit tests

     9.67%     +1.56%  [.] sinsp_parser::reset
     6.27%     -0.90%  [.] sinsp_evt::get_type
     1.66%     -0.83%  [.] sinsp::fetch_next_event
     2.21%     -0.70%  [.] std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release
     0.44%     +0.67%  [.] std::_Hashtable<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libsinsp::state::dynamic_struct::field_info>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libsinsp::state::dynamic_struct::field_info> >, std::__detail::_Select1st, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::find
     1.28%     +0.64%  [.] std::_Hashtable<long, std::pair<long const, std::shared_ptr<sinsp_threadinfo> >, std::allocator<std::pair<long const, std::shared_ptr<sinsp_threadinfo> > >, std::__detail::_Select1st, std::equal_to<long>, std::hash<long>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::_M_find_before_node
     0.82%     +0.58%  [.] 0x00000000000e93c0
     4.17%     -0.53%  [.] gzfile_read
     0.95%     -0.48%  [.] std::_Hashtable<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__detail::_Identity, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, true, true> >::find
     1.44%     -0.45%  [.] libsinsp::sinsp_suppress::process_event

Heap diff from master - unit tests

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

Heap diff from master - scap file

peak heap memory consumption: 0B
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.0074         +0.0074           145           147           145           146
BM_sinsp_split_median                                          +0.0131         +0.0132           145           147           145           147
BM_sinsp_split_stddev                                          +0.6026         +0.5950             1             2             1             2
BM_sinsp_split_cv                                              +0.5909         +0.5832             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_mean                  -0.0221         -0.0221            42            41            42            41
BM_sinsp_concatenate_paths_relative_path_median                -0.0242         -0.0242            42            41            42            41
BM_sinsp_concatenate_paths_relative_path_stddev                +0.2198         +0.2218             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_cv                    +0.2473         +0.2493             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_mean                     -0.0163         -0.0163            17            17            17            17
BM_sinsp_concatenate_paths_empty_path_median                   -0.0155         -0.0155            17            17            17            17
BM_sinsp_concatenate_paths_empty_path_stddev                   -0.0302         -0.0306             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_cv                       -0.0141         -0.0146             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_mean                  +0.0590         +0.0590            43            45            43            45
BM_sinsp_concatenate_paths_absolute_path_median                +0.0712         +0.0712            43            46            43            46
BM_sinsp_concatenate_paths_absolute_path_stddev                +5.2584         +5.2542             0             1             0             1
BM_sinsp_concatenate_paths_absolute_path_cv                    +4.9099         +4.9058             0             0             0             0
BM_sinsp_split_container_image_mean                            +0.0185         +0.0186           349           355           349           355
BM_sinsp_split_container_image_median                          +0.0187         +0.0187           350           356           349           356
BM_sinsp_split_container_image_stddev                          -0.0417         -0.0458             3             3             3             3
BM_sinsp_split_container_image_cv                              -0.0591         -0.0632             0             0             0             0

Copy link

codecov bot commented Aug 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.31%. Comparing base (e2c5174) to head (ef6e08b).
Report is 33 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2038   +/-   ##
=======================================
  Coverage   74.31%   74.31%           
=======================================
  Files         253      253           
  Lines       30966    30966           
  Branches     5403     5421   +18     
=======================================
  Hits        23011    23011           
- Misses       7942     7951    +9     
+ Partials       13        4    -9     
Flag Coverage Δ
libsinsp 74.31% <ø> (ø)

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.

Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
BreakBeforeBraces: Allman
BasedOnStyle: Google
AccessModifierOffset: -4
BreakBeforeBraces: Attach
Copy link
Member Author

Choose a reason for hiding this comment

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

this would mean:

struct finfo {
	file_info_t info;
	char16_t filename[256];
};

Instead of:

struct finfo 
{
	file_info_t info;
	char16_t filename[256];
};

IMO this should improve our code readability. Moreover, it would also avoid noisy frictions when we switch from other languages like Go/Rust. But this is just my opinion, let me know :)

AllowShortLoopsOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 100
Copy link
Member Author

Choose a reason for hiding this comment

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

Seems reasonable but again let me know

Copy link
Member Author

Choose a reason for hiding this comment

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

You can see an example of our repo formatted with this style here https://github.com/Andreagit97/tmp_libs/blob/master

Comment on lines +8 to +23
"_help_line_width": [
"How wide to allow formatted cmake files"
],
"line_width": 100,
"_help_tab_size": [
"How many spaces to tab for indent"
],
"tab_size": 4,
"_help_use_tabchars": [
"If true, lines are indented using tab characters (utf-8",
"0x09) instead of <tab_size> space characters (utf-8 0x20).",
"In cases where the layout would require a fractional tab",
"character, the behavior of the fractional indentation is",
"governed by <fractional_tab_policy>"
],
"use_tabchars": true,
Copy link
Member Author

Choose a reason for hiding this comment

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

these are the most important ones

@@ -0,0 +1,3 @@
# This commit has completely rewritten the history of file `userspace/libscap/engine/savefile/scap_savefile.c`.
# https://github.com/falcosecurity/libs/commit/a6df8fc90e3be0209c5a5f9bb0443077cd5d0967
Copy link
Member Author

Choose a reason for hiding this comment

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

I figured out that this commit completely overwrites the blame of userspace/libscap/engine/savefile/scap_savefile.c, so it seems reasonable to put it here


- name: Upload the git diff artifact 📦
if: failure()
uses: actions/upload-artifact@v3
Copy link
Member Author

Choose a reason for hiding this comment

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

at the moment we just upload the patch file.

Makefile Outdated
Copy link
Member Author

Choose a reason for hiding this comment

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

this should be used locally if we want to run the tools manually without pre-commit

Copy link
Member Author

Choose a reason for hiding this comment

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

do you have in mind other files that we need to exclude?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes all the driver syscalls tables for all architectures. Also we need to update syscalls-bumper to add the autogenerated tag to those files.
Isn't there s way to specify a lost of "DO NOT FORMAT" files in the main clang formatter config file instead? It would be much easier.

Copy link
Member Author

Choose a reason for hiding this comment

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

when I initially worked on that 2 years ago there was no way to do that, but maybe today is our lucky day because i found this llvm/llvm-project#76327, but before saying this is a lucky day I need to check the supported clang-version for this...

Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

@Andreagit97
Copy link
Member Author

Ok now we use .clang-format-ignore 🚀 unfortunately this is only supported starting from clang 18.0.0 so we need to increase the minimum clang version required, but I don't see it as a big issue since pre-commit automatically installs the clang version we want!

Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
@Andreagit97
Copy link
Member Author

Added the new line config at the end of each file

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

/approve
This is GREAT!

@poiana
Copy link
Contributor

poiana commented Sep 6, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, 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

@poiana
Copy link
Contributor

poiana commented Sep 6, 2024

LGTM label has been added.

Git tree hash: c2d923a09862a4ba71942dca24552d236f8a644c

@poiana poiana merged commit 0701271 into falcosecurity:master Sep 6, 2024
45 of 47 checks passed
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.

Coding style for libs repo
4 participants