2626import org .apache .spark .unsafe .PlatformDependent ;
2727import org .apache .spark .unsafe .memory .MemoryBlock ;
2828import org .apache .spark .unsafe .memory .TaskMemoryManager ;
29+ import org .apache .spark .util .Utils ;
2930import org .slf4j .Logger ;
3031import org .slf4j .LoggerFactory ;
3132
@@ -40,7 +41,7 @@ public final class UnsafeExternalSorter {
4041
4142 private final Logger logger = LoggerFactory .getLogger (UnsafeExternalSorter .class );
4243
43- private static final int PAGE_SIZE = 1024 * 1024 ; // TODO: tune this
44+ private static final int PAGE_SIZE = 1 << 27 ; // 128 megabytes
4445
4546 private final PrefixComparator prefixComparator ;
4647 private final RecordComparator recordComparator ;
@@ -107,6 +108,12 @@ private void openSorter() throws IOException {
107108
108109 @ VisibleForTesting
109110 public void spill () throws IOException {
111+ logger .info ("Thread {} spilling sort data of {} to disk ({} {} so far)" ,
112+ Thread .currentThread ().getId (),
113+ Utils .bytesToString (getMemoryUsage ()),
114+ numSpills ,
115+ numSpills > 1 ? " times" : " time" );
116+
110117 final UnsafeSorterSpillWriter spillWriter =
111118 new UnsafeSorterSpillWriter (blockManager , fileBufferSize , writeMetrics );
112119 spillWriters .add (spillWriter );
@@ -129,14 +136,13 @@ public void spill() throws IOException {
129136 taskContext .taskMetrics ().incMemoryBytesSpilled (spillSize );
130137 taskContext .taskMetrics ().incDiskBytesSpilled (spillWriter .numberOfSpilledBytes ());
131138 numSpills ++;
132- final long threadId = Thread .currentThread ().getId ();
133- // TODO: messy; log _before_ spill
134- logger .info ("Thread " + threadId + " spilling in-memory map of " +
135- org .apache .spark .util .Utils .bytesToString (spillSize ) + " to disk (" +
136- (numSpills + ((numSpills > 1 ) ? " times" : " time" )) + " so far)" );
137139 openSorter ();
138140 }
139141
142+ private long getMemoryUsage () {
143+ return sorter .getMemoryUsage () + (allocatedPages .size () * (long ) PAGE_SIZE );
144+ }
145+
140146 public long freeMemory () {
141147 long memoryFreed = 0 ;
142148 final Iterator <MemoryBlock > iter = allocatedPages .iterator ();
0 commit comments