Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4070552pxj; Tue, 15 Jun 2021 14:54:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydagvoDb5c69QtAGz/j1SucWvhOlitmxkIqHCEokqwfrHhBWbZJ7QayQFoo2YIsIkGBpVL X-Received: by 2002:a5e:930d:: with SMTP id k13mr1054481iom.61.1623794067432; Tue, 15 Jun 2021 14:54:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623794067; cv=none; d=google.com; s=arc-20160816; b=HBTuPI7BvLOl1eEVTIBA7So2PsUjJTJVLDMReCfe7JY/BD//EKelVRWhpZV6hHAwSD wic0lBjh4R471VdoVrEQcJ3iqntc8qG5mHN5gfSJzunG1dBCvcjHC6AJqErwHt/+rU3N 7m5espahVmYqSa1y3TrV4ffaf3yQYHT3a9FsSoQkgVGmGtxDjU2J7PfMGtPw5pd4kLy/ IYEtv14nz5kOaLOmElCw/yZZT9Gd7UfvkKwAJ8ORhQIkKSvOnj85etN0QGblXPs5au0Y kvr+7WQHkMnSn9+B8GCyETuedCdTIea/EuLeguAEjNDEVTGhZeaCxq6eKMV9c4wZiGWR 0TFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=0XQnzVjTuP7aBILPyoyVQYQXqE/KQql4/BGG/kkVsjE=; b=tEtoJUb9Btc0ITOnn67CQAva36t6W+N1cfS+OswcrRdSZ3GCwKW0AC7Gr64M0gVL4A naHbIdxxz06PDMuexR5hoHFCyZf4tAr9qqs34VKsZ4xzbJSRIY7JqKwaenvMWOYvvpxT FWlzi4QjDjaBWFgPLV2B8A5c5K4BsO6PJdMMMebJkIeNg2hOiRCrwER0AQN+sYee4Gw7 PGrsUe6c0VnsZsMPf3fBW1pC0kOroqOpmi6YlmXwo5w8sW+0m/f0Tx71hh4DmQBDsD58 WqoYx8XIOUgyCP3nzpJ9c0brRHsWmIUdOqSUr+4X3+XAvfWezJ63Z07UI8MeU19XE12H tUPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gGfsNADV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m3si87601ild.74.2021.06.15.14.54.13; Tue, 15 Jun 2021 14:54:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gGfsNADV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229898AbhFOVyz (ORCPT + 99 others); Tue, 15 Jun 2021 17:54:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbhFOVyz (ORCPT ); Tue, 15 Jun 2021 17:54:55 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE50CC061574 for ; Tue, 15 Jun 2021 14:52:49 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id b37so720318ljr.13 for ; Tue, 15 Jun 2021 14:52:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0XQnzVjTuP7aBILPyoyVQYQXqE/KQql4/BGG/kkVsjE=; b=gGfsNADVzHheLJtshFSvaD/DGD7MMjGpXn9cuW+cqCUX2Xd2zmn5p3v+yDXFjhYjtK PDLnOr1KKcT1OLCV6DmMntBGhrfUy0fxFML9PTdLQwix8Sd1hhPKRkJq6T0G7E4Ih+NA bygB5L8TqSiTWvrpf2PeCWl4nP29lwEvs0p8hAUMjE225soFkN2+WNABhSNuB+euV8Q/ HSj4o5EqiwXTsqhpK8SRG3q2fz0JovM5ppq9RUlGel+tygTg+OEql0sia8kFjIp7n0lC rSEFJnNMUDNnbVoVZyuQrRWa9O1SgCOtrk9ljun/mmLILGGgE16kacXSjSAUEPM4K2jZ iBoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0XQnzVjTuP7aBILPyoyVQYQXqE/KQql4/BGG/kkVsjE=; b=K7KGhUUkSh1tt+6a9bcIreLxGNNoCOAca6zxC+XX++Qv6Ud06ypNpxW+AP+KzDDpcs vBlmo+Nl0JcxKiFRDcJYXaGLLPJG8xFWz4mhKF9/pne8J48HsUtI/yy6zY9STPDxtgfE gpAOYH2+MGdW1BEy917pqSiRpD3T5CionxyHvCmsINr4AkRy5pYU2NZxvtUXghyAXdT6 NyC4N0DlHRebvnibqTkyoFm/PVviJCc76tm0UBeahOLsqhQmCYBGfaYxKa+S/9a1wsaZ XFrUCFJ1ytdrlq0JUOSX0I5tPGuX2eKiB/JnQtSvsQGe4FJ6ayY2137mFV7ShOUEJsfs JYwQ== X-Gm-Message-State: AOAM532IB2s5qIIHREOXzp3bCIZ0q/qQY7S3scDYdJSieCDxAOC+nepm p50+d05yYHDXHCz3yitShcythMaB4XiPWnyIZzZUyg== X-Received: by 2002:a05:651c:3c6:: with SMTP id f6mr1450704ljp.456.1623793968085; Tue, 15 Jun 2021 14:52:48 -0700 (PDT) MIME-Version: 1.0 References: <20210615174435.4174364-1-shakeelb@google.com> <20210615174435.4174364-2-shakeelb@google.com> In-Reply-To: From: Shakeel Butt Date: Tue, 15 Jun 2021 14:52:37 -0700 Message-ID: Subject: Re: [PATCH v2 2/2] memcg: periodically flush the memcg stats To: Johannes Weiner Cc: Tejun Heo , Muchun Song , Michal Hocko , Roman Gushchin , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Huang Ying , Andrew Morton , Cgroups , Linux MM , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 15, 2021 at 12:29 PM Johannes Weiner wrote: > > Hey Shakeel, > > On Tue, Jun 15, 2021 at 10:44:35AM -0700, Shakeel Butt wrote: > > At the moment memcg stats are read in four contexts: > > > > 1. memcg stat user interfaces > > 2. dirty throttling > > 3. page fault > > 4. memory reclaim > > > > Currently the kernel flushes the stats for first two cases. Flushing the > > stats for remaining two casese may have performance impact. Always > > flushing the memcg stats on the page fault code path may negatively > > impacts the performance of the applications. In addition flushing in the > > memory reclaim code path, though treated as slowpath, can become the > > source of contention for the global lock taken for stat flushing because > > when system or memcg is under memory pressure, many tasks may enter the > > reclaim path. > > > > Instead of synchronously flushing the stats, this patch adds support of > > asynchronous periodic flushing of the memcg stats. For now the flushing > > period is hardcoded to 2*HZ but that can be changed later through maybe > > sysctl if need arise. > > I'm concerned that quite a lot can happen in terms of reclaim and page > faults in 2 seconds. It's conceivable that the error of a fixed 2s > flush can actually exceed the error of a fixed percpu batch size. > Yes, that is possible. > The way the global vmstat implementation manages error is doing both: > ratelimiting and timelimiting. It uses percpu batching to limit the > error when it gets busy, and periodic flushing to limit the length of > time consumers of those stats could be stuck trying to reach a state > that the batching would otherwise prevent from being reflected. > > Maybe we can use a combination of ratelimiting and timelimiting too? > > We shouldn't flush on every fault, but what about a percpu ratelimit > that would at least bound the error to NR_CPU instead of nr_cgroups? > Couple questions here: First, to convert the error bound to NR_CPU from nr_cgroups, I think we have to move from (delta > threshold) comparison to (num_update_events > threshold). Previously an increment event followed by decrement would keep the delta to 0 (or same) but after this change num_update_events would be 2. Is that ok? Second, do we want to synchronously flush the stats when we cross the threshold on update or asynchronously by queuing the flush with zero delay? > For thundering herds during reclaim: as long as they all tried to > flush from the root, only one of them would actually need to do the > work, and we could use trylock. If the lock is already taken, you can > move on knowing that somebody is already doing the shared flush work. Yes, this makes sense.