1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* SNOOPY COMMAND LOGGER
*
* File: snoopy-test-datasource.c
*
* Copyright (c) 2015 Bostjan Skufca <bostjan@a2o.si>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Includes order: from local to global
*/
#include "action-common.h"
#include "snoopy.h"
#include "entrypoint/test-cli.h"
#include "configuration.h"
#include "datasourceregistry.h"
#include "tsrm.h"
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define THREAD_COUNT_MAX 10000
#define DS_MESSAGE_BUF_SIZE 1024
/*
* Thread data
*/
typedef struct {
int seqNr;
} tData_t;
/*
* We do not use separate .h file here
*/
void * snoopyTestCli_action_stress_threads_threadMain (void *arg);
int snoopyTestCli_action_stress_threads_randomNumberInclusive (int idMin, int idMax);
/*
* Global variables
*/
pthread_t snoopyTestCli_action_stress_threads_tRepo[THREAD_COUNT_MAX];
pthread_mutex_t threadCountMutex = PTHREAD_MUTEX_INITIALIZER;
int threadCountCreated = 0; // Created threads, as seen by each thread
int threadCountAliveNow = 0; // Number of threads currently alive
int threadCountAliveMax = 0; // Maximum number of threads alive at any one time
int verbose;
void snoopyTestCli_action_stress_threads_showHelp ()
{
char * helpContent =
"Snoopy TEST SUITE CLI utility :: Action `stress` :: Subsystem `threads`\n"
"\n"
"Usage:\n"
" snoopy-test stress threads THREAD_COUNT [-v]\n"
"\n"
"Description:\n"
" Stresses Snoopy's threading implementation by creating and destroying THREAD_COUNT\n"
" threads as fast as possible.\n"
"\n"
"Arguments:\n"
" THREAD_COUNT Number of threads to create and destroy\n"
" -v Verbose debugging output\n"
"\n"
"Output:\n"
" Various threading-related messages and some statistics at the end.\n"
"\n";
printf("%s", helpContent);
}
int snoopyTestCli_action_stress_threads (int argc, char ** argv)
{
int threadsToCreate;
int maxConcurrentThreadsObserved = 0;
int retVal = 0;
/* Check arguments and parse them */
if (argc < 1) {
snoopyTestCli_action_stress_threads_showHelp();
fatalError("Missing argument: number of threads to create");
}
threadsToCreate = atoi(argv[0]);
if ((threadsToCreate < 1) || (threadsToCreate > THREAD_COUNT_MAX)) {
snoopyTestCli_action_stress_threads_showHelp();
fatalError("Invalid number of threads to create (min 1, max THREAD_COUNT_MAX)");
}
if ((argc >= 2) && (0 == strcmp(argv[1], "-v"))) {
verbose = 1;
} else {
verbose = 0;
}
// Disable config file parsing
snoopy_configuration_preinit_disableConfigFileParsing();
// Do not call snoopy_init() in the main thread here, as we're letting the non-main threads
// figure it out for themselves.
// Create threads and run the function in them
printf("M: Starting threads... ");
for (int i=0 ; i<threadsToCreate ; i++) {
tData_t *tArgs = malloc(sizeof *tArgs);
tArgs->seqNr = i;
if (verbose) printf(" M: Starting thread #%d:\n", i+1);
retVal = pthread_create(&snoopyTestCli_action_stress_threads_tRepo[i], NULL, &snoopyTestCli_action_stress_threads_threadMain, tArgs);
}
printf("done.\n");
printf("M: Threads alive right after thread creation was completed: %d\n", threadCountAliveNow);
// Sleep a bit, and get thread count, should be max
if (verbose) {
struct timespec ts_sleep;
ts_sleep.tv_sec = 0;
ts_sleep.tv_nsec = 200000000;
nanosleep(&ts_sleep, NULL);
maxConcurrentThreadsObserved = snoopy_tsrm_get_threadCount();
printf("M: Threads after first sleep: %d\n", maxConcurrentThreadsObserved);
// Sleep a bit more for all threads to finish
nanosleep(&ts_sleep, NULL);
printf("M: Threads after all threads are supposedly finished: %d\n", snoopy_tsrm_get_threadCount());
}
// Wait for threads to finish
printf("M: Waiting for all threads to finish... ");
for (int i=0 ; i<threadsToCreate ; i++) {
pthread_join(snoopyTestCli_action_stress_threads_tRepo[i], NULL);
if (verbose) {
printf("M: Thread joined: #%d\n", i+1);
printf(" M: Thread #%d joined.\n", i+1);
}
}
printf("done.\n");
printf("M: Number of threads created: %d\n", threadCountCreated);
printf("M: Max threads alive simultaneously: %d\n", threadCountAliveMax);
// This should return 1 thread still active
if (verbose) printf("M: Threads after all threads, except main, have finished: %d\n", snoopy_tsrm_get_threadCount());
// Why did we have snoopy_cleanup() commented out here?
if (verbose) printf("M: Threads after all threads have finished: %d\n", snoopy_tsrm_get_threadCount());
if (verbose) printf("SUCCESS! Expected Snoopy threads count reached: %d\n", maxConcurrentThreadsObserved);
return retVal;
}
/*
* threadMain()
*
* Description:
* Main thread routine
*
* Params:
* errorMsg Error message to display to user
*
* Return:
* int Exit status to return to calling process
*/
void* snoopyTestCli_action_stress_threads_threadMain (void *args)
{
tData_t *tArgs = args;
int seqNr = tArgs->seqNr;
int seqNrPub = seqNr + 1;
int dsCount;
int dsId;
char *dsName;
const char *dsArg = "";
char dsResult[DS_MESSAGE_BUF_SIZE];
int retVal;
// Initialize thread
pthread_mutex_lock(&threadCountMutex);
threadCountCreated++;
threadCountAliveNow++;
if (threadCountAliveNow > threadCountAliveMax) {
threadCountAliveMax = threadCountAliveNow;
}
pthread_mutex_unlock(&threadCountMutex);
// Hello the user
if (verbose) printf(" t%d %llu : Hello from thread #%d\n", seqNrPub, (unsigned long long)pthread_self(), seqNrPub);
// Initialize Snoopy
if (verbose) printf(" t%d %llu : Threads before snoopy_init(): %d\n", seqNrPub, (unsigned long long)pthread_self(), snoopy_tsrm_get_threadCount());
snoopy_entrypoint_test_cli_threads_init();
if (verbose) printf(" t%d %llu : Threads after snoopy_init(): %d\n", seqNrPub, (unsigned long long)pthread_self(), snoopy_tsrm_get_threadCount());
// Run a random snoopy datasource
dsCount = snoopy_datasourceregistry_getCount();
dsId = snoopyTestCli_action_stress_threads_randomNumberInclusive(0, dsCount-1);
dsName = snoopy_datasourceregistry_getName(dsId);
retVal = snoopy_datasourceregistry_callById(dsId, dsResult, DS_MESSAGE_BUF_SIZE, dsArg);
if (0 > retVal) {
printf(" t%d %llu : Datasource %s returned negative result: %d\n", seqNrPub, (unsigned long long)pthread_self(), dsName, retVal);
} else {
printf(" t%d %llu : DS result: %30s = %s\n", seqNrPub, (unsigned long long)pthread_self(), dsName, dsResult);
}
// Retest at thread end
if (verbose) printf(" t%d %llu : Threads before snoopy_cleanup(): %d\n", seqNrPub, (unsigned long long)pthread_self(), snoopy_tsrm_get_threadCount());
snoopy_entrypoint_test_cli_exit();
if (verbose) printf(" t%d %llu : Threads after snoopy_cleanup(): %d\n", seqNrPub, (unsigned long long)pthread_self(), snoopy_tsrm_get_threadCount());
// Goodbye to user
if (verbose) printf(" t%d %llu : Thread exiting: #%d\n", seqNrPub, (unsigned long long)pthread_self(), seqNrPub);
// Cleanup thread
pthread_mutex_lock(&threadCountMutex);
threadCountAliveNow--;
pthread_mutex_unlock(&threadCountMutex);
free (tArgs);
return NULL;
}
/*
* randomNumberInclusive()
*
* Description:
* Return random number between idMin and idMax inclusive
*
* Params:
*
* Return:
* int Random number
*/
int snoopyTestCli_action_stress_threads_randomNumberInclusive (int nMin, int nMax)
{
int randomNrRaw = 0;
int randomNr;
ssize_t bytesRead = 0;
unsigned char buffer[sizeof(randomNrRaw)];
// Read the random content
int fd = open("/dev/urandom", O_RDONLY);
if (-1 == fd) {
printf("ERROR: Unable to open /dev/urandom.\n");
return -1; // Yeah, not the best error handling
}
bytesRead = read(fd, buffer, sizeof(randomNrRaw));
close(fd);
if (bytesRead != sizeof(randomNrRaw)) {
// SonarCloud may complain about redundant casts here, but removing these
// "redundant" casts causes builds to fail on non-64bit platforms.
printf("ERROR: Unable to read %lu bytes from /dev/urandom, only got %li bytes.\n", (long unsigned) sizeof(randomNrRaw), (long int) bytesRead);
return -1;
}
// Convert the read bytes to random positive number
for (unsigned int i=0 ; i<sizeof(randomNrRaw) ; ++i) {
randomNrRaw += (int) buffer[i] << i*8;
}
if (randomNrRaw < 0) {
randomNrRaw = -randomNrRaw;
}
// Generate
randomNr = ( randomNrRaw % (nMax - nMin + 1) ) + nMin;
return randomNr;
}
|