memory page_alloc zonelist caching speedup
Subsystems (6)
Optimize the critical zonelist scanning for free pages in the kernel memory allocator by caching the zones that were found to be full recently, and skipping them.
Remembers the zones in a zonelist that were short of free memory in the last second. And it stashes a zone-to-node table in the zonelist struct, to optimize that conversion (minimize its cache footprint.)
Recent changes:
This differs in a significant way from a similar patch that I posted a week ago. Now, instead of having a nodemask_t of recently full nodes, I have a bitmask of recently full zones. This solves a problem that last weeks patch had, which on systems with multiple zones per node (such as DMA zone) would take seeing any of these zones full as meaning that all zones on that node were full.
Also I changed names - from "zonelist faster" to "zonelist cache", as that seemed to better convey what we're doing here - caching some of the key zonelist state (for faster access.)
See below for some performance benchmark results. After all that discussion with David on why I didn't need them, I went and got some ;). I wanted to verify that I had not hurt the normal case of memory allocation noticeably. At least for my one little microbenchmark, I found (1) the normal case wasn't affected, and
(2) workloads that forced scanning across multiple nodes for memory improved up to 10% fewer System CPU cycles and lower elapsed clock time ('sys' and 'real'). Good. See details, below.
I didn't have the logic in get_page_from_freelist() for various full nodes and zone reclaim failures correct. That should be fixed up now - notice the new goto labels zonelist_scan, this_zone_full, and try_next_zone, in get_page_from_freelist().
There are two reasons I persued this alternative, over some earlier proposals that would have focused on optimizing the fake numa emulation case by caching the last useful zone:
1) Contrary to what I said before, we (SGI, on large ia64 sn2 systems) have seen real customer loads where the cost to scan the zonelist was a problem, due to many nodes being full of memory before we got to a node we could use. Or at least, I think we have. This was related to me by another engineer, based on experiences from some time past. So this is not guaranteed. Most likely, though.
The following approach should help such real numa systems just as much as it helps fake numa systems, or any combination thereof.
2) The effort to distinguish fake from real numa, using node_distance, so that we could cache a fake numa node and optimize choosing it over equivalent distance fake nodes, while continuing to properly scan all real nodes in distance order, was going to require a nasty blob of zonelist and node distance munging.
The following approach has no new dependency on node distances or zone sorting.
See comment in the patch below for a description of what it actually does.
Technical details of note (or controversy):
- See the use of "zlc_active" and "did_zlc_setup" below, to delay adding any work for this new mechanism until we've looked at the first zone in zonelist. I figured the odds of the first zone having the memory we needed were high enough that we should just look there, first, then get fancy only if we need to keep looking.
- Some odd hackery was needed to add items to struct zonelist, while not tripping up the custom zonelists built by the mm/mempolicy.c code for MPOL_BIND. My usual wordy comments below explain this. Search for "MPOL_BIND".
- Some per-node data in the struct zonelist is now modified frequently, with no locking. Multiple CPU cores on a node could hit and mangle this data. The theory is that this is just performance hint data, and the memory allocator will work just fine despite any such mangling. The fields at risk are the struct 'zonelist_cache' fields 'fullzones' (a bitmask) and 'last_full_zap' (unsigned long jiffies). It should all be self correcting after at most a one second delay.
- This still does a linear scan of the same lengths as before. All I've optimized is making the scan faster, not algorithmically shorter. It is now able to scan a compact array of 'unsigned short' in the case of many full nodes, so one cache line should cover quite a few nodes, rather than each node hitting another one or two new and distinct cache lines.
- If both Andi and Nick don't find this too complicated, I will be (pleasantly) flabbergasted.
- I removed the comment claiming we only use one cachline's worth of zonelist. We seem, at least in the fake numa case, to have put the lie to that claim.
- I pay no attention to the various watermarks and such in this performance hint. A node could be marked full for one watermark, and then skipped over when searching for a page using a different watermark. I think that's actually quite ok, as it will tend to slightly increase the spreading of memory over other nodes, away from a memory stressed node.
===============
Performance - some benchmark results and analysis:
This benchmark runs a memory hog program that uses multiple threads to touch alot of memory as quickly as it can.
Multiple runs were made, touching 12, 38, 64 or 90 GBytes out of the total 96 GBytes on the system, and using 1, 19, 37, or 55 threads (on a 56 CPU system.) System, user and real (elapsed) timings were recorded for each run, shown in units of seconds, in the table below.
Two kernels were tested - 2.6.18-mm3 and the same kernel with this zonelist caching patch added. The table also shows the difference in timings, between these two kernels, and what percentage improvement the zonelist caching sys time is over (lower than) the stock *-mm kernel.
number 2.6.18-mm3 zonelist-cache delta (< 0 good) percent
GBs N ------------ -------------- ---------------- systime
mem threads sys user real sys user real sys user real better
12 1 153 24 177 151 24 176 -2 0 -1 1%
12 19 99 22 8 99 22 8 0 0 0 0% 12 37 111 25 6 112 25 6 1 0 0 -0% 12 55 115 25 5 110 23 5 -5 -2 0 4% 38 1 502 74 576 497 73 570 -5 -1 -6 0% 38 19 426 78 48 373 76 39 -53 -2 -9 12% 38 37 544 83 36 547 82 36 3 -1 0 -0% 38 55 501 77 23 511 80 24 10 3 1 -1% 64 1 917 125 1042 890 124 1014 -27 -1 -28 2% 64 19 1118 138 119 965 141 103 -153 3 -16 13% 64 37 1202 151 94 1136 150 81 -66 -1 -13 5% 64 55 1118 141 61 1072 140 58 -46 -1 -3 4% 90 1 1342 177 1519 1275 174 1450 -67 -3 -69 4% 90 19 2392 199 192 2116 189 176 -276 -10 -16 11% 90 37 3313 238 175 2972 225 145 -341 -13 -30 10% 90 55 1948 210 104 1843 213 100 -105 3 -4 5%
Notes:
1) This test ran a memory hog program that started a specified number N of threads, and had each thread allocate and touch 1/N'th of the total memory to be used in the test run in a single loop, writing a constant word to memory, one store every 4096 bytes. Watching this test during some earlier trial runs, I would see each of these threads sit down on one CPU and stay there, for the remainder of the pass, a different CPU for each thread.
2) The 'real' column is not comparable to the 'sys' or 'user' columns. The 'real' column is seconds wall clock time elapsed, from beginning to end of that test pass. The 'sys' and 'user' columns are total CPU seconds spent on that test pass. For a 19 thread test run, for example, the sum of 'sys' and 'user' could be up to 19 times the number of 'real' elapsed wall clock seconds.
3) Tests were run on a fresh, single-user boot, to minimize the amount of memory already in use at the start of the test, and to minimize the amount of background activity that might interfere.
4) Tests were done on a 56 CPU, 28 Node system with 96 GBytes of RAM.
5) Notice that the 'real' time gets large for the single thread runs, even though the measured 'sys' and 'user' times are modest. I'm not sure what that means - probably something to do with it being slow for one thread to be accessing memory along ways away. Perhaps the fake numa system, running ostensibly the same workload, would not show this substantial degradation of 'real' time for one thread on many nodes -- lets hope not.
6) The high thread count passes (one thread per CPU - on 55 of 56 CPUs) ran quite efficiently, as one might expect. Each pair of threads needed to allocate and touch the memory on the node the two threads shared, a pleasantly parallizable workload.
7) The intermediate thread count passes, when asking for alot of memory forcing them to go to a few neighboring nodes, improved the most with this zonelist caching patch.
Conclusions:
* This zonelist cache patch probably makes little difference one way or the other for most workloads on real numa hardware, if those workloads avoid heavy off node allocations.
* For memory intensive workloads requiring substantial off-node allocations on real numa hardware, this patch improves both kernel and elapsed timings up to ten per-cent.
* For fake numa systems, I'm optimistic, but will have to leave that up to Rohit Seth to actually test (once I get him a 2.6.18 backport.)
===============
---
Andrew,
This patch applies to 2.6.18-mm3, plus the patches:
memory page alloc minor cleanups memory page_alloc revert empty zonelist check
The non-NUMA build problem you (Andrew) hit Monday is fixed.
include/linux/cpuset.h | 2 include/linux/mmzone.h | 85 ++++++++++++++++++++-- mm/mempolicy.c | 2 mm/page_alloc.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 265 insertions(+), 12 deletions(-)
include/linux/mmzone.h +80 -5
@@ -315,19 +315,94 @@ struct zone {*/ #define DEF_PRIORITY 12 +/* Maximum number of zones on a zonelist */ +#define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES) + +#ifdef CONFIG_NUMA +/* +* We cache key information from each zonelist for smaller cache +* footprint when scanning for free pages in get_page_from_freelist(). +* +* 1) The BITMAP fullzones tracks which zones in a zonelist have come +* up short of free memory since the last time (last_fullzone_zap) +* we zero'd fullzones. +* 2) The array z_to_n[] maps each zone in the zonelist to its node +* id, so that we can efficiently evaluate whether that node is +* set in the current tasks mems_allowed. +* +* Both fullzones and z_to_n[] are one-to-one with the zonelist, +* indexed by a zones offset in the zonelist zones[] array. +* +* The get_page_from_freelist() routine does two scans. During the +* first scan, we skip zones whose corresponding bit in 'fullzones' +* is set or whose corresponding node in current->mems_allowed (which +* comes from cpusets) is not set. During the second scan, we bypass +* this zonelist_cache, to ensure we look methodically at each zone. +* +* Once per second, we zero out (zap) fullzones, forcing us to +* reconsider nodes that might have regained more free memory. +* The field last_full_zap is the time we last zapped fullzones. +* +* This mechanism reduces the amount of time we waste repeatedly +* reexaming zones for free memory when they just came up low on +* memory momentarilly ago. +* +* The zonelist_cache struct members logically belong in struct +* zonelist. However, the mempolicy zonelists constructed for +* MPOL_BIND are intentionally variable length (and usually much +* shorter). A general purpose mechanism for handling structs with +* multiple variable length members is more mechanism than we want +* here. We resort to some special case hackery instead. +* +* The MPOL_BIND zonelists don't need this zonelist_cache (in good +* part because they are shorter), so we put the fixed length stuff +* at the front of the zonelist struct, ending in a variable length +* zones[], as is needed by MPOL_BIND. +* +* Then we put the optional zonelist cache on the end of the zonelist +* struct. This optional stuff is found by a 'zlcache_ptr' pointer in +* the fixed length portion at the front of the struct. This pointer +* both enables us to find the zonelist cache, and in the case of +* MPOL_BIND zonelists, (which will just set the zlcache_ptr to NULL) +* to know that the zonelist cache is not there. +* +* The end result is that struct zonelists come in two flavors: +* 1) The full, fixed length version, shown below, and +* 2) The custom zonelists for MPOL_BIND. +* The custom MPOL_BIND zonelists have a NULL zlcache_ptr and no zlcache. +* +* Even though there may be multiple CPU cores on a node modifying +* fullzones or last_full_zap in the same zonelist_cache at the same +* time, we don't lock it. This is just hint data - if it is wrong now +* and then, the allocator will still function, perhaps a bit slower. +*/ + + +struct zonelist_cache { + DECLARE_BITMAP(fullzones, MAX_ZONES_PER_ZONELIST); /* zone full? */ +unsigned short z_to_n[MAX_ZONES_PER_ZONELIST ];/* zone->nid */ +unsigned long last_full_zap;/* when last zap'd (jiffies) */ +}; +#else +struct zonelist_cache ; +#endif +/* * One allocation request operates on a zonelist. A zonelist * is a list of zones, the first one is the 'goal' of the * allocation, the other zones are fallback zones, in decreasing * priority. * -* Right now a zonelist takes up less than a cacheline. We never -* modify it apart from boot-up, and only a few indices are used, -* so despite the zonelist table being relatively big, the cache -* footprint of this construct is very small. +* If zlcache_ptr is not NULL, then it is just the address of zlcache, +* as explained above. If zlcache_ptr is NULL, there is no zlcache. */ +struct zonelist { -struct zone *zones[MAX_NUMNODES *MAX_NR_ZONES +1 ];// NULL delimited +struct zonelist_cache *zlcache_ptr;// NULL or &zlcache +struct zone *zones[MAX_ZONES_PER_ZONELIST +1 ];// NULL delimited +#ifdef CONFIG_NUMA +struct zonelist_cache zlcache;// optional ... +#endif };#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
mm/page_alloc.c +181 -7
245 lines not on this page · raw message
mm/mempolicy.c +2 -0
@@ -141,9 +141,11 @@ static struct zonelist *bind_zonelist(noenum zone_type k; max =1 +MAX_NR_ZONES *nodes_weight (*nodes); + max++;/* space for zlcache_ptr (see mmzone.h) */ zl =kmalloc (sizeof (struct zone *) * max,GFP_KERNEL );if (!zl)return NULL ; + zl->zlcache_ptr =NULL ; num =0 ;/* First put in the highest zones from all nodes, then all the next lower zones etc. Avoid empty zones because the memory allocator
include/linux/cpuset.h +2 -0
@@ -23,6 +23,7 @@ extern void cpuset_fork(struct task_struextern void cpuset_exit (struct task_struct *p);extern cpumask_t cpuset_cpus_allowed (struct task_struct *p);extern nodemask_t cpuset_mems_allowed (struct task_struct *p); +#define cpuset_current_mems_allowed (current->mems_allowed)void cpuset_init_current_mems_allowed (void );void cpuset_update_task_memory_state (void );#define cpuset_nodes_subset_current_mems_allowed (nodes) \
@@ -83,6 +84,7 @@ static inline nodemask_t cpuset_mems_allreturn node_possible_map; } +#define cpuset_current_mems_allowed (node_online_map)static inline void cpuset_init_current_mems_allowed (void ) {}static inline void cpuset_update_task_memory_state (void ) {}#define cpuset_nodes_subset_current_mems_allowed (nodes) (1)
Signature · 9 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>Quoted 7 lines
1) Contrary to what I said before, we (SGI, on large ia64 sn2 systems) have seen real customer loads where the cost to scan the zonelist was a problem, due to many nodes being full of memory before we got to a node we could use. Or at least, I think we have. This was related to me by another engineer, based on experiences from some time past. So this is not guaranteed. Most likely, though.
I think some more precise numbers would be appreciated before doing such changes.
Quoted 4 lines
- Some per-node data in the struct zonelist is now modified frequently, with no locking. Multiple CPU cores on a node could hit and mangle this data. The theory is that this is just performance hint data, and the memory allocator will work just fine despite any such mangling.
Yes but you will add latencies for cache line bounces won't you? The old zone lists were completely read only. That is what worries me most.
-Andi
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 2 lines · Andi
I think some more precise numbers would be appreciated before doing such changes.
Aren't there more precise numbers further down in the message to which you were responding?
Quoted 3 lines
Yes but you will add latencies for cache line bounces won't you? The old zone lists were completely read only. That is what worries me most.
There is one zonelist_cache per node, added at the end of the regular zonelist array for each node. It will have a few words updated typically at the rate of once per second by the CPUs sharing that node.
Quoted 1 line
From what my tests showed, and from what I'd expect, updating a few
node local words per second is not a problem.
If you have a particular architecture in mind for which the tradeoffs in the proposed patch don't seem right, could you spell out that architecture a bit, so we can sensibly consider whether some refinements to this patch will suit that architecture better?
Certainly, from the numbers that I have, and by private email that Rohit Seth has on a different architecture, this patch is neutral or a win for the cases we considered, depending on the degree of node locality of the memory accesses.
Signature · 9 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.925.600.0401
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>Quoted 4 lines · Paul Jackson
Quoted 3 lines · Andi
Yes but you will add latencies for cache line bounces won't you? The old zone lists were completely read only. That is what worries me most.
Paul, I think Andi is concerned that we will have a heavily shared cache line which now becomes frequently invalidated. That invalidate is quite expensive compared to just letting the lines quietly leave the cpus cache and refetching them.
Thanks,
Robin
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 2 lines · Robin
Paul, I think Andi is concerned that we will have a heavily shared cache line which now becomes frequently invalidated.
Once a second invalidation of a few cache lines that are only accessed from one node beats once per memory allocation cross-node accesses of cache lines invalidated every free.
Big time.
Signature · 9 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.925.600.0401
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>And note that in the vast majority of cases, where the requested page is available on the local node, there is no difference.
Only if we are forced to start scanning other nodes do we examine, and once per-second zap, our node local cache of whether the other nodes we scan were recently found to be full.
The classic parts of the zonelist zone arrays are still intact, read-only, at the front of the struct zonelist.
The compact, node-local, periodically written zonelist cache is added at the end of the zonelist struct, most likely in its own cache lines, and lets us avoid node remote access to a larger set of more frequently updated cache lines on each allocation request.
Only on configs with small, but greater than one, MAX_NUMNODES, is there any chance of the zonelist cache being on the same cache line as the class zone array. As best as I can tell from a quick grep, that would be avr32, arm/collie, powerpc/cell, and powerpc/pseries, which have NODES_SHIFT values between 2 and 4, and various L1 cache line sizes between 16 and 128 bytes.
Perhaps I should add a ____cacheline_aligned qualifier to the zonelist_cache struct, for these arch's?
Signature · 9 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.925.600.0401
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>As discussed in a separate lkml thread:
Avoid allocating during interleave from almost full nodes
the suggestion was made by Andrew and Christoph to consider replacing the wall clock based zapping of this zonelist cache with something based on the rate of vm paging activity, such as perhaps the counters. PGALLOC_* and PGSCAN_* (see further vmstat.h).
But it will be a few weeks before I can get to doing this; I've got to do some other stuff first.
I'm assuming that, as a practical matter, for the short term, either "time base" works, so that this is not an urgent change.
Of course, if any lurkers want to jump in and do this sooner, have at it.
Signature · 9 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.925.600.0401
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>