From patchwork Mon Dec 18 13:15:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Jinchao X-Patchwork-Id: 180411 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1236976dyi; Mon, 18 Dec 2023 05:26:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IGUXukEyjsDVguUl5Qr+O8kaKqWDGh0OfdylKI48V5nxwgLVdxmb1UkJOz63n553D6ul/G3 X-Received: by 2002:a17:903:228b:b0:1d0:77c9:a9a2 with SMTP id b11-20020a170903228b00b001d077c9a9a2mr10217067plh.25.1702905997024; Mon, 18 Dec 2023 05:26:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702905997; cv=none; d=google.com; s=arc-20160816; b=L6E0fH+uJdsc6SJyB1VUp5hZLKPzO9g5jC6Acik7kCyoRyM7t2W1eibmrPaikHW+co FGNBdOWpm3siU5Y2zeuLeGdeLPXFdHbH8vlPaYKOyIdBp/0JqmILjW+/p5M04el9YZJi qVLAiFc0lNuhH1ZEnF87sTE8ksZjjipXVxZoVYXGftZM/8uMLnhERjpHlsZDXT0Ccjls 5bmyEe+Kn01mPMXgA3MbkeuQyv8oqCV0VU6j150F8Fyjq1hubLcor3k5G1IvbhtbQChb oUtHYcmItE1i8sC3kNlXn0oZtW98J2pDWaL2RaA2KoBv/q0BfvjRJUoink+n+vIL5KuF QlpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date; bh=kh2xxQml2U+QR1vpYxXFLIhlImbbs02A0oscWjdHgw0=; fh=4+C44d5FvAT5uGJR7G9h25rM/y/KQA7MVCUrmnQdEd0=; b=vO9g8bmcYr2hYSYzsGPiuRtWaB+pTRzDApKrPzLKgoNeo3dhsv7uXJu9SryHgaOToM yVlcEu9EgSEZ56JbthF9Wpkx6OdENeO+VCfM5lPHgvAZao+8N+4DTcTE1Idt/Ckda3rO /PM6LgbttehDkjZNW8CNVBFBC5v8hP6bsG7X6Ai4ec6wtYEHNX8ZEGZ5ONKggSdNatw3 IPjakQiiw127d9Kuc0rXLw4uzMDwcEQsSVFaeJD7HoK/u1UgFeSs4bkHW6FOTZtLS4WG 3bmjVj5VbrDR+Ry90NZh+aDiPGcLgCMUh4YHJAdqWJYuUAJBYdS5MviFuc7ux2IkeSoK kplw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-3723-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3723-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k14-20020a170902ba8e00b001d080a72b2fsi17516694pls.289.2023.12.18.05.26.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 05:26:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3723-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-3723-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3723-ouuuleilei=gmail.com@vger.kernel.org" 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id EF1EFB231AB for ; Mon, 18 Dec 2023 13:21:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0F6833D557; Mon, 18 Dec 2023 13:15:09 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from wxsgout04.xfusion.com (wxsgout04.xfusion.com [36.139.87.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74BAB3A1B7; Mon, 18 Dec 2023 13:15:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xfusion.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xfusion.com Received: from wuxshcsitd00600.xfusion.com (unknown [10.32.133.213]) by wxsgout04.xfusion.com (SkyGuard) with ESMTPS id 4Sv0Zm32HmzB1CfX; Mon, 18 Dec 2023 21:11:28 +0800 (CST) Received: from localhost (10.82.147.3) by wuxshcsitd00600.xfusion.com (10.32.133.213) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Mon, 18 Dec 2023 21:15:01 +0800 Date: Mon, 18 Dec 2023 21:15:01 +0800 From: WangJinchao To: Herbert Xu , "David S. Miller" , Tim Chen , , CC: , Subject: [PATCH v2] crypto:tcrypt: add script tcrypt_speed_compare.py Message-ID: <202312182113+0800-wangjinchao@xfusion.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline X-ClientProxiedBy: wuxshcsitd00602.xfusion.com (10.32.132.250) To wuxshcsitd00600.xfusion.com (10.32.133.213) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785626358688122581 X-GMAIL-MSGID: 1785626358688122581 Create a script for comparing tcrypt speed test logs. The script will systematically analyze differences item by item and provide a summary (average). This tool is useful for evaluating the stability of cryptographic module algorithms and assisting with performance optimization. Please note that for such a comparison, stability depends on whether we allow frequency to float or pin the frequency. The script produces comparisons in two scenes: 1. For operations in seconds ================================================================================ rfc4106(gcm(aes)) (pcrypt(rfc4106(gcm_base(ctr(aes-generic),ghash-generic)))) encryption -------------------------------------------------------------------------------- bit key | byte blocks | base ops | new ops | differ(%) 160 | 16 | 66439 | 63063 | -5.08 160 | 64 | 62220 | 57439 | -7.68 ... 288 | 4096 | 15059 | 16278 | 8.09 288 | 8192 | 9043 | 9526 | 5.34 -------------------------------------------------------------------------------- average differ(%s) | total_differ(%) -------------------------------------------------------------------------------- 5.70 | -4.49 ================================================================================ 2. For avg cycles of operation ================================================================================ rfc4106(gcm(aes)) (pcrypt(rfc4106(gcm_base(ctr(aes-generic),ghash-generic)))) encryption -------------------------------------------------------------------------------- bit key | byte blocks | base cycles | new cycles | differ(%) 160 | 16 | 32500 | 35847 | 10.3 160 | 64 | 33175 | 45808 | 38.08 ... 288 | 4096 | 131369 | 132132 | 0.58 288 | 8192 | 229503 | 234581 | 2.21 -------------------------------------------------------------------------------- average differ(%s) | total_differ(%) -------------------------------------------------------------------------------- 8.41 | -6.70 ================================================================================ Signed-off-by: WangJinchao --- MAINTAINERS | 6 + tools/crypto/tcrypt/tcrypt_speed_compare.py | 190 ++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100755 tools/crypto/tcrypt/tcrypt_speed_compare.py diff --git a/MAINTAINERS b/MAINTAINERS index bba17f97eda7..b9c8dd607bce 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5524,6 +5524,12 @@ F: include/crypto/ F: include/linux/crypto* F: lib/crypto/ +CRYPTO SPEED TEST COMPARE +M: Wang Jinchao +L: linux-crypto@vger.kernel.org +S: Maintained +F: tools/crypto/tcrypt/tcrypt_speed_compare.py + CRYPTOGRAPHIC RANDOM NUMBER GENERATOR M: Neil Horman L: linux-crypto@vger.kernel.org diff --git a/tools/crypto/tcrypt/tcrypt_speed_compare.py b/tools/crypto/tcrypt/tcrypt_speed_compare.py new file mode 100755 index 000000000000..f3f5783cdc06 --- /dev/null +++ b/tools/crypto/tcrypt/tcrypt_speed_compare.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) xFusion Digital Technologies Co., Ltd., 2023 +# +# Author: Wang Jinchao +# +""" +A tool for comparing tcrypt speed test logs. + +Please note that for such a comparison, stability depends +on whether we allow frequency to float or pin the frequency. + +Both support tests for operations within one second and +cycles of operation. +For example, use it in the bash script below. + +```bash +#!/bin/bash + +# log file prefix +seq_num=0 + +# When sec=0, it will perform cycle tests; +# otherwise, it indicates the duration of a single test +sec=0 +num_mb=8 +mode=211 + +# base speed test +lsmod | grep pcrypt && modprobe -r pcrypt +dmesg -C +modprobe tcrypt alg="pcrypt(rfc4106(gcm(aes)))" type=3 +modprobe tcrypt mode=${mode} sec=${sec} num_mb=${num_mb} +dmesg > ${seq_num}_base_dmesg.log + +# new speed test +lsmod | grep pcrypt && modprobe -r pcrypt +dmesg -C +modprobe tcrypt alg="pcrypt(rfc4106(gcm(aes)))" type=3 +modprobe tcrypt mode=${mode} sec=${sec} num_mb=${num_mb} +dmesg > ${seq_num}_new_dmesg.log +lsmod | grep pcrypt && modprobe -r pcrypt + +tools/crypto/tcrypt/tcrypt_speed_compare.py \ + ${seq_num}_base_dmesg.log \ + ${seq_num}_new_dmesg.log \ + >${seq_num}_compare.log +grep 'average' -A2 -B0 --group-separator="" ${seq_num}_compare.log +``` +""" + +import sys +import re + + +def parse_title(line): + pattern = r'tcrypt: testing speed of (.*?) (encryption|decryption)' + match = re.search(pattern, line) + if match: + alg = match.group(1) + op = match.group(2) + return alg, op + else: + return "", "" + + +def parse_item(line): + pattern_operations = r'\((\d+) bit key, (\d+) byte blocks\): (\d+) operations' + pattern_cycles = r'\((\d+) bit key, (\d+) byte blocks\): 1 operation in (\d+) cycles' + match = re.search(pattern_operations, line) + if match: + res = { + "bit_key": int(match.group(1)), + "byte_blocks": int(match.group(2)), + "operations": int(match.group(3)), + } + return res + + match = re.search(pattern_cycles, line) + if match: + res = { + "bit_key": int(match.group(1)), + "byte_blocks": int(match.group(2)), + "cycles": int(match.group(3)), + } + return res + + return None + + +def parse(filepath): + result = {} + alg, op = "", "" + with open(filepath, 'r') as file: + for line in file: + if not line: + continue + _alg, _op = parse_title(line) + if _alg: + alg, op = _alg, _op + if alg not in result: + result[alg] = {} + if op not in result[alg]: + result[alg][op] = [] + continue + parsed_result = parse_item(line) + if parsed_result: + result[alg][op].append(parsed_result) + return result + + +def merge(base, new): + merged = {} + for alg in base.keys(): + merged[alg] = {} + for op in base[alg].keys(): + if op not in merged[alg]: + merged[alg][op] = [] + for index in range(len(base[alg][op])): + merged_item = { + "bit_key": base[alg][op][index]["bit_key"], + "byte_blocks": base[alg][op][index]["byte_blocks"], + } + if "operations" in base[alg][op][index].keys(): + merged_item["base_ops"] = base[alg][op][index]["operations"] + merged_item["new_ops"] = new[alg][op][index]["operations"] + else: + merged_item["base_cycles"] = base[alg][op][index]["cycles"] + merged_item["new_cycles"] = new[alg][op][index]["cycles"] + + merged[alg][op].append(merged_item) + return merged + + +def format(merged): + for alg in merged.keys(): + for op in merged[alg].keys(): + base_sum = 0 + new_sum = 0 + differ_sum = 0 + differ_cnt = 0 + print() + hlen = 80 + print("="*hlen) + print(f"{alg}") + print(f"{' '*(len(alg)//3) + op}") + print("-"*hlen) + key = "" + if "base_ops" in merged[alg][op][0]: + key = "ops" + print(f"bit key | byte blocks | base ops | new ops | differ(%)") + else: + key = "cycles" + print(f"bit key | byte blocks | base cycles | new cycles | differ(%)") + for index in range(len(merged[alg][op])): + item = merged[alg][op][index] + base_cnt = item[f"base_{key}"] + new_cnt = item[f"new_{key}"] + base_sum += base_cnt + new_sum += new_cnt + differ = round((new_cnt - base_cnt)*100/base_cnt, 2) + differ_sum += differ + differ_cnt += 1 + bit_key = item["bit_key"] + byte_blocks = item["byte_blocks"] + print( + f"{bit_key:<7} | {byte_blocks:<11} | {base_cnt:<11} | {new_cnt:<11} | {differ:<8}") + average_speed_up = "{:.2f}".format(differ_sum/differ_cnt) + ops_total_speed_up = "{:.2f}".format( + (base_sum - new_sum) * 100 / base_sum) + print('-'*hlen) + print(f"average differ(%s) | total_differ(%)") + print('-'*hlen) + print(f"{average_speed_up:<21} | {ops_total_speed_up:<10}") + print('='*hlen) + + +def main(base_log, new_log): + base = parse(base_log) + new = parse(new_log) + merged = merge(base, new) + format(merged) + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print(f"usage: {sys.argv[0]} base_log new_log") + exit(-1) + main(sys.argv[1], sys.argv[2])