mailing list of musl libc
 help / color / mirror / code / Atom feed
From: 张飞 <zhangfei@nj.iscas.ac.cn>
To: musl@lists.openwall.com
Subject: Re: Re: Re: Re: [musl] memset_riscv64
Date: Wed, 26 Apr 2023 15:25:11 +0800 (GMT+08:00)	[thread overview]
Message-ID: <37ca1f0c.1a115.187bc73b1d2.Coremail.zhangfei@nj.iscas.ac.cn> (raw)
In-Reply-To: <20230421133034.GS3630668@port70.net>

[-- Attachment #1: Type: text/plain, Size: 17550 bytes --]

Hi!
I used your test example and modified the code for the link you provided to enable it to
run on the riscv platform, just like test_memset1.c and test_memset2.c.

Here are the test results for test_memset1.c:
                                 First run result
------------------------------------------------------------------------------------
              |   C language implementation      | Basic instruction implementation
------------------------------------------------------------------------------------
size(bytes)   |  min_time(s)       speed(GB/s)   |  min_time(s)       speed(GB/s)
------------------------------------------------------------------------------------
16            |  0.000022152          0.18       |  0.000020818          0.20       
32            |  0.000026968          0.30       |  0.000023380          0.35       
48            |  0.000027450          0.45       |  0.000023860          0.52       
64            |  0.000029215          0.56       |  0.000024341          0.67       
96            |  0.000030178          0.81       |  0.000025302          0.97       
200           |  0.000033228          1.54       |  0.000027864          1.84       
300           |  0.000036279          2.12       |  0.000031227          2.46       
400           |  0.000039810          2.57       |  0.000033949          3.02       
600           |  0.000047836          3.21       |  0.000040515          3.79       
1024          |  0.000064531          4.06       |  0.000054127          4.84      
2048          |  0.000106910          4.90       |  0.000089998          5.83      
4096          |  0.000191828          5.47       |  0.000155656          6.74       
8192          |  0.000356046          5.89       |  0.000286811          7.31       
16384         |  0.000685124          6.12       |  0.000549441          7.63       
32768         |  0.001460304          5.74       |  0.001222189          6.86      
65536         |  0.012082280          1.39       |  0.012054872          1.39
------------------------------------------------------------------------------------

                                 Second run result 
------------------------------------------------------------------------------------
              |   C language implementation      | Basic instruction implementation
------------------------------------------------------------------------------------
size(bytes)   |  min_time(s)       speed(GB/s)   |  min_time(s)       speed(GB/s)
------------------------------------------------------------------------------------
16            |  0.000021755          0.19       |  0.000020750          0.20      
32            |  0.000026484          0.31       |  0.000022810          0.36      
48            |  0.000026957          0.46       |  0.000023601          0.52       
64            |  0.000028692          0.57       |  0.000023918          0.69       
96            |  0.000029638          0.83       |  0.000024868          0.99       
200           |  0.000032633          1.57       |  0.000027403          1.87       
300           |  0.000035628          2.16       |  0.000030887          2.49       
400           |  0.000038781          2.64       |  0.000033580          3.05        
600           |  0.000046979          3.27       |  0.000040233          3.82       
1024          |  0.000063532          4.13       |  0.000053538          4.90     
2048          |  0.000104993          4.99       |  0.000088861          5.90    
4096          |  0.000188389          5.57       |  0.000153804          6.82        
8192          |  0.000349664          6.00       |  0.000283691          7.39       
16384         |  0.000673000          6.23       |  0.000543464          7.72       
32768         |  0.001433181          5.85       |  0.001217448          6.89     
65536         |  0.011850111          1.42       |  0.011945281          1.40
------------------------------------------------------------------------------------

                                 Third run result 
------------------------------------------------------------------------------------
              |   C language implementation      | Basic instruction implementation
------------------------------------------------------------------------------------
size(bytes)   |  min_time(s)       speed(GB/s)   |  min_time(s)       speed(GB/s)
------------------------------------------------------------------------------------
16            |  0.000021885          0.19       |  0.000020816          0.20      
32            |  0.000026642          0.31       |  0.000023040          0.36      
48            |  0.000027118          0.45       |  0.000023676          0.52       
64            |  0.000028863          0.57       |  0.000024311          0.67      
96            |  0.000029814          0.82       |  0.000024947          0.99      
200           |  0.000034413          1.49       |  0.000027648          1.85       
300           |  0.000035841          2.14       |  0.000031144          2.47       
400           |  0.000039329          2.60       |  0.000034005          3.01        
600           |  0.000047259          3.25       |  0.000040360          3.81       
1024          |  0.000063752          4.11       |  0.000053867          4.87     
2048          |  0.000105620          4.96       |  0.000089302          5.87   
4096          |  0.000189513          5.53       |  0.000154610          6.78       
8192          |  0.000351749          5.96       |  0.000284591          7.37        
16384         |  0.000676855          6.20       |  0.000545187          7.69        
32768         |  0.001440141          5.82       |  0.001208756          6.94      
65536         |  0.011974218          1.40       |  0.011976172          1.40
------------------------------------------------------------------------------------

Here are the test results for test_memset2.c:

                        C language implementation
Random memset (bytes/ns):
           memset_call 32K: 0.36 64K: 0.29 128K: 0.25 256K: 0.23 512K: 0.22 1024K: 0.21 avg 0.25

Medium memset (bytes/ns):
           memset_call 8B: 0.28 16B: 0.30 32B: 0.48 64B: 0.86 128B: 1.55 256B: 2.60 512B: 3.86
Large memset (bytes/ns):
           memset_call 1K: 4.82 2K: 5.40 4K: 5.83 8K: 6.09 16K: 6.22 32K: 6.14 64K: 1.39

                        Basic instruction implementation
Random memset (bytes/ns):
           memset_call 32K: 0.45 64K: 0.35 128K: 0.30 256K: 0.28 512K: 0.27 1024K: 0.25 avg 0.30

Medium memset (bytes/ns):
           memset_call 8B: 0.18 16B: 0.48 32B: 0.91 64B: 1.63 128B: 2.71 256B: 4.40 512B: 5.67
Large memset (bytes/ns):
           memset_call 1K: 6.62 2K: 7.03 4K: 7.47 8K: 7.71 16K: 7.83 32K: 7.64 64K: 1.40

From the test results, it can be seen that the memset implemented in C language performs better 
at around 8 bytes, while in other cases, the assembly implementation will perform better.

Fei Zhang
&gt; -----原始邮件-----
&gt; 发件人: "Szabolcs Nagy" <nsz@port70.net>
&gt; 发送时间: 2023-04-21 21:30:34 (星期五)
&gt; 收件人: "张飞" <zhangfei@nj.iscas.ac.cn>
&gt; 抄送: musl@lists.openwall.com
&gt; 主题: Re: Re: Re: [musl] memset_riscv64
&gt; 
&gt; * 张飞 <zhangfei@nj.iscas.ac.cn> [2023-04-20 16:17:10 +0800]:
&gt; &gt; Hi!
&gt; &gt; I listened to your suggestions and referred to string.c in Musl's test set(libc-bench), 
&gt; &gt; and then modified the test cases. Since BUFLEN is a fixed value in strlen.c, I modified 
&gt; &gt; it to a variable as a parameter in my own test case and passed it to the memset function. 
&gt; &gt; I adjusted the LOOP_TIMES has been counted up to 500 times and the running time has been 
&gt; &gt; sorted, only recording the running time of the middle 300 times.
&gt; &gt; 
&gt; &gt; I took turns executing two programs on the SiFive chip three times each, and the results 
&gt; &gt; are shown below.
&gt; &gt;                              First run result
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; length(byte)  C language implementation(s)   Basic instruction implementation(s)
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 100                 0.002208102                     0.002304056
&gt; &gt; 200                 0.005053208                     0.004629598
&gt; &gt; 400                 0.008666684                     0.007739176
&gt; &gt; 800                 0.014065196                     0.012372702
&gt; &gt; 1600                0.023377685                     0.020090966
&gt; &gt; 3200                0.040221849                     0.034059631
&gt; &gt; 6400                0.072095377                     0.060028906
&gt; &gt; 12800               0.134040475                     0.110039387
&gt; &gt; 25600               0.257426806                     0.210710952
&gt; &gt; 51200               1.173755160                     1.121833227
&gt; &gt; 102400              3.693170402                     3.637194098
&gt; &gt; 204800              8.919975455                     8.865504460
&gt; &gt; 409600             19.410922418                    19.360956493
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 
&gt; &gt;                              Second run result 
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; length(byte)  C language implementation(s)   Basic instruction implementation(s)
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 100                 0.002208109                     0.002293857
&gt; &gt; 200                 0.005057374                     0.004640669
&gt; &gt; 400                 0.008674218                     0.007760795
&gt; &gt; 800                 0.014068582                     0.012417084
&gt; &gt; 1600                0.023381095                     0.020124496
&gt; &gt; 3200                0.040225138                     0.034093181
&gt; &gt; 6400                0.072098744                     0.060069574
&gt; &gt; 12800               0.134043954                     0.110088141
&gt; &gt; 25600               0.256453187                     0.208578633
&gt; &gt; 51200               1.166602505                     1.118972796
&gt; &gt; 102400              3.684957231                     3.635116808
&gt; &gt; 204800              8.916302592                     8.861590734
&gt; &gt; 409600             19.411057216                    19.358777670
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 
&gt; &gt;                              Third run result 
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; length(byte)  C language implementation(s)   Basic instruction implementation(s)
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 100                 0.002208111                     0.002293227
&gt; &gt; 200                 0.005056101                     0.004628539
&gt; &gt; 400                 0.008677756                     0.007748687
&gt; &gt; 800                 0.014085242                     0.012404443
&gt; &gt; 1600                0.023397782                     0.020115710
&gt; &gt; 3200                0.040242985                     0.034084435
&gt; &gt; 6400                0.072116665                     0.060063767
&gt; &gt; 12800               0.134060262                     0.110082427
&gt; &gt; 25600               0.257865186                     0.209101754
&gt; &gt; 51200               1.174257177                     1.117753408
&gt; &gt; 102400              3.696518162                     3.635417503
&gt; &gt; 204800              8.929357747                     8.858765915
&gt; &gt; 409600             19.426520562                     19.356515671
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 
&gt; &gt; From the test results, it can be seen that the runtime of memset implemented using the basic 
&gt; &gt; instruction set assembly is basically shorter than that implemented using the C language. 
&gt; &gt; May I ask if the test results are convincing?
&gt; 
&gt; small sizes are much more common than large sizes, memsets can be
&gt; distributed such that sizes [0,100), [100,1000), [1000,inf) are
&gt; used for 1/3 of all memsets each (not the call count, but the
&gt; amount of bytes memset using such sizes), i.e. if you speed up
&gt; the size = [100,1000) and [1000,inf) cases by 10% but regress the
&gt; [0,100) case by 20% then the overall performance roughly stays
&gt; the same. (of course this is very workload dependent, but across
&gt; a system this is what i'd expect, probably even more skewed to
&gt; smaller sizes).
&gt; 
&gt; so we need to know what happens in the [0,100) range. what i see
&gt; is a ~4% regression there while there is a ~10% improvement in
&gt; the [100,1000) case and ~15% improvement in the [1000,inf) case
&gt; (it would be nice to know why the 25k case is so much faster and
&gt; why that speed up only applies to that size, we don't want to
&gt; optimize for some obscure cpu bug that will go away next year)
&gt; 
&gt; on practical workloads i would expect &lt; 10% speedup overall from
&gt; the asm code (but we need more data in the [0,100) range to tell).
&gt; this may not be enough to justify the asm code.
&gt; 
&gt; rich already said he prefers a different style of implementation
&gt; (where the body of the function is in c but the inner loop is in
&gt; asm if that helps e.g. via simd).
&gt; 
&gt; here is an example of a benchmark that takes input distribution
&gt; into account from a workload:
&gt; https://github.com/ARM-software/optimized-routines/blob/master/string/bench/memset.c#L53
&gt; 
&gt; &gt; #include <stdio.h>
&gt; &gt; #include <stdlib.h>
&gt; &gt; #include <string.h>
&gt; &gt; #include <time.h>
&gt; &gt; 
&gt; &gt; #define BUFLEN 500000
&gt; &gt; #define LOOP_TIMES 500
&gt; &gt; 
&gt; &gt; int cmp(const void *a, const void *b) {
&gt; &gt;     double x = *(double *)a;
&gt; &gt;     double y = *(double *)b;
&gt; &gt;     if (x &lt; y) return -1;
&gt; &gt;     if (x &gt; y) return 1;
&gt; &gt;     return 0;
&gt; &gt; }
&gt; &gt; 
&gt; &gt; int main(){
&gt; &gt;         char *buf = malloc(BUFLEN);
&gt; &gt; 	double *arr = malloc(sizeof(double) * LOOP_TIMES);
&gt; &gt;         size_t i,j,k;
&gt; &gt;         struct timespec tv0,tv;
&gt; &gt; 	double times;
&gt; &gt; 
&gt; &gt;         for(j=100; j<buflen; j*="2){"> &gt;           for(k=0; k<loop_times; k++){=""> &gt;             for (i=0; i&lt;100; i++)
&gt; &gt;                   memset(buf+i, i, j-i);
&gt; &gt;           }
&gt; &gt;         }
&gt; &gt; 
&gt; &gt;         for(j=100; j<buflen; j*="2){"> &gt;           for(k=0; k<loop_times; k++){=""> &gt;             clock_gettime(CLOCK_REALTIME, &amp;tv0);
&gt; &gt;             for (i=0; i&lt;100; i++)
&gt; &gt;                   memset(buf+i, i, j-i);
&gt; 
&gt; alignment only matters up to 64 byte alignment and usually inputs
&gt; are at least 8byte aligned.
&gt; 
&gt; value is almost always 0. (we probably don't even need to test
&gt; non-0 case: a 0 check is correctly predicted in practice.)
&gt; 
&gt; i think length should have a small variation, just enough to add
&gt; penalty to small size checks where implementations may use many
&gt; branches.
&gt; 
&gt; so something like this may be better (madeup off,al numbers):
&gt; 
&gt; 	buf = malloc((1&lt;&lt;16)+32);
&gt; 	size_t sz[] = {16, 32, 48, 64, 96, 200, 300, 400, 600, 1&lt;&lt;10, 1&lt;&lt;11, 1&lt;&lt;12, 1&lt;&lt;13, 1&lt;&lt;14, 1&lt;&lt;15, 1&lt;&lt;16, 0};
&gt; 	size_t off[16] = {0, 0, 0, -8, 8, 16, 0, 0, -16, -12, 0, 4, -4, 0, 0, 12};
&gt; 	size_t al[16] = {0, 0, 8, 4, 8, 0, 8, 16, 8, 16, 4, 2, 1, 8, 16, 1};
&gt; 	for (j=0; sz[j]; j++)
&gt; 		for (k=0; k&lt;20; k++) {
&gt; 			t0 = tic();
&gt; 			// large loop count is important for small sizes
&gt; 			for (i=0; i&lt;256; i++)
&gt; 				memset(buf + al[i%16], 0, sz[j] + off[i%16]);
&gt; 			t1 = tic();
&gt; 			tmin = min(tmin,t1-t0);
&gt; 		}
&gt; 
&gt; large memset (&gt;=1k) can be tested separately (no.need to add off,al
&gt; variaion then, less inner loop is enough, but it should not hurt to
&gt; include them here).
&gt; 
&gt; &gt;             clock_gettime(CLOCK_REALTIME, &amp;tv);
&gt; &gt;             tv.tv_sec -= tv0.tv_sec;
&gt; &gt;             if ((tv.tv_nsec -= tv0.tv_nsec) &lt; 0) {
&gt; &gt;                 tv.tv_nsec += 1000000000;
&gt; &gt;                 tv.tv_sec--;
&gt; &gt;             }
&gt; &gt; 	    arr[k] = tv.tv_sec + (double)tv.tv_nsec/1000000000;
&gt; &gt;           }
&gt; &gt;           qsort(arr, 500, sizeof(double), cmp); 
&gt; 
&gt; just take the minimum. we want to know the fastest execution.
&gt; 
&gt; &gt;           
&gt; &gt; 	  for (int m = 100; m &lt; LOOP_TIMES - 100; m++) {
&gt; &gt;               times += arr[m];
&gt; &gt;           }
&gt; &gt; 	  printf("len: %ld  time: %.9lf\n",j, times);
&gt; 
&gt; you can also print GB/s which is 256*sz[j]/tmin in my example.
&gt; 
&gt; &gt; 	}
&gt; &gt;         free(buf);
&gt; &gt;         return 0;
&gt; &gt; }
</loop_times;></buflen;></loop_times;></buflen;></time.h></string.h></stdlib.h></stdio.h></zhangfei@nj.iscas.ac.cn></zhangfei@nj.iscas.ac.cn></nsz@port70.net>

[-- Attachment #2: test_memset1.c --]
[-- Type: text/plain, Size: 1395 bytes --]

#include <float.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main(){
	int j,k,i;
	double times;
	struct timespec tv0,tv;
	int sz[] = {16, 32, 48, 64, 96, 200, 300, 400, 600, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, 1<<16, 0};
	int off[16] = {0, 0, 0, -8, 8, 16, 0, 0, -16, -12, 0, 4, -4, 0, 0, 12};
	int al[16] = {0, 0, 8, 4, 8, 0, 8, 16, 8, 16, 4, 2, 1, 8, 16, 1};

	char *buf = malloc((1<<16)+32);
	if (buf == NULL) {
           printf("malloc failed\n");
           exit(1);
        }
	// warm up
        for (j=0; sz[j]; j++)
	    for (k=0; k<20; k++)
	        for (i=0; i<256; i++)
		    memset(buf + al[i%16], 0, sz[j] + off[i%16]);

	printf("%-15s %-20s %-20s\n", "size(bytes)", "min_time(s)", "speed(GB/s)");
	for (j=0; sz[j]; j++) {
	        double min_time = DBL_MAX;
		for (k=0; k<20; k++) {
			clock_gettime(CLOCK_REALTIME, &tv0);
			// large loop count is important for small sizes
			for (i=0; i<256; i++)
				memset(buf + al[i%16], 0, sz[j] + off[i%16]);
			clock_gettime(CLOCK_REALTIME, &tv);
			tv.tv_sec -= tv0.tv_sec;
			if ((tv.tv_nsec -= tv0.tv_nsec) < 0) {
				tv.tv_nsec += 1000000000;
				tv.tv_sec--;
			}
			times = tv.tv_sec + (double)tv.tv_nsec/1e9;
			min_time = min_time < times ? min_time : times;
		}
                printf("%-15d %-20.9lf %-20.2lf\n",sz[j], min_time, 256*sz[j] / (min_time * 1e9));
        }
	return 0;
}

[-- Attachment #3: test_memset2.c --]
[-- Type: text/plain, Size: 7461 bytes --]

/*
 * memset benchmark.
 *
 * Copyright (c) 2021, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
//#include "stringlib.h"
#include "benchlib.h"

#define ITERS  5000
#define ITERS2 20000000
#define ITERS3 1000000
#define NUM_TESTS 16384
#define MIN_SIZE 32768
#define MAX_SIZE (1024 * 1024)

static uint8_t a[MAX_SIZE + 4096] __attribute__((__aligned__(64)));

/*
#define F(x) {#x, x},

static const struct fun
{
  const char *name;
  void *(*fun)(void *, int, size_t);
} funtab[] =
{
#if __aarch64__
  F(__memset_aarch64)
#elif __arm__
  F(__memset_arm)
#endif
  F(memset)
#undef F
  {0, 0}
};
*/
typedef struct { uint32_t offset : 20, len : 12; } memset_test_t;
static memset_test_t test_arr[NUM_TESTS];

typedef struct { uint16_t size; uint16_t freq; } freq_data_t;
typedef struct { uint8_t align; uint16_t freq; } align_data_t;

#define SIZE_NUM 65536
#define SIZE_MASK (SIZE_NUM-1)
static uint8_t len_arr[SIZE_NUM];

/* Frequency data for memset sizes up to 4096 based on SPEC2017.  */
static freq_data_t memset_len_freq[] =
{
{40,28817}, {32,15336}, { 16,3823}, {296,3545}, { 24,3454}, {  8,1412},
{292,1202}, { 48, 927}, { 12, 613}, { 11, 539}, {284, 493}, {108, 414},
{ 88, 380}, { 20, 295}, {312, 271}, { 72, 233}, {  2, 200}, {  4, 192},
{ 15, 180}, { 14, 174}, { 13, 160}, { 56, 151}, { 36, 144}, { 64, 140},
{4095,133}, { 10, 130}, {  9, 124}, {  3, 124}, { 28, 120}, {  0, 118},
{288, 110}, {1152, 96}, {104,  90}, {  1,  86}, {832,  76}, {248,  74},
{1024, 69}, {120,  64}, {512,  63}, {384,  60}, {  6,  59}, { 80,  54},
{ 17,  50}, {  7,  49}, {520,  47}, {2048, 39}, {256,  37}, {864,  33},
{1440, 28}, { 22,  27}, {2056, 24}, {260,  23}, { 68,  23}, {  5,  22},
{ 18,  21}, {200,  18}, {2120, 18}, { 60,  17}, { 52,  16}, {336,  15},
{ 44,  13}, {192,  13}, {160,  12}, {2064, 12}, {128,  12}, { 76,  11},
{164,  11}, {152,  10}, {136,   9}, {488,   7}, { 96,   6}, {560,   6},
{1016,  6}, {112,   5}, {232,   5}, {168,   5}, {952,   5}, {184,   5},
{144,   4}, {252,   4}, { 84,   3}, {960,   3}, {3808,  3}, {244,   3},
{280,   3}, {224,   3}, {156,   3}, {1088,  3}, {440,   3}, {216,   2},
{304,   2}, { 23,   2}, { 25,   2}, { 26,   2}, {264,   2}, {328,   2},
{1096,  2}, {240,   2}, {1104,  2}, {704,   2}, {1664,  2}, {360,   2},
{808,   1}, {544,   1}, {236,   1}, {720,   1}, {368,   1}, {424,   1},
{640,   1}, {1112,  1}, {552,   1}, {272,   1}, {776,   1}, {376,   1},
{ 92,   1}, {536,   1}, {824,   1}, {496,   1}, {760,   1}, {792,   1},
{504,   1}, {344,   1}, {1816,  1}, {880,   1}, {176,   1}, {320,   1},
{352,   1}, {2008,  1}, {208,   1}, {408,   1}, {228,   1}, {2072,  1},
{568,   1}, {220,   1}, {616,   1}, {600,   1}, {392,   1}, {696,   1},
{2144,  1}, {1280,  1}, {2136,  1}, {632,   1}, {584,   1}, {456,   1},
{472,   1}, {3440,  1}, {2088,  1}, {680,   1}, {2928,  1}, {212,   1},
{648,   1}, {1752,  1}, {664,   1}, {3512,  1}, {1032,  1}, {528,   1},
{4072,  1}, {204,   1}, {2880,  1}, {3392,  1}, {712,   1}, { 59,   1},
{736,   1}, {592,   1}, {2520,  1}, {744,   1}, {196,   1}, {172,   1},
{728,   1}, {2040,  1}, {1192,  1}, {3600,  1}, {0, 0}
};

#define ALIGN_NUM 1024
#define ALIGN_MASK (ALIGN_NUM-1)
static uint8_t align_arr[ALIGN_NUM];

/* Alignment data for memset based on SPEC2017.  */
static align_data_t memset_align_freq[] =
{
 {16, 338}, {8, 307}, {32, 148}, {64, 131}, {4, 72}, {1, 23}, {2, 5}, {0, 0}
};

static void
init_memset_distribution (void)
{
  int i, j, freq, size, n;

  for (n = i = 0; (freq = memset_len_freq[i].freq) != 0; i++)
    for (j = 0, size = memset_len_freq[i].size; j < freq; j++)
      len_arr[n++] = size;
  assert (n == SIZE_NUM);

  for (n = i = 0; (freq = memset_align_freq[i].freq) != 0; i++)
    for (j = 0, size = memset_align_freq[i].align; j < freq; j++)
      align_arr[n++] = size - 1;
  assert (n == ALIGN_NUM);
}

static size_t
init_memset (size_t max_size)
{
  size_t total = 0;
  /* Create a random set of memsets with the given size and alignment
     distributions.  */
  for (int i = 0; i < NUM_TESTS; i++)
    {
      test_arr[i].offset = (rand32 (0) & (max_size - 1));
      test_arr[i].offset &= ~align_arr[rand32 (0) & ALIGN_MASK];
      test_arr[i].len = len_arr[rand32 (0) & SIZE_MASK];
      total += test_arr[i].len;
    }

  return total;
}


int main (void)
{
  init_memset_distribution ();

  memset (a, 1, sizeof (a));

  printf("Random memset (bytes/ns):\n");
  /*
  for (int f = 0; funtab[f].name != 0; f++)
    {
      size_t total_size = 0;
      uint64_t tsum = 0;
      printf ("%22s ", funtab[f].name);
      rand32 (0x12345678);

      for (int size = MIN_SIZE; size <= MAX_SIZE; size *= 2)
	{
	  size_t memset_size = init_memset (size) * ITERS;

	  for (int c = 0; c < NUM_TESTS; c++)
	    funtab[f].fun (a + test_arr[c].offset, 0, test_arr[c].len);

	  uint64_t t = clock_get_ns ();
	  for (int i = 0; i < ITERS; i++)
	    for (int c = 0; c < NUM_TESTS; c++)
	      funtab[f].fun (a + test_arr[c].offset, 0, test_arr[c].len);
	  t = clock_get_ns () - t;
	  total_size += memset_size;
	  tsum += t;
	  printf ("%dK: %.2f ", size / 1024, (double)memset_size / t);
	}
      printf( "avg %.2f\n", (double)total_size / tsum);
    }
    */
  size_t total_size = 0;
  uint64_t tsum = 0;
  printf ("%22s ", "memset_call");
  rand32 (0x12345678);

  for (int size = MIN_SIZE; size <= MAX_SIZE; size *= 2)
    {
      size_t memset_size = init_memset (size) * ITERS;

      for (int c = 0; c < NUM_TESTS; c++)
	memset (a + test_arr[c].offset, 0, test_arr[c].len);

      uint64_t t = clock_get_ns ();
      for (int i = 0; i < ITERS; i++)
	for (int c = 0; c < NUM_TESTS; c++)
	  memset (a + test_arr[c].offset, 0, test_arr[c].len);
      t = clock_get_ns () - t;
      total_size += memset_size;
      tsum += t;
      printf ("%dK: %.2f ", size / 1024, (double)memset_size / t);
    }
  printf( "avg %.2f\n", (double)total_size / tsum);


  printf ("\nMedium memset (bytes/ns):\n");
  /*
  for (int f = 0; funtab[f].name != 0; f++)
    {
      printf ("%22s ", funtab[f].name);

      for (int size = 8; size <= 512; size *= 2)
	{
	  uint64_t t = clock_get_ns ();
	  for (int i = 0; i < ITERS2; i++)
	    funtab[f].fun (a, 0, size);
	  t = clock_get_ns () - t;
	  printf ("%dB: %.2f ", size, (double)size * ITERS2 / t);
	}
      printf ("\n");
    }
    */
  printf ("%22s ", "memset_call");
  for (int size = 8; size <= 512; size *= 2)
    {
      uint64_t t = clock_get_ns ();
      for (int i = 0; i < ITERS2; i++)
	memset (a, 0, size);
      t = clock_get_ns () - t;
      printf ("%dB: %.2f ", size, (double)size * ITERS2 / t);
    }


  printf ("\nLarge memset (bytes/ns):\n");
  /*
  for (int f = 0; funtab[f].name != 0; f++)
    {
      printf ("%22s ", funtab[f].name);

      for (int size = 1024; size <= 65536; size *= 2)
	{
	  uint64_t t = clock_get_ns ();
	  for (int i = 0; i < ITERS3; i++)
	    funtab[f].fun (a, 0, size);
	  t = clock_get_ns () - t;
	  printf ("%dK: %.2f ", size / 1024, (double)size * ITERS3 / t);
	}
      printf ("\n");
    }
    */
  printf ("%22s ", "memset_call");
  for (int size = 1024; size <= 65536; size *= 2)
    {
      uint64_t t = clock_get_ns ();
      for (int i = 0; i < ITERS3; i++)
	memset (a, 0, size);
      t = clock_get_ns () - t;
      printf ("%dK: %.2f ", size / 1024, (double)size * ITERS3 / t);
    }
  printf ("\n\n");

  return 0;
}

      parent reply	other threads:[~2023-04-26  7:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-11  2:17 张飞
2023-04-11  9:48 ` Pedro Falcato
2023-04-19  5:33   ` 张飞
2023-04-19  9:02     ` Szabolcs Nagy
2023-04-20  8:17       ` 张飞
2023-04-21 13:30         ` Szabolcs Nagy
2023-04-21 14:50           ` Pedro Falcato
2023-04-21 16:54             ` Rich Felker
2023-04-21 17:01               ` enh
2023-04-26  7:25           ` 张飞 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=37ca1f0c.1a115.187bc73b1d2.Coremail.zhangfei@nj.iscas.ac.cn \
    --to=zhangfei@nj.iscas.ac.cn \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).