MLECO-3096: Removing "timer" from HAL profile.

Attempting to have timer functionality contained within
the platform drivers "package" as it should (in line with
the refactoring work done so far under HAL). This will
ensure that we don't need two timer implementations under
HAL "profiles" and therefore, this whole directory can be
removed.

This change also addressed issue with the applicatio level
Profiler code knowing about how the PMU has been set up by
the platform code. This link has been removed completely.
This will make it much easier to add/amend the Ethos-U PMU
event counters types and give each platform the capability
of populating their relvant counters. The application level
Profiler doesn't know which metrics it is displaying but
just calculates and maintains statistics for whatever PMU
counters it receives from the HAL level.

A fix for timing adapter issue introduced in the last CR
is also included.

Change-Id: Ia46e03a06e7b8e42b9ed2ba8f2af2dcd2229c110
Signed-off-by: Kshitij Sisodia <kshitij.sisodia@arm.com>
diff --git a/source/profiler/Profiler.cc b/source/profiler/Profiler.cc
index efbc64d..7e10097 100644
--- a/source/profiler/Profiler.cc
+++ b/source/profiler/Profiler.cc
@@ -40,7 +40,7 @@
         }
         if (this->m_pPlatform && !this->m_started) {
             this->m_pPlatform->timer->reset();
-            this->m_tstampSt = this->m_pPlatform->timer->start_profiling();
+            this->m_tstampSt = this->m_pPlatform->timer->get_counters();
             this->m_started = true;
             return true;
         }
@@ -51,7 +51,7 @@
     bool Profiler::StopProfiling()
     {
         if (this->m_pPlatform && this->m_started) {
-            this->m_tstampEnd = this->m_pPlatform->timer->stop_profiling();
+            this->m_tstampEnd = this->m_pPlatform->timer->get_counters();
             this->m_started = false;
 
             this->AddProfilingUnit(this->m_tstampSt, this->m_tstampEnd, this->m_name);
@@ -99,111 +99,28 @@
             result.name = item.first;
             result.samplesNum = series.size();
 
-            Statistics AXI0_RD {
-                .name = "NPU AXI0_RD_DATA_BEAT_RECEIVED",
-                .unit = "beats",
-                .total = 0,
-                .avrg = 0.0,
-                .min = series[0].axi0writes,
-                .max = 0
-            };
-            Statistics AXI0_WR {
-                    .name = "NPU AXI0_WR_DATA_BEAT_WRITTEN",
-                    .unit = "beats",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = series[0].axi0reads,
-                    .max = 0
-            };
-            Statistics AXI1_RD {
-                    .name = "NPU AXI1_RD_DATA_BEAT_RECEIVED",
-                    .unit = "beats",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = series[0].axi1reads,
-                    .max = 0
-            };
-            Statistics NPU_ACTIVE {
-                    .name = "NPU ACTIVE",
-                    .unit = "cycles",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = series[0].activeNpuCycles,
-                    .max = 0
-            };
-            Statistics NPU_IDLE {
-                    .name = "NPU IDLE",
-                    .unit = "cycles",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = series[0].idleNpuCycles,
-                    .max = 0
-            };
-            Statistics NPU_Total {
-                    .name = "NPU TOTAL",
-                    .unit = "cycles",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = series[0].npuCycles,
-                    .max = 0,
-            };
-#if defined(CPU_PROFILE_ENABLED)
-            Statistics CPU_ACTIVE {
-                    .name = "CPU ACTIVE",
-                    .unit = "cycles (approx)",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = series[0].cpuCycles - NPU_ACTIVE.min,
-                    .max = 0
-            };
-            Statistics TIME {
-                    .name = "Time",
-                    .unit = "ms",
-                    .total = 0,
-                    .avrg = 0.0,
-                    .min = static_cast<uint64_t>(series[0].time),
-                    .max = 0
-            };
-#endif
-            for(ProfilingUnit& unit: series){
-
-                calcProfilingStat(unit.npuCycles,
-                                  NPU_Total, result.samplesNum);
-
-                calcProfilingStat(unit.activeNpuCycles,
-                                  NPU_ACTIVE, result.samplesNum);
-
-                calcProfilingStat(unit.idleNpuCycles,
-                                  NPU_IDLE, result.samplesNum);
-
-                calcProfilingStat(unit.axi0writes,
-                                  AXI0_WR, result.samplesNum);
-
-                calcProfilingStat(unit.axi0reads,
-                                  AXI0_RD, result.samplesNum);
-
-                calcProfilingStat(unit.axi1reads,
-                                  AXI1_RD, result.samplesNum);
-#if defined(CPU_PROFILE_ENABLED)
-                calcProfilingStat(static_cast<uint64_t>(unit.time),
-                                  TIME, result.samplesNum);
-
-                calcProfilingStat(unit.cpuCycles - unit.activeNpuCycles,
-                                  CPU_ACTIVE, result.samplesNum);
-#endif
+            std::vector<Statistics> stats(series[0].counters.num_counters);
+            for (size_t i = 0; i < stats.size(); ++i) {
+                stats[i].name = series[0].counters.counters[i].name;
+                stats[i].unit = series[0].counters.counters[i].unit;
             }
-            result.data.emplace_back(AXI0_RD);
-            result.data.emplace_back(AXI0_WR);
-            result.data.emplace_back(AXI1_RD);
-            result.data.emplace_back(NPU_ACTIVE);
-            result.data.emplace_back(NPU_IDLE);
-            result.data.emplace_back(NPU_Total);
-#if defined(CPU_PROFILE_ENABLED)
-            result.data.emplace_back(CPU_ACTIVE);
-            result.data.emplace_back(TIME);
-#endif
-        results.emplace_back(result);
+
+            for(ProfilingUnit& unit: series) {
+                for (size_t i = 0; i < stats.size(); ++i) {
+                    calcProfilingStat(
+                        unit.counters.counters[i].value,
+                        stats[i],
+                        result.samplesNum);
+                }
+            }
+
+            for (Statistics& stat : stats) {
+                result.data.emplace_back(stat);
+            }
+
+            results.emplace_back(result);
         }
+
         this->Reset();
     }
 
@@ -216,10 +133,11 @@
         std::vector<ProfileResult> results{};
         GetAllResultsAndReset(results);
         for(ProfileResult& result: results) {
-            info("Profile for %s:\n", result.name.c_str());
-
-            if (printFullStat) {
-                printStatisticsHeader(result.samplesNum);
+            if (result.data.size()) {
+                info("Profile for %s:\n", result.name.c_str());
+                if (printFullStat) {
+                    printStatisticsHeader(result.samplesNum);
+                }
             }
 
             for (Statistics &stat: result.data) {
@@ -228,7 +146,7 @@
                          stat.name.c_str(), stat.unit.c_str(),
                          stat.total, stat.avrg, stat.min, stat.max);
                 } else {
-                    info("%s %s: %.0f\n", stat.name.c_str(), stat.unit.c_str(), stat.avrg);
+                    info("%s: %.0f %s\n", stat.name.c_str(), stat.avrg, stat.unit.c_str());
                 }
             }
         }
