| 196 | |
| 197 | namespace { |
| 198 | void UpdateStats(OpKernelContext* context, |
| 199 | StepStatsCollector* step_stats_collector, |
| 200 | NodeExecStats* stats) { |
| 201 | for (const auto& allocator_pair : context->ConsumeWrappedAllocators()) { |
| 202 | AllocatorMemoryUsed* memory = stats->add_memory(); |
| 203 | memory->set_allocator_name(allocator_pair.first->Name()); |
| 204 | auto sizes = allocator_pair.second->GetSizes(); |
| 205 | memory->set_total_bytes(std::get<0>(sizes)); |
| 206 | memory->set_peak_bytes(std::get<1>(sizes)); |
| 207 | memory->set_live_bytes(std::get<2>(sizes)); |
| 208 | |
| 209 | absl::optional<AllocatorStats> allocator_stats = |
| 210 | allocator_pair.first->GetStats(); |
| 211 | if (allocator_stats) { |
| 212 | memory->set_allocator_bytes_in_use(allocator_stats->bytes_in_use); |
| 213 | } |
| 214 | allocator_pair.second->GetRecordsAndUnRef(); |
| 215 | } |
| 216 | auto* ms = stats->mutable_memory_stats(); |
| 217 | ms->set_temp_memory_size(context->temp_memory_allocated()); |
| 218 | for (const auto& alloc_id : context->persistent_alloc_ids()) { |
| 219 | ms->mutable_persistent_tensor_alloc_ids()->Add(alloc_id); |
| 220 | } |
| 221 | |
| 222 | ms->set_persistent_memory_size(context->persistent_memory_allocated()); |
| 223 | step_stats_collector->Finalize(); |
| 224 | } |
| 225 | |
| 226 | // In certain contexts (e.g. TPU async executions), the CancellationManager is |
| 227 | // used to shut down the device in error scenarios (as opposed to using the |
no test coverage detected