From patchwork Sat Jul 3 04:58:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Staples X-Patchwork-Id: 44132 X-Patchwork-Delegate: siddhesh@gotplt.org 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 130A13985447 for ; Sat, 3 Jul 2021 04:59:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 130A13985447 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625288359; bh=pbou4zT3NsOAcvzZd9aes/1wu102SlOtLvhu2HAmIeQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=PK5TpEP7Iy8W9ObLxFgwxG3Wg6Bjq8UAAgH7vZx++acF/uoH4hKJPX1GPo4f1XSUD LhxYrTXSWK23sh9UPtu2r7DUwl7Br8Bkah3VfOpo+UlBUrlWwP+0pa3blKLEe2yQn7 GCwLSWkvv6JiJtUlnL0pPihtKY+nrU4mvF8CVFPk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by sourceware.org (Postfix) with ESMTPS id 39B663839C61 for ; Sat, 3 Jul 2021 04:58:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 39B663839C61 Received: by mail-pf1-x441.google.com with SMTP id w22so7499970pff.5 for ; Fri, 02 Jul 2021 21:58:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=pbou4zT3NsOAcvzZd9aes/1wu102SlOtLvhu2HAmIeQ=; b=OK9aIObzv4QAIc1Xx+PqL+8B1cnikEyE4bvrwYcNr9V3im7zV8vFY1Q8HQ9xYgQRo9 5ro7wanyuv30YZWhf/w/fnAjyOzcfsBiP6CXx4PafEiXvNXD3B6pjfSgdOokuDqXZi5W UISWOrkIOg2HaEmNXjT6JKSeBuuG1ERG15mYECb6PS5j0SGYmwV6nJZ0a4PXZwFXNklG 9e2RuVBbdcDJS8J35L/okma1FtfMbhUhEfGpJrJggW+DD/MOFsRs1BOLfi34Ehe6LXGj +WhkqtzNIvgUinH+T7q3XuExzsHyeWZOfmXOwkLlnpTxKI/XPSOSQzCE/dq8f6Tr3Oqg f8Hw== X-Gm-Message-State: AOAM532B34TvgiStxBwWwMnN1MOnWwK6/tKG0bx0kKOdRfuKtBq1hOmM J+OyyATHZXfnIll9fB/ka/5ZMHgeRAhMpNWhlCw= X-Google-Smtp-Source: ABdhPJwrz45hXYXGSF8mJawY42ZvjN260bsEhwHrOv4Lt6hPBeVlnSEaeVRO4BFUSDo2qYhewWZLxw== X-Received: by 2002:aa7:8f2f:0:b029:313:f1e6:d576 with SMTP id y15-20020aa78f2f0000b0290313f1e6d576mr2961912pfr.25.1625288335237; Fri, 02 Jul 2021 21:58:55 -0700 (PDT) Received: from localhost.localdomain ([2600:8800:9200:c100:d7:ce47:7997:5411]) by smtp.gmail.com with ESMTPSA id t9sm7218561pjs.50.2021.07.02.21.58.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jul 2021 21:58:54 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] bench-malloc-thread.c: add assert that malloc() is never out of memory Date: Fri, 2 Jul 2021 21:58:45 -0700 Message-Id: <20210703045845.1610215-1-ercaguy@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Gabriel Staples via Libc-alpha From: Gabriel Staples Reply-To: Gabriel Staples Cc: Gabriel Staples Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" ...so we get realistic speed tests. Also add units to the results variables. And Replace tabs w/spaces for consistency, since the file was mixed format. --- benchtests/bench-malloc-thread.c | 68 +++++++++++++++++++------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/benchtests/bench-malloc-thread.c b/benchtests/bench-malloc-thread.c index 04b98c0af6..05e5170103 100644 --- a/benchtests/bench-malloc-thread.c +++ b/benchtests/bench-malloc-thread.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -31,8 +32,8 @@ #include "json-lib.h" /* Benchmark duration in seconds. */ -#define BENCHMARK_DURATION 10 -#define RAND_SEED 88 +#define BENCHMARK_DURATION 10 +#define RAND_SEED 88 #ifndef NUM_THREADS # define NUM_THREADS 1 @@ -44,10 +45,10 @@ However due to the distribution of the random block sizes the typical amount allocated will be much smaller. */ -#define WORKING_SET_SIZE 1024 +#define WORKING_SET_SIZE 1024 -#define MIN_ALLOCATION_SIZE 4 -#define MAX_ALLOCATION_SIZE 32768 +#define MIN_ALLOCATION_SIZE 4 +#define MAX_ALLOCATION_SIZE 32768 /* Get a random block size with an inverse square distribution. */ static unsigned int @@ -66,11 +67,11 @@ get_block_size (unsigned int rand_data) float r = (float) rand_data / RAND_MAX; return (unsigned int) powf ((max_pow - min_pow) * r + min_pow, - 1 / (exponent + 1)); + 1 / (exponent + 1)); } -#define NUM_BLOCK_SIZES 8000 -#define NUM_OFFSETS ((WORKING_SET_SIZE) * 4) +#define NUM_BLOCK_SIZES 8000 +#define NUM_OFFSETS ((WORKING_SET_SIZE) * 4) static unsigned int random_block_sizes[NUM_BLOCK_SIZES]; static unsigned int random_offsets[NUM_OFFSETS]; @@ -137,7 +138,14 @@ malloc_benchmark_loop (void **ptr_arr) free (ptr_arr[next_idx]); - ptr_arr[next_idx] = malloc (next_block); + void* ptr = malloc (next_block); + // allocation time isn't realistic if out of memory, so ensure all allocations succeed + if (ptr == NULL) + { + printf ("Out of memory\n"); + assert (ptr != NULL); + } + ptr_arr[next_idx] = ptr; iters++; } @@ -199,24 +207,24 @@ do_benchmark (size_t num_threads, size_t *iters) *iters = 0; for (size_t i = 0; i < num_threads; i++) - { - args[i].working_set = working_set[i]; - pthread_create(&threads[i], NULL, benchmark_thread, &args[i]); - } + { + args[i].working_set = working_set[i]; + pthread_create(&threads[i], NULL, benchmark_thread, &args[i]); + } for (size_t i = 0; i < num_threads; i++) - { - pthread_join(threads[i], NULL); - TIMING_ACCUM (elapsed, args[i].elapsed); - *iters += args[i].iters; - } + { + pthread_join(threads[i], NULL); + TIMING_ACCUM (elapsed, args[i].elapsed); + *iters += args[i].iters; + } } return elapsed; } -static void usage(const char *name) +static void usage(const char *executable_name) { - fprintf (stderr, "%s: \n", name); + fprintf (stderr, "Usage: %s: \n", executable_name); exit (1); } @@ -239,7 +247,7 @@ main (int argc, char **argv) ret = strtol(argv[1], NULL, 10); if (errno || ret == 0) - usage(argv[0]); + usage(argv[0]); num_threads = ret; } @@ -275,14 +283,18 @@ main (int argc, char **argv) d_total_s = cur; d_total_i = iters; - json_attr_double (&json_ctx, "duration", d_total_s); + // Units for x86 processors are "clock cycles" + json_attr_double (&json_ctx, "duration_clock_cycles", d_total_s); + // each iteration is equal to 1 free + 1 malloc 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); + // Units for x86 processors are "clock cycles" + json_attr_double (&json_ctx, "time_per_iteration_clock_cycles", d_total_s / d_total_i); + // Maximum resident set size (in kilobytes) + json_attr_double (&json_ctx, "max_rss_kb", usage.ru_maxrss); + + json_attr_double (&json_ctx, "num_threads", num_threads); + json_attr_double (&json_ctx, "min_allocation_size_bytes)", MIN_ALLOCATION_SIZE); + json_attr_double (&json_ctx, "max_allocation_size_bytes)", MAX_ALLOCATION_SIZE); json_attr_double (&json_ctx, "random_seed", RAND_SEED); json_attr_object_end (&json_ctx);