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

feat: add a function to get number of logical cpu cores #4880

Closed

Conversation

Lloyd-Pottiger
Copy link
Contributor

@Lloyd-Pottiger Lloyd-Pottiger commented May 12, 2022

What problem does this PR solve?

Issue Number: close #4879

Problem Summary:

What is changed and how it works?

Add a function to get number of logical cpu cores:

  1. Normally, $cpu = std::thread::hardware_concurrency()
  2. With cgroup, $cpu = min(len(cpuset), cgroup_quota / cgroup_period)

detail doc: #4879 (comment)

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 12, 2022
@Lloyd-Pottiger
Copy link
Contributor Author

Lloyd-Pottiger commented May 12, 2022

TODO:

  • add unit tests (if gtest support) (Can not mock the env with different logical cpu, so delete)
  • test on k8s/docker env
  • support cgroup v2 limitation

@Lloyd-Pottiger
Copy link
Contributor Author

Lloyd-Pottiger commented May 16, 2022

Test on docker with --cpus=$n, the result of getNumberOfLogicalCPUCores equal to $n.
Test on WSL, the result is the same as expected.

@ywqzzy ywqzzy self-requested a review May 17, 2022 03:53
@Lloyd-Pottiger Lloyd-Pottiger changed the title [WIP]feat: add a function to get number of logical cpu cores feat: add a function to get number of logical cpu cores May 17, 2022
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 17, 2022
dbms/src/Common/getNumberOfLogicalCPUCores.cpp Outdated Show resolved Hide resolved
dbms/src/Common/getNumberOfLogicalCPUCores.cpp Outdated Show resolved Hide resolved
dbms/src/Common/getNumberOfLogicalCPUCores.cpp Outdated Show resolved Hide resolved
@Lloyd-Pottiger
Copy link
Contributor Author

/rebuild

Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
@Lloyd-Pottiger
Copy link
Contributor Author

Signed-off-by: Lloyd-Pottiger <[email protected]>
@Lloyd-Pottiger
Copy link
Contributor Author

It does not work in some case…… I will write a doc and do more tests.

@breezewish
Copy link
Member

Is it easy to add tests for it? How about setting up a docker, or more directly, just use cgroup in tests?

@Lloyd-Pottiger
Copy link
Contributor Author

Is it easy to add tests for it? How about setting up a docker, or more directly, just use cgroup in tests?

Currently, I did not find the way to test by gtests. I tested manually before. (I did not test on cgroup v2 because I still could not set up the environment after trying a lot on my own machine) Do you mean run a docker in a unit test? Maybe it is hard, and is not a good way which means we may need to introduce more dependencies. However, there are some bugs indeed. I will fix later.

@Lloyd-Pottiger
Copy link
Contributor Author

I have wrote a doc about this pr in #4879 (comment).

Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
Signed-off-by: Lloyd-Pottiger <[email protected]>
@Lloyd-Pottiger
Copy link
Contributor Author

std::getline(infile, line);
unsigned cpu_count = 0;
size_t first = 0;
while (first < line.size())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change it to for(line by line)?

return std::min(cpuset_count, quota_count);
}

static unsigned read_cpuset_count_from(const char * filename, unsigned default_value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you like to add some gtests to test all of functions?

You can find some example in man.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will

@Lloyd-Pottiger Lloyd-Pottiger deleted the feat-get-logical-cpu-cores branch June 10, 2022 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add a function to get number of logical cpu cores
7 participants