Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp566294lqp; Thu, 21 Mar 2024 09:03:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX6Oa01eATrtSfbsVyGul1cHz/HBPiWbhJWYmTEirGHad6cxvkR7pENbpuodLWKA3vPCMnSNMl0/W1u9Zt2b/E2k3BbqQn1Z05M2++86Q== X-Google-Smtp-Source: AGHT+IGtZ9mSWYx1MlfKW3WvipUf71FWp53XmI1QUxzymljsJdX51du+zOd+wbQ++mjqsTkYTnmv X-Received: by 2002:a17:907:394:b0:a45:511:1533 with SMTP id ss20-20020a170907039400b00a4505111533mr1537853ejb.75.1711037001994; Thu, 21 Mar 2024 09:03:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711037001; cv=pass; d=google.com; s=arc-20160816; b=XuzZqdJnrBJjBfLR5iznbui1VMZUg9p1TKvDxerQ58T+YrmaOsKaPurNFtAY9RLjEl RN4ZkUMA2VkgJEl7q4H4GGwVZdto9VLTA+7gGbjb1qxw5ilUmiLGLygXlbszJ/Ilws8A cXSJu49GOTgyOo649yG2dyCdN9JYQAZMOiFgzGQQrudQ79X228xoIko/Bj+U1NsQVOly 1eoWvxPu+xRoFlatqvj25FhsMwbm1E8d/FSF5ebDEZphzCMSrOLmw8+qynTRutrWRqPu TU66pj2/VzBU0vjbeXg0SzxQNIvvY7OPzBFE598UCPzNBxXi7eIk99O46SGyT/M7WiRa 3YxA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:date:dkim-signature; bh=bTRt1HiPuKsc9DFIQ0MUxj2NIV+5m7aSyK2vQDOvUXE=; fh=wy8DNViL5SWGFy+rPZpDzhlBqaePek+ipuzqAsDIp5E=; b=WPqimlN2fJNjG/VgVpusUHCL3qsmGeML8KedGSihcI5+EVjeYpaF1yreCMWFhpTSCe btjV/hmW9zxH9/qxkuyFZJXHTeAr28OpgWGUH2yxzH4sAeYHVs65ZLw0rDFosCDNbTrl Y1b7AxmR2zwKSLOn10IZ161MHuOPqOkgodIswr5BphbcSnLgsSNEejDZRhljv+Md0w5m DmLBE7O56b8LZyPt7zXRF/k2duKF90m0q/4s/80PmoHvi3MGkXqwJxgHT3F8Weeh++aG qi04r7+CLP4M+jIjbES23H3e9GJNutQangIOWpFYcQnHfy/vUXf+7cXa/ST/K9vQGguj reHQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="p5/3wQnM"; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110295-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110295-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s13-20020a170906a18d00b00a4671ca019bsi62171ejy.80.2024.03.21.09.03.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 09:03:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110295-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="p5/3wQnM"; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110295-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110295-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8DF0C1F21C35 for ; Thu, 21 Mar 2024 16:03:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7205B12BF3A; Thu, 21 Mar 2024 16:03:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p5/3wQnM" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D186012BF1A for ; Thu, 21 Mar 2024 16:03:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711036991; cv=none; b=Oh1Ll8hYujYjJW7hMYdb4zFxm4iZ76fVhVLixZMOIjJ4A78iIQBa6MrdNuazCaDQ1tZmyE95nxRSe9T+CYH583rrhuVVGm+9pk2gtfHbXqQjyQWXiZhHnpL+0jUjsEOHBYz9ZT/vMWimEEURUImVttlaaDntjSKk81f4g3dVjkI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711036991; c=relaxed/simple; bh=nB1k5uaFBqYaBOpNNY+26UH1wVO5501tHpsS/EBiFYQ=; h=Date:Message-Id:Mime-Version:Subject:From:To:Content-Type; b=f42vvCz5fRaxRBPtRkV/OuoWwykUjzUL3T/XJvDO/TtNBOgCX5ZjGXviDofgsdnJKXPPfSXN6g3bY6LqxDcjE4df5KwUTFWVmP0Cl2gsWvQS/4kqbel70WVP+kAJSYf6Gc0D5BKhTwK0faTc+ww3qDNfVkKYyrp6EpSxSpJzIz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p5/3wQnM; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26845cdso1547435276.3 for ; Thu, 21 Mar 2024 09:03:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711036987; x=1711641787; darn=vger.kernel.org; h=to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=bTRt1HiPuKsc9DFIQ0MUxj2NIV+5m7aSyK2vQDOvUXE=; b=p5/3wQnM3cqV4w8xndCAPuTFpxQw1lwaQ47lv1lUULB9H6T7p5cYS5ww5UhUZ6q8+p IIsH8CxvvCrJKd94DCyGnZPyGG63IdpvEF6qnliEsWEvdD7UlRPYipz+8fGcI55+dQhx Sgdokqtcu7kVS13PCur9/nDmazc5cNZ7yRvNapsy7ibjs94CAhP51LrmBCkGDGMaCzdn QSpwsH86JMaeFSFi5XDHcIKqAP0RdTT7nb9BaylswtIck4TA5Sskx/zDWSmDtsRWcCSS VmBsKpOHveLJNY97kWEG6R/rl0NuDoaKzja71DgsZazo9DXq4NKXlSa7ItODvZ6Bbebf ZA6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711036987; x=1711641787; h=to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=bTRt1HiPuKsc9DFIQ0MUxj2NIV+5m7aSyK2vQDOvUXE=; b=wEBVJjfgfiI5Fm85DB2M9thOS8PgZMOAF26mIofmMOrVMPpfbBU0c8Wvd8haElGCp7 KHOBYtiOkKGKHhWbEaCeTxMIQZzPkNa/uy2EB7T6O/CxfqD5dPoRJ2DwAdvRSXbs29e3 e5QqKhhoz55eWO8oxvvuc5MzTm0R4AT7/iSYkRiNIOOX36asoE99IVZnrLjAW2/kFi1C zUhtblr51JHiXM1VntvEccEqQPKrecwlXhcFG22ZF13hldTUvtdoIQsBhkFkywQBGft6 qfB3BMMgU8ldpRAV3kgv37FwBfku2VD4L4csgjjmjsKQZi1uKko4bRdlFLexbG3CZ51E 0ENA== X-Forwarded-Encrypted: i=1; AJvYcCXheDRKY1V8TkjlK9lnyy7Il5RzbfUFRZPbABNfhVVCqGND4QU8nDQM+DkXy4snjq2CxDCP1lxf4d01BST4vb8Gum5Oyn0CEO8EPulb X-Gm-Message-State: AOJu0Yw42S1RSGkfRUdvwFY6y9xjM1lkm18K6W33iy5VEVu0rIyF7YEw CnWrXd5K3s9lpzaAXNDlVcrB56UzDkhtfJK8QhP8X5OMzGllLg7HE4ohIBa/fMIxwQiDuplEZDh tbJAP9w== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7f3:cf74:42c4:c0a]) (user=irogers job=sendgmr) by 2002:a05:6902:100e:b0:dc9:5ef8:2b2d with SMTP id w14-20020a056902100e00b00dc95ef82b2dmr5504757ybt.4.1711036986806; Thu, 21 Mar 2024 09:03:06 -0700 (PDT) Date: Thu, 21 Mar 2024 09:02:47 -0700 Message-Id: <20240321160300.1635121-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Subject: [PATCH v2 00/13] dso/dsos memory savings and clean up From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Athira Rajeev , Colin Ian King , "=?UTF-8?q?Ahelenia=20Ziemia=C5=84ska?=" , Leo Yan , Song Liu , Miguel Ojeda , Liam Howlett , Ilkka Koskinen , Ben Gainey , K Prateek Nayak , Kan Liang , Yanteng Si , Ravi Bangoria , Sun Haiyong , Changbin Du , Masami Hiramatsu , zhaimingbing , Paran Lee , Li Dong , elfring@users.sourceforge.net, Andi Kleen , Markus Elfring , Chengen Du , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 13 more patches from: https://lore.kernel.org/lkml/20240202061532.1939474-1-irogers@google.com/ a near half year old adventure in trying to lower perf's dynamic memory use. Bits like the memory overhead of opendir are on the sidelines for now, too much fighting over how distributions/C-libraries present getdents. These changes are more good old fashioned replace an rb-tree with a sorted array and add reference count tracking. The changes migrate dsos code, the collection of dso structs, more into the dsos.c/dsos.h files. As with maps and threads, this is done so the internals can be changed - replacing a linked list (for fast iteration) and an rb-tree (for fast finds) with a lazily sorted array. The complexity of operations remain roughly the same, although iterating an array is likely faster than iterating a linked list, the memory usage is at least reduce by half. As fixing the memory usage necessitates changing operations like find, modify these operations so that they increment the reference count to avoid races like a find in dsos and a remove. Similarly tighten up lock usage so that operations working on dsos state hold the appropriate lock. Here are some questions (with answers) that I am expecting from reviewers: - Why not refactor dso with accessors first and then do the other things? My ambition with this change was to lower memory overhead not to particularly clean up and fix dso. Fixing the memory overhead, by refactoring and changing the internals, showed that locking discipline and reference counting discipline was lacking. The later changes try to fix these as a service to the community while I am changing the code and to also ensure that code is correct (more correct than it was wrt locking and reference counting than before the patches). Reordering the patches to do the refactoring first will be a giant pain. It will merge conflict with every other patch in the series and is basically a request to reimplement everything from square 1. The only thing I'd have in my favor would be how the code should look at the end of the series, and reordering patches doesn't change the eventual outcome of applying the patches. Note also, were I to send the memory saving patches and then a week later send the API clean up and reference counting fix patches the patches would be merged in the order they are here. I've done my best, I know you may consider that I'm adding to your reviewing overhead but I've also got to think about the overhead to me. - Please break apart this change... The first changes are moving things, but when a broken API is spotted like the missing get on dsos__find I put it in a change to move the function and to add the missed get. Could this be two changes? Yes, it could. Does moving code materially change the behavior of the tool? No. I've done it in one patch to minimize churn and to some extent for my sanity. Such changes are less than 100 lines of code and all independently tested. - The logic in dso around short, long name and id with sorting is weird Yes, I've tried to make it less weird while retaining the existing behavior. It would be easy to make a series of patches just cleaning it up but I came here to save memory not change the dso API. - Move the fixes in the 12th patch earlier. This is possible but then impossible to test with reference count checking. This does mean there are broken reference counts before the patch is applied, but this is generally already the case. Yes, some hypothetical person may decide to fork midway through this patch series and my order would mean they wouldn't have a fix. I've done my best while working within the bounds of my time and trying to avoid churn. v2. Rebases on top of tmp.perf-tools-next resolving merge conflicts. Ian Rogers (13): perf dso: Reorder variables to save space in struct dso perf dsos: Attempt to better abstract dsos internals perf dsos: Tidy reference counting and locking perf dsos: Add dsos__for_each_dso perf dso: Move dso functions out of dsos perf dsos: Switch more loops to dsos__for_each_dso perf dsos: Switch backing storage to array from rbtree/list perf dsos: Remove __dsos__addnew perf dsos: Remove __dsos__findnew_link_by_longname_id perf dsos: Switch hand code to bsearch perf dso: Add reference count checking and accessor functions perf dso: Reference counting related fixes perf dso: Use container_of to avoid a pointer in dso_data tools/perf/builtin-annotate.c | 8 +- tools/perf/builtin-buildid-cache.c | 2 +- tools/perf/builtin-buildid-list.c | 18 +- tools/perf/builtin-inject.c | 96 +-- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-mem.c | 4 +- tools/perf/builtin-record.c | 2 +- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-script.c | 8 +- tools/perf/builtin-top.c | 4 +- tools/perf/builtin-trace.c | 2 +- tools/perf/tests/code-reading.c | 8 +- tools/perf/tests/dso-data.c | 67 ++- tools/perf/tests/hists_common.c | 6 +- tools/perf/tests/hists_cumulate.c | 4 +- tools/perf/tests/hists_output.c | 2 +- tools/perf/tests/maps.c | 4 +- tools/perf/tests/symbols.c | 8 +- tools/perf/tests/vmlinux-kallsyms.c | 6 +- tools/perf/ui/browsers/annotate.c | 6 +- tools/perf/ui/browsers/hists.c | 8 +- tools/perf/ui/browsers/map.c | 4 +- tools/perf/util/annotate-data.c | 14 +- tools/perf/util/annotate.c | 47 +- tools/perf/util/auxtrace.c | 2 +- tools/perf/util/block-info.c | 2 +- tools/perf/util/bpf-event.c | 8 +- tools/perf/util/build-id.c | 136 ++--- tools/perf/util/build-id.h | 2 - tools/perf/util/callchain.c | 2 +- tools/perf/util/data-convert-json.c | 2 +- tools/perf/util/db-export.c | 6 +- tools/perf/util/dlfilter.c | 12 +- tools/perf/util/dso.c | 472 +++++++++------ tools/perf/util/dso.h | 554 +++++++++++++++--- tools/perf/util/dsos.c | 529 +++++++++++------ tools/perf/util/dsos.h | 40 +- tools/perf/util/event.c | 8 +- tools/perf/util/header.c | 8 +- tools/perf/util/hist.c | 4 +- tools/perf/util/intel-pt.c | 22 +- tools/perf/util/machine.c | 192 ++---- tools/perf/util/machine.h | 2 + tools/perf/util/map.c | 82 ++- tools/perf/util/maps.c | 14 +- tools/perf/util/probe-event.c | 25 +- .../util/scripting-engines/trace-event-perl.c | 6 +- .../scripting-engines/trace-event-python.c | 21 +- tools/perf/util/session.c | 21 + tools/perf/util/session.h | 2 + tools/perf/util/sort.c | 19 +- tools/perf/util/srcline.c | 65 +- tools/perf/util/symbol-elf.c | 145 +++-- tools/perf/util/symbol-minimal.c | 4 +- tools/perf/util/symbol.c | 186 +++--- tools/perf/util/symbol_fprintf.c | 4 +- tools/perf/util/synthetic-events.c | 24 +- tools/perf/util/thread.c | 4 +- tools/perf/util/unwind-libunwind-local.c | 18 +- tools/perf/util/unwind-libunwind.c | 2 +- tools/perf/util/vdso.c | 56 +- 61 files changed, 1817 insertions(+), 1220 deletions(-) -- 2.44.0.396.g6e790dbe36-goog