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 | |
| 6 | #include "../FileOnlyProfilingConnection.hpp" |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 7 | |
| 8 | #include <ProfilingService.hpp> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 9 | #include <Runtime.hpp> |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 10 | #include <Filesystem.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 11 | #include <armnn/utility/IgnoreUnused.hpp> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 12 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 13 | #include <boost/filesystem.hpp> |
| 14 | #include <boost/numeric/conversion/cast.hpp> |
| 15 | #include <boost/test/unit_test.hpp> |
| 16 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 17 | #include <cstdio> |
| 18 | #include <fstream> |
| 19 | #include <sstream> |
| 20 | #include <sys/stat.h> |
| 21 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 22 | using namespace armnn::profiling; |
| 23 | using namespace armnn; |
| 24 | |
| 25 | using namespace std::chrono_literals; |
| 26 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 27 | class FileOnlyHelperService : public ProfilingService |
| 28 | { |
| 29 | public: |
| 30 | // Wait for a notification from the send thread |
| 31 | bool WaitForPacketsSent(uint32_t timeout = 1000) |
| 32 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 33 | return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 34 | } |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 35 | armnn::profiling::ProfilingService m_ProfilingService; |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests) |
| 39 | |
Jan Eilers | 158997a | 2020-01-30 13:50:36 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd, * boost::unit_test::disabled()) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 41 | { |
| 42 | // Create a temporary file name. |
| 43 | boost::filesystem::path tempPath = boost::filesystem::temp_directory_path(); |
| 44 | boost::filesystem::path tempFile = boost::filesystem::unique_path(); |
| 45 | tempPath = tempPath / tempFile; |
| 46 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 47 | options.m_EnableProfiling = true; |
| 48 | options.m_FileOnly = true; |
| 49 | options.m_IncomingCaptureFile = ""; |
Rob Hughes | 270233f | 2019-11-13 11:53:48 +0000 | [diff] [blame] | 50 | options.m_OutgoingCaptureFile = tempPath.string(); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 51 | options.m_CapturePeriod = 100; |
| 52 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 53 | FileOnlyHelperService helper; |
| 54 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 55 | // Enable the profiling service |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 56 | armnn::profiling::ProfilingService profilingService; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 57 | profilingService.ResetExternalProfilingOptions(options, true); |
| 58 | // Bring the profiling service to the "WaitingForAck" state |
| 59 | profilingService.Update(); |
| 60 | profilingService.Update(); |
| 61 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 62 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 63 | BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 64 | |
| 65 | profilingService.Update(); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 66 | // First packet sent will be the SendStreamMetaDataPacket, it's possible though unlikely that it will be sent twice |
| 67 | // The second or possibly third packet will be the CounterDirectoryPacket which means the |
| 68 | // ConnectionAcknowledgedCommandHandler has set the state to active |
| 69 | uint32_t packetCount = 0; |
| 70 | while(profilingService.GetCurrentState() != ProfilingState::Active && packetCount < 3) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 71 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 72 | if(!helper.WaitForPacketsSent()) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 73 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 74 | BOOST_FAIL("Timeout waiting for packets"); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 75 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 76 | packetCount++; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 79 | BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 80 | // Minimum test here is to check that the file was created. |
| 81 | BOOST_CHECK(boost::filesystem::exists(tempPath.c_str()) == true); |
| 82 | |
| 83 | // Increment a counter. |
| 84 | BOOST_CHECK(profilingService.IsCounterRegistered(0) == true); |
| 85 | profilingService.IncrementCounterValue(0); |
| 86 | BOOST_CHECK(profilingService.GetCounterValue(0) > 0); |
| 87 | |
| 88 | // At this point the profiling service is active and we've activated all the counters. Waiting a collection |
| 89 | // period should be enough to have some data in the file. |
| 90 | |
| 91 | // Wait for 1 collection period plus a bit of overhead.. |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 92 | helper.WaitForPacketsSent(); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 93 | |
| 94 | // In order to flush the files we need to gracefully close the profiling service. |
| 95 | options.m_EnableProfiling = false; |
| 96 | profilingService.ResetExternalProfilingOptions(options, true); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 97 | |
| 98 | // The output file size should be greater than 0. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 99 | BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) > 0); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 100 | |
| 101 | // Delete the tmp file. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 102 | BOOST_CHECK(armnnUtils::Filesystem::Remove(tempPath.string().c_str())); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_SUITE_END() |