Skip to content

Commit 36be4a0

Browse files
jasonbuxiaoxiang781216
authored andcommitted
resmonitor: add showinfo/filldisk/fillcpu/fillmem from test/
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>
1 parent a526a4a commit 36be4a0

File tree

8 files changed

+1686
-0
lines changed

8 files changed

+1686
-0
lines changed

testing/resmonitor/CMakeLists.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# ##############################################################################
2+
# apps/testing/resmonitor/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_TESTING_RESMONITOR)
22+
nuttx_add_application(
23+
NAME
24+
showinfo
25+
SRCS
26+
showinfo.c
27+
STACKSIZE
28+
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
29+
PRIORITY
30+
${CONFIG_TESTING_RESMONITOR_PRIORITY})
31+
32+
if(CONFIG_TESTING_RESMONITOR_FILL)
33+
nuttx_add_application(
34+
NAME
35+
filldisk
36+
SRCS
37+
filldisk.c
38+
STACKSIZE
39+
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
40+
PRIORITY
41+
${CONFIG_TESTING_RESMONITOR_PRIORITY})
42+
43+
nuttx_add_application(
44+
NAME
45+
fillcpu
46+
SRCS
47+
fillcpu.c
48+
STACKSIZE
49+
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
50+
PRIORITY
51+
${CONFIG_TESTING_RESMONITOR_PRIORITY})
52+
53+
nuttx_add_application(
54+
NAME
55+
fillmem
56+
SRCS
57+
fillmem.c
58+
STACKSIZE
59+
${CONFIG_TESTING_RESMONITOR_STACKSIZE}
60+
PRIORITY
61+
${CONFIG_TESTING_RESMONITOR_PRIORITY})
62+
endif()
63+
endif()

testing/resmonitor/Kconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config TESTING_RESMONITOR
7+
bool "enable resource monitor [showinfo]"
8+
default n
9+
---help---
10+
Enable resource show with specific duration.
11+
Should not use with LOW_RESOURCE_TEST at the same time.
12+
13+
if TESTING_RESMONITOR
14+
15+
config TESTING_RESMONITOR_PRIORITY
16+
int "Task priority"
17+
default 100
18+
19+
config TESTING_RESMONITOR_STACKSIZE
20+
int "Stack size"
21+
default 4096
22+
23+
config TESTING_RESMONITOR_FILL
24+
bool "[filldisk/fillcpu/fillmem]"
25+
default n
26+
27+
endif

testing/resmonitor/Make.defs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
############################################################################
2+
# apps/testing/resmonitor/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
#
20+
############################################################################
21+
22+
ifneq ($(CONFIG_TESTING_RESMONITOR),)
23+
CONFIGURED_APPS += $(APPDIR)/testing/resmonitor
24+
endif

testing/resmonitor/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# apps/testing/resmonitor/Makefile
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
include $(APPDIR)/Make.defs
16+
17+
PRIORITY = $(CONFIG_TESTING_RESMONITOR_PRIORITY)
18+
STACKSIZE = $(CONFIG_TESTING_RESMONITOR_STACKSIZE)
19+
MODULE = $(CONFIG_TESTING_RESMONITOR)
20+
21+
PROGNAME += showinfo
22+
MAINSRC += $(CURDIR)/showinfo.c
23+
24+
ifeq ($(CONFIG_TESTING_RESMONITOR_FILL),y)
25+
PROGNAME += filldisk
26+
MAINSRC += $(CURDIR)/filldisk.c
27+
PROGNAME += fillcpu
28+
MAINSRC += $(CURDIR)/fillcpu.c
29+
PROGNAME += fillmem
30+
MAINSRC += $(CURDIR)/fillmem.c
31+
endif
32+
33+
include $(APPDIR)/Application.mk

