From patchwork Tue Nov 26 07:33:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wangyang Guo X-Patchwork-Id: 101870 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7CCC83858C3A for ; Tue, 26 Nov 2024 07:45:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CCC83858C3A Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=gPt5ttwa X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) by sourceware.org (Postfix) with ESMTPS id DAAA63858416 for ; Tue, 26 Nov 2024 07:37:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DAAA63858416 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DAAA63858416 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.13 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732606636; cv=none; b=pUbLsDJf8slp96GfCoNO9LOWZJE/nvn0il2qb/7udbcRyDhzseD0cIKlGexBVKtOiqtwTp6tiaay1DZTXNiZD7zxLt3pTW3d1+7E4ZEvYZf9nGGsPifUWOK73Y2D6ew5xfrxLfssuxTYoLijro9oGaUdjAfJAgs/nRPh923va3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732606636; c=relaxed/simple; bh=luNoxlLPLK2VW5ekTIhERpMRb9U3VOg+pa4DN8tSSIU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PAhJQBxwWG2zPZpUuJ8Lam+Cpv+3UhW7fXSs4ypzcvb6aU6QVkTvOoc2H3TiizEtq/n0Llk5oEsO4D1UsTuZSwJKKxdvysNmPdoVlQ1/aSoDkJtWbGW27Ce2ooMGLVFbmmGP2ws2VHoUnxlaACu7xfyurQiMt1Yet7NEyK80fxU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DAAA63858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1732606636; x=1764142636; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=luNoxlLPLK2VW5ekTIhERpMRb9U3VOg+pa4DN8tSSIU=; b=gPt5ttwag8YOX3K9ACGSZjHeg6BgxcMW65ROe5/vGJvcSuye4FwKhjJ1 STL0TZliqgMDtCBeXz6P1MKSHgM8uju5RHtXBy2S1TYTPkXpRGtnQrkxY qa2OtI8veCTlr5AxkZY+evGnuK8QLaICs0SslvrHkFCLJ6SJ4Xf6jddQn waIiicRTAwTkF8dRRCaktVnHtJ2sGAxNJTub3ThXxLd09o5p4cPnVP0tO qQl+4Jr3ucpoIvow9D4pKuwnCgbRusOe7B8ZY+kHcjz+0Q5i1YGLfwLAL NiaTfc9F16xRgCZVH1reiw22doGDIgBddKZius+Rp7BNtGIlATZntPU++ Q==; X-CSE-ConnectionGUID: Z6SksporT9ihNqcXCliZpg== X-CSE-MsgGUID: I4q5kE9lR96TUSUhMTev9Q== X-IronPort-AV: E=McAfee;i="6700,10204,11267"; a="43816023" X-IronPort-AV: E=Sophos;i="6.12,185,1728975600"; d="scan'208";a="43816023" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2024 23:37:15 -0800 X-CSE-ConnectionGUID: Ecmn8NstTqOTKu2Ht1cbsg== X-CSE-MsgGUID: 9ZD8OibTSXWUxDjFnXIaVA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,185,1728975600"; d="scan'208";a="95950768" Received: from linux-pnp-server-11.sh.intel.com ([10.239.176.178]) by fmviesa005.fm.intel.com with ESMTP; 25 Nov 2024 23:37:13 -0800 From: Wangyang Guo To: fweimer@redhat.com, hjl.tools@gmail.com Cc: libc-alpha@sourceware.org, goldstein.w.n@gmail.com, tianyou.li@intel.com, Wangyang Guo Subject: [PATCH v4 2/3] benchtests: Add calloc function test to bench-malloc-thread Date: Tue, 26 Nov 2024 15:33:39 +0800 Message-ID: <20241126073340.3724382-3-wangyang.guo@intel.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241126073340.3724382-1-wangyang.guo@intel.com> References: <20241126073340.3724382-1-wangyang.guo@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org --- benchtests/bench-malloc-thread.c | 114 ++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 38 deletions(-) diff --git a/benchtests/bench-malloc-thread.c b/benchtests/bench-malloc-thread.c index 46fdabd30c..e8429cec10 100644 --- a/benchtests/bench-malloc-thread.c +++ b/benchtests/bench-malloc-thread.c @@ -123,6 +123,8 @@ alarm_handler (int signum) timeout = true; } +typedef size_t (*loop_func_t)(void **); + /* Allocate and free blocks in a random order. */ static size_t malloc_benchmark_loop (void **ptr_arr) @@ -145,10 +147,32 @@ malloc_benchmark_loop (void **ptr_arr) return iters; } +static size_t +calloc_benchmark_loop (void **ptr_arr) +{ + unsigned int offset_state = 0, block_state = 0; + size_t iters = 0; + + while (!timeout) + { + unsigned int next_idx = get_random_offset (&offset_state); + unsigned int next_block = get_random_block_size (&block_state); + + free (ptr_arr[next_idx]); + + ptr_arr[next_idx] = calloc (1, next_block); + + iters++; + } + + return iters; +} + struct thread_args { size_t iters; void **working_set; + loop_func_t benchmark_loop; timing_t elapsed; }; @@ -161,7 +185,7 @@ benchmark_thread (void *arg) timing_t start, stop; TIMING_NOW (start); - iters = malloc_benchmark_loop (thread_set); + iters = args->benchmark_loop (thread_set); TIMING_NOW (stop); TIMING_DIFF (args->elapsed, start, stop); @@ -171,7 +195,7 @@ benchmark_thread (void *arg) } static timing_t -do_benchmark (size_t num_threads, size_t *iters) +do_benchmark (loop_func_t benchmark_loop, size_t num_threads, size_t *iters) { timing_t elapsed = 0; @@ -183,7 +207,7 @@ do_benchmark (size_t num_threads, size_t *iters) memset (working_set, 0, sizeof (working_set)); TIMING_NOW (start); - *iters = malloc_benchmark_loop (working_set); + *iters = benchmark_loop (working_set); TIMING_NOW (stop); TIMING_DIFF (elapsed, start, stop); @@ -201,6 +225,7 @@ do_benchmark (size_t num_threads, size_t *iters) for (size_t i = 0; i < num_threads; i++) { args[i].working_set = working_set[i]; + args[i].benchmark_loop = benchmark_loop; pthread_create(&threads[i], NULL, benchmark_thread, &args[i]); } @@ -214,6 +239,47 @@ do_benchmark (size_t num_threads, size_t *iters) return elapsed; } +static void +bench_function (json_ctx_t *json_ctx, size_t num_threads, + const char *func_name, loop_func_t benchmark_loop) +{ + timing_t cur; + size_t iters = 0; + double d_total_s, d_total_i; + + init_random_values (); + + json_attr_object_begin (json_ctx, func_name); + + json_attr_object_begin (json_ctx, ""); + + timeout = false; + alarm (BENCHMARK_DURATION); + + cur = do_benchmark (benchmark_loop, num_threads, &iters); + + struct rusage usage; + getrusage(RUSAGE_SELF, &usage); + + d_total_s = cur; + d_total_i = iters; + + json_attr_double (json_ctx, "duration", d_total_s); + json_attr_double (json_ctx, "iterations", d_total_i); + json_attr_double (json_ctx, "time_per_iteration", d_total_s / d_total_i); + json_attr_double (json_ctx, "max_rss", usage.ru_maxrss); + + json_attr_double (json_ctx, "threads", num_threads); + json_attr_double (json_ctx, "min_size", MIN_ALLOCATION_SIZE); + json_attr_double (json_ctx, "max_size", MAX_ALLOCATION_SIZE); + json_attr_double (json_ctx, "random_seed", RAND_SEED); + + json_attr_object_end (json_ctx); + + json_attr_object_end (json_ctx); + +} + static void usage(const char *name) { fprintf (stderr, "%s: \n", name); @@ -223,10 +289,8 @@ static void usage(const char *name) int main (int argc, char **argv) { - timing_t cur; - size_t iters = 0, num_threads = 1; + size_t num_threads = 1; json_ctx_t json_ctx; - double d_total_s, d_total_i; struct sigaction act; if (argc == 1) @@ -246,48 +310,22 @@ main (int argc, char **argv) else usage(argv[0]); - init_random_values (); - - json_init (&json_ctx, 0, stdout); - - json_document_begin (&json_ctx); - - json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); - - json_attr_object_begin (&json_ctx, "functions"); - - json_attr_object_begin (&json_ctx, "malloc"); - - json_attr_object_begin (&json_ctx, ""); - memset (&act, 0, sizeof (act)); act.sa_handler = &alarm_handler; sigaction (SIGALRM, &act, NULL); - alarm (BENCHMARK_DURATION); - - cur = do_benchmark (num_threads, &iters); - - struct rusage usage; - getrusage(RUSAGE_SELF, &usage); + json_init (&json_ctx, 0, stdout); - d_total_s = cur; - d_total_i = iters; + json_document_begin (&json_ctx); - json_attr_double (&json_ctx, "duration", d_total_s); - json_attr_double (&json_ctx, "iterations", d_total_i); - json_attr_double (&json_ctx, "time_per_iteration", d_total_s / d_total_i); - json_attr_double (&json_ctx, "max_rss", usage.ru_maxrss); + json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); - json_attr_double (&json_ctx, "threads", num_threads); - json_attr_double (&json_ctx, "min_size", MIN_ALLOCATION_SIZE); - json_attr_double (&json_ctx, "max_size", MAX_ALLOCATION_SIZE); - json_attr_double (&json_ctx, "random_seed", RAND_SEED); + json_attr_object_begin (&json_ctx, "functions"); - json_attr_object_end (&json_ctx); + bench_function (&json_ctx, num_threads, "malloc", malloc_benchmark_loop); - json_attr_object_end (&json_ctx); + bench_function (&json_ctx, num_threads, "calloc", calloc_benchmark_loop); json_attr_object_end (&json_ctx);