Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 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 | |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 20 | #include <androidfw/StringPiece.h> |
| 21 | |
Yurii Zubrytskyi | 76d51b1 | 2023-08-16 13:45:09 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <string_view> |
| 24 | #include <vector> |
| 25 | |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 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 | |
Yurii Zubrytskyi | 76d51b1 | 2023-08-16 13:45:09 -0700 | [diff] [blame] | 32 | // Convenience RAII object to automatically finish an event when object goes out of scope. |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 33 | class Trace { |
| 34 | public: |
Yurii Zubrytskyi | 76d51b1 | 2023-08-16 13:45:09 -0700 | [diff] [blame] | 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_; |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | // Manual markers. |
Yurii Zubrytskyi | 76d51b1 | 2023-08-16 13:45:09 -0700 | [diff] [blame] | 47 | void BeginTrace(std::string tag); |
| 48 | void EndTrace(std::string tag); |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 49 | |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 50 | // A main trace is required to flush events to disk. Events are formatted in systrace |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 51 | // json format. |
| 52 | class FlushTrace { |
| 53 | public: |
Yurii Zubrytskyi | 76d51b1 | 2023-08-16 13:45:09 -0700 | [diff] [blame] | 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 | |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 61 | private: |
| 62 | std::string basepath_; |
Yurii Zubrytskyi | 76d51b1 | 2023-08-16 13:45:09 -0700 | [diff] [blame] | 63 | std::string tag_; |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 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 |