testing/resmonitor/fillcpu.c

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/****************************************************************************
2+
* apps/testing/resmonitor/fillcpu.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <fcntl.h>
26+
#include <signal.h>
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include <sys/stat.h>
31+
#include <sys/time.h>
32+
#include <sys/types.h>
33+
#include <syslog.h>
34+
#include <unistd.h>
35+
36+
/****************************************************************************
37+
* Pre-processor Definitions
38+
****************************************************************************/
39+
40+
#define PATH "/proc"
41+
#define CPULOAD "cpuload"
42+
#define LOADAVG "loadavg"
43+
44+
/****************************************************************************
45+
* Private Data
46+
****************************************************************************/
47+
48+
static int go = 1;
49+
50+
/****************************************************************************
51+
* Private Functions
52+
****************************************************************************/
53+
54+
static void handler(int sig)
55+
{
56+
go = 0;
57+
}
58+
59+
static void show_usages(void)
60+
{
61+
syslog(LOG_WARNING,
62+
"Usage: CMD [-c <cpu>] [-f <filepath>] [-a]\n"
63+
"\t\t-c: set cpu occupation that you except, default 80\n"
64+
"\t\t-f: set write payload path, eg. /tmp/payload, program will "
65+
"write to memory if -f not set\n"
66+
"\t\t-a: the cpu (set by -c) is the cpu occupied by this program\n");
67+
exit(1);
68+
}
69+
70+
static float get_cpu(int pid)
71+
{
72+
float cpu = 0;
73+
char filepath[20];
74+
int ret;
75+
if (pid <= 0)
76+
{
77+
ret = snprintf(filepath, 20, "%s/%s", PATH, CPULOAD);
78+
}
79+
else
80+
{
81+
ret = snprintf(filepath, 20, "%s/%d/%s", PATH, pid, LOADAVG);
82+
}
83+
84+
if (ret < 0)
85+
{
86+
/* syslog(LOG_ERR, "snprintf error\n"); */
87+
88+
return cpu;
89+
}
90+
91+
FILE *fp = fopen(filepath, "r");
92+
if (!fp)
93+
{
94+
return cpu;
95+
}
96+
97+
char buf[8];
98+
fgets(buf, 8, fp);
99+
sscanf(buf, "%f", &cpu);
100+
fclose(fp);
101+
return cpu;
102+
}
103+
104+
static int writefile(char *filepath, char *buffer1, char *buffer2)
105+
{
106+
if (strlen(filepath) == 0)
107+
{
108+
memset(buffer2, '*', 1024);
109+
memcpy(buffer1, buffer2, 1024);
110+
return 0;
111+
}
112+
else
113+
{
114+
int fd;
115+
memset(buffer1, '*', 1024);
116+
if ((fd = open(filepath, O_WRONLY | O_CREAT, 0700)) <= 0)
117+
{
118+
syslog(LOG_ERR, "open file error\n");
119+
return -1;
120+
}
121+
122+
if (write(fd, buffer1, 1024) <= 0)
123+
{
124+
syslog(LOG_ERR, "write file error\n");
125+
close(fd);
126+
return -1;
127+
}
128+
129+
return close(fd);
130+
}
131+
}
132+
133+
int main(int argc, char *argv[])
134+
{
135+
char buf1[1024];
136+
char buf2[1024];
137+
char filepath[40];
138+
struct timeval sleeptime;
139+
int n = 0;
140+
int lowcount = 0;
141+
int cpu = 80;
142+
bool ispid = false;
143+
int time = 10000;
144+
float fcpu;
145+
int o;
146+
147+
memset(filepath, 0, 40);
148+
go = 1;
149+
if (argc == 1)
150+
{
151+
show_usages();
152+
}
153+
154+
while ((o = getopt(argc, argv, "c:f:a")) != EOF)
155+
{
156+
switch (o)
157+
{
158+
case 'c':
159+
cpu = atoi(optarg);
160+
break;
161+
case 'f':
162+
snprintf(filepath, 40, "%s", optarg);
163+
break;
164+
case 'a':
165+
ispid = true;
166+
break;
167+
default:
168+
show_usages();
169+
break;
170+
}
171+
}
172+
173+
signal(SIGINT, handler);
174+
signal(SIGKILL, handler);
175+
176+
while (go)
177+
{
178+
if (time < 1000)
179+
{
180+
time = 1000;
181+
lowcount++;
182+
if (lowcount > 4)
183+
{
184+
lowcount = 0;
185+
n += 2;
186+
}
187+
}
188+
189+
else if (time > 10000)
190+
{
191+
time = 10000;
192+
n -= 1;
193+
n = (n < 0 ? 0 : n);
194+
}
195+
else
196+
{
197+
lowcount = 0;
198+
}
199+
200+
sleeptime.tv_sec = 0;
201+
sleeptime.tv_usec = time;
202+
select(0, NULL, NULL, NULL, &sleeptime);
203+
if (ispid)
204+
{
205+
fcpu = get_cpu(getpid());
206+
}
207+
else
208+
{
209+
fcpu = get_cpu(0);
210+
}
211+
212+
if (fcpu > cpu)
213+
{
214+
time += 1000;
215+
}
216+
else
217+
{
218+
time -= 1000;
219+
}
220+
221+
for (int i = 0; i < n; i++)
222+
{
223+
if (writefile(filepath, buf1, buf2) != 0)
224+
{
225+
break;
226+
}
227+
}
228+
}
229+
230+
syslog(LOG_INFO, "program complete!\n");
231+
return 0;
232+
}

0 commit comments

Comments
 (0)