From patchwork Thu Mar 25 06:53:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 12163133 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA7F1C433C1 for ; Thu, 25 Mar 2021 06:54:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A2A2F61A1D for ; Thu, 25 Mar 2021 06:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229581AbhCYGxx (ORCPT ); Thu, 25 Mar 2021 02:53:53 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:62084 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbhCYGx0 (ORCPT ); Thu, 25 Mar 2021 02:53:26 -0400 Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 12P6rBB1013419 for ; Wed, 24 Mar 2021 23:53:25 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=W+dKSrDwHm8/4+TVD31NBbugo1POQzoZeeJLlveft9Q=; b=UBVRo+ptqjRv1SCjnvcqgnNlZOCVILFIc5Pdfad5EtgPHAdAXGAjG6jStXvjqz++HBiE erL5HQWN3zh2ukRq7mugitkl9BRzpfgkkBLKcfbv0siRxHla4IU+fOA8X/G0JaMwaeeG gpHodDjEE9kRu7MqoXrXtiU77bvdOq6Y0YQ= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 37fpght1k1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 24 Mar 2021 23:53:25 -0700 Received: from intmgw002.25.frc3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 24 Mar 2021 23:53:23 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 19A8AAE26BA; Wed, 24 Mar 2021 23:53:22 -0700 (PDT) From: Yonghong Song To: Arnaldo Carvalho de Melo , CC: Alexei Starovoitov , Andrii Nakryiko , Bill Wendling , , Subject: [PATCH dwarves 1/3] dwarf_loader: permits flexible HASHTAGS__BITS Date: Wed, 24 Mar 2021 23:53:22 -0700 Message-ID: <20210325065322.3121605-1-yhs@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210325065316.3121287-1-yhs@fb.com> References: <20210325065316.3121287-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-03-25_01:2021-03-24,2021-03-25 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 mlxlogscore=971 priorityscore=1501 suspectscore=0 malwarescore=0 spamscore=0 phishscore=0 lowpriorityscore=0 mlxscore=0 impostorscore=0 adultscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2103250050 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Currently, types/tags hash table has fixed HASHTAGS__BITS = 15. That means the number of buckets will be 1UL << 15 = 32768. In my experiments, a thin-LTO built vmlinux has roughly 9M entries in types table and 5.2M entries in tags table. So the number of buckets is too less for an efficient lookup. This patch refactored the code to allow the number of buckets to be changed. In addition, currently hashtags__fn(key) return value is assigned to uint16_t. Change to uint32_t as in a later patch the number of hashtag bits can be increased to be more than 16. Signed-off-by: Yonghong Song Signed-off-by: Bill Wendling Signed-off-by: Arnaldo Carvalho de Melo --- dwarf_loader.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/dwarf_loader.c b/dwarf_loader.c index c106919..a02ef23 100644 --- a/dwarf_loader.c +++ b/dwarf_loader.c @@ -50,7 +50,12 @@ struct strings *strings; #define DW_FORM_implicit_const 0x21 #endif -#define hashtags__fn(key) hash_64(key, HASHTAGS__BITS) +static uint32_t hashtags__bits = 15; + +uint32_t hashtags__fn(Dwarf_Off key) +{ + return hash_64(key, hashtags__bits); +} bool no_bitfield_type_recode = true; @@ -102,9 +107,6 @@ static void dwarf_tag__set_spec(struct dwarf_tag *dtag, dwarf_off_ref spec) *(dwarf_off_ref *)(dtag + 1) = spec; } -#define HASHTAGS__BITS 15 -#define HASHTAGS__SIZE (1UL << HASHTAGS__BITS) - #define obstack_chunk_alloc malloc #define obstack_chunk_free free @@ -118,22 +120,41 @@ static void *obstack_zalloc(struct obstack *obstack, size_t size) } struct dwarf_cu { - struct hlist_head hash_tags[HASHTAGS__SIZE]; - struct hlist_head hash_types[HASHTAGS__SIZE]; + struct hlist_head *hash_tags; + struct hlist_head *hash_types; struct obstack obstack; struct cu *cu; struct dwarf_cu *type_unit; }; -static void dwarf_cu__init(struct dwarf_cu *dcu) +static int dwarf_cu__init(struct dwarf_cu *dcu) { + uint64_t hashtags_size = 1UL << hashtags__bits; + dcu->hash_tags = malloc(sizeof(struct hlist_head) * hashtags_size); + if (!dcu->hash_tags) + return -ENOMEM; + + dcu->hash_types = malloc(sizeof(struct hlist_head) * hashtags_size); + if (!dcu->hash_types) { + free(dcu->hash_tags); + return -ENOMEM; + } + unsigned int i; - for (i = 0; i < HASHTAGS__SIZE; ++i) { + for (i = 0; i < hashtags_size; ++i) { INIT_HLIST_HEAD(&dcu->hash_tags[i]); INIT_HLIST_HEAD(&dcu->hash_types[i]); } obstack_init(&dcu->obstack); dcu->type_unit = NULL; + return 0; +} + +static void dwarf_cu__delete(struct cu *cu) +{ + struct dwarf_cu *dcu = cu->priv; + free(dcu->hash_tags); + free(dcu->hash_types); } static void hashtags__hash(struct hlist_head *hashtable, @@ -151,7 +172,7 @@ static struct dwarf_tag *hashtags__find(const struct hlist_head *hashtable, struct dwarf_tag *tpos; struct hlist_node *pos; - uint16_t bucket = hashtags__fn(id); + uint32_t bucket = hashtags__fn(id); const struct hlist_head *head = hashtable + bucket; hlist_for_each_entry(tpos, pos, head, hash_node) { @@ -2429,7 +2450,9 @@ static int cus__load_debug_types(struct cus *cus, struct conf_load *conf, } cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB; - dwarf_cu__init(dcup); + if (dwarf_cu__init(dcup) != 0) + return DWARF_CB_ABORT; + dcup->cu = cu; /* Funny hack. */ dcup->type_unit = dcup; @@ -2521,7 +2544,9 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, struct dwarf_cu dcu; - dwarf_cu__init(&dcu); + if (dwarf_cu__init(&dcu) != 0) + return DWARF_CB_ABORT; + dcu.cu = cu; dcu.type_unit = type_cu ? &type_dcu : NULL; cu->priv = &dcu; @@ -2672,5 +2697,6 @@ struct debug_fmt_ops dwarf__ops = { .tag__decl_file = dwarf_tag__decl_file, .tag__decl_line = dwarf_tag__decl_line, .tag__orig_id = dwarf_tag__orig_id, + .cu__delete = dwarf_cu__delete, .has_alignment_info = true, }; From patchwork Thu Mar 25 06:53:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 12163137 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33DDDC433E4 for ; Thu, 25 Mar 2021 06:54:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 03EE761A21 for ; Thu, 25 Mar 2021 06:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229547AbhCYGxy (ORCPT ); Thu, 25 Mar 2021 02:53:54 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:60754 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229662AbhCYGxd (ORCPT ); Thu, 25 Mar 2021 02:53:33 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 12P6i8MG012094 for ; Wed, 24 Mar 2021 23:53:33 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=MBeP5zF/AXazYMbbkVm421GWjxInhr5Fh1zvcgIM5Ng=; b=p1QceDf4Frunouqan3UHdfYbeHPe1KXofo1HFH+gwecHap6ekjQJJLjwY8rDzdJuSotO SVeqfSfNJKoGT10CcCWjdaqatN2EU4Y8OIkX6UPtO+yK3fT/kJ8crTjWds8nMYaWISzA aBD5v6LnfejPzTlmM9rtfq4eE2M5esvq38o= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 37fn33tm64-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 24 Mar 2021 23:53:32 -0700 Received: from intmgw001.25.frc3.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 24 Mar 2021 23:53:31 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 5296AAE26C7; Wed, 24 Mar 2021 23:53:27 -0700 (PDT) From: Yonghong Song To: Arnaldo Carvalho de Melo , CC: Alexei Starovoitov , Andrii Nakryiko , Bill Wendling , , Subject: [PATCH dwarves 2/3] dwarf_loader: factor out common code to initialize a cu Date: Wed, 24 Mar 2021 23:53:27 -0700 Message-ID: <20210325065327.3122071-1-yhs@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210325065316.3121287-1-yhs@fb.com> References: <20210325065316.3121287-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-03-25_01:2021-03-24,2021-03-25 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 mlxscore=0 priorityscore=1501 mlxlogscore=999 phishscore=0 spamscore=0 suspectscore=0 adultscore=0 impostorscore=0 malwarescore=0 lowpriorityscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2103250049 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Both cus__load_debug_types() and cus__load_module() created new cu's followed by initialization. The initialization codes are identical so let us refactor into a common function which can be used later as well when dealing with merging cu's. Signed-off-by: Yonghong Song --- dwarf_loader.c | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/dwarf_loader.c b/dwarf_loader.c index a02ef23..dc66df0 100644 --- a/dwarf_loader.c +++ b/dwarf_loader.c @@ -2411,6 +2411,23 @@ static int finalize_cu_immediately(struct cus *cus, struct cu *cu, return lsk; } +static int cu__set_common(struct cu *cu, struct conf_load *conf, + Dwfl_Module *mod, Elf *elf) +{ + cu->uses_global_strings = true; + cu->elf = elf; + cu->dwfl = mod; + cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0; + cu->has_addr_info = conf ? conf->get_addr_info : 0; + + GElf_Ehdr ehdr; + if (gelf_getehdr(elf, &ehdr) == NULL) + return DWARF_CB_ABORT; + + cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB; + return 0; +} + static int cus__load_debug_types(struct cus *cus, struct conf_load *conf, Dwfl_Module *mod, Dwarf *dw, Elf *elf, const char *filename, @@ -2434,22 +2451,11 @@ static int cus__load_debug_types(struct cus *cus, struct conf_load *conf, cu = cu__new("", pointer_size, build_id, build_id_len, filename); - if (cu == NULL) { + if (cu == NULL || + cu__set_common(cu, conf, mod, elf) != 0) { return DWARF_CB_ABORT; } - cu->uses_global_strings = true; - cu->elf = elf; - cu->dwfl = mod; - cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0; - cu->has_addr_info = conf ? conf->get_addr_info : 0; - - GElf_Ehdr ehdr; - if (gelf_getehdr(elf, &ehdr) == NULL) { - return DWARF_CB_ABORT; - } - cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB; - if (dwarf_cu__init(dcup) != 0) return DWARF_CB_ABORT; @@ -2528,19 +2534,8 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, const char *name = attr_string(cu_die, DW_AT_name); struct cu *cu = cu__new(name ?: "", pointer_size, build_id, build_id_len, filename); - if (cu == NULL) - return DWARF_CB_ABORT; - cu->uses_global_strings = true; - cu->elf = elf; - cu->dwfl = mod; - cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0; - cu->has_addr_info = conf ? conf->get_addr_info : 0; - - GElf_Ehdr ehdr; - if (gelf_getehdr(elf, &ehdr) == NULL) { + if (cu == NULL || cu__set_common(cu, conf, mod, elf) != 0) return DWARF_CB_ABORT; - } - cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB; struct dwarf_cu dcu; From patchwork Thu Mar 25 06:53:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 12163135 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2A20C433DB for ; Thu, 25 Mar 2021 06:54:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C4EFC61A24 for ; Thu, 25 Mar 2021 06:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229626AbhCYGxy (ORCPT ); Thu, 25 Mar 2021 02:53:54 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:22322 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229664AbhCYGxf (ORCPT ); Thu, 25 Mar 2021 02:53:35 -0400 Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 12P6pWho029566 for ; Wed, 24 Mar 2021 23:53:35 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=uGv19X6Jg7UiY365DY9yx28kp6nGdDll8V+NVR/yBFY=; b=mUjwPA4PS7NGOiYe7w6T42/3brGPjjf7WhbdeFo5goHjYwBG6Fxx4CZhhM60ac2rqFr8 YyDwUB74wiW2YATIh6qU9ova7mrG6ZbYj9+EDVUQ/wHrn7V26+LObnxdJZDeQ5pVzodN nxy0ouwL0m8bXYugWgkqcpSxbKIeEL24yBQ= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 37fpbm9xtf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 24 Mar 2021 23:53:35 -0700 Received: from intmgw001.05.ash9.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 24 Mar 2021 23:53:34 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 8AA0FAE26D9; Wed, 24 Mar 2021 23:53:32 -0700 (PDT) From: Yonghong Song To: Arnaldo Carvalho de Melo , CC: Alexei Starovoitov , Andrii Nakryiko , Bill Wendling , , Subject: [PATCH dwarves 3/3] dwarf_loader: add option to merge more dwarf cu's into one pahole cu Date: Wed, 24 Mar 2021 23:53:32 -0700 Message-ID: <20210325065332.3122473-1-yhs@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210325065316.3121287-1-yhs@fb.com> References: <20210325065316.3121287-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-03-25_01:2021-03-24,2021-03-25 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 lowpriorityscore=0 adultscore=0 mlxlogscore=999 priorityscore=1501 impostorscore=0 malwarescore=0 clxscore=1015 phishscore=0 suspectscore=0 mlxscore=0 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2103250050 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This patch added an option "merge_cus", which will permit to merge all debug info cu's into one pahole cu. For vmlinux built with clang thin-lto or lto, there exist cross cu type references. For example, you could have compile unit 1: tag 10: type A compile unit 2: ... refer to type A (tag 10 in compile unit 1) I only checked a few but have seen type A may be a simple type like "unsigned char" or a complex type like an array of base types. There are two different ways to resolve this issue: (1). merge all compile units as one pahole cu so tags/types can be resolved easily, or (2). try to do on-demand type traversal in other debuginfo cu's when we do die_process(). The method (2) is much more complicated so I picked method (1). An option "merge_cus" is added to permit such an operation. Merging cu's will create a single cu with lots of types, tags and functions. For example with clang thin-lto built vmlinux, I saw 9M entries in types table, 5.2M in tags table. The below are pahole wallclock time for different hashbits: command line: time pahole -J --merge_cus vmlinux # of hashbits wallclock time in seconds 15 460 16 255 17 131 18 97 19 75 20 69 21 64 22 62 23 58 24 64 Note that the number of hashbits 24 makes performance worse than 23. The reason could be that 23 hashbits can cover 8M buckets (close to 9M for the number of entries in types table). Higher number of hash bits allocates more memory and becomes less cache efficient compared to 23 hashbits. This patch picks # of hashbits 21 as the starting value and will try to allocate memory based on that, if memory allocation fails, we will go with less hashbits until we reach hashbits 15 which is the default for non merge-cu case. Signed-off-by: Yonghong Song --- dwarf_loader.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ dwarves.h | 2 ++ pahole.c | 8 +++++ 3 files changed, 100 insertions(+) diff --git a/dwarf_loader.c b/dwarf_loader.c index dc66df0..ed4f0da 100644 --- a/dwarf_loader.c +++ b/dwarf_loader.c @@ -51,6 +51,7 @@ struct strings *strings; #endif static uint32_t hashtags__bits = 15; +static uint32_t max_hashtags__bits = 21; uint32_t hashtags__fn(Dwarf_Off key) { @@ -2484,6 +2485,85 @@ static int cus__load_debug_types(struct cus *cus, struct conf_load *conf, return 0; } +static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf, + Dwfl_Module *mod, Dwarf *dw, Elf *elf, + const char *filename, + const unsigned char *build_id, + int build_id_len, + struct dwarf_cu *type_dcu) +{ + uint8_t pointer_size, offset_size; + struct dwarf_cu *dcu = NULL; + Dwarf_Off off = 0, noff; + struct cu *cu = NULL; + size_t cuhl; + + /* Merge all cus */ + while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, &pointer_size, + &offset_size) == 0) { + Dwarf_Die die_mem; + Dwarf_Die *cu_die = dwarf_offdie(dw, off + cuhl, &die_mem); + + if (cu_die == NULL) + break; + + if (cu == NULL) { + cu = cu__new("", pointer_size, build_id, build_id_len, + filename); + if (cu == NULL || cu__set_common(cu, conf, mod, elf) != 0) + return DWARF_CB_ABORT; + + dcu = malloc(sizeof(struct dwarf_cu)); + if (dcu == NULL) + return DWARF_CB_ABORT; + + /* Merged cu tends to need a lot more memory. + * Let us start with max_hashtags__bits and + * go down to find a proper hashtag bit value. + */ + uint32_t default_hbits = hashtags__bits; + for (hashtags__bits = max_hashtags__bits; + hashtags__bits >= default_hbits; + hashtags__bits--) { + if (dwarf_cu__init(dcu) == 0) + break; + } + if (hashtags__bits < default_hbits) + return DWARF_CB_ABORT; + + dcu->cu = cu; + dcu->type_unit = type_dcu; + cu->priv = dcu; + cu->dfops = &dwarf__ops; + cu->language = attr_numeric(cu_die, DW_AT_language); + } + + const uint16_t tag = dwarf_tag(cu_die); + if (tag != DW_TAG_compile_unit && tag != DW_TAG_type_unit) { + fprintf(stderr, "%s: DW_TAG_compile_unit or DW_TAG_type_unit expected got %s!\n", + __FUNCTION__, dwarf_tag_name(tag)); + return DWARF_CB_ABORT; + } + + Dwarf_Die child; + if (dwarf_child(cu_die, &child) == 0) { + if (die__process_unit(&child, cu) != 0) + return DWARF_CB_ABORT; + } + + off = noff; + } + + /* process merged cu */ + if (cu__recode_dwarf_types(cu) != LSK__KEEPIT) + return DWARF_CB_ABORT; + if (finalize_cu_immediately(cus, cu, dcu, conf) + == LSK__STOP_LOADING) + return DWARF_CB_ABORT; + + return 0; +} + static int cus__load_module(struct cus *cus, struct conf_load *conf, Dwfl_Module *mod, Dwarf *dw, Elf *elf, const char *filename) @@ -2518,6 +2598,15 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, } } + if (conf->merge_cus == true) { + res = cus__merge_and_process_cu(cus, conf, mod, dw, elf, filename, + build_id, build_id_len, + type_cu ? &type_dcu : NULL); + if (res != 0) + return res; + goto out; + } + while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, &pointer_size, &offset_size) == 0) { Dwarf_Die die_mem; @@ -2557,6 +2646,7 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, off = noff; } +out: if (type_lsk == LSK__DELETE) cu__delete(type_cu); diff --git a/dwarves.h b/dwarves.h index 98caf1a..29b518d 100644 --- a/dwarves.h +++ b/dwarves.h @@ -40,6 +40,7 @@ struct conf_fprintf; * @extra_dbg_info - keep original debugging format extra info * (e.g. DWARF's decl_{line,file}, id, etc) * @fixup_silly_bitfields - Fixup silly things such as "int foo:32;" + * @merge_cus - Merge compile units except possible types_cu * @get_addr_info - wheter to load DW_AT_location and other addr info */ struct conf_load { @@ -50,6 +51,7 @@ struct conf_load { bool extra_dbg_info; bool fixup_silly_bitfields; bool get_addr_info; + bool merge_cus; struct conf_fprintf *conf_fprintf; }; diff --git a/pahole.c b/pahole.c index df6aa83..29fbe1d 100644 --- a/pahole.c +++ b/pahole.c @@ -827,6 +827,7 @@ ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version; #define ARGP_btf_base 321 #define ARGP_btf_gen_floats 322 #define ARGP_btf_gen_all 323 +#define ARGP_merge_cus 324 static const struct argp_option pahole__options[] = { { @@ -1151,6 +1152,11 @@ static const struct argp_option pahole__options[] = { .key = ARGP_numeric_version, .doc = "Print a numeric version, i.e. 119 instead of v1.19" }, + { + .name = "merge_cus", + .key = ARGP_merge_cus, + .doc = "Merge all cus (except possible types_cu)" + }, { .name = NULL, } @@ -1270,6 +1276,8 @@ static error_t pahole__options_parser(int key, char *arg, btf_gen_floats = true; break; case ARGP_btf_gen_all: btf_gen_floats = true; break; + case ARGP_merge_cus: + conf_load.merge_cus = true; break; default: return ARGP_ERR_UNKNOWN; }