-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Profile: add public APIs for creating and dumping
These functions have been implemented before. This commit organizes them together to a separate Scala file.
- Loading branch information
1 parent
2b8f538
commit bacbfb3
Showing
4 changed files
with
112 additions
and
48 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
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,87 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2024 Institute of Computing Technology, Chinese Academy of Sciences | ||
* | ||
* DiffTest is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* http://license.coscl.org.cn/MulanPSL2 | ||
* | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
* | ||
* See the Mulan PSL v2 for more details. | ||
***************************************************************************************/ | ||
|
||
package difftest.util | ||
|
||
import difftest.DifftestBundle | ||
import difftest.common.FileControl | ||
import org.json4s.DefaultFormats | ||
import org.json4s.native._ | ||
|
||
import java.nio.file.{Files, Paths} | ||
|
||
// Note: json4s seems to load integers with BigInt. We may need to convert them into int if necessary | ||
|
||
case class BundleProfile( | ||
className: String, | ||
classArgs: Map[String, Any], | ||
delay: Int, | ||
) { | ||
def toBundle: DifftestBundle = { | ||
val constructor = Class.forName(className).getConstructors()(0) | ||
val args = constructor.getParameters.map { param => | ||
(classArgs(param.getName), param.getType.getName) match { | ||
case (arg: BigInt, "int") => arg.toInt | ||
case (arg, _) => arg | ||
} | ||
} | ||
constructor.newInstance(args: _*).asInstanceOf[DifftestBundle] | ||
} | ||
} | ||
|
||
case class DifftestProfile( | ||
cpu: String, | ||
numCores: Int, | ||
bundles: Seq[BundleProfile], | ||
) { | ||
def toJsonString: String = Serialization.writePretty(this)(DefaultFormats) | ||
} | ||
|
||
object DifftestProfile { | ||
def fromBundles(cpu: String, bundles: Seq[(DifftestBundle, Int)]): DifftestProfile = { | ||
val numCores = bundles.count(_._1.isUniqueIdentifier) | ||
require(bundles.length % numCores == 0, "cannot create the profile if cores are not symmetric") | ||
val bundleProfiles = bundles.take(bundles.length / numCores).map { case (b, d) => BundleProfile.fromBundle(b, d) } | ||
DifftestProfile(cpu, numCores, bundleProfiles) | ||
} | ||
|
||
def fromJson(filename: String): DifftestProfile = { | ||
val profileStr = new String(Files.readAllBytes(Paths.get(filename))) | ||
val profiles = JsonMethods.parse(profileStr).extract(DefaultFormats, manifest[Map[String, Any]]) | ||
val cpu = profiles("cpu").asInstanceOf[String] | ||
val numCores = profiles("numCores").asInstanceOf[BigInt].toInt | ||
val bundles = profiles("bundles").asInstanceOf[List[Map[String, Any]]].map { bundleProfileMap => | ||
BundleProfile( | ||
bundleProfileMap("className").asInstanceOf[String], | ||
bundleProfileMap("classArgs").asInstanceOf[Map[String, Any]], | ||
bundleProfileMap("delay").asInstanceOf[BigInt].toInt, | ||
) | ||
} | ||
DifftestProfile(cpu, numCores, bundles) | ||
} | ||
} | ||
|
||
object BundleProfile { | ||
def fromBundle(bundle: DifftestBundle, delay: Int): BundleProfile = { | ||
BundleProfile(bundle.getClass.getName, bundle.classArgs, delay) | ||
} | ||
} | ||
|
||
object Profile { | ||
def generateJson(cpu: String, bundles: Seq[(DifftestBundle, Int)], profileName: String = "difftest"): Unit = { | ||
val difftestProfile = DifftestProfile.fromBundles(cpu, bundles) | ||
FileControl.write(Seq(difftestProfile.toJsonString), s"${profileName}_profile.json") | ||
} | ||
} |
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