From patchwork Thu Feb 9 19:30:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 19200 Received: (qmail 112475 invoked by alias); 9 Feb 2017 19:30:58 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk 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 112451 invoked by uid 89); 9 Feb 2017 19:30:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=4096, 576, Destination, 37, 7 X-HELO: EUR03-VE1-obe.outbound.protection.outlook.com From: Wilco Dijkstra To: "libc-alpha@sourceware.org" CC: nd Subject: [PATCH v2] Add random memcpy test Date: Thu, 9 Feb 2017 19:30:44 +0000 Message-ID: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Wilco.Dijkstra@arm.com; x-ms-office365-filtering-correlation-id: b5d25206-23ec-4eb8-634b-08d45122245c x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081); SRVR:AM5PR0802MB2610; x-microsoft-exchange-diagnostics: 1; AM5PR0802MB2610; 7:jBfMYYit9yCc6SBbXEK0ENM4BZLEKxfl6R0Qp7j8YxlI8QkbzGao8no3VCnAPHwDucpCxTroGWt5QCWSgbWIwFdWiECvpoRnqstjT6dNcGS4QDFM9wshpsUb/vXj0akoTTr2KHiodV1Ou+S6mE1sDuHI66IegCrAJWZ/InzevEOWAfMQWKk7My5oX2Q/fuFRjpUmIQlDYj/q9VxsKT4JZFcbKbWuM2I/s9Pf6xhtMX1ojeApdkm+rXKRMiRaI8XeGsSQbLuJgz8I9pgghnhlylxOMfRpQlvuHcIo/NCrwvHeMSOQXg5ZkfcRCk3Z7BMKURWHkMs0V7jf1PN0H+rRGs2awhvf+ano2slycoeoTgfWR4oeGy6MMFZFXjaKp/6MC5Bgvt4icz9teazBumh3jGv/Kw+MHw/pl98e//MHZnm/vnSzoDCqxxRDXT1acGm44J0jQ3eHShhqcJBZHByi3rvdC44KXxIAPPurkyxY+bHtJUoTz+qjITZXM4kKe2TnSjw2WsgcYNsLtg9CCIAynQ== nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(250305191791016)(180628864354917)(22074186197030); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026)(6041248)(20161123564025)(20161123562025)(20161123558025)(20161123555025)(20161123560025)(6072148); SRVR:AM5PR0802MB2610; BCL:0; PCL:0; RULEID:; SRVR:AM5PR0802MB2610; x-forefront-prvs: 02135EB356 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(7916002)(39840400002)(39450400003)(39410400002)(39860400002)(39850400002)(377424004)(199003)(54534003)(189002)(97736004)(66066001)(86362001)(74316002)(2906002)(189998001)(2351001)(53936002)(6116002)(102836003)(81166006)(68736007)(2900100001)(101416001)(54356999)(50986999)(450100001)(122556002)(8676002)(81156014)(3846002)(6916009)(2501003)(4326007)(7736002)(92566002)(3280700002)(3660700001)(55016002)(106116001)(33656002)(305945005)(105586002)(7696004)(6436002)(8936002)(5640700003)(106356001)(25786008)(6506006)(9686003)(110136004)(5660300001)(77096006)(6306002)(38730400002)(99286003)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:AM5PR0802MB2610; H:AM5PR0802MB2610.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; received-spf: None (protection.outlook.com: arm.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Feb 2017 19:30:44.5694 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0802MB2610 The 2nd version of this patch removes the made-up distribution and one based on a real trace instead: Add a new randomized memcpy test for copies up to 256 bytes. The distribution of size and alignment is based on a trace of SPEC2006 (other traces could be added in the future). Instead of repeating the same copy over and over again like the existing tests, it times several thousand different copies to more accurately estimate the overhead of branch prediction due to the different sizes and alignments. ChangeLog: 2017-02-09 Wilco Dijkstra * benchtests/Makefile (string-benchset): Add memcpy-random. * benchtests/bench-memcpy-random.c: New file. diff --git a/benchtests/Makefile b/benchtests/Makefile index 81edf8a933ce7371ac60c118a4a46daee6391800..a96e9533b3b2d7223c2ce90a723b6a434ba1a1ea 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -37,7 +37,7 @@ string-benchset := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \ strcat strchr strchrnul strcmp strcpy strcspn strlen \ strncasecmp strncat strncmp strncpy strnlen strpbrk strrchr \ strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok \ - strcoll memcpy-large memmove-large memset-large + strcoll memcpy-large memcpy-random memmove-large memset-large # Build and run locale-dependent benchmarks only if we're building natively. ifeq (no,$(cross-compiling)) diff --git a/benchtests/bench-memcpy-random.c b/benchtests/bench-memcpy-random.c new file mode 100644 index 0000000000000000000000000000000000000000..0e6955b292cde96d3100429e19138ba3d0114096 --- /dev/null +++ b/benchtests/bench-memcpy-random.c @@ -0,0 +1,157 @@ +/* Measure memcpy performance. + Copyright (C) 2016-2017 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 + . */ + +#define MIN_PAGE_SIZE 131072 +#define TEST_MAIN +#define TEST_NAME "memcpy-random" +#include "bench-string.h" +#include + +IMPL (memcpy, 0) + +#define NUM_COPIES 4096 + +typedef struct { uint16_t size; uint16_t freq; } freq_data_t; +typedef struct { uint8_t align; uint8_t freq; } align_data_t; + +#define SIZE_NUM 1024 +#define SIZE_MASK (SIZE_NUM-1) +static uint8_t size_arr[SIZE_NUM]; + +/* Frequency data for memcpy of less than 256 bytes based on SPEC2006. */ +static freq_data_t size_freq[] = +{ + { 8, 576}, {104, 94}, { 24, 78}, { 48, 58}, { 32, 48}, { 16, 46}, + { 1, 30}, { 96, 12}, { 72, 11}, {216, 11}, {192, 8}, { 12, 7}, + {144, 5}, { 2, 4}, { 64, 4}, {120, 4}, { 4, 3}, { 40, 2}, + { 7, 2}, {168, 2}, {160, 2}, {128, 1}, { 3, 1}, { 9, 1}, + {176, 1}, {240, 1}, { 11, 1}, { 0, 1}, { 5, 1}, { 6, 1}, + { 80, 1}, { 52, 1}, {152, 1}, { 10, 1}, { 56, 1}, { 51, 1}, + { 14, 1}, {208, 1}, { 0, 0} +}; + +#define ALIGN_NUM 256 +#define ALIGN_MASK (ALIGN_NUM-1) +static uint8_t src_align_arr[ALIGN_NUM]; +static uint8_t dst_align_arr[ALIGN_NUM]; + +/* Source alignment frequency for memcpy based on SPEC2006. */ +static align_data_t src_align_freq[] = +{ + {16, 144}, {8, 86}, {3, 23}, {1, 3}, {0, 0} +}; + +/* Destination alignment frequency for memcpy based on SPEC2006. */ +static align_data_t dst_align_freq[] = +{ + {16, 197}, {8, 30}, {3, 23}, {1, 6}, {0, 0} +}; + +typedef struct +{ + uint16_t src; + uint16_t dst; + uint16_t len; +} copy_t; + +static copy_t copy[NUM_COPIES]; + +typedef char *(*proto_t) (char *, const char *, size_t); + +static void +init_copy_distribution (void) +{ + int i, j, freq, size, n; + + for (n = i = 0; freq = size_freq[i].freq; i++) + for (j = 0, size = size_freq[i].size; j < freq; j++) + size_arr[n++] = size; + assert (n == SIZE_NUM); + + for (n = i = 0; freq = src_align_freq[i].freq; i++) + for (j = 0, size = src_align_freq[i].align; j < freq; j++) + src_align_arr[n++] = size - 1; + assert (n == ALIGN_NUM); + + for (n = i = 0; freq = dst_align_freq[i].freq; i++) + for (j = 0, size = dst_align_freq[i].align; j < freq; j++) + dst_align_arr[n++] = size - 1; + assert (n == ALIGN_NUM); +} + + +static void +do_one_test (impl_t *impl, char *dst, char *src, copy_t *copy, size_t n) +{ + timing_t start, stop, cur; + size_t iters = INNER_LOOP_ITERS * 20; + + TIMING_NOW (start); + for (int i = 0; i < iters; ++i) + for (int j = 0; j < n; j++) + CALL (impl, dst + copy[j].dst, src + copy[j].src, copy[j].len); + TIMING_NOW (stop); + + TIMING_DIFF (cur, start, stop); + + TIMING_PRINT_MEAN ((double) cur, (double) iters); +} + +static void +do_test (size_t max_size) +{ + for (int i = 0; i < max_size; i++) + buf1[i] = i * 3; + + /* Create a random set of copies with the given size and alignment + distributions. */ + for (int i = 0; i < NUM_COPIES; i++) + { + copy[i].dst = (rand () & (max_size - 1)) | 1; + copy[i].dst &= ~dst_align_arr[rand () & ALIGN_MASK]; + copy[i].src = (rand () & (max_size - 1)) | 3; + copy[i].src &= ~src_align_arr[rand () & ALIGN_MASK]; + copy[i].len = size_arr[rand () & SIZE_MASK]; + } + + printf ("Memory size %6zd:", max_size); + + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) buf2, (char *) buf1, copy, NUM_COPIES); + + putchar ('\n'); +} + +int +test_main (void) +{ + test_init (); + init_copy_distribution (); + + printf ("%23s", ""); + FOR_EACH_IMPL (impl, 0) + printf ("\t%s", impl->name); + putchar ('\n'); + + for (int i = 4; i <= 64; i = i * 2) + do_test (i * 1024); + + return ret; +} + +#include