add zlibwrapper sample test
Signed-off-by: Wenkai Lin linwenkai6@hisilicon.com --- test/zlibwrapper.c | 410 +++++++++++++++++++++++++++++++++++++++++++++ test/zlibwrapper.h | 15 ++ 2 files changed, 425 insertions(+) create mode 100644 test/zlibwrapper.c create mode 100644 test/zlibwrapper.h
diff --git a/test/zlibwrapper.c b/test/zlibwrapper.c new file mode 100644 index 0000000..20c053f --- /dev/null +++ b/test/zlibwrapper.c @@ -0,0 +1,410 @@ +#include "acc_v2_common.h" +#include "acc_v2_util.h" +#include <assert.h> + +#define RESERVE_MM 1024 +#define SIZE_1K 1024 + +static int stream_chunk = 1024 * 128; + +struct create_comp_data { + int threadid; + long long loop; + int numa_id; + bool sync; + bool use_env; + char *infile; + char *outfile; + char *alg; + enum wd_comp_op_type op; + acc_source_mode src_type; + acc_mem_mode mem_type; + comp_check_type check_type; + + size_t input_size; + size_t output_size; + int count; + int thread_num; + int cmode; + int win_sz; + int win_bs; + int comp_lv; + int debug; + int result; + struct v2_setup_util *util; + handle_t mp, bp; + bool perf_flag; + unsigned long perf; + double ops; +}; + +static int test_uadk_zlib_deflate(z_stream *zstrmp, void *src, size_t src_len, void *dest, + size_t *dst_len, int level, int windowbits) +{ + __u32 chunk = stream_chunk; + z_stream zstrm = {0}; + int ret, flush, have; + + if (zstrmp == NULL) { + zstrmp = &zstrm; + } + ret = wd_deflate_init(&zstrm, level, windowbits); + if (ret) { + LOG(ERR, "fail to init deflate.\n"); + return ret; + } + + zstrm.next_in = src; + do { + if (src_len > chunk) { + zstrm.avail_in = chunk; + src_len -= chunk; + } else { + zstrm.avail_in = src_len; + src_len = 0; + } + + flush = src_len ? Z_SYNC_FLUSH : Z_FINISH; + + /* + * Run wd_deflate() on input until output buffer not full, + * finish compression if all of source has been read in. + */ + do { + zstrm.avail_out = chunk; + zstrm.next_out = dest; + ret = wd_deflate(&zstrm, flush); + if (ret < 0) { + LOG(ERR, "Error: wd_deflate fail, ret = %d", ret); + return ret; + } + have = chunk - zstrm.avail_out; + dest += have; + } while (zstrm.avail_in > 0); + + /* done when last data in file processed */ + } while (flush != Z_FINISH); + + LOG(DBG, "zstrm.total_out is %lld\n", zstrm.total_out); + *dst_len = zstrm.total_out; + + (void)wd_deflate_end(&zstrm); + if (ret != Z_STREAM_END) { + ret = Z_ERRNO; + } else { + ret = Z_OK; + } + return ret; +} + +static int test_uadk_zlib_inflate(z_stream *zstrmp, void *src, size_t src_len, void *dest, + size_t *dst_len, int windowbits) +{ + __u32 chunk = stream_chunk; + z_stream zstrm = {0}; + int ret, have; + + if (zstrmp == NULL) { + zstrmp = &zstrm; + } + ret = wd_inflate_init(&zstrm, windowbits); + if (ret) { + LOG(ERR, "fail to init inflate.\n"); + return ret; + } + + zstrm.next_in = src; + do { + if (src_len > chunk) { + zstrm.avail_in = chunk; + src_len -= chunk; + } else { + zstrm.avail_in = src_len; + src_len = 0; + } + /* + * Run wd_deflate() on input until output buffer not full, + * finish compression if all of source has been read in. + */ + do { + zstrm.avail_out = chunk; + zstrm.next_out = dest; + ret = wd_inflate(&zstrm, Z_SYNC_FLUSH); + if (ret < 0) { + LOG(ERR, "Error: wd_deflate fail, ret = %d", ret); + return ret; + } + have = chunk - zstrm.avail_out; + dest += have; + } while (zstrm.avail_in > 0); + + /* done when last data in file processed */ + } while (ret != Z_STREAM_END); + + LOG(DBG, "zstrm.total_out is %lld\n", zstrm.total_out); + *dst_len = zstrm.total_out; + + (void)wd_inflate_end(&zstrm); + if (ret != Z_STREAM_END) { + ret = Z_ERRNO; + } else { + ret = Z_OK; + } + return ret; +} + +/* report a zlib or i/o error */ +static void zerr(int ret) +{ +} + +static int zlibwrapper_data_process(struct create_comp_data *data) +{ + z_stream zstrm = {0}, *zstrmp; + struct timeval begin_tval; + struct timeval end_tval; + unsigned long Perf = 0; + size_t output_size = 0; + size_t input_size = 0; + int cnt = data->count; + double time_used = 0; + char *output = NULL; + char *input = NULL; + double speed = 0; + double ops = 0; + int ret = AGF; + + input_size = data->input_size; + ret = v2_key_from_random(2, &input, input_size); + if (ret) { + LOG(ERR, "Error: generate_key_from_random failed!\n"); + ret = RSF; + goto err; + } + + if (data->op == WD_DIR_COMPRESS) { + output_size = (input_size * COMPRESS_TIMES) + RESERVE_MM; + } else { + output_size = (input_size * DECOMPRESS_TIMES) + RESERVE_MM; + } + output = malloc(output_size); + if (output == NULL) { + LOG(ERR, "Error: output malloc failed!\n"); + ret = RSF; + goto err; + } + memset(output, 0, output_size); + data->input_size = input_size; + data->output_size = output_size; + gettimeofday(&begin_tval, NULL); + + if (data->use_env) { + zstrmp = &zstrm; + if (data->threadid == 0) { + data->win_bs = ZLIB_MAX_WBITS; + } else if (data->threadid == 1) { + data->win_bs = GZIP_MAX_WBITS; + } else { + data->win_bs = DEFLATE_MAX_WBITS; + } + } else { + zstrmp = NULL; + } + if (data->loop > 0) { + LOG(NTC, "Notice: Start comp loop test.\n"); + while (1) { + if (data->op == WD_DIR_COMPRESS) { + ret = test_uadk_zlib_deflate(zstrmp, input, input_size, output, + &output_size, data->comp_lv, data->win_bs); + } else { + ret = test_uadk_zlib_inflate(zstrmp, input, input_size, output, &output_size, data->win_bs); + } + if (ret != Z_OK) { + zerr(ret); + return ret; + } + cnt++; + if (data->loop > 0) { + gettimeofday(&end_tval, NULL); + time_used = (double)((end_tval.tv_sec - begin_tval.tv_sec) * S_TO_US_TIME + + end_tval.tv_usec - begin_tval.tv_usec); + if (time_used >= data->loop * S_TO_US_TIME) + break; + } + } + } + while (data->count--) { + if (data->op == WD_DIR_COMPRESS) { + ret = test_uadk_zlib_deflate(zstrmp, input, input_size, output, &output_size, data->comp_lv, data->win_bs); + } else { + ret = test_uadk_zlib_inflate(zstrmp, input, input_size, output, &output_size, data->win_bs); + } + if (ret != Z_OK) { + zerr(ret); + return ret; + } + } +err: + data->result = ret; + if (input) { + free(input); + } + if (output) { + free(output); + } + return ret; +} + +static int zlibwrapper_setup(struct create_comp_data *data) +{ + int thread_num = data->thread_num; + unsigned long total_perf = 0; + int i = 0, start_thread = 0; + int debug = data->debug; + double total_ops = 0; + int ret = RSF; + + if (0 == thread_num) { + data->threadid = -1; + data->sync = true; + data->result = 0; + ret = zlibwrapper_data_process(data); + if (0 != ret) { + LOG(ERR, "Error: single process comp compute test failed, ret = %d \n", ret); + return ret; + } + total_perf = data->perf; + total_ops = data->ops; + } else { + LOG(INF, "Info: do multiple threads comp compute test. \n"); + struct create_comp_data thread_data[thread_num]; + pthread_t thread_process[thread_num]; + + for (i = start_thread; i < thread_num; i++) { + memcpy(&thread_data[i], data, sizeof(struct create_comp_data)); + thread_data[i].threadid = i; + thread_data[i].result = 0; + thread_data[i].debug = debug; + ret = pthread_create(&thread_process[i], NULL, (void *)zlibwrapper_data_process, (void *)&thread_data[i]); + if (ret) { + LOG(ERR, "Error: create data process thread failed, ret = %d \n", ret); + ret = THF; + return ret; + } + } + for (i = 0; i < thread_num; i++) { + ret = pthread_join(thread_process[i], NULL); + if (ret) { + LOG(ERR, "Error: join thread[%d] failed, return ret => %d \n", i, ret); + ret = THF; + return ret; + } + } + for (i = start_thread; i < thread_num; i++) { + total_perf += thread_data[i].perf; + total_ops += thread_data[i].ops; + if (0 != thread_data[i].result) { + LOG(ERR, "Error: thread[%d] run failed, return ret => %d \n", i, thread_data[i].result); + ret = THF; + return ret; + } + } + } + PRINTF_PERF(data->alg, thread_num, data->loop, total_perf, total_ops); + + return ret; +} + +int wd_zlibwrapper_test(void *param) +{ + struct acc_v2_common_param *g_acc_param = (struct acc_v2_common_param *)param; + char *infile = g_acc_param->comm_param.infile; + char a[10], b[10], c[10], d[10]; + char fileName[FILE_NAME_LEN]; + struct create_comp_data data; + size_t input_size = 0; + char *src = NULL; + int ret = AGF; + + memset(&data, 0, sizeof(struct create_comp_data)); + + data.comp_lv = WD_COMP_L8; + if (sscanf(g_acc_param->comm_param.alg_mode, "%[^-]-%[^-]-%[^-]-%s", a, b, c, d) == 4) + data.comp_lv = atoi(d); + else if (sscanf(g_acc_param->comm_param.alg_mode, "%[^-]-%[^-]-%s", a, b, c) != 3) { + LOG(ERR, "Error: comp alg_mode invalid \n"); + return AGF; + } + + if (!strcmp(a, "compress")) { + data.op = WD_DIR_COMPRESS; + } else if (!strcmp(a, "decompress")) { + data.op = WD_DIR_DECOMPRESS; + } else { + LOG(ERR, "Error: comp op is compress or decompress ? \n"); + return AGF; + } + if (!strcmp(b, "stream")) + data.cmode = 0; + else if (!strcmp(b, "block")) + LOG(ERR, "Error: zlibwrapper not support block\n"); + else { + LOG(ERR, "Error: comp mode is stream or block ? \n"); + return AGF; + } + + if (data.cmode == 0 && g_acc_param->comm_param.async) { + LOG(ERR, "Error: stream mode is not support async\n"); + return AGF; + } + if (strcmp(c, "4K") == 0) + data.win_sz = WD_COMP_WS_4K; + else if (strcmp(c, "8K") == 0) + data.win_sz = WD_COMP_WS_8K; + else if (strcmp(c, "16K") == 0) + data.win_sz = WD_COMP_WS_16K; + else if (strcmp(c, "24K") == 0) + data.win_sz = WD_COMP_WS_24K; + else if (strcmp(c, "32K") == 0) + data.win_sz = WD_COMP_WS_32K; + else { + LOG(ERR, "Error: windows size?\n"); + return AGF; + } + if (g_acc_param->comm_param.src_mode == SRC_RANDOM) { + if (sscanf(g_acc_param->comm_param.alg_key_size, "%ld", &input_size) != 1) { + LOG(ERR, "Error: comp size format invalid \n"); + return AGF; + } + } else { + LOG(ERR, "Error: comp not support do-openssl\n"); + return AGF; + } + data.input_size = input_size; + LOG (INF, "Info: input_size: %ld\n", data.input_size); + + { + data.alg = g_acc_param->comm_param.alg_name; + data.sync = !g_acc_param->comm_param.async; + data.loop = g_acc_param->comm_param.loop; + data.src_type = g_acc_param->comm_param.src_mode; + data.mem_type = g_acc_param->mem_mode; + data.use_env = g_acc_param->use_env; + data.numa_id = g_acc_param->numa_id; + data.count = g_acc_param->comm_param.count; + data.infile = g_acc_param->comm_param.infile; + data.outfile = g_acc_param->comm_param.outfile; + data.thread_num = g_acc_param->comm_param.thread_num; + data.perf_flag = g_acc_param->comm_param.perf_flag; + data.win_bs = g_acc_param->windowbits; + data.debug = log_lvl; + } + + if (data.use_env) { + data.thread_num = 3; + } + + ret = zlibwrapper_setup(&data); + + return ret; +} diff --git a/test/zlibwrapper.h b/test/zlibwrapper.h new file mode 100644 index 0000000..33395b1 --- /dev/null +++ b/test/zlibwrapper.h @@ -0,0 +1,15 @@ +#ifndef KP_TOOLS_ZLIBWRAPPER_H +#define KP_TOOLS_ZLIBWRAPPER_H + +#include "wd_zlibwrapper.h" + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ +#define FILE_NAME_LEN 500 +#define TOOLS_NAME_LEN 50 +#define ZLIB_MAX_WBITS 15 +#define GZIP_MAX_WBITS 31 +#define DEFLATE_MAX_WBITS -15 + +int wd_zlibwrapper_test(void *param); + +#endif