From patchwork Fri Oct 3 15:11:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Newton X-Patchwork-Id: 38330 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f70.google.com (mail-wg0-f70.google.com [74.125.82.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 56CB32057C for ; Fri, 3 Oct 2014 15:12:08 +0000 (UTC) Received: by mail-wg0-f70.google.com with SMTP id a1sf817663wgh.1 for ; Fri, 03 Oct 2014 08:12:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:mailing-list :precedence:list-id:list-unsubscribe:list-subscribe:list-archive :list-post:list-help:sender:delivered-to:from:to:subject:date :message-id:in-reply-to:references:x-original-sender :x-original-authentication-results; bh=aIkQZBGeHqsQPAhjC+8h07WrSTvkzKiS28OOIJzcw7U=; b=bhReP2IZisY1tuR/BO0/FKxvly/+BFwkhHYZuq20Cd3r8nowcNCPd9Uifo8cNAxuGn UJpKus4AZGu5ZXEF5eNBtlqfZx7L/mT4eUSdx6fRW+u/nvbHGeVPgajxuFN1F/P7B8ST wFPw0J6zqjXyrYwK7w9D71Jq8PQ4GpovP0WAOixl2evWZjLJmmx+Rm/98j/cWjcuRiw0 W8wVtXG/neV28skjm+M695AdaGtIk/ZmcTS9s3ihjPqlm3qFlfDI69IZJpOoQHaJHrke yo5rgA+EWJOpdIqwuFSeSX6odbtvFo8VvQxHn1Lfy00jUzN/tWqlz7G9+B/fzdahAuhG 2w8Q== X-Gm-Message-State: ALoCoQm8kShGzNGPR7SXNKTq9UwpakaRN/x228xMgpuzyS+nFy4RhCZD2ss5qvPEGpzmnoX4vNW8 X-Received: by 10.180.221.236 with SMTP id qh12mr1935949wic.4.1412349127561; Fri, 03 Oct 2014 08:12:07 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.19.73 with SMTP id c9ls360574lae.37.gmail; Fri, 03 Oct 2014 08:12:07 -0700 (PDT) X-Received: by 10.152.44.136 with SMTP id e8mr6793394lam.36.1412349127397; Fri, 03 Oct 2014 08:12:07 -0700 (PDT) Received: from mail-la0-x235.google.com (mail-la0-x235.google.com [2a00:1450:4010:c03::235]) by mx.google.com with ESMTPS id dv4si11538391lbc.129.2014.10.03.08.12.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 03 Oct 2014 08:12:07 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::235 as permitted sender) client-ip=2a00:1450:4010:c03::235; Received: by mail-la0-f53.google.com with SMTP id gq15so1246692lab.12 for ; Fri, 03 Oct 2014 08:12:07 -0700 (PDT) X-Received: by 10.152.7.73 with SMTP id h9mr6475125laa.27.1412349127299; Fri, 03 Oct 2014 08:12:07 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.130.169 with SMTP id of9csp245954lbb; Fri, 3 Oct 2014 08:12:06 -0700 (PDT) X-Received: by 10.66.185.49 with SMTP id ez17mr7751219pac.58.1412349123596; Fri, 03 Oct 2014 08:12:03 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id fm5si7311812pbd.234.2014.10.03.08.12.02 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Oct 2014 08:12:03 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-53275-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 3291 invoked by alias); 3 Oct 2014 15:11:40 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list List-Id: List-Unsubscribe: , List-Subscribe: List-Archive: List-Post: , List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 3164 invoked by uid 89); 3 Oct 2014 15:11:39 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-wi0-f180.google.com X-Received: by 10.180.104.201 with SMTP id gg9mr13468961wib.15.1412349094789; Fri, 03 Oct 2014 08:11:34 -0700 (PDT) From: Will Newton To: libc-alpha@sourceware.org Subject: [PATCH 1/3] benchtests: Add malloc microbenchmark Date: Fri, 3 Oct 2014 16:11:24 +0100 Message-Id: <1412349086-11473-2-git-send-email-will.newton@linaro.org> In-Reply-To: <1412349086-11473-1-git-send-email-will.newton@linaro.org> References: <1412349086-11473-1-git-send-email-will.newton@linaro.org> X-Original-Sender: will.newton@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::235 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@sourceware.org X-Google-Group-Id: 836684582541 Add a microbenchmark for measuring malloc and free performance. The benchmark allocates and frees buffers of random sizes in a random order and measures the overall execution time and RSS. The random block sizes used follow an inverse square distribution which is intended to mimic the behaviour of real applications which tend to allocate many more small blocks than large ones. ChangeLog: 2014-10-03 Will Newton * benchtests/Makefile (stdlib-bench): Add malloc benchmark. * benchtests/bench-malloc.c: New file. --- benchtests/Makefile | 2 +- benchtests/bench-malloc.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 benchtests/bench-malloc.c diff --git a/benchtests/Makefile b/benchtests/Makefile index fd3036d..1f8eb82 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -37,7 +37,7 @@ string-bench := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \ strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok string-bench-all := $(string-bench) -stdlib-bench := strtod +stdlib-bench := strtod malloc benchset := $(string-bench-all) $(stdlib-bench) diff --git a/benchtests/bench-malloc.c b/benchtests/bench-malloc.c new file mode 100644 index 0000000..54631ed --- /dev/null +++ b/benchtests/bench-malloc.c @@ -0,0 +1,219 @@ +/* Benchmark malloc and free functions. + Copyright (C) 2013-2014 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bench-timing.h" +#include "json-lib.h" + +/* Benchmark duration in seconds. */ +#define BENCHMARK_DURATION 60 +#define RAND_SEED 88 + +/* Maximum memory that can be allocated at any one time is: + + WORKING_SET_SIZE * MAX_ALLOCATION_SIZE + + However due to the distribution of the random block sizes + the typical amount allocated will be much smaller. */ +#define WORKING_SET_SIZE 1024 + +#define MIN_ALLOCATION_SIZE 4 +#define MAX_ALLOCATION_SIZE 32768 + +/* Get a random block size with an inverse square distribution. */ +static unsigned int +get_block_size (unsigned int rand_data) +{ + /* Inverse square. */ + const float exponent = -2; + /* Minimum value of distribution. */ + const float dist_min = MIN_ALLOCATION_SIZE; + /* Maximum value of distribution. */ + const float dist_max = MAX_ALLOCATION_SIZE; + + float min_pow = powf (dist_min, exponent + 1); + float max_pow = powf (dist_max, exponent + 1); + + float r = (float) rand_data / RAND_MAX; + + return (unsigned int) powf ((max_pow - min_pow) * r + min_pow, 1 / (exponent + 1)); +} + +#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]; + +static void +init_random_values (void) +{ + for (size_t i = 0; i < NUM_BLOCK_SIZES; i++) + random_block_sizes[i] = get_block_size (rand ()); + + for (size_t i = 0; i < NUM_OFFSETS; i++) + random_offsets[i] = rand () % WORKING_SET_SIZE; +} + +static unsigned int +get_random_block_size (unsigned int *state) +{ + unsigned int idx = *state; + + if (idx >= NUM_BLOCK_SIZES - 1) + idx = 0; + else + idx++; + + *state = idx; + + return random_block_sizes[idx]; +} + +static unsigned int +get_random_offset (unsigned int *state) +{ + unsigned int idx = *state; + + if (idx >= NUM_OFFSETS - 1) + idx = 0; + else + idx++; + + *state = idx; + + return random_offsets[idx]; +} + +static volatile bool timeout; + +static void alarm_handler (int signum) +{ + timeout = true; +} + +/* Allocate and free blocks in a random order. */ +static size_t +malloc_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] = malloc (next_block); + + iters++; + } + + return iters; +} + +static timing_t +do_benchmark (size_t *iters) +{ + timing_t elapsed, start, stop; + void *working_set[WORKING_SET_SIZE]; + + memset (working_set, 0, sizeof (working_set)); + + TIMING_NOW (start); + *iters = malloc_benchmark_loop (working_set); + TIMING_NOW (stop); + + TIMING_DIFF (elapsed, start, stop); + + return elapsed; +} + +int +main (int argc, char **argv) +{ + timing_t cur; + size_t iters = 0; + unsigned long res; + json_ctx_t json_ctx; + double d_total_s, d_total_i; + struct sigaction act; + + 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, ""); + + TIMING_INIT (res); + + (void) res; + + memset (&act, 0, sizeof (act)); + act.sa_handler = &alarm_handler; + + sigaction (SIGALRM, &act, NULL); + + alarm (BENCHMARK_DURATION); + + cur = do_benchmark (&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, "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); + + json_attr_object_end (&json_ctx); + + json_document_end (&json_ctx); + + return 0; +}