Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 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_HEAP_PROFILE_TRACKER_H_ |
| 18 | #define SRC_TRACE_PROCESSOR_HEAP_PROFILE_TRACKER_H_ |
| 19 | |
| 20 | #include <deque> |
| 21 | |
Florian Mayer | 5716fc1 | 2019-06-24 11:50:51 -0700 | [diff] [blame] | 22 | #include "perfetto/ext/base/optional.h" |
| 23 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 24 | #include "protos/perfetto/trace/profiling/profile_common.pbzero.h" |
| 25 | #include "protos/perfetto/trace/profiling/profile_packet.pbzero.h" |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 26 | #include "src/trace_processor/stack_profile_tracker.h" |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 27 | #include "src/trace_processor/trace_storage.h" |
| 28 | |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 29 | namespace perfetto { |
| 30 | namespace trace_processor { |
| 31 | |
| 32 | class TraceProcessorContext; |
| 33 | |
| 34 | class HeapProfileTracker { |
| 35 | public: |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 36 | struct SourceAllocation { |
| 37 | uint64_t pid = 0; |
Florian Mayer | 12c8999 | 2019-05-14 13:03:10 +0100 | [diff] [blame] | 38 | // This is int64_t, because we get this from the TraceSorter which also |
| 39 | // converts this for us. |
| 40 | int64_t timestamp = 0; |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 41 | StackProfileTracker::SourceCallstackId callstack_id = 0; |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 42 | 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 Mayer | 252e448 | 2019-09-30 17:26:36 +0100 | [diff] [blame] | 48 | void SetProfilePacketIndex(uint64_t id); |
| 49 | |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 50 | explicit HeapProfileTracker(TraceProcessorContext* context); |
| 51 | |
Florian Mayer | 981b274 | 2019-06-14 15:07:18 +0100 | [diff] [blame] | 52 | void StoreAllocation(SourceAllocation); |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 53 | |
Florian Mayer | 981b274 | 2019-06-14 15:07:18 +0100 | [diff] [blame] | 54 | // Call after the last profile packet of a dump to commit the allocations |
Florian Mayer | 5716fc1 | 2019-06-24 11:50:51 -0700 | [diff] [blame] | 55 | // that had been stored using StoreAllocation and clear internal indices |
Florian Mayer | 981b274 | 2019-06-14 15:07:18 +0100 | [diff] [blame] | 56 | // for that dump. |
Oystein Eftevaag | 24bba37 | 2019-10-14 11:55:43 -0700 | [diff] [blame] | 57 | void FinalizeProfile(StackProfileTracker* stack_profile_tracker, |
| 58 | const StackProfileTracker::InternLookup* lookup); |
Ioannis Ilkos | e672755 | 2019-08-14 15:10:59 +0100 | [diff] [blame] | 59 | |
Florian Mayer | 981b274 | 2019-06-14 15:07:18 +0100 | [diff] [blame] | 60 | // Only commit the allocations that had been stored using StoreAllocations. |
| 61 | // This is only needed in tests, use FinalizeProfile instead. |
Oystein Eftevaag | 24bba37 | 2019-10-14 11:55:43 -0700 | [diff] [blame] | 62 | void CommitAllocations(StackProfileTracker* stack_profile_tracker, |
| 63 | const StackProfileTracker::InternLookup* lookup); |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 64 | |
| 65 | ~HeapProfileTracker(); |
| 66 | |
| 67 | private: |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 68 | void AddAllocation( |
Oystein Eftevaag | 24bba37 | 2019-10-14 11:55:43 -0700 | [diff] [blame] | 69 | StackProfileTracker* stack_profile_tracker, |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 70 | const SourceAllocation&, |
| 71 | const StackProfileTracker::InternLookup* intern_lookup = nullptr); |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 72 | |
Florian Mayer | 981b274 | 2019-06-14 15:07:18 +0100 | [diff] [blame] | 73 | std::vector<SourceAllocation> pending_allocs_; |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 74 | |
Florian Mayer | 98ff2f8 | 2019-05-14 16:28:11 +0100 | [diff] [blame] | 75 | 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 Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 82 | TraceProcessorContext* const context_; |
Florian Mayer | 252e448 | 2019-09-30 17:26:36 +0100 | [diff] [blame] | 83 | uint64_t last_profile_packet_index_ = 0; |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 84 | const StringId empty_; |
| 85 | }; |
| 86 | |
| 87 | } // namespace trace_processor |
| 88 | } // namespace perfetto |
| 89 | |
| 90 | #endif // SRC_TRACE_PROCESSOR_HEAP_PROFILE_TRACKER_H_ |