-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmargo-core.c
2447 lines (2079 loc) · 78.8 KB
/
margo-core.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
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
/*
* (C) 2015 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <abt.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <json-c/json.h>
#include "margo.h"
#include "margo-abt-macros.h"
#include "margo-globals.h"
#include "margo-progress.h"
#include "margo-monitoring-internal.h"
#include "margo-handle-cache.h"
#include "margo-logging.h"
#include "margo-instance.h"
#include "margo-bulk-util.h"
#include "margo-timer-private.h"
#include "margo-serialization.h"
#include "margo-id.h"
#include "utlist.h"
#include "uthash.h"
#include "abtx_prof.h"
/* need endian macro shims for MacOS */
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#endif /* __APPLE__ */
static void margo_rpc_data_free(void* ptr);
static hg_id_t margo_register_internal(margo_instance_id mid,
const char* name,
hg_id_t id,
hg_proc_cb_t in_proc_cb,
hg_proc_cb_t out_proc_cb,
hg_rpc_cb_t rpc_cb,
ABT_pool pool);
static hg_return_t check_error_in_output(hg_handle_t out);
static hg_return_t check_parent_id_in_input(hg_handle_t handle,
hg_id_t* parent_id);
margo_instance_id margo_init(const char* addr_str,
int mode,
int use_progress_thread,
int rpc_thread_count)
{
char config[1024];
snprintf(config, 1024,
"{ \"use_progress_thread\" : %s, \"rpc_thread_count\" : %d }",
use_progress_thread ? "true" : "false", rpc_thread_count);
struct margo_init_info args = {0};
args.json_config = config;
return margo_init_ext(addr_str, mode, &args);
}
// LCOV_EXCL_START
margo_instance_id margo_init_opt(const char* addr_str,
int mode,
const struct hg_init_info* hg_init_info,
int use_progress_thread,
int rpc_thread_count)
{
char config[1024];
snprintf(config, 1024,
"{ \"use_progress_thread\" : %s, \"rpc_thread_count\" : %d }",
use_progress_thread ? "true" : "false", rpc_thread_count);
struct margo_init_info args = {0};
args.json_config = config;
args.hg_init_info = (struct hg_init_info*)hg_init_info;
return margo_init_ext(addr_str, mode, &args);
}
// LCOV_EXCL_END
// LCOV_EXCL_START
margo_instance_id margo_init_pool(ABT_pool progress_pool,
ABT_pool rpc_pool,
hg_context_t* hg_context)
{
struct margo_init_info args = {0};
hg_class_t* hg_class = HG_Context_get_class(hg_context);
args.hg_class = hg_class;
args.hg_context = hg_context;
args.progress_pool = progress_pool;
args.rpc_pool = rpc_pool;
hg_bool_t listening = HG_Class_is_listening(hg_class);
return margo_init_ext(NULL, listening, &args);
}
// LCOV_EXCL_END
static void margo_call_finalization_callbacks(margo_instance_id mid)
{
/* call finalize callbacks */
MARGO_TRACE(mid, "Calling finalize callbacks");
struct margo_finalize_cb* fcb = mid->finalize_cb;
while (fcb) {
mid->finalize_cb = fcb->next;
(fcb->callback)(fcb->uargs);
struct margo_finalize_cb* tmp = fcb;
fcb = mid->finalize_cb;
free(tmp);
}
}
static void margo_cleanup(margo_instance_id mid)
{
MARGO_TRACE(mid, "Entering margo_cleanup");
struct margo_registered_rpc* next_rpc;
/* monitoring */
struct margo_monitor_finalize_args monitoring_args = {0};
__MARGO_MONITOR(mid, FN_START, finalize, monitoring_args);
margo_deregister(mid, mid->shutdown_rpc_id);
margo_deregister(mid, mid->identity_rpc_id);
/* Start with the handle cache, to clean up any Mercury-related
* data */
MARGO_TRACE(mid, "Destroying handle cache");
__margo_handle_cache_destroy(mid);
if (mid->abt_profiling_enabled) {
MARGO_TRACE(mid, "Dumping ABT profile");
margo_dump_abt_profiling(mid, "margo-profile", 1, NULL);
}
/* finalize Mercury before anything else because this
* could trigger some margo_cb for forward operations that
* have not completed yet (cancelling them) */
MARGO_TRACE(mid, "Destroying Mercury environment");
__margo_hg_destroy(&(mid->hg));
MARGO_TRACE(mid, "Cleaning up RPC data");
while (mid->registered_rpcs) {
next_rpc = mid->registered_rpcs->next;
free(mid->registered_rpcs);
mid->registered_rpcs = next_rpc;
}
/* shut down pending timers */
MARGO_TRACE(mid, "Cleaning up pending timers");
__margo_timer_list_free(mid);
MARGO_TRACE(mid, "Destroying mutex and condition variables");
ABT_mutex_free(&mid->finalize_mutex);
ABT_cond_free(&mid->finalize_cond);
ABT_mutex_free(&mid->pending_operations_mtx);
ABT_key_free(&(mid->current_rpc_id_key));
/* monitoring (destroyed before Argobots since it contains mutexes) */
__MARGO_MONITOR(mid, FN_END, finalize, monitoring_args);
MARGO_TRACE(mid, "Destroying monitoring context");
if (mid->monitor && mid->monitor->finalize)
mid->monitor->finalize(mid->monitor->uargs);
free(mid->monitor);
free(mid->plumber_bucket_policy);
free(mid->plumber_nic_policy);
MARGO_TRACE(mid, "Destroying Argobots environment");
__margo_abt_destroy(&(mid->abt));
free(mid);
MARGO_TRACE(0, "Completed margo_cleanup");
}
hg_return_t margo_instance_ref_incr(margo_instance_id mid)
{
if (!mid) return HG_INVALID_ARG;
mid->refcount++;
return HG_SUCCESS;
}
hg_return_t margo_instance_ref_count(margo_instance_id mid, unsigned* refcount)
{
if (!mid) return HG_INVALID_ARG;
*refcount = mid->refcount;
return HG_SUCCESS;
}
hg_return_t margo_instance_release(margo_instance_id mid)
{
if (!mid) return HG_INVALID_ARG;
if (!mid->refcount) return HG_OTHER_ERROR;
unsigned refcount = --mid->refcount;
if (refcount == 0) {
if (!mid->finalize_flag) {
++mid->refcount; // needed because margo_finalize will itself
// decrease it back to 0
margo_finalize(mid);
} else {
margo_cleanup(mid);
}
}
return HG_SUCCESS;
}
hg_return_t margo_instance_is_finalized(margo_instance_id mid, bool* flag)
{
if (!mid) return HG_INVALID_ARG;
*flag = mid->finalize_flag;
return HG_SUCCESS;
}
void margo_finalize(margo_instance_id mid)
{
MARGO_TRACE(mid, "Calling margo_finalize");
int do_cleanup;
/* check if there are pending operations */
int pending;
ABT_mutex_lock(mid->pending_operations_mtx);
pending = mid->pending_operations;
if (pending) {
mid->finalize_requested = 1;
ABT_mutex_unlock(mid->pending_operations_mtx);
MARGO_TRACE(mid, "Pending operations, exiting margo_finalize");
return;
}
ABT_mutex_unlock(mid->pending_operations_mtx);
MARGO_TRACE(mid, "Executing pre-finalize callbacks");
/* before exiting the progress loop, pre-finalize callbacks need to be
* called */
/* monitoring */
struct margo_monitor_prefinalize_args monitoring_args = {0};
__MARGO_MONITOR(mid, FN_START, prefinalize, monitoring_args);
struct margo_finalize_cb* fcb = mid->prefinalize_cb;
while (fcb) {
mid->prefinalize_cb = fcb->next;
(fcb->callback)(fcb->uargs);
struct margo_finalize_cb* tmp = fcb;
fcb = mid->prefinalize_cb;
free(tmp);
}
/* monitoring */
__MARGO_MONITOR(mid, FN_END, prefinalize, monitoring_args);
/* tell progress thread to wrap things up */
mid->hg_progress_shutdown_flag = 1;
PROGRESS_NEEDED_INCR(mid);
/* wait for it to shutdown cleanly */
MARGO_TRACE(mid, "Waiting for progress thread to complete");
ABT_thread_join(mid->hg_progress_tid);
ABT_thread_free(&mid->hg_progress_tid);
PROGRESS_NEEDED_DECR(mid);
mid->refcount--;
ABT_mutex_lock(mid->finalize_mutex);
mid->finalize_flag = true;
margo_call_finalization_callbacks(mid);
do_cleanup = mid->finalize_refcount == 0 && mid->refcount == 0;
ABT_mutex_unlock(mid->finalize_mutex);
ABT_cond_broadcast(mid->finalize_cond);
/* if there was noone waiting on the finalize at the time of the finalize
* broadcast, then we're safe to clean up. Otherwise, let the finalizer do
* it */
if (do_cleanup) margo_cleanup(mid);
MARGO_TRACE(NULL, "Finalize completed");
return;
}
void margo_finalize_and_wait(margo_instance_id mid)
{
MARGO_TRACE(mid, "Start to finalize and wait");
int do_cleanup;
ABT_mutex_lock(mid->finalize_mutex);
mid->finalize_requested = 1;
mid->finalize_refcount++;
ABT_mutex_unlock(mid->finalize_mutex);
// try finalizing
margo_finalize(mid);
ABT_mutex_lock(mid->finalize_mutex);
while (!mid->finalize_flag)
ABT_cond_wait(mid->finalize_cond, mid->finalize_mutex);
mid->finalize_refcount--;
do_cleanup = mid->finalize_refcount == 0 && mid->refcount == 0;
ABT_mutex_unlock(mid->finalize_mutex);
if (do_cleanup) margo_cleanup(mid);
MARGO_TRACE(NULL, "Done finalizing and waiting");
return;
}
void margo_wait_for_finalize(margo_instance_id mid)
{
MARGO_TRACE(mid, "Start waiting for finalize");
int do_cleanup;
ABT_mutex_lock(mid->finalize_mutex);
mid->finalize_refcount++;
while (!mid->finalize_flag)
ABT_cond_wait(mid->finalize_cond, mid->finalize_mutex);
mid->finalize_refcount--;
do_cleanup = mid->finalize_refcount == 0 && mid->refcount == 0;
ABT_mutex_unlock(mid->finalize_mutex);
if (do_cleanup) margo_cleanup(mid);
MARGO_TRACE(NULL, "Done waiting for finalize");
return;
}
hg_bool_t margo_is_listening(margo_instance_id mid)
{
if (!mid) return HG_FALSE;
return HG_Class_is_listening(mid->hg.hg_class);
}
void margo_push_prefinalize_callback(margo_instance_id mid,
margo_finalize_callback_t cb,
void* uargs)
{
margo_provider_push_prefinalize_callback(mid, NULL, cb, uargs);
}
int margo_pop_prefinalize_callback(margo_instance_id mid)
{
return margo_provider_pop_prefinalize_callback(mid, NULL);
}
int margo_top_prefinalize_callback(margo_instance_id mid,
margo_finalize_callback_t* cb,
void** uargs)
{
return margo_provider_top_prefinalize_callback(mid, NULL, cb, uargs);
}
void margo_provider_push_prefinalize_callback(margo_instance_id mid,
const void* owner,
margo_finalize_callback_t cb,
void* uargs)
{
if (cb == NULL) return;
struct margo_finalize_cb* fcb
= (struct margo_finalize_cb*)malloc(sizeof(*fcb));
fcb->owner = owner;
fcb->callback = cb;
fcb->uargs = uargs;
struct margo_finalize_cb* next = mid->prefinalize_cb;
fcb->next = next;
mid->prefinalize_cb = fcb;
}
int margo_provider_top_prefinalize_callback(margo_instance_id mid,
const void* owner,
margo_finalize_callback_t* cb,
void** uargs)
{
struct margo_finalize_cb* fcb = mid->prefinalize_cb;
while (fcb != NULL && fcb->owner != owner) { fcb = fcb->next; }
if (fcb == NULL) return 0;
if (cb) *cb = fcb->callback;
if (uargs) *uargs = fcb->uargs;
return 1;
}
int margo_provider_pop_prefinalize_callback(margo_instance_id mid,
const void* owner)
{
struct margo_finalize_cb* prev = NULL;
struct margo_finalize_cb* fcb = mid->prefinalize_cb;
while (fcb != NULL && fcb->owner != owner) {
prev = fcb;
fcb = fcb->next;
}
if (fcb == NULL) return 0;
if (prev == NULL) {
mid->prefinalize_cb = fcb->next;
} else {
prev->next = fcb->next;
}
free(fcb);
return 1;
}
void margo_push_finalize_callback(margo_instance_id mid,
margo_finalize_callback_t cb,
void* uargs)
{
margo_provider_push_finalize_callback(mid, NULL, cb, uargs);
}
int margo_pop_finalize_callback(margo_instance_id mid)
{
return margo_provider_pop_finalize_callback(mid, NULL);
}
int margo_top_finalize_callback(margo_instance_id mid,
margo_finalize_callback_t* cb,
void** uargs)
{
return margo_provider_top_finalize_callback(mid, NULL, cb, uargs);
}
void margo_provider_push_finalize_callback(margo_instance_id mid,
const void* owner,
margo_finalize_callback_t cb,
void* uargs)
{
if (cb == NULL) return;
struct margo_finalize_cb* fcb
= (struct margo_finalize_cb*)malloc(sizeof(*fcb));
fcb->owner = owner;
fcb->callback = cb;
fcb->uargs = uargs;
struct margo_finalize_cb* next = mid->finalize_cb;
fcb->next = next;
mid->finalize_cb = fcb;
}
int margo_provider_pop_finalize_callback(margo_instance_id mid,
const void* owner)
{
struct margo_finalize_cb* prev = NULL;
struct margo_finalize_cb* fcb = mid->finalize_cb;
while (fcb != NULL && fcb->owner != owner) {
prev = fcb;
fcb = fcb->next;
}
if (fcb == NULL) return 0;
if (prev == NULL) {
mid->finalize_cb = fcb->next;
} else {
prev->next = fcb->next;
}
free(fcb);
return 1;
}
int margo_provider_top_finalize_callback(margo_instance_id mid,
const void* owner,
margo_finalize_callback_t* cb,
void** uargs)
{
struct margo_finalize_cb* fcb = mid->finalize_cb;
while (fcb != NULL && fcb->owner != owner) { fcb = fcb->next; }
if (fcb == NULL) return 0;
if (cb) *cb = fcb->callback;
if (uargs) *uargs = fcb->uargs;
return 1;
}
void margo_enable_remote_shutdown(margo_instance_id mid)
{
mid->enable_remote_shutdown = 1;
}
int margo_shutdown_remote_instance(margo_instance_id mid, hg_addr_t remote_addr)
{
hg_return_t hret;
hg_handle_t handle;
hret = margo_create(mid, remote_addr, mid->shutdown_rpc_id, &handle);
if (hret != HG_SUCCESS) return -1;
hret = margo_forward(handle, NULL);
if (hret != HG_SUCCESS) {
// LCOV_EXCL_START
margo_destroy(handle);
return -1;
// LCOV_EXCL_END
}
margo_shutdown_out_t out;
hret = margo_get_output(handle, &out);
if (hret != HG_SUCCESS) {
// LCOV_EXCL_START
margo_destroy(handle);
return -1;
// LCOV_EXCL_END
}
margo_free_output(handle, &out);
margo_destroy(handle);
return out.ret;
}
hg_id_t margo_provider_register_name(margo_instance_id mid,
const char* func_name,
hg_proc_cb_t in_proc_cb,
hg_proc_cb_t out_proc_cb,
hg_rpc_cb_t rpc_cb,
uint16_t provider_id,
ABT_pool pool)
{
hg_id_t id;
struct margo_registered_rpc* tmp_rpc;
if (!rpc_cb) rpc_cb = _handler_for_NULL;
id = gen_id(func_name, provider_id);
/* track information about this rpc registration for debugging and
* profiling
* NOTE: we do this even if profiling is currently disabled; it may be
* enabled later on at run time.
*/
tmp_rpc = calloc(1, sizeof(*tmp_rpc));
if (!tmp_rpc) return (0);
strncpy(tmp_rpc->func_name, func_name, 63);
tmp_rpc->id = id;
tmp_rpc->next = mid->registered_rpcs;
mid->registered_rpcs = tmp_rpc;
mid->num_registered_rpcs++;
id = margo_register_internal(mid, func_name, id, in_proc_cb, out_proc_cb,
rpc_cb, pool);
if (id == 0) {
mid->registered_rpcs = tmp_rpc->next;
free(tmp_rpc);
mid->num_registered_rpcs--;
return (id);
}
return (id);
}
hg_return_t margo_deregister(margo_instance_id mid, hg_id_t rpc_id)
{
/* monitoring */
struct margo_monitor_deregister_args monitoring_args
= {.id = rpc_id, .ret = HG_SUCCESS};
__MARGO_MONITOR(mid, FN_START, deregister, monitoring_args);
/* get data */
struct margo_rpc_data* data
= (struct margo_rpc_data*)HG_Registered_data(mid->hg.hg_class, rpc_id);
if (data) {
/* decrement the numner of RPC id used by the pool */
__margo_abt_lock(&mid->abt);
int32_t index = __margo_abt_find_pool_by_handle(&mid->abt, data->pool);
if (index >= 0) mid->abt.pools[index].refcount--;
__margo_abt_unlock(&mid->abt);
}
/* deregister */
hg_return_t hret = HG_Deregister(mid->hg.hg_class, rpc_id);
/* monitoring */
monitoring_args.ret = hret;
__MARGO_MONITOR(mid, FN_END, deregister, monitoring_args);
return hret;
}
hg_return_t margo_registered_name(margo_instance_id mid,
const char* func_name,
hg_id_t* id,
hg_bool_t* flag)
{
*id = gen_id(func_name, 0);
return (HG_Registered(mid->hg.hg_class, *id, flag));
}
hg_return_t margo_provider_registered_name(margo_instance_id mid,
const char* func_name,
uint16_t provider_id,
hg_id_t* id,
hg_bool_t* flag)
{
*id = gen_id(func_name, provider_id);
return HG_Registered(mid->hg.hg_class, *id, flag);
}
hg_return_t margo_register_data(margo_instance_id mid,
hg_id_t id,
void* data,
void (*free_callback)(void*))
{
struct margo_rpc_data* margo_data
= (struct margo_rpc_data*)HG_Registered_data(mid->hg.hg_class, id);
if (!margo_data) return HG_OTHER_ERROR;
if (margo_data->user_data && margo_data->user_free_callback) {
(margo_data->user_free_callback)(margo_data->user_data);
}
margo_data->user_data = data;
margo_data->user_free_callback = free_callback;
return HG_SUCCESS;
}
void* margo_registered_data(margo_instance_id mid, hg_id_t id)
{
struct margo_rpc_data* data
= (struct margo_rpc_data*)HG_Registered_data(margo_get_class(mid), id);
if (!data)
return NULL;
else
return data->user_data;
}
hg_return_t margo_registered_disable_response(margo_instance_id mid,
hg_id_t id,
int disable_flag)
{
return (HG_Registered_disable_response(mid->hg.hg_class, id, disable_flag));
}
hg_return_t margo_registered_disabled_response(margo_instance_id mid,
hg_id_t id,
int* disabled_flag)
{
hg_bool_t b;
hg_return_t ret = HG_Registered_disabled_response(mid->hg.hg_class, id, &b);
if (ret != HG_SUCCESS) return ret;
*disabled_flag = b;
return HG_SUCCESS;
}
/* Mercury 2.x provides two versions of lookup (async and sync). If a
* synchronous lookup call is available then we do not need this callback.
*/
#ifndef HG_Addr_lookup
static hg_return_t margo_addr_lookup_cb(const struct hg_cb_info* info)
{
struct lookup_cb_evt evt;
evt.hret = info->ret;
evt.addr = info->info.lookup.addr;
ABT_eventual eventual = (ABT_eventual)(info->arg);
/* propagate return code out through eventual */
ABT_eventual_set(eventual, &evt, sizeof(evt));
return (HG_SUCCESS);
}
#endif
hg_return_t
margo_addr_lookup(margo_instance_id mid, const char* name, hg_addr_t* addr)
{
hg_return_t hret;
#ifdef HG_Addr_lookup
/* monitoring */
struct margo_monitor_lookup_args monitoring_args
= {.name = name, .addr = HG_ADDR_NULL, .ret = HG_SUCCESS};
__MARGO_MONITOR(mid, FN_START, lookup, monitoring_args);
/* Mercury 2.x provides two versions of lookup (async and sync). Choose the
* former if available to avoid context switch
*/
hret = HG_Addr_lookup2(mid->hg.hg_class, name, addr);
/* monitoring */
monitoring_args.addr = addr ? *addr : HG_ADDR_NULL;
monitoring_args.ret = hret;
__MARGO_MONITOR(mid, FN_END, lookup, monitoring_args);
#else /* !defined HG_Addr_lookup */
struct lookup_cb_evt* evt;
ABT_eventual eventual;
int ret;
ret = ABT_eventual_create(sizeof(*evt), &eventual);
if (ret != 0) { return (HG_NOMEM_ERROR); }
hret = HG_Addr_lookup(mid->hg_context, margo_addr_lookup_cb,
(void*)eventual, name, HG_OP_ID_IGNORE);
PROGRESS_NEEDED_INCR(mid);
if (hret == HG_SUCCESS) {
ABT_eventual_wait(eventual, (void**)&evt);
*addr = evt->addr;
hret = evt->hret;
}
PROGRESS_NEEDED_DECR(mid);
ABT_eventual_free(&eventual);
#endif
return (hret);
}
hg_return_t margo_addr_free(margo_instance_id mid, hg_addr_t addr)
{
return (HG_Addr_free(mid->hg.hg_class, addr));
}
hg_return_t margo_addr_self(margo_instance_id mid, hg_addr_t* addr)
{
hg_return_t hret = HG_SUCCESS;
/* monitoring */
struct margo_monitor_lookup_args monitoring_args
= {.name = NULL, .addr = HG_ADDR_NULL, .ret = HG_SUCCESS};
__MARGO_MONITOR(mid, FN_START, lookup, monitoring_args);
hret = HG_Addr_self(mid->hg.hg_class, addr);
/* monitoring */
monitoring_args.addr = addr ? *addr : HG_ADDR_NULL;
monitoring_args.ret = hret;
__MARGO_MONITOR(mid, FN_END, lookup, monitoring_args);
return hret;
}
hg_return_t
margo_addr_dup(margo_instance_id mid, hg_addr_t addr, hg_addr_t* new_addr)
{
return (HG_Addr_dup(mid->hg.hg_class, addr, new_addr));
}
hg_bool_t
margo_addr_cmp(margo_instance_id mid, hg_addr_t addr1, hg_addr_t addr2)
{
return HG_Addr_cmp(mid->hg.hg_class, addr1, addr2);
}
hg_return_t margo_addr_set_remove(margo_instance_id mid, hg_addr_t addr)
{
return HG_Addr_set_remove(mid->hg.hg_class, addr);
}
hg_return_t margo_addr_to_string(margo_instance_id mid,
char* buf,
hg_size_t* buf_size,
hg_addr_t addr)
{
return (HG_Addr_to_string(mid->hg.hg_class, buf, buf_size, addr));
}
hg_return_t margo_create(margo_instance_id mid,
hg_addr_t addr,
hg_id_t id,
hg_handle_t* handle)
{
hg_return_t hret = HG_OTHER_ERROR;
/* monitoring */
struct margo_monitor_create_args monitoring_args
= {.addr = addr, .id = id, .handle = HG_HANDLE_NULL, .ret = HG_SUCCESS};
__MARGO_MONITOR(mid, FN_START, create, monitoring_args);
/* look for a handle to reuse */
hret = __margo_handle_cache_get(mid, addr, id, handle);
if (hret != HG_SUCCESS) {
/* else try creating a new handle */
hret = HG_Create(mid->hg.hg_context, addr, id, handle);
}
if (hret != HG_SUCCESS) goto finish;
hret = __margo_internal_set_handle_data(*handle);
finish:
/* monitoring */
monitoring_args.handle = handle ? *handle : HG_HANDLE_NULL;
monitoring_args.ret = hret;
__MARGO_MONITOR(mid, FN_END, create, monitoring_args);
return hret;
}
hg_return_t margo_destroy(hg_handle_t handle)
{
if (handle == HG_HANDLE_NULL) return HG_SUCCESS;
/* check if the reference count of the handle is 1 */
int32_t refcount = HG_Ref_get(handle);
if (refcount != 1) {
/* if different from 1, then HG_Destroy will simply decrease it */
return HG_Destroy(handle);
}
margo_instance_id mid;
hg_return_t hret = HG_OTHER_ERROR;
/* use the handle to get the associated mid
* Note: we need to do that before cleaning handle_data */
mid = margo_hg_handle_get_instance(handle);
/* monitoring */
struct margo_monitor_destroy_args monitoring_args
= {.handle = handle, .ret = HG_SUCCESS};
__MARGO_MONITOR(mid, FN_START, destroy, monitoring_args);
/* remove the margo_handle_data associated with the handle */
struct margo_handle_data* handle_data = HG_Get_data(handle);
if (handle_data) {
if (handle_data->user_free_callback) {
handle_data->user_free_callback(handle_data->user_data);
}
memset(handle_data, 0, sizeof(*handle_data));
}
if (mid) {
/* recycle this handle if it came from the handle cache */
hret = __margo_handle_cache_put(mid, handle);
if (hret != HG_SUCCESS) {
/* else destroy the handle manually and free the handle data */
hret = HG_Destroy(handle);
}
} else {
hret = HG_OTHER_ERROR;
}
/* monitoring */
monitoring_args.ret = hret;
__MARGO_MONITOR(mid, FN_END, destroy, monitoring_args);
return hret;
}
static hg_return_t margo_cb(const struct hg_cb_info* info)
{
hg_return_t hret = info->ret;
margo_request req = (margo_request)(info->arg);
margo_instance_id mid = req->mid;
/* monitoring */
struct margo_monitor_cb_args monitoring_args
= {.info = info, .request = req, .ret = HG_SUCCESS};
switch (info->type) {
case HG_CB_FORWARD:
__MARGO_MONITOR(mid, FN_START, forward_cb, monitoring_args);
break;
case HG_CB_RESPOND:
__MARGO_MONITOR(mid, FN_START, respond_cb, monitoring_args);
break;
case HG_CB_BULK:
__MARGO_MONITOR(mid, FN_START, bulk_transfer_cb, monitoring_args);
break;
default:
break;
};
if (hret == HG_CANCELED && req->timer) { hret = HG_TIMEOUT; }
/* remove timer if there is one and it is still in place */
if (req->timer) {
margo_timer_cancel(req->timer);
margo_timer_destroy(req->timer);
}
if (req->kind == MARGO_REQ_CALLBACK) {
if (req->u.callback.cb) req->u.callback.cb(req->u.callback.uargs, hret);
} else {
req->u.eventual.hret = hret;
MARGO_EVENTUAL_SET(req->u.eventual.ev);
}
/* monitoring */
monitoring_args.ret = hret;
switch (info->type) {
case HG_CB_FORWARD:
__MARGO_MONITOR(mid, FN_END, forward_cb, monitoring_args);
break;
case HG_CB_RESPOND:
__MARGO_MONITOR(mid, FN_END, respond_cb, monitoring_args);
break;
case HG_CB_BULK:
__MARGO_MONITOR(mid, FN_END, bulk_transfer_cb, monitoring_args);
break;
default:
break;
};
// a callback-based request is heap-allocated but is not
// handed to the user, hence it has to be freed here.
if (req->kind == MARGO_REQ_CALLBACK) free(req);
PROGRESS_NEEDED_DECR(mid);
return HG_SUCCESS;
}
static hg_return_t margo_wait_internal(margo_request req)
{
hg_return_t hret = HG_SUCCESS;
if (req->kind != MARGO_REQ_EVENTUAL) // should not happen
return HG_INVALID_ARG;
/* monitoring */
struct margo_monitor_wait_args monitoring_args
= {.request = req, .ret = HG_SUCCESS};
__MARGO_MONITOR(req->mid, FN_START, wait, monitoring_args);
MARGO_EVENTUAL_WAIT(req->u.eventual.ev);
MARGO_EVENTUAL_FREE(&(req->u.eventual.ev));
if (req->u.eventual.hret != HG_SUCCESS) {
hret = req->u.eventual.hret;
goto finish;
}
if (req->type == MARGO_FORWARD_REQUEST)
hret = check_error_in_output(req->handle);
finish:
/* monitoring */
monitoring_args.ret = hret;
__MARGO_MONITOR(req->mid, FN_END, wait, monitoring_args);
return hret;
}
static void margo_forward_timeout_cb(void* arg)
{
margo_request req = (margo_request)arg;
/* cancel the Mercury op if the forward timed out */
HG_Cancel(req->handle);
}
static hg_return_t margo_provider_iforward_internal(
uint16_t provider_id,
hg_handle_t handle,
double timeout_ms,
void* in_struct,
margo_request req) /* the request should have been allocated */
{
hg_return_t hret = HG_TIMEOUT;
int ret;
margo_eventual_t eventual;
const struct hg_info* hgi;
struct margo_handle_data* handle_data;
hg_id_t client_id, server_id;
hg_proc_cb_t in_cb, out_cb;
margo_instance_id mid;
hgi = HG_Get_info(handle);
handle_data = (struct margo_handle_data*)HG_Get_data(handle);
if (!handle_data) {
margo_error(MARGO_INSTANCE_NULL,
"in %s: HG_Get_data failed to return data", __func__);
return HG_NO_MATCH;
}
mid = handle_data->mid;
in_cb = handle_data->in_proc_cb;
out_cb = handle_data->out_proc_cb;
client_id = hgi->id;
server_id = mux_id(client_id, provider_id);
if (!mid) {
margo_error(MARGO_INSTANCE_NULL,
"in %s: handle is not associated"
" with a valid margo instance",
__func__);
return HG_OTHER_ERROR;
}
/* monitoring */
struct margo_monitor_forward_args monitoring_args
= {.provider_id = provider_id,
.handle = handle,
.data = in_struct,
.timeout_ms = timeout_ms,
.request = req,
.ret = HG_SUCCESS};