Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1603661 > unrolled thread
| Started by | Tim Murray <timmurray@google.com> |
|---|---|
| First post | 2017-03-18 00:30 +0100 |
| Last post | 2017-03-20 09:20 +0100 |
| Articles | 10 — 6 participants |
Back to article view | Back to linux.kernel
[RFC 0/1] add support for reclaiming priorities per mem cgroup Tim Murray <timmurray@google.com> - 2017-03-18 00:30 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Minchan Kim <minchan@kernel.org> - 2017-03-20 07:00 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Vinayak Menon <vinmenon@codeaurora.org> - 2017-03-20 15:20 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Johannes Weiner <hannes@cmpxchg.org> - 2017-03-20 17:00 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Vinayak Menon <vinmenon@codeaurora.org> - 2017-03-22 13:30 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Tim Murray <timmurray@google.com> - 2017-03-21 18:20 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Minchan Kim <minchan@kernel.org> - 2017-03-22 05:50 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Minchan Kim <minchan@kernel.org> - 2017-03-22 06:30 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup peter enderborg <peter.enderborg@sonymobile.com> - 2017-03-20 08:10 +0100
Re: [RFC 0/1] add support for reclaiming priorities per mem cgroup Kyungmin Park <kmpark@infradead.org> - 2017-03-20 09:20 +0100
| From | Tim Murray <timmurray@google.com> |
|---|---|
| Date | 2017-03-18 00:30 +0100 |
| Subject | [RFC 0/1] add support for reclaiming priorities per mem cgroup |
| Message-ID | <tm9tL-5Ls-3@gated-at.bofh.it> |
Hi all, I've been working to improve Android's memory management and drop lowmemorykiller from the kernel, and I'd like to get some feedback on a small patch with a lot of side effects. Currently, when an Android device is under memory pressure, one of three things will happen from kswapd: 1. Compress an anonymous page to ZRAM. 2. Evict a file page. 3. Kill a process via lowmemorykiller. The first two are cheap and per-page, the third is relatively cheap in the short term, frees many pages, and may cause power and performance penalties later on when the process has to be started again. For lots of reasons, I'd like a better balance between reclamation and killing on Android. One of the nice things about Android from an optimization POV is that the execution model is more constrained than a generic Linux machine. There are only a limited number of processes that need to execute quickly for the device to appear to have good performance, and a userspace daemon (called ActivityManagerService) knows exactly what those processes are at any given time. We've made use of that in the past via cpusets and schedtune to limit the CPU resources available to background processes, and I think we can apply the same concept to memory. This patch adds a new tunable to mem cgroups, memory.priority. A mem cgroup with a non-zero priority will not be eligible for scanning until the scan_control's priority is greater than zero. Once the mem cgroup is eligible for scanning, the priority acts as a bias to reduce the number of pages that should be scanned. We've seen cases on Android where the global LRU isn't sufficient. For example, notifications in Android are rendered as part of a separate process that runs infrequently. However, when a notification appears and the user slides down the notification tray, we'll often see dropped frames due to page faults if there has been severe memory pressure. There are similar issues with other persistent processes. The goal on an Android device is to aggressively evict from very low-priority background tasks that are likely to be killed anyway, since this will reduce the likelihood of lowmemorykiller running in the first place. It will still evict some from foreground and persistent processes, but it should help ensure that background processes are effectively reduced to the size of their heaps before evicting from more critical tasks. This should mean fewer background processes end up killed, which should improve performance and power on Android across the board (since it costs significantly less to page things back in than to replay the entirety of application startup). The follow-on that I'm also experimenting with is how to improve vmpressure such that userspace can have some idea when low-priority memory cgroups are about as small as they can get. The correct time for Android to kill a background process under memory pressure is when there is evidence that a process has to be killed in order to alleviate memory pressure. If the device is below the low memory watermark and we know that there's probably no way to reclaim any more from background processes, then a userspace daemon should kill one or more background processes to fix that. Per-cgroup priority could be the first step toward that information. I've tested a version of this patch on a Pixel running 3.18 along with an overhauled version of lmkd (the Android userspace lowmemorykiller daemon), and it does seem to work fine. I've ported it forward but have not yet rigorously tested it at TOT, since I don't have an Android test setup running TOT. While I'm getting my tests ported over, I would like some feedback on adding another tunable as well as what the tunable's interface should be--I really don't like the 0-10 priority scheme I have in the patch but I don't have a better idea. Thanks, Tim Tim Murray (1): mm, memcg: add prioritized reclaim include/linux/memcontrol.h | 20 +++++++++++++++++++- mm/memcontrol.c | 33 +++++++++++++++++++++++++++++++++ mm/vmscan.c | 3 ++- 3 files changed, 54 insertions(+), 2 deletions(-) -- 2.12.0.367.g23dc2f6d3c-goog
[toc] | [next] | [standalone]
| From | Minchan Kim <minchan@kernel.org> |
|---|---|
| Date | 2017-03-20 07:00 +0100 |
| Message-ID | <tmYwh-c3-1@gated-at.bofh.it> |
| In reply to | #1603661 |
Hello, On Fri, Mar 17, 2017 at 04:16:35PM -0700, Tim Murray wrote: > Hi all, > > I've been working to improve Android's memory management and drop lowmemorykiller from the kernel, and I'd like to get some feedback on a small patch with a lot of side effects. > > Currently, when an Android device is under memory pressure, one of three things will happen from kswapd: > > 1. Compress an anonymous page to ZRAM. > 2. Evict a file page. > 3. Kill a process via lowmemorykiller. > > The first two are cheap and per-page, the third is relatively cheap in the short term, frees many pages, and may cause power and performance penalties later on when the process has to be started again. For lots of reasons, I'd like a better balance between reclamation and killing on Android. > > One of the nice things about Android from an optimization POV is that the execution model is more constrained than a generic Linux machine. There are only a limited number of processes that need to execute quickly for the device to appear to have good performance, and a userspace daemon (called ActivityManagerService) knows exactly what those processes are at any given time. We've made use of that in the past via cpusets and schedtune to limit the CPU resources available to background processes, and I think we can apply the same concept to memory. > AFAIK, many platforms as well as android have done it. IOW, they know what processes are important while others are not critical for user-response. > This patch adds a new tunable to mem cgroups, memory.priority. A mem cgroup with a non-zero priority will not be eligible for scanning until the scan_control's priority is greater than zero. Once the mem cgroup is eligible for scanning, the priority acts as a bias to reduce the number of pages that should be scanned. First of all, the concept makes sense to me. The problem with cgroup-per-app model is that it's really hard to predict how many of memory a group needs to make system smooth although we know what processes are important. Because of it, it's hard to tune memcg low/high/max proactively. So, it would be great if admin can give more priority some groups like graphic mamager, laucher and killer applications like TV manager, Dial manager and so (ie, when memory pressure happens, please reclaim more pages from low priority groups). However, I'm not sure your approach is good. It seems your approach just reclaims pages from groups (DEF_PRIORITY - memcg->priority) >= sc->priority. IOW, it is based on *temporal* memory pressure fluctuation sc->priority. Rather than it, I guess pages to be reclaimed should be distributed by memcg->priority. Namely, if global memory pressure happens and VM want to reclaim 100 pages, VM should reclaim 90 pages from memcg-A(priority-10) and 10 pages from memcg-B(prioirty-90). Anyway, it's really desireble approach so memcg maintainers, Please, have a look. Thanks. > > We've seen cases on Android where the global LRU isn't sufficient. For example, notifications in Android are rendered as part of a separate process that runs infrequently. However, when a notification appears and the user slides down the notification tray, we'll often see dropped frames due to page faults if there has been severe memory pressure. There are similar issues with other persistent processes. > > The goal on an Android device is to aggressively evict from very low-priority background tasks that are likely to be killed anyway, since this will reduce the likelihood of lowmemorykiller running in the first place. It will still evict some from foreground and persistent processes, but it should help ensure that background processes are effectively reduced to the size of their heaps before evicting from more critical tasks. This should mean fewer background processes end up killed, which should improve performance and power on Android across the board (since it costs significantly less to page things back in than to replay the entirety of application startup). > > The follow-on that I'm also experimenting with is how to improve vmpressure such that userspace can have some idea when low-priority memory cgroups are about as small as they can get. The correct time for Android to kill a background process under memory pressure is when there is evidence that a process has to be killed in order to alleviate memory pressure. If the device is below the low memory watermark and we know that there's probably no way to reclaim any more from background processes, then a userspace daemon should kill one or more background processes to fix that. Per-cgroup priority could be the first step toward that information. > > I've tested a version of this patch on a Pixel running 3.18 along with an overhauled version of lmkd (the Android userspace lowmemorykiller daemon), and it does seem to work fine. I've ported it forward but have not yet rigorously tested it at TOT, since I don't have an Android test setup running TOT. While I'm getting my tests ported over, I would like some feedback on adding another tunable as well as what the tunable's interface should be--I really don't like the 0-10 priority scheme I have in the patch but I don't have a better idea. > > Thanks, > Tim > > Tim Murray (1): > mm, memcg: add prioritized reclaim > > include/linux/memcontrol.h | 20 +++++++++++++++++++- > mm/memcontrol.c | 33 +++++++++++++++++++++++++++++++++ > mm/vmscan.c | 3 ++- > 3 files changed, 54 insertions(+), 2 deletions(-) > > -- > 2.12.0.367.g23dc2f6d3c-goog > > -- > To unsubscribe, send a message with 'unsubscribe linux-mm' in > the body to majordomo@kvack.org. For more info on Linux MM, > see: http://www.linux-mm.org/ . > Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
[toc] | [prev] | [next] | [standalone]
| From | Vinayak Menon <vinmenon@codeaurora.org> |
|---|---|
| Date | 2017-03-20 15:20 +0100 |
| Message-ID | <tn6kb-5SK-33@gated-at.bofh.it> |
| In reply to | #1604185 |
On Fri, Mar 17, 2017 at 04:16:35PM -0700, Tim Murray wrote: Hi Tim, >> Hi all, >> >> I've been working to improve Android's memory management and drop lowmemorykiller from the kernel, and I'd like to get some feedback on a small patch with a lot of side effects. >> >> Currently, when an Android device is under memory pressure, one of three things will happen from kswapd: >> >> 1. Compress an anonymous page to ZRAM. >> 2. Evict a file page. >> 3. Kill a process via lowmemorykiller. >> >> The first two are cheap and per-page, the third is relatively cheap in the short term, frees many pages, and may cause power and performance penalties later on when the process has to be started again. For lots of reasons, I'd like a better balance between reclamation and killing on Android. >> >> One of the nice things about Android from an optimization POV is that the execution model is more constrained than a generic Linux machine. There are only a limited number of processes that need to execute quickly for the device to appear to have good performance, and a userspace daemon (called ActivityManagerService) knows exactly what those processes are at any given time. We've made use of that in the past via cpusets and schedtune to limit the CPU resources available to background processes, and I think we can apply the same concept to memory. >> This patch adds a new tunable to mem cgroups, memory.priority. A mem cgroup with a non-zero priority will not be eligible for scanning until the scan_control's priority is greater than zero. Once the mem cgroup is eligible for scanning, the priority acts as a bias to reduce the number of pages that should be scanned. From the discussions @ https://lkml.org/lkml/2017/3/3/752, I assume you are trying per-app memcg. We were trying to implement per app memory cgroups and were encountering some issues (https://www.spinics.net/lists/linux-mm/msg121665.html) . I am curious if you have seen similar issues and would like to know if the patch also address some of these problems. The major issues were: (1) Because of multiple per-app memcgs, the per memcg LRU size is so small and results in kswapd priority drop. This results in sudden increase in scan at lower priorities. And kswapd ends up consuming around 3 times more time. (2) Due to kswapd taking more time in freeing up memory, allocstalls are high and for similar reasons stated above direct reclaim path consumes 2.5 times more time. (3) Because of multiple LRUs, the aging of pages is affected and this results in wrong pages being evicted resulting in higher number of major faults. Since soft reclaim was not of much help in mitigating the problem, I was trying out something similar to memcg priority. But what I have seen is that this aggravates the above mentioned problems. I think this is because, even though the high priority tasks (foreground) are having pages which are used at the moment, there are idle pages too which could be reclaimed. But due to the high priority of foreground memcg, it requires the kswapd priority to drop down much to reclaim these idle pages. This results in excessive reclaim from background apps resulting in increased major faults, pageins and thus increased launch latency when these apps are later brought back to foreground. One thing which is found to fix the above problems is to have both global LRU and the per-memcg LRU. Global reclaim can use the global LRU thus fixing the above 3 issues. The memcg LRUs can then be used for soft reclaim or a proactive reclaim similar to Minchan's Per process reclaim for the background or low priority tasks. I have been trying this change on 4.4 kernel (yet to try the per-app reclaim/soft reclaim part). One downside is the extra list_head in struct page and the memory it consumes. >> We've seen cases on Android where the global LRU isn't sufficient. For example, notifications in Android are rendered as part of a separate process that runs infrequently. However, when a notification appears and the user slides down the notification tray, we'll often see dropped frames due to page faults if there has been severe memory pressure. There are similar issues with other persistent processes. >> >> The goal on an Android device is to aggressively evict from very low-priority background tasks that are likely to be killed anyway, since this will reduce the likelihood of lowmemorykiller running in the first place. It will still evict some from foreground and persistent processes, but it should help ensure that background processes are effectively reduced to the size of their heaps before evicting from more critical tasks. This should mean fewer background processes end up killed, which should improve performance and power on Android across the board (since it costs significantly less to page things back in than to replay the entirety of application startup). >> >> The follow-on that I'm also experimenting with is how to improve vmpressure such that userspace can have some idea when low-priority memory cgroups are about as small as they can get. The correct time for Android to kill a background process under memory pressure is when there is evidence that a process has to be killed in order to alleviate memory pressure. If the device is below the low memory watermark and we know that there's probably no way to reclaim any more from background processes, then a userspace daemon should kill one or more background processes to fix that. Per-cgroup priority could be the first step toward that information. >> >> I've tested a version of this patch on a Pixel running 3.18 along with an overhauled version of lmkd (the Android userspace lowmemorykiller daemon), and it does seem to work fine. I've ported it forward but have not yet rigorously tested it at TOT, since I don't have an Android test setup running TOT. While I'm getting my tests ported over, I would like some feedback on adding another tunable as well as what the tunable's interface should be--I really don't like the 0-10 priority scheme I have in the patch but I don't have a better idea. >> >> Thanks, >> Tim >> >> Tim Murray (1): >> mm, memcg: add prioritized reclaim >> >> include/linux/memcontrol.h | 20 +++++++++++++++++++- >> mm/memcontrol.c | 33 +++++++++++++++++++++++++++++++++ >> mm/vmscan.c | 3 ++- >> 3 files changed, 54 insertions(+), 2 deletions(-) >> >> -- >> 2.12.0.367.g23dc2f6d3c-goog >> >> -- >> To unsubscribe, send a message with 'unsubscribe linux-mm' in >> the body to majordomo@kvack.org. For more info on Linux MM, >> see: http://www.linux-mm.org/ . >> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
[toc] | [prev] | [next] | [standalone]
| From | Johannes Weiner <hannes@cmpxchg.org> |
|---|---|
| Date | 2017-03-20 17:00 +0100 |
| Message-ID | <tn7SV-6Ou-15@gated-at.bofh.it> |
| In reply to | #1604621 |
On Mon, Mar 20, 2017 at 07:28:53PM +0530, Vinayak Menon wrote: > From the discussions @ https://lkml.org/lkml/2017/3/3/752, I assume you are trying > per-app memcg. We were trying to implement per app memory cgroups and were > encountering some issues (https://www.spinics.net/lists/linux-mm/msg121665.html) . > I am curious if you have seen similar issues and would like to know if the patch also > address some of these problems. > > The major issues were: > (1) Because of multiple per-app memcgs, the per memcg LRU size is so small and > results in kswapd priority drop. This results in sudden increase in scan at lower priorities. > And kswapd ends up consuming around 3 times more time. There shouldn't be a connection between those two things. Yes, priority levels used to dictate aggressiveness of reclaim, and we did add a bunch of memcg code to avoid priority drops. But nowadays the priority level should only set the LRU scan window and we bail out once we have reclaimed enough (see the code in shrink_node_memcg()). If kswapd gets stuck on smaller LRUs, we should find out why and then address that problem. > (2) Due to kswapd taking more time in freeing up memory, allocstalls are high and for > similar reasons stated above direct reclaim path consumes 2.5 times more time. > (3) Because of multiple LRUs, the aging of pages is affected and this results in wrong > pages being evicted resulting in higher number of major faults. > > Since soft reclaim was not of much help in mitigating the problem, I was trying out > something similar to memcg priority. But what I have seen is that this aggravates the > above mentioned problems. I think this is because, even though the high priority tasks > (foreground) are having pages which are used at the moment, there are idle pages too > which could be reclaimed. But due to the high priority of foreground memcg, it requires > the kswapd priority to drop down much to reclaim these idle pages. This results in excessive > reclaim from background apps resulting in increased major faults, pageins and thus increased > launch latency when these apps are later brought back to foreground. This is what the soft limit *should* do, but unfortunately its semantics and implementation in cgroup1 are too broken for this. Have you tried configuring memory.low for the foreground groups in cgroup2? That protects those pages from reclaim as long as there are reclaimable idle pages in the memory.low==0 background groups. > One thing which is found to fix the above problems is to have both global LRU and the per-memcg LRU. > Global reclaim can use the global LRU thus fixing the above 3 issues. The memcg LRUs can then be used > for soft reclaim or a proactive reclaim similar to Minchan's Per process reclaim for the background or > low priority tasks. I have been trying this change on 4.4 kernel (yet to try the per-app > reclaim/soft reclaim part). One downside is the extra list_head in struct page and the memory it consumes. That would be a major step backwards, and I'm not entirely convinced that the issues you are seeing cannot be fixed by improving the way we do global round-robin reclaim and/or configuring memory.low.
[toc] | [prev] | [next] | [standalone]
| From | Vinayak Menon <vinmenon@codeaurora.org> |
|---|---|
| Date | 2017-03-22 13:30 +0100 |
| Message-ID | <tnNyO-2kr-17@gated-at.bofh.it> |
| In reply to | #1604712 |
On 3/20/2017 8:53 PM, Johannes Weiner wrote:
> On Mon, Mar 20, 2017 at 07:28:53PM +0530, Vinayak Menon wrote:
>> From the discussions @ https://lkml.org/lkml/2017/3/3/752, I assume you are trying
>> per-app memcg. We were trying to implement per app memory cgroups and were
>> encountering some issues (https://www.spinics.net/lists/linux-mm/msg121665.html) .
>> I am curious if you have seen similar issues and would like to know if the patch also
>> address some of these problems.
>>
>> The major issues were:
>> (1) Because of multiple per-app memcgs, the per memcg LRU size is so small and
>> results in kswapd priority drop. This results in sudden increase in scan at lower priorities.
>> And kswapd ends up consuming around 3 times more time.
> There shouldn't be a connection between those two things.
>
> Yes, priority levels used to dictate aggressiveness of reclaim, and we
> did add a bunch of memcg code to avoid priority drops.
>
> But nowadays the priority level should only set the LRU scan window
> and we bail out once we have reclaimed enough (see the code in
> shrink_node_memcg()).
>
> If kswapd gets stuck on smaller LRUs, we should find out why and then
> address that problem.
Hi Johannes, Thanks for your comments. I will try to explain what I have observed while debugging this
problem.
When there are multiple small LRUs and very few LRUs with considerable size (by considerable size I mean
those sizes which can result in a non-zero scan value in get_scan_count at priorities near to DEF_PRIORITY).
Since I am trying on 4.4 kernel there are more small LRUs per app (per memcg) because of further split due to per zone LRU.
Considering the case where most of the apps in the system are of this small category, the scan calculated by
get_scan_count for these memcg LRUs at around DEF_PRIORITY become zero or very less, either because of
size >> sc->priority is 0 or because of SCAN_FRACT. For these runs around DEF_PRIORITY (say till DEF_PRIORITY/2)
since sc->nr_scanned is < sc->nr_to_reclaim, the kswapd priority drops. Now say at kswapd priority less than
DEF_PRIORITY/2, the scan returned by get_scan_count gets higher slowly for all memcgs. This causes sudden
excessive scanning of most of the memcgs (because this also results in heavy scanning of memcgs which have
considerable size). As I understand, the scan priority in this case results in aggressive reclaim and not just decides the
scan window because, in the following check in shrink_node_memcg, the "nr_to_reclaim" (sc->nr_to_reclaim)
is a high value compared to the memcg LRU size. I have seen that this also causes either nr_file or nr_anon
go zero most of the time (after this check), which as I understand means that proportional scanning does not happen.
if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
continue;
I had tried making the "nr_to_reclaim" proportional to the lru size and that brings some benefits, but does not
solve the problem. Because when that is done, in some cases, the scanned pages at this priority decreases again,
resulting in further priority drop.
The priority drop and excessive scan/reclaim at lower priorities I have confirmed by keeping scanned and reclaimed
counters for each priority in vmstat. And yes, this results in kswapd being awake and running for longer time.
There was some benefit by prioritizing the memcgs similar to what Tim does in his patch and also by proportionally
reclaiming from the per-task memcgs based on their priority. But still the stats are far bad compared to having
a global LRU. One thing which was commonly seen in all these experiments is the multi fold increase in majfaults,
which I think is partly caused by poor aging of pages when the pages are distributed among a large number of tiny LRUs,
and global reclaim trying to reclaim from all of them.
>> (2) Due to kswapd taking more time in freeing up memory, allocstalls are high and for
>> similar reasons stated above direct reclaim path consumes 2.5 times more time.
>> (3) Because of multiple LRUs, the aging of pages is affected and this results in wrong
>> pages being evicted resulting in higher number of major faults.
>>
>> Since soft reclaim was not of much help in mitigating the problem, I was trying out
>> something similar to memcg priority. But what I have seen is that this aggravates the
>> above mentioned problems. I think this is because, even though the high priority tasks
>> (foreground) are having pages which are used at the moment, there are idle pages too
>> which could be reclaimed. But due to the high priority of foreground memcg, it requires
>> the kswapd priority to drop down much to reclaim these idle pages. This results in excessive
>> reclaim from background apps resulting in increased major faults, pageins and thus increased
>> launch latency when these apps are later brought back to foreground.
> This is what the soft limit *should* do, but unfortunately its
> semantics and implementation in cgroup1 are too broken for this.
>
> Have you tried configuring memory.low for the foreground groups in
> cgroup2? That protects those pages from reclaim as long as there are
> reclaimable idle pages in the memory.low==0 background groups.
I have not yet tried cgroup2. I was trying to understand it sometime back and IIUC it supports only a
single hierarchy and a process can be part of only one cgroup, which means when we try per-task mem
cgroup, this would mean all other controllers will have to be configured per-task. No ? I would like to try
memory.low that you suggest. Let me check if I have a way to test this without disturbing other controllers,
or will try with memory cgroup alone.
>> One thing which is found to fix the above problems is to have both global LRU and the per-memcg LRU.
>> Global reclaim can use the global LRU thus fixing the above 3 issues. The memcg LRUs can then be used
>> for soft reclaim or a proactive reclaim similar to Minchan's Per process reclaim for the background or
>> low priority tasks. I have been trying this change on 4.4 kernel (yet to try the per-app
>> reclaim/soft reclaim part). One downside is the extra list_head in struct page and the memory it consumes.
> That would be a major step backwards, and I'm not entirely convinced
> that the issues you are seeing cannot be fixed by improving the way we
> do global round-robin reclaim and/or configuring memory.low.
I understand and agree that it would be better to fix the existing design if it is possible.
Thanks,
Vinayak
[toc] | [prev] | [next] | [standalone]
| From | Tim Murray <timmurray@google.com> |
|---|---|
| Date | 2017-03-21 18:20 +0100 |
| Message-ID | <tnvBT-6kG-9@gated-at.bofh.it> |
| In reply to | #1604185 |
On Sun, Mar 19, 2017 at 10:59 PM, Minchan Kim <minchan@kernel.org> wrote: > However, I'm not sure your approach is good. It seems your approach just > reclaims pages from groups (DEF_PRIORITY - memcg->priority) >= sc->priority. > IOW, it is based on *temporal* memory pressure fluctuation sc->priority. > > Rather than it, I guess pages to be reclaimed should be distributed by > memcg->priority. Namely, if global memory pressure happens and VM want to > reclaim 100 pages, VM should reclaim 90 pages from memcg-A(priority-10) > and 10 pages from memcg-B(prioirty-90). This is what I debated most while writing this patch. If I'm understanding your concern correctly, I think I'm doing more than skipping high-priority cgroups: - If the scan isn't high priority yet, then skip high-priority cgroups. - When the scan is high priority, scan fewer pages from higher-priority cgroups (using the priority to modify the shift in get_scan_count). This is tightly coupled with the question of what to do with vmpressure. The right thing to do on an Android device under memory pressure is probably something like this: 1. Reclaim aggressively from low-priority background processes. The goal here is to reduce the pages used by background processes to the size of their heaps (or smaller with ZRAM) but zero file pages. They're already likely to be killed by userspace and we're keeping them around opportunistically, so a performance hit if they run and have to do IO to restore some of that working set is okay. 2. Reclaim a small amount from persistent processes. These often have a performance-critical subset of pages that we absolutely don't want paged out, but some reclaim of these processes is fine. They're large, some of them only run sporadically and don't impact performance, it's okay to touch these sometimes. 3. If there still aren't enough free pages, notify userspace to kill any processes it can. If I put my "Android performance engineer working on userspace" hat on, what I'd want to know from userspace is that kswapd/direct reclaim probably has to scan foreground processes in order to reclaim enough free pages to satisfy watermarks. That's a signal I could directly act on from userspace. 4. If that still isn't enough, reclaim from foreground processes, since those processes are performance-critical. As a result, I like not being fair about which cgroups are scanned initially. Some cgroups are strictly more important than others. (With that said, I'm not tied to enforcing unfairness in scanning. Android would probably use different priority levels for each app level for fair scanning vs unfair scanning, but my guess is that the actual reclaiming behavior would look similar in both schemes.) Mem cgroup priority suggests a useful signal for vmpressure. If scanning is starting to touch cgroups at a higher priority than persistent processes, then the userspace lowmemorykiller could kill one or more background processes (which would be in low-priority cgroups that have already been scanned aggressively). The current lmk hand-tuned watermarks would be gone, and tuning the /proc/sys/vm knobs would be all that's required to make an Android device do the right thing in terms of memory. On Sun, Mar 19, 2017 at 10:59 PM, Minchan Kim <minchan@kernel.org> wrote: > Hello, > > On Fri, Mar 17, 2017 at 04:16:35PM -0700, Tim Murray wrote: >> Hi all, >> >> I've been working to improve Android's memory management and drop lowmemorykiller from the kernel, and I'd like to get some feedback on a small patch with a lot of side effects. >> >> Currently, when an Android device is under memory pressure, one of three things will happen from kswapd: >> >> 1. Compress an anonymous page to ZRAM. >> 2. Evict a file page. >> 3. Kill a process via lowmemorykiller. >> >> The first two are cheap and per-page, the third is relatively cheap in the short term, frees many pages, and may cause power and performance penalties later on when the process has to be started again. For lots of reasons, I'd like a better balance between reclamation and killing on Android. >> >> One of the nice things about Android from an optimization POV is that the execution model is more constrained than a generic Linux machine. There are only a limited number of processes that need to execute quickly for the device to appear to have good performance, and a userspace daemon (called ActivityManagerService) knows exactly what those processes are at any given time. We've made use of that in the past via cpusets and schedtune to limit the CPU resources available to background processes, and I think we can apply the same concept to memory. >> > > AFAIK, many platforms as well as android have done it. IOW, they know what > processes are important while others are not critical for user-response. > >> This patch adds a new tunable to mem cgroups, memory.priority. A mem cgroup with a non-zero priority will not be eligible for scanning until the scan_control's priority is greater than zero. Once the mem cgroup is eligible for scanning, the priority acts as a bias to reduce the number of pages that should be scanned. > > First of all, the concept makes sense to me. The problem with cgroup-per-app > model is that it's really hard to predict how many of memory a group needs to > make system smooth although we know what processes are important. > Because of it, it's hard to tune memcg low/high/max proactively. > > So, it would be great if admin can give more priority some groups like > graphic mamager, laucher and killer applications like TV manager, Dial > manager and so (ie, when memory pressure happens, please reclaim more pages > from low priority groups). > > However, I'm not sure your approach is good. It seems your approach just > reclaims pages from groups (DEF_PRIORITY - memcg->priority) >= sc->priority. > IOW, it is based on *temporal* memory pressure fluctuation sc->priority. > > Rather than it, I guess pages to be reclaimed should be distributed by > memcg->priority. Namely, if global memory pressure happens and VM want to > reclaim 100 pages, VM should reclaim 90 pages from memcg-A(priority-10) > and 10 pages from memcg-B(prioirty-90). > > Anyway, it's really desireble approach so memcg maintainers, Please, have a > look. > > Thanks. > >> >> We've seen cases on Android where the global LRU isn't sufficient. For example, notifications in Android are rendered as part of a separate process that runs infrequently. However, when a notification appears and the user slides down the notification tray, we'll often see dropped frames due to page faults if there has been severe memory pressure. There are similar issues with other persistent processes. >> >> The goal on an Android device is to aggressively evict from very low-priority background tasks that are likely to be killed anyway, since this will reduce the likelihood of lowmemorykiller running in the first place. It will still evict some from foreground and persistent processes, but it should help ensure that background processes are effectively reduced to the size of their heaps before evicting from more critical tasks. This should mean fewer background processes end up killed, which should improve performance and power on Android across the board (since it costs significantly less to page things back in than to replay the entirety of application startup). >> >> The follow-on that I'm also experimenting with is how to improve vmpressure such that userspace can have some idea when low-priority memory cgroups are about as small as they can get. The correct time for Android to kill a background process under memory pressure is when there is evidence that a process has to be killed in order to alleviate memory pressure. If the device is below the low memory watermark and we know that there's probably no way to reclaim any more from background processes, then a userspace daemon should kill one or more background processes to fix that. Per-cgroup priority could be the first step toward that information. >> >> I've tested a version of this patch on a Pixel running 3.18 along with an overhauled version of lmkd (the Android userspace lowmemorykiller daemon), and it does seem to work fine. I've ported it forward but have not yet rigorously tested it at TOT, since I don't have an Android test setup running TOT. While I'm getting my tests ported over, I would like some feedback on adding another tunable as well as what the tunable's interface should be--I really don't like the 0-10 priority scheme I have in the patch but I don't have a better idea. >> >> Thanks, >> Tim >> >> Tim Murray (1): >> mm, memcg: add prioritized reclaim >> >> include/linux/memcontrol.h | 20 +++++++++++++++++++- >> mm/memcontrol.c | 33 +++++++++++++++++++++++++++++++++ >> mm/vmscan.c | 3 ++- >> 3 files changed, 54 insertions(+), 2 deletions(-) >> >> -- >> 2.12.0.367.g23dc2f6d3c-goog >> >> -- >> To unsubscribe, send a message with 'unsubscribe linux-mm' in >> the body to majordomo@kvack.org. For more info on Linux MM, >> see: http://www.linux-mm.org/ . >> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
[toc] | [prev] | [next] | [standalone]
| From | Minchan Kim <minchan@kernel.org> |
|---|---|
| Date | 2017-03-22 05:50 +0100 |
| Message-ID | <tnGnD-5mA-9@gated-at.bofh.it> |
| In reply to | #1605835 |
Hi Tim,
On Tue, Mar 21, 2017 at 10:18:26AM -0700, Tim Murray wrote:
> On Sun, Mar 19, 2017 at 10:59 PM, Minchan Kim <minchan@kernel.org> wrote:
> > However, I'm not sure your approach is good. It seems your approach just
> > reclaims pages from groups (DEF_PRIORITY - memcg->priority) >= sc->priority.
> > IOW, it is based on *temporal* memory pressure fluctuation sc->priority.
> >
> > Rather than it, I guess pages to be reclaimed should be distributed by
> > memcg->priority. Namely, if global memory pressure happens and VM want to
> > reclaim 100 pages, VM should reclaim 90 pages from memcg-A(priority-10)
> > and 10 pages from memcg-B(prioirty-90).
>
> This is what I debated most while writing this patch. If I'm
> understanding your concern correctly, I think I'm doing more than
> skipping high-priority cgroups:
Yes, that is my concern. It could give too much pressure lower-priority
group. You already reduced scanning window for high-priority group so
I guess it would be enough for working.
The rationale from my thining is high-priority group can have cold pages(
for instance, used-once pages, madvise_free pages and so on) so, VM should
age every groups to reclaim cold pages but we can reduce scanning window
for high-priority group to keep more workingset as you did. By that, we
already give more pressure to lower priority group than high-prioirty group.
>
> - If the scan isn't high priority yet, then skip high-priority cgroups.
This part is the one I think it's too much ;-)
I think no need to skip but just reduce scanning window by the group's
prioirty.
> - When the scan is high priority, scan fewer pages from
> higher-priority cgroups (using the priority to modify the shift in
> get_scan_count).
That sounds lkie a good idea but need to tune more.
How about this?
get_scan_count for memcg-A:
..
size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx) *
(memcg-A / sum(memcg all priorities))
get_scan_count for memcg-B:
..
size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx) *
(memcg-B / sum(memcg all priorities))
By that, can't it support memcg-hierarchy as well? I don't know. ;(
Hope memcg guys give more thought.
>
> This is tightly coupled with the question of what to do with
> vmpressure. The right thing to do on an Android device under memory
> pressure is probably something like this:
>
> 1. Reclaim aggressively from low-priority background processes. The
> goal here is to reduce the pages used by background processes to the
> size of their heaps (or smaller with ZRAM) but zero file pages.
> They're already likely to be killed by userspace and we're keeping
> them around opportunistically, so a performance hit if they run and
> have to do IO to restore some of that working set is okay.
> 2. Reclaim a small amount from persistent processes. These often have
> a performance-critical subset of pages that we absolutely don't want
> paged out, but some reclaim of these processes is fine. They're large,
> some of them only run sporadically and don't impact performance, it's
> okay to touch these sometimes.
That's why I wanted to age LRU from all of memcg but slow for high-priority
group via reduing scanning window, which means high-priority group's
pages makes more chance to be activated. So, it's already prioirty-boost.
> 3. If there still aren't enough free pages, notify userspace to kill
> any processes it can. If I put my "Android performance engineer
> working on userspace" hat on, what I'd want to know from userspace is
> that kswapd/direct reclaim probably has to scan foreground processes
> in order to reclaim enough free pages to satisfy watermarks. That's a
> signal I could directly act on from userspace.
Hmm, could you tell us how many of memcg groups do you thinking now?
background, foreground? Just two?
The reason I ask is if you want to make foregroud/background memcg
and move apps between them back and forth when the status changed,
we need to remember lru pages are not moved from originated memcg
so it wouldn't work as expected.
> 4. If that still isn't enough, reclaim from foreground processes,
> since those processes are performance-critical.
>
> As a result, I like not being fair about which cgroups are scanned
> initially. Some cgroups are strictly more important than others. (With
Yeb, *initially* is arguable point. I hope only reducing scanning
window would work. However, just my two cent. If it have a problem,
yes, need more thing.
> that said, I'm not tied to enforcing unfairness in scanning. Android
> would probably use different priority levels for each app level for
> fair scanning vs unfair scanning, but my guess is that the actual
> reclaiming behavior would look similar in both schemes.)
>
> Mem cgroup priority suggests a useful signal for vmpressure. If
> scanning is starting to touch cgroups at a higher priority than
> persistent processes, then the userspace lowmemorykiller could kill
> one or more background processes (which would be in low-priority
> cgroups that have already been scanned aggressively). The current lmk
> hand-tuned watermarks would be gone, and tuning the /proc/sys/vm knobs
> would be all that's required to make an Android device do the right
> thing in terms of memory.
Yes, it's better way. I think.
Thanks.
[toc] | [prev] | [next] | [standalone]
| From | Minchan Kim <minchan@kernel.org> |
|---|---|
| Date | 2017-03-22 06:30 +0100 |
| Message-ID | <tnH0l-5Rk-1@gated-at.bofh.it> |
| In reply to | #1606209 |
On Wed, Mar 22, 2017 at 01:41:17PM +0900, Minchan Kim wrote:
> Hi Tim,
>
> On Tue, Mar 21, 2017 at 10:18:26AM -0700, Tim Murray wrote:
> > On Sun, Mar 19, 2017 at 10:59 PM, Minchan Kim <minchan@kernel.org> wrote:
> > > However, I'm not sure your approach is good. It seems your approach just
> > > reclaims pages from groups (DEF_PRIORITY - memcg->priority) >= sc->priority.
> > > IOW, it is based on *temporal* memory pressure fluctuation sc->priority.
> > >
> > > Rather than it, I guess pages to be reclaimed should be distributed by
> > > memcg->priority. Namely, if global memory pressure happens and VM want to
> > > reclaim 100 pages, VM should reclaim 90 pages from memcg-A(priority-10)
> > > and 10 pages from memcg-B(prioirty-90).
> >
> > This is what I debated most while writing this patch. If I'm
> > understanding your concern correctly, I think I'm doing more than
> > skipping high-priority cgroups:
>
> Yes, that is my concern. It could give too much pressure lower-priority
> group. You already reduced scanning window for high-priority group so
> I guess it would be enough for working.
>
> The rationale from my thining is high-priority group can have cold pages(
> for instance, used-once pages, madvise_free pages and so on) so, VM should
> age every groups to reclaim cold pages but we can reduce scanning window
> for high-priority group to keep more workingset as you did. By that, we
> already give more pressure to lower priority group than high-prioirty group.
>
> >
> > - If the scan isn't high priority yet, then skip high-priority cgroups.
>
> This part is the one I think it's too much ;-)
> I think no need to skip but just reduce scanning window by the group's
> prioirty.
>
> > - When the scan is high priority, scan fewer pages from
> > higher-priority cgroups (using the priority to modify the shift in
> > get_scan_count).
>
> That sounds lkie a good idea but need to tune more.
>
> How about this?
>
> get_scan_count for memcg-A:
> ..
> size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx) *
> (memcg-A / sum(memcg all priorities))
>
> get_scan_count for memcg-B:
> ..
> size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx) *
> (memcg-B / sum(memcg all priorities))
>
Huh, correction.
size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
scan = size >> sc->priority;
scan = scan * (sum(memcg) - memcg A) / sum(memcg);
[toc] | [prev] | [next] | [standalone]
| From | peter enderborg <peter.enderborg@sonymobile.com> |
|---|---|
| Date | 2017-03-20 08:10 +0100 |
| Message-ID | <tmZC1-1e5-3@gated-at.bofh.it> |
| In reply to | #1603661 |
Hi Tim. Do you have a link to the new version lmkd? On 03/18/2017 12:16 AM, Tim Murray wrote: > Hi all, > > I've been working to improve Android's memory management and drop lowmemorykiller from the kernel, and I'd like to get some feedback on a small patch with a lot of side effects. > > Currently, when an Android device is under memory pressure, one of three things will happen from kswapd: > > 1. Compress an anonymous page to ZRAM. > 2. Evict a file page. > 3. Kill a process via lowmemorykiller. > > The first two are cheap and per-page, the third is relatively cheap in the short term, frees many pages, and may cause power and performance penalties later on when the process has to be started again. For lots of reasons, I'd like a better balance between reclamation and killing on Android. > > One of the nice things about Android from an optimization POV is that the execution model is more constrained than a generic Linux machine. There are only a limited number of processes that need to execute quickly for the device to appear to have good performance, and a userspace daemon (called ActivityManagerService) knows exactly what those processes are at any given time. We've made use of that in the past via cpusets and schedtune to limit the CPU resources available to background processes, and I think we can apply the same concept to memory. > > This patch adds a new tunable to mem cgroups, memory.priority. A mem cgroup with a non-zero priority will not be eligible for scanning until the scan_control's priority is greater than zero. Once the mem cgroup is eligible for scanning, the priority acts as a bias to reduce the number of pages that should be scanned. > > We've seen cases on Android where the global LRU isn't sufficient. For example, notifications in Android are rendered as part of a separate process that runs infrequently. However, when a notification appears and the user slides down the notification tray, we'll often see dropped frames due to page faults if there has been severe memory pressure. There are similar issues with other persistent processes. > > The goal on an Android device is to aggressively evict from very low-priority background tasks that are likely to be killed anyway, since this will reduce the likelihood of lowmemorykiller running in the first place. It will still evict some from foreground and persistent processes, but it should help ensure that background processes are effectively reduced to the size of their heaps before evicting from more critical tasks. This should mean fewer background processes end up killed, which should improve performance and power on Android across the board (since it costs significantly less to page things back in than to replay the entirety of application startup). > > The follow-on that I'm also experimenting with is how to improve vmpressure such that userspace can have some idea when low-priority memory cgroups are about as small as they can get. The correct time for Android to kill a background process under memory pressure is when there is evidence that a process has to be killed in order to alleviate memory pressure. If the device is below the low memory watermark and we know that there's probably no way to reclaim any more from background processes, then a userspace daemon should kill one or more background processes to fix that. Per-cgroup priority could be the first step toward that information. > > I've tested a version of this patch on a Pixel running 3.18 along with an overhauled version of lmkd (the Android userspace lowmemorykiller daemon), and it does seem to work fine. I've ported it forward but have not yet rigorously tested it at TOT, since I don't have an Android test setup running TOT. While I'm getting my tests ported over, I would like some feedback on adding another tunable as well as what the tunable's interface should be--I really don't like the 0-10 priority scheme I have in the patch but I don't have a better idea. > > Thanks, > Tim > > Tim Murray (1): > mm, memcg: add prioritized reclaim > > include/linux/memcontrol.h | 20 +++++++++++++++++++- > mm/memcontrol.c | 33 +++++++++++++++++++++++++++++++++ > mm/vmscan.c | 3 ++- > 3 files changed, 54 insertions(+), 2 deletions(-) >
[toc] | [prev] | [next] | [standalone]
| From | Kyungmin Park <kmpark@infradead.org> |
|---|---|
| Date | 2017-03-20 09:20 +0100 |
| Message-ID | <tn0HM-1Xo-9@gated-at.bofh.it> |
| In reply to | #1603661 |
On Sat, Mar 18, 2017 at 8:16 AM, Tim Murray <timmurray@google.com> wrote: > Hi all, > > I've been working to improve Android's memory management and drop lowmemorykiller from the kernel, and I'd like to get some feedback on a small patch with a lot of side effects. > > Currently, when an Android device is under memory pressure, one of three things will happen from kswapd: > > 1. Compress an anonymous page to ZRAM. > 2. Evict a file page. > 3. Kill a process via lowmemorykiller. > > The first two are cheap and per-page, the third is relatively cheap in the short term, frees many pages, and may cause power and performance penalties later on when the process has to be started again. For lots of reasons, I'd like a better balance between reclamation and killing on Android. > > One of the nice things about Android from an optimization POV is that the execution model is more constrained than a generic Linux machine. There are only a limited number of processes that need to execute quickly for the device to appear to have good performance, and a userspace daemon (called ActivityManagerService) knows exactly what those processes are at any given time. We've made use of that in the past via cpusets and schedtune to limit the CPU resources available to background processes, and I think we can apply the same concept to memory. > > This patch adds a new tunable to mem cgroups, memory.priority. A mem cgroup with a non-zero priority will not be eligible for scanning until the scan_control's priority is greater than zero. Once the mem cgroup is eligible for scanning, the priority acts as a bias to reduce the number of pages that should be scanned. Here's old discussion to support app-per-memcg reclaim "[PATCH] memcg: Add force_reclaim to reclaim tasks' memory in memcg." http://www.spinics.net/lists/cgroups/msg07874.html unlike existing interface, it can reclaim the memory while process is still in memcg. In our case, it's used for reclaim and swap out pages for that app. Thank you, Kyungmin Park > > We've seen cases on Android where the global LRU isn't sufficient. For example, notifications in Android are rendered as part of a separate process that runs infrequently. However, when a notification appears and the user slides down the notification tray, we'll often see dropped frames due to page faults if there has been severe memory pressure. There are similar issues with other persistent processes. > > The goal on an Android device is to aggressively evict from very low-priority background tasks that are likely to be killed anyway, since this will reduce the likelihood of lowmemorykiller running in the first place. It will still evict some from foreground and persistent processes, but it should help ensure that background processes are effectively reduced to the size of their heaps before evicting from more critical tasks. This should mean fewer background processes end up killed, which should improve performance and power on Android across the board (since it costs significantly less to page things back in than to replay the entirety of application startup). > > The follow-on that I'm also experimenting with is how to improve vmpressure such that userspace can have some idea when low-priority memory cgroups are about as small as they can get. The correct time for Android to kill a background process under memory pressure is when there is evidence that a process has to be killed in order to alleviate memory pressure. If the device is below the low memory watermark and we know that there's probably no way to reclaim any more from background processes, then a userspace daemon should kill one or more background processes to fix that. Per-cgroup priority could be the first step toward that information. > > I've tested a version of this patch on a Pixel running 3.18 along with an overhauled version of lmkd (the Android userspace lowmemorykiller daemon), and it does seem to work fine. I've ported it forward but have not yet rigorously tested it at TOT, since I don't have an Android test setup running TOT. While I'm getting my tests ported over, I would like some feedback on adding another tunable as well as what the tunable's interface should be--I really don't like the 0-10 priority scheme I have in the patch but I don't have a better idea. > > Thanks, > Tim > > Tim Murray (1): > mm, memcg: add prioritized reclaim > > include/linux/memcontrol.h | 20 +++++++++++++++++++- > mm/memcontrol.c | 33 +++++++++++++++++++++++++++++++++ > mm/vmscan.c | 3 ++- > 3 files changed, 54 insertions(+), 2 deletions(-) > > -- > 2.12.0.367.g23dc2f6d3c-goog > > -- > To unsubscribe, send a message with 'unsubscribe linux-mm' in > the body to majordomo@kvack.org. For more info on Linux MM, > see: http://www.linux-mm.org/ . > Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web