]>
Commit | Line | Data |
---|---|---|
d01cbb6e SP |
1 | /* Measure memcpy performance. |
2 | Copyright (C) 2016-2017 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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #define MIN_PAGE_SIZE 131072 | |
20 | #define TEST_MAIN | |
29d92a8e | 21 | #define TEST_NAME "memcpy" |
d01cbb6e SP |
22 | #include "bench-string.h" |
23 | #include <assert.h> | |
5ee1e3ce | 24 | #include "json-lib.h" |
d01cbb6e | 25 | |
29d92a8e | 26 | IMPL (memcpy, 1) |
d01cbb6e SP |
27 | |
28 | #define NUM_COPIES 4096 | |
29 | ||
30 | typedef struct { uint16_t size; uint16_t freq; } freq_data_t; | |
31 | typedef struct { uint8_t align; uint8_t freq; } align_data_t; | |
32 | ||
33 | #define SIZE_NUM 1024 | |
34 | #define SIZE_MASK (SIZE_NUM-1) | |
35 | static uint8_t size_arr[SIZE_NUM]; | |
36 | ||
37 | /* Frequency data for memcpy of less than 256 bytes based on SPEC2006. */ | |
38 | static freq_data_t size_freq[] = | |
39 | { | |
40 | { 8, 576}, {104, 94}, { 24, 78}, { 48, 58}, { 32, 48}, { 16, 46}, | |
41 | { 1, 30}, { 96, 12}, { 72, 11}, {216, 11}, {192, 8}, { 12, 7}, | |
42 | {144, 5}, { 2, 4}, { 64, 4}, {120, 4}, { 4, 3}, { 40, 2}, | |
43 | { 7, 2}, {168, 2}, {160, 2}, {128, 1}, { 3, 1}, { 9, 1}, | |
44 | {176, 1}, {240, 1}, { 11, 1}, { 0, 1}, { 5, 1}, { 6, 1}, | |
45 | { 80, 1}, { 52, 1}, {152, 1}, { 10, 1}, { 56, 1}, { 51, 1}, | |
46 | { 14, 1}, {208, 1}, { 0, 0} | |
47 | }; | |
48 | ||
49 | #define ALIGN_NUM 256 | |
50 | #define ALIGN_MASK (ALIGN_NUM-1) | |
51 | static uint8_t src_align_arr[ALIGN_NUM]; | |
52 | static uint8_t dst_align_arr[ALIGN_NUM]; | |
53 | ||
54 | /* Source alignment frequency for memcpy based on SPEC2006. */ | |
55 | static align_data_t src_align_freq[] = | |
56 | { | |
57 | {16, 144}, {8, 86}, {3, 23}, {1, 3}, {0, 0} | |
58 | }; | |
59 | ||
60 | /* Destination alignment frequency for memcpy based on SPEC2006. */ | |
61 | static align_data_t dst_align_freq[] = | |
62 | { | |
63 | {16, 197}, {8, 30}, {3, 23}, {1, 6}, {0, 0} | |
64 | }; | |
65 | ||
66 | typedef struct | |
67 | { | |
68 | uint16_t src; | |
69 | uint16_t dst; | |
70 | uint16_t len; | |
71 | } copy_t; | |
72 | ||
73 | static copy_t copy[NUM_COPIES]; | |
74 | ||
75 | typedef char *(*proto_t) (char *, const char *, size_t); | |
76 | ||
77 | static void | |
78 | init_copy_distribution (void) | |
79 | { | |
80 | int i, j, freq, size, n; | |
81 | ||
82 | for (n = i = 0; (freq = size_freq[i].freq) != 0; i++) | |
83 | for (j = 0, size = size_freq[i].size; j < freq; j++) | |
84 | size_arr[n++] = size; | |
85 | assert (n == SIZE_NUM); | |
86 | ||
87 | for (n = i = 0; (freq = src_align_freq[i].freq) != 0; i++) | |
88 | for (j = 0, size = src_align_freq[i].align; j < freq; j++) | |
89 | src_align_arr[n++] = size - 1; | |
90 | assert (n == ALIGN_NUM); | |
91 | ||
92 | for (n = i = 0; (freq = dst_align_freq[i].freq) != 0; i++) | |
93 | for (j = 0, size = dst_align_freq[i].align; j < freq; j++) | |
94 | dst_align_arr[n++] = size - 1; | |
95 | assert (n == ALIGN_NUM); | |
96 | } | |
97 | ||
98 | ||
99 | static void | |
5ee1e3ce SP |
100 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, char *src, |
101 | copy_t *copy, size_t n) | |
d01cbb6e SP |
102 | { |
103 | timing_t start, stop, cur; | |
104 | size_t iters = INNER_LOOP_ITERS * 20; | |
105 | ||
106 | TIMING_NOW (start); | |
107 | for (int i = 0; i < iters; ++i) | |
108 | for (int j = 0; j < n; j++) | |
109 | CALL (impl, dst + copy[j].dst, src + copy[j].src, copy[j].len); | |
110 | TIMING_NOW (stop); | |
111 | ||
112 | TIMING_DIFF (cur, start, stop); | |
113 | ||
5ee1e3ce | 114 | json_element_double (json_ctx, (double) cur / (double) iters); |
d01cbb6e SP |
115 | } |
116 | ||
117 | static void | |
5ee1e3ce | 118 | do_test (json_ctx_t *json_ctx, size_t max_size) |
d01cbb6e SP |
119 | { |
120 | for (int i = 0; i < max_size; i++) | |
121 | buf1[i] = i * 3; | |
122 | ||
123 | /* Create a random set of copies with the given size and alignment | |
124 | distributions. */ | |
125 | for (int i = 0; i < NUM_COPIES; i++) | |
126 | { | |
127 | copy[i].dst = (rand () & (max_size - 1)) | 1; | |
128 | copy[i].dst &= ~dst_align_arr[rand () & ALIGN_MASK]; | |
129 | copy[i].src = (rand () & (max_size - 1)) | 3; | |
130 | copy[i].src &= ~src_align_arr[rand () & ALIGN_MASK]; | |
131 | copy[i].len = size_arr[rand () & SIZE_MASK]; | |
132 | } | |
133 | ||
5ee1e3ce SP |
134 | json_element_object_begin (json_ctx); |
135 | json_attr_uint (json_ctx, "max-size", (double) max_size); | |
136 | json_array_begin (json_ctx, "timings"); | |
d01cbb6e SP |
137 | |
138 | FOR_EACH_IMPL (impl, 0) | |
5ee1e3ce | 139 | do_one_test (json_ctx, impl, (char *) buf2, (char *) buf1, copy, NUM_COPIES); |
d01cbb6e | 140 | |
5ee1e3ce SP |
141 | json_array_end (json_ctx); |
142 | json_element_object_end (json_ctx); | |
d01cbb6e SP |
143 | } |
144 | ||
145 | int | |
146 | test_main (void) | |
147 | { | |
5ee1e3ce SP |
148 | json_ctx_t json_ctx; |
149 | ||
d01cbb6e SP |
150 | test_init (); |
151 | init_copy_distribution (); | |
152 | ||
5ee1e3ce SP |
153 | json_init (&json_ctx, 0, stdout); |
154 | ||
155 | json_document_begin (&json_ctx); | |
156 | json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); | |
157 | ||
158 | json_attr_object_begin (&json_ctx, "functions"); | |
7ee38e60 | 159 | json_attr_object_begin (&json_ctx, TEST_NAME); |
5ee1e3ce SP |
160 | json_attr_string (&json_ctx, "bench-variant", "random"); |
161 | ||
162 | json_array_begin (&json_ctx, "ifuncs"); | |
d01cbb6e | 163 | FOR_EACH_IMPL (impl, 0) |
5ee1e3ce SP |
164 | json_element_string (&json_ctx, impl->name); |
165 | json_array_end (&json_ctx); | |
d01cbb6e | 166 | |
5ee1e3ce | 167 | json_array_begin (&json_ctx, "results"); |
d01cbb6e | 168 | for (int i = 4; i <= 64; i = i * 2) |
5ee1e3ce SP |
169 | do_test (&json_ctx, i * 1024); |
170 | ||
171 | json_array_end (&json_ctx); | |
172 | json_attr_object_end (&json_ctx); | |
173 | json_attr_object_end (&json_ctx); | |
174 | json_document_end (&json_ctx); | |
d01cbb6e SP |
175 | |
176 | return ret; | |
177 | } | |
178 | ||
179 | #include <support/test-driver.c> |