Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 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 | #include "src/trace_processor/trace_storage.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | namespace perfetto { |
| 22 | namespace trace_processor { |
| 23 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 24 | TraceStorage::TraceStorage() { |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 25 | // Upid/utid 0 is reserved for invalid processes/threads. |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame^] | 26 | unique_processes_.emplace_back(0); |
| 27 | unique_threads_.emplace_back(0); |
| 28 | |
| 29 | // Reserve string ID 0 for the empty string. |
| 30 | InternString("", 0); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 33 | TraceStorage::~TraceStorage() {} |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 34 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 35 | void TraceStorage::AddSliceToCpu(uint32_t cpu, |
| 36 | uint64_t start_ns, |
| 37 | uint64_t duration_ns, |
| 38 | UniqueTid utid) { |
| 39 | cpu_events_[cpu].AddSlice(start_ns, duration_ns, utid); |
| 40 | }; |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 41 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 42 | StringId TraceStorage::InternString(const char* data, size_t length) { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame^] | 43 | uint32_t hash = 0x811c9dc5; // FNV-1a-32 offset basis. |
Lalit Maganti | 3ad1a6a | 2018-06-07 17:54:27 +0100 | [diff] [blame] | 44 | for (size_t i = 0; i < length; ++i) { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame^] | 45 | hash ^= static_cast<decltype(hash)>(data[i]); |
| 46 | hash *= 16777619; // FNV-1a-32 prime. |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 47 | } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 48 | auto id_it = string_index_.find(hash); |
| 49 | if (id_it != string_index_.end()) { |
| 50 | // TODO(lalitm): check if this DCHECK happens and if so, then change hash |
| 51 | // to 64bit. |
Lalit Maganti | 3ad1a6a | 2018-06-07 17:54:27 +0100 | [diff] [blame] | 52 | PERFETTO_DCHECK( |
| 53 | strncmp(string_pool_[id_it->second].c_str(), data, length) == 0); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 54 | return id_it->second; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 55 | } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 56 | string_pool_.emplace_back(data, length); |
| 57 | StringId string_id = string_pool_.size() - 1; |
| 58 | string_index_.emplace(hash, string_id); |
| 59 | return string_id; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 62 | void TraceStorage::ResetStorage() { |
| 63 | *this = TraceStorage(); |
| 64 | } |
| 65 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 66 | } // namespace trace_processor |
| 67 | } // namespace perfetto |