1/*
2 * memcpy benchmark.
3 *
4 * Copyright (c) 2020, Arm Limited.
5 * SPDX-License-Identifier: MIT
6 */
7
8#define _GNU_SOURCE
9#include <stdint.h>
10#include <stdio.h>
11#include <string.h>
12#include <assert.h>
13#include "stringlib.h"
14#include "benchlib.h"
15
16#define ITERS 5000
17#define ITERS2 20000000
18#define ITERS3 500000
19#define MAX_COPIES 8192
20#define SIZE (256*1024)
21
22static uint8_t a[SIZE + 4096] __attribute__((__aligned__(64)));
23static uint8_t b[SIZE + 4096] __attribute__((__aligned__(64)));
24
25#define F(x) {#x, x},
26
27static const struct fun
28{
29  const char *name;
30  void *(*fun)(void *, const void *, size_t);
31} funtab[] =
32{
33  F(memcpy)
34#if __aarch64__
35  F(__memcpy_aarch64)
36# if __ARM_NEON
37  F(__memcpy_aarch64_simd)
38# endif
39#elif __arm__
40  F(__memcpy_arm)
41#endif
42#undef F
43  {0, 0}
44};
45
46typedef struct { uint16_t size; uint16_t freq; } freq_data_t;
47typedef struct { uint8_t align; uint16_t freq; } align_data_t;
48
49#define SIZE_NUM 65536
50#define SIZE_MASK (SIZE_NUM-1)
51static uint8_t size_arr[SIZE_NUM];
52
53/* Frequency data for memcpy of less than 4096 bytes based on SPEC2017.  */
54static freq_data_t size_freq[] =
55{
56{32,22320}, { 16,9554}, {  8,8915}, {152,5327}, {  4,2159}, {292,2035},
57{ 12,1608}, { 24,1343}, {1152,895}, {144, 813}, {884, 733}, {284, 721},
58{120, 661}, {  2, 649}, {882, 550}, {  5, 475}, {  7, 461}, {108, 460},
59{ 10, 361}, {  9, 361}, {  6, 334}, {  3, 326}, {464, 308}, {2048,303},
60{  1, 298}, { 64, 250}, { 11, 197}, {296, 194}, { 68, 187}, { 15, 185},
61{192, 184}, {1764,183}, { 13, 173}, {560, 126}, {160, 115}, {288,  96},
62{104,  96}, {1144, 83}, { 18,  80}, { 23,  78}, { 40,  77}, { 19,  68},
63{ 48,  63}, { 17,  57}, { 72,  54}, {1280, 51}, { 20,  49}, { 28,  47},
64{ 22,  46}, {640,  45}, { 25,  41}, { 14,  40}, { 56,  37}, { 27,  35},
65{ 35,  33}, {384,  33}, { 29,  32}, { 80,  30}, {4095, 22}, {232,  22},
66{ 36,  19}, {184,  17}, { 21,  17}, {256,  16}, { 44,  15}, { 26,  15},
67{ 31,  14}, { 88,  14}, {176,  13}, { 33,  12}, {1024, 12}, {208,  11},
68{ 62,  11}, {128,  10}, {704,  10}, {324,  10}, { 96,  10}, { 60,   9},
69{136,   9}, {124,   9}, { 34,   8}, { 30,   8}, {480,   8}, {1344,  8},
70{273,   7}, {520,   7}, {112,   6}, { 52,   6}, {344,   6}, {336,   6},
71{504,   5}, {168,   5}, {424,   5}, {  0,   4}, { 76,   3}, {200,   3},
72{512,   3}, {312,   3}, {240,   3}, {960,   3}, {264,   2}, {672,   2},
73{ 38,   2}, {328,   2}, { 84,   2}, { 39,   2}, {216,   2}, { 42,   2},
74{ 37,   2}, {1608,  2}, { 70,   2}, { 46,   2}, {536,   2}, {280,   1},
75{248,   1}, { 47,   1}, {1088,  1}, {1288,  1}, {224,   1}, { 41,   1},
76{ 50,   1}, { 49,   1}, {808,   1}, {360,   1}, {440,   1}, { 43,   1},
77{ 45,   1}, { 78,   1}, {968,   1}, {392,   1}, { 54,   1}, { 53,   1},
78{ 59,   1}, {376,   1}, {664,   1}, { 58,   1}, {272,   1}, { 66,   1},
79{2688,  1}, {472,   1}, {568,   1}, {720,   1}, { 51,   1}, { 63,   1},
80{ 86,   1}, {496,   1}, {776,   1}, { 57,   1}, {680,   1}, {792,   1},
81{122,   1}, {760,   1}, {824,   1}, {552,   1}, { 67,   1}, {456,   1},
82{984,   1}, { 74,   1}, {408,   1}, { 75,   1}, { 92,   1}, {576,   1},
83{116,   1}, { 65,   1}, {117,   1}, { 82,   1}, {352,   1}, { 55,   1},
84{100,   1}, { 90,   1}, {696,   1}, {111,   1}, {880,   1}, { 79,   1},
85{488,   1}, { 61,   1}, {114,   1}, { 94,   1}, {1032,  1}, { 98,   1},
86{ 87,   1}, {584,   1}, { 85,   1}, {648,   1}, {0, 0}
87};
88
89#define ALIGN_NUM 1024
90#define ALIGN_MASK (ALIGN_NUM-1)
91static uint8_t src_align_arr[ALIGN_NUM];
92static uint8_t dst_align_arr[ALIGN_NUM];
93
94/* Source alignment frequency for memcpy based on SPEC2017.  */
95static align_data_t src_align_freq[] =
96{
97  {8, 300}, {16, 292}, {32, 168}, {64, 153}, {4, 79}, {2, 14}, {1, 18}, {0, 0}
98};
99
100static align_data_t dst_align_freq[] =
101{
102  {8, 265}, {16, 263}, {64, 209}, {32, 174}, {4, 90}, {2, 10}, {1, 13}, {0, 0}
103};
104
105typedef struct
106{
107  uint64_t src : 24;
108  uint64_t dst : 24;
109  uint64_t len : 16;
110} copy_t;
111
112static copy_t copy[MAX_COPIES];
113
114typedef char *(*proto_t) (char *, const char *, size_t);
115
116static void
117init_copy_distribution (void)
118{
119  int i, j, freq, size, n;
120
121  for (n = i = 0; (freq = size_freq[i].freq) != 0; i++)
122    for (j = 0, size = size_freq[i].size; j < freq; j++)
123      size_arr[n++] = size;
124  assert (n == SIZE_NUM);
125
126  for (n = i = 0; (freq = src_align_freq[i].freq) != 0; i++)
127    for (j = 0, size = src_align_freq[i].align; j < freq; j++)
128      src_align_arr[n++] = size - 1;
129  assert (n == ALIGN_NUM);
130
131  for (n = i = 0; (freq = dst_align_freq[i].freq) != 0; i++)
132    for (j = 0, size = dst_align_freq[i].align; j < freq; j++)
133      dst_align_arr[n++] = size - 1;
134  assert (n == ALIGN_NUM);
135}
136
137static size_t
138init_copies (size_t max_size)
139{
140  size_t total = 0;
141  /* Create a random set of copies with the given size and alignment
142     distributions.  */
143  for (int i = 0; i < MAX_COPIES; i++)
144    {
145      copy[i].dst = (rand32 (0) & (max_size - 1));
146      copy[i].dst &= ~dst_align_arr[rand32 (0) & ALIGN_MASK];
147      copy[i].src = (rand32 (0) & (max_size - 1));
148      copy[i].src &= ~src_align_arr[rand32 (0) & ALIGN_MASK];
149      copy[i].len = size_arr[rand32 (0) & SIZE_MASK];
150      total += copy[i].len;
151    }
152
153  return total;
154}
155
156int main (void)
157{
158  init_copy_distribution ();
159
160  memset (a, 1, sizeof (a));
161  memset (b, 2, sizeof (b));
162
163  printf("Random memcpy:\n");
164  for (int f = 0; funtab[f].name != 0; f++)
165    {
166      size_t total = 0;
167      uint64_t tsum = 0;
168      printf ("%22s (B/ns) ", funtab[f].name);
169      rand32 (0x12345678);
170
171      for (int size = 16384; size <= SIZE; size *= 2)
172	{
173	  size_t copy_size = init_copies (size) * ITERS;
174
175	  for (int c = 0; c < MAX_COPIES; c++)
176	    funtab[f].fun (b + copy[c].dst, a + copy[c].src, copy[c].len);
177
178	  uint64_t t = clock_get_ns ();
179	  for (int i = 0; i < ITERS; i++)
180	    for (int c = 0; c < MAX_COPIES; c++)
181	      funtab[f].fun (b + copy[c].dst, a + copy[c].src, copy[c].len);
182	  t = clock_get_ns () - t;
183	  total += copy_size;
184	  tsum += t;
185	  printf ("%dK: %.2f ", size / 1024, (double)copy_size / t);
186	}
187      printf( "avg %.2f\n", (double)total / tsum);
188    }
189
190  printf ("\nMedium memcpy:\n");
191  for (int f = 0; funtab[f].name != 0; f++)
192    {
193      printf ("%22s (B/ns) ", funtab[f].name);
194
195      for (int size = 16; size <= 512; size *= 2)
196	{
197	  uint64_t t = clock_get_ns ();
198	  for (int i = 0; i < ITERS2; i++)
199	    funtab[f].fun (b, a, size);
200	  t = clock_get_ns () - t;
201	  printf ("%d%c: %.2f ", size < 1024 ? size : size / 1024,
202		  size < 1024 ? 'B' : 'K', (double)size * ITERS2 / t);
203	}
204      printf ("\n");
205    }
206
207  printf ("\nLarge memcpy:\n");
208  for (int f = 0; funtab[f].name != 0; f++)
209    {
210      printf ("%22s (B/ns) ", funtab[f].name);
211
212      for (int size = 1024; size <= 32768; size *= 2)
213	{
214	  uint64_t t = clock_get_ns ();
215	  for (int i = 0; i < ITERS3; i++)
216	    funtab[f].fun (b, a, size);
217	  t = clock_get_ns () - t;
218	  printf ("%d%c: %.2f ", size < 1024 ? size : size / 1024,
219		  size < 1024 ? 'B' : 'K', (double)size * ITERS3 / t);
220	}
221      printf ("\n");
222    }
223
224  printf ("\nUnaligned forwards memmove:\n");
225  for (int f = 0; funtab[f].name != 0; f++)
226    {
227      printf ("%22s (B/ns) ", funtab[f].name);
228
229      for (int size = 1024; size <= 32768; size *= 2)
230	{
231	  uint64_t t = clock_get_ns ();
232	  for (int i = 0; i < ITERS3; i++)
233	    funtab[f].fun (a, a + 256 + (i & 31), size);
234	  t = clock_get_ns () - t;
235	  printf ("%d%c: %.2f ", size < 1024 ? size : size / 1024,
236		  size < 1024 ? 'B' : 'K', (double)size * ITERS3 / t);
237	}
238      printf ("\n");
239    }
240
241
242  printf ("\nUnaligned backwards memmove:\n");
243  for (int f = 0; funtab[f].name != 0; f++)
244    {
245      printf ("%22s (B/ns) ", funtab[f].name);
246
247      for (int size = 1024; size <= 32768; size *= 2)
248	{
249	  uint64_t t = clock_get_ns ();
250	  for (int i = 0; i < ITERS3; i++)
251	    funtab[f].fun (a + 256 + (i & 31), a, size);
252	  t = clock_get_ns () - t;
253	  printf ("%d%c: %.2f ", size < 1024 ? size : size / 1024,
254		  size < 1024 ? 'B' : 'K', (double)size * ITERS3 / t);
255	}
256      printf ("\n");
257    }
258
259  return 0;
260}
261