| 1 | /* Measure bzero functions. |
| 2 | Copyright (C) 2022-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #define TEST_MAIN |
| 20 | #ifdef DO_MEMSET |
| 21 | # define TEST_NAME "memset" |
| 22 | #else |
| 23 | # define TEST_NAME "bzero" |
| 24 | #endif |
| 25 | #define MIN_PAGE_SIZE 131072 |
| 26 | #include "bench-string.h" |
| 27 | |
| 28 | #include "json-lib.h" |
| 29 | |
| 30 | #ifdef DO_MEMSET |
| 31 | void *generic_memset (void *, int, size_t); |
| 32 | |
| 33 | typedef void *(*proto_t) (void *, int, size_t); |
| 34 | |
| 35 | IMPL (memset, 1) |
| 36 | IMPL (generic_memset, 0) |
| 37 | |
| 38 | #else |
| 39 | static void |
| 40 | memset_zero (void * s, size_t len) |
| 41 | { |
| 42 | memset (s, '\0', len); |
| 43 | } |
| 44 | |
| 45 | typedef void (*proto_t) (void *, size_t); |
| 46 | |
| 47 | IMPL (bzero, 1) |
| 48 | IMPL (memset_zero, 0) |
| 49 | #endif |
| 50 | |
| 51 | static void |
| 52 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, size_t n) |
| 53 | { |
| 54 | size_t i, iters = INNER_LOOP_ITERS8; |
| 55 | timing_t start, stop, cur; |
| 56 | |
| 57 | TIMING_NOW (start); |
| 58 | for (i = 0; i < iters; ++i) |
| 59 | { |
| 60 | #ifdef DO_MEMSET |
| 61 | CALL (impl, s, 0, n); |
| 62 | #else |
| 63 | CALL (impl, s, n); |
| 64 | #endif |
| 65 | } |
| 66 | TIMING_NOW (stop); |
| 67 | |
| 68 | TIMING_DIFF (cur, start, stop); |
| 69 | |
| 70 | json_element_double (ctx: json_ctx, d: (double) cur / (double) iters); |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | do_test (json_ctx_t *json_ctx, size_t align, size_t len) |
| 75 | { |
| 76 | align &= 4095; |
| 77 | if ((align + len) * sizeof (CHAR) > page_size) |
| 78 | return; |
| 79 | |
| 80 | json_element_object_begin (ctx: json_ctx); |
| 81 | json_attr_uint (ctx: json_ctx, name: "length" , d: len); |
| 82 | json_attr_uint (ctx: json_ctx, name: "alignment" , d: align); |
| 83 | json_array_begin (ctx: json_ctx, name: "timings" ); |
| 84 | |
| 85 | FOR_EACH_IMPL (impl, 0) |
| 86 | { |
| 87 | do_one_test (json_ctx, impl, s: (CHAR *) (buf1) + align, n: len); |
| 88 | } |
| 89 | |
| 90 | json_array_end (ctx: json_ctx); |
| 91 | json_element_object_end (ctx: json_ctx); |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | test_main (void) |
| 96 | { |
| 97 | json_ctx_t json_ctx; |
| 98 | size_t i; |
| 99 | |
| 100 | test_init (); |
| 101 | alloc_bufs (); |
| 102 | json_init (ctx: &json_ctx, indent_level: 0, stdout); |
| 103 | |
| 104 | json_document_begin (ctx: &json_ctx); |
| 105 | json_attr_string (ctx: &json_ctx, name: "timing_type" , TIMING_TYPE); |
| 106 | |
| 107 | json_attr_object_begin (ctx: &json_ctx, name: "functions" ); |
| 108 | json_attr_object_begin (ctx: &json_ctx, TEST_NAME); |
| 109 | json_attr_string (ctx: &json_ctx, name: "bench-variant" , s: "default" ); |
| 110 | |
| 111 | json_array_begin (ctx: &json_ctx, name: "ifuncs" ); |
| 112 | FOR_EACH_IMPL (impl, 0) |
| 113 | json_element_string (ctx: &json_ctx, s: impl->name); |
| 114 | json_array_end (ctx: &json_ctx); |
| 115 | |
| 116 | json_array_begin (ctx: &json_ctx, name: "results" ); |
| 117 | |
| 118 | for (i = 0; i < 18; ++i) |
| 119 | do_test (json_ctx: &json_ctx, align: 0, len: 1 << i); |
| 120 | for (i = 0; i < 64; ++i) |
| 121 | { |
| 122 | do_test (json_ctx: &json_ctx, align: i, len: i); |
| 123 | do_test (json_ctx: &json_ctx, align: 4096 - i, len: i); |
| 124 | do_test (json_ctx: &json_ctx, align: 4095, len: i); |
| 125 | if (i & (i - 1)) |
| 126 | do_test (json_ctx: &json_ctx, align: 0, len: i); |
| 127 | } |
| 128 | for (i = 32; i < 1024; i+=32) |
| 129 | { |
| 130 | do_test (json_ctx: &json_ctx, align: 0, len: i); |
| 131 | do_test (json_ctx: &json_ctx, align: i, len: i); |
| 132 | } |
| 133 | do_test (json_ctx: &json_ctx, align: 1, len: 14); |
| 134 | do_test (json_ctx: &json_ctx, align: 3, len: 1024); |
| 135 | do_test (json_ctx: &json_ctx, align: 4, len: 64); |
| 136 | do_test (json_ctx: &json_ctx, align: 2, len: 25); |
| 137 | |
| 138 | for (i = 33; i <= 256; i += 4) |
| 139 | { |
| 140 | do_test (json_ctx: &json_ctx, align: 0, len: 32 * i); |
| 141 | do_test (json_ctx: &json_ctx, align: i, len: 32 * i); |
| 142 | } |
| 143 | |
| 144 | json_array_end (ctx: &json_ctx); |
| 145 | json_attr_object_end (ctx: &json_ctx); |
| 146 | json_attr_object_end (ctx: &json_ctx); |
| 147 | json_document_end (ctx: &json_ctx); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | #include <support/test-driver.c> |
| 153 | |
| 154 | #ifdef DO_MEMSET |
| 155 | # define libc_hidden_builtin_def(X) |
| 156 | # define libc_hidden_def(X) |
| 157 | # define libc_hidden_weak(X) |
| 158 | # define weak_alias(X,Y) |
| 159 | # undef MEMSET |
| 160 | # define MEMSET generic_memset |
| 161 | # include <string/memset.c> |
| 162 | #endif |
| 163 | |