blob: f0333d1fe07130151530cf140b0907fde6f29a82 [file] [log] [blame]
Fabien Sanglard2d34e762019-02-21 15:13:29 -08001/*
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 Sanglard2d34e762019-02-21 15:13:29 -080020#include <androidfw/StringPiece.h>
21
Yurii Zubrytskyi76d51b12023-08-16 13:45:09 -070022#include <string>
23#include <string_view>
24#include <vector>
25
Fabien Sanglard2d34e762019-02-21 15:13:29 -080026namespace 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 Zubrytskyi76d51b12023-08-16 13:45:09 -070032// Convenience RAII object to automatically finish an event when object goes out of scope.
Fabien Sanglard2d34e762019-02-21 15:13:29 -080033class Trace {
34public:
Yurii Zubrytskyi76d51b12023-08-16 13:45:09 -070035 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
42private:
43 std::string tag_;
Fabien Sanglard2d34e762019-02-21 15:13:29 -080044};
45
46// Manual markers.
Yurii Zubrytskyi76d51b12023-08-16 13:45:09 -070047void BeginTrace(std::string tag);
48void EndTrace(std::string tag);
Fabien Sanglard2d34e762019-02-21 15:13:29 -080049
Ryan Mitchell4ea90752020-07-31 08:21:43 -070050// A main trace is required to flush events to disk. Events are formatted in systrace
Fabien Sanglard2d34e762019-02-21 15:13:29 -080051// json format.
52class FlushTrace {
53public:
Yurii Zubrytskyi76d51b12023-08-16 13:45:09 -070054 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 Sanglard2d34e762019-02-21 15:13:29 -080061private:
62 std::string basepath_;
Yurii Zubrytskyi76d51b12023-08-16 13:45:09 -070063 std::string tag_;
Fabien Sanglard2d34e762019-02-21 15:13:29 -080064};
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