MLIR  22.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"
12 #include "llvm/Support/Threading.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <chrono>
15 
16 using namespace mlir;
17 using namespace mlir::tracing;
18 
19 //===----------------------------------------------------------------------===//
20 // ActionProfiler
21 //===----------------------------------------------------------------------===//
23  Breakpoint *breakpoint, bool willExecute) {
24  print(action, "B"); // begin event.
25 }
26 
28  print(action, "E"); // end event.
29 }
30 
31 // Print an event in JSON format.
32 void ActionProfiler::print(const ActionActiveStack *action,
33  llvm::StringRef phase) {
34  // Create the event.
35  std::string str;
36  llvm::raw_string_ostream event(str);
37  event << "{";
38  event << R"("name": ")" << action->getAction().getTag() << "\", ";
39  event << R"("cat": "PERF", )";
40  event << R"("ph": ")" << phase << "\", ";
41  event << R"("pid": 0, )";
42  event << R"("tid": )" << llvm::get_threadid() << ", ";
43  auto ts = std::chrono::steady_clock::now() - startTime;
44  event << R"("ts": )"
45  << std::chrono::duration_cast<std::chrono::microseconds>(ts).count();
46  if (phase == "B") {
47  event << R"(, "args": {)";
48  event << R"("desc": ")";
49  action->getAction().print(event);
50  event << "\"}";
51  }
52  event << "}";
53 
54  // Print the event.
55  std::lock_guard<std::mutex> guard(mutex);
56  if (printComma)
57  os << ",\n";
58  printComma = true;
59  os << str;
60  os.flush();
61 }
virtual void print(raw_ostream &os) const
Definition: Action.h:50
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.