Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4451221rwb; Tue, 8 Aug 2023 08:41:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3gZMh2Z+AuMTa7D178906QTgbmKol5nskBnWCGFHyOr0GXCu5DWH4OPhMO7xJ2O88X4zv X-Received: by 2002:a05:6a20:96ce:b0:12f:1f1f:b506 with SMTP id hq14-20020a056a2096ce00b0012f1f1fb506mr10736312pzc.21.1691509310973; Tue, 08 Aug 2023 08:41:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691509310; cv=none; d=google.com; s=arc-20160816; b=0k1SFjO07eswKzWojehGxLSUtJJd/213Rp2EL17H+QO9wUmG1n3xiAzvmYyagStCY9 0icRlCKiabI1houNDkzBg8jgYhqVweQtTV7ZIOE07K59fxSiYq0/AetIO+qkuEexb8DV nc54kxYGwvbTI1Z5nJ2OaHLUhNVpdQgIYvrsuYt0af2S71ZgltmedvT5iXOtIfXk2804 GUuusKvdDHonzgNywLMOAmaci+pjJvcmepZCLpplX0cjHBHjub6KVR/iOY5YWPbwupP+ 2aaMTcOxjFnG3ae/L70a7GDh79ETz4QXx2sC6I6ed6bXXV5BsocXse8XCGhMrShMqUA2 YIKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=kKVyc+zFlVWpO5/CXfGPIS3QFb7qUfQws+wZbKQtn3g=; fh=1AFv2pJqDtHNKGfZiyosbvxwghqFptoVvdc5/f2/vxA=; b=gIHydxdI/jBH2F25qNUC7fOWKm/fnAg0D184rdAjpkSssQzeHC+pxQlGukkCZ1l0bi eSCm83pDuSwGij+y5cZVP8G7/mVM5uxFfdQj/el2RNN/hFcFQEmdMrPOU3OJugPAtgD7 rvuXP/8vjXSUWHclhdwE3euFe8k9ye8qUk8G6Ff+bzd4nCmN5NSLE7ERyFqUhGqOUZNW 7Pu/VT6h8S0f8fw+OkXMqPeS0V0pWo6e95SyzcgCucg3vTsi6G1YJz2/plGYM9vBvxam LJmOpTMojk8x1eFgZ9Hu0i3Es8Khw7Hn1vYToXSs4QsT7etfaE6yldn3fqn5Nb4jJEyq FPgg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p68-20020a634247000000b0055793097dbdsi7107338pga.453.2023.08.08.08.41.39; Tue, 08 Aug 2023 08:41:50 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229761AbjHHCyz (ORCPT + 99 others); Mon, 7 Aug 2023 22:54:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbjHHCyx (ORCPT ); Mon, 7 Aug 2023 22:54:53 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE5EEE62 for ; Mon, 7 Aug 2023 19:54:51 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4RKd861nsqz4f3tDW for ; Tue, 8 Aug 2023 10:54:46 +0800 (CST) Received: from [10.174.178.55] (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgBH16l3rtFk1crIAA--.12029S3; Tue, 08 Aug 2023 10:54:49 +0800 (CST) Subject: Re: [PATCH 1/2] hexdump: minimize the output width of the offset To: Randy Dunlap , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , John Ogness , linux-kernel@vger.kernel.org Cc: Zhen Lei References: <20230805072116.1260-1-thunder.leizhen@huaweicloud.com> <20230805072116.1260-2-thunder.leizhen@huaweicloud.com> <41713c95-51a8-8e2a-9c70-dab10dacf26d@infradead.org> <8b7a579e-473d-ea90-58e0-e2f69f9bd002@huaweicloud.com> From: "Leizhen (ThunderTown)" Message-ID: <7476190c-cf50-1b46-ef92-61ce97330956@huaweicloud.com> Date: Tue, 8 Aug 2023 10:54:47 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CM-TRANSID: gCh0CgBH16l3rtFk1crIAA--.12029S3 X-Coremail-Antispam: 1UD129KBjvJXoWxCr47tw43Ww4kAF45JFyftFb_yoW5tFWUpr 98JayUKFWkJryxKr12qr10grn0yry7C3W8WrWqka4jvFyqqF93Jry8GFW5KFy5Gr1YvF4a qr12y3WS9F17G3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk0b4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4CEbIxvr21l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv 6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUOyCJDUUUU X-CM-SenderInfo: hwkx0vthuozvpl2kv046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE 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 2023/8/8 10:42, Randy Dunlap wrote: > > > On 8/7/23 18:10, Leizhen (ThunderTown) wrote: >> >> >> On 2023/8/8 6:37, Randy Dunlap wrote: >>> Hi-- >>> >>> On 8/5/23 00:21, thunder.leizhen@huaweicloud.com wrote: >>>> From: Zhen Lei >>>> >>>> The offset of case DUMP_PREFIX_OFFSET always starts from 0. Currently, >>>> the output width is fixed to 8. But we usually dump only tens or hundreds >>>> of bytes, occasionally thousands of bytes. Therefore, the output offset >>>> value always has a number of leading zeros, which increases the number of >>>> bytes printed and reduces readability. Let's minimize the output width of >>>> the offset based on the number of significant bits of its maximum value. >>>> >>>> Before: >>>> dump_size=36: >>>> 00000000: c0 ba 8c 80 00 80 ff ff 6c 93 ee 2f ee bf ff ff >>>> 00000010: 00 50 1e 98 ff 27 ff ff 01 00 00 00 00 00 00 00 >>>> 00000020: 80 ca 2f 98 >>>> >>>> After: >>>> dump_size=8: >>>> 0: c0 ba 89 80 00 80 ff ff >>>> >>>> dump_size=36: >>>> 00: c0 3a 91 80 00 80 ff ff 6c 93 ae 76 30 ce ff ff >>>> 10: 00 60 cd 60 7d 4e ff ff 01 00 00 00 00 00 00 00 >>>> 20: 40 9e 29 40 >>>> >>>> dump_size=300: >>>> 000: c0 ba 8d 80 00 80 ff ff 6c 93 ce d4 78 a7 ff ff >>>> 010: 00 00 16 18 0c 40 ff ff 01 00 00 00 00 00 00 00 >>>> 020: 01 00 00 00 00 00 00 00 e8 bc 8d 80 00 80 ff ff >>>> ... ... >>>> 110: 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >>>> 120: 00 08 12 01 0c 40 ff ff 00 00 01 00 >>>> >>>> Signed-off-by: Zhen Lei >>>> --- >>>> lib/hexdump.c | 13 +++++++++++-- >>>> 1 file changed, 11 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/lib/hexdump.c b/lib/hexdump.c >>>> index 06833d404398d74..86cb4cc3eec485a 100644 >>>> --- a/lib/hexdump.c >>>> +++ b/lib/hexdump.c >>>> @@ -263,12 +263,21 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, >>>> const void *buf, size_t len, bool ascii) >>>> { >>>> const u8 *ptr = buf; >>>> - int i, linelen, remaining = len; >>>> + int i, linelen, width = 0, remaining = len; >>>> unsigned char linebuf[32 * 3 + 2 + 32 + 1]; >>>> >>>> if (rowsize != 16 && rowsize != 32) >>>> rowsize = 16; >>>> >>>> + if (prefix_type == DUMP_PREFIX_OFFSET) { >>>> + unsigned long tmp = len - 1; /* offset start from 0, so minus 1 */ >>>> + >>>> + do { >>>> + width++; >>>> + tmp >>= 4; >>>> + } while (tmp); >>>> + } >>>> + >>> >>> You could put all of that ^^^ in the case DUMP_PREFIX_OFFSET below. >> >> for (i = 0; i < len; i += rowsize) { >> >> "case DUMP_PREFIX_OFFSET" is in the loop, and moving the code above >> to the case DUMP_PREFIX_OFFSET will be calculate multiple times. But >> following your prompt, I thought again, I can control it with the >> local variable width. I will post v2 right away. > > Ah I see. My apologies. It should be okay to move it into the case DUMP_PREFIX_OFFSET. The compiler can optimize it. > >>> Otherwise LGTM. >>> >>> Reviewed-by: Randy Dunlap >>> Thanks. >>> >>>> for (i = 0; i < len; i += rowsize) { >>>> linelen = min(remaining, rowsize); >>>> remaining -= rowsize; >>>> @@ -282,7 +291,7 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, >>>> level, prefix_str, ptr + i, linebuf); >>>> break; >>>> case DUMP_PREFIX_OFFSET: >>>> - printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf); >>>> + printk("%s%s%0*x: %s\n", level, prefix_str, width, i, linebuf); >>>> break; >>>> default: >>>> printk("%s%s%s\n", level, prefix_str, linebuf); >>> >> > -- Regards, Zhen Lei