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 <string>
21 #include <vector>
22 
23 #include <androidfw/StringPiece.h>
24 
25 namespace aapt {
26 
27 // Record timestamps for beginning and end of a task and generate systrace json fragments.
28 // This is an in-process ftrace which has the advantage of being platform independent.
29 // These methods are NOT thread-safe since aapt2 is not multi-threaded.
30 
31 // Convenience RIAA object to automatically finish an event when object goes out of scope.
32 class Trace {
33 public:
34   Trace(const std::string& tag);
35   Trace(const std::string& tag, const std::vector<android::StringPiece>& args);
36   ~Trace();
37 };
38 
39 // Manual markers.
40 void BeginTrace(const std::string& tag);
41 void EndTrace();
42 
43 // A master trace is required to flush events to disk. Events are formatted in systrace
44 // json format.
45 class FlushTrace {
46 public:
47   explicit FlushTrace(const std::string& basepath, const std::string& tag);
48   explicit FlushTrace(const std::string& basepath, const std::string& tag,
49       const std::vector<android::StringPiece>& args);
50   explicit FlushTrace(const std::string& basepath, const std::string& tag,
51       const std::vector<std::string>& args);
52   ~FlushTrace();
53 private:
54   std::string basepath_;
55 };
56 
57 #define TRACE_CALL() Trace __t(__func__)
58 #define TRACE_NAME(tag) Trace __t(tag)
59 #define TRACE_NAME_ARGS(tag, args) Trace __t(tag, args)
60 
61 #define TRACE_FLUSH(basename, tag) FlushTrace __t(basename, tag)
62 #define TRACE_FLUSH_ARGS(basename, tag, args) FlushTrace __t(basename, tag, args)
63 } // namespace aapt
64 #endif //AAPT_TRACEBUFFER_H
65