99
1010#include " rmt_adapter_info.h"
1111#include " rmt_address_helper.h"
12+ #include " rmt_memory_aliasing_timeline.h"
1213#include " rmt_assert.h"
1314#include " rmt_constants.h"
1415#include " rmt_data_snapshot.h"
@@ -166,6 +167,8 @@ RmtErrorCode DestroySnapshotWriter(RmtDataSet* data_set)
166167 return kRmtOk ;
167168}
168169
170+ using namespace RmtMemoryAliasingTimelineAlgorithm ;
171+
169172// Map used to lookup unique resource ID hash using the original resource ID as the key.
170173static std::unordered_map<RmtResourceIdentifier, RmtResourceIdentifier> unique_resource_id_lookup_map;
171174
@@ -653,7 +656,7 @@ static void PerformFree(RmtDataSet* data_set, void* pointer)
653656}
654657
655658// Allocate memory for a snapshot.
656- static RmtErrorCode AllocateMemoryForSnapshot (RmtDataSet* data_set, RmtDataSnapshot* out_snapshot)
659+ static RmtErrorCode AllocateMemoryForSnapshot (RmtDataSet* data_set, RmtDataSnapshot* out_snapshot, bool enable_aliased_resource_usage_sizes )
657660{
658661 // Set a pointer to parent data set.
659662 out_snapshot->data_set = data_set;
@@ -687,7 +690,8 @@ static RmtErrorCode AllocateMemoryForSnapshot(RmtDataSet* data_set, RmtDataSnaps
687690 out_snapshot->resource_list_buffer ,
688691 resource_list_buffer_size,
689692 &out_snapshot->virtual_allocation_list ,
690- data_set->data_profile .max_concurrent_resources );
693+ data_set->data_profile .max_concurrent_resources ,
694+ enable_aliased_resource_usage_sizes);
691695 RMT_ASSERT (error_code == kRmtOk );
692696 RMT_RETURN_ON_ERROR (error_code == kRmtOk , error_code);
693697 }
@@ -710,7 +714,25 @@ static RmtErrorCode ProcessTokenForSnapshot(RmtDataSet* data_set, RmtToken* curr
710714 {
711715 case kRmtTokenTypeVirtualFree :
712716 {
713-
717+ const RmtVirtualAllocation* virtual_allocation = nullptr ;
718+ RmtErrorCode result = RmtVirtualAllocationListGetAllocationForAddress (
719+ &(out_snapshot->virtual_allocation_list ), current_token->virtual_free_token .virtual_address , &virtual_allocation);
720+ // Remove the virtual allocation if it is being tracked and a virtual allocation could be found.
721+ if ((result == kRmtOk ) && (out_snapshot->resource_list .enable_aliased_resource_usage_sizes ))
722+ {
723+ // Update memory sizes grouped by resource usage types taking into account overlapped aliased resources.
724+ RmtMemoryAliasingCalculator* memory_aliasing_calculator = RmtMemoryAliasingCalculatorInstance ();
725+ RMT_ASSERT (memory_aliasing_calculator != nullptr );
726+ memory_aliasing_calculator->DestroyAllocation (virtual_allocation->allocation_identifier );
727+ SizePerResourceUsageType sizes_per_resource_usage_type;
728+ SizeType unbound_size;
729+ memory_aliasing_calculator->CalculateSizes (sizes_per_resource_usage_type, unbound_size);
730+ for (int usage_index = 0 ; usage_index < RmtResourceUsageType::kRmtResourceUsageTypeCount ; usage_index++)
731+ {
732+ out_snapshot->resource_list .total_resource_usage_aliased_size [usage_index] = sizes_per_resource_usage_type.size_ [usage_index];
733+ }
734+ out_snapshot->resource_list .total_resource_usage_aliased_size [kRmtResourceUsageTypeFree ] = unbound_size;
735+ }
714736 error_code = RmtVirtualAllocationListRemoveAllocation (&out_snapshot->virtual_allocation_list , current_token->virtual_free_token .virtual_address );
715737 RMT_ASSERT (error_code == kRmtOk );
716738 // RMT_RETURN_ON_ERROR(error_code == kRmtOk, error_code);
@@ -826,6 +848,13 @@ static RmtErrorCode ProcessTokenForSnapshot(RmtDataSet* data_set, RmtToken* curr
826848 kDummyHeapPref ,
827849 RmtOwnerType::kRmtOwnerTypeClientDriver ,
828850 allocation_identifier);
851+
852+ if (out_snapshot->resource_list .enable_aliased_resource_usage_sizes )
853+ {
854+ RmtMemoryAliasingCalculator* memory_aliasing_calculator = RmtMemoryAliasingCalculatorInstance ();
855+ RMT_ASSERT (memory_aliasing_calculator != nullptr );
856+ memory_aliasing_calculator->CreateAllocation (allocation_identifier, current_token->resource_bind_token .size_in_bytes );
857+ }
829858 }
830859 else if (error_code == kRmtErrorResourceAlreadyBound )
831860 {
@@ -925,9 +954,11 @@ static RmtErrorCode ProcessTokenForSnapshot(RmtDataSet* data_set, RmtToken* curr
925954 case kRmtTokenTypeVirtualAllocate :
926955 {
927956 // The byte offset of the token in the data stream is used to uniquely identify this allocation.
928- // The offset is used rather than the virtual allocation address in case there are allocations/frees then another allocation with the same base address.
957+ // The offset is used rather than the virtual allocation address in case there are allocations/frees
958+ // and then another allocation is made with the same base address.
929959 uint64_t allocation_identifier = current_token->common .offset ;
930- error_code = RmtVirtualAllocationListAddAllocation (&out_snapshot->virtual_allocation_list ,
960+
961+ error_code = RmtVirtualAllocationListAddAllocation (&out_snapshot->virtual_allocation_list ,
931962 current_token->common .timestamp ,
932963 current_token->virtual_allocate_token .virtual_address ,
933964 (int32_t )(current_token->virtual_allocate_token .size_in_bytes >> 12 ),
@@ -936,6 +967,14 @@ static RmtErrorCode ProcessTokenForSnapshot(RmtDataSet* data_set, RmtToken* curr
936967 allocation_identifier);
937968 RMT_ASSERT (error_code == kRmtOk );
938969 RMT_RETURN_ON_ERROR (error_code == kRmtOk , error_code);
970+
971+ if (out_snapshot->resource_list .enable_aliased_resource_usage_sizes )
972+ {
973+ // Track virtual allocation for aliased resource size calculation.
974+ RmtMemoryAliasingCalculator* memory_aliasing_calculator = RmtMemoryAliasingCalculatorInstance ();
975+ RMT_ASSERT (memory_aliasing_calculator != nullptr );
976+ memory_aliasing_calculator->CreateAllocation (allocation_identifier, current_token->virtual_allocate_token .size_in_bytes );
977+ }
939978 }
940979 break ;
941980
@@ -1397,9 +1436,13 @@ static int32_t UpdateSeriesValuesFromCurrentSnapshot(const RmtDataSnapshot* curr
13971436
13981437 case kRmtDataTimelineTypeResourceUsageVirtualSize :
13991438 {
1439+ // For Resource Usage Virtual Size timeline type, aliased sizing should be enabled
1440+ // (disabled for all other timeline types).
1441+ RMT_ASSERT (current_snapshot->resource_list .enable_aliased_resource_usage_sizes );
1442+
14001443 for (int32_t current_resource_index = 0 ; current_resource_index < kRmtResourceUsageTypeCount ; ++current_resource_index)
14011444 {
1402- const uint64_t resource_size_for_usage_type = current_snapshot->resource_list .resource_usage_size [current_resource_index];
1445+ const uint64_t resource_size_for_usage_type = current_snapshot->resource_list .total_resource_usage_aliased_size [current_resource_index];
14031446
14041447 // Write this to the correct slot in the series.
14051448 if (current_resource_index == kRmtResourceUsageTypeHeap )
@@ -1488,11 +1531,15 @@ static RmtErrorCode TimelineGeneratorParseData(RmtDataSet* data_set, RmtDataTime
14881531{
14891532 RMT_ASSERT (data_set);
14901533
1534+ // Reset the cancel flag.
1535+ data_set->flags .cancel_background_task_flag = false ;
1536+
14911537 // Allocate temporary snapshot.
14921538 RmtDataSnapshot* temp_snapshot = (RmtDataSnapshot*)PerformAllocation (data_set, sizeof (RmtDataSnapshot), alignof (RmtDataSnapshot));
14931539 RMT_ASSERT (temp_snapshot);
14941540 RMT_RETURN_ON_ERROR (temp_snapshot, kRmtErrorOutOfMemory );
1495- RmtErrorCode error_code = AllocateMemoryForSnapshot (data_set, temp_snapshot);
1541+ RmtErrorCode error_code =
1542+ AllocateMemoryForSnapshot (data_set, temp_snapshot, timeline_type == RmtDataTimelineType::kRmtDataTimelineTypeResourceUsageVirtualSize );
14961543 RMT_ASSERT (error_code == kRmtOk );
14971544 RMT_RETURN_ON_ERROR (error_code == kRmtOk , error_code);
14981545
@@ -1534,7 +1581,7 @@ static RmtErrorCode TimelineGeneratorParseData(RmtDataSet* data_set, RmtDataTime
15341581
15351582 // if the heap has something there, then add it.
15361583 int32_t last_value_index = -1 ;
1537- while (!RmtStreamMergerIsEmpty (&data_set->stream_merger ))
1584+ while (!RmtStreamMergerIsEmpty (&data_set->stream_merger ) && ! RmtDataSetIsBackgroundTaskCancelled (data_set) )
15381585 {
15391586 // grab the next token from the heap.
15401587 RmtToken current_token = {};
@@ -1561,6 +1608,10 @@ static RmtErrorCode TimelineGeneratorParseData(RmtDataSet* data_set, RmtDataTime
15611608 }
15621609
15631610 // clean up temporary structures we allocated to construct the timeline.
1611+ if (timeline_type == RmtDataTimelineType::kRmtDataTimelineTypeResourceUsageVirtualSize )
1612+ {
1613+ RmtMemoryAliasingCalculatorCleanup ();
1614+ }
15641615 RmtDataSnapshotDestroy (temp_snapshot);
15651616 PerformFree (data_set, temp_snapshot);
15661617 return kRmtOk ;
@@ -1745,8 +1796,13 @@ static RmtErrorCode MergeResourceMemoryRegions(const RmtVirtualAllocation* virtu
17451796 {
17461797 const RmtGpuAddress allocation_base_address = virtual_allocation->base_address ;
17471798 const RmtResource* current_resource = virtual_allocation->resources [current_resource_index];
1748- bound_memory_regions.push_back (RegionOffsets{current_resource->address - allocation_base_address,
1749- (current_resource->address - allocation_base_address) + current_resource->size_in_bytes });
1799+
1800+ // Skip over Heap type resources.
1801+ if (current_resource->resource_type != RmtResourceType::kRmtResourceTypeHeap )
1802+ {
1803+ bound_memory_regions.push_back (RegionOffsets{current_resource->address - allocation_base_address,
1804+ (current_resource->address - allocation_base_address) + current_resource->size_in_bytes });
1805+ }
17501806 }
17511807
17521808 // Sort the bound memory regions by starting offsets.
@@ -1800,7 +1856,6 @@ static RmtErrorCode SnapshotGeneratorAddUnboundResources(RmtDataSnapshot* snapsh
18001856 std::vector<RmtMemoryRegion> unbound_regions; // /< The list of unbound memory regions.
18011857 uint64_t allocation_size_in_bytes = RmtGetAllocationSizeInBytes (virtual_allocation->size_in_4kb_page ,
18021858 kRmtPageSize4Kb ); // /< The virtual allocation size in bytes.
1803-
18041859 if (bound_regions.size () < 1 )
18051860 {
18061861 // Create an unbound region covering the entire virtual allocation.
@@ -1866,7 +1921,7 @@ static RmtErrorCode SnapshotGeneratorAddUnboundResources(RmtDataSnapshot* snapsh
18661921 return kRmtOk ;
18671922}
18681923
1869- // Calculate the aliased size for each resource.
1924+ // Calculate the size after aliasing for each resource.
18701925static RmtErrorCode SnapshotGeneratorCalculateAliasedResourceSizes (RmtDataSnapshot* snapshot)
18711926{
18721927 uint64_t resource_usage_mask = (1ULL << (kRmtResourceUsageTypeCount - 1 )) - 1 ;
@@ -2007,7 +2062,7 @@ RmtErrorCode RmtDataSetGenerateSnapshot(RmtDataSet* data_set, RmtSnapshotPoint*
20072062 // set up the snapshot.
20082063 memcpy (out_snapshot->name , snapshot_point->name , RMT_MINIMUM (strlen (snapshot_point->name ), sizeof (out_snapshot->name )));
20092064 out_snapshot->timestamp = snapshot_point->timestamp ;
2010- RmtErrorCode error_code = AllocateMemoryForSnapshot (data_set, out_snapshot);
2065+ RmtErrorCode error_code = AllocateMemoryForSnapshot (data_set, out_snapshot, false );
20112066
20122067 out_snapshot->maximum_physical_memory_in_bytes = RmtDataSetGetTotalVideoMemoryInBytes (data_set);
20132068
@@ -2247,3 +2302,23 @@ uint64_t RmtDataSetGetTotalVideoMemoryInBytes(const RmtDataSet* data_set)
22472302{
22482303 return data_set->segment_info [kRmtHeapTypeLocal ].size + data_set->segment_info [kRmtHeapTypeInvisible ].size ;
22492304}
2305+
2306+ void RmtDataSetCancelBackgroundTask (RmtDataSet* data_set)
2307+ {
2308+ RMT_ASSERT (data_set != nullptr );
2309+ data_set->flags .cancel_background_task_flag = true ;
2310+ }
2311+
2312+ bool RmtDataSetIsBackgroundTaskCancelled (const RmtDataSet* data_set)
2313+ {
2314+ RMT_ASSERT (data_set != nullptr );
2315+
2316+ bool result = false ;
2317+
2318+ if (data_set->flags .cancel_background_task_flag )
2319+ {
2320+ result = true ;
2321+ }
2322+
2323+ return result;
2324+ }
0 commit comments