Repository navigation
Expand file tree
/
Copy pathwd_join_gather.c
More file actions
1902 lines (1575 loc) · 49.4 KB
/
Copy pathwd_join_gather.c
File metadata and controls
1902 lines (1575 loc) · 49.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright 2025 Huawei Technologies Co.,Ltd. All rights reserved.
*/
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <limits.h>
#include "include/drv/wd_join_gather_drv.h"
#include "wd_join_gather.h"
#define DECIMAL_PRECISION_OFFSET 8
#define DAE_INT_SIZE 4
#define DAE_LONG_SIZE 8
#define DAE_LONG_DECIMAL_SIZE 16
/* Sum of the max row number of standard and external hash table */
#define MAX_HASH_TABLE_ROW_NUM 0x1FFFFFFFE
enum wd_join_sess_state {
WD_JOIN_SESS_UNINIT, /* Uninit session */
WD_JOIN_SESS_INIT, /* Hash table has been set */
WD_JOIN_SESS_BUILD_HASH, /* Input stage has started */
WD_JOIN_SESS_PREPARE_REHASH, /* New hash table has been set */
WD_JOIN_SESS_REHASH, /* Rehash stage has started */
WD_JOIN_SESS_PROBE, /* Output stage has started */
};
struct wd_join_gather_setting {
enum wd_status status;
struct wd_ctx_config_internal config;
struct wd_sched sched;
struct wd_async_msg_pool pool;
void *dlhandle;
void *dlh_list;
};
struct wd_join_cols_conf {
struct wd_join_gather_col_info *cols;
__u64 *data_size;
__u32 cols_num;
bool key_output_enable;
};
struct wd_gather_tables_conf {
struct wd_gather_table_info *tables;
__u32 *batch_row_size;
__u64 **data_size;
__u32 table_num;
};
struct wd_join_gather_sess {
enum multi_batch_index_type index_type;
enum wd_join_sess_state state;
enum wd_join_gather_alg alg;
struct wd_join_cols_conf join_conf;
struct wd_gather_tables_conf gather_conf;
struct wd_dae_hash_table hash_table;
wd_dev_mask_t *dev_mask;
void *sched_key;
void *priv;
};
static const char *wd_join_gather_alg[WD_JOIN_GATHER_ALG_MAX] = {
"hashjoin", "gather", "join-gather"
};
static struct wd_init_attrs wd_join_gather_init_attrs;
static struct wd_join_gather_setting wd_join_gather_setting;
static int wd_join_gather_poll_ctx(__u32 idx, __u32 expt, __u32 *count);
static void wd_join_gather_close_driver(void)
{
#ifndef WD_STATIC_DRV
wd_dlclose_drv(wd_join_gather_setting.dlh_list);
wd_join_gather_setting.dlh_list = NULL;
#else
hisi_dae_join_gather_remove();
#endif
}
static int wd_join_gather_open_driver(void)
{
#ifndef WD_STATIC_DRV
/*
* Driver lib file path could set by env param.
* then open tham by wd_dlopen_drv()
* use NULL means dynamic query path
*/
wd_join_gather_setting.dlh_list = wd_dlopen_drv(NULL);
if (!wd_join_gather_setting.dlh_list) {
WD_ERR("fail to open driver lib files.\n");
return -WD_EINVAL;
}
#else
hisi_dae_join_gather_probe();
#endif
return WD_SUCCESS;
}
static bool wd_join_gather_check_inner(void)
{
struct uacce_dev_list *list;
list = wd_get_accel_list("hashjoin");
if (!list)
goto out;
wd_free_list_accels(list);
list = wd_get_accel_list("gather");
if (!list)
goto out;
wd_free_list_accels(list);
return true;
out:
WD_ERR("invalid: the device cannot support hashjoin and gather!\n");
return false;
}
static bool wd_join_gather_alg_check(const char *alg_name)
{
__u32 i;
/* Check for the virtual algorithms */
if (!strcmp(alg_name, "join-gather"))
return wd_join_gather_check_inner();
for (i = 0; i < WD_JOIN_GATHER_ALG_MAX; i++) {
/* Some algorithms do not support all modes */
if (!wd_join_gather_alg[i] || !strlen(wd_join_gather_alg[i]))
continue;
if (!strcmp(alg_name, wd_join_gather_alg[i]))
return true;
}
return false;
}
static int check_col_data_info(enum wd_dae_data_type type, __u16 col_data_info)
{
__u8 all_precision, decimal_precision;
switch (type) {
case WD_DAE_DATE:
case WD_DAE_INT:
case WD_DAE_LONG:
case WD_DAE_VARCHAR:
break;
case WD_DAE_SHORT_DECIMAL:
case WD_DAE_LONG_DECIMAL:
/* High 8 bit: decimal part precision, low 8 bit: the whole data precision */
all_precision = col_data_info;
decimal_precision = col_data_info >> DECIMAL_PRECISION_OFFSET;
if (!all_precision || decimal_precision > all_precision) {
WD_ERR("failed to check data precision, all: %u, decimal: %u!\n",
all_precision, decimal_precision);
return -WD_EINVAL;
}
break;
case WD_DAE_CHAR:
if (!col_data_info) {
WD_ERR("invalid: char length is zero!\n");
return -WD_EINVAL;
}
break;
default:
WD_ERR("invalid: data type %u is not supported!\n", type);
return -WD_EINVAL;
}
return WD_SUCCESS;
}
static int get_data_type_size(enum wd_dae_data_type type, __u16 col_data_info,
__u64 *col, __u32 idx)
{
switch (type) {
case WD_DAE_DATE:
case WD_DAE_INT:
col[idx] = DAE_INT_SIZE;
break;
case WD_DAE_LONG:
case WD_DAE_SHORT_DECIMAL:
col[idx] = DAE_LONG_SIZE;
break;
case WD_DAE_LONG_DECIMAL:
col[idx] = DAE_LONG_DECIMAL_SIZE;
break;
case WD_DAE_CHAR:
col[idx] = col_data_info;
break;
case WD_DAE_VARCHAR:
col[idx] = 0;
break;
default:
return -WD_EINVAL;
}
return WD_SUCCESS;
}
static int check_key_cols_info(struct wd_join_gather_sess_setup *setup)
{
struct wd_join_table_info *table = &setup->join_table;
struct wd_join_gather_col_info *build = table->build_key_cols;
__u32 i;
int ret;
if (table->build_key_cols_num != table->probe_key_cols_num) {
WD_ERR("invalid: build key_cols_num: %u, probe key_cols_num: %u!\n",
table->build_key_cols_num, table->probe_key_cols_num);
return -WD_EINVAL;
}
ret = memcmp_consttime(table->build_key_cols, table->probe_key_cols,
table->build_key_cols_num * sizeof(struct wd_join_gather_col_info));
if (ret) {
WD_ERR("invalid: build and probe table key infomation is not same!\n");
return -WD_EINVAL;
}
for (i = 0; i < table->build_key_cols_num; i++) {
if (!build[i].has_empty) {
WD_ERR("invalid: key col has no empty data! col: %u\n", i);
return -WD_EINVAL;
}
ret = check_col_data_info(build[i].data_type, build[i].data_info);
if (ret) {
WD_ERR("failed to check key col data info! col: %u\n", i);
return ret;
}
}
return WD_SUCCESS;
}
static int wd_join_check_params(struct wd_join_gather_sess_setup *setup)
{
struct wd_join_table_info *table = &setup->join_table;
if (!table->build_key_cols_num || !table->build_key_cols) {
WD_ERR("invalid: build key cols is NULL or key_cols_num is 0!\n");
return -WD_EINVAL;
}
if (!table->probe_key_cols_num || !table->probe_key_cols) {
WD_ERR("invalid: probe key cols is NULL or key_cols_num is 0!\n");
return -WD_EINVAL;
}
if (setup->index_type >= WD_BATCH_INDEX_TYPE_MAX) {
WD_ERR("failed to check batch index type!\n");
return -WD_EINVAL;
}
if (check_key_cols_info(setup)) {
WD_ERR("failed to check join setup key cols info!\n");
return -WD_EINVAL;
}
return WD_SUCCESS;
}
static int wd_gather_check_params(struct wd_join_gather_sess_setup *setup)
{
struct wd_gather_table_info *table = setup->gather_tables;
struct wd_join_gather_col_info *col;
__u32 i, j;
int ret;
if (!setup->gather_tables || !setup->gather_table_num) {
WD_ERR("invalid: gather table is NULL, table num: %u\n", setup->gather_table_num);
return -WD_EINVAL;
}
if (setup->index_type >= WD_BATCH_INDEX_TYPE_MAX) {
WD_ERR("failed to check gather batch index type!\n");
return -WD_EINVAL;
}
for (i = 0; i < setup->gather_table_num; i++) {
if (!table[i].cols || !table[i].cols_num) {
WD_ERR("failed to check gather table cols, num: %u\n", table[i].cols_num);
return -WD_EINVAL;
}
col = table[i].cols;
for (j = 0; j < table[i].cols_num; j++) {
ret = check_col_data_info(col[j].data_type, col[j].data_info);
if (ret) {
WD_ERR("failed to check gather info! col: %u, table: %u\n", j, i);
return ret;
}
}
}
return WD_SUCCESS;
}
static int wd_join_gather_check_params(struct wd_join_gather_sess_setup *setup)
{
int ret;
if (!setup) {
WD_ERR("invalid: hashjoin or gather sess setup is NULL!\n");
return -WD_EINVAL;
}
switch (setup->alg) {
case WD_JOIN:
return wd_join_check_params(setup);
case WD_GATHER:
return wd_gather_check_params(setup);
case WD_JOIN_GATHER:
ret = wd_join_check_params(setup);
if (ret)
return ret;
return wd_gather_check_params(setup);
default:
WD_ERR("invalid: hashjoin sess setup alg is wrong!\n");
return -WD_EINVAL;
}
}
static void sess_data_size_uninit(struct wd_join_gather_sess *sess)
{
__u32 i;
if (sess->join_conf.cols)
free(sess->join_conf.cols);
if (sess->gather_conf.tables) {
for (i = 0; i < sess->gather_conf.table_num; i++)
free(sess->gather_conf.data_size[i]);
free(sess->gather_conf.tables);
}
}
static int sess_data_size_init(struct wd_join_gather_sess *sess,
struct wd_join_gather_sess_setup *setup)
{
struct wd_gather_table_info *gtable = setup->gather_tables;
struct wd_join_table_info *jtable = &setup->join_table;
struct wd_join_gather_col_info *key = jtable->build_key_cols;
__u64 key_size, key_data_size, gather_size, gather_data_size;
__u32 i, j;
__atomic_store_n(&sess->state, WD_JOIN_SESS_UNINIT, __ATOMIC_RELEASE);
if (setup->alg != WD_GATHER) {
key_size = jtable->build_key_cols_num * sizeof(struct wd_join_gather_col_info);
key_data_size = jtable->build_key_cols_num * sizeof(__u64);
sess->join_conf.cols = malloc(key_size + key_data_size);
if (!sess->join_conf.cols)
return -WD_ENOMEM;
memcpy(sess->join_conf.cols, key, key_size);
sess->join_conf.data_size = (void *)sess->join_conf.cols + key_size;
for (i = 0; i < jtable->build_key_cols_num; i++)
(void)get_data_type_size(key[i].data_type, key[i].data_info,
sess->join_conf.data_size, i);
sess->join_conf.cols_num = jtable->build_key_cols_num;
if (setup->alg == WD_JOIN)
return WD_SUCCESS;
}
gather_size = setup->gather_table_num * sizeof(struct wd_gather_table_info);
gather_data_size = setup->gather_table_num * sizeof(__u64 *);
sess->gather_conf.tables = malloc(gather_size + gather_data_size);
if (!sess->gather_conf.tables)
goto free_join;
memcpy(sess->gather_conf.tables, gtable, gather_size);
sess->gather_conf.data_size = (void *)sess->gather_conf.tables + gather_size;
for (i = 0; i < setup->gather_table_num; i++) {
sess->gather_conf.data_size[i] = malloc(gtable[i].cols_num * sizeof(__u64));
if (!sess->gather_conf.data_size[i])
goto free_gather;
}
for (i = 0; i < setup->gather_table_num; i++)
for (j = 0; j < gtable[i].cols_num; j++)
(void)get_data_type_size(gtable[i].cols[j].data_type,
gtable[i].cols[j].data_info,
sess->gather_conf.data_size[i], j);
sess->gather_conf.table_num = setup->gather_table_num;
return WD_SUCCESS;
free_gather:
for (j = 0; j < i; j++)
free(sess->gather_conf.data_size[j]);
free(sess->gather_conf.tables);
free_join:
if (setup->alg != WD_GATHER)
free(sess->join_conf.cols);
return -WD_ENOMEM;
}
static void cleanup_partial_init(struct wd_join_gather_sess *sess)
{
struct wd_ctx_config_internal *config = &wd_join_gather_setting.config;
struct wd_alg_driver *drv;
struct wd_join_gather_ops *eops;
if (!sess->priv)
return;
drv = config->ctxs[0].drv;
if (drv->extend_ops) {
eops = drv->extend_ops;
if (eops->sess_uninit)
eops->sess_uninit(sess->priv);
}
}
static void wd_join_gather_uninit_sess(struct wd_join_gather_sess *sess)
{
if (!sess)
return;
cleanup_partial_init(sess);
free(sess->gather_conf.batch_row_size);
sess->gather_conf.batch_row_size = NULL;
}
static int handle_algorithm_specific_init(struct wd_join_gather_ops *eops,
struct wd_join_gather_sess *sess,
struct wd_join_gather_sess_setup *setup)
{
__u32 array_size;
int ret;
if (!sess->priv)
return WD_SUCCESS;
if (eops->get_table_row_size && setup->alg != WD_GATHER) {
ret = eops->get_table_row_size(sess->priv);
if (ret <= 0) {
WD_ERR("failed to get hash table row size: %d!\n", ret);
return -WD_EINVAL;
}
sess->hash_table.table_row_size = ret;
}
if (eops->get_batch_row_size && setup->alg != WD_JOIN) {
array_size = setup->gather_table_num * sizeof(__u32);
if (!sess->gather_conf.batch_row_size) {
sess->gather_conf.batch_row_size = malloc(array_size);
if (!sess->gather_conf.batch_row_size)
return -WD_ENOMEM;
ret = eops->get_batch_row_size(sess->priv,
sess->gather_conf.batch_row_size,
array_size);
if (ret) {
free(sess->gather_conf.batch_row_size);
sess->gather_conf.batch_row_size = NULL;
WD_ERR("failed to get batch table row size!\n");
return -WD_EINVAL;
}
}
}
return WD_SUCCESS;
}
static int init_single_driver_session(struct wd_alg_driver *drv,
struct wd_join_gather_sess *sess,
struct wd_join_gather_sess_setup *setup)
{
struct wd_join_gather_ops *eops;
int ret;
if (!drv->get_extend_ops || !drv->extend_ops)
return WD_SUCCESS;
ret = drv->get_extend_ops(drv->extend_ops);
if (ret) {
WD_ERR("failed to set session extend ops!\n");
return -WD_EINVAL;
}
eops = drv->extend_ops;
if (eops->sess_init) {
if (!eops->sess_uninit) {
WD_ERR("failed to get session uninit ops!\n");
return -WD_EINVAL;
}
ret = eops->sess_init(setup, &sess->priv);
if (ret) {
WD_ERR("failed to init session priv!\n");
return ret;
}
}
ret = handle_algorithm_specific_init(eops, sess, setup);
if (ret != WD_SUCCESS) {
if (eops->sess_uninit)
eops->sess_uninit(sess->priv);
return ret;
}
return WD_SUCCESS;
}
static int wd_join_gather_init_sess(struct wd_join_gather_sess *sess,
struct wd_join_gather_sess_setup *setup)
{
struct wd_ctx_config_internal *config = &wd_join_gather_setting.config;
struct wd_alg_driver *first_drv = NULL;
__u32 i;
/*
* The algorithm always works in stream mode, so it supports only
* the switchover between ctxs of the same driver type.
*/
for (i = 0; i < config->ctx_num; i++) {
if (!first_drv) {
first_drv = config->ctxs[i].drv;
} else if (config->ctxs[i].drv != first_drv) {
WD_ERR("incompatible driver: %s vs %s, only single driver type allowed!\n",
config->ctxs[i].drv->drv_name, first_drv->drv_name);
return -WD_EINVAL;
}
}
return init_single_driver_session(first_drv, sess, setup);
}
handle_t wd_join_gather_alloc_sess(struct wd_join_gather_sess_setup *setup)
{
struct wd_sched_params params = {0};
struct wd_join_gather_sess *sess;
int ret;
ret = wd_join_gather_check_params(setup);
if (ret)
return (handle_t)0;
sess = malloc(sizeof(struct wd_join_gather_sess));
if (!sess) {
WD_ERR("failed to alloc join gather session memory!\n");
return (handle_t)0;
}
memset(sess, 0, sizeof(struct wd_join_gather_sess));
sess->alg = setup->alg;
sess->index_type = setup->index_type;
sess->join_conf.key_output_enable = setup->join_table.key_output_enable;
ret = wd_drv_alg_support(wd_join_gather_alg[sess->alg], &wd_join_gather_setting.config);
if (!ret) {
WD_ERR("failed to check driver alg: %s!\n", wd_join_gather_alg[sess->alg]);
goto free_sess;
}
/* Some simple scheduler don't need scheduling parameters */
sess->sched_key = (void *)wd_join_gather_setting.sched.sched_init(
wd_join_gather_setting.sched.h_sched_ctx, setup->sched_param);
if (WD_IS_ERR(sess->sched_key)) {
WD_ERR("failed to init join_gather session schedule key!\n");
goto free_sess;
}
params.alg_name = wd_join_gather_alg[sess->alg];
params.ctxs = wd_join_gather_setting.config.ctxs;
wd_join_gather_setting.sched.set_param(
wd_join_gather_setting.sched.h_sched_ctx,
sess->sched_key, ¶ms);
ret = wd_join_gather_init_sess(sess, setup);
if (ret)
goto free_key;
ret = sess_data_size_init(sess, setup);
if (ret) {
WD_ERR("failed to init join gather session data size!\n");
goto uninit_sess;
}
return (handle_t)sess;
uninit_sess:
wd_join_gather_uninit_sess(sess);
free_key:
if (sess->sched_key) {
if (wd_join_gather_setting.sched.sched_uninit)
wd_join_gather_setting.sched.sched_uninit(
wd_join_gather_setting.sched.h_sched_ctx,
(handle_t)sess->sched_key);
else
free(sess->sched_key);
}
free_sess:
free(sess);
return (handle_t)0;
}
void wd_join_gather_free_sess(handle_t h_sess)
{
struct wd_join_gather_sess *sess = (struct wd_join_gather_sess *)h_sess;
if (!sess) {
WD_ERR("invalid: join gather input sess is NULL!\n");
return;
}
sess_data_size_uninit(sess);
wd_join_gather_uninit_sess(sess);
if (sess->sched_key) {
if (wd_join_gather_setting.sched.sched_uninit)
wd_join_gather_setting.sched.sched_uninit(
wd_join_gather_setting.sched.h_sched_ctx,
(handle_t)sess->sched_key);
else
free(sess->sched_key);
}
free(sess);
}
int wd_gather_get_batch_rowsize(handle_t h_sess, __u8 table_index)
{
struct wd_join_gather_sess *sess = (struct wd_join_gather_sess *)h_sess;
if (!sess || !sess->gather_conf.batch_row_size) {
WD_ERR("invalid: gather sess or batch_row_size is NULL!\n");
return -WD_EINVAL;
}
if (table_index >= sess->gather_conf.table_num) {
WD_ERR("invalid: gather table index(%u) is larger than %u!\n",
table_index, sess->gather_conf.table_num);
return -WD_EINVAL;
}
return sess->gather_conf.batch_row_size[table_index];
}
int wd_join_get_table_rowsize(handle_t h_sess)
{
struct wd_join_gather_sess *sess = (struct wd_join_gather_sess *)h_sess;
if (!sess) {
WD_ERR("invalid: hashjoin input sess is NULL!\n");
return -WD_EINVAL;
}
if (sess->alg != WD_JOIN && sess->alg != WD_JOIN_GATHER) {
WD_ERR("invalid: the session is not used for hashjoin!\n");
return -WD_EINVAL;
}
if (!sess->hash_table.table_row_size) {
WD_ERR("invalid: hashjoin sess hash table row size is 0!\n");
return -WD_EINVAL;
}
return sess->hash_table.table_row_size;
}
static int wd_join_init_sess_state(struct wd_join_gather_sess *sess,
enum wd_join_sess_state *expected)
{
enum wd_join_sess_state next;
int ret;
if (sess->hash_table.std_table) {
*expected = WD_JOIN_SESS_BUILD_HASH;
next = WD_JOIN_SESS_PREPARE_REHASH;
} else {
*expected = WD_JOIN_SESS_UNINIT;
next = WD_JOIN_SESS_INIT;
}
ret = __atomic_compare_exchange_n(&sess->state, expected, next,
false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
if (!ret) {
WD_ERR("invalid: join sess state is %u!\n", *expected);
return -WD_EINVAL;
}
return WD_SUCCESS;
}
int wd_join_set_hash_table(handle_t h_sess, struct wd_dae_hash_table *info)
{
struct wd_join_gather_sess *sess = (struct wd_join_gather_sess *)h_sess;
enum wd_join_sess_state expected;
struct wd_ctx_config_internal *config = &wd_join_gather_setting.config;
struct wd_join_gather_ops *eops;
int ret;
if (!sess || !info) {
WD_ERR("invalid: hashjoin sess or hash table is NULL!\n");
return -WD_EINVAL;
}
if (sess->alg != WD_JOIN && sess->alg != WD_JOIN_GATHER) {
WD_ERR("invalid: the session is not used for hashjoin!\n");
return -WD_EINVAL;
}
ret = wd_join_init_sess_state(sess, &expected);
if (ret)
return ret;
if (info->table_row_size != sess->hash_table.table_row_size) {
WD_ERR("invalid: hash table row size is not equal, expt: %u, real: %u!\n",
sess->hash_table.table_row_size, info->table_row_size);
ret = -WD_EINVAL;
goto out;
}
if (!info->std_table) {
WD_ERR("invalid: standard hash table is NULL!\n");
ret = -WD_EINVAL;
goto out;
}
if (info->std_table_row_num < sess->hash_table.std_table_row_num) {
WD_ERR("invalid: standard hash table is too small, expt: %u, real: %u!\n",
sess->hash_table.std_table_row_num, info->std_table_row_num);
ret = -WD_EINVAL;
goto out;
}
if (!info->ext_table_row_num || !info->ext_table)
WD_INFO("info: extern hash table is NULL!\n");
eops = config->ctxs[0].drv->extend_ops;
if (eops && eops->hash_table_init) {
ret = eops->hash_table_init(info, sess->priv);
if (ret)
goto out;
}
memcpy(&sess->hash_table, info, sizeof(struct wd_dae_hash_table));
return WD_SUCCESS;
out:
__atomic_store_n(&sess->state, expected, __ATOMIC_RELEASE);
return ret;
}
static bool wd_join_gather_atfork_registered = false;
static void wd_join_gather_clear_status(void)
{
wd_alg_clear_init(&wd_join_gather_setting.status);
}
static int wd_join_gather_alg_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
int ret;
ret = wd_set_epoll_en("WD_JOIN_GATHER_EPOLL_EN", &wd_join_gather_setting.config.epoll_en);
if (ret < 0)
return ret;
wd_join_gather_setting.config.alg_name = "hashjoin gather";
ret = wd_init_ctx_config(&wd_join_gather_setting.config, config);
if (ret < 0)
return ret;
ret = wd_init_sched(&wd_join_gather_setting.sched, sched);
if (ret < 0)
goto out_clear_ctx_config;
/* Allocate async pool for every ctx */
ret = wd_init_async_request_pool(&wd_join_gather_setting.pool, config, WD_POOL_MAX_ENTRIES,
sizeof(struct wd_join_gather_msg));
if (ret < 0)
goto out_clear_sched;
return WD_SUCCESS;
out_clear_sched:
wd_clear_sched(&wd_join_gather_setting.sched);
out_clear_ctx_config:
wd_clear_ctx_config(&wd_join_gather_setting.config);
return ret;
}
static void wd_join_gather_alg_uninit(void)
{
/* Uninit async request pool */
wd_uninit_async_request_pool(&wd_join_gather_setting.pool);
/* Unset config, sched, driver */
wd_clear_sched(&wd_join_gather_setting.sched);
}
int wd_join_gather_init(char *alg, __u32 sched_type, int task_type,
struct wd_ctx_params *ctx_params)
{
struct wd_ctx_params join_gather_ctx_params = {0};
struct wd_ctx_nums join_gather_ctx_num = {0};
int ret = -WD_EINVAL;
int try_cnt = 0;
int state;
bool flag;
if (!wd_join_gather_atfork_registered) {
if (pthread_atfork(NULL, NULL, wd_join_gather_clear_status) == 0)
wd_join_gather_atfork_registered = true;
}
state = wd_alg_try_init(&wd_join_gather_setting.status);
if (state)
return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT || task_type == TASK_MIX ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
WD_ERR("invalid: join_gathe init input param is wrong!\n");
goto out_uninit;
}
flag = wd_join_gather_alg_check(alg);
if (!flag) {
WD_ERR("invalid: alg: %s is unsupported!\n", alg);
goto out_uninit;
}
state = wd_join_gather_open_driver();
if (state)
goto out_uninit;
while (ret != 0) {
if (try_cnt++ >= WD_INIT2_MAX_RETRY) {
WD_ERR("failed to init2 after %d retries.\n",
WD_INIT2_MAX_RETRY);
goto out_driver;
}
memset(&wd_join_gather_setting.config, 0, sizeof(struct wd_ctx_config_internal));
join_gather_ctx_params.ctx_set_num = &join_gather_ctx_num;
ret = wd_ctx_param_init(&join_gather_ctx_params, ctx_params, alg,
WD_JOIN_GATHER_TYPE, 1);
if (ret) {
if (ret == -WD_EAGAIN)
continue;
goto out_driver;
}
(void)strcpy(wd_join_gather_init_attrs.alg, alg);
wd_join_gather_init_attrs.sched_type = sched_type;
wd_join_gather_init_attrs.task_type = task_type;
wd_join_gather_init_attrs.ctx_params = &join_gather_ctx_params;
wd_join_gather_init_attrs.alg_init = wd_join_gather_alg_init;
wd_join_gather_init_attrs.alg_poll_ctx = wd_join_gather_poll_ctx;
ret = wd_alg_attrs_init(&wd_join_gather_init_attrs);
if (ret) {
if (ret == -WD_ENODEV) {
wd_ctx_param_uninit(&join_gather_ctx_params);
continue;
}
WD_ERR("fail to init alg attrs.\n");
goto out_params_uninit;
}
}
ret = wd_ctx_bind_drivers(&wd_join_gather_setting.config,
wd_join_gather_init_attrs.ctx_config_internal,
WD_TYPE_V2);
if (ret) {
WD_ERR("failed to bind driver for hashjoin!\n");
goto out_common_uninit;
}
ret = wd_alg_init_driver(&wd_join_gather_setting.config);
if (ret)
goto out_unbind_drivers;
wd_alg_set_init(&wd_join_gather_setting.status);
wd_ctx_param_uninit(&join_gather_ctx_params);
return WD_SUCCESS;
out_unbind_drivers:
wd_ctx_unbind_drivers(&wd_join_gather_setting.config);
out_common_uninit:
wd_join_gather_alg_uninit();
wd_alg_attrs_uninit(&wd_join_gather_init_attrs);
out_params_uninit:
wd_ctx_param_uninit(&join_gather_ctx_params);
out_driver:
wd_join_gather_close_driver();
out_uninit:
wd_alg_clear_init(&wd_join_gather_setting.status);
return ret;
}
void wd_join_gather_uninit(void)
{
enum wd_status status;
wd_alg_get_init(&wd_join_gather_setting.status, &status);
if (status != WD_INIT)
return;
wd_alg_uninit_driver(&wd_join_gather_setting.config);
wd_ctx_unbind_drivers(&wd_join_gather_setting.config);
wd_join_gather_setting.config.drv_array = NULL;
wd_join_gather_setting.config.drv_count = 0;
wd_join_gather_alg_uninit();
wd_alg_attrs_uninit(&wd_join_gather_init_attrs);
wd_join_gather_close_driver();
wd_alg_clear_init(&wd_join_gather_setting.status);
}
static void fill_build_hash_msg(struct wd_join_gather_msg *msg,
struct wd_join_gather_sess *sess)
{
msg->index_type = sess->index_type;
msg->key_cols_num = sess->join_conf.cols_num;
}
static void fill_probe_msg(struct wd_join_gather_msg *msg,
struct wd_join_gather_sess *sess)
{
msg->key_cols_num = sess->join_conf.cols_num;
msg->index_type = sess->index_type;
msg->key_out_en = sess->join_conf.key_output_enable;
}
static void fill_rehash_msg(struct wd_join_gather_msg *msg,
struct wd_join_gather_sess *sess)
{
msg->key_cols_num = sess->join_conf.cols_num;
}
static void fill_complete_msg(struct wd_join_gather_msg *msg,
struct wd_join_gather_sess *sess)
{
__u32 table_index = msg->req.gather_req.table_index;
msg->index_type = sess->index_type;
msg->multi_batch_en = sess->gather_conf.tables[table_index].is_multi_batch;
}
static void fill_join_gather_msg(struct wd_join_gather_msg *msg, struct wd_join_gather_req *req,
struct wd_join_gather_sess *sess)
{
memcpy(&msg->req, req, sizeof(struct wd_join_gather_req));
msg->op_type = req->op_type;
switch (req->op_type) {
case WD_JOIN_BUILD_HASH:
fill_build_hash_msg(msg, sess);
break;
case WD_JOIN_PROBE:
fill_probe_msg(msg, sess);
break;
case WD_JOIN_REHASH:
fill_rehash_msg(msg, sess);
break;
case WD_GATHER_CONVERT:
break;
case WD_GATHER_COMPLETE:
fill_complete_msg(msg, sess);
break;
default:
break;
}
}
static int wd_join_gather_check_common(struct wd_join_gather_sess *sess,
struct wd_join_gather_req *req,
__u8 mode, bool is_join)
{
if (!sess) {
WD_ERR("invalid: join or gather session is NULL!\n");
return -WD_EINVAL;
}
if (!req) {
WD_ERR("invalid: join input req is NULL!\n");
return -WD_EINVAL;
}
if (mode == CTX_MODE_ASYNC && !req->cb) {
WD_ERR("invalid: join gather req cb is NULL!\n");
return -WD_EINVAL;
}
switch (sess->alg) {
case WD_JOIN:
if (!is_join || !sess->join_conf.data_size) {
WD_ERR("invalid: join session data size is NULL!\n");
return -WD_EINVAL;
}
break;