xref: /llvm-project/flang/lib/Support/Timing.cpp (revision 310c281b020b169e760ca75f878f5873ffbb2a9f)
1 //===- Timing.cpp - Execution time measurement facilities -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Facilities to measure and provide statistics on execution time.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "flang/Support/Timing.h"
14 #include "llvm/Support/Format.h"
15 
16 class OutputStrategyText : public mlir::OutputStrategy {
17 protected:
18   static constexpr llvm::StringLiteral header = "Flang execution timing report";
19 
20 public:
21   OutputStrategyText(llvm::raw_ostream &os) : mlir::OutputStrategy(os) {}
22 
23   void printHeader(const mlir::TimeRecord &total) override {
24     // Figure out how many spaces to description name.
25     unsigned padding = (80 - header.size()) / 2;
26     os << "===" << std::string(73, '-') << "===\n";
27     os.indent(padding) << header << '\n';
28     os << "===" << std::string(73, '-') << "===\n";
29 
30     // Print the total time followed by the section headers.
31     os << llvm::format("  Total Execution Time: %.4f seconds\n\n", total.wall);
32     os << "  ----User Time----  ----Wall Time----  ----Name----\n";
33   }
34 
35   void printFooter() override { os.flush(); }
36 
37   void printTime(
38       const mlir::TimeRecord &time, const mlir::TimeRecord &total) override {
39     os << llvm::format(
40         "  %8.4f (%5.1f%%)", time.user, 100.0 * time.user / total.user);
41     os << llvm::format(
42         "  %8.4f (%5.1f%%)  ", time.wall, 100.0 * time.wall / total.wall);
43   }
44 
45   void printListEntry(llvm::StringRef name, const mlir::TimeRecord &time,
46       const mlir::TimeRecord &total, bool lastEntry) override {
47     printTime(time, total);
48     os << name << "\n";
49   }
50 
51   void printTreeEntry(unsigned indent, llvm::StringRef name,
52       const mlir::TimeRecord &time, const mlir::TimeRecord &total) override {
53     printTime(time, total);
54     os.indent(indent) << name << "\n";
55   }
56 
57   void printTreeEntryEnd(unsigned indent, bool lastEntry) override {}
58 };
59 
60 namespace Fortran::support {
61 
62 std::unique_ptr<mlir::OutputStrategy> createTimingFormatterText(
63     llvm::raw_ostream &os) {
64   return std::make_unique<OutputStrategyText>(os);
65 }
66 
67 } // namespace Fortran::support
68