1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef AAPT_TRACEBUFFER_H 18 #define AAPT_TRACEBUFFER_H 19 20 #include <androidfw/StringPiece.h> 21 22 #include <string> 23 #include <string_view> 24 #include <vector> 25 26 namespace aapt { 27 28 // Record timestamps for beginning and end of a task and generate systrace json fragments. 29 // This is an in-process ftrace which has the advantage of being platform independent. 30 // These methods are NOT thread-safe since aapt2 is not multi-threaded. 31 32 // Convenience RAII object to automatically finish an event when object goes out of scope. 33 class Trace { 34 public: 35 Trace(const char* tag); 36 Trace(std::string tag); 37 Trace(std::string_view tag, const std::vector<android::StringPiece>& args); 38 ~Trace(); 39 40 static bool enable(bool value = true); 41 42 private: 43 std::string tag_; 44 }; 45 46 // Manual markers. 47 void BeginTrace(std::string tag); 48 void EndTrace(std::string tag); 49 50 // A main trace is required to flush events to disk. Events are formatted in systrace 51 // json format. 52 class FlushTrace { 53 public: 54 explicit FlushTrace(std::string_view basepath, std::string_view tag); 55 explicit FlushTrace(std::string_view basepath, std::string_view tag, 56 const std::vector<android::StringPiece>& args); 57 explicit FlushTrace(std::string_view basepath, std::string_view tag, 58 const std::vector<std::string>& args); 59 ~FlushTrace(); 60 61 private: 62 std::string basepath_; 63 std::string tag_; 64 }; 65 66 #define TRACE_CALL() Trace __t(__func__) 67 #define TRACE_NAME(tag) Trace __t(tag) 68 #define TRACE_NAME_ARGS(tag, args) Trace __t(tag, args) 69 70 #define TRACE_FLUSH(basename, tag) FlushTrace __t(basename, tag) 71 #define TRACE_FLUSH_ARGS(basename, tag, args) FlushTrace __t(basename, tag, args) 72 } // namespace aapt 73 #endif //AAPT_TRACEBUFFER_H 74