26
26
import org .apache .spark .unsafe .PlatformDependent ;
27
27
import org .apache .spark .unsafe .memory .MemoryBlock ;
28
28
import org .apache .spark .unsafe .memory .TaskMemoryManager ;
29
+ import org .apache .spark .util .Utils ;
29
30
import org .slf4j .Logger ;
30
31
import org .slf4j .LoggerFactory ;
31
32
@@ -40,7 +41,7 @@ public final class UnsafeExternalSorter {
40
41
41
42
private final Logger logger = LoggerFactory .getLogger (UnsafeExternalSorter .class );
42
43
43
- private static final int PAGE_SIZE = 1024 * 1024 ; // TODO: tune this
44
+ private static final int PAGE_SIZE = 1 << 27 ; // 128 megabytes
44
45
45
46
private final PrefixComparator prefixComparator ;
46
47
private final RecordComparator recordComparator ;
@@ -107,6 +108,12 @@ private void openSorter() throws IOException {
107
108
108
109
@ VisibleForTesting
109
110
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
+
110
117
final UnsafeSorterSpillWriter spillWriter =
111
118
new UnsafeSorterSpillWriter (blockManager , fileBufferSize , writeMetrics );
112
119
spillWriters .add (spillWriter );
@@ -129,14 +136,13 @@ public void spill() throws IOException {
129
136
taskContext .taskMetrics ().incMemoryBytesSpilled (spillSize );
130
137
taskContext .taskMetrics ().incDiskBytesSpilled (spillWriter .numberOfSpilledBytes ());
131
138
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)" );
137
139
openSorter ();
138
140
}
139
141
142
+ private long getMemoryUsage () {
143
+ return sorter .getMemoryUsage () + (allocatedPages .size () * (long ) PAGE_SIZE );
144
+ }
145
+
140
146
public long freeMemory () {
141
147
long memoryFreed = 0 ;
142
148
final Iterator <MemoryBlock > iter = allocatedPages .iterator ();
0 commit comments