1. Support test fork.
2. Support test wd_comp_init2.
3. Support test zlibwrapper.
Signed-off-by: Yang Shen <shenyang39(a)huawei.com>
---
sample/uadk_comp.c | 364 +++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 353 insertions(+), 11 deletions(-)
diff --git a/sample/uadk_comp.c b/sample/uadk_comp.c
index 908c7bcb..1d259f0b 100644
--- a/sample/uadk_comp.c
+++ b/sample/uadk_comp.c
@@ -7,6 +7,7 @@
#include "wd_alg_common.h"
#include "wd_comp.h"
#include "wd_sched.h"
+#include "wd_zlibwrapper.h"
#define SCHED_RR_NAME "sched_rr"
@@ -19,6 +20,8 @@
#define required_argument 1
#define optional_argument 2
+#define MAX_THREAD 1024
+
struct request_config {
char algname[MAX_ALG_LEN];
enum wd_comp_alg_type alg;
@@ -42,6 +45,11 @@ struct acc_alg_item {
int alg;
};
+struct comp_sample_data {
+ int size;
+ char data[128];
+};
+
static struct request_config config = {
.complv = WD_COMP_L8,
.optype = WD_DIR_COMPRESS,
@@ -52,6 +60,8 @@ static struct request_config config = {
static struct request_data data;
+static pthread_t threads[MAX_THREAD];
+
static struct acc_alg_item alg_options[] = {
{"zlib", WD_ZLIB},
{"gzip", WD_GZIP},
@@ -60,6 +70,11 @@ static struct acc_alg_item alg_options[] = {
{"", WD_COMP_ALG_MAX}
};
+static struct comp_sample_data sample_data = {
+ .size = 20,
+ .data = "Welcome to use uadk!",
+};
+
static void cowfail(char *s)
{
fprintf(stderr, ""
@@ -74,6 +89,313 @@ static void cowfail(char *s)
"\n", s);
}
+static void *initthread(void *data)
+{
+ int ret;
+
+ ret = wd_comp_init2("zlib", 0, TASK_HW);
+ if (ret)
+ fprintf(stderr, "%s: something is wrong, ret = %d!", __func__, ret);
+
+ return NULL;
+}
+
+static int test_fork(void)
+{
+ int ret;
+
+ pthread_create(&threads[0], NULL, initthread, NULL);
+
+ sleep(2);
+ ret = fork();
+ if (ret == 0)
+ ret = wd_comp_init2("zlib", 0, TASK_HW);
+ else
+ ret = pthread_join(threads[0], NULL);
+
+ wd_comp_uninit2();
+
+ return ret;
+}
+
+static int test_uadk_init2(void)
+{
+ struct wd_comp_sess_setup setup[2] = {0};
+ struct sched_params param[2] = {0};
+ struct wd_comp_req req[2] = {0};
+ handle_t h_sess[2];
+ void *src, *dst;
+ int ret;
+
+ ret = wd_comp_init2("zlib", 0, TASK_HW);
+ if (ret)
+ return ret;
+
+ setup[0].alg_type = WD_ZLIB;
+ setup[0].op_type = WD_DIR_COMPRESS;
+ setup[0].comp_lv = 1;
+ setup[0].win_sz = 1;
+ param[0].type = WD_DIR_COMPRESS;
+ setup[0].sched_param = ¶m[0];
+
+ h_sess[0] = wd_comp_alloc_sess(&setup[0]);
+ if (!h_sess[0]) {
+ fprintf(stderr, "%s fail to alloc comp sess.\n", __func__);
+ ret = -WD_EINVAL;
+ goto out_uninit;
+ }
+
+ setup[1].alg_type = WD_ZLIB;
+ setup[1].op_type = WD_DIR_DECOMPRESS;
+ setup[1].comp_lv = 1;
+ setup[1].win_sz = 1;
+ param[1].type = WD_DIR_DECOMPRESS;
+ setup[1].sched_param = ¶m[1];
+ h_sess[1] = wd_comp_alloc_sess(&setup[1]);
+ if (!h_sess[1]) {
+ fprintf(stderr, "%s fail to alloc decomp sess.\n", __func__);
+ ret = -WD_EINVAL;
+ goto out_free_comp_sess;
+ }
+
+ src = calloc(1, sizeof(char) * 128);
+ if (!src) {
+ ret = -WD_ENOMEM;
+ goto out_free_decomp_sess;
+ }
+
+ dst = calloc(1, sizeof(char) * 128);
+ if (!dst) {
+ ret = -WD_ENOMEM;
+ goto out_free_src;
+ }
+
+ req[0].src = sample_data.data;
+ req[0].src_len = sample_data.size;
+ req[0].op_type = WD_DIR_COMPRESS;
+ req[0].dst = dst;
+ req[0].dst_len = 128;
+
+ ret = wd_do_comp_sync(h_sess[0], &req[0]);
+ if (ret)
+ goto out_free_dst;
+
+ req[1].src = dst;
+ req[1].src_len = req[0].dst_len;
+ req[1].op_type = WD_DIR_DECOMPRESS;
+ req[1].dst = src;
+ req[1].dst_len = 128;
+
+ ret = wd_do_comp_sync(h_sess[1], &req[1]);
+
+ ret = strcmp(sample_data.data, src);
+ if (ret)
+ fprintf(stderr, "decompress fail\n");
+ else
+ fprintf(stderr, "good\n");
+
+out_free_dst:
+ free(dst);
+out_free_src:
+ free(src);
+out_free_decomp_sess:
+ wd_comp_free_sess(h_sess[1]);
+out_free_comp_sess:
+ wd_comp_free_sess(h_sess[0]);
+out_uninit:
+ wd_comp_uninit2();
+ return ret;
+}
+
+static int test_uadk_zlib_deflate(void *src, int src_len, void *dest, int dst_len)
+{
+ __u32 chunk = 128 * 1024;
+ z_stream zstrm = {0};
+ int ret, flush, have;
+
+ ret = wd_comp_init2("zlib", 0, TASK_HW);
+ if (ret) {
+ fprintf(stderr, "%s fail to init wd_comp.\n", __func__);
+ return ret;
+ }
+
+ ret = wd_deflate_init(&zstrm, 0, 15);
+ if (ret) {
+ fprintf(stderr, "%s fail to init deflate.\n", __func__);
+ 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);
+ have = chunk - zstrm.avail_out;
+ dest += have;
+ } while (zstrm.avail_in > 0);
+
+ /* done when last data in file processed */
+ } while (flush != Z_FINISH);
+
+ ret = ret == Z_STREAM_END ? zstrm.total_out : ret;
+
+ (void)wd_deflate_end(&zstrm);
+
+ return ret;
+}
+
+static int test_uadk_zlib_inflate(void *src, int src_len, void *dest, int dst_len)
+{
+ __u32 chunk = 128 * 1024;
+ // __u32 chunk = 1024 * 1024 * 2;
+ z_stream zstrm = {0};
+ int ret, have;
+
+ ret = wd_inflate_init(&zstrm, 15);
+ if (ret) {
+ fprintf(stderr, "%s fail to init inflate.\n", __func__);
+ 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);
+ have = chunk - zstrm.avail_out;
+ dest += have;
+ } while (zstrm.avail_in > 0);
+
+ /* done when last data in file processed */
+ } while (ret != Z_STREAM_END);
+
+ ret = ret == Z_STREAM_END ? zstrm.total_out : ret;
+
+ (void)wd_inflate_end(&zstrm);
+
+ return ret;
+}
+
+static int test_uadk_zlib(void)
+{
+ void *src, *dst, *src2;
+ FILE *source = stdin;
+ FILE *dest = stdout;
+ int ret, fd, sz;
+ struct stat s;
+
+ fd = fileno(source);
+ ret = fstat(fd, &s);
+ if (ret < 0) {
+ fprintf(stderr, "%s fstat error!\n", __func__);
+ return ret;
+ }
+
+ src = calloc(1, sizeof(char) * s.st_size);
+ if (!src) {
+ fprintf(stderr, "%s calloc error!\n", __func__);
+ return -WD_ENOMEM;
+ }
+
+ src2 = calloc(1, sizeof(char) * s.st_size * 2);
+ if (!src2) {
+ fprintf(stderr, "%s calloc2 error!\n", __func__);
+ ret = -WD_ENOMEM;
+ goto free_src;
+ }
+
+ dst = calloc(1, sizeof(char) * s.st_size * 2);
+ if (!dst) {
+ fprintf(stderr, "%s calloc error!\n", __func__);
+ ret = -WD_ENOMEM;
+ goto free_src2;
+ }
+
+ sz = fread(src, 1, s.st_size, source);
+ if (sz != s.st_size) {
+ fprintf(stderr, "%s read file sz != file.size!\n", __func__);
+ ret = -WD_EINVAL;
+ goto free_dst;
+ }
+
+ ret = test_uadk_zlib_deflate(src, sz, dst, sz * 2);
+ if (ret < 0) {
+ fprintf(stderr, "%s do deflate fail ret %d\n", __func__, ret);
+ goto free_dst;
+ }
+
+ ret = fwrite(dst, 1, ret, dest);
+ if (ret < 0)
+ fprintf(stderr, "%s file write fail ret %d\n", __func__, ret);
+
+ ret = test_uadk_zlib_inflate(dst, ret, src2, sz * 2);
+ if (ret < 0) {
+ fprintf(stderr, "%s do inflate fail ret %d\n", __func__, ret);
+ goto free_dst;
+ }
+
+ ret = memcmp(src, src2, sz);
+ if (!ret)
+ fprintf(stderr, "%s good!\n", __func__);
+
+free_dst:
+ free(dst);
+free_src2:
+ free(src2);
+free_src:
+ free(src);
+ return ret;
+}
+
+static int test_func(int test_mode)
+{
+ int ret;
+
+ switch (test_mode) {
+ case 0:
+ ret = test_fork();
+ break;
+ case 1:
+ ret = test_uadk_init2();
+ break;
+ case 2:
+ ret = test_uadk_zlib();
+ break;
+ default:
+ ret = -WD_EINVAL;
+ break;
+ }
+
+ return ret;
+}
+
static struct uacce_dev_list* get_dev_list(char *alg_name)
{
struct uacce_dev_list *list, *p, *head = NULL, *prev = NULL;
@@ -147,7 +469,7 @@ static struct wd_sched *uadk_comp_sched_init(void)
sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 2, 2, lib_poll_func);
if (!sched) {
- printf("%s fail to alloc sched.\n", __func__);
+ fprintf(stderr, "%s fail to alloc sched.\n", __func__);
return NULL;
}
sched->name = SCHED_RR_NAME;
@@ -388,31 +710,31 @@ static int operation(FILE *source, FILE *dest)
ret = uadk_comp_ctx_init();
if (ret) {
- fprintf(stderr, "%s fail to init ctx!\n", __func__);
+ fprintf(stderr, "%s fail to init ctx! %d\n", __func__, ret);
return ret;
}
ret = uadk_comp_sess_init();
if (ret) {
- fprintf(stderr, "%s fail to init sess!\n", __func__);
+ fprintf(stderr, "%s fail to init sess! %d\n", __func__, ret);
goto out_ctx_uninit;
}
ret = uadk_comp_request_init(source);
if (ret) {
- fprintf(stderr, "%s fail to init request!\n", __func__);
+ fprintf(stderr, "%s fail to init request! %d\n", __func__, ret);
goto out_sess_uninit;
}
ret = uadk_do_comp();
if (ret) {
- fprintf(stderr, "%s fail to do request!\n", __func__);
+ fprintf(stderr, "%s fail to do request! %d\n", __func__, ret);
goto out_sess_uninit;
}
ret = uadk_comp_write_file(dest);
if (ret)
- fprintf(stderr, "%s fail to write result!\n", __func__);
+ fprintf(stderr, "%s fail to write result! %d\n", __func__, ret);
uadk_comp_request_uninit();
@@ -445,9 +767,10 @@ static void print_help(void)
int main(int argc, char *argv[])
{
+ int ret, c, test_mode;
int option_index = 0;
int help = 0;
- int ret, c;
+ int test = 0;
static struct option long_options[] = {
{"help", no_argument, 0, 0},
@@ -455,6 +778,9 @@ int main(int argc, char *argv[])
{"complv", required_argument, 0, 2},
{"optype", required_argument, 0, 3},
{"winsize", required_argument, 0, 4},
+ {"fork", no_argument, 0, 5},
+ {"init2", no_argument, 0, 6},
+ {"zlib", no_argument, 0, 7},
{0, 0, 0, 0}
};
@@ -470,8 +796,8 @@ int main(int argc, char *argv[])
case 1:
config.list = get_dev_list(optarg);
if (!config.list) {
- cowfail("Can't find your algorithm!\n");
help = 1;
+ cowfail("Can't find your algorithm!\n");
} else {
strcpy(config.algname, optarg);
}
@@ -485,9 +811,21 @@ int main(int argc, char *argv[])
case 4:
config.winsize = strtol(optarg, NULL, 0);
break;
+ case 5:
+ test = 1;
+ test_mode = 0;
+ break;
+ case 6:
+ test = 1;
+ test_mode = 1;
+ break;
+ case 7:
+ test = 1;
+ test_mode = 2;
+ break;
default:
help = 1;
- cowfail("bad input test parameter!\n");
+ cowfail("Bad input test parameter!\n");
break;
}
}
@@ -497,9 +835,13 @@ int main(int argc, char *argv[])
exit(-1);
}
- ret = operation(stdin, stdout);
+ if (test == 1)
+ ret = test_func(test_mode);
+ else
+ ret = operation(stdin, stdout);
+
if (ret)
- cowfail("So sad for we do something wrong!\n");
+ cowfail("So sad for someting wrong!\n");
return ret;
}
--
2.33.0