summaryrefslogtreecommitdiffstats
path: root/compat/Thread.c
blob: 307535e1f59aabf3cb684e22f9b62d7b03057cc7 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*---------------------------------------------------------------
 * Copyright (c) 1999,2000,2001,2002,2003
 * The Board of Trustees of the University of Illinois
 * All Rights Reserved.
 *---------------------------------------------------------------
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software (Iperf) and associated
 * documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 *
 * Redistributions of source code must retain the above
 * copyright notice, this list of conditions and
 * the following disclaimers.
 *
 *
 * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimers in the documentation and/or other materials
 * provided with the distribution.
 *
 *
 * Neither the names of the University of Illinois, NCSA,
 * nor the names of its contributors may be used to endorse
 * or promote products derived from this Software without
 * specific prior written permission.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * ________________________________________________________________
 * National Laboratory for Applied Network Research
 * National Center for Supercomputing Applications
 * University of Illinois at Urbana-Champaign
 * http://www.ncsa.uiuc.edu
 * ________________________________________________________________
 *
 * Thread.c
 * by Kevin Gibbs <kgibbs@nlanr.net>
 *
 * Based on:
 * Thread.cpp
 * by Mark Gates <mgates@nlanr.net>
 * -------------------------------------------------------------------
 * The thread subsystem is responsible for all thread functions. It
 * provides a thread implementation agnostic interface to Iperf. If
 * threads are not available (HAVE_THREAD is undefined), thread_start
 * does not start a new thread but just launches the specified object
 * in the current thread. Everything that defines a thread of
 * execution in Iperf is contained in an thread_Settings structure. To
 * start a thread simply pass one such structure into thread_start.
 * -------------------------------------------------------------------
 * headers
 * uses
 *   <stdlib.h>
 *   <stdio.h>
 *   <assert.h>
 *   <errno.h>
 * Thread.h may include <pthread.h>
 * ------------------------------------------------------------------- */

#include "headers.h"

#include "Thread.h"
#include "Locale.h"
#include "util.h"

