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

Implement parallel ARC eviction #16486

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

allanjude
Copy link
Contributor

Sponsored-by: Expensify, Inc.
Sponsored-by: Klara, Inc.

Motivation and Context

Read and write performance can become limited by the arc_evict process being single threaded.
Additional data cannot be added to the ARC until sufficient existing data is evicted.

On many-core systems with TBs of RAM, a single thread becomes a significant bottleneck.

With the change we see a 25% increase in read and write throughput

Description

Use a new taskq to run multiple multiple arc_evict() threads at once, each given a fraction of the desired memory to reclaim

How Has This Been Tested?

Benchmarking with a full ARC to measure the performance difference.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance enhancement (non-breaking change which improves efficiency)
  • Code cleanup (non-breaking change which makes code smaller or more readable)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Library ABI change (libzfs, libzfs_core, libnvpair, libuutil and libzfsbootenv)
  • Documentation (a change to man pages or other documentation)

Checklist:

module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
@adamdmoss
Copy link
Contributor

adamdmoss commented Sep 12, 2024

I've been casually testing this out (combined with the parallel_dbuf_evict PR) over the last couple of weeks (most recently, 5b070d1 ).

I've not been hammering it hard or specifically, just letting it do its thing with my messing-around desktop system.

Hit a probable regression today, though: while mv'ing a meager 8GB of files from one pool to another, all my zfs IO got really high-latency, and an iotop showed that the copy part of the move (this being a mv across pools, so in reality it's a copy-and-remove) was running at a painful few 100KB/sec, and the zfs arc_evict thread was taking a whole core... but just one core.

In time it all cleared up and of course I can't conclusively blame this PR's changes, but I left with two fuzzy observations:

  • In many years of mucking around with ZFS I've never(?) seemed to get the 'arc_evict is pegging CPU badly' issue until I started testing this PR's changes (though I'm aware the issue occurs in the wild for folks on master/release ZFSes)
  • arc_evict was only using one core as far as I can tell, so I guess the parallelism which is the point of this PR just wasn't kicking-in for some reason anyway and/or the spinning was happening outside of the parallelized part

@0mp 0mp force-pushed the parallel_arc_evict branch from 146fe45 to e128026 Compare September 12, 2024 10:03
@0mp
Copy link
Contributor

0mp commented Sep 12, 2024

I have updated the patch with a different logic for picking the default maximum number of ARC eviction threads. The new logic aims to pick the number that is one-eighth of the available CPUs, with a minimum of 2 and a maximum of 16.

@amotin
Copy link
Member

amotin commented Sep 12, 2024

one-eighth of the available CPUs, with a minimum of 2 and a maximum of 16.

Why would we need two evict threads on a single-core system? In that case I would probably prefer to disable taskqs completely. If that is a way to make it more logarithmic, then I would think about highbit(), though then it will grow pretty slow for very large systems, so that the limit of 16 will never be reached. But I am not exactly sure the faster growth would make sense, since it may cause more lock contentions in memory allocator, etc.

@behlendorf behlendorf added the Status: Code Review Needed Ready for review and testing label Sep 13, 2024
@allanjude
Copy link
Contributor Author

one-eighth of the available CPUs, with a minimum of 2 and a maximum of 16.

Why would we need two evict threads on a single-core system? In that case I would probably prefer to disable taskqs completely. If that is a way to make it more logarithmic, then I would think about highbit(), though then it will grow pretty slow for very large systems, so that the limit of 16 will never be reached. But I am not exactly sure the faster growth would make sense, since it may cause more lock contentions in memory allocator, etc.

Right now, this is only enabled by a separate tunable, to enable multiple threads. So for the single CPU case, we don't expect it to be enabled. But for something like 4-12 core systems, we would want it to use at least 2 threads, and then grow from there, reaching 16 threads at 128 cores.

@amotin
Copy link
Member

amotin commented Sep 16, 2024

Right now, this is only enabled by a separate tunable, to enable multiple threads. So for the single CPU case, we don't expect it to be enabled.

Now that you mentioned it, I've noticed its been disabled by default. I don't like the idea to tune it manually in production depending on system size. I would prefer to to have reasonable automatic defaults.

@0mp 0mp force-pushed the parallel_arc_evict branch 2 times, most recently from b6a65a2 to e99733e Compare October 23, 2024 19:49
@0mp
Copy link
Contributor

0mp commented Oct 23, 2024

Hey! So, here's what changed in the patch:

Formula

There is now a different formula for automatically scaling the number of evict threads when the parameter is set to 0. The formula is:

MIN(MAX(max_ncpus > 6 ? 2 : 1, ilog2(max_ncpus) + (max_ncpus >> 6)), 16);

It looks like this (the x axis is the CPU count and the y axis is the evict thread count):

image

Here's also a table:

CPUs zfs_arc_evict_threads Evict threads count Using taskq?
1 0 1 (autoscaled) No
2 0 1 (autoscaled) No
5 0 1 (autoscaled) No
6 0 2 (autoscaled) Yes
1024 0 16 (autoscaled) Yes
(not using autoscaling, CPU count is irrelevant) 1 1 No
(not using autoscaling, CPU count is irrelevant) 32 32 Yes

Less parameters

zfs_arc_evict_threads is now the only parameter exposed to control the evict thread count. The zfs_arc_evict_threads_parallel has been removed in favor of enabling the use of taskqs when there are two or more evict threads.

This approach has been suggested by @tonyhutter in another PR (#16487 (comment)).

Stability improvements

It is no longer possible to modify the actual evict threads count during runtime. Since the evict taskqs are only created during arc_init(), the module saves the actual number of evict threads it is going to use and does not care about changes to zfs_arc_evict_threads from then on. This behavior has been documented in the manual page.

Copy link
Member

@amotin amotin 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 automating it. Few comments to that part, and please take a look on my earlier comments.

man/man4/zfs.4 Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
@amotin amotin added the Status: Revision Needed Changes are required for the PR to be accepted label Nov 6, 2024
@github-actions github-actions bot removed the Status: Revision Needed Changes are required for the PR to be accepted label Nov 12, 2024
module/zfs/arc.c Outdated Show resolved Hide resolved
@amotin
Copy link
Member

amotin commented Nov 19, 2024

I am not sure it is right, but it seems GCC does no like it:

  module/zfs/arc.c: In function 'arc_evict_state':
  module/zfs/arc.c:4095:15: error: 'evarg' may be used uninitialized in this function [-Werror=maybe-uninitialized]
    evict_arg_t *evarg;
                 ^~~~~

@amotin amotin added the Status: Revision Needed Changes are required for the PR to be accepted label Nov 19, 2024
@github-actions github-actions bot removed the Status: Revision Needed Changes are required for the PR to be accepted label Nov 21, 2024
@adamdmoss
Copy link
Contributor

adamdmoss commented Nov 29, 2024

I gave this another spin (not in isolation though FYI - it was along with the parallel dbuf eviction PR) and got a repeat of the previously noted behavior. Seems to not be coincidence.

In stress-testing the intended use-case (chugging through data when the arc is already full) this PR seems benign and probably even beneficial - multiple arc reaper threads are active and busy, and throughput is very healthy.

However, later just puttering around in desktop usage under quite light reads I noticed that a reading app is blocked for several seconds at a time and the experience was quite unpleasant. Lo and behold, one or more arc_evict threads were spinning hard, eventually settling down but then spinning hard again for several seconds under further light read load. This pattern repeated until I rebooted (into a zfs branch minus this PR :) ).

module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
Copy link
Member

@amotin amotin left a comment

Choose a reason for hiding this comment

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

Thanks. We are getting closer. ;)

