blob: 38cb4143a10caf40750a39871bf1cc409ff6f833 [file] [log] [blame]
Lalit Maganticaed37e2018-06-01 03:03:08 +01001/*
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
21namespace perfetto {
22namespace trace_processor {
23
Isabelle Taylor47328cf2018-06-12 14:33:59 +010024TraceStorage::TraceStorage() {
Isabelle Taylor68e42192018-06-19 16:19:31 +010025 // Upid/utid 0 is reserved for invalid processes/threads.
Primiano Tuccib75dcee2018-08-08 12:21:36 +010026 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 Taylor47328cf2018-06-12 14:33:59 +010031}
32
Lalit Maganti35622b72018-06-06 12:03:11 +010033TraceStorage::~TraceStorage() {}
Lalit Maganticaed37e2018-06-01 03:03:08 +010034
Isabelle Taylora0a22972018-08-03 12:06:12 +010035void 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 Taylor3dd366c2018-06-22 16:21:41 +010041
Primiano Tucci0d72a312018-08-07 14:42:45 +010042StringId TraceStorage::InternString(const char* data, size_t length) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010043 uint32_t hash = 0x811c9dc5; // FNV-1a-32 offset basis.
Lalit Maganti3ad1a6a2018-06-07 17:54:27 +010044 for (size_t i = 0; i < length; ++i) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010045 hash ^= static_cast<decltype(hash)>(data[i]);
46 hash *= 16777619; // FNV-1a-32 prime.
Lalit Maganticaed37e2018-06-01 03:03:08 +010047 }
Lalit Maganti35622b72018-06-06 12:03:11 +010048 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 Maganti3ad1a6a2018-06-07 17:54:27 +010052 PERFETTO_DCHECK(
53 strncmp(string_pool_[id_it->second].c_str(), data, length) == 0);
Lalit Maganti35622b72018-06-06 12:03:11 +010054 return id_it->second;
Lalit Maganticaed37e2018-06-01 03:03:08 +010055 }
Lalit Maganti35622b72018-06-06 12:03:11 +010056 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 Maganticaed37e2018-06-01 03:03:08 +010060}
61
Isabelle Taylora0a22972018-08-03 12:06:12 +010062void TraceStorage::ResetStorage() {
63 *this = TraceStorage();
64}
65
Lalit Maganticaed37e2018-06-01 03:03:08 +010066} // namespace trace_processor
67} // namespace perfetto