Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp754487imm; Sat, 26 May 2018 10:16:12 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoR3d45Qv68MVHdlqdLBYWyTCfUFyuxTFnhp5yl0S9IOPHcTsVc3UJAhJZYOTCRARzWfLCp X-Received: by 2002:a17:902:2702:: with SMTP id c2-v6mr7059021plb.297.1527354972765; Sat, 26 May 2018 10:16:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527354972; cv=none; d=google.com; s=arc-20160816; b=ek1w4URmA27Dm6yeWETPBUuqh1RExzTO7r/SYzAmGAPBZ9EQYzyqLbmZUEsWR3YA5A 3e+hGjmlo+iX1PBfTxCcuHqrpe0VaBTSy6nU71ByY1AD6a0n7puWaxkoaCmehQQI5/Se 5jmYKNvaStptT6zzgw0ZqnH4AQ0wCYQNJrdd8KttAEUAT0CMQoRcXCiiGTFByDaErYO1 o/uf1ayNAW+sgZ8/KOd8EOhGAIpZMDlezBpeiEZghDeFvLRdbg0sIrVxv6MNvb4/e0hk cq9Wh5ydlBXBplsXDCKtluVhYgai0MNjD0onqbTgkkFWz4k0MSRl/GZl73KkhG7POV8u UfLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:arc-authentication-results; bh=FV+gnMYfnors2BiiFL3scY6wus7qlNcwaQmrXDkrv4U=; b=N2C0IO6Kkt/+DlI8mE7kNdfktaHU0S0qCRg9y4+0NJVYZ3vyhAht/GNdAut/sCVRf3 VtF5DFHomXRzo0D9uruSnroZhBFwxdRYwyN3ySchY9VNgo1vD+PNg2VqjnMRz92QTBi0 Po7myGLVNFy8hDrMhrCRfs3+562FiUCkuX06tialrbadxXwZvnY+Hzx1Mz2dJhvb8CJo N9gcMspR0QcxM67nMpRpNFTZiBFE1Z5h4Hmel+DV2K4lEUvOkHIXf+TBVaGdCB9bbWeI PJt3m1d7Z9WbYrE6X6uE6PY/hSoPC+sGPanJKjbbhZeeCtQvdjX7oz/j1D+hn/UsybOI qWLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XxhK+X4q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w5-v6si25419374plz.356.2018.05.26.10.15.57; Sat, 26 May 2018 10:16:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XxhK+X4q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1032230AbeEZRPk (ORCPT + 99 others); Sat, 26 May 2018 13:15:40 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:54978 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1032007AbeEZRPi (ORCPT ); Sat, 26 May 2018 13:15:38 -0400 Received: by mail-wm0-f68.google.com with SMTP id f6-v6so21885026wmc.4 for ; Sat, 26 May 2018 10:15:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=FV+gnMYfnors2BiiFL3scY6wus7qlNcwaQmrXDkrv4U=; b=XxhK+X4qZ5LGwNT99IDlGwqW9bSxxSDFcNIQKl93pxdj2FZICoC1zfGAfu4k9TKnoy ItKYfjM4pQ0ART+xurVIlJe0VwSyxDzelOSC1YYrBVeOU+eHkHJ8GeAL5ZM5VG+Su4NW dl7nOO/HWXfNyfxi8V26Y1t9ApQdv0qQd55InIZ5iDMyLmiu4WOLtlafwLXAEo/swpz8 xRC+DZ/UGwHE+dYKaFh8wgE3mJBxkEhli562+g0W5ivX7eCIBw6T7YszQSlO25Emkfy9 aFSENXygKt14DXcQ2zuqipav8p4VB/u0fdhfhrIcfYcpSfxMXWUv6GIg06Z4Ro21N8IG U1Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=FV+gnMYfnors2BiiFL3scY6wus7qlNcwaQmrXDkrv4U=; b=MYpxFDozV6uuGaIU4E9pj//XT/xM7nOauxE8UNdCwTd3nYmEd1tQM/yAEuDHw5glZX 3tlxgl0eMiF8QtSRxp05U1u8lCijr2YhNZ/naj1+jOEoNCaQDOOgjFEot6EEAgGbvar4 +gMJBdYrd+Dhb4D13ef7Ak1iqh2MfahuE8kpcF8oLhRxWRmXRwtNMm7X9TqAGEBtzoJE gHUxaB1GzWMdxDhjgMIL7fvtbH3bICvvHAhFN2Xjuk2cdwpoDzn6o9X75RtzGPiIn0QK OQoExNFhmRtvA81yQCZtVk+paiWsSV6mzaj3UsyHW4tCjQfnfsXkpieM+N32kWi68VUo sBaw== X-Gm-Message-State: ALKqPwfdz9RUw8e8TrAlCnIugH6prE/YDFebcvzK/T9IqA1sLW1NVK62 LlpC9Kb9Ba45oyscuAV7m/g= X-Received: by 2002:a2e:1614:: with SMTP id w20-v6mr4492565ljd.105.1527354937312; Sat, 26 May 2018 10:15:37 -0700 (PDT) Received: from esperanza (81.5.110.211.dhcp.mipt-telecom.ru. [81.5.110.211]) by smtp.gmail.com with ESMTPSA id z18-v6sm6049108lfj.38.2018.05.26.10.15.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 26 May 2018 10:15:36 -0700 (PDT) Date: Sat, 26 May 2018 20:15:33 +0300 From: Vladimir Davydov To: Kirill Tkhai Cc: akpm@linux-foundation.org, shakeelb@google.com, viro@zeniv.linux.org.uk, hannes@cmpxchg.org, mhocko@kernel.org, tglx@linutronix.de, pombredanne@nexb.com, stummala@codeaurora.org, gregkh@linuxfoundation.org, sfr@canb.auug.org.au, guro@fb.com, mka@chromium.org, penguin-kernel@I-love.SAKURA.ne.jp, chris@chris-wilson.co.uk, longman@redhat.com, minchan@kernel.org, ying.huang@intel.com, mgorman@techsingularity.net, jbacik@fb.com, linux@roeck-us.net, linux-kernel@vger.kernel.org, linux-mm@kvack.org, willy@infradead.org, lirongqing@baidu.com, aryabinin@virtuozzo.com Subject: Re: [PATCH v7 00/17] Improve shrink_slab() scalability (old complexity was O(n^2), new is O(n)) Message-ID: <20180526171533.ucg27d2tnbvzt4oz@esperanza> References: <152698356466.3393.5351712806709424140.stgit@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <152698356466.3393.5351712806709424140.stgit@localhost.localdomain> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Kirill, The whole patch set looks good to me now. Acked-by: Vladimir Davydov Thanks, Vladimir On Tue, May 22, 2018 at 01:07:10PM +0300, Kirill Tkhai wrote: > Hi, > > this patches solves the problem with slow shrink_slab() occuring > on the machines having many shrinkers and memory cgroups (i.e., > with many containers). The problem is complexity of shrink_slab() > is O(n^2) and it grows too fast with the growth of containers > numbers. > > Let we have 200 containers, and every container has 10 mounts > and 10 cgroups. All container tasks are isolated, and they don't > touch foreign containers mounts. > > In case of global reclaim, a task has to iterate all over the memcgs > and to call all the memcg-aware shrinkers for all of them. This means, > the task has to visit 200 * 10 = 2000 shrinkers for every memcg, > and since there are 2000 memcgs, the total calls of do_shrink_slab() > are 2000 * 2000 = 4000000. > > 4 million calls are not a number operations, which can takes 1 cpu cycle. > E.g., super_cache_count() accesses at least two lists, and makes arifmetical > calculations. Even, if there are no charged objects, we do these calculations, > and replaces cpu caches by read memory. I observed nodes spending almost 100% > time in kernel, in case of intensive writing and global reclaim. The writer > consumes pages fast, but it's need to shrink_slab() before the reclaimer > reached shrink pages function (and frees SWAP_CLUSTER_MAX pages). Even if > there is no writing, the iterations just waste the time, and slows reclaim down. > > Let's see the small test below: > > $echo 1 > /sys/fs/cgroup/memory/memory.use_hierarchy > $mkdir /sys/fs/cgroup/memory/ct > $echo 4000M > /sys/fs/cgroup/memory/ct/memory.kmem.limit_in_bytes > $for i in `seq 0 4000`; > do mkdir /sys/fs/cgroup/memory/ct/$i; > echo $$ > /sys/fs/cgroup/memory/ct/$i/cgroup.procs; > mkdir -p s/$i; mount -t tmpfs $i s/$i; touch s/$i/file; > done > > Then, let's see drop caches time (5 sequential calls): > $time echo 3 > /proc/sys/vm/drop_caches > > 0.00user 13.78system 0:13.78elapsed 99%CPU > 0.00user 5.59system 0:05.60elapsed 99%CPU > 0.00user 5.48system 0:05.48elapsed 99%CPU > 0.00user 8.35system 0:08.35elapsed 99%CPU > 0.00user 8.34system 0:08.35elapsed 99%CPU > > > Last four calls don't actually shrink something. So, the iterations > over slab shrinkers take 5.48 seconds. Not so good for scalability. > > The patchset solves the problem by making shrink_slab() of O(n) > complexity. There are following functional actions: > > 1)Assign id to every registered memcg-aware shrinker. > 2)Maintain per-memcgroup bitmap of memcg-aware shrinkers, > and set a shrinker-related bit after the first element > is added to lru list (also, when removed child memcg > elements are reparanted). > 3)Split memcg-aware shrinkers and !memcg-aware shrinkers, > and call a shrinker if its bit is set in memcg's shrinker > bitmap. > (Also, there is a functionality to clear the bit, after > last element is shrinked). > > This gives signify performance increase. The result after patchset is applied: > > $time echo 3 > /proc/sys/vm/drop_caches > > 0.00user 1.10system 0:01.10elapsed 99%CPU > 0.00user 0.00system 0:00.01elapsed 64%CPU > 0.00user 0.01system 0:00.01elapsed 82%CPU > 0.00user 0.00system 0:00.01elapsed 64%CPU > 0.00user 0.01system 0:00.01elapsed 82%CPU > > The results show the performance increases at least in 548 times. > > So, the patchset makes shrink_slab() of less complexity and improves > the performance in such types of load I pointed. This will give a profit > in case of !global reclaim case, since there also will be less > do_shrink_slab() calls. > > This patchset is made against linux-next.git tree. > > v7: Refactorings and readability improvements. > > v6: Added missed rcu_dereference() to memcg_set_shrinker_bit(). > Use different functions for allocation and expanding map. > Use new memcg_shrinker_map_size variable in memcontrol.c. > Refactorings. > > v5: Make the optimizing logic under CONFIG_MEMCG_SHRINKER instead of MEMCG && !SLOB > > v4: Do not use memcg mem_cgroup_idr for iteration over mem cgroups > > v3: Many changes requested in commentaries to v2: > > 1)rebase on prealloc_shrinker() code base > 2)root_mem_cgroup is made out of memcg maps > 3)rwsem replaced with shrinkers_nr_max_mutex > 4)changes around assignment of shrinker id to list lru > 5)everything renamed > > v2: Many changes requested in commentaries to v1: > > 1)the code mostly moved to mm/memcontrol.c; > 2)using IDR instead of array of shrinkers; > 3)added a possibility to assign list_lru shrinker id > at the time of shrinker registering; > 4)reorginized locking and renamed functions and variables. > > --- > > Kirill Tkhai (16): > list_lru: Combine code under the same define > mm: Introduce CONFIG_MEMCG_KMEM as combination of CONFIG_MEMCG && !CONFIG_SLOB > mm: Assign id to every memcg-aware shrinker > memcg: Move up for_each_mem_cgroup{,_tree} defines > mm: Assign memcg-aware shrinkers bitmap to memcg > mm: Refactoring in workingset_init() > fs: Refactoring in alloc_super() > fs: Propagate shrinker::id to list_lru > list_lru: Add memcg argument to list_lru_from_kmem() > list_lru: Pass dst_memcg argument to memcg_drain_list_lru_node() > list_lru: Pass lru argument to memcg_drain_list_lru_node() > mm: Export mem_cgroup_is_root() > mm: Set bit in memcg shrinker bitmap on first list_lru item apearance > mm: Iterate only over charged shrinkers during memcg shrink_slab() > mm: Add SHRINK_EMPTY shrinker methods return value > mm: Clear shrinker bit if there are no objects related to memcg > > Vladimir Davydov (1): > mm: Generalize shrink_slab() calls in shrink_node() > > > fs/super.c | 11 ++ > include/linux/list_lru.h | 18 ++-- > include/linux/memcontrol.h | 46 +++++++++- > include/linux/sched.h | 2 > include/linux/shrinker.h | 11 ++ > include/linux/slab.h | 2 > init/Kconfig | 5 + > mm/list_lru.c | 90 ++++++++++++++----- > mm/memcontrol.c | 173 +++++++++++++++++++++++++++++++------ > mm/slab.h | 6 + > mm/slab_common.c | 8 +- > mm/vmscan.c | 204 +++++++++++++++++++++++++++++++++++++++----- > mm/workingset.c | 11 ++ > 13 files changed, 478 insertions(+), 109 deletions(-) > > -- > Signed-off-by: Kirill Tkhai