Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1319023rwn; Thu, 8 Sep 2022 17:44:08 -0700 (PDT) X-Google-Smtp-Source: AA6agR7aeyIOyC2JmnTHwrO5igY5qNeR4Rc5N5ULcekZydQ3xB74u4X8uUGWMNasIgWSWUEq1G/S X-Received: by 2002:a63:1257:0:b0:42f:fbe8:3ac0 with SMTP id 23-20020a631257000000b0042ffbe83ac0mr10064040pgs.412.1662684248335; Thu, 08 Sep 2022 17:44:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662684248; cv=none; d=google.com; s=arc-20160816; b=J4bAAqFnLibWATuGEKn5+Qv7KOKsM1mLTXotLejDy7Vk+p1BhUzW68w/Ixr5spOSPJ /+wNHm/7KVG9oUH18hXEML/glN42Ii518XzzW5G12BAhD2Mv5z8A47/9WXRynX31us+d m6YCPKw+dh3USObzxAWf7PGwDMo/rXJ0nGFwOZukZhjr875WnCh4RckRSmb5DdICyXQI quUE5CBf2qlHOXWsQaMFH7/IlEspws8jcClkQtWaRT/9x1p7z76wnIvngNQWql6KaD1F /Fjc8PqUAM1ctzXVsm3ZC+yrhPN1zmsKXiMA799OtY3QPu6QBhPLaXKw8xsYEMxjttid E3JQ== 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:date:dkim-signature; bh=RUcUYAJZGU901kXStidccK27lsK7j2zOm94hwmU7LWc=; b=yHyjk72fpNpQxkPaf0IFQd/d44hp2TfieGgkyoXg+CCQyJwG6l/+jTBL5xf+Oj0C/3 gSqMQWq+k5P+vdsH1k34DPm5Cy20sBRhWlFYsbt5rFa8bA+yIn1dw1b9R07dc3EiCNNp qY8gnJRSnGOOBWhmNAdTQ6wCH9KxAwtV1bEVBmqxYDLH9yFmRnAfqTLciz1QOm+1Pe/c nm1xxzMGQppITSZk8Z4U+xjfY6nJBxEDRy1Iip6BAv+Qg5C2u/gjYjiRTiM1MqYEiNCj 4v48u9UXpjJC3aqHMdwJSCUzdh5QZ6jCIs8HjmVHtK/jije2SnysZ5mqNFlpz9M8ewGH Hk7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=p3QIvpvv; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t71-20020a63814a000000b0043507dbae6csi173136pgd.681.2022.09.08.17.43.46; Thu, 08 Sep 2022 17:44:08 -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=@suse.com header.s=susede1 header.b=p3QIvpvv; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229589AbiIIAXM (ORCPT + 99 others); Thu, 8 Sep 2022 20:23:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbiIIAXJ (ORCPT ); Thu, 8 Sep 2022 20:23:09 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41AD689822; Thu, 8 Sep 2022 17:23:06 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 8F3071F88C; Fri, 9 Sep 2022 00:23:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1662682984; h=from:from:reply-to: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=RUcUYAJZGU901kXStidccK27lsK7j2zOm94hwmU7LWc=; b=p3QIvpvvl1+OlHebZtJU5545iMnFcY++IJbcSYaNlcbllQWhTrKccHI+ZKQu1Ppix3rQsk 78KYNcOprAr6pSy/mMr57cubhvkeX0n6mcs8iwaIN/7DOBLF73HQEwlRHVr5dm3vMsV/zo FlmTC47AB1hzLgp6jz/TBPL5aSIfWqc= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 56B4013A93; Fri, 9 Sep 2022 00:23:04 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id LgceFGiHGmOvLwAAMHmgww (envelope-from ); Fri, 09 Sep 2022 00:23:04 +0000 Date: Fri, 9 Sep 2022 02:23:02 +0200 From: Michal =?iso-8859-1?Q?Koutn=FD?= To: Shakeel Butt Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/3] memcg: reduce size of memcg vmstats structures Message-ID: References: <20220907043537.3457014-1-shakeelb@google.com> <20220907043537.3457014-4-shakeelb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220907043537.3457014-4-shakeelb@google.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Hello. On Wed, Sep 07, 2022 at 04:35:37AM +0000, Shakeel Butt wrote: > /* Subset of vm_event_item to report for memcg event stats */ > static const unsigned int memcg_vm_event_stat[] = { > + PGPGIN, > + PGPGOUT, > PGSCAN_KSWAPD, > PGSCAN_DIRECT, > PGSTEAL_KSWAPD, What about adding a dummy entry at the beginning like: static const unsigned int memcg_vm_event_stat[] = { + NR_VM_EVENT_ITEMS, + PGPGIN, + PGPGOUT, PGSCAN_KSWAPD, PGSCAN_DIRECT, > @@ -692,14 +694,30 @@ static const unsigned int memcg_vm_event_stat[] = { > #endif > }; > > +#define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) > +static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; > + > +static void init_memcg_events(void) > +{ > + int i; > + > + for (i = 0; i < NR_MEMCG_EVENTS; ++i) > + mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; Start such loops from i = 1, save i to the table. > +} > + > +static inline int memcg_events_index(enum vm_event_item idx) > +{ > + return mem_cgroup_events_index[idx] - 1; > +} And the there'd be no need for the reverse transforms -1. I.e. it might be just a negligible micro-optimization but since the event updates are on some fast (albeit longer) paths, it may be worth sacrificing one of the saved 8Bs in favor of no arithmetics. What do you think about this? > static unsigned long memcg_events(struct mem_cgroup *memcg, int event) > { > - return READ_ONCE(memcg->vmstats->events[event]); > + int index = memcg_events_index(event); > + > + if (index < 0) > + return 0; As a bonus these undefined maps could use the zero at the dummy location without branch (slow paths though). > @@ -5477,7 +5511,7 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) > parent->vmstats->state_pending[i] += delta; > } > > - for (i = 0; i < NR_VM_EVENT_ITEMS; i++) { > + for (i = 0; i < NR_MEMCG_EVENTS; i++) { I applaud this part :-) Michal