Skip to content

Commit

Permalink
resmonitor: add showinfo/filldisk/fillcpu/fillmem from test/
Browse files Browse the repository at this point in the history
add cmake support

after enable TESTING_RESMONITOR, use showinfo -i 2 can show cpuload to
syslog every two seconds.

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
  • Loading branch information
jasonbu authored and xiaoxiang781216 committed Oct 28, 2024
1 parent a526a4a commit 36be4a0
Show file tree
Hide file tree
Showing 8 changed files with 1,686 additions and 0 deletions.
63 changes: 63 additions & 0 deletions testing/resmonitor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# ##############################################################################
# apps/testing/resmonitor/CMakeLists.txt
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
# ##############################################################################

if(CONFIG_TESTING_RESMONITOR)
nuttx_add_application(
NAME
showinfo
SRCS
showinfo.c
STACKSIZE
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
PRIORITY
${CONFIG_TESTING_RESMONITOR_PRIORITY})

if(CONFIG_TESTING_RESMONITOR_FILL)
nuttx_add_application(
NAME
filldisk
SRCS
filldisk.c
STACKSIZE
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
PRIORITY
${CONFIG_TESTING_RESMONITOR_PRIORITY})

nuttx_add_application(
NAME
fillcpu
SRCS
fillcpu.c
STACKSIZE
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
PRIORITY
${CONFIG_TESTING_RESMONITOR_PRIORITY})

nuttx_add_application(
NAME
fillmem
SRCS
fillmem.c
STACKSIZE
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
PRIORITY
${CONFIG_TESTING_RESMONITOR_PRIORITY})
endif()
endif()
27 changes: 27 additions & 0 deletions testing/resmonitor/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config TESTING_RESMONITOR
bool "enable resource monitor [showinfo]"
default n
---help---
Enable resource show with specific duration.
Should not use with LOW_RESOURCE_TEST at the same time.

if TESTING_RESMONITOR

config TESTING_RESMONITOR_PRIORITY
int "Task priority"
default 100

config TESTING_RESMONITOR_STACKSIZE
int "Stack size"
default 4096

config TESTING_RESMONITOR_FILL
bool "[filldisk/fillcpu/fillmem]"
default n

endif
24 changes: 24 additions & 0 deletions testing/resmonitor/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
############################################################################
# apps/testing/resmonitor/Make.defs
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
#
############################################################################

ifneq ($(CONFIG_TESTING_RESMONITOR),)
CONFIGURED_APPS += $(APPDIR)/testing/resmonitor
endif
33 changes: 33 additions & 0 deletions testing/resmonitor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# apps/testing/resmonitor/Makefile
#
# Licensed 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

# http://www.apache.org/licenses/LICENSE-2.0

# 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.

include $(APPDIR)/Make.defs

PRIORITY = $(CONFIG_TESTING_RESMONITOR_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_RESMONITOR_STACKSIZE)
MODULE = $(CONFIG_TESTING_RESMONITOR)

PROGNAME += showinfo
MAINSRC += $(CURDIR)/showinfo.c

ifeq ($(CONFIG_TESTING_RESMONITOR_FILL),y)
PROGNAME += filldisk
MAINSRC += $(CURDIR)/filldisk.c
PROGNAME += fillcpu
MAINSRC += $(CURDIR)/fillcpu.c
PROGNAME += fillmem
MAINSRC += $(CURDIR)/fillmem.c
endif

include $(APPDIR)/Application.mk
232 changes: 232 additions & 0 deletions testing/resmonitor/fillcpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/****************************************************************************
* apps/testing/resmonitor/fillcpu.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define PATH "/proc"
#define CPULOAD "cpuload"
#define LOADAVG "loadavg"

/****************************************************************************
* Private Data
****************************************************************************/

static int go = 1;

/****************************************************************************
* Private Functions
****************************************************************************/

static void handler(int sig)
{
go = 0;
}

static void show_usages(void)
{
syslog(LOG_WARNING,
"Usage: CMD [-c <cpu>] [-f <filepath>] [-a]\n"
"\t\t-c: set cpu occupation that you except, default 80\n"
"\t\t-f: set write payload path, eg. /tmp/payload, program will "
"write to memory if -f not set\n"
"\t\t-a: the cpu (set by -c) is the cpu occupied by this program\n");
exit(1);
}

static float get_cpu(int pid)
{
float cpu = 0;
char filepath[20];
int ret;
if (pid <= 0)
{
ret = snprintf(filepath, 20, "%s/%s", PATH, CPULOAD);
}
else
{
ret = snprintf(filepath, 20, "%s/%d/%s", PATH, pid, LOADAVG);
}

if (ret < 0)
{
/* syslog(LOG_ERR, "snprintf error\n"); */

return cpu;
}

FILE *fp = fopen(filepath, "r");
if (!fp)
{
return cpu;
}

char buf[8];
fgets(buf, 8, fp);
sscanf(buf, "%f", &cpu);
fclose(fp);
return cpu;
}

static int writefile(char *filepath, char *buffer1, char *buffer2)
{
if (strlen(filepath) == 0)
{
memset(buffer2, '*', 1024);
memcpy(buffer1, buffer2, 1024);
return 0;
}
else
{
int fd;
memset(buffer1, '*', 1024);
if ((fd = open(filepath, O_WRONLY | O_CREAT, 0700)) <= 0)
{
syslog(LOG_ERR, "open file error\n");
return -1;
}

if (write(fd, buffer1, 1024) <= 0)
{
syslog(LOG_ERR, "write file error\n");
close(fd);
return -1;
}

return close(fd);
}
}

int main(int argc, char *argv[])
{
char buf1[1024];
char buf2[1024];
char filepath[40];
struct timeval sleeptime;
int n = 0;
int lowcount = 0;
int cpu = 80;
bool ispid = false;
int time = 10000;
float fcpu;
int o;

memset(filepath, 0, 40);
go = 1;
if (argc == 1)
{
show_usages();
}

while ((o = getopt(argc, argv, "c:f:a")) != EOF)
{
switch (o)
{
case 'c':
cpu = atoi(optarg);
break;
case 'f':
snprintf(filepath, 40, "%s", optarg);
break;
case 'a':
ispid = true;
break;
default:
show_usages();
break;
}
}

signal(SIGINT, handler);
signal(SIGKILL, handler);

while (go)
{
if (time < 1000)
{
time = 1000;
lowcount++;
if (lowcount > 4)
{
lowcount = 0;
n += 2;
}
}

else if (time > 10000)
{
time = 10000;
n -= 1;
n = (n < 0 ? 0 : n);
}
else
{
lowcount = 0;
}

sleeptime.tv_sec = 0;
sleeptime.tv_usec = time;
select(0, NULL, NULL, NULL, &sleeptime);
if (ispid)
{
fcpu = get_cpu(getpid());
}
else
{
fcpu = get_cpu(0);
}

if (fcpu > cpu)
{
time += 1000;
}
else
{
time -= 1000;
}

for (int i = 0; i < n; i++)
{
if (writefile(filepath, buf1, buf2) != 0)
{
break;
}
}
}

syslog(LOG_INFO, "program complete!\n");
return 0;
}
Loading

0 comments on commit 36be4a0

Please sign in to comment.