forked from openucx/sparkucx
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/scala/org/apache/spark/shuffle/utils/UcxUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) Mellanox Technologies Ltd. 2020. ALL RIGHTS RESERVED. | ||
* See file LICENSE for terms. | ||
*/ | ||
package org.apache.spark.shuffle.utils | ||
|
||
import java.io.IOException | ||
import java.math.{MathContext, RoundingMode} | ||
import java.util.Locale | ||
import scala.util.control.NonFatal | ||
|
||
object UcxUtils extends UcxLogging { | ||
def tryOrIOException[T](block: => T): T = { | ||
try { | ||
block | ||
} catch { | ||
case e: IOException => | ||
logError("Exception encountered", e) | ||
throw e | ||
case NonFatal(e) => | ||
logError("Exception encountered", e) | ||
throw new IOException(e) | ||
} | ||
} | ||
|
||
def bytesToString(size: Long): String = bytesToString(BigInt(size)) | ||
|
||
def bytesToString(size: BigInt): String = { | ||
val EB = 1L << 60 | ||
val PB = 1L << 50 | ||
val TB = 1L << 40 | ||
val GB = 1L << 30 | ||
val MB = 1L << 20 | ||
val KB = 1L << 10 | ||
|
||
if (size >= BigInt(1L << 11) * EB) { | ||
// The number is too large, show it in scientific notation. | ||
BigDecimal(size, new MathContext(3, RoundingMode.HALF_UP)).toString() + " B" | ||
} else { | ||
val (value, unit) = { | ||
if (size >= 2 * EB) { | ||
(BigDecimal(size) / EB, "EB") | ||
} else if (size >= 2 * PB) { | ||
(BigDecimal(size) / PB, "PB") | ||
} else if (size >= 2 * TB) { | ||
(BigDecimal(size) / TB, "TB") | ||
} else if (size >= 2 * GB) { | ||
(BigDecimal(size) / GB, "GB") | ||
} else if (size >= 2 * MB) { | ||
(BigDecimal(size) / MB, "MB") | ||
} else if (size >= 2 * KB) { | ||
(BigDecimal(size) / KB, "KB") | ||
} else { | ||
(BigDecimal(size), "B") | ||
} | ||
} | ||
"%.1f %s".formatLocal(Locale.US, value, unit) | ||
} | ||
} | ||
} |