-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysqos_dispatch_base_node.c
executable file
·936 lines (827 loc) · 29.8 KB
/
sysqos_dispatch_base_node.c
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
//
// Created by root on 18-7-25.
//
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include "sysqos_dispatch_base_node.h"
#include "sysqos_protocol.h"
#include "sysqos_interface.h"
#include "sysqos_test_lib.h"
static inline bool is_to_reset(dispatch_base_node_t *desc, long new_ver)
{
bool is_reset = false;
pthread_rwlock_rdlock(&desc->lck);
if ( new_ver < desc->version )
{
is_reset = true;
}
pthread_rwlock_unlock(&desc->lck);
return is_reset;
}
static inline bool is_version_same(dispatch_base_node_t *desc, long new_ver)
{
bool is_same = false;
pthread_rwlock_rdlock(&desc->lck);
if ( new_ver - desc->version == 0 )
{
is_same = true;
}
pthread_rwlock_unlock(&desc->lck);
return is_same;
}
static bool check_update_total_version(dispatch_base_node_t *base_node,
unsigned long new_total,
long new_ver, bool *is_reset)
{
bool is_resource_increased = false;
assert(is_reset);
*is_reset = is_to_reset(base_node, new_ver);
if ( *is_reset )
{
return false;
}
if ( is_version_same(base_node, new_ver) )
{
return false;
}
pthread_rwlock_wrlock(&base_node->lck);
if ( new_ver - base_node->version > 0 )
{
if ( base_node->token_quota < new_total )
{
is_resource_increased = true;
base_node->token_quota = base_node->token_quota_new = new_total;
}
else if ( base_node->token_quota > new_total )
{
base_node->token_quota_new = new_total;
base_node->token_quota = max(new_total,
base_node->token_quota -
base_node->respond_step);
}
base_node->version = new_ver;
}
pthread_rwlock_unlock(&base_node->lck);
// check_update_version(base_node);
return is_resource_increased;
}
static int check_alloc_from_desc(dispatch_base_node_t *desc, long cost)
{
pthread_rwlock_rdlock(&desc->lck);
if ( cost + desc->token_inuse > desc->token_quota )
{
pthread_rwlock_unlock(&desc->lck);
return QOS_ERROR_PENDING;
}
pthread_rwlock_unlock(&desc->lck);
return QOS_ERROR_OK;
}
static int try_alloc_from_desc(dispatch_base_node_t *desc, long cost)
{
int err = check_alloc_from_desc(desc, cost);
if ( err )
{
return err;
}
pthread_rwlock_wrlock(&desc->lck);
if ( cost + desc->token_inuse <= desc->token_quota )
{
desc->token_inuse += cost;
if ( desc->token_quota_new < desc->token_quota )
{
desc->token_quota = max(desc->token_quota_new,
desc->token_quota - desc->respond_step);
}
}
pthread_rwlock_unlock(&desc->lck);
return QOS_ERROR_OK;
}
static void free_to_desc(dispatch_base_node_t *desc, long cost)
{
pthread_rwlock_wrlock(&desc->lck);
desc->token_inuse -= cost;
pthread_rwlock_unlock(&desc->lck);
}
static long get_currentversion(dispatch_base_node_t *desc)
{
long res = 0;
pthread_rwlock_rdlock(&desc->lck);
res = desc->version;
pthread_rwlock_unlock(&desc->lck);
return res;
}
static unsigned long get_alloced(dispatch_base_node_t *desc)
{
unsigned long res = 0;
pthread_rwlock_rdlock(&desc->lck);
res = desc->token_inuse;
pthread_rwlock_unlock(&desc->lck);
return res;
}
static void reset(struct dispatch_base_node *desc)
{
pthread_rwlock_wrlock(&desc->lck);
desc->token_quota = MIN_RS_NUM;
desc->token_quota_new = MIN_RS_NUM;
desc->version = 0;
pthread_rwlock_unlock(&desc->lck);
}
int dispatch_base_node_init(dispatch_base_node_t *base_node)
{
base_node->token_inuse = 0;
base_node->version = 0;
base_node->token_quota = MIN_RS_NUM;
base_node->token_quota_new = MIN_RS_NUM;
base_node->respond_step = MMAX_RESPOND_STEP;
base_node->get_version = get_currentversion;
base_node->get_token_inuse = get_alloced;
// base_node->check_alloc_from_desc = check_alloc_from_desc;
base_node->free_to_base = free_to_desc;
base_node->try_alloc_from_base = try_alloc_from_desc;
base_node->check_update_quota_version = check_update_total_version;
base_node->reset = reset;
return pthread_rwlock_init(&base_node->lck, NULL);
}
void dispatch_base_node_exit(dispatch_base_node_t *base_node)
{
pthread_rwlock_destroy(&base_node->lck);
}
/**for test*****************************************************************************************/
typedef struct disp_desc_manager_event
{
void (*resource_increased)(struct disp_desc_manager_event *event, void *id);
} disp_desc_manager_event_t;
typedef struct disp_desc_manager
{
hash_table_t *tab;
memory_cache_t cache;
disp_desc_manager_event_t *event_to_permission;
pthread_rwlock_t lck;
/******************************************************************************/
msg_event_ops_t msg_event;//NOTE:此对象之实现online,offline,rcvd
int
(*check_alloc_resource)(struct disp_desc_manager *manager, resource_t *rs);
int
(*try_alloc_resource)(struct disp_desc_manager *manager, resource_t *rs);
int (*free_resource)(struct disp_desc_manager *manager, resource_t *rs);
unsigned long
(*get_currentversion)(struct disp_desc_manager *manager, void *id);
void (*set_hash)(struct disp_desc_manager *manager, hash_func hash);
void
(*set_compare)(struct disp_desc_manager *manager, compare_id_func compare);
void (*set_event)(struct disp_desc_manager *manager,
disp_desc_manager_event_t *event);
} disp_desc_manager_t;
static int
check_alloc_resource(struct disp_desc_manager *manager, resource_t *rs)
{
int err = QOS_ERROR_OK;
void *desc = NULL;
assert(manager && rs && rs->cost);
pthread_rwlock_rdlock(&manager->lck);
err = manager->tab->find(manager->tab, rs->id, &desc);
if ( err )
end_func(err, QOS_ERROR_FAILEDNODE, unlock_manager);
assert(desc);
err = check_alloc_from_desc(desc, rs->cost);
if ( err )
end_func(err, QOS_ERROR_PENDING, unlock_manager);
unlock_manager:
pthread_rwlock_unlock(&manager->lck);
return err;
}
static int try_alloc_resource(struct disp_desc_manager *manager, resource_t *rs)
{
int err = QOS_ERROR_OK;
void *desc = NULL;
assert(manager && rs && rs->cost);
pthread_rwlock_rdlock(&manager->lck);
err = manager->tab->find(manager->tab, rs->id, &desc);
if ( err )
end_func(err, QOS_ERROR_FAILEDNODE, unlock_manager);
assert(desc);
err = try_alloc_from_desc(desc, rs->cost);
if ( err )
end_func(err, QOS_ERROR_PENDING, unlock_manager);
unlock_manager:
pthread_rwlock_unlock(&manager->lck);
return err;
}
int free_resource(struct disp_desc_manager *manager, resource_t *rs)
{
int err = QOS_ERROR_OK;
void *desc = NULL;
assert(manager && rs && rs->cost);
pthread_rwlock_rdlock(&manager->lck);
err = manager->tab->find(manager->tab, rs->id, &desc);
if ( err )
end_func(err, QOS_ERROR_FAILEDNODE, unlock_manager);
assert(desc);
free_to_desc(desc, rs->cost);
unlock_manager:
pthread_rwlock_unlock(&manager->lck);
return err;
}
unsigned long get_currentversion_m(struct disp_desc_manager *manager, void *id)
{
int err = QOS_ERROR_OK;
unsigned long ver = 0;
void *desc;
assert(manager);
pthread_rwlock_rdlock(&manager->lck);
err = manager->tab->find(manager->tab, id, &desc);
if ( !err )
{
assert(desc);
ver = ((dispatch_base_node_t *) desc)->get_version(desc);
}
pthread_rwlock_unlock(&manager->lck);
return ver;
}
void set_hash(struct disp_desc_manager *manager, hash_func hash)
{
assert(manager && hash);
manager->tab->set_hash(manager->tab, hash);
}
void set_compare(struct disp_desc_manager *manager, compare_id_func compare)
{
assert(manager && compare);
manager->tab->set_compare(manager->tab, compare);
}
void
set_event(struct disp_desc_manager *manager, disp_desc_manager_event_t *event)
{
assert(manager && event);
manager->event_to_permission = event;
}
static inline disp_desc_manager_t *
disp_desc_manager_by_msg_event(struct msg_event_ops *ops)
{
assert(ops);
return container_of(ops, disp_desc_manager_t, msg_event);
}
static void node_online(struct msg_event_ops *ops, void *id)
{
disp_desc_manager_t *manager = disp_desc_manager_by_msg_event(ops);
int err = QOS_ERROR_OK;
dispatch_base_node_t *desc = manager->cache.alloc(&manager->cache);
assert(desc);
err = dispatch_base_node_init(desc);
assert(err == 0);
pthread_rwlock_wrlock(&manager->lck);
err = manager->tab->insert(manager->tab, id, desc);
pthread_rwlock_unlock(&manager->lck);
if ( err )
{
dispatch_base_node_exit(desc);
manager->cache.free(&manager->cache, desc);
}
}
static void node_reset(struct msg_event_ops *ops, void *id)
{
disp_desc_manager_t *manager = disp_desc_manager_by_msg_event(ops);
void *desc = NULL;
int err = QOS_ERROR_OK;
pthread_rwlock_wrlock(&manager->lck);
do
{
err = manager->tab->find(manager->tab, id, &desc);
if ( err )
{
break;
}
((dispatch_base_node_t *) desc)->reset((dispatch_base_node_t *) desc);
} while ( 0 );
pthread_rwlock_unlock(&manager->lck);
}
static void node_offline(struct msg_event_ops *ops, void *id)
{
disp_desc_manager_t *manager = disp_desc_manager_by_msg_event(ops);
void *desc = NULL;
int err = QOS_ERROR_OK;
pthread_rwlock_wrlock(&manager->lck);
err = manager->tab->erase(manager->tab, id, &desc);
pthread_rwlock_unlock(&manager->lck);
if ( err == 0 && desc )
{
dispatch_base_node_exit((dispatch_base_node_t *) desc);
manager->cache.free(&manager->cache, desc);
}
}
static void
rcvd(struct msg_event_ops *ops, void *id, long len, unsigned char *buf)
{
disp_desc_manager_t *manager =
disp_desc_manager_by_msg_event(ops);
bool is_resource_increased = false;
bool is_reset = false;
dispatch2app_t app;
assert(len == sizeof(dispatch2app_t) && manager->event_to_permission &&
manager->event_to_permission->resource_increased);
memcpy(&app, buf, len);
void *desc;
pthread_rwlock_rdlock(&manager->lck);
do
{
int err = manager->tab->find(manager->tab, id, &desc);
if ( err )
{
break;
}
is_resource_increased =
check_update_total_version((dispatch_base_node_t *) desc,
app.token_quota,
app.version, &is_reset);
} while ( 0 );
pthread_rwlock_unlock(&manager->lck);
if ( is_reset )
{
ops->node_reset(ops, id);
}
else if ( is_resource_increased )
{
manager->event_to_permission
->resource_increased(manager->event_to_permission, id);
}
}
static int disp_desc_manager_init(disp_desc_manager_t *manager, int tab_len,
int max_node_num)
{
int err = QOS_ERROR_OK;
memset(manager, 0, sizeof(disp_desc_manager_t));
manager->tab = alloc_hash_table(tab_len, max_node_num);
if ( !manager->tab )
end_func(err, QOS_ERROR_MEMORY, alloc_hash_table_failed);
err = memory_cache_init(&manager->cache, sizeof(dispatch_base_node_t),
max_node_num);
if ( err )
end_func(err, QOS_ERROR_MEMORY, memory_cache_init_failed);
err = pthread_rwlock_init(&manager->lck, NULL);
if ( err )
end_func(err, QOS_ERROR_MEMORY, rwlock_init_failed);
manager->msg_event.node_offline = node_offline;
manager->msg_event.node_online = node_online;
manager->msg_event.node_reset = node_reset;
manager->msg_event.rcvd = rcvd;
manager->set_compare = set_compare;
manager->set_hash = set_hash;
manager->set_event = set_event;
manager->check_alloc_resource = check_alloc_resource;
manager->try_alloc_resource = try_alloc_resource;
manager->free_resource = free_resource;
manager->get_currentversion = get_currentversion_m;
return QOS_ERROR_OK;
pthread_rwlock_destroy(&manager->lck);
rwlock_init_failed:
memory_cache_exit(&manager->cache);
memory_cache_init_failed:
free_hash_table(manager->tab);
alloc_hash_table_failed:
return err;
}
static void disp_desc_manager_exit(disp_desc_manager_t *manager)
{
assert(manager && manager->tab);
pthread_rwlock_destroy(&manager->lck);
memory_cache_exit(&manager->cache);
free_hash_table(manager->tab);
}
/**for test*****************************************************************************************/
#ifdef DISP_DESC_MANAGER_TEST
#include <stdio.h>
#define TABLE_LEN 128
#define MAX_NODE_NUM 4096
#define FIRST_TOTAL 2000
#define RESOURCE_COST_LIST_NUM 10
const unsigned long rs_cost_list[RESOURCE_COST_LIST_NUM] =
{5, 23, 17, 16, 9, 7, 31, 37, 53, 91};
#define FIRST_NODE_NUM 5
static bool is_increased = false;
static void resource_increased(struct disp_desc_manager_event *event, void *id)
{
is_increased = true;
}
disp_desc_manager_event_t test_event =
{
.resource_increased = resource_increased
};
static void test_case_init_exit(disp_desc_manager_t *manager)
{
int err = disp_desc_manager_init(manager, TABLE_LEN, MAX_NODE_NUM);
assert(err == 0);
manager->set_compare(manager, test_compare);
manager->set_hash(manager, test_hash);
manager->set_event(manager, &test_event);
disp_desc_manager_exit(manager);
err = disp_desc_manager_init(manager, TABLE_LEN, MAX_NODE_NUM);
assert(err == 0);
manager->set_compare(manager, test_compare);
manager->set_hash(manager, test_hash);
manager->set_event(manager, &test_event);
printf("[test_case_init_exit] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_regular_online_offline(disp_desc_manager_t *manager)
{
int err = QOS_ERROR_OK;
resource_t rs;
rs.cost = 1;
rs.id = (void *) 1;
err = manager->try_alloc_resource(manager, &rs);
assert(err);
err = manager->free_resource(manager, &rs);
assert(err);
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event.node_online(&manager->msg_event, (void *) 2);
manager->msg_event.node_offline(&manager->msg_event, (void *) 2);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
manager->msg_event.node_offline(&manager->msg_event, (void *) 2);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_online_offline] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_regular_rcvd(disp_desc_manager_t *manager)
{
dispatch2app_t dta;
dta.version = 1;
dta.token_quota = FIRST_TOTAL;
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
assert(manager->get_currentversion(manager, (void *) 1) == 0);
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(manager->get_currentversion(manager, (void *) 1) == dta.version);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_rcvd] %s[OK]%s\n", GREEN, RESET);
}
//FIXME 改成更具step分步骤 减少的 用 例
static void test_case_regular_rcvd_reduce(disp_desc_manager_t *manager)
{
unsigned long left_len = FIRST_TOTAL;
int i = 0;
int err = 0;
void *desc;
dispatch2app_t dta;
dta.version = 1;
dta.token_quota = FIRST_TOTAL;
is_increased = false;
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(is_increased == true);
resource_t rs;
for ( i = 0; i < FIRST_TOTAL; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = try_alloc_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
dta.version = 2;
dta.token_quota = FIRST_TOTAL / 2;
is_increased = false;
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(is_increased == false);
assert(manager->get_currentversion(manager, (void *) 1) == 2);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL / 2);
left_len = FIRST_TOTAL / 2;
for ( i = 0; i < FIRST_TOTAL / 2; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = free_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(manager->get_currentversion(manager, (void *) 1) == 2);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL / 2);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL / 2);
assert(is_increased == false);
left_len = FIRST_TOTAL / 2;
for ( i = 0; i < FIRST_TOTAL / 2; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = free_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(manager->get_currentversion(manager, (void *) 1) == 2);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == 0);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL / 2);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_rcvd_reduce] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_regular_reset(disp_desc_manager_t *manager)
{
unsigned long left_len = FIRST_TOTAL;
int i = 0;
int err = 0;
void *desc;
dispatch2app_t dta;
dta.version = 1;
dta.token_quota = FIRST_TOTAL;
is_increased = false;
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(is_increased == true);
resource_t rs;
for ( i = 0; i < FIRST_TOTAL; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = try_alloc_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
dta.version = 0;
dta.token_quota = FIRST_TOTAL / 2;
is_increased = false;
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(is_increased == false);
assert(manager->get_currentversion(manager, (void *) 1) == 0);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL);
assert(((dispatch_base_node_t *) desc)->token_quota == MIN_RS_NUM);
left_len = FIRST_TOTAL / 2;
for ( i = 0; i < FIRST_TOTAL / 2; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = free_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(manager->get_currentversion(manager, (void *) 1) == 0);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL / 2);
assert(((dispatch_base_node_t *) desc)->token_quota == MIN_RS_NUM);
assert(is_increased == false);
left_len = FIRST_TOTAL / 2;
for ( i = 0; i < FIRST_TOTAL / 2; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = free_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(manager->get_currentversion(manager, (void *) 1) == 0);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == 0);
assert(((dispatch_base_node_t *) desc)->token_quota == MIN_RS_NUM);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_reset] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_regular_rcvd_increase(disp_desc_manager_t *manager)
{
unsigned long left_len = FIRST_TOTAL / 2;
int i = 0;
int err = 0;
void *desc;
dispatch2app_t dta;
dta.version = 1;
dta.token_quota = FIRST_TOTAL / 2;
is_increased = false;
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(is_increased == true);
resource_t rs;
for ( i = 0; i < FIRST_TOTAL / 2; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = try_alloc_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
err = try_alloc_resource(manager, &rs);
assert(err != 0);
dta.version = 2;
dta.token_quota = FIRST_TOTAL;
is_increased = false;
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
assert(is_increased == true);
assert(manager->get_currentversion(manager, (void *) 1) == 2);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL / 2);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL);
left_len = FIRST_TOTAL / 2;
for ( i = 0; i < FIRST_TOTAL / 2; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = try_alloc_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(manager->get_currentversion(manager, (void *) 1) == 2);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_rcvd_increase] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_regular_alloc_free(disp_desc_manager_t *manager)
{
unsigned long left_len = FIRST_TOTAL;
int i = 0;
int err = 0;
void *desc;
dispatch2app_t dta;
dta.version = 1;
dta.token_quota = FIRST_TOTAL;
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
resource_t rs;
for ( i = 0; i < FIRST_TOTAL; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = try_alloc_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
err = try_alloc_resource(manager, &rs);
assert(err != 0);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL);
left_len = FIRST_TOTAL;
for ( i = 0; i < FIRST_TOTAL; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = free_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == 0);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_alloc_free] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_regular_check_free(disp_desc_manager_t *manager)
{
unsigned long left_len = FIRST_TOTAL;
int i = 0;
int err = 0;
void *desc;
dispatch2app_t dta;
dta.version = 1;
dta.token_quota = FIRST_TOTAL;
manager->msg_event.node_online(&manager->msg_event, (void *) 1);
manager->msg_event
.rcvd(&manager->msg_event, (void *) 1, sizeof(dispatch2app_t),
(unsigned char *) &dta);
resource_t rs;
for ( i = 0; i < FIRST_TOTAL; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = check_alloc_resource(manager, &rs);
assert(err == 0);
err = try_alloc_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
err = check_alloc_resource(manager, &rs);
assert(err != 0);
err = try_alloc_resource(manager, &rs);
assert(err != 0);
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == FIRST_TOTAL);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL);
left_len = FIRST_TOTAL;
for ( i = 0; i < FIRST_TOTAL; ++i )
{
if ( left_len == 0 )
{
break;
}
rs.cost = min(rs_cost_list[i % RESOURCE_COST_LIST_NUM], left_len);
rs.id = (void *) 1;
err = free_resource(manager, &rs);
assert(err == 0);
left_len -= rs.cost;
}
assert(0 == manager->tab->find(manager->tab, (void *) 1, &desc));
assert(((dispatch_base_node_t *) desc)->token_inuse == 0);
assert(((dispatch_base_node_t *) desc)->token_quota == FIRST_TOTAL);
manager->msg_event.node_offline(&manager->msg_event, (void *) 1);
#ifdef CACHE_OPEN_CNT
assert(manager->cache.alloc_cnt == manager->cache.free_cnt);
assert(manager->tab->cache.alloc_cnt == manager->tab->cache.free_cnt);
#endif
printf("[test_case_regular_alloc_free] %s[OK]%s\n", GREEN, RESET);
}
#endif
void test_dispatch_base_node()
{
#ifdef DISP_DESC_MANAGER_TEST
int err = 0;
printf(YELLOW"--------------test_dispatch_base_node----------------:\n"RESET);
disp_desc_manager_t manager;
test_case_init_exit(&manager);
test_case_regular_online_offline(&manager);
test_case_regular_rcvd(&manager);
test_case_regular_alloc_free(&manager);
test_case_regular_check_free(&manager);
// test_case_regular_rcvd_reduce(&manager);
test_case_regular_rcvd_increase(&manager);
test_case_regular_reset(&manager);
//FIXME:设计一个梯度用例 token_quota_new step
disp_desc_manager_exit(&manager);
printf("[disp_desc_manager_exit] %s[OK]%s\n", GREEN, RESET);
printf(BLUE"--------------test_dispatch_base_node end--------------:\n"RESET);
#endif
}