Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / pios / common / pios_instrumentation.c
blob381e42bfb0d296d46dbac374de74db2ca5db66b1
1 /**
2 ******************************************************************************
4 * @file pios_instrumentation.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
6 * @brief PiOS instrumentation infrastructure
7 * Allow to collects performance indexes and execution times
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <pios_instrumentation.h>
29 pios_perf_counter_t *pios_instrumentation_perf_counters = NULL;
30 int8_t pios_instrumentation_max_counters = -1;
31 int8_t pios_instrumentation_last_used_counter = -1;
33 void PIOS_Instrumentation_Init(int8_t maxCounters)
35 PIOS_Assert(maxCounters >= 0);
36 if (maxCounters > 0) {
37 pios_instrumentation_perf_counters = (pios_perf_counter_t *)pvPortMalloc(sizeof(pios_perf_counter_t) * maxCounters);
38 PIOS_Assert(pios_instrumentation_perf_counters);
39 memset(pios_instrumentation_perf_counters, 0, sizeof(pios_perf_counter_t) * maxCounters);
40 pios_instrumentation_max_counters = maxCounters;
41 } else {
42 pios_instrumentation_perf_counters = NULL;
43 pios_instrumentation_max_counters = -1;
47 pios_counter_t PIOS_Instrumentation_CreateCounter(uint32_t id)
49 PIOS_Assert(pios_instrumentation_perf_counters && (pios_instrumentation_max_counters > pios_instrumentation_last_used_counter));
51 pios_counter_t counter_handle = PIOS_Instrumentation_SearchCounter(id);
52 if (!counter_handle) {
53 pios_perf_counter_t *newcounter = &pios_instrumentation_perf_counters[++pios_instrumentation_last_used_counter];
54 newcounter->id = id;
55 newcounter->max = INT32_MIN + 1;
56 newcounter->min = INT32_MAX - 1;
57 counter_handle = (pios_counter_t)newcounter;
59 return counter_handle;
62 pios_counter_t PIOS_Instrumentation_SearchCounter(uint32_t id)
64 PIOS_Assert(pios_instrumentation_perf_counters);
65 uint8_t i = 0;
66 while (i < pios_instrumentation_last_used_counter && pios_instrumentation_perf_counters[i].id != id) {
67 i++;
69 if (pios_instrumentation_perf_counters[i].id != id) {
70 return NULL;
72 return (pios_counter_t)&pios_instrumentation_perf_counters[i];
75 void PIOS_Instrumentation_ForEachCounter(InstrumentationCounterCallback callback, void *context)
77 PIOS_Assert(pios_instrumentation_perf_counters);
78 for (int8_t index = 0; index < pios_instrumentation_last_used_counter + 1; index++) {
79 const pios_perf_counter_t *counter = &pios_instrumentation_perf_counters[index];
80 callback(counter, index, context);