Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 6 | #include <armnn/utility/IgnoreUnused.hpp> |
| 7 | #include <FileOnlyProfilingConnection.hpp> |
| 8 | #include <Filesystem.hpp> |
| 9 | #include <NullProfilingConnection.hpp> |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 10 | #include <ProfilingService.hpp> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 11 | #include <Runtime.hpp> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 12 | #include "PrintPacketHeaderHandler.hpp" |
| 13 | #include "TestTimelinePacketHandler.hpp" |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 14 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 15 | #include <boost/filesystem.hpp> |
| 16 | #include <boost/numeric/conversion/cast.hpp> |
| 17 | #include <boost/test/unit_test.hpp> |
| 18 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 19 | #include <cstdio> |
| 20 | #include <fstream> |
| 21 | #include <sstream> |
| 22 | #include <sys/stat.h> |
| 23 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 24 | using namespace armnn::profiling; |
| 25 | using namespace armnn; |
| 26 | |
| 27 | using namespace std::chrono_literals; |
| 28 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 29 | class FileOnlyHelperService : public ProfilingService |
| 30 | { |
| 31 | public: |
| 32 | // Wait for a notification from the send thread |
| 33 | bool WaitForPacketsSent(uint32_t timeout = 1000) |
| 34 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 35 | return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 36 | } |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 37 | armnn::profiling::ProfilingService m_ProfilingService; |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests) |
| 41 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 42 | std::string UniqueFileName() |
| 43 | { |
| 44 | std::time_t t = std::time(nullptr); |
| 45 | char mbstr[100]; |
| 46 | std::strftime(mbstr, sizeof(mbstr), "%Y_%m_%d_%H_%M_%S_", std::localtime(&t)); |
| 47 | std::stringstream ss; |
| 48 | ss << mbstr; |
| 49 | ss << t; |
| 50 | ss << ".bin"; |
| 51 | return ss.str(); |
| 52 | } |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(TestFileOnlyProfiling) |
| 55 | { |
| 56 | // Create a temporary file name. |
| 57 | boost::filesystem::path tempPath = boost::filesystem::temp_directory_path(); |
| 58 | boost::filesystem::path tempFile = UniqueFileName(); |
| 59 | tempPath = tempPath / tempFile; |
| 60 | armnn::Runtime::CreationOptions creationOptions; |
| 61 | creationOptions.m_ProfilingOptions.m_EnableProfiling = true; |
| 62 | creationOptions.m_ProfilingOptions.m_FileOnly = true; |
| 63 | creationOptions.m_ProfilingOptions.m_CapturePeriod = 100; |
| 64 | creationOptions.m_ProfilingOptions.m_TimelineEnabled = true; |
| 65 | ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>(); |
| 66 | creationOptions.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr); |
| 67 | |
| 68 | armnn::Runtime runtime(creationOptions); |
| 69 | |
| 70 | // Load a simple network |
| 71 | // build up the structure of the network |
| 72 | INetworkPtr net(INetwork::Create()); |
| 73 | |
| 74 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 75 | |
| 76 | NormalizationDescriptor descriptor; |
| 77 | IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor, "normalization"); |
| 78 | |
| 79 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 80 | |
| 81 | input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0)); |
| 82 | normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 83 | |
| 84 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 85 | normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 86 | |
| 87 | // optimize the network |
| 88 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 89 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime.GetDeviceSpec()); |
| 90 | |
| 91 | // Load it into the runtime. It should succeed. |
| 92 | armnn::NetworkId netId; |
| 93 | BOOST_TEST(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); |
| 94 | |
| 95 | static_cast<TestTimelinePacketHandler*>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000); |
| 96 | } |
| 97 | |
Jan Eilers | 158997a | 2020-01-30 13:50:36 +0000 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd, * boost::unit_test::disabled()) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 99 | { |
| 100 | // Create a temporary file name. |
| 101 | boost::filesystem::path tempPath = boost::filesystem::temp_directory_path(); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 102 | boost::filesystem::path tempFile = UniqueFileName(); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 103 | tempPath = tempPath / tempFile; |
| 104 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 105 | options.m_EnableProfiling = true; |
| 106 | options.m_FileOnly = true; |
| 107 | options.m_IncomingCaptureFile = ""; |
Rob Hughes | 270233f | 2019-11-13 11:53:48 +0000 | [diff] [blame] | 108 | options.m_OutgoingCaptureFile = tempPath.string(); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 109 | options.m_CapturePeriod = 100; |
| 110 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 111 | FileOnlyHelperService helper; |
| 112 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 113 | // Enable the profiling service |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 114 | armnn::profiling::ProfilingService profilingService; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 115 | profilingService.ResetExternalProfilingOptions(options, true); |
| 116 | // Bring the profiling service to the "WaitingForAck" state |
| 117 | profilingService.Update(); |
| 118 | profilingService.Update(); |
| 119 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 120 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 121 | BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 122 | |
| 123 | profilingService.Update(); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 124 | // First packet sent will be the SendStreamMetaDataPacket, it's possible though unlikely that it will be sent twice |
| 125 | // The second or possibly third packet will be the CounterDirectoryPacket which means the |
| 126 | // ConnectionAcknowledgedCommandHandler has set the state to active |
| 127 | uint32_t packetCount = 0; |
| 128 | while(profilingService.GetCurrentState() != ProfilingState::Active && packetCount < 3) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 129 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 130 | if(!helper.WaitForPacketsSent()) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 131 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 132 | BOOST_FAIL("Timeout waiting for packets"); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 133 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 134 | packetCount++; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 137 | BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 138 | // Minimum test here is to check that the file was created. |
| 139 | BOOST_CHECK(boost::filesystem::exists(tempPath.c_str()) == true); |
| 140 | |
| 141 | // Increment a counter. |
| 142 | BOOST_CHECK(profilingService.IsCounterRegistered(0) == true); |
| 143 | profilingService.IncrementCounterValue(0); |
| 144 | BOOST_CHECK(profilingService.GetCounterValue(0) > 0); |
| 145 | |
| 146 | // At this point the profiling service is active and we've activated all the counters. Waiting a collection |
| 147 | // period should be enough to have some data in the file. |
| 148 | |
| 149 | // Wait for 1 collection period plus a bit of overhead.. |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 150 | helper.WaitForPacketsSent(); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 151 | |
| 152 | // In order to flush the files we need to gracefully close the profiling service. |
| 153 | options.m_EnableProfiling = false; |
| 154 | profilingService.ResetExternalProfilingOptions(options, true); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 155 | |
| 156 | // The output file size should be greater than 0. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 157 | BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) > 0); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 158 | |
| 159 | // Delete the tmp file. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 160 | BOOST_CHECK(armnnUtils::Filesystem::Remove(tempPath.string().c_str())); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_SUITE_END() |