Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkResult.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "BenchmarkResult.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/Support/FileOutputBuffer.h" |
| 13 | #include "llvm/Support/FileSystem.h" |
| 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | |
| 17 | // Defining YAML traits for IO. |
| 18 | namespace llvm { |
| 19 | namespace yaml { |
| 20 | |
| 21 | // std::vector<exegesis::Measure> will be rendered as a list. |
| 22 | template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> { |
| 23 | static const bool flow = false; |
| 24 | }; |
| 25 | |
| 26 | // exegesis::Measure is rendererd as a flow instead of a list. |
| 27 | // e.g. { "key": "the key", "value": 0123 } |
| 28 | template <> struct MappingTraits<exegesis::BenchmarkMeasure> { |
| 29 | static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) { |
| 30 | Io.mapRequired("key", Obj.Key); |
| 31 | Io.mapRequired("value", Obj.Value); |
| 32 | Io.mapOptional("debug_string", Obj.DebugString); |
| 33 | } |
| 34 | static const bool flow = true; |
| 35 | }; |
| 36 | |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 37 | template <> struct MappingTraits<exegesis::InstructionBenchmarkKey> { |
| 38 | static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj) { |
| 39 | Io.mapRequired("opcode_name", Obj.OpcodeName); |
| 40 | Io.mapRequired("mode", Obj.Mode); |
| 41 | Io.mapOptional("config", Obj.Config); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 42 | } |
| 43 | }; |
| 44 | |
| 45 | template <> struct MappingTraits<exegesis::InstructionBenchmark> { |
| 46 | static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj) { |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 47 | Io.mapRequired("key", Obj.Key); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 48 | Io.mapRequired("cpu_name", Obj.CpuName); |
| 49 | Io.mapRequired("llvm_triple", Obj.LLVMTriple); |
| 50 | Io.mapRequired("num_repetitions", Obj.NumRepetitions); |
| 51 | Io.mapRequired("measurements", Obj.Measurements); |
| 52 | Io.mapRequired("error", Obj.Error); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 53 | Io.mapOptional("info", Obj.Info); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
| 57 | } // namespace yaml |
| 58 | } // namespace llvm |
| 59 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 60 | LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(exegesis::InstructionBenchmark) |
| 61 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 62 | namespace exegesis { |
| 63 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 64 | template <typename ObjectOrList> |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 65 | static ObjectOrList readYamlOrDieCommon(llvm::StringRef Filename) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 66 | std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail( |
| 67 | llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))); |
| 68 | llvm::yaml::Input Yin(*MemBuffer); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 69 | ObjectOrList Benchmark; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 70 | Yin >> Benchmark; |
| 71 | return Benchmark; |
| 72 | } |
| 73 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 74 | InstructionBenchmark |
| 75 | InstructionBenchmark::readYamlOrDie(llvm::StringRef Filename) { |
| 76 | return readYamlOrDieCommon<InstructionBenchmark>(Filename); |
| 77 | } |
| 78 | |
| 79 | std::vector<InstructionBenchmark> |
| 80 | InstructionBenchmark::readYamlsOrDie(llvm::StringRef Filename) { |
| 81 | return readYamlOrDieCommon<std::vector<InstructionBenchmark>>(Filename); |
| 82 | } |
| 83 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 84 | void InstructionBenchmark::writeYamlTo(llvm::raw_ostream &S) { |
| 85 | llvm::yaml::Output Yout(S); |
| 86 | Yout << *this; |
| 87 | } |
| 88 | |
| 89 | void InstructionBenchmark::readYamlFrom(llvm::StringRef InputContent) { |
| 90 | llvm::yaml::Input Yin(InputContent); |
| 91 | Yin >> *this; |
| 92 | } |
| 93 | |
| 94 | // FIXME: Change the API to let the caller handle errors. |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 95 | void InstructionBenchmark::writeYamlOrDie(const llvm::StringRef Filename) { |
| 96 | if (Filename == "-") { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 97 | writeYamlTo(llvm::outs()); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 98 | } else { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 99 | int ResultFD = 0; |
| 100 | llvm::cantFail(llvm::errorCodeToError( |
| 101 | openFileForWrite(Filename, ResultFD, llvm::sys::fs::F_Text))); |
| 102 | llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/); |
| 103 | writeYamlTo(Ostr); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame^] | 107 | void BenchmarkMeasureStats::push(const BenchmarkMeasure &BM) { |
| 108 | if (Key.empty()) |
| 109 | Key = BM.Key; |
| 110 | assert(Key == BM.Key); |
| 111 | ++NumValues; |
| 112 | SumValues += BM.Value; |
| 113 | MaxValue = std::max(MaxValue, BM.Value); |
| 114 | MinValue = std::min(MinValue, BM.Value); |
| 115 | } |
| 116 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 117 | } // namespace exegesis |