void executormgr_unbind_executor_task(struct DispatchData *data, QueryExecutor *executor, struct DispatchTask *task, struct DispatchSlice *slice) { if (executor->state == QES_UNINIT) return; /* Return executors */ TIMING_BEGIN(executor->time_free_begin); if (!executor->takeovered) executormgr_free_executor(executor->desc); executor->state = QES_UNINIT; TIMING_END(executor->time_free_end); }
/* compare contiguous, chunked, and compressed performance */ int main(int argc, char *argv[]) { int stat; /* return status */ int ncid; /* netCDF id */ int i, j, k; int dim1id, dim2id, dim3id; int varid_g; /* varid for contiguous */ int varid_k; /* varid for chunked */ int varid_x; /* varid for compressed */ float *varxy, *varxz, *varyz; /* 2D memory slabs used for I/O */ int mm; size_t dims[] = {256, 256, 256}; /* default dim lengths */ size_t chunks[] = {32, 32, 32}; /* default chunk sizes */ size_t start[3], count[3]; float contig_time, chunked_time, compressed_time, ratio; int deflate_level = 1; /* default compression level, 9 is * better and slower. If negative, * turn on shuffle filter also. */ int shuffle = NC_NOSHUFFLE; size_t cache_size_def; size_t cache_hash_def; float cache_pre_def; size_t cache_size = 0; /* use library default */ size_t cache_hash = 0; /* use library default */ float cache_pre = -1.0f; /* use library default */ /* rank (number of dimensions) for each variable */ # define RANK_var1 3 /* variable shapes */ int var_dims[RANK_var1]; TIMING_DECLS(TMsec) ; /* From args, get parameters for timing, including variable and chunk sizes. Negative deflate level means also use shuffle filter. */ parse_args(argc, argv, &deflate_level, &shuffle, dims, chunks, &cache_size, &cache_hash, &cache_pre); /* get cache defaults, then set cache parameters that are not default */ if((stat = nc_get_chunk_cache(&cache_size_def, &cache_hash_def, &cache_pre_def))) ERR1(stat); if(cache_size == 0) cache_size = cache_size_def; if(cache_hash == 0) cache_hash = cache_hash_def; if(cache_pre == -1.0f) cache_pre = cache_pre_def; if((stat = nc_set_chunk_cache(cache_size, cache_hash, cache_pre))) ERR1(stat); printf("cache: %3.2f MBytes %ld objs %3.2f preempt, ", cache_size/1.e6, cache_hash, cache_pre); if(deflate_level == 0) { printf("uncompressed "); } else { printf("compression level %d", deflate_level); } if(shuffle == 1) { printf(", shuffled"); } printf("\n\n"); /* initialize 2D slabs for writing along each axis with phony data */ varyz = (float *) emalloc(sizeof(float) * 1 * dims[1] * dims[2]); varxz = (float *) emalloc(sizeof(float) * dims[0] * 1 * dims[2]); varxy = (float *) emalloc(sizeof(float) * dims[0] * dims[1] * 1); mm = 0; for(j = 0; j < dims[1]; j++) { for(k = 0; k < dims[2]; k++) { varyz[mm++] = k + dims[2]*j; } } mm = 0; for(i = 0; i < dims[0]; i++) { for(k = 0; k < dims[2]; k++) { varxz[mm++] = k + dims[2]*i; } } mm = 0; for(i = 0; i < dims[0]; i++) { for(j = 0; j < dims[1]; j++) { varxy[mm++] = j + dims[1]*i; } } if((stat = nc_create(FILENAME, NC_NETCDF4 | NC_CLASSIC_MODEL, &ncid))) ERR1(stat); /* define dimensions */ if((stat = nc_def_dim(ncid, "dim1", dims[0], &dim1id))) ERR1(stat); if((stat = nc_def_dim(ncid, "dim2", dims[1], &dim2id))) ERR1(stat); if((stat = nc_def_dim(ncid, "dim3", dims[2], &dim3id))) ERR1(stat); /* define variables */ var_dims[0] = dim1id; var_dims[1] = dim2id; var_dims[2] = dim3id; if((stat = nc_def_var(ncid, "var_contiguous", NC_FLOAT, RANK_var1, var_dims, &varid_g))) ERR1(stat); if((stat = nc_def_var(ncid, "var_chunked", NC_FLOAT, RANK_var1, var_dims, &varid_k))) ERR1(stat); if((stat = nc_def_var(ncid, "var_compressed", NC_FLOAT, RANK_var1, var_dims, &varid_x))) ERR1(stat); if((stat = nc_def_var_chunking(ncid, varid_g, NC_CONTIGUOUS, 0))) ERR1(stat); if((stat = nc_def_var_chunking(ncid, varid_k, NC_CHUNKED, chunks))) ERR1(stat); if((stat = nc_def_var_chunking(ncid, varid_x, NC_CHUNKED, chunks))) ERR1(stat); if (deflate_level != 0) { if((stat = nc_def_var_deflate(ncid, varid_x, shuffle, NC_COMPRESSED, deflate_level))) ERR1(stat); } /* leave define mode */ if((stat = nc_enddef (ncid))) ERR1(stat); /* write each variable one yz slab at a time */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = dims[1]; count[2] = dims[2]; sprintf(time_mess," contiguous write %3ld %3ld %3ld", 1, dims[1], dims[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { start[0] = i; if((stat = nc_put_vara(ncid, varid_g, start, count, &varyz[0]))) ERR1(stat); } TIMING_END(TMsec) ; printf("\n"); contig_time = TMsec; sprintf(time_mess," chunked write %3ld %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { start[0] = i; if((stat = nc_put_vara(ncid, varid_k, start, count, &varyz[0]))) ERR1(stat); } TIMING_END(TMsec) ; chunked_time = TMsec; ratio = contig_time/chunked_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); sprintf(time_mess," compressed write %3ld %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { start[0] = i; if((stat = nc_put_vara(ncid, varid_x, start, count, &varyz[0]))) ERR1(stat); } TIMING_END(TMsec) ; compressed_time = TMsec; ratio = contig_time/compressed_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); printf("\n"); /* write each variable one xz slab at a time */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = dims[0]; count[1] = 1; count[2] = dims[2]; sprintf(time_mess," contiguous write %3ld %3ld %3ld", dims[0], 1, dims[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { start[1] = i; if((stat = nc_put_vara(ncid, varid_g, start, count, &varxz[0]))) ERR1(stat); } TIMING_END(TMsec) ; printf("\n"); contig_time = TMsec; sprintf(time_mess," chunked write %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { start[1] = i; if((stat = nc_put_vara(ncid, varid_k, start, count, &varxz[0]))) ERR1(stat); } TIMING_END(TMsec) ; chunked_time = TMsec; ratio = contig_time/chunked_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); sprintf(time_mess," compressed write %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { start[1] = i; if((stat = nc_put_vara(ncid, varid_x, start, count, &varxz[0]))) ERR1(stat); } TIMING_END(TMsec) ; compressed_time = TMsec; ratio = contig_time/compressed_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); printf("\n"); /* write each variable one xy slab at a time */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = dims[0]; count[1] = dims[1]; count[2] = 1; sprintf(time_mess," contiguous write %3ld %3ld %3ld", dims[0], dims[1], 1); TIMING_START ; for(i = 0; i < dims[2]; i++) { start[2] = i; if((stat = nc_put_vara(ncid, varid_g, start, count, &varxy[0]))) ERR1(stat); } TIMING_END(TMsec) ; printf("\n"); contig_time = TMsec; sprintf(time_mess," chunked write %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { start[2] = i; if((stat = nc_put_vara(ncid, varid_k, start, count, &varxy[0]))) ERR1(stat); } TIMING_END(TMsec) ; chunked_time = TMsec; ratio = contig_time/chunked_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); sprintf(time_mess," compressed write %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { start[2] = i; if((stat = nc_put_vara(ncid, varid_x, start, count, &varxy[0]))) ERR1(stat); } TIMING_END(TMsec) ; compressed_time = TMsec; ratio = contig_time/compressed_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); printf("\n"); /* read each variable one yz slab at a time */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = dims[1]; count[2] = dims[2]; sprintf(time_mess," contiguous read %3ld %3ld %3ld", 1, dims[1], dims[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { start[0] = i; if((stat = nc_get_vara(ncid, varid_g, start, count, &varyz[0]))) ERR1(stat); } TIMING_END(TMsec) ; printf("\n"); contig_time = TMsec; sprintf(time_mess," chunked read %3ld %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2] , chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { start[0] = i; if((stat = nc_get_vara(ncid, varid_k, start, count, &varyz[0]))) ERR1(stat); } TIMING_END(TMsec) ; chunked_time = TMsec; ratio = contig_time/chunked_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); sprintf(time_mess," compressed read %3ld %3ld %3ld %3ld %3ld %3ld", 1, dims[1], dims[2] , chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[0]; i++) { start[0] = i; if((stat = nc_get_vara(ncid, varid_x, start, count, &varyz[0]))) ERR1(stat); } TIMING_END(TMsec) ; compressed_time = TMsec; ratio = contig_time/compressed_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); printf("\n"); /* read each variable one xz slab at a time */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = dims[0]; count[1] = 1; count[2] = dims[2]; sprintf(time_mess," contiguous read %3ld %3ld %3ld", dims[0], 1, dims[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { start[1] = i; if((stat = nc_get_vara(ncid, varid_g, start, count, &varxz[0]))) ERR1(stat); } TIMING_END(TMsec) ; printf("\n"); contig_time = TMsec; sprintf(time_mess," chunked read %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { start[1] = i; if((stat = nc_get_vara(ncid, varid_k, start, count, &varxz[0]))) ERR1(stat); } TIMING_END(TMsec) ; chunked_time = TMsec; ratio = contig_time/chunked_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); sprintf(time_mess," compressed read %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[1]; i++) { start[1] = i; if((stat = nc_get_vara(ncid, varid_x, start, count, &varxz[0]))) ERR1(stat); } TIMING_END(TMsec) ; compressed_time = TMsec; ratio = contig_time/compressed_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); printf("\n"); /* read variable one xy slab at a time */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = dims[0]; count[1] = dims[1]; count[2] = 1; sprintf(time_mess," contiguous read %3ld %3ld %3ld", dims[0], dims[1], 1); TIMING_START ; for(i = 0; i < dims[2]; i++) { start[2] = i; if((stat = nc_get_vara(ncid, varid_g, start, count, &varxy[0]))) ERR1(stat); } TIMING_END(TMsec) ; printf("\n"); contig_time = TMsec; sprintf(time_mess," chunked read %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { start[2] = i; if((stat = nc_get_vara(ncid, varid_k, start, count, &varxy[0]))) ERR1(stat); } TIMING_END(TMsec) ; chunked_time = TMsec; ratio = contig_time/chunked_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); sprintf(time_mess," compressed read %3ld %3ld %3ld %3ld %3ld %3ld", dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]); TIMING_START ; for(i = 0; i < dims[2]; i++) { start[2] = i; if((stat = nc_get_vara(ncid, varid_x, start, count, &varxy[0]))) ERR1(stat); } TIMING_END(TMsec) ; compressed_time = TMsec; ratio = contig_time/compressed_time; if(ratio >= 1.0) printf(" %5.2g x faster\n", ratio); else printf(" %5.2g x slower\n", 1.0/ratio); if((stat = nc_close(ncid))) ERR1(stat); return 0; }
int main(int argc, char *argv[]) { uint8_t active = 1; int error; int pipefd[2]; /* buffer length in samples. would be multiplied by channels and sample size */ int buffer_length = 1024; int listen_port = 5002; uint32_t counter = 0; pthread_t audio_thr; /* pthread_attr_t audio_thr_attr; */ /* record parameters */ pa_sample_spec ss = { /* for fft need PA_SAMPLE_FLOAT32LE or PA_SAMPLE_FLOAT32BE */ .format = PA_SAMPLE_FLOAT32LE, // PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 }; SAUDIORECTHRPARAMS audio_thr_params = {0}; HPINLIST connection = NULL; HPIN pin, pipe_pin; HBUF sample, dummy_sample; TIMING_MEASURE_AREA; if (argc < 2) { printf("usage: source <listen_port>\n"); } sscanf(argv[1], "%i", &listen_port); printf("Will listen %i port\n", listen_port); connection = pin_list_create(MAX_EVENTS); pin_listen(connection, listen_port, BACKLOG, NULL); dummy_sample = buf_alloc(dummy_size_callback); if (pipe(pipefd) == -1) { handle_error("pipe()"); } setnonblocking(pipefd[0]); setnonblocking(pipefd[1]); pipe_pin = pin_list_add_custom_fd(connection, pipefd[0], PIN_TYPE_CUSTOM); /* thread creation */ audio_thr_params.pipefd = pipefd[1]; audio_thr_params.buffer_size = buffer_length; audio_thr_params.sample_spec = &ss; audio_thr_params.argc = argc; audio_thr_params.argv = argv; audio_thr_params.active = 1; #if 0 if ( (error = pthread_attr_init(&audio_thr_attr)) != 0 ) handle_error_en(error, "pthread_attr_init()"); if ( (error = pthread_attr_setstacksize(&audio_thr_attr, 5000)) != 0) handle_error_en(error, "pthread_attr_setstacksize()"); #endif if ( (error = pthread_create(&audio_thr, NULL, audio_capture_thr, &audio_thr_params)) != 0 ) handle_error_en(error, "pthread_create()"); /** * :TODO: It's need to improve latency while sending buffers */ while (active && pin_list_wait(connection, -1) != PIN_ERROR) { pin_list_deliver(connection); while ( (pin = pin_list_get_next_event(connection, PIN_EVENT_READ)) != NULL ) { if (pin == pipe_pin) { TIMING_START(); counter = 0; while ( pin_read_raw(pin, &sample, PTR_SIZE) != 0 ) { /* if pin = pipe_pin, read pointer to buffer, write buffer into socket and free it */ /* printf("[source] read %p\n", sample); */ print_header((PSSAMPLEHEADER)sample->buf, sample->buf + HEADER_SIZE, sample->size - HEADER_SIZE); pin_list_write_sample(connection, sample, 0); buf_free(sample); counter++; } /* printf("[source] %u samples\n", counter); */ TIMING_END("source"); continue; } switch (pin_read_sample(pin, dummy_sample)) { case PIN_STATUS_READY: { break; } case PIN_STATUS_CLOSED: { if (pin->type == PIN_TYPE_INPUT) { printf("one of inputs closed. exit.\n"); active = 0; continue; } else { printf("connection closed\n"); } pin_disconnect(pin); /* close data and skip iteration */ continue; } case PIN_STATUS_PARTIAL: { printf(" partial data. %u / %u\n", dummy_sample->size, dummy_sample->full_size); /* do nothing since no data ready */ break; } case PIN_STATUS_NO_DATA: { printf(" no data. %u / %u\n", dummy_sample->size, dummy_sample->full_size); /* do nothing since no data ready */ break; } default: { break; } } dummy_sample->size = 0; } } //finish: /* loop until pipe_pin is null. and free recieved buffers. */ audio_thr_params.active = 0; pthread_join(audio_thr, NULL); pin_list_destroy(connection); exit(EXIT_SUCCESS); }
static void * audio_capture_thr(void *args) { int error; PSAUDIORECTHRPARAMS params = (PSAUDIORECTHRPARAMS)args; pa_simple *pa_context = NULL; uint8_t active = 1; uint32_t buffer_size; SSAMPLEHEADER sample_header = {0}; HBUF sample; TIMING_MEASURE_AREA; buffer_size = HEADER_SIZE + params->sample_spec->channels * params->buffer_size * pa_sample_size(params->sample_spec); sample_header.number = 0; sample_header.buf_type = BUF_TYPE_INTERLEAVED; sample_header.sample_size = pa_sample_size(params->sample_spec); sample_header.samples = params->buffer_size; sample_header.channels = params->sample_spec->channels; sample_header.samplerate = params->sample_spec->rate; if ( !(pa_context = pa_simple_new(NULL, params->argv[0], PA_STREAM_RECORD, NULL, "record", params->sample_spec, NULL, NULL, &error)) ) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto audio_thr_finish; } printf("[source] audio thr\n"); while (active) { TIMING_START(); sample = buf_alloc(NULL); buf_resize(sample, buffer_size); sample->full_size = sample->size = sample->alloced_size; sample_zero_buffer(sample); memcpy(sample->buf, &sample_header, sizeof(SSAMPLEHEADER)); if (pa_simple_read(pa_context, sample->buf + HEADER_SIZE, sample->alloced_size - HEADER_SIZE, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); goto audio_thr_finish; } pa_gettimeofday(&(sample_header.timestamp)); sample_header.number += 1; /* printf("[audio] read %p\n", sample); */ if ( (error = write(params->pipefd, &sample, sizeof(sample))) != sizeof(sample)) { if (error == -1) { handle_error("[audio] write()"); } buf_free(sample); perror("[audio] "); printf("[audio] uverrun. free buffer\n"); } active = params->active; TIMING_END(" audio"); } audio_thr_finish: pa_simple_free(pa_context); return NULL; }
static void * ui_updater_thr(void *args) { PSUIUPDATERPARAMS params = (PSUIUPDATERPARAMS)args; uint8_t active = 1; HPINLIST pin_list; HPIN pin; PSMETABUFER meta; TIMING_MEASURE_AREA; pin_list = pin_list_create(2); pin_list_add_custom_fd(pin_list, params->params->infd, PIN_TYPE_CUSTOM); printf("[updater] started\n"); while (active && pin_list_wait(pin_list, -1) != PIN_ERROR) { while ( (pin = pin_list_get_next_event(pin_list, PIN_EVENT_READ)) != NULL ) { pin_read_raw(pin, &meta, PTR_SIZE); if (meta == MESSAGE_END) { active = 0; continue; } if ( meta != NULL ) { TIMING_START(); /* calculate data. send to ui thread */ #if 1 /* samples debug print */ PRINT_LOCK(params->params->print_mutex); print_header((PSSAMPLEHEADER)meta->left->buf, meta->left->buf + HEADER_SIZE, meta->left->size - HEADER_SIZE); print_header((PSSAMPLEHEADER)meta->right->buf, meta->right->buf + HEADER_SIZE, meta->right->size - HEADER_SIZE); print_header((PSSAMPLEHEADER)meta->left_fft->buf, meta->left_fft->buf + HEADER_SIZE, meta->left_fft->size - HEADER_SIZE); print_header((PSSAMPLEHEADER)meta->right_fft->buf, meta->right_fft->buf + HEADER_SIZE, meta->right_fft->size - HEADER_SIZE); print_header((PSSAMPLEHEADER)meta->sd_log->buf, meta->sd_log->buf + HEADER_SIZE, meta->sd_log->size - HEADER_SIZE); print_header((PSSAMPLEHEADER)meta->sd_mod->buf, meta->sd_mod->buf + HEADER_SIZE, meta->sd_mod->size - HEADER_SIZE); PRINT_UNLOCK(params->params->print_mutex); #endif #if 1 /* draw data */ glwin_draw_data(GRAPH_SAMPLES, (float *)(meta->left->buf + HEADER_SIZE), (float *)(meta->right->buf + HEADER_SIZE), ((PSSAMPLEHEADER)meta->right->buf)->samples); glwin_draw_data_c(GRAPH_FFT, (float *)(meta->left_fft->buf + HEADER_SIZE), (float *)(meta->right_fft->buf + HEADER_SIZE), ((PSSAMPLEHEADER)meta->right_fft->buf)->samples); glwin_draw_data(GRAPH_SDENS, (float *)(meta->sd_log->buf + HEADER_SIZE), (float *)(meta->sd_mod->buf + HEADER_SIZE), ((PSSAMPLEHEADER)meta->sd_mod->buf)->samples); glwin_render_data(); #endif metabuf_free(meta); TIMING_END(" updater"); } //buf_free(sample); } } return NULL; }