• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 INCLUDE_PERFETTO_BASE_METATRACE_H_
18 #define INCLUDE_PERFETTO_BASE_METATRACE_H_
19 
20 #include <string.h>
21 
22 #include <string>
23 
24 #include "perfetto/base/logging.h"
25 #include "perfetto/base/utils.h"
26 
27 namespace perfetto {
28 namespace base {
29 
30 class MetaTrace {
31  public:
32   static constexpr uint32_t kMainThreadCpu = 255;
33 
MetaTrace(const char * evt_name,size_t cpu)34   MetaTrace(const char* evt_name, size_t cpu) : evt_name_(evt_name), cpu_(cpu) {
35     WriteEvent('B', evt_name, cpu);
36   }
37 
MetaTrace(const std::string & str,size_t cpu)38   MetaTrace(const std::string& str, size_t cpu)
39       : str_copy_(str), evt_name_(str_copy_.c_str()), cpu_(cpu) {
40     WriteEvent('B', evt_name_, cpu);
41   }
42 
~MetaTrace()43   ~MetaTrace() { WriteEvent('E', evt_name_, cpu_); }
44 
45  private:
46   MetaTrace(const MetaTrace&) = delete;
47   MetaTrace& operator=(const MetaTrace&) = delete;
48 
49   void WriteEvent(char type, const char* evt_name, size_t cpu);
50 
51   std::string str_copy_;
52   const char* const evt_name_;
53   const size_t cpu_;
54 };
55 
56 #define PERFETTO_METATRACE_UID2(a, b) a##b
57 #define PERFETTO_METATRACE_UID(x) PERFETTO_METATRACE_UID2(metatrace_, x)
58 #if PERFETTO_DCHECK_IS_ON() && PERFETTO_BUILDFLAG(PERFETTO_STANDALONE_BUILD)
59 
60 #define PERFETTO_METATRACE(...) \
61   ::perfetto::base::MetaTrace PERFETTO_METATRACE_UID(__COUNTER__)(__VA_ARGS__)
62 #else
63 #define PERFETTO_METATRACE(...) ::perfetto::base::ignore_result(__VA_ARGS__)
64 #endif
65 
66 }  // namespace base
67 }  // namespace perfetto
68 
69 #endif  // INCLUDE_PERFETTO_BASE_METATRACE_H_
70