Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp474067rwb; Tue, 25 Jul 2023 20:02:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlHMEfzUMlufbXqkeCfP0ZK/Al+ypCcDfCLRtQFG6MzIZwJE34HMRU+wzd4LbXu+sCluM3XY X-Received: by 2002:a17:906:315b:b0:993:d88e:41ed with SMTP id e27-20020a170906315b00b00993d88e41edmr530654eje.3.1690340558545; Tue, 25 Jul 2023 20:02:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690340558; cv=none; d=google.com; s=arc-20160816; b=q2UrOYuM0qt4wLVFcBceivk8V2yR73Q0j1Rijqt7nn+Lmjz0MuHVRpeB2Y5YOWG2j+ VSLPpMjXglKv+URpy9quimKP/xfJ67On89vgppQpBfYqUd0HJhp+wjC72G0vSs3OkxYR QCUZ4gbB+co5l4i8IAhzoMO6uB8qlEpulFKfcAzJbgfOVRAkYXsx2EZf4ktN4eyY+r81 W6sQGL0GR9SJx833FubPzKYiOvis8EmAyg7ajnIev5B4Wjh2HDUzdJThRh7aaqW1aZ55 hX3q+a5RHVsKthqT6jSZTJFlJGo75DTAVCFGB+UF2izDnGBtNCMYWgNFO8DHsSiIbq9r LnZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=LkLUCGJT74cllQvTQ6MTx9QgnJE9Fo6dHQUygyCQSWc=; fh=z/dIvAw+pPw7C8AUhTua7BIjxfl1EG008tJ6lsQfBF0=; b=GrWIB/l8o6ROZM+fRybQPYkEnoN3a3c8s2XekqJYA3kFx94eBsiTLxPUA+qBN0CxIN IAIeFl1xibiayUg2RarX6XqiyJrldhskhmScypcA8LFgPPTRIgKA53DlCcETTm0TwCKf K8odftaSlWLrry6i0Wzlej4z+PnkatuMD6uTYZKSao4MS7vgALEkyV/Ay1nUmGUTuIdP 4XEu27gM9yfZPE2xa33Bfsrocr2zmPB7llk+q3vsb7EPmNZF1skit25kniM9NBE0Do50 dZokjyhpE9B3lweMiXXepiPZVloQw0Pj4KNkpljnPOT1PxmuCcg89oSI57bJfNNKgu7W Xhtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=P59UCGFi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lf5-20020a170907174500b00986f786c94asi9186161ejc.113.2023.07.25.20.02.12; Tue, 25 Jul 2023 20:02:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=P59UCGFi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229821AbjGZCP4 (ORCPT + 99 others); Tue, 25 Jul 2023 22:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229520AbjGZCPz (ORCPT ); Tue, 25 Jul 2023 22:15:55 -0400 Received: from out-18.mta0.migadu.com (out-18.mta0.migadu.com [91.218.175.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CD701BD6 for ; Tue, 25 Jul 2023 19:15:53 -0700 (PDT) Date: Tue, 25 Jul 2023 19:15:41 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690337752; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=LkLUCGJT74cllQvTQ6MTx9QgnJE9Fo6dHQUygyCQSWc=; b=P59UCGFim3QDtZSMOt0VwRb3ocDo8l1wi2zSA6eSN/BqU/YIRayerwirvZbuLcLx1R37DN YdCcgNxDBq30Q2BEtIH4BuGRjBqYNWesRuyquAwbIs9iakR3r3SbyA4SCffn+s846tmvRs qjD5LYy9+6Me2P46VLNrPfjgPrgZvp8= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Yosry Ahmed Cc: Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v2] mm: memcg: use rstat for non-hierarchical stats Message-ID: References: <20230726002904.655377-1-yosryahmed@google.com> <20230726002904.655377-2-yosryahmed@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230726002904.655377-2-yosryahmed@google.com> X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 26, 2023 at 12:29:04AM +0000, Yosry Ahmed wrote: > Currently, memcg uses rstat to maintain hierarchical stats. Counters are > maintained for hierarchical stats at each memcg. Rstat tracks which > cgroups have updates on which cpus to keep those counters fresh on the > read-side. > > For non-hierarchical stats, we do not maintain counters. Instead, the global? > percpu counters for a given stat need to be summed to get the > non-hierarchical stat value. The original implementation did the same. > At some point before rstat, non-hierarchical counters were introduced by > commit a983b5ebee57 ("mm: memcontrol: fix excessive complexity in > memory.stat reporting"). However, those counters were updated on the > performance critical write-side, which caused regressions, so they were > later removed by commit 815744d75152 ("mm: memcontrol: don't batch > updates of local VM stats and events"). See [1] for more detailed > history. > > Kernel versions in between a983b5ebee57 & 815744d75152 (a year and a > half) enjoyed cheap reads of non-hierarchical stats, specifically on > cgroup v1. When moving to more recent kernels, a performance regression > for reading non-hierarchical stats is observed. > > Now that we have rstat, we know exactly which percpu counters have > updates for each stat. We can maintain non-hierarchical counters again, > making reads much more efficient, without affecting the performance > critical write-side. Hence, add non-hierarchical (i.e local) counters > for the stats, and extend rstat flushing to keep those up-to-date. > > A caveat is that we now a stats flush before reading need? > local/non-hierarchical stats through {memcg/lruvec}_page_state_local() > or memcg_events_local(), where we previously only needed a flush to > read hierarchical stats. Most contexts reading non-hierarchical stats > are already doing a flush, add a flush to the only missing context in > count_shadow_nodes(). > > With this patch, reading memory.stat from 1000 memcgs is 3x faster on a > machine with 256 cpus on cgroup v1: > # for i in $(seq 1000); do mkdir /sys/fs/cgroup/memory/cg$i; done > # time cat /dev/cgroup/memory/cg*/memory.stat > /dev/null > real 0m0.125s > user 0m0.005s > sys 0m0.120s > > After: > real 0m0.032s > user 0m0.005s > sys 0m0.027s > > [1]https://lore.kernel.org/lkml/20230725201811.GA1231514@cmpxchg.org/ > > Signed-off-by: Yosry Ahmed > Acked-by: Johannes Weiner Acked-by: Roman Gushchin Thank you!