man/man4/zfs.4 Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
@alex-stetsenko
Copy link
Contributor

Thanks. We are getting closer. ;)

Is anything left unresolved?

@amotin
Copy link
Member

amotin commented Dec 10, 2024

@alex-stetsenko I'll take another look a bit later, but meanwhile would be good to fix style issue (some line is too long), squash it all into one commit and rebase on top of master.

module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated
Comment on lines 4127 to 4129
uint_t nthreads = zfs_arc_evict_threads == 1 ?
zfs_arc_evict_threads_max :
MIN(zfs_arc_evict_threads, zfs_arc_evict_threads_max);
Copy link
Member

Choose a reason for hiding this comment

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

We could limit nthreads to num_sublists. Multiple threads per sublist might not have much sense. It should normally be true, but zfs_multilist_num_sublists if configurable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is worth checking num_sublist when zfs_arc_evict_threads_max is computed. Updated.

Copy link
Member

Choose a reason for hiding this comment

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

While it may be some better in practice by not creating extra threads, it dives into internal multilist details, which is not good. I wont insist, but I would not do it this way myself.

module/zfs/arc.c Outdated Show resolved Hide resolved
Copy link
Member

@amotin amotin left a comment

Choose a reason for hiding this comment

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

Couple more comments, but more important please take a look why no CI tests are passing. It seems they all time out for some reason. And since I saw it at least on several pushes of this PR but no others, it makes me think it is not a CI glitch, but something is wrong here.

module/zfs/arc.c Outdated
Comment on lines 4127 to 4129
uint_t nthreads = zfs_arc_evict_threads == 1 ?
zfs_arc_evict_threads_max :
MIN(zfs_arc_evict_threads, zfs_arc_evict_threads_max);
Copy link
Member

Choose a reason for hiding this comment

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

While it may be some better in practice by not creating extra threads, it dives into internal multilist details, which is not good. I wont insist, but I would not do it this way myself.

man/man4/zfs.4 Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
module/zfs/arc.c Outdated Show resolved Hide resolved
@alex-stetsenko alex-stetsenko force-pushed the parallel_arc_evict branch 3 times, most recently from 2818d9c to c7e84a8 Compare December 12, 2024 18:28
@amotin
Copy link
Member

amotin commented Dec 13, 2024

@alex-stetsenko The CI still looks very unhappy, but now I have no guesses why.

Read and write performance can become limited by the arc_evict
process being single threaded. Additional data cannot be added
to the ARC until sufficient existing data is evicted.

On many-core systems with TBs of RAM, a single thread becomes
a significant bottleneck.

With the change we see a 25% increase in read and write throughput

Sponsored-by: Expensify, Inc.
Sponsored-by: Klara, Inc.
Co-authored-by: Allan Jude <[email protected]>
Co-authored-by: Mateusz Piotrowski <[email protected]>
Signed-off-by: Alexander Stetsenko <[email protected]>
Signed-off-by: Allan Jude <[email protected]>
Signed-off-by: Mateusz Piotrowski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Code Review Needed Ready for review and testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants