extern "C" magma_int_t magma_zbulge_back_m(magma_int_t nrgpu, magma_uplo_t uplo, magma_int_t n, magma_int_t nb, magma_int_t ne, magma_int_t Vblksiz, magmaDoubleComplex *Z, magma_int_t ldz, magmaDoubleComplex *V, magma_int_t ldv, magmaDoubleComplex *TAU, magmaDoubleComplex *T, magma_int_t ldt, magma_int_t* info) { magma_int_t threads = magma_get_parallel_numthreads(); magma_int_t mklth = magma_get_lapack_numthreads(); magma_set_lapack_numthreads(1); real_Double_t timeaplQ2=0.0; double f= 1.; magma_int_t n_gpu = ne; //#if defined(PRECISION_s) || defined(PRECISION_d) // double gpu_cpu_perf = 32; //gpu over cpu performance //#else // double gpu_cpu_perf = 32; // gpu over cpu performance //#endif double perf_temp= .85; double perf_temp2= perf_temp; for (magma_int_t itmp=1; itmp < nrgpu; ++itmp) perf_temp2 *= perf_temp; magma_int_t gpu_cpu_perf = magma_get_zbulge_gcperf(); if (threads > 1) { f = 1. / (1. + (double)(threads-1)/ ((double)gpu_cpu_perf*(1.-perf_temp2)/(1.-perf_temp))); n_gpu = (magma_int_t)(f*ne); } /**************************************************** * apply V2 from left to the eigenvectors Z. dZ = (I-V2*T2*V2')*Z * **************************************************/ timeaplQ2 = magma_wtime(); /*============================ * use GPU+CPU's *==========================*/ //n_gpu = ne; if (n_gpu < ne) { // define the size of Q to be done on CPU's and the size on GPU's // note that GPU use Q(1:N_GPU) and CPU use Q(N_GPU+1:N) #ifdef ENABLE_DEBUG printf("---> calling GPU + CPU(if N_CPU > 0) to apply V2 to Z with NE %d N_GPU %d N_CPU %d\n",ne, n_gpu, ne-n_gpu); #endif magma_zapplyQ_m_data data_applyQ(nrgpu, threads, n, ne, n_gpu, nb, Vblksiz, Z, ldz, V, ldv, TAU, T, ldt); magma_zapplyQ_m_id_data* arg; magma_malloc_cpu((void**) &arg, threads*sizeof(magma_zapplyQ_m_id_data)); pthread_t* thread_id; magma_malloc_cpu((void**) &thread_id, threads*sizeof(pthread_t)); pthread_attr_t thread_attr; // =============================== // relaunch thread to apply Q // =============================== // Set one thread per core pthread_attr_init(&thread_attr); pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM); pthread_setconcurrency(threads); // Launch threads for (magma_int_t thread = 1; thread < threads; thread++) { arg[thread] = magma_zapplyQ_m_id_data(thread, &data_applyQ); pthread_create(&thread_id[thread], &thread_attr, magma_zapplyQ_m_parallel_section, &arg[thread]); } arg[0] = magma_zapplyQ_m_id_data(0, &data_applyQ); magma_zapplyQ_m_parallel_section(&arg[0]); // Wait for completion for (magma_int_t thread = 1; thread < threads; thread++) { void *exitcodep; pthread_join(thread_id[thread], &exitcodep); } magma_free_cpu(thread_id); magma_free_cpu(arg); /*============================ * use only GPU *==========================*/ } else { magma_zbulge_applyQ_v2_m(nrgpu, MagmaLeft, ne, n, nb, Vblksiz, Z, ldz, V, ldv, T, ldt, info); magma_device_sync(); } timeaplQ2 = magma_wtime()-timeaplQ2; magma_set_lapack_numthreads(mklth); return MAGMA_SUCCESS; }
int main(int argc, char *argv[]) { int i, j; int fd; char *fdata; struct stat finfo; char * fname; pthread_t *pid; pthread_attr_t attr; thread_arg_t *arg; int red[256]; int green[256]; int blue[256]; int num_procs; int num_per_thread; int excess; // Make sure a filename is specified if (argv[1] == NULL) { printf("USAGE: %s <bitmap filename>\n", argv[0]); exit(1); } fname = argv[1]; // Read in the file CHECK_ERROR((fd = open(fname, O_RDONLY)) < 0); // Get the file info (for file length) CHECK_ERROR(fstat(fd, &finfo) < 0); // Memory map the file CHECK_ERROR((fdata = mmap(0, finfo.st_size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) == NULL); if ((fdata[0] != 'B') || (fdata[1] != 'M')) { printf("File is not a valid bitmap file. Exiting\n"); exit(1); } test_endianess(); // will set the variable "swap" unsigned short *bitsperpixel = (unsigned short *)(&(fdata[BITS_PER_PIXEL_POS])); if (swap) { swap_bytes((char *)(bitsperpixel), sizeof(*bitsperpixel)); } if (*bitsperpixel != 24) { // ensure its 3 bytes per pixel printf("Error: Invalid bitmap format - "); printf("This application only accepts 24-bit pictures. Exiting\n"); exit(1); } unsigned short *data_pos = (unsigned short *)(&(fdata[IMG_DATA_OFFSET_POS])); if (swap) { swap_bytes((char *)(data_pos), sizeof(*data_pos)); } long imgdata_bytes = (long)finfo.st_size - (long)(*(data_pos)); long num_pixels = ((long)finfo.st_size - (long)(*(data_pos))) / 3; printf("This file has %ld bytes of image data, %ld pixels\n", imgdata_bytes, num_pixels); printf("Starting pthreads histogram\n"); memset(&(red[0]), 0, sizeof(int) * 256); memset(&(green[0]), 0, sizeof(int) * 256); memset(&(blue[0]), 0, sizeof(int) * 256); /* Set a global scope */ pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); num_procs = atoi(argv[2]); //CHECK_ERROR((num_procs = sysconf(_SC_NPROCESSORS_ONLN)) <= 0); num_per_thread = num_pixels / num_procs; excess = num_pixels % num_procs; CHECK_ERROR( (pid = (pthread_t *)malloc(sizeof(pthread_t) * num_procs)) == NULL); CHECK_ERROR( (arg = (thread_arg_t *)calloc(sizeof(thread_arg_t), num_procs)) == NULL); /* Assign portions of the image to each thread */ long curr_pos = (long)(*data_pos); for (i = 0; i < num_procs; i++) { arg[i].data = (unsigned char *)fdata; arg[i].data_pos = curr_pos; arg[i].data_len = num_per_thread; if (excess > 0) { arg[i].data_len++; excess--; } arg[i].data_len *= 3; // 3 bytes per pixel curr_pos += arg[i].data_len; pthread_create(&(pid[i]), &attr, calc_hist, (void *)(&(arg[i]))); } for (i = 0; i < num_procs; i++) { pthread_join(pid[i] , NULL); } for (i = 0; i < num_procs; i++) { for (j = 0; j < 256; j++) { red[j] += arg[i].red[j]; green[j] += arg[i].green[j]; blue[j] += arg[i].blue[j]; } } dprintf("\n\nBlue\n"); dprintf("----------\n\n"); for (i = 0; i < 256; i++) { dprintf("%d - %d\n", i, blue[i]); } dprintf("\n\nGreen\n"); dprintf("----------\n\n"); for (i = 0; i < 256; i++) { dprintf("%d - %d\n", i, green[i]); } dprintf("\n\nRed\n"); dprintf("----------\n\n"); for (i = 0; i < 256; i++) { dprintf("%d - %d\n", i, red[i]); } CHECK_ERROR(munmap(fdata, finfo.st_size + 1) < 0); CHECK_ERROR(close(fd) < 0); /*free(pid); for(i = 0; i < num_procs; i++) { free(arg[i].red); free(arg[i].green); free(arg[i].blue); } free(arg);*/ pthread_attr_destroy(&attr); return 0; }
/* read command line, initialize, and create threads */ int main(int argc, char *argv[]) { int i, j; long l; /* use long in case of a 64-bit system */ pthread_attr_t attr; pthread_t workerid[MAXWORKERS]; /* set global thread attributes */ pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* initialize mutex and condition variable */ pthread_mutex_init(&sum_mutex, NULL); pthread_mutex_init(&max_mutex, NULL); pthread_mutex_init(&min_mutex, NULL); /* read command line args if any */ size = (argc > 1)? atoi(argv[1]) : MAXSIZE; numWorkers = (argc > 2)? atoi(argv[2]) : MAXWORKERS; if (size > MAXSIZE) size = MAXSIZE; if (numWorkers > MAXWORKERS) numWorkers = MAXWORKERS; stripSize = size/numWorkers; srand(7); /* initialize the matrix */ for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { matrix[i][j] = rand()%99; } } /* print the matrix */ #ifdef DEBUG for (i = 0; i < size; i++) { printf("[ "); for (j = 0; j < size; j++) { printf(" %d", matrix[i][j]); } printf(" ]\n"); } #endif /* Init sum, min, max */ sum = 0; min.value = matrix[0][0]; max.value = matrix[0][0]; min.x = min.y = max.x = max.y = 0; /* do the parallel work: create the workers */ start_time = read_timer(); for (l = 0; l < numWorkers; l++) pthread_create(&workerid[l], &attr, Worker, (void *) l); /* Wait for all threads */ for (i = 0; i < numWorkers; i++) pthread_join(workerid[i], NULL); /* get end time */ end_time = read_timer(); /* print results */ printf("The total is %ld, min is %d at [%d,%d], max is %d at [%d,%d]\n", sum, min.value, min.y, min.x, max.value, max.y, max.x); printf("The execution time is %g sec\n", end_time - start_time); pthread_exit(NULL); }
/*********************************************************************** * RtlCreateUserThread (NTDLL.@) */ NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr, BOOLEAN suspended, PVOID stack_addr, SIZE_T stack_reserve, SIZE_T stack_commit, PRTL_THREAD_START_ROUTINE start, void *param, HANDLE *handle_ptr, CLIENT_ID *id ) { sigset_t sigset; pthread_t pthread_id; pthread_attr_t attr; struct ntdll_thread_data *thread_data; struct startup_info *info; HANDLE handle = 0, actctx = 0; TEB *teb = NULL; DWORD tid = 0; int request_pipe[2]; NTSTATUS status; if (process != NtCurrentProcess()) { apc_call_t call; apc_result_t result; memset( &call, 0, sizeof(call) ); call.create_thread.type = APC_CREATE_THREAD; call.create_thread.func = wine_server_client_ptr( start ); call.create_thread.arg = wine_server_client_ptr( param ); call.create_thread.reserve = stack_reserve; call.create_thread.commit = stack_commit; call.create_thread.suspend = suspended; status = server_queue_process_apc( process, &call, &result ); if (status != STATUS_SUCCESS) return status; if (result.create_thread.status == STATUS_SUCCESS) { if (id) id->UniqueThread = ULongToHandle(result.create_thread.tid); if (handle_ptr) *handle_ptr = wine_server_ptr_handle( result.create_thread.handle ); else NtClose( wine_server_ptr_handle( result.create_thread.handle )); } return result.create_thread.status; } if (server_pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES; wine_server_send_fd( request_pipe[0] ); SERVER_START_REQ( new_thread ) { req->access = THREAD_ALL_ACCESS; req->attributes = 0; /* FIXME */ req->suspend = suspended; req->request_fd = request_pipe[0]; if (!(status = wine_server_call( req ))) { handle = wine_server_ptr_handle( reply->handle ); tid = reply->tid; } close( request_pipe[0] ); } SERVER_END_REQ; if (status) { close( request_pipe[1] ); return status; } pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset ); if ((status = signal_alloc_thread( &teb ))) goto error; teb->Peb = NtCurrentTeb()->Peb; teb->ClientId.UniqueProcess = ULongToHandle(GetCurrentProcessId()); teb->ClientId.UniqueThread = ULongToHandle(tid); teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer; teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer); /* create default activation context frame for new thread */ RtlGetActiveActivationContext(&actctx); if (actctx) { RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame; frame = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*frame)); frame->Previous = NULL; frame->ActivationContext = actctx; frame->Flags = 0; teb->ActivationContextStack.ActiveFrame = frame; } info = (struct startup_info *)(teb + 1); info->teb = teb; info->entry_point = start; info->entry_arg = param; thread_data = (struct ntdll_thread_data *)teb->SpareBytes1; thread_data->request_fd = request_pipe[1]; thread_data->reply_fd = -1; thread_data->wait_fd[0] = -1; thread_data->wait_fd[1] = -1; if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit ))) goto error; pthread_attr_init( &attr ); pthread_attr_setstack( &attr, teb->DeallocationStack, (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack ); pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread */ interlocked_xchg_add( &nb_threads, 1 ); if (pthread_create( &pthread_id, &attr, (void * (*)(void *))start_thread, info )) { interlocked_xchg_add( &nb_threads, -1 ); pthread_attr_destroy( &attr ); status = STATUS_NO_MEMORY; goto error; } pthread_attr_destroy( &attr ); pthread_sigmask( SIG_SETMASK, &sigset, NULL ); if (id) id->UniqueThread = ULongToHandle(tid); if (handle_ptr) *handle_ptr = handle; else NtClose( handle ); return STATUS_SUCCESS; error: if (teb) signal_free_thread( teb ); if (handle) NtClose( handle ); pthread_sigmask( SIG_SETMASK, &sigset, NULL ); close( request_pipe[1] ); return status; }
void Thread::start(int prio, void* ptr) { // Changed by Tim. p3.3.17 userPtr = ptr; pthread_attr_t* attributes = 0; _realTimePriority = prio; /* attributes = (pthread_attr_t*) malloc(sizeof(pthread_attr_t)); pthread_attr_init(attributes); */ // pthread_mutexattr_t mutexattr; // pthread_mutexattr_init(&mutexattr); // pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_TIMED_NP); // pthread_mutex_init(&lock, &mutexattr); // pthread_cond_init(&ready, 0); // pthread_mutex_lock(&lock); if (_realTimePriority) { attributes = (pthread_attr_t*) malloc(sizeof (pthread_attr_t)); pthread_attr_init(attributes); if (pthread_attr_setschedpolicy(attributes, SCHED_FIFO)) { printf("cannot set FIFO scheduling class for RT thread\n"); } if (pthread_attr_setscope(attributes, PTHREAD_SCOPE_SYSTEM)) { printf("Cannot set scheduling scope for RT thread\n"); } if (pthread_attr_setinheritsched(attributes, PTHREAD_EXPLICIT_SCHED)) { printf("Cannot set setinheritsched for RT thread\n"); } struct sched_param rt_param; memset(&rt_param, 0, sizeof (rt_param)); rt_param.sched_priority = _realTimePriority; if (pthread_attr_setschedparam(attributes, &rt_param)) { printf("Cannot set scheduling priority %d for RT thread (%s)\n", _realTimePriority, strerror(errno)); } } /* if (pthread_create(&thread, attributes, ::loop, this)) perror("creating thread failed:"); // else // { // pthread_cond_wait(&ready, &lock); // } // pthread_mutex_unlock(&lock); */ int rv; if ((rv = pthread_create(&thread, attributes, ::loop, this))) { fprintf(stderr, "creating thread <%s> failed: %s\n", _name, strerror(rv)); thread = 0; } //undoSetuid(); }
int TclpThreadCreate( Tcl_ThreadId *idPtr, /* Return, the ID of the thread */ Tcl_ThreadCreateProc *proc, /* Main() function of the thread */ ClientData clientData, /* The one argument to Main() */ int stackSize, /* Size of stack for the new thread */ int flags) /* Flags controlling behaviour of the new * thread. */ { #ifdef TCL_THREADS pthread_attr_t attr; pthread_t theThread; int result; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE if (stackSize != TCL_THREAD_STACK_DEFAULT) { pthread_attr_setstacksize(&attr, (size_t) stackSize); #ifdef TCL_THREAD_STACK_MIN } else { /* * Certain systems define a thread stack size that by default is too * small for many operations. The user has the option of defining * TCL_THREAD_STACK_MIN to a value large enough to work for their * needs. This would look like (for 128K min stack): * make MEM_DEBUG_FLAGS=-DTCL_THREAD_STACK_MIN=131072L * * This solution is not optimal, as we should allow the user to * specify a size at runtime, but we don't want to slow this function * down, and that would still leave the main thread at the default. */ size_t size; result = pthread_attr_getstacksize(&attr, &size); if (!result && (size < TCL_THREAD_STACK_MIN)) { pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN); } #endif /* TCL_THREAD_STACK_MIN */ } #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */ if (! (flags & TCL_THREAD_JOINABLE)) { pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); } if (pthread_create(&theThread, &attr, (void * (*)(void *))proc, (void *)clientData) && pthread_create(&theThread, NULL, (void * (*)(void *))proc, (void *)clientData)) { result = TCL_ERROR; } else { *idPtr = (Tcl_ThreadId)theThread; result = TCL_OK; } pthread_attr_destroy(&attr); return result; #else return TCL_ERROR; #endif /* TCL_THREADS */ }
int main(int argc, char *argv[]) { int i, j; struct rlimit rlb; char *arg; pthread_t tid; pthread_attr_t pab; int yu_tmp_len; int depth = 0; argv0 = argv[0]; //setlocale(LC_CTYPE, ""); getrlimit(RLIMIT_NOFILE, &rlb); rlb.rlim_cur = rlb.rlim_max; setrlimit(RLIMIT_NOFILE, &rlb); signal(SIGPIPE, SIG_IGN); nworkers = 2; pthread_mutex_init(&print_lock, NULL); pthread_mutex_init(&aworker_lock, NULL); pthread_mutex_init(&matches_lock, NULL); pthread_cond_init(&aworker_cv, NULL); for (i = 1; i < argc && argv[i][0] == '-'; i++) for (j = 1; j > 0 && argv[i][j]; ++j) switch (argv[i][j]) { case '-': ++i; goto EndOptions; case 'V': print_version(stdout); break; case 'd': ++debug; break; case 'i': ignore_case = 1; break; case 'v': ++verbose; break; case 'h': usage(stdout); exit(0); case 'l': ++line_f; break; case 'L': if (argv[i][2]) arg = argv[i]+2; else arg = argv[++i]; if (!arg || sscanf(arg, "%u", &maxlen) != 1){ fprintf(stderr, "%s: Invalid length specification: %s\n", argv[0], arg ? arg : "<null>"); exit(1); } j = -2; break; case 'n': if (argv[i][2]) arg = argv[i]+2; else arg = argv[++i]; if (!arg || sscanf(arg, "%u", &nworkers) != 1){ fprintf(stderr,"%s: Invalid workers specification: %s\n", argv[0], arg ? arg : "<null>"); exit(1); } j = -2; break; default: fprintf(stderr, "%s: unknown command line switch: -%c\n", argv[0], argv[i][j]); exit(1); } EndOptions: yu_tmp_len = strlen(argv[i]) + 1; rstr = (unsigned char*) malloc( yu_tmp_len * sizeof(unsigned char)); strcpy(rstr, argv[i]); //, yu_tmp_len); i++; rlen = deslash(rstr); if (bm_init(&bmb, rstr, rlen, ignore_case) < 0) { fprintf(stderr, "%s: Failed search string setup: %s\n", argv[0], rstr); exit(1); } max_depth = rlb.rlim_max - nworkers - 16; if (debug) fprintf(stderr, "max_depth = %d, nworkers = %d\n", max_depth, nworkers); pqueue_init(&pqb, nworkers + 8); pthread_attr_init(&pab); pthread_attr_setscope(&pab, PTHREAD_SCOPE_SYSTEM); aworkers = nworkers; for (j = 0; j < nworkers; ++j) if (pthread_create(&tid, &pab, worker, NULL) != 0) { fprintf(stderr, "%s: pthread_create: failed to create worker thread\n", argv[0]); exit(1); } //while (i < argc && do_ftw(argv[i++]) == 0) //; for (; i < argc; i++) { scan_root(argv[i], argv[i], &depth, max_depth); } pqueue_close(&pqb); if (debug) fprintf(stderr, "Waiting for workers to finish...\n"); pthread_mutex_lock(&aworker_lock); while (aworkers > 0) pthread_cond_wait(&aworker_cv, &aworker_lock); pthread_mutex_unlock(&aworker_lock); if (debug) fprintf(stderr, "n_files = %d, n_matches = %d, n_workers = %d, n_Mbytes = %d\n", n_files, n_matches, nworkers, (int) (n_bytes / 1000000)); //pthread_exit(0); return n_matches; }
/** string_match_splitter() * Splitter Function to assign portions of the file to each thread */ void string_match_splitter(void *data_in) { key1_final = malloc(strlen(key1) + 1); key2_final = malloc(strlen(key2) + 1); key3_final = malloc(strlen(key3) + 1); key4_final = malloc(strlen(key4) + 1); compute_hashes(key1, key1_final); compute_hashes(key2, key2_final); compute_hashes(key3, key3_final); compute_hashes(key4, key4_final); pthread_attr_t attr; pthread_t * tid; int i, num_procs; CHECK_ERROR((num_procs = sysconf(_SC_NPROCESSORS_ONLN)) <= 0); printf("THe number of processors is %d\n", num_procs); str_data_t * data = (str_data_t *)data_in; /* Check whether the various terms exist */ assert(data_in); tid = (pthread_t *)MALLOC(num_procs * sizeof(pthread_t)); /* Thread must be scheduled systemwide */ pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); int req_bytes = data->keys_file_len / num_procs; str_map_data_t *map_data = (str_map_data_t*)malloc(sizeof(str_map_data_t) * num_procs); map_args_t* out = (map_args_t*)malloc(sizeof(map_args_t) * num_procs); for(i=0; i<num_procs; i++) { map_data[i].encrypt_file = data->encrypt_file; map_data[i].keys_file = data->keys_file + data->bytes_comp; map_data[i].TID = i; /* Assign the required number of bytes */ int available_bytes = data->keys_file_len - data->bytes_comp; if(available_bytes < 0) available_bytes = 0; out[i].length = (req_bytes < available_bytes)? req_bytes:available_bytes; out[i].data = &(map_data[i]); char* final_ptr = map_data[i].keys_file + out[i].length; int counter = data->bytes_comp + out[i].length; /* make sure we end at a word */ while(counter <= data->keys_file_len && *final_ptr != '\n' && *final_ptr != '\r' && *final_ptr != '\0') { counter++; final_ptr++; } if(*final_ptr == '\r') counter+=2; else if(*final_ptr == '\n') counter++; out[i].length = counter - data->bytes_comp; data->bytes_comp = counter; CHECK_ERROR(pthread_create(&tid[i], &attr, string_match_map, (void*)(&(out[i]))) != 0); } /* Barrier, wait for all threads to finish */ for (i = 0; i < num_procs; i++) { int ret_val; CHECK_ERROR(pthread_join(tid[i], (void **)(void*)&ret_val) != 0); CHECK_ERROR(ret_val != 0); } pthread_attr_destroy(&attr); free(tid); free(key1_final); free(key2_final); free(key3_final); free(key4_final); free(out); free(map_data); }
int main(int argc, char *argv[]) { int fd; char * fdata; char * fname; struct stat finfo; int req_units, num_threads, num_procs, i; pthread_attr_t attr; lreg_args* tid_args; // Make sure a filename is specified if (argv[1] == NULL) { printf("USAGE: %s <filename>\n", argv[0]); exit(1); } fname = argv[1]; // Read in the file CHECK_ERROR((fd = open(fname, O_RDONLY)) < 0); // Get the file info (for file length) CHECK_ERROR(fstat(fd, &finfo) < 0); // Memory map the file CHECK_ERROR((fdata = (char*)mmap(0, finfo.st_size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) == NULL); CHECK_ERROR((num_procs = sysconf(_SC_NPROCESSORS_ONLN)) <= 0); printf("The number of processors is %d\n\n", num_procs); pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); num_threads = num_procs; printf("Linear Regression P-Threads: Running...\n"); POINT_T *points = (POINT_T*)fdata; long long n = (long long) finfo.st_size / sizeof(POINT_T); req_units = n / num_threads; tid_args = (lreg_args *)CALLOC(sizeof(lreg_args), num_procs); // Assign a portion of the points for each thread for(i = 0; i < num_threads; i++) { tid_args[i].points = &points[i*req_units]; tid_args[i].num_elems = req_units; if(i == (num_threads - 1)) tid_args[i].num_elems = n - i*req_units; CHECK_ERROR(pthread_create(&tid_args[i].tid, &attr, linear_regression_pthread, (void*)&tid_args[i]) != 0); } long long SX_ll = 0, SY_ll = 0, SXX_ll = 0, SYY_ll = 0, SXY_ll = 0; /* Barrier, wait for all threads to finish */ for (i = 0; i < num_threads; i++) { int ret_val; CHECK_ERROR(pthread_join(tid_args[i].tid, (void **)(void*)&ret_val) != 0); CHECK_ERROR(ret_val != 0); SX_ll += tid_args[i].SX; SY_ll += tid_args[i].SY; SXX_ll += tid_args[i].SXX; SYY_ll += tid_args[i].SYY; SXY_ll += tid_args[i].SXY; } free(tid_args); double a, b, xbar, ybar, r2; double SX = (double)SX_ll; double SY = (double)SY_ll; double SXX= (double)SXX_ll; double SYY= (double)SYY_ll; double SXY= (double)SXY_ll; b = (double)(n*SXY - SX*SY) / (n*SXX - SX*SX); a = (SY_ll - b*SX_ll) / n; xbar = (double)SX_ll / n; ybar = (double)SY_ll / n; r2 = (double)(n*SXY - SX*SY) * (n*SXY - SX*SY) / ((n*SXX - SX*SX)*(n*SYY - SY*SY)); printf("Linear Regression P-Threads Results:\n"); printf("\ta = %lf\n", a); printf("\tb = %lf\n", b); printf("\txbar = %lf\n", xbar); printf("\tybar = %lf\n", ybar); printf("\tr2 = %lf\n", r2); printf("\tSX = %lld\n", SX_ll); printf("\tSY = %lld\n", SY_ll); printf("\tSXX = %lld\n", SXX_ll); printf("\tSYY = %lld\n", SYY_ll); printf("\tSXY = %lld\n", SXY_ll); CHECK_ERROR(pthread_attr_destroy(&attr) < 0); CHECK_ERROR(munmap(fdata, finfo.st_size + 1) < 0); CHECK_ERROR(close(fd) < 0); return 0; }
int main(int argc, char *argv[]) { int x; int result; DATA D = new_data(); CREW crew; /* thread pool - crew.h */ LINES *lines; /* defined in setup.h */ CLIENT *client; /* defined in setup.h */ URL **urls; /* urls struct array */ pthread_t cease; /* the shutdown thread */ pthread_t timer; /* the timer thread */ pthread_attr_t scope_attr; /* set to PTHREAD_SCOPE_SYSTEM */ unsigned int randrseed; /* seed pthread_rand_np */ char exlogmsg[512]; void *statusp; sigset_t sigs; sigemptyset(&sigs); sigaddset(&sigs, SIGHUP); sigaddset(&sigs, SIGINT); sigaddset(&sigs, SIGALRM); sigaddset(&sigs, SIGTERM); sigprocmask(SIG_BLOCK, &sigs, NULL); lines = xcalloc(sizeof(LINES),1); lines->index = 0; lines->line = NULL; memset(&my, 0, sizeof(struct CONFIG)); parse_rc_cmdline(argc, argv); if(init_config() < 0){ /* defined in init.h */ exit( EXIT_FAILURE ); /* polly was a girl... */ } parse_cmdline(argc, argv); /* defined above */ ds_module_check(); /* check config integ */ if(my.get){ my.cusers = 1; my.reps = 1; my.logging = FALSE; my.bench = TRUE; } if(my.config){ show_config(TRUE); } if(my.url != NULL){ my.length = 1; } else { my.length = read_cfg_file( lines, my.file ); } if(my.length == 0){ display_help(); } /* cookie is an EXTERN, defined in setup */ cookie = xcalloc(sizeof(COOKIE), 1); cookie->first = NULL; if((result = pthread_mutex_init( &(cookie->mutex), NULL)) !=0){ joe_fatal( "pthread_mutex_init" ); } /* memory allocation for threads and clients */ urls = xmalloc(my.length * sizeof(URL)); client = xmalloc(sizeof(CLIENT) * my.cusers); if((crew = new_crew(my.cusers, my.cusers, FALSE)) == NULL){ joe_fatal("unable to allocate memory for %d simulated browser", my.cusers); } /** * determine the source of the url(s), * command line or file, and add them * to the urls struct. */ if(my.url != NULL){ urls[0] = add_url(my.url, 1); /* from command line */ } else { for(x = 0; x < my.length; x ++){ urls[x] = add_url(lines->line[x], x); /* from urls.txt file */ } } /** * display information about the siege * to the user and prepare for verbose * output if necessary. */ if(!my.get){ fprintf(stderr, "** "); display_version(FALSE); fprintf(stderr, "** Preparing %d concurrent users for battle.\n", my.cusers); fprintf(stderr, "The server is now under siege..."); if(my.verbose){ fprintf(stderr, "\n"); } if(my.extralog) { snprintf(exlogmsg, sizeof exlogmsg, "** Preparing %d concurrent users for battle.", my.cusers); log_extra(exlogmsg); log_extra("The server is now under siege..."); } } /** * record start time before spawning threads * as the threads begin hitting the server as * soon as they are created. */ data_set_start(D); /** * for each concurrent user, spawn a thread and * loop until condition or pthread_cancel from the * handler thread. */ pthread_attr_init(&scope_attr); pthread_attr_setscope(&scope_attr, PTHREAD_SCOPE_SYSTEM); #if defined(_AIX) /* AIX, for whatever reason, defies the pthreads standard and * * creates threads detached by default. (see pthread.h on AIX) */ pthread_attr_setdetachstate(&scope_attr, PTHREAD_CREATE_JOINABLE); #endif /** * invoke OpenSSL's thread safety */ #ifdef HAVE_SSL SSL_thread_setup(); #endif /** * create the signal handler and timer; the * signal handler thread (cease) responds to * ctrl-C (sigterm) and the timer thread sends * sigterm to cease on time out. */ if((result = pthread_create(&cease, NULL, (void*)sig_handler, (void*)crew)) < 0){ joe_fatal("failed to create handler: %d\n", result); } if(my.secs > 0){ if((result = pthread_create(&timer, NULL, (void*)siege_timer, (void*)cease)) < 0){ joe_fatal("failed to create handler: %d\n", result); } } randrseed = time(0); /** * loop until my.cusers and create a corresponding thread... */ for(x = 0; x < my.cusers && crew_get_shutdown(crew) != TRUE; x++){ client[x].id = x; client[x].bytes = 0; client[x].time = 0.0; client[x].hits = 0; client[x].code = 0; client[x].ok200 = 0; client[x].fail = 0; client[x].U = urls; client[x].auth.www = 0; client[x].auth.proxy = 0; client[x].auth.type.www = BASIC; client[x].auth.type.proxy = BASIC; client[x].rand_r_SEED = pthread_rand_np(&randrseed); result = crew_add(crew, (void*)start_routine, &(client[x])); if(result == FALSE){ my.verbose = FALSE; fprintf(stderr, "Unable to spawn additional threads; you may need to\n"); fprintf(stderr, "upgrade your libraries or tune your system in order\n"); fprintf(stderr, "to exceed %d users.\n", my.cusers); joe_fatal("system resources exhausted"); } } /* end of for pthread_create */ crew_join(crew, TRUE, &statusp); #ifdef HAVE_SSL SSL_thread_cleanup(); #endif /** * collect all the data from all the threads that * were spawned by the run. */ for(x = 0; x < ((crew_get_total(crew) > my.cusers || crew_get_total(crew)==0 ) ? my.cusers : crew_get_total(crew)); x++){ data_increment_count(D, client[x].hits); data_increment_bytes(D, client[x].bytes); data_increment_total(D, client[x].time); data_increment_code (D, client[x].code); data_increment_ok200(D, client[x].ok200); data_increment_fail (D, client[x].fail); data_set_highest (D, client[x].bigtime); data_set_lowest (D, client[x].smalltime); client[x].rand_r_SEED = pthread_rand_np(&randrseed); } /* end of stats accumulation */ /** * record stop time */ data_set_stop(D); /** * cleanup crew */ crew_destroy(crew); if(my.get){ if(data_get_count(D) > 0){ exit(EXIT_SUCCESS); } else { printf("[done]\n"); exit(EXIT_FAILURE); } } /** * take a short nap for cosmetic effect * this does NOT affect performance stats. */ pthread_usleep_np(10000); if(my.verbose) fprintf(stderr, "done.\n"); else fprintf(stderr, "\b done.\n"); if(my.extralog) log_extra("done."); /** * prepare and print statistics. */ if(my.failures > 0 && my.failed >= my.failures){ fprintf(stderr, "%s aborted due to excessive socket failure; you\n", program_name); fprintf(stderr, "can change the failure threshold in $HOME/.%src\n", program_name); } fprintf(stderr, "\bTransactions:\t\t%12u hits\n", data_get_count(D)); fprintf(stderr, "Availability:\t\t%12.2f %%\n", data_get_count(D)==0 ? 0 : (double)data_get_count(D) / (data_get_count(D)+my.failed) *100 ); fprintf(stderr, "Elapsed time:\t\t%12.2f secs\n", data_get_elapsed(D)); fprintf(stderr, "Data transferred:\t%12.2f MB\n", data_get_megabytes(D)); /*%12llu*/ fprintf(stderr, "Response time:\t\t%12.2f secs\n", data_get_response_time(D)); fprintf(stderr, "Transaction rate:\t%12.2f trans/sec\n", data_get_transaction_rate(D)); fprintf(stderr, "Throughput:\t\t%12.2f MB/sec\n", data_get_throughput(D)); fprintf(stderr, "Concurrency:\t\t%12.2f\n", data_get_concurrency(D)); fprintf(stderr, "Successful transactions:%12u\n", data_get_code(D)); if(my.debug){ fprintf(stderr, "HTTP OK received:\t%12u\n", data_get_ok200(D)); } fprintf(stderr, "Failed transactions:\t%12u\n", my.failed); fprintf(stderr, "Longest transaction:\t%12.2f\n", data_get_highest(D)); fprintf(stderr, "Shortest transaction:\t%12.2f\n", data_get_lowest(D)); fprintf(stderr, " \n"); if(my.mark) mark_log_file(my.markstr); if(my.logging) log_transaction(D); data_destroy(D); if(my.url == NULL){ for(x = 0; x < my.length; x++) xfree(lines->line[x]); xfree(lines->line); xfree(lines); } exit(EXIT_SUCCESS); } /* end of int main **/
static int my_pthread_attr_setscope(pthread_attr_t *__attr, int scope) { pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr; return pthread_attr_setscope(realattr, scope); }
int main() { struct sockaddr_in server_addr; bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0 server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htons(INADDR_ANY); server_addr.sin_port = htons(8000); int server_socket = socket(PF_INET,SOCK_STREAM,0); if( server_socket < 0) { printf("Create Socket Failed!"); exit(1); } int opt =1; setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) { perror("Server Bind Port 53 Failed: "); exit(1); } if ( listen(server_socket, 5) ) { printf("Server Listen Failed!"); exit(1); } pserver_context pcontext = (pserver_context) calloc(1,sizeof(server_context)); pcontext->client_context_lt = gdsl_list_alloc("client socket",NULL,NULL); pcontext->kdpfd = epoll_create(5); pthread_attr_t attr; pthread_t threadId; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* 设置线程为分离属性*/ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if(pthread_create(&threadId,&attr,message_handler,(void*)pcontext)) { perror("pthread_creat error!"); exit(-1); } while(1) { struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); printf("waiting for connection...\n"); int new_client_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length); if ( new_client_socket < 0) { printf("Server Accept Failed!\n"); /*break;*/ continue; } setnonblocking(new_client_socket); pconnect_context pconnection = calloc(1,sizeof(connect_context)); if( pconnection == NULL ) { perror("calloc event fail: "); close(new_client_socket); continue; } pconnection->ev.events = EPOLLIN; pconnection->session.socket = new_client_socket; pconnection->socket = new_client_socket; pconnection->ev.data.ptr = (void*)pconnection;/*(void*)&(pconnection->session);*/ if(epoll_ctl(pcontext->kdpfd, EPOLL_CTL_ADD, new_client_socket,&(pconnection->ev))<0) { perror("epoll_ctl fail: "); close(new_client_socket); continue; } gdsl_list_insert_tail( pcontext->client_context_lt,(void*)new_client_socket); } return 0; }
/****************************************************************************** * gst_tiaudenc1_init_audio * Initialize or re-initializes the audio stream ******************************************************************************/ static gboolean gst_tiaudenc1_init_audio(GstTIAudenc1 * audenc1) { Rendezvous_Attrs rzvAttrs = Rendezvous_Attrs_DEFAULT; struct sched_param schedParam; pthread_attr_t attr; GST_LOG("begin init_audio\n"); /* If audio has already been initialized, shut down previous encoder */ if (audenc1->hEngine) { if (!gst_tiaudenc1_exit_audio(audenc1)) { GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED, ("Failed to shut down existing audio encoder\n"), (NULL)); return FALSE; } } /* Make sure we know what codec we're using */ if (!audenc1->engineName) { GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED, ("Engine name not specified\n"), (NULL)); return FALSE; } if (!audenc1->codecName) { GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED, ("Codec name not specified\n"), (NULL)); return FALSE; } /* Initialize thread status management */ audenc1->threadStatus = 0UL; pthread_mutex_init(&audenc1->threadStatusMutex, NULL); /* Initialize rendezvous objects for making threads wait on conditions */ audenc1->waitOnEncodeThread = Rendezvous_create(2, &rzvAttrs); audenc1->waitOnEncodeDrain = Rendezvous_create(100, &rzvAttrs); audenc1->drainingEOS = FALSE; /* Initialize the custom thread attributes */ if (pthread_attr_init(&attr)) { GST_WARNING("failed to initialize thread attrs\n"); gst_tiaudenc1_exit_audio(audenc1); return FALSE; } /* Force the thread to use the system scope */ if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)) { GST_WARNING("failed to set scope attribute\n"); gst_tiaudenc1_exit_audio(audenc1); return FALSE; } /* Force the thread to use custom scheduling attributes */ if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) { GST_WARNING("failed to set schedule inheritance attribute\n"); gst_tiaudenc1_exit_audio(audenc1); return FALSE; } /* Set the thread to be fifo real time scheduled */ if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) { GST_WARNING("failed to set FIFO scheduling policy\n"); gst_tiaudenc1_exit_audio(audenc1); return FALSE; } /* Set the display thread priority */ schedParam.sched_priority = GstTIAudioThreadPriority; if (pthread_attr_setschedparam(&attr, &schedParam)) { GST_WARNING("failed to set scheduler parameters\n"); return FALSE; } /* Create encoder thread */ if (pthread_create(&audenc1->encodeThread, &attr, gst_tiaudenc1_encode_thread, (void*)audenc1)) { GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED, ("Failed to create encode thread\n"), (NULL)); gst_tiaudenc1_exit_audio(audenc1); return FALSE; } gst_tithread_set_status(audenc1, TIThread_CODEC_CREATED); /* Destroy the custom thread attributes */ if (pthread_attr_destroy(&attr)) { GST_WARNING("failed to destroy thread attrs\n"); gst_tiaudenc1_exit_audio(audenc1); return FALSE; } /* Make sure circular buffer and display buffer handles are created by * encoder thread. */ Rendezvous_meet(audenc1->waitOnEncodeThread); if (audenc1->circBuf == NULL || audenc1->hOutBufTab == NULL) { GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED, ("Encode thread failed to create circbuf or display buffer handles\n"), (NULL)); return FALSE; } GST_LOG("end init_audio\n"); return TRUE; }
/** * Reads a NOAAPORT data stream, creates LDM data-products from the stream, and * inserts the data-products into an LDM product-queue. The NOAAPORT data * stream can take the form of multicast UDP packets from (for example) a * Novra S300 DVB-S2 receiver or the standard input stream. * * Usage: * noaaportIngester [-l <em>log</em>] [-n|-v|-x] [-q <em>queue</em>] [-u <em>n</em>] [-m <em>mcastAddr</em>] [-I <em>iface</em>] [-b <em>npages</em>]\n * * Where: * <dl> * <dt>-b <em>npages</em></dt> * <dd>Allocate \e npages pages of memory for the internal buffer.</dd> * * <dt>-I <em>iface</em></dt> * <dd>Listen for multicast packets on interface \e iface.</dd> * * <dt>-l <em>log</em></dt> * <dd>Log to file \e log. The default is to use the system logging daemon * if the current process is a daemon; otherwise, the standard error * stream is used.</dd> * * <dt>-m <em>mcastAddr</em></dt> * <dd>Use the multicast address \e mcastAddr. The default is to * read from the standard input stream.</dd> * * <dt>-n</dt> * <dd>Log messages of level NOTE and higher priority. Each data-product * will generate a log message.</dd> * * <dt>-q <em>queue</em></dt> * <dd>Use \e queue as the pathname of the LDM product-queue. The default * is to use the default LDM pathname of the product-queue.</dd> * * <dt>-u <em>n</em></dt> * <dd>If logging is to the system logging daemon, then use facility * <b>local</b><em>n</em>. The default is to use the LDM facility.</dd> * * <dt>-v</dt> * <dd>Log messages of level INFO and higher priority.</dd> * * <dt>-x</dt> * <dd>Log messages of level DEBUG and higher priority.</dd> * </dl> * * If neither -n, -v, nor -x is specified, then logging will be restricted to * levels ERROR and WARN only. * * @retval 0 if successful. * @retval 1 if an error occurred. At least one error-message will be logged. */ int main( const int argc, /**< [in] Number of arguments */ char* const argv[]) /**< [in] Arguments */ { int status = 0; /* default success */ extern int optind; extern int opterr; int ch; const char* const progName = ubasename(argv[0]); const char* interface = NULL; int logmask = LOG_UPTO(LOG_WARNING); const unsigned logOptions = LOG_CONS | LOG_PID; const char* mcastSpec = NULL; const char* prodQueuePath = NULL; size_t npages = DEFAULT_NPAGES; Fifo* fifo; int ttyFd = open("/dev/tty", O_RDONLY); int processPriority = 0; int idx; const char* logPath = (-1 == ttyFd) ? NULL /* log to system logging daemon */ : "-"; /* log to standard error stream */ (void)close(ttyFd); (void)setulogmask(logmask); status = initLogging(progName, logOptions, logFacility, logPath); opterr = 0; /* no error messages from getopt(3) */ while (0 == status && (ch = getopt(argc, argv, "b:I:l:m:np:q:r:s:t:u:vx")) != -1) { switch (ch) { extern char* optarg; extern int optopt; case 'b': { unsigned long n; if (sscanf(optarg, "%lu", &n) != 1) { LOG_SERROR1("Couldn't decode FIFO size in pages: \"%s\"", optarg); status = 1; } else { npages = n; } break; } case 'I': interface = optarg; break; case 'l': logPath = optarg; status = initLogging(progName, logOptions, logFacility, logPath); break; case 'm': mcastSpec = optarg; break; case 'n': logmask |= LOG_MASK(LOG_NOTICE); (void)setulogmask(logmask); break; case 'p': { char* cp; errno = 0; processPriority = (int)strtol(optarg, &cp, 0); if (0 != errno) { LOG_SERROR1("Couldn't decode priority \"%s\"", optarg); log_log(LOG_ERR); } else { if (processPriority < -20) processPriority = -20; else if (processPriority > 20) processPriority = 20; } break; } case 'q': prodQueuePath = optarg; break; case 'r': #ifdef RETRANS_SUPPORT retrans_xmit_enable = atoi(optarg); if(retrans_xmit_enable == 1) retrans_xmit_enable = OPTION_ENABLE; else retrans_xmit_enable = OPTION_DISABLE; #endif break; case 's': { #ifdef RETRANS_SUPPORT strcpy(sbn_channel_name, optarg); if(!strcmp(optarg,NAME_SBN_TYP_GOES)) { sbn_type = SBN_TYP_GOES; break; } if(!strcmp(optarg,NAME_SBN_TYP_NOAAPORT_OPT)) { sbn_type = SBN_TYP_NOAAPORT_OPT; break; } if(!strcmp(optarg,"NWSTG")) { sbn_type = SBN_TYP_NMC; break; } if(!strcmp(optarg,NAME_SBN_TYP_NMC)) { sbn_type = SBN_TYP_NMC; break; } if(!strcmp(optarg,NAME_SBN_TYP_NMC2)) { sbn_type = SBN_TYP_NMC2; break; } if(!strcmp(optarg,NAME_SBN_TYP_NMC3)) { sbn_type = SBN_TYP_NMC3; break; } if(!strcmp(optarg,NAME_SBN_TYP_NWWS)) { sbn_type = SBN_TYP_NWWS; break; } if(!strcmp(optarg,NAME_SBN_TYP_ADD)) { sbn_type = SBN_TYP_ADD; break; } if(!strcmp(optarg,NAME_SBN_TYP_ENC)) { sbn_type = SBN_TYP_ENC; break; } if(!strcmp(optarg,NAME_SBN_TYP_EXP)) { sbn_type = SBN_TYP_EXP; break; } if(!strcmp(optarg,NAME_SBN_TYP_GRW)) { sbn_type = SBN_TYP_GRW; break; } if(!strcmp(optarg,NAME_SBN_TYP_GRE)) { sbn_type = SBN_TYP_GRE; break; } printf("Operator input: UNKNOWN type must be\n"); printf(" %s, %s, %s, %s, %s, %s, %s, %s, %s, %s or %s \n", NAME_SBN_TYP_NMC, NAME_SBN_TYP_GOES, NAME_SBN_TYP_NOAAPORT_OPT, NAME_SBN_TYP_NMC2, NAME_SBN_TYP_NMC3, NAME_SBN_TYP_NWWS, NAME_SBN_TYP_ADD, NAME_SBN_TYP_ENC, NAME_SBN_TYP_EXP, NAME_SBN_TYP_GRW, NAME_SBN_TYP_GRE); #endif break; } case 't': #ifdef RETRANS_SUPPORT strcpy(transfer_type, optarg); if(!strcmp(transfer_type,"MHS") || !strcmp(transfer_type,"mhs")){ /** Using MHS for communication with NCF **/ }else{ uerror("No other mechanism other than MHS is currently supported\n"); status = 1; } #endif break; case 'u': { int i = atoi(optarg); if (0 > i || 7 < i) { LOG_START1("Invalid logging facility number: %d", i); status = 1; } else { static int logFacilities[] = {LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7}; logFacility = logFacilities[i]; status = initLogging(progName, logOptions, logFacility, logPath); } break; } case 'v': logmask |= LOG_MASK(LOG_INFO); (void)setulogmask(logmask); break; case 'x': logmask |= LOG_MASK(LOG_DEBUG); (void)setulogmask(logmask); break; default: optopt = ch; /*FALLTHROUGH*/ /* no break */ case '?': { uerror("Unknown option: \"%c\"", optopt); status = 1; break; } } /* option character switch */ } /* getopt() loop */ if (0 == status) { if (optind < argc) { uerror("Extraneous command-line argument: \"%s\"", argv[optind]); status = 1; } } if (0 != status) { uerror("Error decoding command-line"); usage(progName); } else { unotice("Starting Up %s", PACKAGE_VERSION); unotice("%s", COPYRIGHT_NOTICE); if ((status = fifoNew(npages, &fifo)) != 0) { LOG_ADD0("Couldn't create FIFO"); log_log(LOG_ERR); } else { LdmProductQueue* prodQueue; if ((status = lpqGet(prodQueuePath, &prodQueue)) != 0) { LOG_ADD0("Couldn't open LDM product-queue"); log_log(LOG_ERR); } else { if (NULL == mcastSpec) { if (0 == (status = spawnProductMaker(NULL, fifo, prodQueue, &productMaker, &productMakerThread))) { status = spawnFileReader(NULL, NULL, fifo, &reader, &readerThread); } } /* reading file */ else { pthread_attr_t attr; if (0 != (status = pthread_attr_init(&attr))) { LOG_ERRNUM0(status, "Couldn't initialize thread attribute"); } else { #ifndef _POSIX_THREAD_PRIORITY_SCHEDULING uwarn("Can't adjust thread priorities due to lack of " "necessary support from environment"); #else /* * In order to not miss any data, the reader thread * should preempt the product-maker thread as soon as * data is available and run as long as data is * available. */ const int SCHED_POLICY = SCHED_FIFO; struct sched_param param; param.sched_priority = sched_get_priority_max(SCHED_POLICY) - 1; (void)pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); (void)pthread_attr_setschedpolicy(&attr, SCHED_POLICY); (void)pthread_attr_setschedparam(&attr, ¶m); (void)pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); #endif #ifdef RETRANS_SUPPORT if (retrans_xmit_enable == OPTION_ENABLE){ /* Copy mcastAddress needed to obtain the cpio entries */ strcpy(mcastAddr, mcastSpec); } #endif if (0 == (status = spawnProductMaker(&attr, fifo, prodQueue, &productMaker, &productMakerThread))) { #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING param.sched_priority++; (void)pthread_attr_setschedparam(&attr, ¶m); #endif status = spawnMulticastReader(&attr, mcastSpec, interface, fifo, &reader, &readerThread); } /* product-maker spawned */ } /* "attr" initialized */ } /* reading multicast packets */ if (0 != status) { log_log(LOG_ERR); status = 1; } else { pthread_t statThread; (void)gettimeofday(&startTime, NULL); reportTime = startTime; (void)pthread_create(&statThread, NULL, reportStatsWhenSignaled, NULL); set_sigactions(); (void)pthread_join(readerThread, NULL); status = readerStatus(reader); (void)pthread_cancel(statThread); (void)pthread_join(statThread, NULL); (void)fifoCloseWhenEmpty(fifo); (void)pthread_join(productMakerThread, NULL); if (0 != status) status = pmStatus(productMaker); reportStats(); readerFree(reader); #ifdef RETRANS_SUPPORT /** Release buffer allocated for retransmission **/ if(retrans_xmit_enable == OPTION_ENABLE){ freeRetransMem(); } #endif } /* "reader" spawned */ (void)lpqClose(prodQueue); } /* "prodQueue" open */ } /* "fifo" created */ } /* command line decoded */ return status; }
void create_fsal_up_threads() { int rc, id; pthread_attr_t attr_thr; fsal_up_arg_t *fsal_up_args; exportlist_t *pcurrent; memset(&attr_thr, 0, sizeof(attr_thr)); /* Initialization of thread attrinbutes borrowed from nfs_init.c */ if(pthread_attr_init(&attr_thr) != 0) LogDebug(COMPONENT_THREAD, "can't init pthread's attributes"); if(pthread_attr_setscope(&attr_thr, PTHREAD_SCOPE_SYSTEM) != 0) LogDebug(COMPONENT_THREAD, "can't set pthread's scope"); if(pthread_attr_setdetachstate(&attr_thr, PTHREAD_CREATE_JOINABLE) != 0) LogDebug(COMPONENT_THREAD, "can't set pthread's join state"); if(pthread_attr_setstacksize(&attr_thr, THREAD_STACK_SIZE) != 0) LogDebug(COMPONENT_THREAD, "can't set pthread's stack size"); /* The admin thread is the only other thread that may be * messing around with the export entries. */ // LOCK EXPORT LIST for(pcurrent = nfs_param.pexportlist; pcurrent != NULL; pcurrent = pcurrent->next) { if (pcurrent->use_fsal_up == FALSE) continue; /* Make sure there are not multiple fsal_up_threads handling multiple * exports on the same filesystem. This could potentially cause issues. */ LogEvent(COMPONENT_INIT, "Checking if export id %d with filesystem " "id %llu.%llu already has an assigned FSAL_UP thread.", pcurrent->fsalid, pcurrent->filesystem_id.major, pcurrent->filesystem_id.minor); id = fsal_up_thread_exists(pcurrent); if (id) { LogEvent(COMPONENT_INIT, "Filesystem %llu.%llu already has an " "assigned FSAL_UP with export id %d so export w/ id %d" " is not being assigned a new FSAL_UP thread.", pcurrent->filesystem_id.major, pcurrent->filesystem_id.minor, id, pcurrent->id); continue; } else { LogEvent(COMPONENT_INIT, "Filesystem %llu.%llu export id %d does not" " have an FSAL_UP thread yet, creating a thread now.", pcurrent->filesystem_id.major, pcurrent->filesystem_id.minor, pcurrent->id); if((fsal_up_args = (fsal_up_arg_t *) Mem_Alloc(sizeof(fsal_up_arg_t))) == NULL) { LogError(COMPONENT_INIT, ERR_SYS, ERR_MALLOC, errno); Fatal(); } memset(fsal_up_args, 0, sizeof(fsal_up_arg_t)); fsal_up_args->export_entry = pcurrent; if( ( rc = pthread_create( &pcurrent->fsal_up_thr, &attr_thr, fsal_up_thread,(void *)fsal_up_args)) != 0) { Mem_Free(fsal_up_args); LogFatal(COMPONENT_THREAD, "Could not create fsal_up_thread, error = %d (%s)", errno, strerror(errno)); Fatal(); } } } }
/* This function will initialize every pthread_attr_t object in the scenarii array */ void scenar_init() { int ret=0; int i; int old; long pagesize, minstacksize; long tsa, tss, tps; pagesize =sysconf(_SC_PAGESIZE); minstacksize =sysconf(_SC_THREAD_STACK_MIN); tsa =sysconf(_SC_THREAD_ATTR_STACKADDR); tss =sysconf(_SC_THREAD_ATTR_STACKSIZE); tps =sysconf(_SC_THREAD_PRIORITY_SCHEDULING); #if VERBOSE > 0 output("System abilities:\n"); output(" TSA: %li\n", tsa); output(" TSS: %li\n", tss); output(" TPS: %li\n", tps); output(" pagesize: %li\n", pagesize); output(" min stack size: %li\n", minstacksize); #endif if (minstacksize % pagesize) { UNTESTED("The min stack size is not a multiple of the page size"); } for (i=0; i<NSCENAR; i++) { #if VERBOSE > 2 output("Initializing attribute for scenario %i: %s\n", i, scenarii[i].descr); #endif ret = pthread_attr_init(&scenarii[i].ta); if (ret != 0) { UNRESOLVED(ret, "Failed to initialize a thread attribute object"); } /* Set the attributes according to the scenario */ if (scenarii[i].detached == 1) { ret = pthread_attr_setdetachstate(&scenarii[i].ta, PTHREAD_CREATE_DETACHED); if (ret != 0) { UNRESOLVED(ret, "Unable to set detachstate"); } } else { ret = pthread_attr_getdetachstate(&scenarii[i].ta, &old); if (ret != 0) { UNRESOLVED(ret, "Unable to get detachstate from initialized attribute"); } if (old != PTHREAD_CREATE_JOINABLE) { FAILED("The default attribute is not PTHREAD_CREATE_JOINABLE"); } } #if VERBOSE > 4 output("Detach state was set sucessfully\n"); #endif /* Sched related attributes */ if (tps>0) /* This routine is dependent on the Thread Execution Scheduling option */ { if (scenarii[i].explicitsched == 1) ret = pthread_attr_setinheritsched(&scenarii[i].ta, PTHREAD_EXPLICIT_SCHED); else ret = pthread_attr_setinheritsched(&scenarii[i].ta, PTHREAD_INHERIT_SCHED); if (ret != 0) { UNRESOLVED(ret, "Unable to set inheritsched attribute"); } #if VERBOSE > 4 output("inheritsched state was set sucessfully\n"); #endif } #if VERBOSE > 4 else { output("TPS unsupported => inheritsched parameter untouched\n"); } #endif if (tps>0) /* This routine is dependent on the Thread Execution Scheduling option */ { if (scenarii[i].schedpolicy == 1) { ret = pthread_attr_setschedpolicy(&scenarii[i].ta, SCHED_FIFO); } if (scenarii[i].schedpolicy == 2) { ret = pthread_attr_setschedpolicy(&scenarii[i].ta, SCHED_RR); } if (ret != 0) { UNRESOLVED(ret, "Unable to set the sched policy"); } #if VERBOSE > 4 if (scenarii[i].schedpolicy) output("Sched policy was set sucessfully\n"); else output("Sched policy untouched\n"); #endif } #if VERBOSE > 4 else output("TPS unsupported => sched policy parameter untouched\n"); #endif if (scenarii[i].schedparam != 0) { struct sched_param sp; ret = pthread_attr_getschedpolicy(&scenarii[i].ta, &old); if (ret != 0) { UNRESOLVED(ret, "Unable to get sched policy from attribute"); } if (scenarii[i].schedparam == 1) sp.sched_priority = sched_get_priority_max(old); if (scenarii[i].schedparam == -1) sp.sched_priority = sched_get_priority_min(old); ret = pthread_attr_setschedparam(&scenarii[i].ta, &sp); if (ret != 0) { UNRESOLVED(ret, "Failed to set the sched param"); } #if VERBOSE > 4 output("Sched param was set sucessfully to %i\n", sp.sched_priority); } else { output("Sched param untouched\n"); #endif } if (tps>0) /* This routine is dependent on the Thread Execution Scheduling option */ { ret = pthread_attr_getscope(&scenarii[i].ta, &old); if (ret != 0) { UNRESOLVED(ret, "Failed to get contension scope from thread attribute"); } if (scenarii[i].altscope != 0) { if (old == PTHREAD_SCOPE_PROCESS) old = PTHREAD_SCOPE_SYSTEM; else old = PTHREAD_SCOPE_PROCESS; ret = pthread_attr_setscope(&scenarii[i].ta, old); //if (ret != 0) { UNRESOLVED(ret, "Failed to set contension scope"); } if (ret != 0) { output("WARNING: The TPS option is claimed to be supported but setscope fails\n"); } #if VERBOSE > 4 output("Contension scope set to %s\n", old==PTHREAD_SCOPE_PROCESS?"PTHREAD_SCOPE_PROCESS":"PTHREAD_SCOPE_SYSTEM"); } else { output("Contension scope untouched (%s)\n", old==PTHREAD_SCOPE_PROCESS?"PTHREAD_SCOPE_PROCESS":"PTHREAD_SCOPE_SYSTEM"); #endif } } #if VERBOSE > 4 else output("TPS unsupported => sched contension scope parameter untouched\n"); #endif /* Stack related attributes */ if ((tss>0) && (tsa>0)) /* This routine is dependent on the Thread Stack Address Attribute and Thread Stack Size Attribute options */ { if (scenarii[i].altstack != 0) { /* This is slightly more complicated. We need to alloc a new stack and free it upon test termination */ /* We will alloc with a simulated guardsize of 1 pagesize */ scenarii[i].bottom = malloc(minstacksize + pagesize); if (scenarii[i].bottom == NULL) { fprintf(stderr, "test %d\n", i); UNRESOLVED(errno,"Unable to alloc enough memory for alternative stack"); } ret = pthread_attr_setstack(&scenarii[i].ta, scenarii[i].bottom, minstacksize); if (ret != 0) { fprintf(stderr, "test %d pthread_attr_setstack did not return 0, returned %d\n", i, ret); UNRESOLVED(ret, "Failed to specify alternate stack, pthread_attr_setstack"); } #if VERBOSE > 1 output("Alternate stack created successfully. Bottom=%p, Size=%i\n", scenarii[i].bottom, minstacksize); #endif } } #if VERBOSE > 4 else { output("TSA or TSS unsupported => No alternative stack\n"); } #endif #ifndef WITHOUT_XOPEN if (scenarii[i].guard != 0) { if (scenarii[i].guard == 1){ ret = pthread_attr_setguardsize(&scenarii[i].ta, 0); } if (scenarii[i].guard == 2){ ret = pthread_attr_setguardsize(&scenarii[i].ta, pagesize); } if (ret != 0) { fprintf(stderr, "test %d, ret %d\n", i, ret); UNRESOLVED(ret, "Unable to set guard area size in thread stack"); } #if VERBOSE > 4 output("Guard size set to %i\n", (scenarii[i].guard==1)?1:pagesize); #endif } #endif if (tss>0) /* This routine is dependent on the Thread Stack Size Attribute option */ { if (scenarii[i].altsize != 0) { ret = pthread_attr_setstacksize(&scenarii[i].ta, minstacksize); if (ret != 0) { fprintf(stderr, "test %d, ret %d\n", i, ret); UNRESOLVED(ret, "Unable to change stack size"); } #if VERBOSE > 4 output("Stack size set to %i (this is the min)\n", minstacksize); #endif } } #if VERBOSE > 4 else output("TSS unsupported => stack size unchanged\n"); #endif ret = sem_init(&scenarii[i].sem, 0,0); if (ret == -1) { UNRESOLVED(errno, "Unable to init a semaphore"); } } #if VERBOSE > 0 output("All %i thread attribute objects were initialized\n\n", NSCENAR); #endif }