Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2756440rwp; Fri, 14 Jul 2023 10:48:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlHs/kQQ9WCsYceooIzOtJPGYithocayxyGuFpCzzdf6MnL742J0CqL6psd5dfPn97cw/rUd X-Received: by 2002:a17:907:3f1b:b0:96f:9cea:a34d with SMTP id hq27-20020a1709073f1b00b0096f9ceaa34dmr4379762ejc.21.1689356902040; Fri, 14 Jul 2023 10:48:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689356902; cv=none; d=google.com; s=arc-20160816; b=hy++Y3CLY+QaL69CJy/L4mKsm/vIohA5A0Ua9ndnRS7XI4LfvMNezXwOazLgPRqQ5P u76T5Z7NU47gMNf6hNp+FXMLJS0TLWE8J+sQmHs6Mx+c3TbN1Yla3lzunrQuERYcO4tm fK8HrmWbxcPOSP27M6pP4uC+Ua/jL9mqMClRXRSP3gTHEvhJMza3JYSTOPMBFp2xd4Dc AOCtIQvbo/z3AAUL6TxfhTPR5+9FqZnFZTBdNHchak9Od2BlLH1OkKOuBvSKib2+Okin E7n1eHRxP04BZQ3ZZn/hmrzcS7JmgRmLhYfeDrmK5ZHoSYCOi9R4ZGM62295Xv1jfOjf P0Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=IjSCUEVQqiObQodmnZyPQvDEZ31gPn7XJ/bLLSzzo2c=; fh=d1SNCp0uQnBHa27Y03z4xO0sOyswYd3GTbrna9969Q4=; b=DJJN7wqPaqZe0QhGq+3omSXhn3PmjEO2tWyWvFB4q2MvK2NWMrz2z+w2RBBbyFPhmD hgCGKk4sRPdLkK5Fq1PKhSTcBhHWCGZs/I1/Hm6hw44ufh+Lha1hIU6JtV9LPDsz0p5n tTOfGhP3MfxKYvTRvFUhICuY6bRaURPg1EOrhAIvug2hLXJ7GZxWuYG8PpdVuaFzG9Lb u/TDd2Ef2sGEkR3yg9t2RFODZU1dAZFaDD+OHnSo5I4yQ8sFjAohLEErM9H0Nao5jTZh FkEODP/Ia4ateQ42XwNfK6NDEo83+zg1XGAF/q3AFhdGYyXQUqLtYimYpDbeR8EWh7Eu 87FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YolIWghA; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i20-20020a170906115400b00992acfbcee1si8925793eja.851.2023.07.14.10.47.58; Fri, 14 Jul 2023 10:48:22 -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=@redhat.com header.s=mimecast20190719 header.b=YolIWghA; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230093AbjGNRYh (ORCPT + 99 others); Fri, 14 Jul 2023 13:24:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233301AbjGNRYg (ORCPT ); Fri, 14 Jul 2023 13:24:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAE682691 for ; Fri, 14 Jul 2023 10:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689355427; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IjSCUEVQqiObQodmnZyPQvDEZ31gPn7XJ/bLLSzzo2c=; b=YolIWghA4llRqvQWD1hyTOG0g0WM9IPvPbVJMwgH5h1IFNqBju2y2c6FHYvDsrjLrIQ0rq mYvfYQ/aTvCFEJQo7Hi/0bVTf5bLgHAfDjQ4ZPrdYhuwiUVe0dtLiw5hBzbbjMNx1/IRGT kIt0dJbXu57F5CkKJ6vImspD5umVdgM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-250-1q70EXljPLGuQ4wmDRDicA-1; Fri, 14 Jul 2023 13:23:44 -0400 X-MC-Unique: 1q70EXljPLGuQ4wmDRDicA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E4614185A78F; Fri, 14 Jul 2023 17:23:43 +0000 (UTC) Received: from [10.22.9.81] (unknown [10.22.9.81]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35CD5C2C856; Fri, 14 Jul 2023 17:23:43 +0000 (UTC) Message-ID: Date: Fri, 14 Jul 2023 13:23:42 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: Expensive memory.stat + cpu.stat reads Content-Language: en-US To: Ivan Babrou Cc: Shakeel Butt , cgroups@vger.kernel.org, Linux MM , kernel-team , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Andrew Morton , linux-kernel References: <20230706062045.xwmwns7cm4fxd7iu@google.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 7/13/23 19:25, Ivan Babrou wrote: > On Mon, Jul 10, 2023 at 5:44 PM Waiman Long wrote: >> On 7/10/23 19:21, Ivan Babrou wrote: >>> On Wed, Jul 5, 2023 at 11:20 PM 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 flamegraphs: >>>>> >>>>> * 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.stat >>>>> /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.stat >>>>>> /dev/null; done >>>>> As you might've noticed from the output, splitting the loop into two >>>>> 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 patches >>>>> that touch the cgroup or mm subsystems, so you can assume vanilla >>>>> kernel. >>>>> >>>>> From the flamegraph it just looks like rstat flushing takes longer. 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 understand >>>>> this better. >>>> Hi Ivan, >>>> >>>> Thanks a lot, as always, for reporting this. This is not expected and >>>> should be fixed. Is the issue easy to repro or some specific workload or >>>> high load/traffic is required? Can you repro this with the latest linus >>>> tree? Also do you see any difference of root's cgroup.stat where this >>>> 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 reading >> repetitively in a loop, it is likely that all the data are in the cache >> 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 such >> 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. Cheers, Longman