blob: baadb8555d5b608d2e035897faeeb3601d5ba9b4 [file] [log] [blame]
Keith Davis3201eea2019-10-24 17:30:41 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "../FileOnlyProfilingConnection.hpp"
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +00007
8#include <ProfilingService.hpp>
Keith Davis3201eea2019-10-24 17:30:41 +01009#include <Runtime.hpp>
Rob Hughesbdee4262020-01-07 17:05:24 +000010#include <Filesystem.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000011#include <armnn/utility/IgnoreUnused.hpp>
Keith Davis3201eea2019-10-24 17:30:41 +010012
Keith Davis3201eea2019-10-24 17:30:41 +010013#include <boost/filesystem.hpp>
14#include <boost/numeric/conversion/cast.hpp>
15#include <boost/test/unit_test.hpp>
16
Keith Davis3201eea2019-10-24 17:30:41 +010017#include <cstdio>
18#include <fstream>
19#include <sstream>
20#include <sys/stat.h>
21
Keith Davis3201eea2019-10-24 17:30:41 +010022using namespace armnn::profiling;
23using namespace armnn;
24
25using namespace std::chrono_literals;
26
Finn Williams09ad6f92019-12-19 17:05:18 +000027class FileOnlyHelperService : public ProfilingService
28{
29 public:
30 // Wait for a notification from the send thread
31 bool WaitForPacketsSent(uint32_t timeout = 1000)
32 {
Sadik Armagan3184c902020-03-18 10:57:30 +000033 return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout);
Finn Williams09ad6f92019-12-19 17:05:18 +000034 }
Sadik Armagan3184c902020-03-18 10:57:30 +000035 armnn::profiling::ProfilingService m_ProfilingService;
Finn Williams09ad6f92019-12-19 17:05:18 +000036};
37
Keith Davis3201eea2019-10-24 17:30:41 +010038BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests)
39
Jan Eilers158997a2020-01-30 13:50:36 +000040BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd, * boost::unit_test::disabled())
Keith Davis3201eea2019-10-24 17:30:41 +010041{
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 Hughes270233f2019-11-13 11:53:48 +000050 options.m_OutgoingCaptureFile = tempPath.string();
Keith Davis3201eea2019-10-24 17:30:41 +010051 options.m_CapturePeriod = 100;
52
Finn Williams09ad6f92019-12-19 17:05:18 +000053 FileOnlyHelperService helper;
54
Keith Davis3201eea2019-10-24 17:30:41 +010055 // Enable the profiling service
Sadik Armagan3184c902020-03-18 10:57:30 +000056 armnn::profiling::ProfilingService profilingService;
Keith Davis3201eea2019-10-24 17:30:41 +010057 profilingService.ResetExternalProfilingOptions(options, true);
58 // Bring the profiling service to the "WaitingForAck" state
59 profilingService.Update();
60 profilingService.Update();
61
Keith Davis3201eea2019-10-24 17:30:41 +010062
Finn Williams09ad6f92019-12-19 17:05:18 +000063 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +010064
65 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +000066 // 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 Davis3201eea2019-10-24 17:30:41 +010071 {
Finn Williams09ad6f92019-12-19 17:05:18 +000072 if(!helper.WaitForPacketsSent())
Keith Davis3201eea2019-10-24 17:30:41 +010073 {
Finn Williams09ad6f92019-12-19 17:05:18 +000074 BOOST_FAIL("Timeout waiting for packets");
Keith Davis3201eea2019-10-24 17:30:41 +010075 }
Finn Williams09ad6f92019-12-19 17:05:18 +000076 packetCount++;
Keith Davis3201eea2019-10-24 17:30:41 +010077 }
78
Finn Williams09ad6f92019-12-19 17:05:18 +000079 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +010080 // 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 Williams09ad6f92019-12-19 17:05:18 +000092 helper.WaitForPacketsSent();
Keith Davis3201eea2019-10-24 17:30:41 +010093
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 Davis3201eea2019-10-24 17:30:41 +010097
98 // The output file size should be greater than 0.
Rob Hughesbdee4262020-01-07 17:05:24 +000099 BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) > 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100100
101 // Delete the tmp file.
Rob Hughesbdee4262020-01-07 17:05:24 +0000102 BOOST_CHECK(armnnUtils::Filesystem::Remove(tempPath.string().c_str()));
Keith Davis3201eea2019-10-24 17:30:41 +0100103}
104
105BOOST_AUTO_TEST_SUITE_END()