Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp3489999rwo; Mon, 24 Jul 2023 11:49:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlGd1eXrynFyagLBu7rN0CDG3R744G+aG06amJiF3lCvvoE/rB/kMMPPnU1aKxZC13uR3+5d X-Received: by 2002:a17:907:7746:b0:99b:8ed2:8ff with SMTP id kx6-20020a170907774600b0099b8ed208ffmr5464930ejc.1.1690224588307; Mon, 24 Jul 2023 11:49:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690224588; cv=none; d=google.com; s=arc-20160816; b=Jk1fyIjQJnhi0EBiT2UaZ6RHYxFzR0/arwSlasfYzcvHXvt9oNWAQG4uSoU3L5oh9u +MktiqF8cz8uyGCF2h18LFGIYj3+9xpHNxVz1BNmzF9NQyRz3XKidy1VKZaq6ZAl1Iud yIL8uEAXvJGDLsp/czpFw+vRVrlREsmucJJGBZYwwW+brmaZKhI3Qfn0vMtg8bN7Z9Nn fGqH1DxFSCegQKokPy7WZYrzjkVLEKY2CITT/Fve6L7oBzFm9b72sQdhlB/D67kkfK7F EcW8c/OVHyellz8X2V0Pfixp/Jot1KJ0nu0kz2UozVx0KEbf9CB7gvMD5h4Uw1l715AB pQHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=FIh5Dlkd1rVkTGMA3fSOoROkYOEVPiaTlUaZ/96CdNw=; fh=ru85sUnPbrQq65ZCP/+ozrZb6L0PANhU4wqOvnlJ6aE=; b=bswOZD9gdQ9X4mfiPRVR/+h8GqlWw1rK4qfHosinWK6aoP7J1WPaW0PibyM+i0A13n rwYIg3CYSsVVuuidUtnwGFi4q/GlRRjD1zk7Qpw1CyhxWVVMRXAMLz7xklxMbCcV9TeP yACJ7IqQ1l7jQbbwkYrDXI+dPl6dTDAoCCWRPTrbz8bzBp/rtJ3iwqjq7gXMijlgJSZb rzwjuhigpd2G8haPpVnV3SjgPlfnkJwz6q2PRWNDGU1JfNtkmBGbq7N7TNNcdyzFi5SB 1i+Z0zmWBj/C0pK8+PgcT158ClJkphKvgITRr8SxkwiyZRirCp6bfGcMhW2X01yZ9Wcd bO9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=IMhdN1V+; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c24-20020a170906925800b00997bd42d213si6830770ejx.165.2023.07.24.11.49.21; Mon, 24 Jul 2023 11:49:48 -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-foundation.org header.s=korg header.b=IMhdN1V+; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230188AbjGXSbJ (ORCPT + 99 others); Mon, 24 Jul 2023 14:31:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbjGXSbH (ORCPT ); Mon, 24 Jul 2023 14:31:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE3D3A6; Mon, 24 Jul 2023 11:31:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6DE9561309; Mon, 24 Jul 2023 18:31:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54D3DC433C7; Mon, 24 Jul 2023 18:31:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1690223465; bh=VYtMyHmTjfqRNqrGlaslEXmtuJkxe6wetxm/yy7dEvU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=IMhdN1V+KZF00P6hoKJX2tFLxgM5UA9dcRRI6pwyjwKF9aiDNJHJNlMHzyCOnn1d+ 2oQpxdGmHvtLO7jB1HOLpLMtFmtcQ67BKJJH1qJ0ytJZRTyZYoQZKKi2BJcvsCvYuh 10MseRD2bM18o525xvpf+5RgkOcgjQ1VCFYGA7SY= Date: Mon, 24 Jul 2023 11:31:04 -0700 From: Andrew Morton To: Yosry Ahmed Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] mm: memcg: use rstat for non-hierarchical stats Message-Id: <20230724113104.6994bd471fb926ceeaf46707@linux-foundation.org> In-Reply-To: <20230719174613.3062124-1-yosryahmed@google.com> References: <20230719174613.3062124-1-yosryahmed@google.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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, 19 Jul 2023 17:46:13 +0000 Yosry Ahmed wrote: > Currently, memcg uses rstat to maintain hierarchical stats. The rstat > framework keeps track of which cgroups have updates on which cpus. > > For non-hierarchical stats, as memcg moved to rstat, they are no longer > readily available as counters. Instead, the percpu counters for a given > stat need to be summed to get the non-hierarchical stat value. This > causes a performance regression when reading non-hierarchical stats on > kernels where memcg moved to using rstat. This is especially visible > when reading memory.stat on cgroup v1. There are also some code paths > internal to the kernel that read such non-hierarchical stats. > > It is inefficient to iterate and sum counters in all cpus when the rstat > framework knows exactly when a percpu counter has an update. Instead, > maintain cpu-aggregated non-hierarchical counters for each stat. During > an rstat flush, keep those updated as well. When reading > non-hierarchical stats, we no longer need to iterate cpus, we just need > to read the maintainer counters, similar to hierarchical stats. > > A caveat is that we now a stats flush before reading > 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 > I'll queue this for some testing, pending reviewer input, please.