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 #include "src/trace_processor/fuchsia_trace_utils.h"
18
19 namespace perfetto {
20 namespace trace_processor {
21 namespace fuchsia_trace_utils {
22
23 namespace {
24 constexpr uint32_t kInlineStringMarker = 0x8000;
25 constexpr uint32_t kInlineStringLengthMask = 0x7FFF;
26 } // namespace
27
IsInlineString(uint32_t string_ref)28 bool IsInlineString(uint32_t string_ref) {
29 // Treat a string ref of 0 (the empty string) as inline. The empty string is
30 // not a true entry in the string table.
31 return (string_ref & kInlineStringMarker) || (string_ref == 0);
32 }
33
ReadInlineString(const uint64_t ** current_ptr,uint32_t string_ref)34 base::StringView ReadInlineString(const uint64_t** current_ptr,
35 uint32_t string_ref) {
36 // Note that this works correctly for the empty string, where string_ref is 0.
37 size_t len = string_ref & kInlineStringLengthMask;
38 size_t len_words = (len + 7) / 8;
39 base::StringView s(reinterpret_cast<const char*>(*current_ptr), len);
40 *current_ptr += len_words;
41 return s;
42 }
43
IsInlineThread(uint32_t thread_ref)44 bool IsInlineThread(uint32_t thread_ref) {
45 return thread_ref == 0;
46 }
47
ReadInlineThread(const uint64_t ** current_ptr)48 ThreadInfo ReadInlineThread(const uint64_t** current_ptr) {
49 ThreadInfo ret;
50 ret.pid = **current_ptr;
51 (*current_ptr)++;
52 ret.tid = **current_ptr;
53 (*current_ptr)++;
54 return ret;
55 }
56
ReadTimestamp(const uint64_t ** current_ptr,uint64_t ticks_per_second)57 int64_t ReadTimestamp(const uint64_t** current_ptr, uint64_t ticks_per_second) {
58 uint64_t ticks = **current_ptr;
59 (*current_ptr)++;
60 return TicksToNs(ticks, ticks_per_second);
61 }
62
TicksToNs(uint64_t ticks,uint64_t ticks_per_second)63 int64_t TicksToNs(uint64_t ticks, uint64_t ticks_per_second) {
64 return static_cast<int64_t>(ticks * uint64_t(1000000000) / ticks_per_second);
65 }
66
67 } // namespace fuchsia_trace_utils
68 } // namespace trace_processor
69 } // namespace perfetto
70