1 //===- ActionProfiler.h - 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 9 #ifndef MLIR_TRACING_OBSERVERS_ACTIONPROFILER_H 10 #define MLIR_TRACING_OBSERVERS_ACTIONPROFILER_H 11 12 #include "mlir/Debug/ExecutionContext.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/Support/raw_ostream.h" 15 16 #include <chrono> 17 #include <mutex> 18 19 namespace mlir { 20 namespace tracing { 21 22 /// This class defines an observer that profiles events before and after 23 /// execution on the provided stream. The events are stored in the Chrome trace 24 /// event format. 25 struct ActionProfiler : public ExecutionContext::Observer { ActionProfilerActionProfiler26 ActionProfiler(raw_ostream &os) 27 : os(os), startTime(std::chrono::steady_clock::now()) { 28 os << "["; 29 } 30 ~ActionProfilerActionProfiler31 ~ActionProfiler() override { os << "]"; } 32 33 void beforeExecute(const ActionActiveStack *action, Breakpoint *breakpoint, 34 bool willExecute) override; 35 void afterExecute(const ActionActiveStack *action) override; 36 37 private: 38 void print(const ActionActiveStack *action, llvm::StringRef phase); 39 40 raw_ostream &os; 41 std::chrono::time_point<std::chrono::steady_clock> startTime; 42 bool printComma = false; 43 44 /// A mutex used to guard profiling. 45 std::mutex mutex; 46 }; 47 48 } // namespace tracing 49 } // namespace mlir 50 51 #endif // MLIR_TRACING_OBSERVERS_ACTIONPROFILER_H 52