1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *    http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.spark.storage
19
20import org.apache.spark.annotation.DeveloperApi
21import org.apache.spark.rdd.{RDD, RDDOperationScope}
22import org.apache.spark.util.Utils
23
24@DeveloperApi
25class RDDInfo(
26    val id: Int,
27    var name: String,
28    val numPartitions: Int,
29    var storageLevel: StorageLevel,
30    val parentIds: Seq[Int],
31    val callSite: String = "",
32    val scope: Option[RDDOperationScope] = None)
33  extends Ordered[RDDInfo] {
34
35  var numCachedPartitions = 0
36  var memSize = 0L
37  var diskSize = 0L
38  var externalBlockStoreSize = 0L
39
40  def isCached: Boolean = (memSize + diskSize > 0) && numCachedPartitions > 0
41
42  override def toString: String = {
43    import Utils.bytesToString
44    ("RDD \"%s\" (%d) StorageLevel: %s; CachedPartitions: %d; TotalPartitions: %d; " +
45      "MemorySize: %s; DiskSize: %s").format(
46        name, id, storageLevel.toString, numCachedPartitions, numPartitions,
47        bytesToString(memSize), bytesToString(diskSize))
48  }
49
50  override def compare(that: RDDInfo): Int = {
51    this.id - that.id
52  }
53}
54
55private[spark] object RDDInfo {
56  def fromRdd(rdd: RDD[_]): RDDInfo = {
57    val rddName = Option(rdd.name).getOrElse(Utils.getFormattedClassName(rdd))
58    val parentIds = rdd.dependencies.map(_.rdd.id)
59    new RDDInfo(rdd.id, rddName, rdd.partitions.length,
60      rdd.getStorageLevel, parentIds, rdd.creationSite.shortForm, rdd.scope)
61  }
62}
63