forked from OpenVisualCloud/Media-Transport-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt_sch.c
More file actions
1394 lines (1181 loc) · 40.1 KB
/
Copy pathmt_sch.c
File metadata and controls
1394 lines (1181 loc) · 40.1 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: BSD-3-Clause
* Copyright(c) 2022 Intel Corporation
*/
#include "mt_sch.h"
#include <signal.h>
#include "mt_instance.h"
#include "mt_log.h"
#include "mt_stat.h"
#include "mtl_lcore_shm_api.h"
#include "st2110/st_rx_ancillary_session.h"
#include "st2110/st_rx_audio_session.h"
#include "st2110/st_rx_fastmetadata_session.h"
#include "st2110/st_rx_video_session.h"
#include "st2110/st_tx_ancillary_session.h"
#include "st2110/st_tx_audio_session.h"
#include "st2110/st_tx_fastmetadata_session.h"
#include "st2110/st_tx_video_session.h"
static inline void sch_mgr_lock(struct mt_sch_mgr* mgr) {
mt_pthread_mutex_lock(&mgr->mgr_mutex);
}
static inline void sch_mgr_unlock(struct mt_sch_mgr* mgr) {
mt_pthread_mutex_unlock(&mgr->mgr_mutex);
}
static inline void sch_lock(struct mtl_sch_impl* sch) {
mt_pthread_mutex_lock(&sch->mutex);
}
static inline void sch_unlock(struct mtl_sch_impl* sch) {
mt_pthread_mutex_unlock(&sch->mutex);
}
static const char* lcore_type_names[MT_LCORE_TYPE_MAX] = {
"lib_sch", "lib_tap", "lib_rxv_ring", "app_allocated", "lib_app_sch",
};
static const char* lcore_type_name(enum mt_lcore_type type) {
if (type >= MT_LCORE_TYPE_SCH && type < MT_LCORE_TYPE_MAX)
return lcore_type_names[type];
else
return "unknown";
}
static void sch_sleep_wakeup(struct mtl_sch_impl* sch) {
mt_pthread_mutex_lock(&sch->sleep_wake_mutex);
mt_pthread_cond_signal(&sch->sleep_wake_cond);
mt_pthread_mutex_unlock(&sch->sleep_wake_mutex);
}
static void sch_sleep_alarm_handler(void* param) {
struct mtl_sch_impl* sch = param;
sch_sleep_wakeup(sch);
}
static int sch_tasklet_sleep(struct mtl_main_impl* impl, struct mtl_sch_impl* sch) {
/* get sleep us */
uint64_t sleep_us = mt_sch_default_sleep_us(impl);
uint64_t force_sleep_us = mt_sch_force_sleep_us(impl);
int num_tasklet = sch->max_tasklet_idx;
struct mt_sch_tasklet_impl* tasklet;
uint64_t advice_sleep_us;
if (force_sleep_us) {
sleep_us = force_sleep_us;
} else {
for (int i = 0; i < num_tasklet; i++) {
tasklet = sch->tasklet[i];
if (!tasklet) continue;
advice_sleep_us = tasklet->ops.advice_sleep_us;
if (advice_sleep_us && (advice_sleep_us < sleep_us)) sleep_us = advice_sleep_us;
}
}
dbg("%s(%d), sleep_us %" PRIu64 "\n", __func__, sch->idx, sleep_us);
/* sleep now */
uint64_t start = mt_get_tsc(impl);
if (sleep_us < mt_sch_zero_sleep_thresh_us(impl)) {
mt_sleep_ms(0);
} else {
rte_eal_alarm_set(sleep_us, sch_sleep_alarm_handler, sch);
mt_pthread_mutex_lock(&sch->sleep_wake_mutex);
/* timeout 1s */
mt_pthread_cond_timedwait_ns(&sch->sleep_wake_cond, &sch->sleep_wake_mutex, NS_PER_S);
mt_pthread_mutex_unlock(&sch->sleep_wake_mutex);
}
uint64_t end = mt_get_tsc(impl);
uint64_t delta = end - start;
sch->stat_sleep_ns += delta;
sch->stat_sleep_cnt++;
sch->stat_sleep_ns_min = RTE_MIN(delta, sch->stat_sleep_ns_min);
sch->stat_sleep_ns_max = RTE_MAX(delta, sch->stat_sleep_ns_max);
/* cal cpu sleep ratio on every 5s */
sch->sleep_ratio_sleep_ns += delta;
uint64_t sleep_ratio_dur_ns = end - sch->sleep_ratio_start_ns;
if (sleep_ratio_dur_ns > (5 * (uint64_t)NS_PER_S)) {
dbg("%s(%d), sleep %" PRIu64 "ns, total %" PRIu64 "ns\n", __func__, sch->idx,
sch->sleep_ratio_sleep_ns, sleep_ratio_dur_ns);
dbg("%s(%d), end %" PRIu64 "ns, start %" PRIu64 "ns\n", __func__, sch->idx, end,
sch->sleep_ratio_start_ns);
sch->sleep_ratio_score =
(float)sch->sleep_ratio_sleep_ns * 100.0 / sleep_ratio_dur_ns;
sch->sleep_ratio_sleep_ns = 0;
sch->sleep_ratio_start_ns = end;
}
return 0;
}
static bool sch_tasklet_time_measure(struct mtl_main_impl* impl) {
bool enabled = mt_user_tasklet_time_measure(impl);
if (MT_USDT_TASKLET_TIME_MEASURE_ENABLED()) enabled = true;
return enabled;
}
static int sch_tasklet_func(struct mtl_sch_impl* sch) {
struct mtl_main_impl* impl = sch->parent;
int idx = sch->idx;
int num_tasklet, i;
struct mtl_tasklet_ops* ops;
struct mt_sch_tasklet_impl* tasklet;
uint64_t loop_cal_start_ns;
uint64_t loop_cnt = 0;
num_tasklet = sch->max_tasklet_idx;
info("%s(%d), start with %d tasklets, t_pid %d\n", __func__, idx, num_tasklet,
sch->t_pid);
char thread_name[32];
snprintf(thread_name, sizeof(thread_name), "mtl_sch_%d", idx);
mtl_thread_setname(sch->tid, thread_name);
for (i = 0; i < num_tasklet; i++) {
tasklet = sch->tasklet[i];
if (!tasklet) continue;
ops = &tasklet->ops;
if (ops->start) ops->start(ops->priv);
}
sch->sleep_ratio_start_ns = mt_get_tsc(impl);
loop_cal_start_ns = mt_get_tsc(impl);
while (rte_atomic32_read(&sch->request_stop) == 0) {
int pending = MTL_TASKLET_ALL_DONE;
bool time_measure = sch_tasklet_time_measure(impl);
uint64_t tm_sch_tsc_s = 0; /* for sch time_measure */
if (time_measure) tm_sch_tsc_s = mt_get_tsc(impl);
num_tasklet = sch->max_tasklet_idx;
for (i = 0; i < num_tasklet; i++) {
tasklet = sch->tasklet[i];
if (!tasklet) continue;
if (tasklet->request_exit) {
tasklet->ack_exit = true;
sch->tasklet[i] = NULL;
dbg("%s(%d), tasklet %s(%d) exit\n", __func__, idx, tasklet->name, i);
continue;
}
ops = &tasklet->ops;
uint64_t tm_tasklet_tsc_s = 0; /* for tasklet time_measure */
if (time_measure) tm_tasklet_tsc_s = mt_get_tsc(impl);
pending += ops->handler(ops->priv);
if (time_measure) {
uint64_t delta_ns = mt_get_tsc(impl) - tm_tasklet_tsc_s;
mt_stat_u64_update(&tasklet->stat_time, delta_ns);
}
}
if (sch->allow_sleep && (pending == MTL_TASKLET_ALL_DONE)) {
sch_tasklet_sleep(impl, sch);
}
loop_cnt++;
/* cal avg_ns_per_loop per two second */
uint64_t delta_loop_ns = mt_get_tsc(impl) - loop_cal_start_ns;
if (delta_loop_ns > ((uint64_t)NS_PER_S * 2)) {
sch->avg_ns_per_loop = delta_loop_ns / loop_cnt;
loop_cnt = 0;
loop_cal_start_ns = mt_get_tsc(impl);
}
if (time_measure) {
uint64_t delta_ns = mt_get_tsc(impl) - tm_sch_tsc_s;
mt_stat_u64_update(&sch->stat_time, delta_ns);
}
}
num_tasklet = sch->max_tasklet_idx;
for (i = 0; i < num_tasklet; i++) {
tasklet = sch->tasklet[i];
if (!tasklet) continue;
ops = &tasklet->ops;
if (ops->stop) ops->stop(ops->priv);
}
rte_atomic32_set(&sch->stopped, 1);
info("%s(%d), end with %d tasklets\n", __func__, idx, num_tasklet);
return 0;
}
static int sch_tasklet_lcore(void* arg) {
struct mtl_sch_impl* sch = arg;
sch->tid = pthread_self();
sch->t_pid = rte_sys_gettid();
sch_tasklet_func(sch);
return 0;
}
static void* sch_tasklet_thread(void* arg) {
struct mtl_sch_impl* sch = arg;
sch->t_pid = rte_sys_gettid();
sch_tasklet_func(sch);
return NULL;
}
static int sch_start(struct mtl_sch_impl* sch) {
int idx = sch->idx;
int ret;
sch_lock(sch);
if (mt_sch_started(sch)) {
warn("%s(%d), started already\n", __func__, idx);
sch_unlock(sch);
return -EIO;
}
mt_sch_set_cpu_busy(sch, false);
rte_atomic32_set(&sch->request_stop, 0);
rte_atomic32_set(&sch->stopped, 0);
if (!sch->run_in_thread) {
ret = mt_sch_get_lcore(
sch->parent, &sch->lcore,
(sch->type == MT_SCH_TYPE_APP) ? MT_LCORE_TYPE_SCH_USER : MT_LCORE_TYPE_SCH,
mt_sch_socket_id(sch));
if (ret < 0) {
err("%s(%d), get lcore fail %d\n", __func__, idx, ret);
sch_unlock(sch);
return ret;
}
ret = rte_eal_remote_launch(sch_tasklet_lcore, sch, sch->lcore);
} else {
ret = pthread_create(&sch->tid, NULL, sch_tasklet_thread, sch);
}
if (ret < 0) {
err("%s(%d), fail %d to launch\n", __func__, idx, ret);
sch_unlock(sch);
return ret;
}
rte_atomic32_set(&sch->started, 1);
if (!sch->run_in_thread)
info("%s(%d), succ on lcore %u socket %d\n", __func__, idx, sch->lcore,
mt_sch_socket_id(sch));
else
info("%s(%d), succ on tid %" PRIu64 "\n", __func__, idx, sch->tid);
sch_unlock(sch);
return 0;
}
static int sch_stop(struct mtl_sch_impl* sch) {
int idx = sch->idx;
sch_lock(sch);
if (!mt_sch_started(sch)) {
warn("%s(%d), not started\n", __func__, idx);
sch_unlock(sch);
return 0;
}
rte_atomic32_set(&sch->request_stop, 1);
while (rte_atomic32_read(&sch->stopped) == 0) {
mt_sleep_ms(10);
}
if (!sch->run_in_thread) {
rte_eal_wait_lcore(sch->lcore);
mt_sch_put_lcore(sch->parent, sch->lcore);
} else {
pthread_join(sch->tid, NULL);
}
rte_atomic32_set(&sch->started, 0);
mt_sch_set_cpu_busy(sch, false);
info("%s(%d), succ\n", __func__, idx);
sch_unlock(sch);
return 0;
}
static struct mtl_sch_impl* sch_request(struct mtl_main_impl* impl, enum mt_sch_type type,
mt_sch_mask_t mask, struct mtl_sch_ops* ops,
int socket) {
struct mtl_sch_impl* sch;
for (int sch_idx = 0; sch_idx < MT_MAX_SCH_NUM; sch_idx++) {
/* mask check */
if (!(mask & MTL_BIT64(sch_idx))) continue;
sch = mt_sch_instance(impl, sch_idx);
sch_lock(sch);
if (!mt_sch_is_active(sch)) { /* find one free sch */
sch->type = type;
if (ops && ops->name) {
snprintf(sch->name, sizeof(sch->name), "%s", ops->name);
} else {
snprintf(sch->name, sizeof(sch->name), "sch_%d", sch_idx);
}
if (ops && ops->nb_tasklets)
sch->nb_tasklets = ops->nb_tasklets;
else
sch->nb_tasklets = impl->tasklets_nb_per_sch;
sch->tasklet =
mt_rte_zmalloc_socket(sizeof(*sch->tasklet) * sch->nb_tasklets, socket);
if (!sch->tasklet) {
err("%s(%d), %u tasklet malloc fail\n", __func__, sch_idx, sch->nb_tasklets);
sch_unlock(sch);
return NULL;
}
rte_atomic32_inc(&sch->active);
rte_atomic32_inc(&mt_sch_get_mgr(impl)->sch_cnt);
sch_unlock(sch);
/* set the socket id */
sch->socket_id = socket;
info("%s(%d), name %s with %u tasklets, type %d socket %d\n", __func__, sch_idx,
sch->name, sch->nb_tasklets, type, sch->socket_id);
return sch;
}
sch_unlock(sch);
}
err("%s, fail as no free sch\n", __func__);
return NULL;
}
static int sch_free(struct mtl_sch_impl* sch) {
int idx = sch->idx;
if (!mt_sch_is_active(sch)) {
err("%s, sch %d is not allocated\n", __func__, idx);
return -EIO;
}
info("%s(%d), start to free sch: %s \n", __func__, idx, sch->name);
sch_lock(sch);
if (sch->tasklet) {
for (int i = 0; i < sch->nb_tasklets; i++) {
if (sch->tasklet[i]) {
warn("%s(%d), tasklet %d still active\n", __func__, idx, i);
sch_unlock(sch); /* unlock */
mtl_sch_unregister_tasklet(sch->tasklet[i]);
sch_lock(sch);
}
}
mt_rte_free(sch->tasklet);
sch->tasklet = NULL;
}
sch->nb_tasklets = 0;
rte_atomic32_dec(&mt_sch_get_mgr(sch->parent)->sch_cnt);
rte_atomic32_dec(&sch->active);
sch_unlock(sch);
return 0;
}
static int sch_free_quota(struct mtl_sch_impl* sch, int quota_mbs) {
int idx = sch->idx;
if (!mt_sch_is_active(sch)) {
err("%s(%d), sch is not allocated\n", __func__, idx);
return -ENOMEM;
}
sch_lock(sch);
sch->data_quota_mbs_total -= quota_mbs;
if (!sch->data_quota_mbs_total) {
/* no tx/rx video, change to default */
sch->type = MT_SCH_TYPE_DEFAULT;
}
sch_unlock(sch);
info("%s(%d), quota %d total now %d\n", __func__, idx, quota_mbs,
sch->data_quota_mbs_total);
return 0;
}
static bool sch_is_capable(struct mtl_sch_impl* sch, int quota_mbs,
enum mt_sch_type type) {
if (!quota_mbs) { /* zero quota_mbs can be applied to any type */
return true;
}
if ((type == MT_SCH_TYPE_RX_VIDEO_ONLY) && (sch->type == MT_SCH_TYPE_DEFAULT)) {
sch_lock(sch);
if (!sch->data_quota_mbs_total) {
/* change type to rx video only since no quota on this */
sch->type = MT_SCH_TYPE_RX_VIDEO_ONLY;
sch_unlock(sch);
return true;
}
sch_unlock(sch);
}
if (sch->type != type)
return false;
else
return true;
}
static int sch_stat(void* priv) {
struct mtl_sch_impl* sch = priv;
int num_tasklet = sch->max_tasklet_idx;
struct mt_sch_tasklet_impl* tasklet;
int idx = sch->idx;
if (!mt_sch_is_active(sch)) return 0;
notice("SCH(%d:%s): tasklets %d, lcore %u(t_pid: %d), avg loop %" PRIu64 " ns\n", idx,
sch->name, num_tasklet, sch->lcore, sch->t_pid, mt_sch_avg_ns_loop(sch));
/* print the stat time info */
struct mt_stat_u64* stat_time = &sch->stat_time;
if (stat_time->cnt) {
uint64_t avg_ns = stat_time->sum / stat_time->cnt;
notice("SCH(%d): time avg %.2fus max %.2fus min %.2fus\n", idx,
(float)avg_ns / NS_PER_US, (float)stat_time->max / NS_PER_US,
(float)stat_time->min / NS_PER_US);
mt_stat_u64_init(stat_time);
}
for (int i = 0; i < num_tasklet; i++) {
tasklet = sch->tasklet[i];
if (!tasklet) continue;
dbg("SCH(%d): tasklet %s at %d\n", idx, tasklet->name, i);
stat_time = &tasklet->stat_time;
if (stat_time->cnt) {
uint64_t avg_ns = stat_time->sum / stat_time->cnt;
notice("SCH(%d,%d): tasklet %s, avg %.2fus max %.2fus min %.2fus\n", idx, i,
tasklet->name, (float)avg_ns / NS_PER_US, (float)stat_time->max / NS_PER_US,
(float)stat_time->min / NS_PER_US);
mt_stat_u64_init(stat_time);
}
}
if (sch->allow_sleep) {
notice("SCH(%d): sleep %fms(ratio:%f), cnt %u, min %" PRIu64 "us, max %" PRIu64
"us\n",
idx, (double)sch->stat_sleep_ns / NS_PER_MS, sch->sleep_ratio_score,
sch->stat_sleep_cnt, sch->stat_sleep_ns_min / NS_PER_US,
sch->stat_sleep_ns_max / NS_PER_US);
sch->stat_sleep_ns = 0;
sch->stat_sleep_cnt = 0;
sch->stat_sleep_ns_min = -1;
sch->stat_sleep_ns_max = 0;
}
if (!mt_sch_started(sch)) {
notice("SCH(%d): active but still not started\n", idx);
}
return 0;
}
static int sch_filelock_lock(struct mt_sch_mgr* mgr) {
int fd = open(MT_FLOCK_PATH, O_RDONLY | O_CREAT, 0666);
if (fd < 0) {
/* sometimes may fail due to user permission, try open read-only */
fd = open(MT_FLOCK_PATH, O_RDONLY);
if (fd < 0) {
err("%s, failed to open %s, %s\n", __func__, MT_FLOCK_PATH, strerror(errno));
return -EIO;
}
}
mgr->lcore_lock_fd = fd;
/* wait until locked */
if (flock(fd, LOCK_EX) != 0) {
err("%s, can not lock file\n", __func__);
close(fd);
mgr->lcore_lock_fd = -1;
return -EIO;
}
return 0;
}
static int sch_filelock_unlock(struct mt_sch_mgr* mgr) {
int fd = mgr->lcore_lock_fd;
if (fd < 0) {
err("%s, wrong lock file fd %d\n", __func__, fd);
return -EIO;
}
if (flock(fd, LOCK_UN) != 0) {
err("%s, can not unlock file\n", __func__);
return -EIO;
}
close(fd);
mgr->lcore_lock_fd = -1;
return 0;
}
static int sch_lcore_shm_init(struct mt_lcore_mgr* mgr, bool clear_on_first) {
struct mt_lcore_shm* lcore_shm = NULL;
int ret;
mgr->lcore_shm_id = -1;
key_t key = ftok("/dev/null", 21);
if (key < 0) {
err("%s, ftok error: %s\n", __func__, strerror(errno));
return -EIO;
}
int shm_id = shmget(key, sizeof(*lcore_shm), 0666 | IPC_CREAT);
if (shm_id < 0) {
err("%s, can not get shared memory for lcore, %s\n", __func__, strerror(errno));
return -EIO;
}
mgr->lcore_shm_id = shm_id;
lcore_shm = shmat(shm_id, NULL, 0);
if (lcore_shm == (void*)-1) {
err("%s, can not attach shared memory for lcore, %s\n", __func__, strerror(errno));
return -EIO;
}
struct shmid_ds stat;
ret = shmctl(shm_id, IPC_STAT, &stat);
if (ret < 0) {
err("%s, shmctl fail\n", __func__);
shmdt(lcore_shm);
return ret;
}
if (clear_on_first && (stat.shm_nattch == 1)) {
info("%s, clear shm as we are the first user\n", __func__);
memset(lcore_shm, 0, sizeof(*lcore_shm));
}
mgr->lcore_shm = lcore_shm;
info("%s, shared memory attached at %p nattch %d shm_id %d key 0x%x\n", __func__,
mgr->lcore_shm, (int)stat.shm_nattch, shm_id, (int)key);
return 0;
}
static int sch_lcore_shm_uinit(struct mt_lcore_mgr* mgr) {
int ret;
if (mgr->lcore_shm) {
ret = shmdt(mgr->lcore_shm);
if (ret < 0) err("%s, shared memory detach failed, %s\n", __func__, strerror(errno));
mgr->lcore_shm = NULL;
}
if (mgr->lcore_shm_id >= 0) {
struct shmid_ds shmds;
ret = shmctl(mgr->lcore_shm_id, IPC_STAT, &shmds);
if (ret < 0) {
err("%s, can not stat shared memory, %s\n", __func__, strerror(errno));
} else {
if (shmds.shm_nattch == 0) { /* remove ipc if we are the last user */
notice("%s, remove shared memory as we are the last user\n", __func__);
ret = shmctl(mgr->lcore_shm_id, IPC_RMID, NULL);
if (ret < 0) {
warn("%s, can not remove shared memory, %s\n", __func__, strerror(errno));
}
}
}
mgr->lcore_shm_id = -1;
}
return 0;
}
static int sch_uinit_lcores(struct mtl_main_impl* impl, struct mt_sch_mgr* mgr) {
int ret;
for (unsigned int lcore = 0; lcore < RTE_MAX_LCORE; lcore++) {
if (mgr->local_lcores_active[lcore]) {
warn("%s, lcore %d still active\n", __func__, lcore);
mt_sch_put_lcore(impl, lcore);
}
}
ret = sch_filelock_lock(mgr);
if (ret < 0) {
err("%s, sch_filelock_lock fail\n", __func__);
return ret;
}
ret = sch_lcore_shm_uinit(&mgr->lcore_mgr);
if (ret < 0) {
err("%s, lcore shm uinit fail %d\n", __func__, ret);
}
ret = sch_filelock_unlock(mgr);
if (ret < 0) {
err("%s, sch_filelock_unlock fail\n", __func__);
return ret;
}
return 0;
}
static int sch_init_lcores(struct mt_sch_mgr* mgr) {
int ret;
if (mgr->lcore_mgr.lcore_shm) {
err("%s, lcore_shm attached\n", __func__);
return -EIO;
}
ret = sch_filelock_lock(mgr);
if (ret < 0) {
err("%s, sch_filelock_lock fail %d\n", __func__, ret);
return ret;
}
ret = sch_lcore_shm_init(&mgr->lcore_mgr, true);
if (ret < 0) {
err("%s, lcore init fail %d\n", __func__, ret);
sch_filelock_unlock(mgr);
return ret;
}
ret = sch_filelock_unlock(mgr);
if (ret < 0) {
err("%s, sch_filelock_unlock fail %d\n", __func__, ret);
return ret;
}
return 0;
}
static inline bool sch_socket_match(int cpu_socket, int dev_socket,
bool skip_numa_check) {
if (skip_numa_check) return true;
return mt_socket_match(cpu_socket, dev_socket);
}
int mt_sch_get_lcore(struct mtl_main_impl* impl, unsigned int* lcore,
enum mt_lcore_type type, int socket) {
unsigned int cur_lcore;
int ret;
bool skip_numa_check = false;
struct mt_sch_mgr* mgr = mt_sch_get_mgr(impl);
int tried = 0;
if (mt_user_not_bind_numa(impl)) skip_numa_check = true;
again:
cur_lcore = 0;
if (mt_is_manager_connected(impl)) {
do {
cur_lcore = rte_get_next_lcore(cur_lcore, 1, 0);
if ((cur_lcore < RTE_MAX_LCORE) &&
sch_socket_match(rte_lcore_to_socket_id(cur_lcore), socket, skip_numa_check)) {
ret = mt_instance_get_lcore(impl, cur_lcore);
if (ret == 0) {
*lcore = cur_lcore;
rte_atomic32_inc(&impl->lcore_cnt);
/* set local lcores info */
mgr->local_lcores_active[cur_lcore] = true;
mgr->local_lcores_type[cur_lcore] = type;
info("%s, succ on manager lcore %d for %s socket %d\n", __func__, cur_lcore,
lcore_type_name(type), socket);
return 0;
}
}
tried++;
} while (cur_lcore < RTE_MAX_LCORE);
} else {
struct mt_user_info* info = &impl->u_info;
struct mt_lcore_shm* lcore_shm = mgr->lcore_mgr.lcore_shm;
struct mt_lcore_shm_entry* shm_entry;
ret = sch_filelock_lock(mgr);
if (ret < 0) {
err("%s, sch_filelock_lock fail\n", __func__);
return ret;
}
do {
cur_lcore = rte_get_next_lcore(cur_lcore, 1, 0);
shm_entry = &lcore_shm->lcores_info[cur_lcore];
if ((cur_lcore < RTE_MAX_LCORE) &&
sch_socket_match(rte_lcore_to_socket_id(cur_lcore), socket, skip_numa_check)) {
if (!shm_entry->active) {
*lcore = cur_lcore;
shm_entry->active = true;
struct mt_user_info* u_info = &shm_entry->u_info;
strncpy(u_info->hostname, info->hostname, sizeof(u_info->hostname));
strncpy(u_info->user, info->user, sizeof(u_info->user));
strncpy(u_info->comm, info->comm, sizeof(u_info->comm));
shm_entry->type = type;
shm_entry->pid = info->pid;
lcore_shm->used++;
rte_atomic32_inc(&impl->lcore_cnt);
/* set local lcores info */
mgr->local_lcores_active[cur_lcore] = true;
mgr->local_lcores_type[cur_lcore] = type;
ret = sch_filelock_unlock(mgr);
info("%s, succ on shm lcore %d for %s socket %d\n", __func__, cur_lcore,
lcore_type_name(type), socket);
if (ret < 0) {
err("%s, sch_filelock_unlock fail\n", __func__);
return ret;
}
return 0;
}
}
tried++;
} while (cur_lcore < RTE_MAX_LCORE);
sch_filelock_unlock(mgr);
}
if (!skip_numa_check && mt_user_across_numa_core(impl)) {
warn("%s, can't find available lcore from socket %d, try with other numa cpu\n",
__func__, socket);
skip_numa_check = true;
goto again;
}
err("%s, no available lcore, type %s tried %d\n", __func__, lcore_type_name(type),
tried);
return -EIO;
}
int mt_sch_put_lcore(struct mtl_main_impl* impl, unsigned int lcore) {
int ret;
struct mt_sch_mgr* mgr = mt_sch_get_mgr(impl);
if (mt_is_manager_connected(impl)) {
ret = mt_instance_put_lcore(impl, lcore);
if (ret == 0) {
rte_atomic32_dec(&impl->lcore_cnt);
mgr->local_lcores_active[lcore] = false;
info("%s, succ on manager lcore %d for %s\n", __func__, lcore,
lcore_type_name(mgr->local_lcores_type[lcore]));
return 0;
} else if (lcore >= RTE_MAX_LCORE) {
err("%s, err %d on manager invalid lcore %d\n", __func__, ret, lcore);
return -EIO;
} else {
err("%s, err %d on manager lcore %d for %s\n", __func__, ret, lcore,
lcore_type_name(mgr->local_lcores_type[lcore]));
return ret;
}
} else {
struct mt_lcore_shm* lcore_shm = mgr->lcore_mgr.lcore_shm;
if (lcore >= RTE_MAX_LCORE) {
err("%s, invalid lcore %d\n", __func__, lcore);
return -EIO;
}
if (!lcore_shm) {
err("%s, no lcore shm attached\n", __func__);
return -EIO;
}
ret = sch_filelock_lock(mgr);
if (ret < 0) {
err("%s, sch_filelock_lock fail\n", __func__);
return ret;
}
if (!lcore_shm->lcores_info[lcore].active) {
err("%s, lcore %d not active\n", __func__, lcore);
ret = -EIO;
goto err_unlock;
}
lcore_shm->lcores_info[lcore].active = false;
lcore_shm->used--;
rte_atomic32_dec(&impl->lcore_cnt);
mgr->local_lcores_active[lcore] = false;
ret = sch_filelock_unlock(mgr);
info("%s, succ on shm lcore %d for %s\n", __func__, lcore,
lcore_type_name(mgr->local_lcores_type[lcore]));
if (ret < 0) {
err("%s, sch_filelock_unlock fail\n", __func__);
return ret;
}
return 0;
err_unlock:
sch_filelock_unlock(mgr);
}
return ret;
}
bool mt_sch_lcore_valid(struct mtl_main_impl* impl, unsigned int lcore) {
struct mt_sch_mgr* mgr = mt_sch_get_mgr(impl);
struct mt_lcore_shm* lcore_shm = mgr->lcore_mgr.lcore_shm;
if (lcore >= RTE_MAX_LCORE) {
err("%s, invalid lcore %d\n", __func__, lcore);
return -EIO;
}
if (mt_is_manager_connected(impl)) return true;
if (!lcore_shm) {
err("%s, no lcore shm attached\n", __func__);
return -EIO;
}
return lcore_shm->lcores_info[lcore].active;
}
int mtl_sch_unregister_tasklet(mtl_tasklet_handle tasklet) {
struct mtl_sch_impl* sch = tasklet->sch;
int sch_idx = sch->idx;
int idx = tasklet->idx;
sch_lock(sch);
if (sch->tasklet[idx] != tasklet) {
err("%s(%d), invalid tasklet on %d\n", __func__, sch_idx, idx);
sch_unlock(sch);
return -EIO;
}
if (mt_sch_started(sch)) {
int retry = 0;
/* wait sch ack this exit */
dbg("%s(%d), tasklet %s(%d) runtime unregistered\n", __func__, sch_idx, tasklet->name,
idx);
tasklet->ack_exit = false;
tasklet->request_exit = true;
do {
mt_sleep_ms(1);
retry++;
if (retry > 1000) {
err("%s(%d), tasklet %s(%d) runtime unregistered timeout\n", __func__, sch_idx,
tasklet->name, idx);
sch_unlock(sch);
return -EIO;
}
} while (!tasklet->ack_exit);
info("%s(%d), tasklet %s(%d) unregistered, retry %d\n", __func__, sch_idx,
tasklet->name, idx, retry);
/* call the stop for runtime path */
if (tasklet->ops.stop) tasklet->ops.stop(tasklet->ops.priv);
} else {
/* safe to directly remove */
sch->tasklet[idx] = NULL;
info("%s(%d), tasklet %s(%d) unregistered\n", __func__, sch_idx, tasklet->name, idx);
}
mt_rte_free(tasklet);
int max_idx = 0;
for (int i = 0; i < sch->nb_tasklets; i++) {
if (sch->tasklet[i]) max_idx = i + 1;
}
sch->max_tasklet_idx = max_idx;
sch_unlock(sch);
return 0;
}
mtl_tasklet_handle mtl_sch_register_tasklet(struct mtl_sch_impl* sch,
struct mtl_tasklet_ops* tasklet_ops) {
int idx = sch->idx;
struct mt_sch_tasklet_impl* tasklet;
sch_lock(sch);
/* find one empty slot in the mgr */
for (int i = 0; i < sch->nb_tasklets; i++) {
if (sch->tasklet[i]) continue;
/* find one empty tasklet slot */
tasklet = mt_rte_zmalloc_socket(sizeof(*tasklet), mt_sch_socket_id(sch));
if (!tasklet) {
err("%s(%d), tasklet malloc fail on %d\n", __func__, idx, i);
sch_unlock(sch);
return NULL;
}
tasklet->ops = *tasklet_ops;
snprintf(tasklet->name, ST_MAX_NAME_LEN - 1, "%s", tasklet_ops->name);
tasklet->sch = sch;
tasklet->idx = i;
mt_stat_u64_init(&tasklet->stat_time);
sch->tasklet[i] = tasklet;
sch->max_tasklet_idx = RTE_MAX(sch->max_tasklet_idx, i + 1);
if (mt_sch_started(sch)) {
if (tasklet_ops->start) tasklet_ops->start(tasklet_ops->priv);
}
sch_unlock(sch);
info("%s(%d), tasklet %s registered into slot %d\n", __func__, idx, tasklet_ops->name,
i);
return tasklet;
}
err("%s(%d), no space on this sch, max %d\n", __func__, idx, sch->nb_tasklets);
sch_unlock(sch);
return NULL;
}
int mt_sch_mrg_init(struct mtl_main_impl* impl, int data_quota_mbs_limit) {
struct mtl_sch_impl* sch;
struct mt_sch_mgr* mgr = mt_sch_get_mgr(impl);
int ret;
mt_pthread_mutex_init(&mgr->mgr_mutex, NULL);
mgr->lcore_lock_fd = -1;
if (!mt_is_manager_connected(impl)) {
ret = sch_init_lcores(mgr);
if (ret < 0) return ret;
}
for (int sch_idx = 0; sch_idx < MT_MAX_SCH_NUM; sch_idx++) {
sch = mt_sch_instance(impl, sch_idx);
mt_pthread_mutex_init(&sch->mutex, NULL);
sch->parent = impl;
sch->idx = sch_idx;
rte_atomic32_set(&sch->started, 0);
rte_atomic32_set(&sch->ref_cnt, 0);
rte_atomic32_set(&sch->active, 0);
sch->max_tasklet_idx = 0;
sch->data_quota_mbs_total = 0;
sch->data_quota_mbs_limit = data_quota_mbs_limit;
sch->run_in_thread = mt_user_tasklet_thread(impl);
mt_stat_u64_init(&sch->stat_time);
/* sleep info init */
sch->allow_sleep = mt_user_tasklet_sleep(impl);
mt_pthread_cond_wait_init(&sch->sleep_wake_cond);
mt_pthread_mutex_init(&sch->sleep_wake_mutex, NULL);
sch->stat_sleep_ns_min = -1;
/* init mgr lock for video */
mt_pthread_mutex_init(&sch->tx_video_mgr_mutex, NULL);
mt_pthread_mutex_init(&sch->rx_video_mgr_mutex, NULL);
/* init mgr lock for audio */
mt_pthread_mutex_init(&sch->tx_a_mgr_mutex, NULL);
mt_pthread_mutex_init(&sch->rx_a_mgr_mutex, NULL);
/* init mgr lock for anc */
mt_pthread_mutex_init(&sch->tx_anc_mgr_mutex, NULL);
mt_pthread_mutex_init(&sch->rx_anc_mgr_mutex, NULL);
/* init mgr lock for fmd */
mt_pthread_mutex_init(&sch->tx_fmd_mgr_mutex, NULL);
mt_pthread_mutex_init(&sch->rx_fmd_mgr_mutex, NULL);
mt_stat_register(impl, sch_stat, sch, "sch");
}
info("%s, succ with data quota %d M\n", __func__, data_quota_mbs_limit);
return 0;
}
int mt_sch_mrg_uinit(struct mtl_main_impl* impl) {
struct mtl_sch_impl* sch;
struct mt_sch_mgr* mgr = mt_sch_get_mgr(impl);
if (!mt_is_manager_connected(impl)) sch_uinit_lcores(impl, mgr);
for (int sch_idx = 0; sch_idx < MT_MAX_SCH_NUM; sch_idx++) {
sch = mt_sch_instance(impl, sch_idx);
if (mt_sch_is_active(sch)) {
warn("%s(%d), sch:%s still active\n", __func__, sch_idx, sch->name);
mtl_sch_free(sch);
}
mt_stat_unregister(impl, sch_stat, sch);
mt_pthread_mutex_destroy(&sch->tx_video_mgr_mutex);
mt_pthread_mutex_destroy(&sch->rx_video_mgr_mutex);
mt_pthread_mutex_destroy(&sch->tx_a_mgr_mutex);
mt_pthread_mutex_destroy(&sch->rx_a_mgr_mutex);
mt_pthread_mutex_destroy(&sch->tx_anc_mgr_mutex);
mt_pthread_mutex_destroy(&sch->rx_anc_mgr_mutex);
mt_pthread_mutex_destroy(&sch->tx_fmd_mgr_mutex);
mt_pthread_mutex_destroy(&sch->rx_fmd_mgr_mutex);
mt_pthread_mutex_destroy(&sch->sleep_wake_mutex);
mt_pthread_cond_destroy(&sch->sleep_wake_cond);
mt_pthread_mutex_destroy(&sch->mutex);
}
mt_pthread_mutex_destroy(&mgr->mgr_mutex);
return 0;
};