• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SRC_TRACE_PROCESSOR_IMPORTERS_SYSCALLS_SYSCALL_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_SYSCALLS_SYSCALL_TRACKER_H_
19 
20 #include <limits>
21 #include <tuple>
22 
23 #include "perfetto/ext/base/string_view.h"
24 #include "src/trace_processor/importers/common/slice_tracker.h"
25 #include "src/trace_processor/importers/common/track_tracker.h"
26 #include "src/trace_processor/storage/trace_storage.h"
27 #include "src/trace_processor/types/destructible.h"
28 #include "src/trace_processor/types/trace_processor_context.h"
29 
30 namespace perfetto {
31 namespace trace_processor {
32 
33 static constexpr size_t kMaxSyscalls = 550;
34 
35 enum Architecture {
36   kUnknown = 0,
37   kArmEabi,  // 32-bit kernel running a 32-bit process (most old devices).
38   kAarch32,  // 64-bit kernel running a 32-bit process (should be rare).
39   kAarch64,  // 64-bit kernel running a 64-bit process (most new devices).
40   kX86_64,
41   kX86,
42 };
43 
44 class SyscallTracker : public Destructible {
45  public:
46   SyscallTracker(const SyscallTracker&) = delete;
47   SyscallTracker& operator=(const SyscallTracker&) = delete;
48   ~SyscallTracker() override;
GetOrCreate(TraceProcessorContext * context)49   static SyscallTracker* GetOrCreate(TraceProcessorContext* context) {
50     if (!context->syscall_tracker) {
51       context->syscall_tracker.reset(new SyscallTracker(context));
52     }
53     return static_cast<SyscallTracker*>(context->syscall_tracker.get());
54   }
55 
56   void SetArchitecture(Architecture architecture);
57 
Enter(int64_t ts,UniqueTid utid,uint32_t syscall_num)58   void Enter(int64_t ts, UniqueTid utid, uint32_t syscall_num) {
59     StringId name = SyscallNumberToStringId(syscall_num);
60     if (!name.is_null()) {
61       TrackId track_id = context_->track_tracker->InternThreadTrack(utid);
62       context_->slice_tracker->Begin(ts, track_id, kNullStringId /* cat */,
63                                      name);
64     }
65   }
66 
Exit(int64_t ts,UniqueTid utid,uint32_t syscall_num)67   void Exit(int64_t ts, UniqueTid utid, uint32_t syscall_num) {
68     StringId name = SyscallNumberToStringId(syscall_num);
69     if (!name.is_null()) {
70       TrackId track_id = context_->track_tracker->InternThreadTrack(utid);
71       context_->slice_tracker->End(ts, track_id, kNullStringId /* cat */, name);
72     }
73   }
74 
75  private:
76   explicit SyscallTracker(TraceProcessorContext*);
77 
78   TraceProcessorContext* const context_;
79 
SyscallNumberToStringId(uint32_t syscall_num)80   inline StringId SyscallNumberToStringId(uint32_t syscall_num) {
81     if (syscall_num > kMaxSyscalls)
82       return kNullStringId;
83     // We see two write sys calls around each userspace slice that is going via
84     // trace_marker, this violates the assumption that userspace slices are
85     // perfectly nested. For the moment ignore all write sys calls.
86     // TODO(hjd): Remove this limitation.
87     StringId id = arch_syscall_to_string_id_[syscall_num];
88     if (id == sys_write_string_id_)
89       return kNullStringId;
90     return id;
91   }
92 
93   // This is table from platform specific syscall number directly to
94   // the relevant StringId (this avoids having to always do two conversions).
95   std::array<StringId, kMaxSyscalls> arch_syscall_to_string_id_{};
96   StringId sys_write_string_id_ = std::numeric_limits<StringId>::max();
97 };
98 
99 }  // namespace trace_processor
100 }  // namespace perfetto
101 
102 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_SYSCALLS_SYSCALL_TRACKER_H_
103