Debian Codegraph @

membership/ccm/ccmgraph.c

raw
1 /*
2 * ccmgraph.c: Keeps track of the connectivity within the cluster members
3 * to derive the largest totally connected subgraph.
4 *
5 * Copyright (c) International Business Machines Corp., 2002
6 * Author: Ram Pai ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 #include <lha_internal.h>
24 #include <ccm.h>
25
26 /* ASSUMPTIONS IN THIS FILE.
27 * we assume that there can be at most MAXNODE number of nodes in the graph.
28 * we assume that we are working with only one graph at a time, from the call
29 * to graph_init() to graph_delete(). If Multiple graph_init() are done
30 * simultaneously then results may be unpredictable.
31 */
32
33 static vertex_t graph[MAXNODE]; /* allocate memory statically */
34 static char vyesorno='n';
35 #define GRAPH_TIMEOUT 15
36 #define GRAPH_TIMEOUT_TOO_LONG 25
37
38
39
40
41 static void
42 bitmap_display(char* bitmap)
43 {
44 ccm_debug2(LOG_DEBUG, "bitmap_display:%x", (unsigned int) bitmap[0]);
45 }
46
47 static void
48 graph_display(graph_t* gr)
49 {
50 int i;
51
52 if (gr == NULL){
53 ccm_log(LOG_ERR, "graph_display:graph is NULL");
54 return;
55 }
56
57 for ( i = 0 ; i < gr->graph_nodes; i++ ) {
58 char* bitmap = gr->graph_node[i]->bitmap;
59 int index = gr->graph_node[i]->uuid;
60
61 ccm_debug2(LOG_DEBUG, "graph_display:node[%d]'s bitmap is:", index);
62 if(bitmap != NULL) {
63 bitmap_display(bitmap);
64 }
65 }
66 }
67
68
69
70 /* */
71 /* clean up the unneccessary bits in the graph and check for */
72 /* inconsistency. */
73 /* */
74 static void
75 graph_sanitize(graph_t *gr)
76 {
77 char *bitmap;
78 int i,j, uuid_i, uuid_j;
79 vertex_t **graph_node;
80
81 (void)graph_display;
82
83 graph_node = gr->graph_node;
84
85 for ( i=0; i < gr->graph_nodes; i++ ) {
86 uuid_i = graph_node[i]->uuid;
87
88 assert(uuid_i >= 0 && uuid_i < MAXNODE);
89 if(graph_node[i]->bitmap == NULL) {
90 bitmap_create(&bitmap, MAXNODE);
91 graph_node[i]->bitmap = bitmap;
92 graph_node[i]->count = 0;
93 }
94 /*
95 * Loop through each uuid from 0 to MAXNODE.
96 * If there is no vertex with the corresponding uuid,
97 * reset the bit corresponding to this uuid.
98 */
99 for(uuid_j=0; uuid_j < MAXNODE; uuid_j++) {
100 for (j=0; j<gr->graph_nodes; j++) {
101 if(uuid_j == graph_node[j]->uuid) {
102 break;
103 }
104 }
105 if(j == gr->graph_nodes) {
106 /* node uuid_j is not in the graph, so clear its
107 * bits.
108 */
109 bitmap_clear(uuid_j, graph_node[i]->bitmap,
110 MAXNODE);
111 } else {
112 if(uuid_i == uuid_j) {
113 continue;
114 }
115
116 if(graph_node[j]->bitmap == NULL) {
117 bitmap_create(&bitmap, MAXNODE);
118 graph_node[j]->bitmap = bitmap;
119 graph_node[j]->count = 0;
120 }
121
122 if(!bitmap_test(uuid_j,graph_node[i]->bitmap,
123 MAXNODE) ||
124 !bitmap_test(uuid_i,graph_node[j]->bitmap,
125 MAXNODE)){
126 bitmap_clear(uuid_j,
127 graph_node[i]->bitmap,MAXNODE);
128 bitmap_clear(uuid_i,
129 graph_node[j]->bitmap,MAXNODE);
130 }
131 }
132 }
133 graph_node[i]->count =
134 bitmap_count(graph_node[i]->bitmap,MAXNODE);
135 }
136
137 return;
138 }
139
140
141 /* */
142 /* print the vertices that belong the largest totally connected subgraph. */
143 /* */
144 static void
145 print_vertex(vertex_t **vertex, int maxnode)
146 {
147 int i,j;
148
149 for ( i = 0 ; i < maxnode ; i++) {
150 printf("%d:\t",i);
151 for ( j = 0 ; j < maxnode ; j++) {
152 if(bitmap_test(j, vertex[i]->bitmap, maxnode)) {
153 printf(" 1 ");
154 }else{
155 printf(" 0 ");
156 }
157 }
158 printf("uuid=%d, count=%d\n",vertex[i]->uuid,vertex[i]->count);
159 printf("\n");
160 }
161 printf("----------------------------------------\n");
162 }
163
164
165 /* */
166 /* BEGIN OF FUNCTIONS THAT FORM THE CORE OF THE ALGORITHM */
167 /* */
168
169 /* */
170 /* the function that orders the vertices in the graph while sorting. */
171 /* */
172 static int
173 compare(const void *value1, const void *value2)
174 {
175 const vertex_t *t1 = *(const vertex_t * const *)value1;
176 const vertex_t *t2 = *(const vertex_t * const *)value2;
177
178 return(t2->count - t1->count);
179 }
180
181
182 static void
183 relocate(vertex_t **vertex,
184 int indx,
185 int size,
186 int *indxtab,
187 int maxnode)
188 {
189 vertex_t *tmp_vertex;
190 int i;
191
192 tmp_vertex = vertex[indx];
193 for ( i = indx+1; i < size; i++ ) {
194 if(tmp_vertex->count >= vertex[i]->count) {
195 break;
196 }
197 vertex[i-1] = vertex[i];
198 indxtab[vertex[i-1]->uuid] = i-1;
199 }
200 vertex[i-1] = tmp_vertex;
201 indxtab[vertex[i-1]->uuid] = i-1;
202 }
203
204 static void
205 decrement_count(vertex_t **vertex, int indx,
206 int size,
207 int *indxtab,
208 int maxnode)
209 {
210 vertex_t *tmp_vertex;
211
212 tmp_vertex = vertex[indx];
213 tmp_vertex->count--;
214 relocate(vertex, indx, size, indxtab, maxnode);
215 }
216
217
218 static int
219 find_best_candidate(vertex_t **vertex, int startindx,
220 int size,
221 int *indxtab,
222 int maxnode)
223 {
224 int min_indx, min_count;
225 int i, uuid;
226 int count, indx;
227
228
229 min_indx = startindx;
230 min_count = INT_MAX;
231
232 for ( i = size-1; i >= startindx; i-- ) {
233 count = 0;
234 for (uuid = 0; uuid < maxnode; uuid++) {
235 if(bitmap_test(uuid, vertex[i]->bitmap, maxnode)){
236 indx = indxtab[uuid];
237 if(indx == -1 || indx >= size) {
238 continue;
239 }
240
241 count += vertex[indx]->count;
242 }
243 }
244 if(count == min_count) {
245 if (vyesorno == 'y') {
246 ccm_debug2(LOG_DEBUG
247 , "find_best_candidate:probably 1 more group exists");
248 }
249 }
250 if(count < min_count) {
251 min_count = count;
252 min_indx = i;
253 }
254 }
255 return min_indx;
256 }
257
258 static int
259 find_size(vertex_t **vertex, int maxnode)
260 {
261 int size, i;
262 assert(vertex[0]->count == 0);
263 size=1;
264 for ( i= 1 ; i < maxnode; i++ ) {
265 if(vertex[i]->count == size) {
266 size++;
267 } else {
268 break;
269 }
270 }
271 return size;
272 }
273
274 static int
275 delete_entry(vertex_t **vertex, int indx, int size,
276 int *indxtab, int maxnode)
277 {
278 vertex_t *tmp_vertex;
279 int uuid;
280 int loc;
281
282 /* move this entry to the end of the table and shuffle the other
283 * entries up
284 */
285 if (vyesorno == 'y') {
286 ccm_debug2(LOG_DEBUG, "delete_entry:k=%d is being removed",indx);
287 }
288 tmp_vertex = vertex[indx];
289 tmp_vertex->count--;
290 relocate(vertex, indx, size, indxtab, maxnode);
291
292 for ( uuid = 0 ; uuid < maxnode ; uuid ++ ){
293 if(bitmap_test(uuid, tmp_vertex->bitmap, maxnode)) {
294 loc = indxtab[uuid];
295 if(loc == -1 || loc >= size-1) {
296 continue;
297 }
298 decrement_count(vertex, loc, size-1, indxtab, maxnode);
299 }
300 }
301 if (vyesorno == 'y') {
302 print_vertex(vertex, maxnode);
303 }
304 if (tmp_vertex->count == 0) {
305 return find_size(vertex+size-1, maxnode-indx);
306 }
307 return -1;
308 }
309
310
311 #ifdef NEED_PRINT_MEMBERS
312 static void
313 print_members(vertex_t **vertex, int maxmem)
314 {
315 int i;
316
317 printf("the members are \n");
318 for ( i = 0 ; i < maxmem ; i++) {
319 printf("%d ", vertex[i]->uuid);
320 }
321 printf("\n");
322 }
323 #endif
324
325 static int
326 get_max_clique(vertex_t **vertex, int maxnode, int *loc)
327 {
328 int i,j,k,num;
329 int maxconnect, tmp_maxconnect;
330 int size;
331 int *indxtab;
332
333 /* sort the scratchpad entries with respect to their
334 * connectivity value
335 */
336 qsort(vertex, maxnode, sizeof(vertex_t *), compare);
337
338 /* indx the uuid into the indx table */
339 indxtab = g_malloc(MAXNODE*sizeof(int));
340
341 /*
342 * TOBEDONE: we really do not need to allocate MAXNODE size array
343 * What is required is: Find the max uuid in the vertex[] array
344 * and allocate a indxtab table of that size. Postponing
345 * the implementation currently.
346 */
347 for ( i = 0 ; i < MAXNODE ; i++ ) {
348 *(indxtab+i) = -1;
349 }
350 for ( i = 0 ; i < maxnode ; i++ ) {
351 if(vertex[i]->uuid != -1) {
352 indxtab[vertex[i]->uuid] = i;
353 }
354 }
355
356 maxconnect = 1;
357 for ( j=i-1 ; j>=0; j-- ) {
358 if (vyesorno == 'y') {
359 print_vertex(vertex, maxnode);
360 }
361
362 if((j+1)<maxconnect){
363 break; /* done */
364 }
365
366 if(vertex[j]->count >= j+1){
367 break; /* done */
368 }
369
370 /* find number of entries with the same connectivity value */
371 num=1;
372 for(k=j-1; k>=0; k--) {
373 if(vertex[j]->count == vertex[k]->count){
374 num++;
375 }
376 else {
377 break;
378 }
379 }
380
381 /* find the best candidate to be considered for removal */
382 k = find_best_candidate(vertex, j-num+1, j+1, indxtab,
383 maxnode);
384 if (vyesorno == 'y') {
385 ccm_debug2(LOG_DEBUG
386 , "get_max_clique:k=%d is the best candidate for removal",k);
387 }
388
389 /* delete the candidate */
390 tmp_maxconnect = delete_entry(vertex, k, j+1, indxtab,
391 maxnode);
392 if(tmp_maxconnect>maxconnect) {
393 *loc = j;
394 maxconnect=tmp_maxconnect;
395 }
396 }
397
398 if ((j+1) < maxconnect) {
399 size = maxconnect;
400 } else {
401 *loc = 0; size = j+1;
402 }
403
404 g_free(indxtab);
405
406
407 return size;
408 }
409 /* */
410 /* END OF FUNCTIONS THAT FORM THE CORE OF THE ALGORITHM */
411 /* */
412
413 /* */
414 /* initialize the graph. */
415 /* */
416 graph_t *
417 graph_init()
418 {
419 int i;
420 graph_t *gr;
421
422 if((gr = (graph_t *)g_malloc(sizeof(graph_t))) == NULL){
423 return NULL;
424 }
425
426 memset(gr, 0, sizeof(graph_t));
427 memset(graph, 0, sizeof(graph));
428 for ( i = 0 ; i < MAXNODE ; i++ ) {
429 gr->graph_node[i] = &graph[i];
430 }
431
432 return gr;
433 }
434
435
436 /* */
437 /* free all the datastructures */
438 /* */
439 void
440 graph_free(graph_t *gr)
441 {
442 int i;
443 if(!gr) {
444 return;
445 }
446
447 for ( i = 0 ; i < gr->graph_nodes; i++ ) {
448 if(gr->graph_node[i]->bitmap != NULL) {
449 bitmap_delete(gr->graph_node[i]->bitmap);
450 }
451 }
452 g_free(gr);
453 return;
454 }
455
456
457 /* */
458 /* add a new member to the graph, whose id is 'uuid' */
459 /* */
460 void
461 graph_add_uuid(graph_t *gr, int uuid)
462 {
463 int i;
464 for ( i = 0 ; i < gr->graph_nodes; i++ ) {
465 if(gr->graph_node[i]->uuid == uuid) {
466 return;
467 }
468 }
469
470 gr->graph_node[gr->graph_nodes++]->uuid = uuid;
471 }
472
473 /* */
474 /* add the member whose id is 'dst_uuid' to the connectivity list */
475 /* of the member with id 'src_uuid' */
476 /* */
477 void
478 graph_add_to_membership(graph_t *gr, int src_uuid, int dst_uuid)
479 {
480 int i;
481 for ( i = 0 ; i < gr->graph_nodes; i++ ) {
482 if(gr->graph_node[i]->uuid == src_uuid) {
483 assert(gr->graph_node[i]->bitmap);
484 bitmap_mark(dst_uuid, gr->graph_node[i]->bitmap,
485 MAXNODE);
486 return;
487 }
488 }
489 assert(0);
490 }
491
492
493
494 /* */
495 /* update the connectivity information of the member whose id is 'uuid'. */
496 /* */
497 void
498 graph_update_membership(graph_t *gr,
499 int uuid,
500 char *bitlist)
501 {
502 int i;
503 for ( i = 0 ; i < gr->graph_nodes; i++ ) {
504 if(gr->graph_node[i]->uuid == uuid) {
505 /* assert that this is not a duplicate message */
506 if(gr->graph_node[i]->bitmap != NULL) {
507 bitmap_delete(gr->graph_node[i]->bitmap);
508 gr->graph_rcvd--;
509 }
510 gr->graph_node[i]->bitmap = bitlist;
511 /* postpone the calculation of count, because
512 * we have to sanitize this graph after
513 * reciving all the bitmaps
514 */
515 gr->graph_rcvd++;
516 break;
517 }
518 }
519
520 /* make sure we have not received message from unknown node */
521 assert(i < gr->graph_nodes);
522
523 return;
524 }
525
526 /* */
527 /* return TRUE, if all the members of the graph have their */
528 /* connectivity information updated. */
529 /* */
530 int
531 graph_filled_all(graph_t *gr)
532 {
533 return (gr->graph_rcvd == gr->graph_nodes);
534 }
535
536 /* */
537 /* return the largest fully connected subgraph. */
538 /* */
539 int
540 graph_get_maxclique(graph_t *gr, char **bitmap)
541 {
542 int loc = 0;
543 int i, size;
544
545 graph_sanitize(gr);
546 size = get_max_clique(gr->graph_node, gr->graph_nodes,
547 &loc);
548 bitmap_create(bitmap, MAXNODE);
549 for ( i = loc ; i < size ; i++ ) {
550 bitmap_mark(gr->graph_node[i]->uuid, *bitmap, MAXNODE);
551 }
552 return size;
553 }