blob: 7f927bdeee9a72074625f2bde9422c2f5cb6843b [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"
7#include <Runtime.hpp>
8
9#include <boost/core/ignore_unused.hpp>
10#include <boost/filesystem.hpp>
11#include <boost/numeric/conversion/cast.hpp>
12#include <boost/test/unit_test.hpp>
13
14#include <ProfilingService.hpp>
15#include <cstdio>
16#include <fstream>
17#include <sstream>
18#include <sys/stat.h>
19
20#if defined(__ANDROID__)
21#define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/data/local/tmp"
22#else
23#define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/tmp"
24#endif
25
26using namespace armnn::profiling;
27using namespace armnn;
28
29using namespace std::chrono_literals;
30
31BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests)
32
33BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd)
34{
35 // Create a temporary file name.
36 boost::filesystem::path tempPath = boost::filesystem::temp_directory_path();
37 boost::filesystem::path tempFile = boost::filesystem::unique_path();
38 tempPath = tempPath / tempFile;
39 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
40 options.m_EnableProfiling = true;
41 options.m_FileOnly = true;
42 options.m_IncomingCaptureFile = "";
43 options.m_OutgoingCaptureFile = tempPath.c_str();
44 options.m_CapturePeriod = 100;
45
46 // Enable the profiling service
47 ProfilingService& profilingService = ProfilingService::Instance();
48 profilingService.ResetExternalProfilingOptions(options, true);
49 // Bring the profiling service to the "WaitingForAck" state
50 profilingService.Update();
51 profilingService.Update();
52
53 u_int32_t timeout = 2000;
54 u_int32_t sleepTime = 50;
55 u_int32_t timeSlept = 0;
56
57 // Give the profiling service sending thread time start executing and send the stream metadata.
58 while (profilingService.GetCurrentState() != ProfilingState::WaitingForAck)
59 {
60 if (timeSlept >= timeout)
61 {
62 BOOST_FAIL("Timeout: Profiling service did not switch to WaitingForAck state");
63 }
64 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
65 timeSlept += sleepTime;
66 }
67
68 profilingService.Update();
69
70 timeSlept = 0;
71
72 while (profilingService.GetCurrentState() != profiling::ProfilingState::Active)
73 {
74 if (timeSlept >= timeout)
75 {
76 BOOST_FAIL("Timeout: Profiling service did not switch to Active state");
77 }
78 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
79 timeSlept += sleepTime;
80 }
81
82 // Minimum test here is to check that the file was created.
83 BOOST_CHECK(boost::filesystem::exists(tempPath.c_str()) == true);
84
85 // Increment a counter.
86 BOOST_CHECK(profilingService.IsCounterRegistered(0) == true);
87 profilingService.IncrementCounterValue(0);
88 BOOST_CHECK(profilingService.GetCounterValue(0) > 0);
89
90 // At this point the profiling service is active and we've activated all the counters. Waiting a collection
91 // period should be enough to have some data in the file.
92
93 // Wait for 1 collection period plus a bit of overhead..
94 std::this_thread::sleep_for(std::chrono::milliseconds(150));
95
96 // In order to flush the files we need to gracefully close the profiling service.
97 options.m_EnableProfiling = false;
98 profilingService.ResetExternalProfilingOptions(options, true);
99 // Wait a short time to allow the threads to clean themselves up.
100 std::this_thread::sleep_for(std::chrono::milliseconds(100));
101
102 // The output file size should be greater than 0.
103 struct stat statusBuffer;
104 BOOST_CHECK(stat(tempPath.c_str(), &statusBuffer) == 0);
105 BOOST_CHECK(statusBuffer.st_size > 0);
106
107 // Delete the tmp file.
108 BOOST_CHECK(remove(tempPath.c_str()) == 0);
109}
110
111BOOST_AUTO_TEST_SUITE_END()