Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp3108286rwp; Fri, 14 Jul 2023 17:54:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlHInnLR03MjywXahC6eteq4sEXbgDoMU1h8Re1kPTj6qKmzxXX/Z+AgHJpyTjf67AKrz5/u X-Received: by 2002:a17:906:24f:b0:993:d88e:41e6 with SMTP id 15-20020a170906024f00b00993d88e41e6mr642993ejl.4.1689382496688; Fri, 14 Jul 2023 17:54:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689382496; cv=none; d=google.com; s=arc-20160816; b=fKtcExY/oXiwFsl7rbLwHiXGjgkBgvsIA4rH75T67dlaeCnCrUYfJQITEh7v7NzCFM IxGOqU1lhS15nD8hW+LrL8kmZ2jPReNptKDPAM/rT+n3oRdnxAIUXDMwBWOqZYoqO3nU f/I7MlmUwQ+pnobYKbR8VG9TYS4KXrTFzBLw9JAMmS23FWGyEYC+kKrmyEmSwJkri4s7 m9yI7SNtHfjohN3yvwg3GPyXALzgjdHga5F5o6ry5cm14BC11Cr8NFrcXOrljYAcGewv /hmH4+O7AdwrReydTRdAqSXqkYbnShdETxlFfOvqUmLoF5V88tEj8k4lNvNLdf8coJR2 uq1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=9qCsYq5AfQu15dBgFcUhM5i+Y9hdaw4Di+rchoqRa1A=; fh=uIJTi+MvVwTQpXy2edvdsmk/o6hpXF+F8XkN0BKpCxA=; b=nqN9pbv2lQCNdQrTkMxDlJVKDZlbI9NLi8Km2tg/xCBr4rPU6LXbB90GT5nPWITZpZ dVTKGRhzK11lbUxDzlntcuDgtWm5T9z6DC9lbC5DKhQBWbnDLFAx0VWxl5p6fBjTdqcO dQ1TKn7TOlR/WHs8/X+ECV1QJI4WII408csXBlcpbuN4fr57Zbl6HyF1LicHAebInLn1 bxUQGuRtVKrgbLcsVhCaWUo0oM1/AbjfUc2VekMgsBtwmbZUplLaoWVck8eTH79re4bH 4/DjcVJGKuiPSSM+iXAIgS7Pz8un8ee0f8ftS9mLOhAD1oDG8PpnFv5uZFqfe+ww56PR EIww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b="w1so6/uR"; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bl1-20020a170906c24100b009889b69c1edsi9867563ejb.697.2023.07.14.17.54.32; Fri, 14 Jul 2023 17:54:56 -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=@cloudflare.com header.s=google header.b="w1so6/uR"; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbjGOABF (ORCPT + 99 others); Fri, 14 Jul 2023 20:01:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229455AbjGOABE (ORCPT ); Fri, 14 Jul 2023 20:01:04 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 000893A84 for ; Fri, 14 Jul 2023 17:01:02 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3142a9ffa89so2699468f8f.0 for ; Fri, 14 Jul 2023 17:01:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; t=1689379261; x=1691971261; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=9qCsYq5AfQu15dBgFcUhM5i+Y9hdaw4Di+rchoqRa1A=; b=w1so6/uRCSkF2JfOE4Lp7UEUm7H2/SnJOrUe5GP0/6Hoc5lu/q59ef2oEAIUdNGlPu 1oBomtwOZE28hBXuVf9l6r3h/U7PyWOeBibY6Emk7xzVuyXK6RJfTioxx73MA4Ej67Ft gK1kNyPKvTTQneIkrmuHgWBjmDfq96bq6Y21Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689379261; x=1691971261; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9qCsYq5AfQu15dBgFcUhM5i+Y9hdaw4Di+rchoqRa1A=; b=bv+P5BhSVrjXwFyGiJGlBNYX1n7nBAF9+gaNLKRlnUW1nunob7yx1E8iTXamKFKmWn w7GsWCSDefRI98aGJGkq/CQry+Mtjb4/O+xfm6o2jJHmhzstiNhPqJTm5XehNYWFE/cX 8Ixi5vjFu4QzyAH9oxdpqElew/HUevMDDjNpYr8yXKKuaI5yS2sW2YY9V0xjBvFfQ4j7 G8z4PYV0xmfJlJbv+IyOQpWQKEn21H39H6GnT6cGutRHIPFViyN2YLcgGNRYjc8Ei19u S9NI4fkYFVys0Yg/Z2QOUZEMnwhD78uBZzKsOWr5e5leVFNY4fzACu9hwv54hn0SqwvZ akDA== X-Gm-Message-State: ABy/qLaacNDdnmqjFQBGTOCfQ3rADxoNm0VeHXOc6FO7LXG7mtr7FY31 9VEU9kNcZ2H7dsP3v38pFo94wPxYHQfUAzTh7ObaIQ== X-Received: by 2002:a5d:66c5:0:b0:316:e073:e547 with SMTP id k5-20020a5d66c5000000b00316e073e547mr5135314wrw.28.1689379261115; Fri, 14 Jul 2023 17:01:01 -0700 (PDT) MIME-Version: 1.0 References: <20230706062045.xwmwns7cm4fxd7iu@google.com> In-Reply-To: From: Ivan Babrou Date: Fri, 14 Jul 2023 17:00:50 -0700 Message-ID: Subject: Re: Expensive memory.stat + cpu.stat reads To: Waiman Long Cc: Shakeel Butt , cgroups@vger.kernel.org, Linux MM , kernel-team , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Andrew Morton , linux-kernel Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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 Fri, Jul 14, 2023 at 10:23=E2=80=AFAM Waiman Long w= rote: > > On 7/13/23 19:25, Ivan Babrou wrote: > > On Mon, Jul 10, 2023 at 5:44=E2=80=AFPM Waiman Long wrote: > >> On 7/10/23 19:21, Ivan Babrou wrote: > >>> On Wed, Jul 5, 2023 at 11:20=E2=80=AFPM Shakeel Butt wrote: > >>>> On Fri, Jun 30, 2023 at 04:22:28PM -0700, Ivan Babrou wrote: > >>>>> Hello, > >>>>> > >>>>> We're seeing CPU load issues with cgroup stats retrieval. I made a > >>>>> public gist with all the details, including the repro code (which > >>>>> unfortunately requires heavily loaded hardware) and some flamegraph= s: > >>>>> > >>>>> * https://gist.github.com/bobrik/5ba58fb75a48620a1965026ad30a0a13 > >>>>> > >>>>> I'll repeat the gist of that gist here. Our repro has the following > >>>>> output after a warm-up run: > >>>>> > >>>>> completed: 5.17s [manual / mem-stat + cpu-stat] > >>>>> completed: 5.59s [manual / cpu-stat + mem-stat] > >>>>> completed: 0.52s [manual / mem-stat] > >>>>> completed: 0.04s [manual / cpu-stat] > >>>>> > >>>>> The first two lines do effectively the following: > >>>>> > >>>>> for _ in $(seq 1 1000); do cat /sys/fs/cgroup/system.slice/memory.s= tat > >>>>> /sys/fs/cgroup/system.slice/cpu.stat > /dev/null > >>>>> > >>>>> The latter two are the same thing, but via two loops: > >>>>> > >>>>> for _ in $(seq 1 1000); do cat /sys/fs/cgroup/system.slice/cpu.stat= > > >>>>> /dev/null; done > >>>>> for _ in $(seq 1 1000); do cat /sys/fs/cgroup/system.slice/memory.s= tat > >>>>>> /dev/null; done > >>>>> As you might've noticed from the output, splitting the loop into tw= o > >>>>> makes the code run 10x faster. This isn't great, because most > >>>>> monitoring software likes to get all stats for one service before > >>>>> reading the stats for the next one, which maps to the slow and > >>>>> expensive way of doing this. > >>>>> > >>>>> We're running Linux v6.1 (the output is from v6.1.25) with no patch= es > >>>>> that touch the cgroup or mm subsystems, so you can assume vanilla > >>>>> kernel. > >>>>> > >>>>> From the flamegraph it just looks like rstat flushing takes longe= r. I > >>>>> used the following flags on an AMD EPYC 7642 system (our usual pick > >>>>> cpu-clock was blaming spinlock irqrestore, which was questionable): > >>>>> > >>>>> perf -e cycles -g --call-graph fp -F 999 -- /tmp/repro > >>>>> > >>>>> Naturally, there are two questions that arise: > >>>>> > >>>>> * Is this expected (I guess not, but good to be sure)? > >>>>> * What can we do to make this better? > >>>>> > >>>>> I am happy to try out patches or to do some tracing to help underst= and > >>>>> this better. > >>>> Hi Ivan, > >>>> > >>>> Thanks a lot, as always, for reporting this. This is not expected an= d > >>>> should be fixed. Is the issue easy to repro or some specific workloa= d or > >>>> high load/traffic is required? Can you repro this with the latest li= nus > >>>> tree? Also do you see any difference of root's cgroup.stat where thi= s > >>>> issue happens vs good state? > >>> I'm afraid there's no easy way to reproduce. We see it from time to > >>> time in different locations. The one that I was looking at for the > >>> initial email does not reproduce it anymore: > >> My understanding of mem-stat and cpu-stat is that they are independent > >> of each other. In theory, reading one shouldn't affect the performance > >> of reading the others. Since you are doing mem-stat and cpu-stat readi= ng > >> repetitively in a loop, it is likely that all the data are in the cach= e > >> most of the time resulting in very fast processing time. If it happens > >> that the specific memory location of mem-stat and cpu-stat data are su= ch > >> that reading one will cause the other data to be flushed out of the > >> cache and have to be re-read from memory again, you could see > >> significant performance regression. > >> > >> It is one of the possible causes, but I may be wrong. > > Do you think it's somewhat similar to how iterating a matrix in rows > > is faster than in columns due to sequential vs random memory reads? > > > > * https://stackoverflow.com/q/9936132 > > * https://en.wikipedia.org/wiki/Row-_and_column-major_order > > * https://en.wikipedia.org/wiki/Loop_interchange > > Yes, it is similar to what is being described in those articles. > > > > > > I've had a similar suspicion and it would be good to confirm whether > > it's that or something else. I can probably collect perf counters for > > different runs, but I'm not sure which ones I'll need. > > > > In a similar vein, if we could come up with a tracepoint that would > > tell us the amount of work done (or any other relevant metric that > > would help) during rstat flushing, I can certainly collect that > > information as well for every reading combination. > > The perf-c2c tool may be able to help. The data to look for is how often > the data is from caches vs direct memory load/store. It looks like c2c only works for the whole system, not individual treads. There's a lot of noise from the rest of the system.