dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
taskmonitor.c
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include "openpilot.h"
28 #include "taskmonitor.h"
29 #include "pios_mutex.h"
30 
31 // Private constants
32 
33 // Private types
34 
35 // Private variables
36 static struct pios_mutex *lock;
37 static struct pios_thread *handles[TASKINFO_RUNNING_NUMELEM];
38 static uint32_t lastMonitorTime;
39 
40 DONT_BUILD_IF(TASKINFO_RUNNING_NUMELEM != TASKINFO_STACKREMAINING_NUMELEM,
41  taskelems1);
42 DONT_BUILD_IF(TASKINFO_RUNNING_NUMELEM != TASKINFO_RUNNINGTIME_NUMELEM,
43  taskelems2);
44 
45 // Private functions
46 
50 int32_t TaskMonitorInitialize(void)
51 {
52  lock = PIOS_Mutex_Create();
53  PIOS_Assert(lock != NULL);
54  memset(handles, 0, sizeof(struct pios_thread *) * TASKINFO_RUNNING_NUMELEM);
55  lastMonitorTime = 0;
56 #if defined(DIAG_TASKS)
57 #if defined(PIOS_INCLUDE_CHIBIOS)
59 #endif /* defined(PIOS_INCLUDE_CHIBIOS) */
60 #endif
61  return 0;
62 }
63 
67 int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
68 {
69  uint32_t task_idx = (uint32_t) task;
70 
71  PIOS_Assert(threadp);
72 
73  if (task_idx < TASKINFO_RUNNING_NUMELEM) {
75  handles[task_idx] = threadp;
76  PIOS_Mutex_Unlock(lock);
77  return 0;
78  } else {
79  return -1;
80  }
81 }
82 
86 int32_t TaskMonitorRemove(TaskInfoRunningElem task)
87 {
88  uint32_t task_idx = (uint32_t) task;
89  if (task_idx < TASKINFO_RUNNING_NUMELEM)
90  {
92  handles[task_idx] = 0;
93  PIOS_Mutex_Unlock(lock);
94  return 0;
95  }
96  else
97  {
98  return -1;
99  }
100 }
101 
105 bool TaskMonitorQueryRunning(TaskInfoRunningElem task)
106 {
107  uint32_t task_idx = (uint32_t) task;
108  if (task_idx < TASKINFO_RUNNING_NUMELEM && handles[task_idx] != 0)
109  return true;
110  return false;
111 }
112 
117 {
118 #if defined(DIAG_TASKS)
119  TaskInfoData data;
120  int n;
121 
122  // Lock
124 
125  uint32_t currentTime = 0;
126  uint32_t deltaTime;
127 
128  /*
129  * Calculate the amount of elapsed run time between the last time we
130  * measured and now. Scale so that we can convert task run times
131  * directly to percentages.
132  */
133 #if defined(PIOS_INCLUDE_CHIBIOS)
134  currentTime = hal_lld_get_counter_value();
135 #endif /* defined(PIOS_INCLUDE_CHIBIOS) */
136  deltaTime = ((currentTime - lastMonitorTime) / 100) ? : 1; /* avoid divide-by-zero if the interval is too small */
137  lastMonitorTime = currentTime;
138 
139  // Update all task information
140  for (n = 0; n < TASKINFO_RUNNING_NUMELEM; ++n)
141  {
142  if (handles[n] != 0)
143  {
144  data.Running[n] = TASKINFO_RUNNING_TRUE;
145  data.StackRemaining[n] = PIOS_Thread_Get_Stack_Usage(handles[n]);
146  /* Generate run time stats */
147  data.RunningTime[n] = PIOS_Thread_Get_Runtime(handles[n]) / deltaTime;
148  }
149  else
150  {
151  data.Running[n] = TASKINFO_RUNNING_FALSE;
152  data.StackRemaining[n] = 0;
153  data.RunningTime[n] = 0;
154  }
155  }
156 
157  // Update object
158  TaskInfoSet(&data);
159 
160  // Done
161  PIOS_Mutex_Unlock(lock);
162 #endif
163 }
164 
bool TaskMonitorQueryRunning(TaskInfoRunningElem task)
Definition: taskmonitor.c:105
DONT_BUILD_IF(TASKINFO_RUNNING_NUMELEM!=TASKINFO_STACKREMAINING_NUMELEM, taskelems1)
static struct pios_thread * handles[TASKINFO_RUNNING_NUMELEM]
Definition: taskmonitor.c:37
void TaskMonitorUpdateAll(void)
Definition: taskmonitor.c:116
bool PIOS_Mutex_Unlock(struct pios_mutex *mtx)
Definition: pios_mutex.c:104
struct pios_mutex * PIOS_Mutex_Create(void)
Definition: pios_mutex.c:43
Task monitoring library.
int32_t TaskMonitorRemove(TaskInfoRunningElem task)
Definition: taskmonitor.c:86
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
static TaskInfoRunningElem task
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
Definition: taskmonitor.c:67
static uint32_t lastMonitorTime
Definition: taskmonitor.c:38
#define hal_lld_get_counter_value()
Returns the current value of the system free running counter.
Definition: chconf.h:483
uint32_t PIOS_Thread_Get_Stack_Usage(struct pios_thread *threadp)
Definition: pios_thread.c:271
static struct pios_mutex * lock
Definition: taskmonitor.c:36
#define halGetCounterValue()
Returns the current value of the system free running counter.
Definition: chconf.h:471
int32_t TaskMonitorInitialize(void)
Definition: taskmonitor.c:50
Includes PiOS and core architecture components.
uint32_t PIOS_Thread_Get_Runtime(struct pios_thread *threadp)
Definition: pios_thread.c:276
#define PIOS_Assert(test)
Definition: pios_debug.h:52
#define PIOS_MUTEX_TIMEOUT_MAX
Definition: pios_mutex.h:30
bool PIOS_Mutex_Lock(struct pios_mutex *mtx, uint32_t timeout_ms)
Definition: pios_mutex.c:66