COMPMID-798 Add instrumentation to NEON kernels

Change-Id: I9dbb090cac731d68bd98a7d1c8ab0e1cb0a5c911
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/116746
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
Reviewed-by: Pablo Tello <pablo.tello@arm.com>
Tested-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/tests/SConscript b/tests/SConscript
index 4261331..a128d39 100644
--- a/tests/SConscript
+++ b/tests/SConscript
@@ -29,9 +29,10 @@
 
 # vars is imported from arm_compute:
 variables = [
+    #FIXME: Remove before release!
+    BoolVariable("benchmark_examples", "Build benchmark examples programs", True),
     BoolVariable("validation_tests", "Build validation test programs", True),
     BoolVariable("benchmark_tests", "Build benchmark test programs", True),
-    BoolVariable("benchmark_examples", "Build benchmark examples programs", True),
     ("test_filter", "Pattern to specify the tests' filenames to be compiled", "*.cpp")
 ]
 
@@ -164,6 +165,7 @@
     Default(arm_compute_validation)
     Export('arm_compute_validation')
 
+#FIXME: Remove before release!
 if test_env['benchmark_examples']:
     files_benchmark_examples = test_env.Object('benchmark_examples/RunExample.cpp')
     arm_compute_benchmark_examples = []
diff --git a/tests/framework/Framework.cpp b/tests/framework/Framework.cpp
index 79a77d9..3091b66 100644
--- a/tests/framework/Framework.cpp
+++ b/tests/framework/Framework.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -41,6 +41,9 @@
     _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::NONE), Instrument::make_instrument<WallClockTimer, ScaleFactor::NONE>);
     _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::TIME_MS), Instrument::make_instrument<WallClockTimer, ScaleFactor::TIME_MS>);
     _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::TIME_S), Instrument::make_instrument<WallClockTimer, ScaleFactor::TIME_S>);
+    _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::SCHEDULER_TIMER, ScaleFactor::NONE), Instrument::make_instrument<SchedulerTimer, ScaleFactor::NONE>);
+    _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::SCHEDULER_TIMER, ScaleFactor::TIME_MS), Instrument::make_instrument<SchedulerTimer, ScaleFactor::TIME_MS>);
+    _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::SCHEDULER_TIMER, ScaleFactor::TIME_S), Instrument::make_instrument<SchedulerTimer, ScaleFactor::TIME_S>);
 #ifdef PMU_ENABLED
     _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::NONE), Instrument::make_instrument<PMUCounter, ScaleFactor::NONE>);
     _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::SCALE_1K), Instrument::make_instrument<PMUCounter, ScaleFactor::SCALE_1K>);
diff --git a/tests/framework/instruments/Instruments.cpp b/tests/framework/instruments/Instruments.cpp
index 641e630..64e87f9 100644
--- a/tests/framework/instruments/Instruments.cpp
+++ b/tests/framework/instruments/Instruments.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -44,6 +44,9 @@
         { "wall_clock_timer", std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::NONE) },
         { "wall_clock_timer_ms", std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::TIME_MS) },
         { "wall_clock_timer_s", std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::TIME_S) },
+        { "scheduler_timer", std::pair<InstrumentType, ScaleFactor>(InstrumentType::SCHEDULER_TIMER, ScaleFactor::NONE) },
+        { "scheduler_timer_ms", std::pair<InstrumentType, ScaleFactor>(InstrumentType::SCHEDULER_TIMER, ScaleFactor::TIME_MS) },
+        { "scheduler_timer_s", std::pair<InstrumentType, ScaleFactor>(InstrumentType::SCHEDULER_TIMER, ScaleFactor::TIME_S) },
         { "pmu", std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::NONE) },
         { "pmu_k", std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::SCALE_1K) },
         { "pmu_m", std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::SCALE_1M) },
diff --git a/tests/framework/instruments/Instruments.h b/tests/framework/instruments/Instruments.h
index 651f0f5..fe4c719 100644
--- a/tests/framework/instruments/Instruments.h
+++ b/tests/framework/instruments/Instruments.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -27,6 +27,7 @@
 #include "MaliCounter.h"
 #include "OpenCLTimer.h"
 #include "PMUCounter.h"
+#include "SchedulerTimer.h"
 #include "WallClockTimer.h"
 
 #include <sstream>
@@ -48,6 +49,7 @@
     PMU_INSTRUCTION_COUNTER = 0x0202,
     MALI                    = 0x0300,
     OPENCL_TIMER            = 0x0400,
+    SCHEDULER_TIMER         = 0x0500,
 };
 
 using InstrumentsDescription = std::pair<InstrumentType, ScaleFactor>;
@@ -82,6 +84,22 @@
                     throw std::invalid_argument("Unsupported instrument scale");
             }
             break;
