Skip to content

Commit

Permalink
Add utils.c and utils.h
Browse files Browse the repository at this point in the history
Move set_affinity(), print_time(), and dbg() to utils.c

Add version.h
Bump priority to both threads
Make the data gathering thread use CPU 1
Make the udp thread use CPU 0
  • Loading branch information
bdubbs committed Oct 20, 2024
1 parent b7b06a8 commit 5e6ef87
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 62 deletions.
3 changes: 3 additions & 0 deletions ABOUT
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ techniques in a Linux envronment. It demonstrates the following:
- Using the fork command
- Creating a thread
- Handling signals
- Raising process priority
- Attaching a process affinity to a CPU core
- Using a mutex (mutual exclusion token)

When the Linux kernel runs, it frequently updates files in the /proc
and /sys virtual file systems. For the purpose of this program we
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
objects = sysmond.o args.o udp.o daemonize.o signal.o data.o temps.o
objects = sysmond.o args.o udp.o daemonize.o signal.o data.o temps.o utils.o

sysmond: $(objects)
gcc -o sysmond $(CFLAGS) -s $(objects)
Expand All @@ -8,11 +8,15 @@ $(objects): %.o: %.c

sysmond.o: sysmond.c sysmond.h
args.o: args.c sysmond.h args.h
udp.o: udp.c sysmond.h udp.h
udp.o: udp.c sysmond.h udp.h utils.h
daemonize.o: daemonize.c sysmond.h
signal.o: signal.c sysmond.h
data.o: data.c sysmond.h
temps.o: temps.c sysmond.h
utils.o: utils.c utils.h

install:
cp -v sysmond /usr/sbin

clean:
rm -f $(objects) sysmond
Expand Down
4 changes: 3 additions & 1 deletion args.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "sysmond.h"
#include "args.h"
#include "version.h"

static const char* shortOptions = "c:p:d:u:s:h:";

Expand Down Expand Up @@ -38,7 +39,8 @@ char dbgFile[64];
// Start functions

void help( char* name )
{ printf( "Syntax: %s {options} \nversion %s\n%s", name, version,
{
printf( "Syntax: %s {options} \nVersion %s\n%s", name, VERSION,
" -c, --config-file <filename> -- config file location (default /etc/sysmond.conf)\n"
" -p, --pid-file <filename> -- pid file location (default /run/sysmond.pid)\n"
" -d, --debug-file <filname> -- debug file location (default /tmp/sysmond.dbg)\n"
Expand Down
4 changes: 2 additions & 2 deletions daemonize.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ void daemonize()
close( STDOUT_FILENO );
close( STDERR_FILENO );

nice( -1 );
set_affinity( 1 );
nice( -1 ); // Bump priority
set_affinity( 1 ); // Just run on CPU 1
}
62 changes: 7 additions & 55 deletions sysmond.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define _GNU_SOURCE

#include <stdio.h> // perror, snprintf, fopen, FILE, fgets, fwrite, fclose
#include <time.h> // time_t, time(), ctime, gmtime, struct tm
#include <unistd.h> // getuid, optind
#include <string.h> // strtok, strcmp, strlen
#include <stdlib.h> // strtol, exit, EXIT_FAILURE
Expand All @@ -12,24 +11,13 @@

#include "sysmond.h"

//#include <sys/time.h>

// Globals
int data_available = 0;
char data[ 2 ][ 1024 ];
const char* version = "3.0.0";
char* time_string;
pthread_mutex_t mutex;

// Start functions

// Populate time_string with formatted time without newline
void print_time()
{
const time_t now = time( NULL );
time_string = ctime( &now );
time_string[ 24 ] = 0; // Remove newline
}

// Determine if 'name' is already running
int is_running( char* name)
{
Expand All @@ -48,12 +36,12 @@ int is_running( char* name)

while( (ent = readdir(dir)) != NULL )
{
/* If endptr is not a null character, the directory is not
* entirely numeric, so ignore it */
// If endptr is not a null character, the directory is not
// entirely numeric, so ignore it
long lpid = strtol( ent->d_name, &endptr, 10 );
if (*endptr != '\0') continue;

/* Try to open the cmdline file */
// Try to open the cmdline file
snprintf( buf, sizeof(buf), "/proc/%ld/cmdline", lpid );
FILE* fp = fopen( buf, "r" );

Expand All @@ -63,9 +51,9 @@ int is_running( char* name)
{
char* first = basename( strtok( buf, " " ) );

/* Check the first token in the file, the program name.
* We should always match this instance, but if another
* is running, we will get more than 1.*/
// Check the first token in the file, the program name.
// We should always match this instance, but if another
// is running, we will get more than 1.

if ( ! strcmp( first, base) ) running++;
}
Expand All @@ -78,42 +66,6 @@ int is_running( char* name)
return running;
}

void set_affinity( int cpu )
{
cpu_set_t mask;

CPU_ZERO( &mask );
CPU_SET( cpu, &mask );
pthread_setaffinity_np( 0, sizeof(mask), &mask );
// Ignore result
}



// Write txt to debug file
void dbg( char* txt )
{
FILE* file;

file = fopen( sysmond_args.dbgFile, "a" );
if ( file != NULL )
{
print_time();
strcat( time_string, " " );
fwrite( time_string,
strlen( time_string ),
1,
file );

fwrite( txt,
strlen( txt ),
1,
file );

fclose( file );
}
}

int main( int argc, char* argv[])
{
// This is really to get the config file.
Expand Down
2 changes: 0 additions & 2 deletions sysmond.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ extern void get_temps ( char* ); // temps.c
extern void set_affinity ( int ); // sysmond.c

// Local functions
void dbg ( char* );
int is_running ( char* );
void print_time ( void );

58 changes: 58 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#define _GNU_SOURCE

#include <stdio.h> // perror, snprintf, fopen, FILE, fgets, fwrite, fclose
#include <time.h> // time_t, time(), ctime, gmtime, struct tm
#include <string.h> // strlen
#include <pthread.h> // pthread_create, pthread_setaffinity_np, CPU_ZERO, CPU_SET

#include "sysmond.h"
#include "utils.h"

// Globals
char* time_string;

// Start functions

// Populate time_string with formatted time without newline
void print_time()
{
const time_t now = time( NULL );
time_string = ctime( &now );
time_string[ 24 ] = 0; // Remove newline
}

// Write txt to debug file
void dbg( char* txt )
{
FILE* file;

file = fopen( sysmond_args.dbgFile, "a" );
if ( file != NULL )
{
print_time();
strcat( time_string, " " );
fwrite( time_string,
strlen( time_string ),
1,
file );

fwrite( txt,
strlen( txt ),
1,
file );

fclose( file );
}
}

// Attach a process/threaad to a specific CPU
void set_affinity( int cpu )
{
cpu_set_t mask;

CPU_ZERO( &mask );
CPU_SET( cpu, &mask );
pthread_setaffinity_np( pthread_self(), sizeof(mask), &mask );
// Ignore result
}

4 changes: 4 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void set_affinity( int );
void print_time ( void );
void dbg ( char* );

2 changes: 2 additions & 0 deletions version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define VERSION "3.0.0"

0 comments on commit 5e6ef87

Please sign in to comment.