summaryrefslogtreecommitdiff
path: root/src/tests/dlt-test-multi-process.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/dlt-test-multi-process.c')
-rw-r--r--src/tests/dlt-test-multi-process.c398
1 files changed, 199 insertions, 199 deletions
diff --git a/src/tests/dlt-test-multi-process.c b/src/tests/dlt-test-multi-process.c
index 93fd069..c5c8ddd 100644
--- a/src/tests/dlt-test-multi-process.c
+++ b/src/tests/dlt-test-multi-process.c
@@ -64,16 +64,16 @@
// Structs
typedef struct {
- int nmsgs; // Number of messages to send
- int nprocs; // Number of processes to start
- int nthreads; // Number of threads to start
- int delay; // Delay between logs messages for each process
- int delay_fudge; // Fudge the delay by 0-n to cause desynchronization
+ int nmsgs; // Number of messages to send
+ int nprocs; // Number of processes to start
+ int nthreads; // Number of threads to start
+ int delay; // Delay between logs messages for each process
+ int delay_fudge; // Fudge the delay by 0-n to cause desynchronization
} s_parameters;
typedef struct {
- s_parameters params;
- DltContext ctx;
+ s_parameters params;
+ DltContext ctx;
} s_thread_data;
// Forward declarations
@@ -97,31 +97,31 @@ unsigned int pidcount = 0;
*/
void usage(char *prog_name)
{
- char version[255];
- dlt_get_version(version,255);
- s_parameters defaults;
- init_params(&defaults);
-
- printf("Usage: %s [options]\n", prog_name);
- printf("Test application for stress testing the daemon with multiple processes and threads.\n");
- printf("%s\n", version);
- printf("Options (Default):\n");
- printf(" -m number Number of messages per thread to send. (%d)\n", defaults.nmsgs);
- printf(" -p number Number of processes to start. (%d), Max %d.\n", defaults.nprocs, MAX_PROCS);
- printf(" -t number Number of threads per process. (%d), Max %d.\n", defaults.nthreads, MAX_THREADS);
- printf(" -d delay Delay in milliseconds to wait between log messages. (%d)\n", defaults.delay);
- printf(" -f delay Random fudge in milliseconds to add to delay. (%d)\n", defaults.delay_fudge);
+ char version[255];
+ dlt_get_version(version,255);
+ s_parameters defaults;
+ init_params(&defaults);
+
+ printf("Usage: %s [options]\n", prog_name);
+ printf("Test application for stress testing the daemon with multiple processes and threads.\n");
+ printf("%s\n", version);
+ printf("Options (Default):\n");
+ printf(" -m number Number of messages per thread to send. (%d)\n", defaults.nmsgs);
+ printf(" -p number Number of processes to start. (%d), Max %d.\n", defaults.nprocs, MAX_PROCS);
+ printf(" -t number Number of threads per process. (%d), Max %d.\n", defaults.nthreads, MAX_THREADS);
+ printf(" -d delay Delay in milliseconds to wait between log messages. (%d)\n", defaults.delay);
+ printf(" -f delay Random fudge in milliseconds to add to delay. (%d)\n", defaults.delay_fudge);
}
/**
* Set nice default values for parameters
*/
void init_params(s_parameters * params) {
- params->nmsgs = 100;
- params->nprocs = 10;
- params->nthreads = 2;
- params->delay = 1000;
- params->delay_fudge = 100;
+ params->nmsgs = 100;
+ params->nprocs = 10;
+ params->nthreads = 2;
+ params->delay = 1000;
+ params->delay_fudge = 100;
}
/**
@@ -129,58 +129,58 @@ void init_params(s_parameters * params) {
*/
int read_cli(s_parameters *params, int argc, char **argv)
{
- int c;
- opterr = 0;
+ int c;
+ opterr = 0;
while ((c = getopt (argc, argv, "m:p:t:d:f:")) != -1)
- {
- switch(c)
- {
- case 'm':
- params->nmsgs = atoi(optarg);
- break;
- case 'p':
- params->nprocs = atoi(optarg);
- if(params->nprocs > MAX_PROCS)
- {
- fprintf(stderr, "Too many processes selected.\n");
- return -1;
- }
- break;
- case 't':
- params->nthreads = atoi(optarg);
- if(params->nprocs > MAX_PROCS)
- {
- fprintf(stderr, "Too many threads selected.\n");
- return -1;
- }
- break;
- case 'd':
- params->delay = atoi(optarg);
- break;
- case 'f':
- params->delay_fudge = atoi(optarg);
- break;
- case '?':
- if(optopt == 'n' || optopt == 'd' || optopt == 'f')
- {
- fprintf(stderr, "Option -%c requires an argument.\n", optopt);
- }
- else if(isprint(optopt))
- {
- fprintf(stderr, "Unknown option '-%c'.\n", optopt);
- }
- else
- {
- fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
- }
- return -1;
- break;
- default:
- abort();
+ {
+ switch(c)
+ {
+ case 'm':
+ params->nmsgs = atoi(optarg);
+ break;
+ case 'p':
+ params->nprocs = atoi(optarg);
+ if(params->nprocs > MAX_PROCS)
+ {
+ fprintf(stderr, "Too many processes selected.\n");
+ return -1;
+ }
+ break;
+ case 't':
+ params->nthreads = atoi(optarg);
+ if(params->nprocs > MAX_PROCS)
+ {
+ fprintf(stderr, "Too many threads selected.\n");
+ return -1;
+ }
+ break;
+ case 'd':
+ params->delay = atoi(optarg);
+ break;
+ case 'f':
+ params->delay_fudge = atoi(optarg);
+ break;
+ case '?':
+ if(optopt == 'n' || optopt == 'd' || optopt == 'f')
+ {
+ fprintf(stderr, "Option -%c requires an argument.\n", optopt);
+ }
+ else if(isprint(optopt))
+ {
+ fprintf(stderr, "Unknown option '-%c'.\n", optopt);
+ }
+ else
+ {
+ fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
+ }
+ return -1;
+ break;
+ default:
+ abort();
return -1;//for parasoft
- }
- }
- return 0;
+ }
+ }
+ return 0;
}
/**
@@ -188,31 +188,31 @@ int read_cli(s_parameters *params, int argc, char **argv)
*/
int main(int argc, char **argv)
{
- // Prepare parameters
- s_parameters params;
- init_params(&params);
- if(read_cli(&params, argc, argv) != 0) {
- usage(argv[0]);
- exit(-1);
- }
-
- // Launch the child processes
- do_forks(params);
-
- // Register signal handlers
- if(signal(SIGINT, quit_handler) == SIG_IGN)
- signal(SIGINT, SIG_IGN); // C-c
- if(signal(SIGHUP, quit_handler) == SIG_IGN)
- signal(SIGHUP, SIG_IGN); // Terminal closed
- if(signal(SIGTERM, quit_handler) == SIG_IGN)
- signal(SIGTERM, SIG_IGN); // kill (nice)
-
- printf("Setup done. Listening. My pid: %d\n", getpid());
- fflush(stdout);
-
- int err = wait_for_death();
- cleanup();
- return err;
+ // Prepare parameters
+ s_parameters params;
+ init_params(&params);
+ if(read_cli(&params, argc, argv) != 0) {
+ usage(argv[0]);
+ exit(-1);
+ }
+
+ // Launch the child processes
+ do_forks(params);
+
+ // Register signal handlers
+ if(signal(SIGINT, quit_handler) == SIG_IGN)
+ signal(SIGINT, SIG_IGN); // C-c
+ if(signal(SIGHUP, quit_handler) == SIG_IGN)
+ signal(SIGHUP, SIG_IGN); // Terminal closed
+ if(signal(SIGTERM, quit_handler) == SIG_IGN)
+ signal(SIGTERM, SIG_IGN); // kill (nice)
+
+ printf("Setup done. Listening. My pid: %d\n", getpid());
+ fflush(stdout);
+
+ int err = wait_for_death();
+ cleanup();
+ return err;
}
/**
@@ -223,34 +223,34 @@ void do_forks(s_parameters params)
{
int i;
- // Launch child processes
- for(i=0;i<params.nprocs;i++)
- {
- pid_t pid = fork();
- switch(pid)
- {
- case -1: // An error occured
- if(errno == EAGAIN)
- {
- fprintf(stderr, "Could not allocate resources for child process.\n");
- cleanup();
- abort();
- }
- if(errno == ENOMEM)
- {
- fprintf(stderr, "Could not allocate memory for child process' kernel structure.\n");
- cleanup();
- abort();
- }
- break;
- case 0: // Child process, start threads
- run_threads(params);
- break;
- default: // Parent process, store the childs pid
- pids[pidcount++] = pid;
- break;
- }
- }
+ // Launch child processes
+ for(i=0;i<params.nprocs;i++)
+ {
+ pid_t pid = fork();
+ switch(pid)
+ {
+ case -1: // An error occured
+ if(errno == EAGAIN)
+ {
+ fprintf(stderr, "Could not allocate resources for child process.\n");
+ cleanup();
+ abort();
+ }
+ if(errno == ENOMEM)
+ {
+ fprintf(stderr, "Could not allocate memory for child process' kernel structure.\n");
+ cleanup();
+ abort();
+ }
+ break;
+ case 0: // Child process, start threads
+ run_threads(params);
+ break;
+ default: // Parent process, store the childs pid
+ pids[pidcount++] = pid;
+ break;
+ }
+ }
}
/**
@@ -259,14 +259,14 @@ void do_forks(s_parameters params)
*/
void quit_handler(int signum)
{
- if(in_handler)
- raise(signum);
- in_handler = 1;
+ if(in_handler)
+ raise(signum);
+ in_handler = 1;
- cleanup();
+ cleanup();
- signal(signum, SIG_DFL);
- raise(signum);
+ signal(signum, SIG_DFL);
+ raise(signum);
}
/**
@@ -274,11 +274,11 @@ void quit_handler(int signum)
*/
void cleanup()
{
- unsigned int i;
- for(i=0;i<pidcount;i++)
- {
- kill(pids[i], SIGINT);
- }
+ unsigned int i;
+ for(i=0;i<pidcount;i++)
+ {
+ kill(pids[i], SIGINT);
+ }
}
/**
@@ -297,22 +297,22 @@ time_t mksleep_time(int delay, int fudge)
*/
void do_logging(s_thread_data *data)
{
- DltContext mycontext;
- char ctid[5];
- char ctid_name[256];
+ DltContext mycontext;
+ char ctid[5];
+ char ctid_name[256];
snprintf(ctid,5,"%.2x", rand() & 0x0000ffff);
- snprintf(ctid_name,256, "Child %s in dlt-test-multi-process", ctid);
- DLT_REGISTER_CONTEXT(mycontext, ctid, ctid_name);
-
- int msgs_left = data->params.nmsgs;
- while(msgs_left-- > 0)
- {
- DLT_LOG(mycontext, DLT_LOG_INFO, DLT_STRING(PAYLOAD_DATA));
- usleep(mksleep_time(data->params.delay, data->params.delay_fudge));
- }
- DLT_UNREGISTER_CONTEXT(mycontext);
+ snprintf(ctid_name,256, "Child %s in dlt-test-multi-process", ctid);
+ DLT_REGISTER_CONTEXT(mycontext, ctid, ctid_name);
+
+ int msgs_left = data->params.nmsgs;
+ while(msgs_left-- > 0)
+ {
+ DLT_LOG(mycontext, DLT_LOG_INFO, DLT_STRING(PAYLOAD_DATA));
+ usleep(mksleep_time(data->params.delay, data->params.delay_fudge));
+ }
+ DLT_UNREGISTER_CONTEXT(mycontext);
}
/**
@@ -320,39 +320,39 @@ void do_logging(s_thread_data *data)
*/
void run_threads(s_parameters params)
{
- pthread_t thread[params.nthreads];
- s_thread_data thread_data;
- char apid[5];
- char apid_name[256];
- int i;
+ pthread_t thread[params.nthreads];
+ s_thread_data thread_data;
+ char apid[5];
+ char apid_name[256];
+ int i;
- srand(getpid());
+ srand(getpid());
snprintf(apid,5,"MT%02u", pidcount);
snprintf(apid_name,256, "Apps %s.", apid);
- DLT_REGISTER_APP(apid, apid_name);
+ DLT_REGISTER_APP(apid, apid_name);
- thread_data.params = params;
+ thread_data.params = params;
- for(i=0;i<params.nthreads;i++)
- {
- if(pthread_create(&(thread[i]), NULL, (void *) &do_logging, &thread_data) != 0)
- {
- printf("Error creating thread.\n");
- abort();
- }
- }
+ for(i=0;i<params.nthreads;i++)
+ {
+ if(pthread_create(&(thread[i]), NULL, (void *) &do_logging, &thread_data) != 0)
+ {
+ printf("Error creating thread.\n");
+ abort();
+ }
+ }
- for(i=0;i<params.nthreads;i++)
- {
- pthread_join(thread[i], NULL);
- }
+ for(i=0;i<params.nthreads;i++)
+ {
+ pthread_join(thread[i], NULL);
+ }
- DLT_UNREGISTER_APP();
- // We can exit now
- exit(0);
+ DLT_UNREGISTER_APP();
+ // We can exit now
+ exit(0);
}
/**
@@ -360,27 +360,27 @@ void run_threads(s_parameters params)
*/
int wait_for_death()
{
- int pids_left = pidcount;
- while(pids_left > 0)
- {
- int status;
- pid_t w = waitpid(WAIT_ANY, &status, 0);
- if(status < 0)
- {
- return -1;
- }
- else
- {
- unsigned int i;
- for(i=0;i<pidcount;i++)
- {
- if(pids[i] == w)
- {
- pids_left--;
- break;
- }
- }
- }
- }
- return 0;
+ int pids_left = pidcount;
+ while(pids_left > 0)
+ {
+ int status;
+ pid_t w = waitpid(WAIT_ANY, &status, 0);
+ if(status < 0)
+ {
+ return -1;
+ }
+ else
+ {
+ unsigned int i;
+ for(i=0;i<pidcount;i++)
+ {
+ if(pids[i] == w)
+ {
+ pids_left--;
+ break;
+ }
+ }
+ }
+ }
+ return 0;
}