#ifdef __cplusplus
extern "C" {
#endif

#if HAVE_THREAD_DEBUG
#include <time.h>
#include <unistd.h>
#if HAVE_GETTID_SYSCALL
#include <sys/syscall.h>
#endif
#include <sys/types.h>
#include <stdarg.h>
#include "Reporter.h"
void reporttype_text(struct ReportHeader *reporthdr, char *rs) {
    switch (reporthdr->type) {
    case DATA_REPORT:
	strncpy(rs,"data", REPORTTXTMAX);
	break;
    case SUM_REPORT:
	strncpy(rs,"sum", REPORTTXTMAX);
	break;
    case SETTINGS_REPORT:
	strncpy(rs,"settings", REPORTTXTMAX);
	break;
    case CONNECTION_REPORT:
	strncpy(rs,"connection", REPORTTXTMAX);
	break;
    case SERVER_RELAY_REPORT:
	strncpy(rs,"server", REPORTTXTMAX);
	break;
    default :
	strncpy(rs,"unknown", REPORTTXTMAX);
    }
    rs[REPORTTXTMAX-1] = '\0';
}

Mutex thread_debug_mutex;
static void __gettimestamp(char *timestr) {
    struct timespec t1;
    clock_gettime(CLOCK_REALTIME, &t1);
    struct tm *t;
    t=localtime(&t1.tv_sec);
    if (t) {
#if WIN32
	strftime(timestr, 200, "%Y-%m-%d %H:%M:%S", t);
#else
        strftime(timestr, 200, "%T", t);
#endif
        // strftime(buf, len, "%F %T", &t);
	snprintf(&timestr[strlen(timestr)], strlen(timestr), ".%09ld", t1.tv_nsec);
        timestr[199]='\0';
    } else {
        *timestr='\0';
    }
}
static int __log(const char *level, const char *format, va_list args) {
    int len;
    char *newformat;
    char timestamp[200];
    char logformat[]="%s(%ld):[%s] %s\n";

    __gettimestamp(timestamp);
  #if HAVE_GETTID_SYSCALL
    unsigned long tid = syscall(SYS_gettid);
  #else
    unsigned long tid = -1;
  #endif
    len = snprintf(NULL, 0, logformat, level, tid, timestamp, format);
    len++;  // Trailing null byte + extra
    newformat = malloc(len);
    len = snprintf(newformat, len, logformat, level, tid, timestamp, format);
    if (len > 0) {
      len = vprintf(newformat, args);
    }
    free(newformat);
    return len;
}

void thread_debug(const char *format, ...) {
    Mutex_Lock(&thread_debug_mutex);
    va_list ap;
    va_start(ap, format);
    __log("THREAD", format, ap);
    va_end(ap);
    fflush(stdout);
    Mutex_Unlock(&thread_debug_mutex);
}
#endif

/* -------------------------------------------------------------------
 * define static variables.
 * ------------------------------------------------------------------- */

// number of currently running threads
int thread_sNum = 0;
// number of currently running traffic threads
int thread_trfc_sNum = 0;
int thread_trfctx_sNum = 0;
int thread_trfcrx_sNum = 0;
// number of non-terminating running threads (ie listener thread)
int nonterminating_num = 0;
// condition to protect updating the above and alerting on
// changes to above
struct Condition thread_sNum_cond;


/* -------------------------------------------------------------------
 * Initialize the thread subsystems variables and set the concurrency
 * level in solaris.
 * ------------------------------------------------------------------- */
void thread_init() {
    Condition_Initialize(&thread_sNum_cond);
#if defined(sun)
    /* Solaris apparently doesn't default to timeslicing threads,
     * as such we force it to play nice. This may not work perfectly
     * when _sending_ multiple _UDP_ streams.
     */
    pthread_setconcurrency (3);
#endif
}

/* -------------------------------------------------------------------
 * Destroy the thread subsystems variables.
 * ------------------------------------------------------------------- */
void thread_destroy() {
    Condition_Destroy(&thread_sNum_cond);
}

/* -------------------------------------------------------------------
 * Start the specified object's thread execution. Increments thread
 * count, spawns new thread, and stores thread ID.
 * ------------------------------------------------------------------- */
void thread_start_all(struct thread_Settings* thread) {
    struct thread_Settings *ithread = thread;
    while(ithread) {
	thread_start(ithread);
	ithread = ithread->runNow;
    }
}

void thread_start(struct thread_Settings* thread) {
    // Make sure this object has not been started already
    if (!thread_equalid(thread->mTID, thread_zeroid())) {
	WARN(1, "thread_start called on running thread");
#if HAVE_THREAD_DEBUG
	thread_debug("Thread_start info %p id=%d ", (void *)thread, (int)thread->mTID);
#endif
    } else {
        // increment thread count
        Condition_Lock(thread_sNum_cond);
        thread_sNum++;
	if ((thread->mThreadMode == kMode_Client) || (thread->mThreadMode == kMode_Server)) {
	    thread_trfc_sNum++;
	}
        Condition_Unlock(thread_sNum_cond);

#if defined(HAVE_POSIX_THREAD)
        // pthreads -- spawn new thread
        if (pthread_create(&thread->mTID, NULL, thread_run_wrapper, thread) != 0) {
            WARN(1, "pthread_create");

            // decrement thread count
            Condition_Lock(thread_sNum_cond);
            thread_sNum--;
	    if (thread->mThreadMode == kMode_Client) {
	        thread_trfc_sNum--;
	        thread_trfctx_sNum--;
	    }
	    if (thread->mThreadMode == kMode_Server) {
	        thread_trfc_sNum--;
	        thread_trfcrx_sNum--;
	    }
            Condition_Unlock(thread_sNum_cond);
        }
#if HAVE_THREAD_DEBUG
	thread_debug("Thread_run_wrapper(%p mode=%x) thread counts tot/trfc=%d/%d (id=%d)", (void *)thread, thread->mThreadMode, thread_sNum, thread_trfc_sNum, (int)thread->mTID);
#endif
#elif defined(HAVE_WIN32_THREAD)
        // Win32 threads -- spawn new thread
        // Win32 has a thread handle in addition to the thread ID
        thread->mHandle = CreateThread(NULL, 0, thread_run_wrapper, thread, 0, &thread->mTID);
        if (thread->mHandle == NULL) {
            WARN(1, "CreateThread");

            // decrement thread count
            Condition_Lock(thread_sNum_cond);
            thread_sNum--;
	    if ((thread->mThreadMode == kMode_Client) || (thread->mThreadMode == kMode_Server)) {
		thread_trfc_sNum--;
	    }
            Condition_Unlock(thread_sNum_cond);
        }
#else
        // single-threaded -- call Run_Wrapper in this thread
        thread_run_wrapper(thread);
#endif
    }
} // end thread_start

/* -------------------------------------------------------------------
 * Stop the specified object's thread execution (if any) immediately.
 * Decrements thread count and resets the thread ID.
 *
 * Note: This does not free any objects and calling it without
 * lots of conideration will likely cause memory leaks. Better to let
 * thread_start's thread_run_wrapper run to completion and not
 * preemptively stop a thread.
 * ------------------------------------------------------------------- */
void thread_stop(struct thread_Settings* thread) {
#ifdef HAVE_THREAD
  #ifdef HAVE_THREAD_DEBUG
  thread_debug("Thread stop invoked %p (%d/%d)", (void *)thread, thread_sNum, thread_trfc_sNum);
  #endif
    // Make sure we have been started
    if (!thread_equalid(thread->mTID, thread_zeroid())) {

        // decrement thread count
        Condition_Lock(thread_sNum_cond);
        thread_sNum--;
	if ((thread->mThreadMode == kMode_Client) || (thread->mThreadMode == kMode_Server)) {
	    thread_trfc_sNum--;
	}
        Condition_Signal(&thread_sNum_cond);
        Condition_Unlock(thread_sNum_cond);

        // use exit()   if called from within this thread
        // use cancel() if called from a different thread
        if (thread_equalid(thread_getid(), thread->mTID)) {

            // Destroy the object
            Settings_Destroy(thread);

            // Exit
#if   defined(HAVE_POSIX_THREAD)
            pthread_exit(NULL);
#else // Win32
            CloseHandle(thread->mHandle);
            ExitThread(0);
#endif
        } else {

            // Cancel
#if   defined(HAVE_POSIX_THREAD)
            // Cray J90 doesn't have pthread_cancel; Iperf works okay without
#ifdef HAVE_PTHREAD_CANCEL
            pthread_cancel(thread->mTID);
#endif
#else // Win32
            // this is a somewhat dangerous function; it's not
            // suggested to Stop() threads a lot.
            TerminateThread(thread->mHandle, 0);
#endif

            // Destroy the object only after killing the thread
            Settings_Destroy(thread);
        }
    }
#endif
} // end Stop

/* -------------------------------------------------------------------
 * This function is the entry point for new threads created in
 * thread_start.
 * ------------------------------------------------------------------- */
#if   defined(HAVE_WIN32_THREAD)
DWORD WINAPI
#else
void*
#endif
thread_run_wrapper(void* paramPtr) {
    bool signal_on_exit = false;
    struct thread_Settings* thread = (struct thread_Settings*) paramPtr;

    // which type of object are we
    switch (thread->mThreadMode) {
        case kMode_Server:
            {
		signal_on_exit = true;
                /* Spawn a Server thread with these settings */
                server_spawn(thread);
            } break;
        case kMode_Client:
            {
		signal_on_exit = true;
                /* Spawn a Client thread with these settings */
                client_spawn(thread);
            } break;
        case kMode_Reporter:
        case kMode_ReporterClient:
            {
                /* Spawn a Reporter thread with these settings */
                reporter_spawn(thread);
            } break;
        case kMode_Listener:
            {
                // Increment the non-terminating thread count
                thread_register_nonterm();
                /* Spawn a Listener thread with these settings */
                listener_spawn(thread);
                // Decrement the non-terminating thread count
                thread_unregister_nonterm();
            } break;
        default:
            {
                FAIL(1, "Unknown Thread Type!\n", thread);
            } break;
    }

#ifdef HAVE_POSIX_THREAD
    // detach Thread. If someone already joined it will not do anything
    // If none has then it will free resources upon return from this
    // function (Run_Wrapper)
    pthread_detach(thread->mTID);
#endif

    // decrement thread count and send condition signal
    Condition_Lock(thread_sNum_cond);
    thread_sNum--;
    if ((thread->mThreadMode == kMode_Client) || (thread->mThreadMode == kMode_Server)) {
       thread_trfc_sNum--;
    }
    Condition_Signal(&thread_sNum_cond);
    Condition_Unlock(thread_sNum_cond);

    // Check if we need to start up a thread after executing this one
    if (thread->runNext != NULL) {
        thread_start(thread->runNext);
    }
    // Destroy this thread object
    Settings_Destroy(thread);
    // signal the reporter thread now that thread state has changed
    if (signal_on_exit) {
	Condition_Signal(&ReportCond);
#if HAVE_THREAD_DEBUG
	thread_debug("Signal sent to reporter thread");
#endif
    }
    return 0;
} // end run_wrapper

/* -------------------------------------------------------------------
 * Wait for all thread object's execution to complete. Depends on the
 * thread count being accurate and the threads sending a condition
 * signal when they terminate.
 * ------------------------------------------------------------------- */
void thread_joinall(void) {
    Condition_Lock(thread_sNum_cond);
    while (thread_sNum > 0) {
        Condition_Wait(&thread_sNum_cond);
    }
    Condition_Unlock(thread_sNum_cond);
} // end Joinall


/* -------------------------------------------------------------------
 * Compare the thread ID's (inLeft == inRight); return true if they
 * are equal. On some OS's nthread_t is a struct so == will not work.
 * TODO use pthread_equal. Any Win32 equivalent??
 * ------------------------------------------------------------------- */
int thread_equalid(nthread_t inLeft, nthread_t inRight) {
    return(memcmp(&inLeft, &inRight, sizeof(inLeft)) == 0);
}

/* -------------------------------------------------------------------
 * Return a zero'd out thread ID. On some OS's nthread_t is a struct
 * so == 0 will not work.
 * [static]
 * ------------------------------------------------------------------- */
nthread_t thread_zeroid(void) {
    nthread_t a;
    memset(&a, 0, sizeof(a));
    return a;
}

/* -------------------------------------------------------------------
 * set a thread to be ignorable, so joinall won't wait on it
 * this simply decrements the thread count that joinall uses.
 * This is utilized by the reporter thread which knows when it
 * is ok to quit (aka no pending reports).
 * ------------------------------------------------------------------- */
void thread_setignore() {
    Condition_Lock(thread_sNum_cond);
    thread_sNum--;
    Condition_Signal(&thread_sNum_cond);
    Condition_Unlock(thread_sNum_cond);
}

/* -------------------------------------------------------------------
 * unset a thread from being ignorable, so joinall will wait on it
 * this simply increments the thread count that joinall uses.
 * This is utilized by the reporter thread which knows when it
 * is ok to quit (aka no pending reports).
 * ------------------------------------------------------------------- */
void thread_unsetignore(void) {
    Condition_Lock(thread_sNum_cond);
    thread_sNum++;
    Condition_Signal(&thread_sNum_cond);
    Condition_Unlock(thread_sNum_cond);
}

/* -------------------------------------------------------------------
 * set a thread to be non-terminating, so if you cancel through
 * Ctrl-C they can be ignored by the joinall.
 * ------------------------------------------------------------------- */
void thread_register_nonterm(void) {
    Condition_Lock(thread_sNum_cond);
    nonterminating_num++;
    Condition_Unlock(thread_sNum_cond);
}

/* -------------------------------------------------------------------
 * unset a thread from being non-terminating, so if you cancel through
 * Ctrl-C they can be ignored by the joinall.
 * ------------------------------------------------------------------- */
void thread_unregister_nonterm(void) {
    Condition_Lock(thread_sNum_cond);
    if (nonterminating_num == 0) {
        // nonterminating has been released with release_nonterm
        // Add back to the threads to wait on
        thread_sNum++;
    } else {
        nonterminating_num--;
    }
    Condition_Unlock(thread_sNum_cond);
}

/* -------------------------------------------------------------------
 * this function releases all non-terminating threads from the list
 * of active threads, so that when all terminating threads quit
 * the joinall will complete. This is called on a Ctrl-C input. It is
 * also used by the -P usage on the server side
 * ------------------------------------------------------------------- */
int thread_release_nonterm(int interrupt) {
    Condition_Lock(thread_sNum_cond);
    thread_sNum -= nonterminating_num;
    if (thread_sNum > 1 && nonterminating_num > 0 && interrupt != 0) {
        fprintf(stderr, "%s", wait_server_threads);
    }
    nonterminating_num = 0;
    Condition_Signal(&thread_sNum_cond);
    Condition_Unlock(thread_sNum_cond);
    return thread_sNum;
}

/* -------------------------------------------------------------------
 * Return the number of threads currently running (doesn't include
 * active threads that have called setdaemon (aka reporter thread))
 * ------------------------------------------------------------------- */
int thread_numuserthreads(void) {
    return thread_sNum;
}

/* -------------------------------------------------------------------
 * Return the number of taffic threads currently running
 * ------------------------------------------------------------------- */
int thread_numtrafficthreads(void) {
    return thread_trfc_sNum;
}

/* -------------------------------------------------------------------
 * Support for realtime scheduling of threads
 * ------------------------------------------------------------------- */
#if HAVE_SCHED_SETSCHEDULER
#include <sched.h>
#endif
#ifdef HAVE_MLOCKALL
#include <sys/mman.h>
#endif
void thread_setscheduler(struct thread_Settings *thread) {
#if HAVE_SCHED_SETSCHEDULER
    if (isRealtime(thread)) {
	struct sched_param sp;
	sp.sched_priority = sched_get_priority_max(SCHED_RR);
	// SCHED_OTHER, SCHED_FIFO, SCHED_RR
	if (sched_setscheduler(0, SCHED_RR, &sp) < 0)  {
	    perror("Client set scheduler");
#ifdef HAVE_MLOCKALL
	} else if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
	    // lock the threads memory
	    perror ("mlockall");
#endif // MLOCK
	}
    }
#endif // SCHED
}

/*
 * -------------------------------------------------------------------
 * Allow another thread to execute. If no other threads are runable this
 * is not guarenteed to actually rest.
 * ------------------------------------------------------------------- */
void thread_rest (void) {
#if defined(HAVE_THREAD)
#if defined(HAVE_POSIX_THREAD)
  #if HAVE_SCHED_YIELD
     sched_yield();
  #endif
#else // Win32
    SwitchToThread();
#endif
#endif
}

#ifdef __cplusplus
} /* end extern "C" */
#endif