Skip to content

Commit 843de30

Browse files
committed
Add CPU/Memory usage statistics
1 parent d9c5a58 commit 843de30

File tree

7 files changed

+270
-31
lines changed

7 files changed

+270
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ Use the provided `run.sh` script to start the Process Watchdog application. This
352352
Or just `./run.sh &` which is recommended.
353353

354354
## TODO
355-
- Redesign the apps.c with DAO pattern
356-
- Add CPU & RAM usage to the statistics
355+
- [DONE]Redesign the apps.c with DAO pattern
356+
- [DONE]Add CPU & RAM usage to the statistics
357357
- Create easy-to-use heartbeat libraries
358358
- Enable commands over UDP
359359
- Enable remote syslog server reporting

src/apps.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ char *get_app_name(int i)
110110
return apps[i].name;
111111
}
112112

113+
int get_app_pid(int i)
114+
{
115+
return apps[i].pid;
116+
}
117+
113118
int get_udp_port(void)
114119
{
115120
return app_state.udp_port;

src/apps.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ int get_app_count();
114114
*/
115115
char *get_app_name(int i);
116116

117+
/**
118+
@brief Gets the process ID of the application at the specified index.
119+
120+
@param i Index of the application.
121+
@return Process ID of the application.
122+
*/
123+
int get_app_pid(int i);
124+
117125
/**
118126
@brief Gets the UDP port number specified in the ini file.
119127

src/main.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extern char *optarg;
136136
extern int opterr, optind;
137137

138138
#define APPNAME basename(argv[0])
139-
#define VERSION "1.1.0"
139+
#define VERSION "1.2.0"
140140
#define OPTSTR "i:v:t:h"
141141
#define USAGE_FMT "%s -i <file.ini> [-v] [-h] [-t testname]\n"
142142

@@ -301,7 +301,7 @@ int main(int argc, char *argv[])
301301
// Read statistics
302302
for(int i = 0; i < get_app_count(); i++)
303303
{
304-
stats_read_from_file(i);
304+
stats_read_from_file(i, get_app_name(i));
305305
}
306306

307307
// data buffer
@@ -346,11 +346,17 @@ int main(int argc, char *argv[])
346346
{
347347
if(process_is_started(i))
348348
{
349+
// Update resource usage stats (1 min)
350+
if((get_uptime() % 60) == 0 && process_is_running(i))
351+
{
352+
stats_update_resource_usage(i, get_app_pid(i));
353+
}
354+
349355
// Update stats files periodically (15 mins)
350356
if((get_uptime() % (15 * 60)) == 0)
351357
{
352-
stats_write_to_file(i);
353-
stats_print_to_file(i);
358+
stats_write_to_file(i, get_app_name(i));
359+
stats_print_to_file(i, get_app_name(i));
354360
}
355361

356362
if(!process_is_running(i))
@@ -435,8 +441,8 @@ int main(int argc, char *argv[])
435441
for(int i = 0; i < get_app_count(); i++)
436442
{
437443
// Update stats files
438-
stats_write_to_file(i);
439-
stats_print_to_file(i);
444+
stats_write_to_file(i, get_app_name(i));
445+
stats_print_to_file(i, get_app_name(i));
440446
// Kill running applications
441447
process_kill(i);
442448

src/process.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@
3232

3333
bool process_is_running(int app_index)
3434
{
35-
Application_t *apps = apps_get_array();
35+
int pid = get_app_pid(app_index);
3636

37-
if(apps[app_index].pid <= 0)
37+
if(pid <= 0)
3838
{
3939
return false;
4040
}
4141

4242
// Check if the application is running
43-
if(kill(apps[app_index].pid, 0) == 0)
43+
if(kill(pid, 0) == 0)
4444
{
4545
return true; // Process is running
4646
}
4747
else if(errno == EPERM)
4848
{
49-
LOGE("No permission to check if process %s is running : %s", apps[app_index].name, strerror(errno));
49+
LOGE("No permission to check if process %s is running : %s", get_app_name(app_index), strerror(errno));
5050
return true;
5151
}
5252
else
5353
{
54-
LOGD("Process %s is not running : %s", apps[app_index].name, strerror(errno));
54+
LOGD("Process %s is not running : %s", get_app_name(app_index), strerror(errno));
5555
}
5656

5757
return false;

0 commit comments

Comments
 (0)