+        case InstrumentType::SCHEDULER_TIMER:
+            switch(instrument.second)
+            {
+                case ScaleFactor::NONE:
+                    stream << "SCHEDULER_TIMER";
+                    break;
+                case ScaleFactor::TIME_MS:
+                    stream << "SCHEDULER_TIMER_MS";
+                    break;
+                case ScaleFactor::TIME_S:
+                    stream << "SCHEDULER_TIMER_S";
+                    break;
+                default:
+                    throw std::invalid_argument("Unsupported instrument scale");
+            }
+            break;
         case InstrumentType::PMU:
             switch(instrument.second)
             {
diff --git a/tests/framework/instruments/SchedulerTimer.cpp b/tests/framework/instruments/SchedulerTimer.cpp
new file mode 100644
index 0000000..e42cebd
--- /dev/null
+++ b/tests/framework/instruments/SchedulerTimer.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2017-2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "SchedulerTimer.h"
+
+#include "WallClockTimer.h"
+#include "arm_compute/core/CPP/ICPPKernel.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace framework
+{
+std::string SchedulerTimer::id() const
+{
+    return "SchedulerTimer";
+}
+
+class Interceptor final : public IScheduler
+{
+public:
+    /** Default constructor. */
+    Interceptor(std::list<SchedulerTimer::kernel_info> &kernels, IScheduler &real_scheduler, ScaleFactor scale_factor)
+        : _kernels(kernels), _real_scheduler(real_scheduler), _timer(scale_factor)
+    {
+    }
+
+    void set_num_threads(unsigned int num_threads) override
+    {
+        _real_scheduler.set_num_threads(num_threads);
+    }
+
+    unsigned int num_threads() const override
+    {
+        return _real_scheduler.num_threads();
+    }
+
+    void schedule(ICPPKernel *kernel, unsigned int split_dimension) override
+    {
+        _timer.start();
+        _real_scheduler.schedule(kernel, split_dimension);
+        _timer.stop();
+
+        SchedulerTimer::kernel_info info;
+        info.name         = kernel->name();
+        info.measurements = _timer.measurements();
+        _kernels.push_back(std::move(info));
+    }
+
+private:
+    std::list<SchedulerTimer::kernel_info> &_kernels;
+    IScheduler                             &_real_scheduler;
+    WallClockTimer                          _timer;
+};
+
+SchedulerTimer::SchedulerTimer(ScaleFactor scale_factor)
+    : _kernels(), _real_scheduler(nullptr), _real_scheduler_type(), _scale_factor(scale_factor)
+{
+}
+
+void SchedulerTimer::start()
+{
+    ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
+    _real_scheduler_type = Scheduler::get_type();
+    //Note: We can't currently replace a custom scheduler
+    if(_real_scheduler_type != Scheduler::Type::CUSTOM)
+    {
+        _real_scheduler  = &Scheduler::get();
+        auto interceptor = std::make_shared<Interceptor>(_kernels, *_real_scheduler, _scale_factor);
+        Scheduler::set(std::static_pointer_cast<IScheduler>(interceptor));
+    }
+    _kernels.clear();
+}
+
+void SchedulerTimer::stop()
+{
+    // Restore real scheduler
+    Scheduler::set(_real_scheduler_type);
+    _real_scheduler = nullptr;
+}
+
+Instrument::MeasurementsMap SchedulerTimer::measurements() const
+{
+    MeasurementsMap measurements;
+    unsigned int    kernel_number = 0;
+    for(auto kernel : _kernels)
+    {
+        measurements.emplace(kernel.name + " #" + support::cpp11::to_string(kernel_number++), kernel.measurements.begin()->second);
+    }
+
+    return measurements;
+}
+} // namespace framework
+} // namespace test
+} // namespace arm_compute
diff --git a/tests/framework/instruments/SchedulerTimer.h b/tests/framework/instruments/SchedulerTimer.h
new file mode 100644
index 0000000..446506a
--- /dev/null
+++ b/tests/framework/instruments/SchedulerTimer.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2017-2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef ARM_COMPUTE_TEST_SCHEDULER_TIMER
+#define ARM_COMPUTE_TEST_SCHEDULER_TIMER
+
+#include "Instrument.h"
+#include "arm_compute/runtime/Scheduler.h"
+#include <list>
+
+namespace arm_compute
+{
+namespace test
+{
+namespace framework
+{
+/** Instrument creating measurements based on the information returned by clGetEventProfilingInfo for each OpenCL kernel executed*/
+class SchedulerTimer : public Instrument
+{
+public:
+    SchedulerTimer(const SchedulerTimer &) = delete;
+    SchedulerTimer &operator=(const SchedulerTimer &) = delete;
+    SchedulerTimer(ScaleFactor scale_factor);
+    std::string                 id() const override;
+    void                        start() override;
+    void                        stop() override;
+    Instrument::MeasurementsMap measurements() const override;
+    struct kernel_info
+    {
+        Instrument::MeasurementsMap measurements{}; /**< Time it took the kernel to run */
+        std::string                 name{};         /**< Kernel name */
+    };
+
+private:
+    std::list<kernel_info> _kernels;
+    IScheduler            *_real_scheduler;
+    Scheduler::Type        _real_scheduler_type;
+    ScaleFactor            _scale_factor;
+};
+} // namespace framework
+} // namespace test
+} // namespace arm_compute
+#endif /* ARM_COMPUTE_TEST_SCHEDULER_TIMER */