MLIR  19.0.0git
ActionProfiler.cpp
Go to the documentation of this file.
1 //===- ActionProfiler.cpp - Profiling Actions *- C++ -*-=====================//
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 
11 #include "mlir/IR/Action.h"
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <chrono>
17 
18 using namespace mlir;
19 using namespace mlir::tracing;
20 
21 //===----------------------------------------------------------------------===//
22 // ActionProfiler
23 //===----------------------------------------------------------------------===//
25  Breakpoint *breakpoint, bool willExecute) {
26  print(action, "B"); // begin event.
27 }
28 
30  print(action, "E"); // end event.
31 }
32 
33 // Print an event in JSON format.
34 void ActionProfiler::print(const ActionActiveStack *action,
35  llvm::StringRef phase) {
36  // Create the event.
37  std::string str;
38  llvm::raw_string_ostream event(str);
39  event << "{";
40  event << R"("name": ")" << action->getAction().getTag() << "\", ";
41  event << R"("cat": "PERF", )";
42  event << R"("ph": ")" << phase << "\", ";
43  event << R"("pid": 0, )";
44  event << R"("tid": )" << llvm::get_threadid() << ", ";
45  auto ts = std::chrono::steady_clock::now() - startTime;
46  event << R"("ts": )"
47  << std::chrono::duration_cast<std::chrono::microseconds>(ts).count();
48  if (phase == "B") {
49  event << R"(, "args": {)";
50  event << R"("desc": ")";
51  action->getAction().print(event);
52  event << "\"}";
53  }
54  event << "}";
55 
56  // Print the event.
57  std::lock_guard<std::mutex> guard(mutex);
58  if (printComma)
59  os << ",\n";
60  printComma = true;
61  os << event.str();
62  os.flush();
63 }
virtual void print(raw_ostream &os) const
Definition: Action.h:51
virtual StringRef getTag() const =0
Return a string "tag" which intends to uniquely identify this type of action.
This abstract class represents a breakpoint.
Include the generated interface declarations.
This class is used to keep track of the active actions in the stack.
const Action & getAction() const
void beforeExecute(const ActionActiveStack *action, Breakpoint *breakpoint, bool willExecute) override
This method is called before the Action is executed If a breakpoint was hit, it is passed as an argum...
void afterExecute(const ActionActiveStack *action) override
This method is called after the Action is executed, if it was executed.