@@ -239,7 +157,7 @@
         this->m_name = std::string(str);
     }
 
-    void Profiler::AddProfilingUnit(time_counter start, time_counter end,
+    void Profiler::AddProfilingUnit(pmu_counters start, pmu_counters end,
                                     const std::string& name)
     {
         if (!this->m_pPlatform) {
@@ -247,33 +165,25 @@
             return;
         }
 
-        platform_timer * timer = this->m_pPlatform->timer;
+        struct ProfilingUnit unit = {
+            .counters = end
+        };
 
-        struct ProfilingUnit unit;
+        if (end.num_counters != start.num_counters ||
+                true != end.initialised || true != start.initialised) {
+            printf_err("Invalid start or end counters\n");
+            return;
+        }
 
-        if (timer->cap.npu_cycles && timer->get_npu_cycles_diff)
-        {
-            const size_t size = 6;
-            uint64_t pmuCounters[size] = {0};
-            /* 6 values: total cc, active cc, idle cc, axi0 read, axi0 write, axi1 read*/
-            if (0 == timer->get_npu_cycles_diff(&start, &end, pmuCounters, size)) {
-                unit.npuCycles = pmuCounters[0];
-                unit.activeNpuCycles = pmuCounters[1];
-                unit.idleNpuCycles = pmuCounters[2];
-                unit.axi0reads = pmuCounters[3];
-                unit.axi0writes = pmuCounters[4];
-                unit.axi1reads = pmuCounters[5];
+        for (size_t i = 0; i < unit.counters.num_counters; ++i) {
+            if (unit.counters.counters[i].value < start.counters[i].value) {
+                warn("Overflow detected for %s\n", unit.counters.counters[i].name);
+                unit.counters.counters[i].value = 0;
+            } else {
+                unit.counters.counters[i].value -= start.counters[i].value;
             }
         }
 
-        if (timer->cap.cpu_cycles && timer->get_cpu_cycle_diff) {
-            unit.cpuCycles = timer->get_cpu_cycle_diff(&start, &end);
-        }
-
-        if (timer->cap.duration_ms && timer->get_duration_ms) {
-            unit.time = timer->get_duration_ms(&start, &end);
-        }
-
         this->m_series[name].emplace_back(unit);
     }