/
Pack.c
4293 lines (3620 loc) · 127 KB
/
Pack.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
/*
Time-stamp: <2009-12-02 12:26:34 simonmar>
Graph packing and unpacking code for sending it to another processor
and retrieving the original graph structure from the packet.
In the old RTS the code was split into Pack.c and Unpack.c (now deceased)
Used in GUM and GrAnSim.
The GrAnSim version of the code defines routines for *simulating* the
packing of closures in the same way it is done in the parallel runtime
system. Basically GrAnSim only puts the addresses of the closures to be
transferred into a buffer. This buffer will then be associated with the
event of transferring the graph. When this event is scheduled, the
@UnpackGraph@ routine is called and the buffer can be discarded
afterwards.
Note that in GranSim we need many buffers, not just one per PE.
*/
//@node Graph packing, , ,
//@section Graph packing
#if defined(PAR) || defined(GRAN) /* whole file */
//@menu
//* Includes::
//* Prototypes::
//* Global variables::
//* ADT of Closure Queues::
//* Initialisation for packing::
//* Packing Functions::
//* Low level packing routines::
//* Unpacking routines::
//* Aux fcts for packing::
//* Printing Packet Contents::
//* End of file::
//@end menu
//*/
//@node Includes, Prototypes, Graph packing, Graph packing
//@subsection Includes
#include "Rts.h"
#include "RtsFlags.h"
#include "RtsUtils.h"
#include "ClosureTypes.h"
#include "Storage.h"
#include "Hash.h"
#include "Parallel.h"
#include "GranSimRts.h"
#include "ParallelRts.h"
# if defined(DEBUG)
# include "sm/Sanity.h"
# include "Printer.h"
# include "ParallelDebug.h"
# endif
#include "FetchMe.h"
/* Which RTS flag should be used to get the size of the pack buffer ? */
# if defined(PAR)
# define RTS_PACK_BUFFER_SIZE RtsFlags.ParFlags.packBufferSize
# else /* GRAN */
# define RTS_PACK_BUFFER_SIZE RtsFlags.GranFlags.packBufferSize
# endif
//@node Prototypes, Global variables, Includes, Graph packing
//@subsection Prototypes
/*
Code declarations.
*/
//@node ADT of closure queues, Init for packing, Prototypes, Prototypes
//@subsubsection ADT of closure queues
static inline void InitClosureQueue(void);
static inline rtsBool QueueEmpty(void);
static inline void QueueClosure(StgClosure *closure);
static inline StgClosure *DeQueueClosure(void);
//@node Init for packing, Packing routines, ADT of closure queues, Prototypes
//@subsubsection Init for packing
static void InitPacking(rtsBool unpack);
# if defined(PAR)
rtsBool InitPackBuffer(void);
# elif defined(GRAN)
rtsPackBuffer *InstantiatePackBuffer (void);
static void reallocPackBuffer (void);
# endif
//@node Packing routines, Low level packing fcts, Init for packing, Prototypes
//@subsubsection Packing routines
static void PackClosure (StgClosure *closure);
//@node Low level packing fcts, Unpacking routines, Packing routines, Prototypes
//@subsubsection Low level packing fcts
# if defined(GRAN)
static void Pack (StgClosure *data);
# else
static void Pack (StgWord data);
static void PackGeneric(StgClosure *closure);
static void PackArray(StgClosure *closure);
static void PackPLC (StgPtr addr);
static void PackOffset (int offset);
static void PackPAP(StgPAP *pap);
static rtsPackBuffer *PackTSO(StgTSO *tso, nat *packBufferSize);
static rtsPackBuffer *PackStkO(StgPtr stko, nat *packBufferSize);
static void PackFetchMe(StgClosure *closure);
static void GlobaliseAndPackGA (StgClosure *closure);
# endif
//@node Unpacking routines, Aux fcts for packing, Low level packing fcts, Prototypes
//@subsubsection Unpacking routines
# if defined(PAR)
void InitPendingGABuffer(nat size);
void CommonUp(StgClosure *src, StgClosure *dst);
static StgClosure *SetGAandCommonUp(globalAddr *gaP, StgClosure *closure,
rtsBool hasGA);
static nat FillInClosure(StgWord ***bufptrP, StgClosure *graph);
static void LocateNextParent(StgClosure **parentP,
nat *pptrP, nat *pptrsP, nat *sizeP);
StgClosure *UnpackGraph(rtsPackBuffer *packBuffer,
globalAddr **gamap,
nat *nGAs);
static StgClosure *UnpackClosure (StgWord ***bufptrP, StgClosure **graphP,
globalAddr *ga);
static StgWord **UnpackGA(StgWord **bufptr, globalAddr *ga);
static StgClosure *UnpackOffset(globalAddr *ga);
static StgClosure *UnpackPLC(globalAddr *ga);
static void UnpackArray(StgWord ***bufptrP, StgClosure *graph);
static nat UnpackPAP(StgWord ***bufptrP, StgClosure *graph);
# elif defined(GRAN)
void CommonUp(StgClosure *src, StgClosure *dst);
StgClosure *UnpackGraph(rtsPackBuffer* buffer);
#endif
//@node Aux fcts for packing, , Unpacking routines, Prototypes
//@subsubsection Aux fcts for packing
# if defined(PAR)
static void DonePacking(void);
static void AmPacking(StgClosure *closure);
static int OffsetFor(StgClosure *closure);
static rtsBool NotYetPacking(int offset);
static inline rtsBool RoomToPack (nat size, nat ptrs);
static inline rtsBool isOffset(globalAddr *ga);
static inline rtsBool isFixed(globalAddr *ga);
static inline rtsBool isConstr(globalAddr *ga);
static inline rtsBool isUnglobalised(globalAddr *ga);
# elif defined(GRAN)
static void DonePacking(void);
static rtsBool NotYetPacking(StgClosure *closure);
# endif
//@node Global variables, ADT of Closure Queues, Prototypes, Graph packing
//@subsection Global variables
/*
Static data declarations
*/
static nat pack_locn, /* ptr to first free loc in pack buffer */
clq_size, clq_pos,
buf_id = 1; /* identifier for buffer */
static nat unpacked_size;
static rtsBool roomInBuffer;
#if defined(PAR)
static GlobalTaskId dest_gtid=0; /* destination for message to send */
#endif
/*
The pack buffer
To be pedantic: in GrAnSim we're packing *addresses* of closures,
not the closures themselves.
*/
static rtsPackBuffer *globalPackBuffer = NULL, /* for packing a graph */
*globalUnpackBuffer = NULL; /* for unpacking a graph */
/*
Bit of a hack for testing if a closure is the root of the graph. This is
set in @PackNearbyGraph@ and tested in @PackClosure@.
*/
static nat packed_thunks = 0;
static StgClosure *graph_root;
# if defined(PAR)
/*
The offset hash table is used during packing to record the location in
the pack buffer of each closure which is packed.
*/
//@cindex offsetTable
static HashTable *offsetTable;
//@cindex PendingGABuffer
static globalAddr *PendingGABuffer, *gaga;
# endif /* PAR */
//@node ADT of Closure Queues, Initialisation for packing, Global variables, Graph packing
//@subsection ADT of Closure Queues
//@menu
//* Closure Queues::
//* Init routines::
//* Basic routines::
//@end menu
//@node Closure Queues, Init routines, ADT of Closure Queues, ADT of Closure Queues
//@subsubsection Closure Queues
/*
Closure Queues
These routines manage the closure queue.
*/
static nat clq_pos, clq_size;
static StgClosure **ClosureQueue = NULL; /* HWL: init in main */
#if defined(DEBUG)
static char graphFingerPrint[MAX_FINGER_PRINT_LEN];
#endif
//@node Init routines, Basic routines, Closure Queues, ADT of Closure Queues
//@subsubsection Init routines
/* @InitClosureQueue@ allocates and initialises the closure queue. */
//@cindex InitClosureQueue
static inline void
InitClosureQueue(void)
{
clq_pos = clq_size = 0;
if (ClosureQueue==NULL)
ClosureQueue = (StgClosure**) stgMallocWords(RTS_PACK_BUFFER_SIZE,
"InitClosureQueue");
}
//@node Basic routines, Types of Global Addresses, Init routines, ADT of Closure Queues
//@subsubsection Basic routines
/*
QueueEmpty returns rtsTrue if the closure queue is empty; rtsFalse otherwise.
*/
//@cindex QueueEmpty
static inline rtsBool
QueueEmpty(void)
{
return(clq_pos >= clq_size);
}
/* QueueClosure adds its argument to the closure queue. */
//@cindex QueueClosure
static inline void
QueueClosure(closure)
StgClosure *closure;
{
if(clq_size < RTS_PACK_BUFFER_SIZE ) {
IF_PAR_DEBUG(paranoia,
belch(">__> <<%d>> Q: %p (%s); %d elems in q",
globalPackBuffer->id, closure, info_type(closure), clq_size-clq_pos));
ClosureQueue[clq_size++] = closure;
} else {
barf("Closure Queue Overflow (EnQueueing %p (%s))",
closure, info_type(closure));
}
}
/* DeQueueClosure returns the head of the closure queue. */
//@cindex DeQueueClosure
static inline StgClosure *
DeQueueClosure(void)
{
if(!QueueEmpty()) {
IF_PAR_DEBUG(paranoia,
belch(">__> <<%d>> DeQ: %p (%s); %d elems in q",
globalPackBuffer->id, ClosureQueue[clq_pos], info_type(ClosureQueue[clq_pos]),
clq_size-clq_pos));
return(ClosureQueue[clq_pos++]);
} else {
return((StgClosure*)NULL);
}
}
/* DeQueueClosure returns the head of the closure queue. */
#if defined(DEBUG)
//@cindex PrintQueueClosure
static void
PrintQueueClosure(void)
{
nat i;
fputs("Closure queue:", stderr);
for (i=clq_pos; i < clq_size; i++)
fprintf(stderr, "%p (%s), ",
(StgClosure *)ClosureQueue[clq_pos++],
info_type(ClosureQueue[clq_pos++]));
fputc('\n', stderr);
}
#endif
//@node Types of Global Addresses, , Basic routines, ADT of Closure Queues
//@subsubsection Types of Global Addresses
/*
Types of Global Addresses
These routines determine whether a GA is one of a number of special types
of GA.
*/
# if defined(PAR)
//@cindex isOffset
static inline rtsBool
isOffset(globalAddr *ga)
{
return (ga->weight == 1U && ga->payload.gc.gtid == (GlobalTaskId)0);
}
//@cindex isFixed
static inline rtsBool
isFixed(globalAddr *ga)
{
return (ga->weight == 0U);
}
//@cindex isConstr
static inline rtsBool
isConstr(globalAddr *ga)
{
return (ga->weight == 2U);
}
//@cindex isUnglobalised
static inline rtsBool
isUnglobalised(globalAddr *ga)
{
return (ga->weight == 2U);
}
# endif
//@node Initialisation for packing, Packing Functions, ADT of Closure Queues, Graph packing
//@subsection Initialisation for packing
/*
Simple Packing Routines
About packet sizes in GrAnSim: In GrAnSim we use a malloced block of
gransim_pack_buffer_size words to simulate a packet of pack_buffer_size
words. In the simulated PackBuffer we only keep the addresses of the
closures that would be packed in the parallel system (see Pack). To
decide if a packet overflow occurs pack_buffer_size must be compared
versus unpacked_size (see RoomToPack). Currently, there is no multi
packet strategy implemented, so in the case of an overflow we just stop
adding closures to the closure queue. If an overflow of the simulated
packet occurs, we just realloc some more space for it and carry on as
usual. -- HWL
*/
# if defined(GRAN)
rtsPackBuffer *
InstantiatePackBuffer (void) {
extern rtsPackBuffer *globalPackBuffer;
globalPackBuffer = (rtsPackBuffer *) stgMallocWords(sizeofW(rtsPackBuffer),
"InstantiatePackBuffer: failed to alloc packBuffer");
globalPackBuffer->size = RtsFlags.GranFlags.packBufferSize_internal;
globalPackBuffer->buffer = (StgWord **) stgMallocWords(RtsFlags.GranFlags.packBufferSize_internal,
"InstantiatePackBuffer: failed to alloc GranSim internal packBuffer");
/* NB: gransim_pack_buffer_size instead of pack_buffer_size -- HWL */
/* stgMallocWords is now simple allocate in Storage.c */
return (globalPackBuffer);
}
/*
Reallocate the GranSim internal pack buffer to make room for more closure
pointers. This is independent of the check for packet overflow as in GUM
*/
static void
reallocPackBuffer (void) {
ASSERT(pack_locn >= (int)globalPackBuffer->size+sizeofW(rtsPackBuffer));
IF_GRAN_DEBUG(packBuffer,
belch("** Increasing size of PackBuffer %p to %d words (PE %u @ %d)\n",
globalPackBuffer, globalPackBuffer->size+REALLOC_SZ,
CurrentProc, CurrentTime[CurrentProc]));
globalPackBuffer = (rtsPackBuffer*)realloc(globalPackBuffer,
sizeof(StgClosure*)*(REALLOC_SZ +
(int)globalPackBuffer->size +
sizeofW(rtsPackBuffer))) ;
if (globalPackBuffer==(rtsPackBuffer*)NULL)
barf("Failing to realloc %d more words for PackBuffer %p (PE %u @ %d)\n",
REALLOC_SZ, globalPackBuffer, CurrentProc, CurrentTime[CurrentProc]);
globalPackBuffer->size += REALLOC_SZ;
ASSERT(pack_locn < globalPackBuffer->size+sizeofW(rtsPackBuffer));
}
# endif
# if defined(PAR)
/* @initPacking@ initialises the packing buffer etc. */
//@cindex InitPackBuffer
rtsBool
InitPackBuffer(void)
{
if (globalPackBuffer==(rtsPackBuffer*)NULL) {
if ((globalPackBuffer = (rtsPackBuffer *)
stgMallocWords(sizeofW(rtsPackBuffer)+RtsFlags.ParFlags.packBufferSize+DEBUG_HEADROOM,
"InitPackBuffer")) == NULL)
return rtsFalse;
}
return rtsTrue;
}
# endif
//@cindex InitPacking
static void
InitPacking(rtsBool unpack)
{
# if defined(GRAN)
globalPackBuffer = InstantiatePackBuffer(); /* for GrAnSim only -- HWL */
/* NB: free in UnpackGraph */
# elif defined(PAR)
if (unpack) {
/* allocate a GA-to-GA map (needed for ACK message) */
InitPendingGABuffer(RtsFlags.ParFlags.packBufferSize);
} else {
/* allocate memory to pack the graph into */
InitPackBuffer();
}
# endif
/* init queue of closures seen during packing */
InitClosureQueue();
if (unpack)
return;
globalPackBuffer->id = buf_id++; /* buffer id are only used for debugging! */
pack_locn = 0; /* the index into the actual pack buffer */
unpacked_size = 0; /* the size of the whole graph when unpacked */
roomInBuffer = rtsTrue;
packed_thunks = 0; /* total number of thunks packed so far */
# if defined(PAR)
offsetTable = allocHashTable();
# endif
}
//@node Packing Functions, Low level packing routines, Initialisation for packing, Graph packing
//@subsection Packing Functions
//@menu
//* Packing Sections of Nearby Graph::
//* Packing Closures::
//@end menu
//@node Packing Sections of Nearby Graph, Packing Closures, Packing Functions, Packing Functions
//@subsubsection Packing Sections of Nearby Graph
/*
Packing Sections of Nearby Graph
@PackNearbyGraph@ packs a closure and associated graph into a static
buffer (@PackBuffer@). It returns the address of this buffer and the
size of the data packed into the buffer (in its second parameter,
@packBufferSize@). The associated graph is packed in a depth first
manner, hence it uses an explicit queue of closures to be packed rather
than simply using a recursive algorithm. Once the packet is full,
closures (other than primitive arrays) are packed as FetchMes, and their
children are not queued for packing. */
//@cindex PackNearbyGraph
/* NB: this code is shared between GranSim and GUM;
tso only used in GranSim */
rtsPackBuffer *
PackNearbyGraph(closure, tso, packBufferSize, dest)
StgClosure* closure;
StgTSO* tso;
nat *packBufferSize;
GlobalTaskId dest;
{
IF_PAR_DEBUG(resume,
graphFingerPrint[0] = '\0');
ASSERT(RTS_PACK_BUFFER_SIZE > 0);
ASSERT(_HS==1); // HWL HACK; compile time constant
#if defined(PAR_TICKY) // HWL HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACK
PAR_TICKY_PACK_NEARBY_GRAPH_START();
#endif
/* ToDo: check that we have enough heap for the packet
ngoq ngo'
if (Hp + PACK_HEAP_REQUIRED > HpLim)
return NULL;
*/
InitPacking(rtsFalse);
# if defined(PAR)
dest_gtid=dest; //-1 to disable
# elif defined(GRAN)
graph_root = closure;
# endif
IF_GRAN_DEBUG(pack,
belch(">>> Packing <<%d>> (buffer @ %p); graph root @ %p [PE %d]\n demanded by TSO %d (%p) [PE %u]",
globalPackBuffer->id, globalPackBuffer, closure, where_is(closure),
tso->id, tso, where_is((StgClosure*)tso)));
IF_GRAN_DEBUG(pack,
belch("** PrintGraph of %p is:", closure);
PrintGraph(closure,0));
IF_PAR_DEBUG(resume,
GraphFingerPrint(closure, graphFingerPrint);
ASSERT(strlen(graphFingerPrint)<=MAX_FINGER_PRINT_LEN);
belch(">>> Packing <<%d>> (buffer @ %p); graph root @ %p [%x]\n demanded by TSO %d (%p); Finger-print is\n {%s}",
globalPackBuffer->id, globalPackBuffer, closure, mytid,
tso->id, tso, graphFingerPrint));
IF_PAR_DEBUG(packet,
belch("** PrintGraph of %p is:", closure);
belch("** pack_locn=%d", pack_locn);
PrintGraph(closure,0));
QueueClosure(closure);
do {
PackClosure(DeQueueClosure());
} while (!QueueEmpty());
# if defined(PAR)
/* Record how much space the graph needs in packet and in heap */
globalPackBuffer->tso = tso; // currently unused, I think (debugging?)
globalPackBuffer->unpacked_size = unpacked_size;
globalPackBuffer->size = pack_locn;
/* Check for buffer overflow (again) */
ASSERT(pack_locn <= RtsFlags.ParFlags.packBufferSize+DEBUG_HEADROOM);
IF_DEBUG(sanity, // write magic end-of-buffer word
globalPackBuffer->buffer[pack_locn] = END_OF_BUFFER_MARKER);
*packBufferSize = pack_locn;
# else /* GRAN */
/* Record how much space is needed to unpack the graph */
// PackBuffer[PACK_FLAG_LOCN] = (P_) MAGIC_PACK_FLAG; for testing
globalPackBuffer->tso = tso;
globalPackBuffer->unpacked_size = unpacked_size;
// ASSERT(pack_locn <= PackBuffer[PACK_SIZE_LOCN]+PACK_HDR_SIZE);
/* ToDo: Print an earlier, more meaningful message */
if (pack_locn==0) /* i.e. packet is empty */
barf("EMPTY PACKET! Can't transfer closure %p at all!!\n",
closure);
globalPackBuffer->size = pack_locn;
*packBufferSize = pack_locn;
# endif
DonePacking(); /* {GrAnSim}vaD 'ut'Ha' */
# if defined(GRAN)
IF_GRAN_DEBUG(pack ,
belch("** Finished <<%d>> packing graph %p; closures packed: %d; thunks packed: %d; size of graph: %d",
globalPackBuffer->id, closure, globalPackBuffer->size, packed_thunks, globalPackBuffer->unpacked_size));
if (RtsFlags.GranFlags.GranSimStats.Global) {
globalGranStats.tot_packets++;
globalGranStats.tot_packet_size += pack_locn;
}
IF_GRAN_DEBUG(pack, PrintPacket(globalPackBuffer));
# elif defined(PAR)
IF_PAR_DEBUG(packet,
belch("** Finished <<%d>> packing graph %p (%s); closures packed: %d; thunks packed: %d; size of graph: %d",
globalPackBuffer->id, closure, info_type(closure),
globalPackBuffer->size, packed_thunks,
globalPackBuffer->unpacked_size));;
IF_DEBUG(sanity, // do a sanity check on the packet just constructed
checkPacket(globalPackBuffer));
# endif /* GRAN */
#if defined(PAR_TICKY) // HWL HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACK
PAR_TICKY_PACK_NEARBY_GRAPH_END(globalPackBuffer->size, packed_thunks);
#endif
return (globalPackBuffer);
}
//@cindex PackOneNode
# if defined(GRAN)
/* This version is used when the node is already local */
rtsPackBuffer *
PackOneNode(closure, tso, packBufferSize)
StgClosure* closure;
StgTSO* tso;
nat *packBufferSize;
{
extern rtsPackBuffer *globalPackBuffer;
int i, clpack_locn;
InitPacking(rtsFalse);
IF_GRAN_DEBUG(pack,
belch("** PackOneNode: %p (%s)[PE %d] requested by TSO %d (%p) [PE %d]",
closure, info_type(closure),
where_is(closure), tso->id, tso, where_is((StgClosure *)tso)));
Pack(closure);
/* Record how much space is needed to unpack the graph */
globalPackBuffer->tso = tso;
globalPackBuffer->unpacked_size = unpacked_size;
/* Set the size parameter */
ASSERT(pack_locn <= RTS_PACK_BUFFER_SIZE);
globalPackBuffer->size = pack_locn;
*packBufferSize = pack_locn;
if (RtsFlags.GranFlags.GranSimStats.Global) {
globalGranStats.tot_packets++;
globalGranStats.tot_packet_size += pack_locn;
}
IF_GRAN_DEBUG(pack,
PrintPacket(globalPackBuffer));
return (globalPackBuffer);
}
# endif /* GRAN */
#if defined(GRAN)
/*
PackTSO and PackStkO are entry points for two special kinds of closure
which are used in the parallel RTS. Compared with other closures they
are rather awkward to pack because they don't follow the normal closure
layout (where all pointers occur before all non-pointers). Luckily,
they're only needed when migrating threads between processors. */
//@cindex PackTSO
rtsPackBuffer*
PackTSO(tso, packBufferSize)
StgTSO *tso;
nat *packBufferSize;
{
extern rtsPackBuffer *globalPackBuffer;
IF_GRAN_DEBUG(pack,
belch("** Packing TSO %d (%p)", tso->id, tso));
*packBufferSize = 0;
// PackBuffer[0] = PackBuffer[1] = 0; ???
return(globalPackBuffer);
}
//@cindex PackStkO
static rtsPackBuffer*
PackStkO(stko, packBufferSize)
StgPtr stko;
nat *packBufferSize;
{
extern rtsPackBuffer *globalPackBuffer;
IF_GRAN_DEBUG(pack,
belch("** Packing STKO %p", stko));
*packBufferSize = 0;
// PackBuffer[0] = PackBuffer[1] = 0;
return(globalPackBuffer);
}
static void
PackFetchMe(StgClosure *closure)
{
barf("{PackFetchMe}Daq Qagh: no FetchMe closures in GRAN!");
}
#elif defined(PAR)
static rtsPackBuffer*
PackTSO(tso, packBufferSize)
StgTSO *tso;
nat *packBufferSize;
{
barf("{PackTSO}Daq Qagh: trying to pack a TSO %d (%p) of size %d; thread migrations not supported, yet",
tso->id, tso, packBufferSize);
}
rtsPackBuffer*
PackStkO(stko, packBufferSize)
StgPtr stko;
nat *packBufferSize;
{
barf("{PackStkO}Daq Qagh: trying to pack a STKO (%p) of size %d; thread migrations not supported, yet",
stko, packBufferSize);
}
//@cindex PackFetchMe
static void
PackFetchMe(StgClosure *closure)
{
StgInfoTable *ip;
nat i;
int offset;
#if defined(DEBUG)
nat x = pack_locn;
#endif
#if defined(GRAN)
barf("{PackFetchMe}Daq Qagh: no FetchMe closures in GRAN!");
#else
offset = OffsetFor(closure);
if (!NotYetPacking(offset)) {
IF_PAR_DEBUG(pack,
belch("*>.. Packing FETCH_ME for closure %p (s) as offset to %d",
closure, info_type(closure), offset));
PackOffset(offset);
// unpacked_size += 0; // unpacked_size unchanged (closure is shared!!)
return;
}
/* Need a GA even when packing a constructed FETCH_ME (cruel world!) */
AmPacking(closure);
/* FMs must be always globalised */
GlobaliseAndPackGA(closure);
IF_PAR_DEBUG(pack,
belch("*>.. Packing FETCH_ME for closure %p (%s) with GA: ((%x, %d, %x))",
closure, info_type(closure),
globalPackBuffer->buffer[pack_locn-2],
globalPackBuffer->buffer[pack_locn-1],
globalPackBuffer->buffer[pack_locn-3]));
/* Pack a FetchMe closure instead of closure */
ip = &stg_FETCH_ME_info;
/* this assumes that the info ptr is always the first word in a closure*/
Pack((StgWord)ip);
for (i = 1; i < _HS; ++i) // pack rest of fixed header
Pack((StgWord)*(((StgPtr)closure)+i));
unpacked_size += sizeofW(StgFetchMe);
/* size of FETCHME in packed is the same as that constant */
ASSERT(pack_locn-x==PACK_FETCHME_SIZE);
/* In the pack buffer the pointer to a GA (in the FetchMe closure)
is expanded to the full GA; this is a compile-time const */
//ASSERT(PACK_FETCHME_SIZE == sizeofW(StgFetchMe)-1+PACK_GA_SIZE);
#endif
}
#endif
#ifdef DIST
static void
PackRemoteRef(StgClosure *closure)
{
StgInfoTable *ip;
nat i;
int offset;
offset = OffsetFor(closure);
if (!NotYetPacking(offset)) {
PackOffset(offset);
unpacked_size += 2;
return;
}
/* Need a GA even when packing a constructed REMOTE_REF (cruel world!) */
AmPacking(closure);
/* basically we just Globalise, but for sticky things we can't have multiple GAs,
so we must prevent the GAs being split.
In returning things to the true sticky owner, this case is already handled, but for
anything else we just give up at the moment... This needs to be fixed!
*/
{ globalAddr *ga;
ga = LAGAlookup(closure); // surely this ga must exist?
// ***************************************************************************
// ***************************************************************************
// REMOTE_REF HACK - dual is in SetGAandCommonUp
// - prevents the weight from ever reaching zero
if(ga != NULL)
ga->weight=0x06660666; //anything apart from 0 really...
// ***************************************************************************
// ***************************************************************************
if((ga != NULL)&&(ga->weight / 2 <= 2))
barf("Cant split the weight any further when packing REMOTE_REF for closure %p (%s) with GA: ((%x, %d, %x))",
closure, info_type(closure),
ga->payload.gc.gtid, ga->payload.gc.slot, ga->weight);
}
GlobaliseAndPackGA(closure);
IF_PAR_DEBUG(pack,
belch("*>.. Packing REMOTE_REF for closure %p (%s) with GA: ((%x, %d, %x))",
closure, info_type(closure),
globalPackBuffer->buffer[pack_locn-2],
globalPackBuffer->buffer[pack_locn-1],
globalPackBuffer->buffer[pack_locn-3]));
/* Pack a REMOTE_REF closure instead of closure */
ip = &stg_REMOTE_REF_info;
/* this assumes that the info ptr is always the first word in a closure*/
Pack((StgWord)ip);
for (i = 1; i < _HS; ++i) // pack rest of fixed header
Pack((StgWord)*(((StgPtr)closure)+i));
unpacked_size += PACK_FETCHME_SIZE;
}
#endif /* DIST */
//@node Packing Closures, , Packing Sections of Nearby Graph, Packing Functions
//@subsubsection Packing Closures
/*
Packing Closures
@PackClosure@ is the heart of the normal packing code. It packs a single
closure into the pack buffer, skipping over any indirections and
globalising it as necessary, queues any child pointers for further
packing, and turns it into a @FetchMe@ or revertible black hole (@RBH@)
locally if it was a thunk. Before the actual closure is packed, a
suitable global address (GA) is inserted in the pack buffer. There is
always room to pack a fetch-me to the closure (guaranteed by the
RoomToPack calculation), and this is packed if there is no room for the
entire closure.
Space is allocated for any primitive array children of a closure, and
hence a primitive array can always be packed along with it's parent
closure. */
//@cindex PackClosure
# if defined(PAR)
void
PackClosure(closure)
StgClosure *closure;
{
StgInfoTable *info;
nat clpack_locn;
ASSERT(LOOKS_LIKE_GHC_INFO(get_itbl(closure)));
closure = UNWIND_IND(closure);
/* now closure is the thing we want to pack */
info = get_itbl(closure);
clpack_locn = OffsetFor(closure);
/* If the closure has been packed already, just pack an indirection to it
to guarantee that the graph doesn't become a tree when unpacked */
if (!NotYetPacking(clpack_locn)) {
PackOffset(clpack_locn);
return;
}
switch (info->type) {
case CONSTR_CHARLIKE:
IF_PAR_DEBUG(pack,
belch("*>^^ Packing a charlike closure %d",
((StgIntCharlikeClosure*)closure)->data));
PackPLC((StgPtr)CHARLIKE_CLOSURE(((StgIntCharlikeClosure*)closure)->data));
// NB: unpacked_size of a PLC is 0
return;
case CONSTR_INTLIKE:
{
StgInt val = ((StgIntCharlikeClosure*)closure)->data;
if ((val <= MAX_INTLIKE) && (val >= MIN_INTLIKE)) {
IF_PAR_DEBUG(pack,
belch("*>^^ Packing a small intlike %d as a PLC",
val));
PackPLC((StgPtr)INTLIKE_CLOSURE(val));
// NB: unpacked_size of a PLC is 0
return;
} else {
IF_PAR_DEBUG(pack,
belch("*>^^ Packing a big intlike %d as a normal closure",
val));
PackGeneric(closure);
return;
}
}
case CONSTR:
case CONSTR_1_0:
case CONSTR_0_1:
case CONSTR_2_0:
case CONSTR_1_1:
case CONSTR_0_2:
/* it's a constructor (i.e. plain data) */
IF_PAR_DEBUG(pack,
belch("*>^^ Packing a CONSTR %p (%s) using generic packing",
closure, info_type(closure)));
PackGeneric(closure);
return;
case THUNK_STATIC: // ToDo: check whether that's ok
case FUN_STATIC: // ToDo: check whether that's ok
case CONSTR_STATIC:
case CONSTR_NOCAF_STATIC:// For now we ship indirections to CAFs: They are
// evaluated on each PE if needed
IF_PAR_DEBUG(pack,
belch("*>~~ Packing a %p (%s) as a PLC",
closure, info_type(closure)));
PackPLC((StgPtr)closure);
// NB: unpacked_size of a PLC is 0
return;
case THUNK_SELECTOR:
{
StgClosure *selectee = ((StgSelector *)closure)->selectee;
IF_PAR_DEBUG(pack,
belch("*>** Found THUNK_SELECTOR at %p (%s) pointing to %p (%s); using PackGeneric",
closure, info_type(closure),
selectee, info_type(selectee)));
PackGeneric(closure);
/* inlined code; probably could use PackGeneric
Pack((StgWord)(*(StgPtr)closure));
Pack((StgWord)(selectee));
QueueClosure(selectee);
unpacked_size += 2;
*/
}
return;
case FUN:
case FUN_1_0:
case FUN_0_1:
case FUN_2_0:
case FUN_1_1:
case FUN_0_2:
case THUNK:
case THUNK_1_0:
case THUNK_0_1:
case THUNK_2_0:
case THUNK_1_1:
case THUNK_0_2:
PackGeneric(closure);
return;
case AP_UPD:
case PAP:
/*
barf("*> Packing of PAP not implemented %p (%s)",
closure, info_type(closure));
Currently we don't pack PAPs; we pack a FETCH_ME to the closure,
instead. Note that since PAPs contain a chunk of stack as payload,
implementing packing of PAPs is a first step towards thread migration.
IF_PAR_DEBUG(pack,
belch("*>.. Packing a PAP closure at %p (%s) as a FETCH_ME",
closure, info_type(closure)));
PackFetchMe(closure);
*/
PackPAP((StgPAP *)closure);
return;
case CAF_BLACKHOLE:
case BLACKHOLE:
case BLACKHOLE_BQ:
case SE_BLACKHOLE:
case SE_CAF_BLACKHOLE:
case RBH:
case FETCH_ME:
case FETCH_ME_BQ:
/* If it's a (revertible) black-hole, pack a FetchMe closure to it */
//ASSERT(pack_locn > PACK_HDR_SIZE);
IF_PAR_DEBUG(pack,
belch("*>.. Packing a BH-like closure at %p (%s) as a FETCH_ME",
closure, info_type(closure)));
/* NB: in case of a FETCH_ME this might build up a chain of FETCH_MEs;
phps short-cut the GA here */
PackFetchMe(closure);
return;
#ifdef DIST
case REMOTE_REF:
IF_PAR_DEBUG(pack,