From patchwork Thu Aug 29 06:27:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Wangyang" X-Patchwork-Id: 1978236 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=lThXh9SC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WvWd657nhz1yfy for ; Thu, 29 Aug 2024 16:31:06 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8AAE4385ED4A for ; Thu, 29 Aug 2024 06:31:04 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by sourceware.org (Postfix) with ESMTPS id D8046385DDF8 for ; Thu, 29 Aug 2024 06:30:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D8046385DDF8 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 D8046385DDF8 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724913011; cv=none; b=CXwBk+XCYxxb7f6HHBefHrY3XTFPhUSSk/gE6GkVtuVBhRb1csyTbK7IKkP1APCggFyAvmJSdXRr6woQyc27U4ovwBlvjrC9ZcBGglyf6CtW2fgss/B5Q/v5cXM0ErkgSNOoHHfp+LASigTWdit3FyaX2aPfMtXK1ROG02lVKFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724913011; c=relaxed/simple; bh=Ri4agsZHRwLwgb7E+XLgCzZkZ70mvET+Zk7J/VbE6Pk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Xg6YQqSp5rMZfdqBdRHoZ7nDL/tSMzBb33gdXnbj06trtrlOhf+Bk/5vdz0oYngTKw5YSH0UJiKuEmY38X9DEv8eqZxvb0fzWP7iYIdtQE7TkRsOZufOUoJqOLLzmPQu0j1JvCiaBPAFk6FmCYExa3hLJU068B5kN3VMBYqc6vc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724913008; x=1756449008; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ri4agsZHRwLwgb7E+XLgCzZkZ70mvET+Zk7J/VbE6Pk=; b=lThXh9SCSAU805bqTMFjOXBaMQMYoNPvrUifngmt7x41CcqRKJfWswYA +cOdOxhveXFoZ8u8BnJmToYw2ghjbIjcG3JALLxdgoyOh+OSG8DhZVJuv tY9qIDKjUYEN7tngn0rJovnO/YXc+QN6LQG9nuqkiEp72KDBQOYZoN7yZ IcW6TEDl7L+07t7XpmQxGTKPqwRAJDhuEHV0ZVIXWQ/+gq0p85AxdeBAF tFw6yCQh3KQDxgLS/ce5uV35fhJp4pGuCRpxlM6o4ZBBWiYXzTUxDUzFj 1pXflVKpupMD0QT9EEM/8VGhnIEjqxcaqgktmcA6YLetJFHCC13gJDvNY A==; X-CSE-ConnectionGUID: ceNQSSprQGqUlahzOaQ3/w== X-CSE-MsgGUID: pw9B6P4QQ4+Ft7XduD6/zA== X-IronPort-AV: E=McAfee;i="6700,10204,11178"; a="22998386" X-IronPort-AV: E=Sophos;i="6.10,185,1719903600"; d="scan'208";a="22998386" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2024 23:30:07 -0700 X-CSE-ConnectionGUID: XL+Ywkc6Ql+/2ZsGLx9etw== X-CSE-MsgGUID: fe1ce4l2Ro+6dr+/HakjSw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,185,1719903600"; d="scan'208";a="63539175" Received: from linux-pnp-server-11.sh.intel.com ([10.239.176.178]) by fmviesa009.fm.intel.com with ESMTP; 28 Aug 2024 23:30:05 -0700 From: Wangyang Guo To: libc-alpha@sourceware.org Cc: fweimer@redhat.com, goldstein.w.n@gmail.com, tianyou.li@intel.com, Wangyang Guo Subject: [PATCH v3 4/5] benchtests: Add calloc function test to bench-malloc-thread Date: Thu, 29 Aug 2024 14:27:31 +0800 Message-ID: <20240829062732.1663342-5-wangyang.guo@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240829062732.1663342-1-wangyang.guo@intel.com> References: <20240829062732.1663342-1-wangyang.guo@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 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, T_SCC_BODY_TEXT_LINE 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~incoming=patchwork.ozlabs.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);