2018-11-24 20:35:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/critmon/critmon.c
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-11-24 20:35:53 +01:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-11-24 20:35:53 +01:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-11-24 20:35:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSTEM_CRITMONITOR
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-25 00:56:23 +01:00
|
|
|
#ifndef CONFIG_SYSTEM_CRITMONITOR_DAEMON_STACKSIZE
|
|
|
|
# define CONFIG_SYSTEM_CRITMONITOR_DAEMON_STACKSIZE 2048
|
2018-11-24 20:35:53 +01:00
|
|
|
#endif
|
|
|
|
|
2018-11-25 00:56:23 +01:00
|
|
|
#ifndef CONFIG_SYSTEM_CRITMONITOR_DAEMON_PRIORITY
|
|
|
|
# define CONFIG_SYSTEM_CRITMONITOR_DAEMON_PRIORITY 50
|
2018-11-24 20:35:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SYSTEM_CRITMONITOR_INTERVAL
|
|
|
|
# define CONFIG_SYSTEM_CRITMONITOR_INTERVAL 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT
|
|
|
|
# define CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/proc"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct critmon_state_s
|
|
|
|
{
|
|
|
|
volatile bool started;
|
|
|
|
volatile bool stop;
|
|
|
|
pid_t pid;
|
|
|
|
char line[80];
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct critmon_state_s g_critmon;
|
|
|
|
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
static const char g_name[] = "Name:";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: critmon_isolate_value
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR char *critmon_isolate_value(FAR char *line)
|
|
|
|
{
|
|
|
|
FAR char *ptr;
|
|
|
|
|
|
|
|
while (isblank(*line) && *line != '\0')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = line;
|
|
|
|
while (*ptr != '\n' && *ptr != '\r' && *ptr != '\0')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: critmon_process_directory
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int critmon_process_directory(FAR struct dirent *entryp)
|
|
|
|
{
|
|
|
|
FAR const char *tmpstr;
|
|
|
|
FAR char *filepath;
|
|
|
|
FAR char *maxpreemp;
|
|
|
|
FAR char *maxcrit;
|
2021-05-07 17:16:07 +02:00
|
|
|
FAR char *maxrun;
|
2023-04-24 06:36:07 +02:00
|
|
|
FAR char *runtime;
|
2018-11-25 14:42:47 +01:00
|
|
|
FAR char *endptr;
|
2018-11-24 20:35:53 +01:00
|
|
|
FILE *stream;
|
|
|
|
int len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
FAR char *name = NULL;
|
|
|
|
|
|
|
|
/* Read the task status to get the task name */
|
|
|
|
|
|
|
|
filepath = NULL;
|
2021-06-10 18:40:35 +02:00
|
|
|
ret = asprintf(&filepath,
|
|
|
|
CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/%s/status",
|
2018-11-24 20:35:53 +01:00
|
|
|
entryp->d_name);
|
2023-04-12 17:42:28 +02:00
|
|
|
if (ret < 0)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
2021-06-10 18:40:35 +02:00
|
|
|
fprintf(stderr,
|
2023-04-12 17:42:28 +02:00
|
|
|
"Csection Monitor: Failed to create path to status file\n");
|
|
|
|
return -ENOMEM;
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the status file */
|
|
|
|
|
|
|
|
stream = fopen(filepath, "r");
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
fprintf(stderr, "Csection Monitor: Failed to open %s: %d\n",
|
|
|
|
filepath, ret);
|
|
|
|
goto errout_with_filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(g_critmon.line, 80, stream) != NULL)
|
|
|
|
{
|
|
|
|
g_critmon.line[79] = '\n';
|
|
|
|
len = strlen(g_name);
|
|
|
|
if (strncmp(g_critmon.line, g_name, len) == 0)
|
|
|
|
{
|
|
|
|
tmpstr = critmon_isolate_value(&g_critmon.line[len]);
|
|
|
|
if (*tmpstr == '\0')
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strdup(tmpstr);
|
|
|
|
if (name == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(filepath);
|
|
|
|
fclose(stream);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Read critical section information */
|
|
|
|
|
|
|
|
filepath = NULL;
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
ret = asprintf(&filepath,
|
|
|
|
CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/%s/critmon",
|
2018-11-24 20:35:53 +01:00
|
|
|
entryp->d_name);
|
2023-04-12 17:42:28 +02:00
|
|
|
if (ret < 0)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
2021-06-10 18:40:35 +02:00
|
|
|
fprintf(stderr, "Csection Monitor: "
|
2023-04-12 17:42:28 +02:00
|
|
|
"Failed to create path to Csection file\n");
|
2018-11-24 20:35:53 +01:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the Csection file */
|
|
|
|
|
|
|
|
stream = fopen(filepath, "r");
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
fprintf(stderr, "Csection Monitor: Failed to open %s: %d\n",
|
|
|
|
filepath, ret);
|
|
|
|
goto errout_with_filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the line containing the Csection max durations */
|
|
|
|
|
|
|
|
if (fgets(g_critmon.line, 80, stream) == NULL)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
fprintf(stderr, "Csection Monitor: Failed to read from %s: %d\n",
|
|
|
|
filepath, ret);
|
|
|
|
goto errout_with_filepath;
|
|
|
|
}
|
|
|
|
|
2021-05-07 17:16:07 +02:00
|
|
|
/* Input Format: X.XXXXXXXXX,X.XXXXXXXXX,X.XXXXXXXXX
|
|
|
|
* Output Format: X.XXXXXXXXX X.XXXXXXXXX X.XXXXXXXXX NNNNN <name>
|
2018-11-25 14:42:47 +01:00
|
|
|
*/
|
2018-11-24 20:35:53 +01:00
|
|
|
|
|
|
|
maxpreemp = g_critmon.line;
|
|
|
|
maxcrit = strchr(g_critmon.line, ',');
|
|
|
|
|
|
|
|
if (maxcrit != NULL)
|
|
|
|
{
|
|
|
|
*maxcrit++ = '\0';
|
2021-05-07 17:16:07 +02:00
|
|
|
|
|
|
|
maxrun = strchr(maxcrit, ',');
|
|
|
|
if (maxrun != NULL)
|
|
|
|
{
|
|
|
|
*maxrun++ = '\0';
|
|
|
|
|
2023-04-24 06:36:07 +02:00
|
|
|
runtime = strchr(maxrun, ',');
|
|
|
|
if (runtime != NULL)
|
2021-05-07 17:16:07 +02:00
|
|
|
{
|
2023-04-24 06:36:07 +02:00
|
|
|
*runtime++ = '\0';
|
|
|
|
endptr = strchr(runtime, '\n');
|
|
|
|
if (endptr != NULL)
|
|
|
|
{
|
|
|
|
*endptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
runtime = "None";
|
2021-05-07 17:16:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-11-25 14:42:47 +01:00
|
|
|
{
|
2021-05-07 17:16:07 +02:00
|
|
|
maxrun = "None";
|
2023-04-24 15:50:21 +02:00
|
|
|
runtime = "None";
|
2018-11-25 14:42:47 +01:00
|
|
|
}
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maxcrit = "None";
|
2021-05-07 17:16:07 +02:00
|
|
|
maxrun = "None";
|
2023-04-24 15:50:21 +02:00
|
|
|
runtime = "None";
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
2020-01-31 15:13:45 +01:00
|
|
|
|
2018-11-24 20:35:53 +01:00
|
|
|
/* Finally, output the stack info that we gleaned from the procfs */
|
|
|
|
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
2023-04-24 06:36:07 +02:00
|
|
|
printf("%11s %11s %11s %-16s %-5s %s\n",
|
|
|
|
maxpreemp, maxcrit, maxrun, runtime, entryp->d_name, name);
|
2018-11-24 20:35:53 +01:00
|
|
|
#else
|
2023-04-24 06:36:07 +02:00
|
|
|
printf("%11s %11s %11s %16s %5s\n",
|
|
|
|
maxpreemp, maxcrit, maxrun, runtime, entryp->d_name);
|
2018-11-24 20:35:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_stream:
|
|
|
|
fclose(stream);
|
|
|
|
|
|
|
|
errout_with_filepath:
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
errout_with_name:
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
if (name != NULL)
|
|
|
|
{
|
|
|
|
free(name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: critmon_check_name
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool critmon_check_name(FAR char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Check each character in the name */
|
|
|
|
|
|
|
|
for (i = 0; i < NAME_MAX && name[i] != '\0'; i++)
|
|
|
|
{
|
|
|
|
if (!isdigit(name[i]))
|
|
|
|
{
|
|
|
|
/* Name contains something other than a decimal numeric character */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: critmon_global_crit
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void critmon_global_crit(void)
|
|
|
|
{
|
|
|
|
FAR char *filepath;
|
|
|
|
FAR char *cpu;
|
|
|
|
FAR char *maxpreemp;
|
|
|
|
FAR char *maxcrit;
|
2018-11-25 14:42:47 +01:00
|
|
|
FAR char *endptr;
|
2018-11-24 20:35:53 +01:00
|
|
|
FILE *stream;
|
|
|
|
int errcode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Read critical section information */
|
|
|
|
|
|
|
|
filepath = NULL;
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
ret = asprintf(&filepath,
|
|
|
|
CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/critmon");
|
2023-04-12 17:42:28 +02:00
|
|
|
if (ret < 0)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
2021-06-10 18:40:35 +02:00
|
|
|
fprintf(stderr, "Csection Monitor: "
|
2023-04-12 17:42:28 +02:00
|
|
|
"Failed to create path to Csection file\n");
|
2018-11-24 20:35:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the Csection file */
|
|
|
|
|
|
|
|
stream = fopen(filepath, "r");
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
errcode = errno;
|
|
|
|
fprintf(stderr, "Csection Monitor: Failed to open %s: %d\n",
|
|
|
|
filepath, errcode);
|
|
|
|
goto errout_with_filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the line containing the Csection max durations for each CPU */
|
|
|
|
|
|
|
|
while (fgets(g_critmon.line, 80, stream) != NULL)
|
|
|
|
{
|
2018-11-25 14:42:47 +01:00
|
|
|
/* Input Format: X,X.XXXXXXXXX,X.XXXXXXXXX
|
2018-11-25 16:50:21 +01:00
|
|
|
* Output Format: X.XXXXXXXXX X.XXXXXXXXX CPU X
|
2018-11-25 14:42:47 +01:00
|
|
|
*/
|
2018-11-24 20:35:53 +01:00
|
|
|
|
|
|
|
cpu = g_critmon.line;
|
|
|
|
maxpreemp = strchr(g_critmon.line, ',');
|
|
|
|
|
|
|
|
if (maxpreemp != NULL)
|
|
|
|
{
|
|
|
|
*maxpreemp++ = '\0';
|
2018-11-25 00:56:23 +01:00
|
|
|
maxcrit = strchr(maxpreemp, ',');
|
2018-11-24 20:35:53 +01:00
|
|
|
if (maxcrit != NULL)
|
|
|
|
{
|
|
|
|
*maxcrit++ = '\0';
|
2018-11-25 14:42:47 +01:00
|
|
|
endptr = strchr(maxcrit, '\n');
|
|
|
|
if (endptr != NULL)
|
|
|
|
{
|
|
|
|
*endptr = '\0';
|
|
|
|
}
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maxcrit = "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maxpreemp = "None";
|
|
|
|
maxcrit = "None";
|
|
|
|
}
|
2020-01-31 15:13:45 +01:00
|
|
|
|
2018-11-24 20:35:53 +01:00
|
|
|
/* Finally, output the stack info that we gleaned from the procfs */
|
|
|
|
|
2023-04-24 06:36:07 +02:00
|
|
|
printf("%11s %11s ----------- ---------------- ---- CPU %s\n",
|
2021-05-07 17:16:07 +02:00
|
|
|
maxpreemp, maxcrit, cpu);
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(stream);
|
|
|
|
|
|
|
|
errout_with_filepath:
|
|
|
|
free(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-03-31 17:30:53 +02:00
|
|
|
* Name: critmon_list_once
|
2018-11-24 20:35:53 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
static int critmon_list_once(void)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
|
|
|
int exitcode = EXIT_SUCCESS;
|
|
|
|
int errcount = 0;
|
2021-03-31 17:30:53 +02:00
|
|
|
DIR *dirp;
|
2018-11-24 20:35:53 +01:00
|
|
|
int ret;
|
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Output a Header */
|
2018-11-25 14:42:47 +01:00
|
|
|
|
2018-11-25 16:50:21 +01:00
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
2023-04-24 06:36:07 +02:00
|
|
|
printf("PRE-EMPTION CSECTION RUN TIME "
|
|
|
|
"PID DESCRIPTION\n");
|
2018-11-25 16:50:21 +01:00
|
|
|
#else
|
2023-04-24 06:36:07 +02:00
|
|
|
printf("PRE-EMPTION CSECTION RUN TIME PID\n");
|
2018-11-25 16:50:21 +01:00
|
|
|
#endif
|
2018-11-25 14:42:47 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Should global usage first */
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
critmon_global_crit();
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Open the top-level procfs directory */
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
dirp = opendir(CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT);
|
|
|
|
if (dirp == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to open the directory */
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
fprintf(stderr, "Csection Monitor: Failed to open directory: %s\n",
|
|
|
|
CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT);
|
2022-04-19 11:03:00 +02:00
|
|
|
return EXIT_FAILURE;
|
2021-03-31 17:30:53 +02:00
|
|
|
}
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Read each directory entry */
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
FAR struct dirent *entryp = readdir(dirp);
|
|
|
|
if (entryp == NULL)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Finished with this directory */
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Task/thread entries in the /proc directory will all be (1)
|
|
|
|
* directories with (2) all numeric names.
|
|
|
|
*/
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
if (DIRENT_ISDIRECTORY(entryp->d_type) &&
|
|
|
|
critmon_check_name(entryp->d_name))
|
|
|
|
{
|
|
|
|
/* Looks good -- process the directory */
|
|
|
|
|
|
|
|
ret = critmon_process_directory(entryp);
|
|
|
|
if (ret < 0)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
2021-03-31 17:30:53 +02:00
|
|
|
/* Failed to process the thread directory */
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
fprintf(stderr, "Csection Monitor: "
|
|
|
|
"Failed to process sub-directory: %s\n",
|
|
|
|
entryp->d_name);
|
2018-11-24 20:35:53 +01:00
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
if (++errcount > 100)
|
|
|
|
{
|
2021-06-10 18:40:35 +02:00
|
|
|
fprintf(stderr, "Csection Monitor: "
|
2021-03-31 17:30:53 +02:00
|
|
|
"Too many errors ... exiting\n");
|
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
break;
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 17:30:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
return exitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: critmon_daemon
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int critmon_daemon(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int exitcode = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
printf("Csection Monitor: Running: %d\n", g_critmon.pid);
|
|
|
|
|
|
|
|
/* Loop until we detect that there is a request to stop. */
|
|
|
|
|
|
|
|
while (!g_critmon.stop)
|
|
|
|
{
|
|
|
|
exitcode = critmon_list_once();
|
|
|
|
if (exitcode != EXIT_SUCCESS)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2021-06-19 17:35:11 +02:00
|
|
|
|
|
|
|
/* Wait for the next sample interval */
|
|
|
|
|
|
|
|
sleep(CONFIG_SYSTEM_CRITMONITOR_INTERVAL);
|
2018-11-24 20:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stopped */
|
|
|
|
|
|
|
|
g_critmon.stop = false;
|
|
|
|
g_critmon.started = false;
|
|
|
|
printf("Csection Monitor: Stopped: %d\n", g_critmon.pid);
|
|
|
|
|
|
|
|
return exitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
int critmon_start_main(int argc, char **argv)
|
2018-11-24 20:35:53 +01:00
|
|
|
{
|
|
|
|
/* Has the monitor already started? */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
if (!g_critmon.started)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* No.. start it now */
|
|
|
|
|
|
|
|
/* Then start the stack monitoring daemon */
|
|
|
|
|
|
|
|
g_critmon.started = true;
|
|
|
|
g_critmon.stop = false;
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
ret = task_create("Csection Monitor",
|
|
|
|
CONFIG_SYSTEM_CRITMONITOR_DAEMON_PRIORITY,
|
2018-11-25 00:56:23 +01:00
|
|
|
CONFIG_SYSTEM_CRITMONITOR_DAEMON_STACKSIZE,
|
2022-10-15 19:55:16 +02:00
|
|
|
critmon_daemon, NULL);
|
2018-11-24 20:35:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2021-06-10 18:40:35 +02:00
|
|
|
printf("Csection Monitor ERROR: "
|
|
|
|
"Failed to start the stack monitor: %d\n",
|
2018-11-24 20:35:53 +01:00
|
|
|
errcode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critmon.pid = ret;
|
|
|
|
printf("Csection Monitor: Started: %d\n", g_critmon.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
printf("Csection Monitor: %s: %d\n",
|
|
|
|
g_critmon.stop ? "Stopping" : "Running", g_critmon.pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int critmon_stop_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* Has the monitor already started? */
|
|
|
|
|
|
|
|
if (g_critmon.started)
|
|
|
|
{
|
|
|
|
/* Stop the stack monitor. The next time the monitor wakes up,
|
|
|
|
* it will see the stop indication and will exist.
|
|
|
|
*/
|
|
|
|
|
|
|
|
printf("Csection Monitor: Stopping: %d\n", g_critmon.pid);
|
|
|
|
g_critmon.stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Csection Monitor: Stopped: %d\n", g_critmon.pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:30:53 +02:00
|
|
|
int critmon_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return critmon_list_once();
|
|
|
|
}
|
|
|
|
|
2018-11-24 20:35:53 +01:00
|
|
|
#endif /* CONFIG_SYSTEM_CRITMONITOR */
|