From 7ae8d724355cb474684bb30562732c2d47b42a36 Mon Sep 17 00:00:00 2001 From: Nils K Date: Sun, 19 Apr 2020 19:07:46 +0200 Subject: [PATCH] added tools to test branch perdiction performance --- branch_predict_test/.gitignore | 1 + branch_predict_test/Makefile | 2 + branch_predict_test/branch_predict | Bin 0 -> 13560 bytes branch_predict_test/branch_predict.c | 227 +++++++++++++++++++++++++++ branch_predict_test/test.sh | 7 + 5 files changed, 237 insertions(+) create mode 100644 branch_predict_test/.gitignore create mode 100644 branch_predict_test/Makefile create mode 100755 branch_predict_test/branch_predict create mode 100644 branch_predict_test/branch_predict.c create mode 100644 branch_predict_test/test.sh diff --git a/branch_predict_test/.gitignore b/branch_predict_test/.gitignore new file mode 100644 index 0000000..2211df6 --- /dev/null +++ b/branch_predict_test/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/branch_predict_test/Makefile b/branch_predict_test/Makefile new file mode 100644 index 0000000..0aab384 --- /dev/null +++ b/branch_predict_test/Makefile @@ -0,0 +1,2 @@ +build: branch_predict.c + gcc branch_predict.c -O2 -o branch_predict.exe diff --git a/branch_predict_test/branch_predict b/branch_predict_test/branch_predict new file mode 100755 index 0000000000000000000000000000000000000000..e1090789711545ee3238664e38badb4b4691f425 GIT binary patch literal 13560 zcmeHOe{@vUoxhVI5CYCb1dYGSs9^ zc{9wkJw5v;moxd^@8|pDe(%rs?tPapGzFKHxm<#iTPzmD)m~;J4N9+S&W6JfTy3wyy`75N2kWpevx|CXu8y%G2 zd4*&zqML14FlD>X!j9tnQ&W{vT&2n@)h-8B{7hAK#ckcuws~`J>+YD{9gU^d&t6|Y zZ}z;o{$$*LyR0|)C*9>MSId&9zqx5C=BfA*M)K1wFWf%ueDb?{mz5vc_Tdk{v2DWW zV`R%eWdG#!5j1^8wC@Bqr3n6|68M89@O35dXbJr868MWH@b(h;Kb64OmcS>W@qCaK zrd|NW+96#6zoi7;QUae;0;lOytlXPQ;MFDYYf98>3-oWquP{9gpjf#ApY?B zy@_bdi0J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 diff --git a/branch_predict_test/branch_predict.c b/branch_predict_test/branch_predict.c new file mode 100644 index 0000000..10e3d20 --- /dev/null +++ b/branch_predict_test/branch_predict.c @@ -0,0 +1,227 @@ +#include +#include +#include +#include + +/* + * range of the chars in the randomly generated string + * + * 33 = '!' + * 127 = '~' + * + */ + +#define CHAR_START 33 +#define CHAR_END 127 + +/* + * returns dynamically allocated random string + * with a strlen between minLen and maxLen + * the memory size is strlen + 1 + * + */ + +char* rand_word(unsigned minLen, unsigned maxLen); +/* + * uses rand returned value >= min and value <= max + */ +unsigned rand_between(unsigned min, unsigned max); + +/* + * returns an array of dynamically allocated strings + * length of array is n + 1, the last value is NULL + */ +char** random_strings(size_t n, unsigned minLen, unsigned maxLen); + +/* + * returns strlen without newline + * removes terminates string at newline position + */ +size_t remove_newline(char* str); + +/*max line size with newline and terminating 0*/ +#define MAX_LINE_SIZE 1024 +/* + * nRead, out: allowed to be NULL + * nRead represents actually read strings, without trailing NULL + * e.g. if 0 strings are read, nRead will be 0, and the returned array will be {NULL} + * returns string array, null terminated + * + */ +char** read_strings(const char* fileName, size_t* nRead); + +/* + * count set bits in string s + */ +int string_bit_count(char* s); + +/* + * returns malloced memory of size memSize + * if it fails, it exits with 7 the programm + * + */ +void* non_null_malloc(size_t memSize); + +int string_sort_func(const void* a, const void* b); + +void usage_and_exit(); + +void usage_and_exit() { + printf("usage\n"); + printf(" measure \n"); + printf(" sort \n"); + printf(" generate \n"); + exit(1); +} +int main(int argc, char* argv[]) +{ + _Bool generate = 0; + _Bool measure = 0; + _Bool sort = 0; + if(argc < 2 ) { + usage_and_exit(); + } + measure = strcmp(argv[1], "measure") == 0; + generate = strcmp(argv[1], "generate") == 0; + sort = strcmp(argv[1], "sort") == 0; + if((!generate) && (!measure) && (!sort)) { + usage_and_exit(); + } + if(measure) { + if(argc != 3 ) { + usage_and_exit(); + } + char** array = read_strings(argv[2], NULL); + char** p = array; + unsigned long long counter = 0; + clock_t t1, t0 = clock(); + for(; *p; ++p) { + counter += string_bit_count(*p); + } + t1 = clock(); + printf("measure file: <%s>\n", argv[2]); + printf("counter = %llu\n", counter); + printf("time = %lf ms\n", (t1 - t0) / (double) CLOCKS_PER_SEC * 1000.0); + + } else if(generate) { + if(argc != 5 ) { + usage_and_exit(); + } + unsigned n, minLen, maxLen; + sscanf(argv[2], "%u", &n); + sscanf(argv[3], "%u", &minLen); + sscanf(argv[4], "%u", &maxLen); + char** words = random_strings(n, minLen, maxLen); + for(int i=0; i\n", s); + int count = 0; + int len = strlen(s); + for(int i=0; i 0) { + sp = non_null_malloc(len+1); + memcpy(sp, buff, len+1); + if(out_array_pos + 1 > out_array_len / sizeof(char*)) { + out_array_len = out_array_len * 2; + out_array = realloc(out_array, out_array_len); + if(! out_array) { perror("allocation failed"); exit(1); } + } + out_array[out_array_pos++] = sp; + out_array[out_array_pos] = NULL; + } + } + fclose(f); + if(nRead) *nRead = out_array_pos; + return out_array; + +} diff --git a/branch_predict_test/test.sh b/branch_predict_test/test.sh new file mode 100644 index 0000000..0501f33 --- /dev/null +++ b/branch_predict_test/test.sh @@ -0,0 +1,7 @@ +./branch_predict generate 5000000 10 10 > unsorted_sample01.txt +./branch_predict sort unsorted_sample01.txt > issorted_sample01.txt +echo "head of unsorted_sample01.txt" >> output.txt +head unsorted_sample01.txt >> output.txt +echo "\n" >> output.txt +./branch_predict measure unsorted_sample01.txt >> output.txt +./branch_predict measure issorted_sample01.txt >> output.txt