forked from dspinellis/dgsh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnegotiate.c
2996 lines (2749 loc) · 88.8 KB
/
negotiate.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
/*
* Copyright 2016, 2017 Marios Fragkoulis
*
* A passive component that aids the dgsh negotiation by passing
* message blocks among participating processes.
* When the negotiation is finished and the processes get connected by
* pipes, it exits.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <assert.h> /* assert() */
#include <errno.h> /* ENOBUFS */
#include <err.h> /* err() */
#include <limits.h> /* IOV_MAX */
#include <stdbool.h> /* bool, true, false */
#include <stdio.h> /* fprintf() in DPRINTF() */
#include <stdlib.h> /* getenv(), errno, atexit() */
#include <string.h> /* memcpy() */
#include <sysexits.h> /* EX_PROTOCOL, EX_OK */
#include <sys/socket.h> /* sendmsg(), recvmsg() */
#include <unistd.h> /* getpid(), getpagesize(),
* STDIN_FILENO, STDOUT_FILENO,
* STDERR_FILENO, alarm(), sysconf()
*/
#include <signal.h> /* signal(), SIGALRM */
#include <time.h> /* nanosleep() */
#include <sys/select.h> /* select(), fd_set, */
#include <stdio.h> /* printf family */
#include "negotiate.h" /* Message block and I/O */
#include "dgsh-debug.h" /* DPRINTF() */
#ifdef TIME
#include <time.h>
static struct timespec tstart={0,0}, tend={0,0};
#endif
/* Default negotiation timeout (s) */
#define DGSH_TIMEOUT 5
#ifndef UNIT_TESTING
/* Models an I/O connection between tools on an dgsh graph. */
struct dgsh_edge {
int from; /* Index of node on the graph where data
* comes from (out).
*/
int to; /* Index of node on the graph that
* receives the data (in).
*/
int instances; /* Number of instances of an edge. */
int from_instances; /* Number of instances the origin node of
* an edge can provide.
*/
int to_instances; /* Number of instances the destination
* of an edge can require.
*/
};
#endif
/* Each tool that participates in an dgsh graph is modelled as follows. */
struct dgsh_node {
pid_t pid;
int index; /* Position in message block's node array. */
char name[100]; /* Tool's name */
int requires_channels; /* Input channels it can take. */
int provides_channels; /* Output channels it can provide. */
int dgsh_in; /* Takes input from other tool(s) on
* dgsh graph.
*/
int dgsh_out; /* Provides output to other tool(s)
* on dgsh graph.
*/
};
/* Holds a node's connections. It contains a piece of the solution. */
struct dgsh_node_connections {
int node_index; /* The subject of the
* connections. For
* verification.
*/
struct dgsh_edge *edges_incoming; /* Array of edges through
* which other nodes provide
* input to node at node_index.
*/
int n_edges_incoming; /* Number of incoming edges */
int n_instances_incoming_free; /* Number of incoming edges
* not yet bound to a pair
* node's output channel.
*/
struct dgsh_edge *edges_outgoing; /* Array of edges through
* which a node provides
* output to other nodes.
*/
int n_edges_outgoing; /* Number of outgoing edges */
int n_instances_outgoing_free; /* Number of outgoing edges
* not yet binded to a pair
* node's outgoing edges.
*/
};
/* The output of the negotiation process. */
struct dgsh_node_pipe_fds {
int *input_fds; /* Array of input file descriptors */
int n_input_fds; /* Number of input file descriptors */
int *output_fds; /* Array of output file descriptors */
int n_output_fds; /* Number of output file descriptors */
};
/**
* Memory organisation of message block.
* Message block will be passed around process address spaces.
* Message block contains a number of scalar fields and two pointers
* to an array of dgsh nodes and edges respectively.
* To pass the message block along with nodes and edges, three writes
* in this order take place.
*/
/* The message block implicitly used by many functions */
struct dgsh_negotiation *chosen_mb;
static struct dgsh_node self_node; /* The dgsh node that models
this tool. */
static char *programname;
static struct node_io_side self_node_io_side; /* Dispatch info for this tool. */
static struct dgsh_node_pipe_fds self_pipe_fds; /* A tool's read and write file
* descriptors to use at execution.
*/
static bool init_error = false;
static volatile sig_atomic_t negotiation_completed = 0;
int dgsh_debug_level = 0;
static void get_environment_vars();
static int dgsh_exit(int state, int flags);
/* Force the inclusion of the ELF note section */
extern int dgsh_force_include;
void
dgsh_force_include_function(void)
{
dgsh_force_include = 1;
}
#ifndef UNIT_TESTING
static void
dgsh_exit_handler(void)
{
if (negotiation_completed)
return;
init_error = true;
/* Finish negotiation, if required */
get_environment_vars();
if (self_node.dgsh_in != 0 || self_node.dgsh_out != 0) {
warnx("exiting before dgsh negotiation is complete");
DPRINTF(4, "dgsh: error state. Enter negotiation to inform the graph");
dgsh_negotiate(0, programname ? programname : "dgsh client", NULL,
NULL, NULL, NULL);
}
}
#endif
void
dgsh_alarm_handler(int signal)
{
if (signal == SIGALRM)
if (negotiation_completed == 0) {
char msg[100];
sprintf(msg, "%d dgsh: timeout for negotiation. Exit.\n",
getpid());
negotiation_completed = 1;
write(2, msg, strlen(msg));
_exit(EX_PROTOCOL);
}
}
#ifndef UNIT_TESTING
__attribute__((constructor))
static void
install_exit_handler(void)
{
atexit(dgsh_exit_handler);
}
#endif
static int iov_max;
// Setup iov_max handling runtime, even if it is defined at runtime
__attribute__((constructor))
static void
setup_iov_max(void)
{
#if defined(IOV_MAX)
iov_max = IOV_MAX;
#else
iov_max = (int)sysconf(_SC_IOV_MAX);
#endif
}
/**
* Remove path to command to save space in the graph plot
* Find first space if any and take the name up to there
* Find and remove path prepended to the name
* Rejoin the name with arguments
* Escape double quotes
*/
STATIC void
process_node_name(char *name, char **processed_name)
{
char no_path_name[strlen(name)];
memset(no_path_name, 0, sizeof(no_path_name));
char *s = strstr(name, " ");
DPRINTF(4, "Node name to process: %s", name);
if (s)
strncpy(no_path_name, name, s - name);
else
strcpy(no_path_name, name);
DPRINTF(4, "no_path_name: %s, s: %s", no_path_name, s);
char *p = no_path_name;
char *m = strstr(no_path_name, "/");
while (m) {
p = ++m;
m = strstr(m, "/");
}
DPRINTF(4, "no_path_name: %s, m: %s", no_path_name, m);
if (s)
sprintf(no_path_name, "%s%s", p, s);
DPRINTF(4, "no_path_name: %s, p: %s", no_path_name, p);
m = strstr(no_path_name, "\"");
char *mm = NULL;
while (m) {
DPRINTF(4, "processed_name: %s, m: %s, mm: %s",
*processed_name, m, mm);
if (strlen(*processed_name) == 0)
strncpy(*processed_name, no_path_name, m - no_path_name);
else {
strcat(*processed_name, "\\");
strncat(*processed_name, mm, m - mm);
DPRINTF(4, "processed_name: %s, m - mm: %ld",
*processed_name, (long)(m - mm));
}
mm = m;
m = strstr(++m, "\"");
}
if (mm) {
strcat(*processed_name, "\\");
strcat(*processed_name, mm);
} else
strcpy(*processed_name, no_path_name);
DPRINTF(4, "final processed_name: %s, m: %s, mm: %s",
*processed_name, m, mm);
}
STATIC enum op_result
output_graph(char *filename)
{
char ffilename[strlen(filename) + 5]; // + .dot
sprintf(ffilename, "%s.dot", filename);
FILE *f = fopen(ffilename, "a");
if (f == NULL) {
fprintf(stderr, "Unable to open file %s", ffilename);
return OP_ERROR;
}
char fnfilename[strlen(filename) + 9]; // + -ngt + .dot
sprintf(fnfilename, "%s-ngt.dot", filename);
FILE *fn = fopen(fnfilename, "a");
if (fn == NULL) {
fprintf(stderr, "Unable to open file %s", fnfilename);
return OP_ERROR;
}
int i, j;
int n_nodes = chosen_mb->n_nodes;
struct dgsh_node_connections *graph_solution =
chosen_mb->graph_solution;
DPRINTF(4, "Output graph in file %s for %d nodes and %d edges",
filename, n_nodes, chosen_mb->n_edges);
fprintf(f, "digraph {\n");
fprintf(fn, "digraph {\n");
for (i = 0; i < n_nodes; i++) {
struct dgsh_node *node = &chosen_mb->node_array[i];
struct dgsh_node_connections *connections =
&graph_solution[i];
int n_edges_outgoing = connections->n_edges_outgoing;
DPRINTF(4, "Output node: %s", node->name);
// Reserve space for quotes
int q = 0;
char *m = strstr(node->name, "\"");
while (m) {
q++;
m = strstr(++m, "\"");
}
char *processed_name = (char *)malloc(sizeof(char) *
(strlen(node->name) + q + 1));
DPRINTF(4, "Malloc %d bytes for processed_name",
(int)strlen(node->name) + q + 1);
memset(processed_name, 0, strlen(node->name) + q + 1);
process_node_name(node->name, &processed_name);
#ifdef DEBUG
fprintf(f, " n%d [label=\"%d %s\"];\n",
node->index, node->index,
processed_name);
fprintf(fn, " n%d [label=\"%d %s\"];\n",
node->index, node->index,
processed_name);
#else
fprintf(f, " n%d [label=\"%s\"];\n",
node->index, processed_name);
fprintf(fn, " n%d [label=\"%s\"];\n",
node->index, processed_name);
#endif
DPRINTF(4, "Node: (%d) %s", node->index, processed_name);
free(processed_name);
for (j = 0; j < n_edges_outgoing; j++) {
fprintf(fn, " n%d -> n%d;\n",
node->index,
chosen_mb->node_array[connections->edges_outgoing[j].to].index);
if (connections->edges_outgoing[j].instances == 0)
continue;
fprintf(f, " n%d -> n%d;\n",
node->index,
chosen_mb->node_array[connections->edges_outgoing[j].to].index);
DPRINTF(4, "Edge: (%d) %s -> %s (%d)",
node->index, node->name,
chosen_mb->node_array[connections->edges_outgoing[j].to].name,
chosen_mb->node_array[connections->edges_outgoing[j].to].index);
}
}
fprintf(f, "}\n");
fprintf(fn, "}\n");
fclose(f);
fclose(fn);
return OP_SUCCESS;
}
/**
* Allocate node indexes to store a node's (at node_index)
* node outgoing or incoming connections (nc_edges).
*/
STATIC enum op_result
alloc_node_connections(struct dgsh_edge **nc_edges, int nc_n_edges, int type,
int node_index)
{
if (!nc_edges) {
DPRINTF(4, "ERROR: Double pointer to node connection edges is NULL.\n");
return OP_ERROR;
}
if (node_index < 0) {
DPRINTF(4, "ERROR: Index of node whose connections will be allocated is negative number.\n");
return OP_ERROR;
}
if (type > 1 || type < 0) {
DPRINTF(4, "ERROR: Type of edge is neither incoming (1) nor outgoing(0).\ntyep is: %d.\n", type);
return OP_ERROR;
}
*nc_edges = (struct dgsh_edge *)malloc(sizeof(struct dgsh_edge) *
nc_n_edges);
if (!*nc_edges) {
DPRINTF(4, "ERROR: Memory allocation for node's index %d %s connections \
failed.\n", node_index, (type) ? "incoming" : "outgoing");
return OP_ERROR;
}
return OP_SUCCESS;
}
/**
* Copy the array of pointers to edges that go to or leave from a node
* (i.e. its incoming or outgoing connections) to a self-contained compact
* array of edges for easy transmission and receipt in one piece.
*/
STATIC enum op_result
make_compact_edge_array(struct dgsh_edge **nc_edges, int nc_n_edges,
struct dgsh_edge **p_edges)
{
int i;
int array_size = sizeof(struct dgsh_edge) * nc_n_edges;
if (nc_n_edges <= 0) {
DPRINTF(4, "ERROR: Size identifier to be used in malloc() is non-positive number: %d.\n", nc_n_edges);
return OP_ERROR;
}
if (nc_edges == NULL) {
DPRINTF(4, "ERROR: Compact edge array to put edges (connections) is NULL.\n");
return OP_ERROR;
}
if (p_edges == NULL) {
DPRINTF(4, "ERROR: Pointer to edge array is NULL.\n");
return OP_ERROR;
}
*nc_edges = (struct dgsh_edge *)malloc(array_size);
if (!(*nc_edges)) {
DPRINTF(4, "ERROR: Memory allocation of size %d for edge array failed.\n",
array_size);
return OP_ERROR;
}
/**
* Copy the edges of interest to the node-specific edge array
* that contains its connections.
*/
for (i = 0; i < nc_n_edges; i++) {
if (p_edges[i] == NULL) {
DPRINTF(4, "ERROR: Pointer to edge array contains NULL pointer.\n");
return OP_ERROR;
}
/**
* Dereference to reach the array base, make i hops of size
* sizeof(struct dgsh_edge), and point to that memory block.
*/
memcpy(&(*nc_edges)[i], p_edges[i], sizeof(struct dgsh_edge));
DPRINTF(4, "%s():Copied edge %d -> %d (%d) at index %d.",
__func__, p_edges[i]->from, p_edges[i]->to,
p_edges[i]->instances, i);
}
return OP_SUCCESS;
}
/* Reallocate array to edge pointers. */
STATIC enum op_result
reallocate_edge_pointer_array(struct dgsh_edge ***edge_array, int n_elements)
{
void **p = NULL;
if (edge_array == NULL) {
DPRINTF(4, "ERROR: Edge array is NULL pointer.\n");
return OP_ERROR;
}
if (n_elements <= 0) {
DPRINTF(4, "ERROR: Size identifier to be used in malloc() is non-positive number: %d.\n", n_elements);
return OP_ERROR;
} else if (n_elements == 1)
p = malloc(sizeof(struct dgsh_edge *) * n_elements);
else
p = realloc(*edge_array,
sizeof(struct dgsh_edge *) * n_elements);
if (!p) {
DPRINTF(4, "ERROR: Memory reallocation for edge failed.\n");
return OP_ERROR;
} else
*edge_array = (struct dgsh_edge **)p;
return OP_SUCCESS;
}
/**
* Gather the constraints on a node's input or output channel
* and then try to find a solution that respects both the node's
* channel constraint and the pair nodes' corresponding channel
* constraints if edges on the channel exist.
* The function is not called otherwise.
* If a solution is found, allocate edge instances to each edge that
* includes the node's channel (has to do with the flexible constraint).
*/
static enum op_result
satisfy_io_constraints(int *free_instances,
int this_channel_constraint, /* A node's required or
* provided constraint
* on this channel
*/
struct dgsh_edge **edges, /* Gathered pointers to edges
* of this channel
*/
int n_edges, /* Number of edges */
bool is_edge_incoming) /* Incoming or outgoing */
{
int i;
int weight = -1, modulo = 0;
if (this_channel_constraint > 0) {
*free_instances = this_channel_constraint;
weight = this_channel_constraint / n_edges;
modulo = this_channel_constraint % n_edges;
/* Edges that have no place in actual execution */
} else if (this_channel_constraint == 0) {
*free_instances = 0;
weight = 0;
modulo = 0;
} else /* Flexible constraint */
*free_instances = -1;
/* Aggregate the constraints for the node's channel. */
for (i = 0; i < n_edges; i++) {
if (this_channel_constraint > 0)
*free_instances -= weight + (modulo > 0);
if (is_edge_incoming) /* Outgoing for the pair node of edge */
edges[i]->to_instances = weight + (modulo > 0);
else
edges[i]->from_instances = weight + (modulo > 0);
if (modulo > 0)
modulo--;
DPRINTF(4, "%s(): edge from %d to %d, is_edge_incoming: %d, free_instances: %d, weight: %d, modulo: %d, from_instances: %d, to_instances: %d.\n", __func__, edges[i]->from, edges[i]->to, is_edge_incoming, *free_instances, weight, modulo, edges[i]->from_instances, edges[i]->to_instances);
}
DPRINTF(4, "%s(): Number of edges: %d, this_channel_constraint: %d, free instances: %d.\n", __func__, n_edges, this_channel_constraint, *free_instances);
return OP_SUCCESS;
}
/**
* Lookup this tool's edges and store pointers to them in order
* to then allow the evaluation of constraints for the current node's
* input and output channels.
*/
static enum op_result
dry_match_io_constraints(struct dgsh_node *current_node,
struct dgsh_node_connections *current_connections,
struct dgsh_edge ***edges_incoming, /* Uninitialised*/
struct dgsh_edge ***edges_outgoing) /* Uninitialised*/
{
int n_edges = chosen_mb->n_edges;
int n_free_in_channels = current_node->requires_channels;
int n_free_out_channels = current_node->provides_channels;
int node_index = current_node->index;
int *n_edges_incoming = ¤t_connections->n_edges_incoming;
int *n_edges_outgoing = ¤t_connections->n_edges_outgoing;
int i;
assert(node_index < chosen_mb->n_nodes);
/* Gather incoming/outgoing edges for node at node_index. */
for (i = 0; i < n_edges; i++) {
struct dgsh_edge *edge = &chosen_mb->edge_array[i];
DPRINTF(4, "%s(): edge at index %d from %d to %d, instances %d, from_instances %d, to_instances %d.", __func__, i, edge->from, edge->to, edge->instances, edge->from_instances, edge->to_instances);
if (edge->from == node_index) {
(*n_edges_outgoing)++;
if (reallocate_edge_pointer_array(edges_outgoing,
*n_edges_outgoing) == OP_ERROR)
return OP_ERROR;
(*edges_outgoing)[*n_edges_outgoing - 1] = edge;
}
if (edge->to == node_index) {
(*n_edges_incoming)++;
if (reallocate_edge_pointer_array(edges_incoming,
*n_edges_incoming) == OP_ERROR)
return OP_ERROR;
(*edges_incoming)[*n_edges_incoming - 1] = edge;
}
}
DPRINTF(4, "%s(): Node at index %d has %d outgoing edges and %d incoming.",
__func__, node_index, *n_edges_outgoing,
*n_edges_incoming);
/* Record the input/output constraints at node level. */
if (*n_edges_outgoing > 0)
if (satisfy_io_constraints(
¤t_connections->n_instances_outgoing_free,
n_free_out_channels,
*edges_outgoing, *n_edges_outgoing, 0) == OP_ERROR)
return OP_ERROR;
if (*n_edges_incoming > 0)
if (satisfy_io_constraints(
¤t_connections->n_instances_incoming_free,
n_free_in_channels,
*edges_incoming, *n_edges_incoming, 1) == OP_ERROR)
return OP_ERROR;
return OP_SUCCESS;
}
/**
* Free the dgsh graph's solution in face of an error.
* node_index: the last node we setup conenctions before error.
*/
static enum op_result
free_graph_solution(int node_index)
{
int i;
struct dgsh_node_connections *graph_solution =
chosen_mb->graph_solution;
assert(node_index < chosen_mb->n_nodes);
for (i = 0; i <= node_index; i++) {
if (graph_solution[i].n_edges_incoming > 0)
free(graph_solution[i].edges_incoming);
if (graph_solution[i].n_edges_outgoing > 0)
free(graph_solution[i].edges_outgoing);
}
free(graph_solution);
chosen_mb->graph_solution = NULL;
DPRINTF(4, "%s: freed %d nodes.", __func__, chosen_mb->n_nodes);
return OP_SUCCESS;
}
/**
* Add or subtract edge instances from an edge that meets a pair node's
* flexible constraint.
*/
static enum op_result
record_move_flexible(int *diff, int *index, int to_move_index, int *instances,
int to_move)
{
if (*diff > 0 || (*diff < 0 && to_move > 1)) {
/* In subtracting at least one edge instance should remain. */
if (*diff < 0 && *diff + (to_move - 1) <= 0)
*instances = -(to_move - 1);
else
*instances = *diff;
*diff -= *instances;
*index = to_move_index;
return OP_SUCCESS;
}
return OP_NOOP;
}
/**
* Add or subtract edge instances from an edge that is unbalanced wrt
* the pair node's constraint.
*/
static enum op_result
record_move_unbalanced(int *diff, int *index, int to_move_index,
int *instances, int to_move, int pair)
{
DPRINTF(4, "%s(): to_move: %d, pair: %d, diff: %d", __func__, to_move, pair, *diff);
/* Can either to_move or pair be 0? I don't think so */
if ((*diff > 0 && to_move < pair) ||
(*diff < 0 && to_move > pair)) {
*index = to_move_index;
if ((*diff > 0 && *diff - (pair - to_move) >= 0) ||
(*diff < 0 && *diff - (pair - to_move) <= 0))
*instances = pair - to_move;
else
*instances = *diff;
*diff -= *instances;
DPRINTF(4, "%s(): move successful: to_move: %d, pair: %d, diff: %d, instances: %d, edge index: %d", __func__, to_move, pair, *diff, *instances, *index);
return OP_SUCCESS;
}
return OP_NOOP;
}
/**
* From the set of unbalanced constraints of a node wrt the pair node's
* constraint on a specific channel, that is, input or output,
* find instances to subtract or add to satisfy the constraint.
* If that does not work try edges where the pair has a flexible constraint.
*/
static enum op_result
move(struct dgsh_edge** edges, int n_edges, int diff, bool is_edge_incoming)
{
int i = 0, j = 0;
int indexes[n_edges];
int instances[n_edges];
/* Try move unbalanced edges first.
* Avoid doing the move at the same edge.
*/
for (i = 0; i < n_edges; i++) {
struct dgsh_edge *edge = edges[i];
int *from = &edge->from_instances;
int *to = &edge->to_instances;
DPRINTF(4, "%s(): before move %s edge %d: from: %d, to: %d, diff %d.", __func__, is_edge_incoming ? "incoming" : "outgoing", i, *from, *to, diff);
if (*from == -1 || *to == -1)
continue;
if (is_edge_incoming) {
if (record_move_unbalanced(&diff, &indexes[j], i,
&instances[j], *to, *from) == OP_SUCCESS)
j++;
} else {
if (record_move_unbalanced(&diff, &indexes[j], i,
&instances[j], *from, *to) == OP_SUCCESS)
j++;
}
DPRINTF(4, "%s(): after move %s edge %d: from: %d, to: %d, diff %d.", __func__, is_edge_incoming ? "incoming" : "outgoing", i, *from, *to, diff);
if (diff == 0)
goto checkout;
}
/* Edges with flexible constraints are by default balanced. Try move */
for (i = 0; i < n_edges; i++) {
struct dgsh_edge *edge = edges[i];
int *from = &edge->from_instances;
int *to = &edge->to_instances;
if (is_edge_incoming) {
if (*from >= 0)
continue;
if (record_move_flexible(&diff, &indexes[j], i,
&instances[j], *to) == OP_SUCCESS)
j++;
} else {
if (*to >= 0)
continue;
if (record_move_flexible(&diff, &indexes[j], i,
&instances[j], *from) == OP_SUCCESS)
j++;
}
if (diff == 0)
goto checkout;
}
checkout:
if (diff == 0) {
int k = 0;
for (k = 0; k < j; k++) {
if (is_edge_incoming)
edges[indexes[k]]->to_instances += instances[k];
else
edges[indexes[k]]->from_instances +=
instances[k];
DPRINTF(4, "%s(): succeeded: move %d from edge %d.", __func__, instances[k], indexes[k]);
}
return OP_SUCCESS;
}
return OP_RETRY;
}
/**
* Try to find a solution that respects both the node's
* channel constraint and the pair nodes' corresponding channel
* constraints if edges on the channel exist.
* The function is not called otherwise.
* If a solution is found, allocate edge instances to each edge that
* includes the node's channel (has to do with the flexible constraint).
*/
static enum op_result
cross_match_io_constraints(int *free_instances,
int this_channel_constraint, /* A node's required
* provided constraint
* on the channel
*/
struct dgsh_edge **edges, /* Gathered pointers
* to edges
*/
int n_edges, /* Number of edges */
bool is_edge_incoming, /* Incoming or outgoing edges*/
bool *constraints_matched,
int *edges_matched)
{
int i;
int from_flex = 0;
int to_flex = 0;
for (i = 0; i < n_edges; i++) {
struct dgsh_edge *e = edges[i];
int *from = &e->from_instances;
int *to = &e->to_instances;
int matched = *edges_matched;
if (*from == -1 || *to == -1) {
DPRINTF(4, "%s(): edge from %d to %d, this_channel_constraint: %d, is_incoming: %d, from_instances: %d, to_instances %d.\n", __func__, e->from, e->to, this_channel_constraint, is_edge_incoming, *from, *to);
if (*from == -1 && *to == -1) {
from_flex++;
to_flex++;
e->instances = 1; // TODO
} else if (*from == -1) {
from_flex++;
e->instances = *to;
} else if (*to == -1) {
to_flex++;
e->instances = *from;
}
(*edges_matched)++;
/* fixed to more than one flexible
* is not solvable in the general case
*/
if (this_channel_constraint > 0 &&
((is_edge_incoming && from_flex > 1) ||
(!is_edge_incoming && to_flex > 1))) {
fprintf(stderr,
"ERROR: More than one edges are flexible. Cannot compute solution. Exiting.\n");
return OP_ERROR;
}
} else if (*from == *to) {
(*edges_matched)++;
e->instances = *from;
} else if (*from < *to) { /* e.g. from=1, to=3; then: */
if (is_edge_incoming) { /* +2: */
if (move(edges, n_edges, (*to - *from), 1)
== OP_SUCCESS) {
*to -= (*to - *from); /* -2: 3 -> 1 */
(*edges_matched)++;
}
} else
if (move(edges, n_edges, -(*to - *from), 0)
== OP_SUCCESS) {
*from += (*to - *from);
(*edges_matched)++;
}
} else {
if (is_edge_incoming) { /* e.g. from=3, to=1 */
if (move(edges, n_edges, -(*from - *to), 1)
== OP_SUCCESS) {
*to += (*from - *to);
(*edges_matched)++;
}
} else
if (move(edges, n_edges, (*from - *to), 0)
== OP_SUCCESS) {
*from -= (*from - *to);
(*edges_matched)++;
}
}
DPRINTF(4, "%s(): edge from %d to %d, this_channel_constraint: %d, is_incoming: %d, from_instances: %d, to_instances %d, edge instances: %d.\n", __func__, e->from, e->to, this_channel_constraint, is_edge_incoming, *from, *to, e->instances);
if (matched == *edges_matched){
DPRINTF(4, "%s(): WARNING: did not manage to match this edge",
__func__);
return OP_SUCCESS;
}
}
/* Is the matching for this channel in line with the (fixed)
* constraint? */
if (this_channel_constraint == -1) {
*constraints_matched = true;
return OP_SUCCESS;
}
int fds = 0;
for (i = 0; i < n_edges; i++) {
struct dgsh_edge *e = edges[i];
fds += e->instances;
}
DPRINTF(4, "%s communication endpoints to setup: %d, constraint: %d",
is_edge_incoming ? "Incoming" : "Outgoing",
fds, this_channel_constraint);
*constraints_matched = (fds == this_channel_constraint);
return OP_SUCCESS;
}
/**
* Search for conc with pid in message block mb
* and return a pointer to the structure or
* NULL if not found.
*/
struct dgsh_conc *
find_conc(struct dgsh_negotiation *mb, pid_t pid)
{
int i;
struct dgsh_conc *ca = mb->conc_array;
for (i = 0; i < mb->n_concs; i++) {
if (ca[i].pid == pid)
return &ca[i];
}
return NULL;
}
/**
* Calculate fds for concs at the multi-pipe
* endpoint.
*/
static enum op_result
calculate_conc_fds(void)
{
int i, calculated = 0, retries = 0;
int n_concs = chosen_mb->n_concs;
DPRINTF(4, "%s for %d n_concs", __func__, n_concs);
if (n_concs == 0)
return OP_SUCCESS;
repeat:
for (i = 0; i < n_concs; i++) {
struct dgsh_conc *c = &chosen_mb->conc_array[i];
DPRINTF(4, "%s() for conc %d at index %d with %d n_proc_pids",
__func__, c->pid, i, c->n_proc_pids);
if (c->input_fds >= 0 && c->output_fds >= 0)
continue;
c->input_fds = 0;
c->output_fds = 0;
if (c->multiple_inputs)
c->output_fds = get_expected_fds_n(chosen_mb,
c->endpoint_pid);
else
c->input_fds = get_provided_fds_n(chosen_mb,
c->endpoint_pid);
DPRINTF(4, "%s(): conc pid %d at index %d: %d %s fds for endpoint pid %d recovered",
__func__, c->pid, i,
c->multiple_inputs ? c->output_fds : c->input_fds,
c->multiple_inputs ? "outgoing" : "incoming",
c->endpoint_pid);
int j, fds;
for (j = 0; j < c->n_proc_pids; j++) {
if (c->multiple_inputs)
fds = get_provided_fds_n(chosen_mb,
c->proc_pids[j]);
else
fds = get_expected_fds_n(chosen_mb,
c->proc_pids[j]);
if (find_conc(chosen_mb, c->proc_pids[j]) && fds == -1) {
c->input_fds = c->output_fds = -1;
DPRINTF(4, "%s(): conc pid %d at index %d: fds for conc with pid %d not yet available",
__func__, c->pid, i, c->proc_pids[j]);
break;
} else
if (c->multiple_inputs)
c->input_fds += fds;
else
c->output_fds += fds;
DPRINTF(4, "%s(): conc pid %d at index %d: %d %s fds for pid %d recovered",
__func__, c->pid, i, fds,
c->multiple_inputs ? "incoming" : "outgoing",
c->proc_pids[j]);
}
// Use what we know for the multi-pipe end to compute the endpoint
if (c->multiple_inputs && c->input_fds >= 0 && c->output_fds == -1)
c->output_fds = c->input_fds;
else if (!c->multiple_inputs && c->output_fds >= 0
&& c->input_fds == -1)
c->input_fds = c->output_fds;
if (c->input_fds >= 0 && c->output_fds >= 0) {
assert(c->input_fds == c->output_fds);
calculated++;
}
DPRINTF(4, "%s(): Conc pid %d at index %d has %d %s fds and %d %s fds",
__func__, c->pid, i,
c->multiple_inputs ? c->input_fds : c->output_fds,
c->multiple_inputs ? "incoming" : "outgoing",
c->multiple_inputs ? c->output_fds : c->input_fds,
c->multiple_inputs ? "outgoing" : "incoming");
DPRINTF(4, "%s(): Calculated fds for %d concs so far", __func__,
calculated);
}
if (calculated != n_concs && retries < n_concs) {
retries++;
goto repeat;
}
if (retries == n_concs)
return OP_ERROR;
return OP_SUCCESS;
}
/**
* For each node substitute pointers to edges with proper edge structures
* (copies) to facilitate transmission and receipt in one piece.
*/
static enum op_result
prepare_solution(void)
{
int i;
int n_nodes = chosen_mb->n_nodes;
struct dgsh_node_connections *graph_solution =
chosen_mb->graph_solution;
enum op_result exit_state = OP_SUCCESS;
for (i = 0; i < n_nodes; i++) {
struct dgsh_node_connections *current_connections =
&graph_solution[i];
/* Hack: struct dgsh_edge* -> struct dgsh_edge** */
struct dgsh_edge **edges_incoming =
(struct dgsh_edge **)current_connections->edges_incoming;
current_connections->edges_incoming = NULL;
/* Hack: struct dgsh_edge* -> struct dgsh_edge** */
struct dgsh_edge **edges_outgoing =
(struct dgsh_edge **)current_connections->edges_outgoing;
current_connections->edges_outgoing = NULL;
int *n_edges_incoming = ¤t_connections->n_edges_incoming;
int *n_edges_outgoing = ¤t_connections->n_edges_outgoing;
DPRINTF(3, "%s(): Node %s, pid: %d, connections in: %d, connections out: %d.",
__func__, chosen_mb->node_array[i].name,
chosen_mb->node_array[i].pid,
*n_edges_incoming, *n_edges_outgoing);
if (*n_edges_incoming > 0) {
if (exit_state == OP_SUCCESS)
if (make_compact_edge_array(
¤t_connections->edges_incoming,
*n_edges_incoming, edges_incoming)
== OP_ERROR)
exit_state = OP_ERROR;
free(edges_incoming);
}