Skip to content

Commit 851d691

Browse files
author
Ilya Ganelin
committed
[SPARK-5932] Updated memoryStringToMb to use new interfaces
1 parent a9f4fcf commit 851d691

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,18 +1080,9 @@ private[spark] object Utils extends Logging {
10801080
* Convert a Java memory parameter passed to -Xmx (such as 300m or 1g) to a number of megabytes.
10811081
*/
10821082
def memoryStringToMb(str: String): Int = {
1083-
val lower = str.toLowerCase
1084-
if (lower.endsWith("k")) {
1085-
(lower.substring(0, lower.length-1).toLong / 1024).toInt
1086-
} else if (lower.endsWith("m")) {
1087-
lower.substring(0, lower.length-1).toInt
1088-
} else if (lower.endsWith("g")) {
1089-
lower.substring(0, lower.length-1).toInt * 1024
1090-
} else if (lower.endsWith("t")) {
1091-
lower.substring(0, lower.length-1).toInt * 1024 * 1024
1092-
} else {// no suffix, so it's just a number in bytes
1093-
(lower.toLong / 1024 / 1024).toInt
1094-
}
1083+
// Convert to bytes, rather than directly to MB, because when no units are specified the unit
1084+
// is assumed to be bytes
1085+
(JavaUtils.byteStringAsBytes(str) / 1048576.0).toInt
10951086
}
10961087

10971088
/**

core/src/test/scala/org/apache/spark/util/UtilsSuite.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,20 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
9191

9292
assert(Utils.byteStringAsGB("1") === 1)
9393
assert(Utils.byteStringAsGB("1g") === 1)
94-
assert(Utils.byteStringAsGB("1000m") === 1)
95-
assert(Utils.byteStringAsGB("1000000k") === 1)
94+
assert(Utils.byteStringAsGB("1023m") === 0)
95+
assert(Utils.byteStringAsGB("1024m") === 1)
96+
assert(Utils.byteStringAsGB("1048575k") === 0)
97+
assert(Utils.byteStringAsGB("1048576k") === 1)
9698
assert(Utils.byteStringAsGB("1k") === 0)
97-
assert(Utils.byteStringAsGB("1023m") === 1)
9899
assert(Utils.byteStringAsGB("1t") === ByteUnit.TB.toGB(1))
99100
assert(Utils.byteStringAsGB("1p") === ByteUnit.PB.toGB(1))
100101

101102
assert(Utils.byteStringAsMB("1") === 1)
102103
assert(Utils.byteStringAsMB("1m") === 1)
103-
assert(Utils.byteStringAsMB("1000k") === 1)
104+
assert(Utils.byteStringAsMB("1048575b") === 0)
105+
assert(Utils.byteStringAsMB("1048576b") === 1)
106+
assert(Utils.byteStringAsMB("1023k") === 0)
107+
assert(Utils.byteStringAsMB("1024k") === 1)
104108
assert(Utils.byteStringAsMB("3645k") === 3)
105109
assert(Utils.byteStringAsMB("1g") === ByteUnit.GB.toMB(1))
106110
assert(Utils.byteStringAsMB("1t") === ByteUnit.TB.toMB(1))

network/common/src/main/java/org/apache/spark/network/util/ByteUnit.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public double convert(double d, ByteUnit u) {
4242

4343
/**
4444
* <pre>
45-
* Kilobyte (kB)
46-
* 10^3 Byte = 1.000 Byte
45+
* Kilobyte (kB) = 1024 Byte
4746
*/
4847
KB {
4948
@Override
@@ -59,8 +58,7 @@ public double convert(double d, ByteUnit u) {
5958

6059
/**
6160
* <pre>
62-
* Megabyte (MB)
63-
* 10^6 Byte = 1.000.000 Byte
61+
* Megabyte (MB) = 1024 * 1024 Byte
6462
*/
6563
MB {
6664
@Override
@@ -76,8 +74,7 @@ public double convert(double d, ByteUnit u) {
7674

7775
/**
7876
* <pre>
79-
* Gigabyte (GB)
80-
* 10^9 Byte = 1.000.000.000 Byte
77+
* Gigabyte (GB) = 1024 * 1024 * 1024 Byte
8178
*/
8279
GB {
8380
@Override
@@ -91,8 +88,7 @@ public double toBytes(double d) {
9188

9289
/**
9390
* <pre>
94-
* Terabyte (TB)
95-
* 10^12 Byte = 1.000.000.000.000 Byte
91+
* Terabyte (TB) = 1024 * 1024 * 1024 * 1024 Byte
9692
*/
9793
TB {
9894
@Override
@@ -108,8 +104,7 @@ public double convert(double d, ByteUnit u) {
108104

109105
/**
110106
* <pre>
111-
* Petabyte (PB)
112-
* 10^15 Byte = 1.000.000.000.000.000 Byte
107+
* Petabyte (PB) = 1024 * 1024 * 1024 * 1024 * 1024 Byte
113108
*/
114109
PB {
115110
@Override
@@ -123,11 +118,11 @@ public double convert(double d, ByteUnit u) {
123118
}
124119
};
125120

126-
static final double C_KB = Math.pow(10d, 3d);
127-
static final double C_MB = Math.pow(10d, 6d);
128-
static final double C_GB = Math.pow(10d, 9d);
129-
static final double C_TB = Math.pow(10d, 12d);
130-
static final double C_PB = Math.pow(10d, 15d);
121+
static final double C_KB = 1024d;
122+
static final double C_MB = Math.pow(1024d, 2d);
123+
static final double C_GB = Math.pow(1024d, 3d);
124+
static final double C_TB = Math.pow(1024d, 4d);
125+
static final double C_PB = Math.pow(1024d, 5d);
131126

132127
private static final double MAX = Double.MAX_VALUE;
133128

0 commit comments

Comments
 (0)