blob: 8b3984f14cdef04494789e449e7eb04024f27c8e [file] [log] [blame]
Florian Mayer438b5ab2019-05-02 11:18:06 +01001/*
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_HEAP_PROFILE_TRACKER_H_
18#define SRC_TRACE_PROCESSOR_HEAP_PROFILE_TRACKER_H_
19
20#include <deque>
21
Florian Mayer5716fc12019-06-24 11:50:51 -070022#include "perfetto/ext/base/optional.h"
23
Primiano Tucci355b8c82019-08-29 08:37:51 +020024#include "protos/perfetto/trace/profiling/profile_common.pbzero.h"
25#include "protos/perfetto/trace/profiling/profile_packet.pbzero.h"
Oystein Eftevaag5419c582019-08-21 13:58:49 -070026#include "src/trace_processor/stack_profile_tracker.h"
Florian Mayer438b5ab2019-05-02 11:18:06 +010027#include "src/trace_processor/trace_storage.h"
28
Florian Mayer438b5ab2019-05-02 11:18:06 +010029namespace perfetto {
30namespace trace_processor {
31
32class TraceProcessorContext;
33
34class HeapProfileTracker {
35 public:
Florian Mayer438b5ab2019-05-02 11:18:06 +010036 struct SourceAllocation {
37 uint64_t pid = 0;
Florian Mayer12c89992019-05-14 13:03:10 +010038 // This is int64_t, because we get this from the TraceSorter which also
39 // converts this for us.
40 int64_t timestamp = 0;
Oystein Eftevaag5419c582019-08-21 13:58:49 -070041 StackProfileTracker::SourceCallstackId callstack_id = 0;
Florian Mayer438b5ab2019-05-02 11:18:06 +010042 uint64_t self_allocated = 0;
43 uint64_t self_freed = 0;
44 uint64_t alloc_count = 0;
45 uint64_t free_count = 0;
46 };
47
Florian Mayer252e4482019-09-30 17:26:36 +010048 void SetProfilePacketIndex(uint64_t id);
49
Florian Mayer438b5ab2019-05-02 11:18:06 +010050 explicit HeapProfileTracker(TraceProcessorContext* context);
51
Florian Mayer981b2742019-06-14 15:07:18 +010052 void StoreAllocation(SourceAllocation);
Oystein Eftevaag5419c582019-08-21 13:58:49 -070053
Florian Mayer981b2742019-06-14 15:07:18 +010054 // Call after the last profile packet of a dump to commit the allocations
Florian Mayer5716fc12019-06-24 11:50:51 -070055 // that had been stored using StoreAllocation and clear internal indices
Florian Mayer981b2742019-06-14 15:07:18 +010056 // for that dump.
Oystein Eftevaag24bba372019-10-14 11:55:43 -070057 void FinalizeProfile(StackProfileTracker* stack_profile_tracker,
58 const StackProfileTracker::InternLookup* lookup);
Ioannis Ilkose6727552019-08-14 15:10:59 +010059
Florian Mayer981b2742019-06-14 15:07:18 +010060 // Only commit the allocations that had been stored using StoreAllocations.
61 // This is only needed in tests, use FinalizeProfile instead.
Oystein Eftevaag24bba372019-10-14 11:55:43 -070062 void CommitAllocations(StackProfileTracker* stack_profile_tracker,
63 const StackProfileTracker::InternLookup* lookup);
Florian Mayer438b5ab2019-05-02 11:18:06 +010064
65 ~HeapProfileTracker();
66
67 private:
Oystein Eftevaag5419c582019-08-21 13:58:49 -070068 void AddAllocation(
Oystein Eftevaag24bba372019-10-14 11:55:43 -070069 StackProfileTracker* stack_profile_tracker,
Oystein Eftevaag5419c582019-08-21 13:58:49 -070070 const SourceAllocation&,
71 const StackProfileTracker::InternLookup* intern_lookup = nullptr);
Florian Mayer438b5ab2019-05-02 11:18:06 +010072
Florian Mayer981b2742019-06-14 15:07:18 +010073 std::vector<SourceAllocation> pending_allocs_;
Florian Mayer438b5ab2019-05-02 11:18:06 +010074
Florian Mayer98ff2f82019-05-14 16:28:11 +010075 std::unordered_map<std::pair<UniquePid, int64_t>,
76 TraceStorage::HeapProfileAllocations::Row>
77 prev_alloc_;
78 std::unordered_map<std::pair<UniquePid, int64_t>,
79 TraceStorage::HeapProfileAllocations::Row>
80 prev_free_;
81
Florian Mayer438b5ab2019-05-02 11:18:06 +010082 TraceProcessorContext* const context_;
Florian Mayer252e4482019-09-30 17:26:36 +010083 uint64_t last_profile_packet_index_ = 0;
Florian Mayer438b5ab2019-05-02 11:18:06 +010084 const StringId empty_;
85};
86
87} // namespace trace_processor
88} // namespace perfetto
89
90#endif // SRC_TRACE_PROCESSOR_HEAP_PROFILE_TRACKER_H_