blob: 77d4d683b64a01833860baf423ee80cfa5837355 [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>
Keith Davis3201eea2019-10-24 17:30:41 +010011
12#include <boost/core/ignore_unused.hpp>
13#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 {
33 return ProfilingService::WaitForPacketSent(ProfilingService::Instance(), timeout);
34 }
35};
36
Keith Davis3201eea2019-10-24 17:30:41 +010037BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests)
38
Jan Eilers158997a2020-01-30 13:50:36 +000039BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd, * boost::unit_test::disabled())
Keith Davis3201eea2019-10-24 17:30:41 +010040{
41 // Create a temporary file name.
42 boost::filesystem::path tempPath = boost::filesystem::temp_directory_path();
43 boost::filesystem::path tempFile = boost::filesystem::unique_path();
44 tempPath = tempPath / tempFile;
45 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
46 options.m_EnableProfiling = true;
47 options.m_FileOnly = true;
48 options.m_IncomingCaptureFile = "";
Rob Hughes270233f2019-11-13 11:53:48 +000049 options.m_OutgoingCaptureFile = tempPath.string();
Keith Davis3201eea2019-10-24 17:30:41 +010050 options.m_CapturePeriod = 100;
51
Finn Williams09ad6f92019-12-19 17:05:18 +000052 FileOnlyHelperService helper;
53
Keith Davis3201eea2019-10-24 17:30:41 +010054 // Enable the profiling service
55 ProfilingService& profilingService = ProfilingService::Instance();
56 profilingService.ResetExternalProfilingOptions(options, true);
57 // Bring the profiling service to the "WaitingForAck" state
58 profilingService.Update();
59 profilingService.Update();
60
Keith Davis3201eea2019-10-24 17:30:41 +010061
Finn Williams09ad6f92019-12-19 17:05:18 +000062 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +010063
64 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +000065 // First packet sent will be the SendStreamMetaDataPacket, it's possible though unlikely that it will be sent twice
66 // The second or possibly third packet will be the CounterDirectoryPacket which means the
67 // ConnectionAcknowledgedCommandHandler has set the state to active
68 uint32_t packetCount = 0;
69 while(profilingService.GetCurrentState() != ProfilingState::Active && packetCount < 3)
Keith Davis3201eea2019-10-24 17:30:41 +010070 {
Finn Williams09ad6f92019-12-19 17:05:18 +000071 if(!helper.WaitForPacketsSent())
Keith Davis3201eea2019-10-24 17:30:41 +010072 {
Finn Williams09ad6f92019-12-19 17:05:18 +000073 BOOST_FAIL("Timeout waiting for packets");
Keith Davis3201eea2019-10-24 17:30:41 +010074 }
Finn Williams09ad6f92019-12-19 17:05:18 +000075 packetCount++;
Keith Davis3201eea2019-10-24 17:30:41 +010076 }
77
Finn Williams09ad6f92019-12-19 17:05:18 +000078 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +010079 // Minimum test here is to check that the file was created.
80 BOOST_CHECK(boost::filesystem::exists(tempPath.c_str()) == true);
81
82 // Increment a counter.
83 BOOST_CHECK(profilingService.IsCounterRegistered(0) == true);
84 profilingService.IncrementCounterValue(0);
85 BOOST_CHECK(profilingService.GetCounterValue(0) > 0);
86
87 // At this point the profiling service is active and we've activated all the counters. Waiting a collection
88 // period should be enough to have some data in the file.
89
90 // Wait for 1 collection period plus a bit of overhead..
Finn Williams09ad6f92019-12-19 17:05:18 +000091 helper.WaitForPacketsSent();
Keith Davis3201eea2019-10-24 17:30:41 +010092
93 // In order to flush the files we need to gracefully close the profiling service.
94 options.m_EnableProfiling = false;
95 profilingService.ResetExternalProfilingOptions(options, true);
Keith Davis3201eea2019-10-24 17:30:41 +010096
97 // The output file size should be greater than 0.
Rob Hughesbdee4262020-01-07 17:05:24 +000098 BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) > 0);
Keith Davis3201eea2019-10-24 17:30:41 +010099
100 // Delete the tmp file.
Rob Hughesbdee4262020-01-07 17:05:24 +0000101 BOOST_CHECK(armnnUtils::Filesystem::Remove(tempPath.string().c_str()));
Keith Davis3201eea2019-10-24 17:30:41 +0100102}
103
104BOOST_AUTO_TEST_SUITE_END()