static char* test_invalid_init() { struct hdr_histogram* h = NULL; mu_assert("Should not allow 0 as lowest trackable value", EINVAL == hdr_init(0, 64*1024, 2, &h)); mu_assert("Should have lowest < 2 * highest", EINVAL == hdr_init(80, 110, 5, &h)); return 0; }
static void load_histograms() { const int64_t highest_trackable_value = 3600L * 1000 * 1000; const int32_t significant_figures = 3; const int64_t interval = 10000L; const int64_t scale = 512; const int64_t scaled_interval = interval * scale; int i; if (raw_histogram) { free(raw_histogram); } hdr_alloc(highest_trackable_value, significant_figures, &raw_histogram); if (cor_histogram) { free(cor_histogram); } hdr_alloc(highest_trackable_value, significant_figures, &cor_histogram); if (scaled_raw_histogram) { free(scaled_raw_histogram); } hdr_init(1000, highest_trackable_value * 512, significant_figures, &scaled_raw_histogram); if (scaled_cor_histogram) { free(scaled_cor_histogram); } hdr_init(1000, highest_trackable_value * 512, significant_figures, &scaled_cor_histogram); for (i = 0; i < 10000; i++) { hdr_record_value(raw_histogram, 1000L); hdr_record_corrected_value(cor_histogram, 1000L, interval); hdr_record_value(scaled_raw_histogram, 1000L * scale); hdr_record_corrected_value(scaled_cor_histogram, 1000L * scale, scaled_interval); } hdr_record_value(raw_histogram, 100000000L); hdr_record_corrected_value(cor_histogram, 100000000L, 10000L); hdr_record_value(scaled_raw_histogram, 100000000L * scale); hdr_record_corrected_value(scaled_cor_histogram, 100000000L * scale, scaled_interval); }
/*=========================================================================== * FUNCTION - hdr_set_params - * * DESCRIPTION: *==========================================================================*/ int hdr_set_params(frame_proc_t *frameCtrl, frame_proc_set_hdr_data_t *data) { int rc = 0; uint32_t index = frameCtrl->handle & 0xFF; hdr_t *hdr = hdrCtrl[index]; switch (data->type) { case FRAME_PROC_HDR_ENABLE: frameCtrl->output.hdr_d.hdr_enable = data->hdr_init_info.hdr_enable; if (frameCtrl->output.hdr_d.hdr_enable) { rc = hdr_init(frameCtrl); //hdr->num_hal_buf = data->hdr_init_info.num_hal_buf; } else rc = hdr_exit(frameCtrl); break; case FRAME_PROC_HDR_HW_INFO: { /* memcpy(&hdr->pGammaTableStruct.gamma_tbl,frameCtrl->input.isp_info.RGB_gamma_table, frameCtrl->input.isp_info.VFE_GAMMA_NUM_ENTRIES*sizeof(int16_t));*/ hdr->pGammaTableStruct.gamma_tbl = frameCtrl->input.isp_info.RGB_gamma_table; hdr->pGammaTableStruct.entry = frameCtrl->input.isp_info.VFE_GAMMA_NUM_ENTRIES; hdr->pGammaTableStruct.gamma_t = GAMMA_TBL_ALL; CDBG_HIGH("%s Gamma Table entries %d",__func__,hdr->pGammaTableStruct.entry ); if (hdr->pGammaTableStruct.entry!=64) { CDBG_HIGH("Error: Invalid gamma table\n"); return -1; } rc = hdr_calculate_gammatbl(frameCtrl,hdr); } break; default: return -1; } return rc; } /* hdr_set_params */
static char* test_encode_and_decode_compressed_large() { const int64_t limit = INT64_C(3600) * 1000 * 1000; struct hdr_histogram* actual = NULL; struct hdr_histogram* expected = NULL; uint8_t* buffer = NULL; size_t len = 0; int rc = 0; hdr_init(1, limit, 4, &expected); srand(5); int i; for (i = 0; i < 8070; i++) { hdr_record_value(expected, rand() % limit); } rc = hdr_encode_compressed(expected, &buffer, &len); mu_assert("Did not encode", validate_return_code(rc)); rc = hdr_decode_compressed(buffer, len, &actual); mu_assert("Did not decode", validate_return_code(rc)); mu_assert("Loaded histogram is null", actual != NULL); mu_assert( "Comparison did not match", compare_histogram(expected, actual)); free(expected); free(actual); return 0; }
static char* test_create_with_large_values() { struct hdr_histogram* h = NULL; int r = hdr_init(20000000, 100000000, 5, &h); mu_assert("Didn't create", r == 0); hdr_record_value(h, 100000000); hdr_record_value(h, 20000000); hdr_record_value(h, 30000000); mu_assert( "50.0% Percentile", hdr_values_are_equivalent(h, 20000000, hdr_value_at_percentile(h, 50.0))); mu_assert( "83.33% Percentile", hdr_values_are_equivalent(h, 30000000, hdr_value_at_percentile(h, 83.33))); mu_assert( "83.34% Percentile", hdr_values_are_equivalent(h, 100000000, hdr_value_at_percentile(h, 83.34))); mu_assert( "99.0% Percentile", hdr_values_are_equivalent(h, 100000000, hdr_value_at_percentile(h, 99.0))); return 0; }
char* test_out_of_range_values() { struct hdr_histogram *h; hdr_init(1, 1000, 4, &h); mu_assert("Should successfully record value", hdr_record_value(h, 32767)); mu_assert("Should not record value", !hdr_record_value(h, 32768)); return 0; }
static char* test_create() { struct hdr_histogram* h = NULL; int r = hdr_init(1, 3600000000, 3, &h); size_t s = hdr_get_memory_size(h); mu_assert("Failed to allocate hdr_histogram", r == 0); mu_assert("Failed to allocate hdr_histogram", h != NULL); mu_assert("Incorrect array length", compare_int64(h->counts_len, 23552)); mu_assert("Size is incorrect", compare_int64(s, 188520)); free(h); return 0; }
static char* decode_v0_log() { const char* v1_log = "jHiccup-2.0.1.logV0.hlog"; FILE* f = fopen(v1_log, "r"); mu_assert("Can not open v1 log file", f != NULL); struct hdr_histogram* accum; hdr_init(1, INT64_C(3600000000000), 3, &accum); struct hdr_histogram* h = NULL; struct hdr_log_reader reader; hdr_timespec timestamp; hdr_timespec interval; hdr_log_reader_init(&reader); int rc = hdr_log_read_header(&reader, f); mu_assert("Failed to read header", rc == 0); int histogram_count = 0; int64_t total_count = 0; while ((rc = hdr_log_read(&reader, f, &h, ×tamp, &interval)) != EOF) { mu_assert("Failed to read histogram", rc == 0); histogram_count++; total_count += h->total_count; int64_t dropped = hdr_add(accum, h); mu_assert("Dropped events", compare_int64(dropped, 0)); free(h); h = NULL; } mu_assert("Wrong number of histograms", compare_int(histogram_count, 81)); mu_assert("Wrong total count", compare_int64(total_count, 61256)); mu_assert("99.9 percentile wrong", compare_int64(1510998015, hdr_value_at_percentile(accum, 99.9))); mu_assert("max value wrong", compare_int64(1569718271, hdr_max(accum))); mu_assert("Seconds wrong", compare_int64(1438869961, reader.start_timestamp.tv_sec)); mu_assert("Nanoseconds wrong", compare_int64(225000000, reader.start_timestamp.tv_nsec)); return 0; }
static char* decode_v3_log() { const char* v3_log = "jHiccup-2.0.7S.logV3.hlog"; FILE* f = fopen(v3_log, "r"); mu_assert("Can not open v3 log file", f != NULL); struct hdr_histogram* accum; hdr_init(1, INT64_C(3600000000000), 3, &accum); struct hdr_histogram* h = NULL; struct hdr_log_reader reader; hdr_timespec timestamp; hdr_timespec interval; hdr_log_reader_init(&reader); int rc = hdr_log_read_header(&reader, f); mu_assert("Failed to read header", validate_return_code(rc)); int histogram_count = 0; int64_t total_count = 0; while ((rc = hdr_log_read(&reader, f, &h, ×tamp, &interval)) != EOF) { mu_assert("Failed to read histogram", validate_return_code(rc)); histogram_count++; total_count += h->total_count; int64_t dropped = hdr_add(accum, h); mu_assert("Dropped events", compare_int64(dropped, 0)); free(h); h = NULL; } mu_assert("Wrong number of histograms", compare_int(histogram_count, 62)); mu_assert("Wrong total count", compare_int64(total_count, 48761)); mu_assert("99.9 percentile wrong", compare_int64(1745879039, hdr_value_at_percentile(accum, 99.9))); mu_assert("max value wrong", compare_int64(1796210687, hdr_max(accum))); mu_assert("Seconds wrong", compare_int64(1441812279, reader.start_timestamp.tv_sec)); mu_assert("Nanoseconds wrong", compare_int64(474000000, reader.start_timestamp.tv_nsec)); return 0; }
int main(int argc, char **argv) { struct hdr_histogram* histogram; int64_t max_value = 24 * 60 * 60 * 1000000L; int64_t min_value = 1; int result = -1; result = hdr_init(min_value, max_value, 4, &histogram); if (result != 0) { fprintf(stderr, "Failed to allocate histogram: %d\n", result); return -1; } struct timespec t0; struct timespec t1; setlocale(LC_NUMERIC, ""); int64_t iterations = 400000000; for (int i = 0; i < 100; i++) { hdr_gettime(&t0); for (int64_t j = 1; j < iterations; j++) { hdr_record_value(histogram, j); } hdr_gettime(&t1); struct timespec taken = diff(t0, t1); double time_taken = taken.tv_sec + taken.tv_nsec / 1000000000.0; double ops_sec = (iterations - 1) / time_taken; printf("%s - %d, ops/sec: %'.2f\n", "Iteration", i + 1, ops_sec); } return 0; }
int disk_open(disk_t *disk, const char *path, int fix, unsigned latency_graph_len) { memset(disk, 0, sizeof(*disk)); disk->fix = fix; INFO("Validating path %s", path); if (access(path, F_OK)) { ERROR("Disk path %s does not exist, errno=%d: %s", path, errno, strerror(errno)); return 1; } const int access_mode_flag = fix ? R_OK|W_OK : R_OK; if (access(path, access_mode_flag)) { ERROR("Disk path %s is inaccessible, errno=%d: %s", path, errno, strerror(errno)); return 1; } if (!disk_dev_open(&disk->dev, path)) { ERROR("Failed to open path %s, errno=%d: %s", path, errno, strerror(errno)); return 1; } if (disk_dev_read_cap(&disk->dev, &disk->num_bytes, &disk->sector_size) < 0) { ERROR("Can't get block device size information for path %s, errno=%d: %s", path, errno, strerror(errno)); goto Error; } if (disk->num_bytes == 0) { ERROR("Invalid number of sectors"); goto Error; } if (disk->sector_size == 0 || disk->sector_size % 512 != 0) { ERROR("Invalid sector size %" PRIu64, disk->sector_size); goto Error; } #if 0 const uint64_t new_bytes_raw = disk->num_bytes / 10; const uint64_t new_bytes_leftover = new_bytes_raw % 512; const uint64_t new_bytes = new_bytes_raw - new_bytes_leftover; disk->num_bytes = new_bytes; #endif if (disk_dev_identify(&disk->dev, disk->vendor, disk->model, disk->fw_rev, disk->serial, &disk->is_ata, disk->ata_buf, &disk->ata_buf_len) < 0) { ERROR("Can't identify disk for path %s, errno=%d: %s", path, errno, strerror(errno)); goto Error; } strncpy(disk->path, path, sizeof(disk->path)); disk->path[sizeof(disk->path)-1] = 0; hdr_init(1, 60*1000*1000, 3, &disk->histogram); disk->latency_graph_len = latency_graph_len; disk->latency_graph = calloc(latency_graph_len, sizeof(latency_t)); if (disk->latency_graph == NULL) { ERROR("Failed to allocate memory for latency graph data"); goto Error; } if (disk->is_ata) disk_ata_monitor_start(disk); else disk_scsi_monitor_start(disk); INFO("Opened disk %s sector size %"PRIu64" num bytes %"PRIu64, path, disk->sector_size, disk->num_bytes); return 0; Error: disk_close(disk); return 1; }
int main(int argc, char** argv) { struct timespec timestamp; struct timespec start_timestamp; struct timespec end_timestamp; struct hdr_interval_recorder recorder; struct hdr_log_writer log_writer; struct config_t config; pthread_t recording_thread; FILE* output = stdout; memset(&config, 0, sizeof(struct config_t)); if (!handle_opts(argc, argv, &config)) { printf("%s", USAGE); return 0; } if (config.filename) { output = fopen(config.filename, "a+"); if (!output) { fprintf( stderr, "Failed to open/create file: %s, %s", config.filename, strerror(errno)); return -1; } } if (0 != hdr_interval_recorder_init(&recorder)) { fprintf(stderr, "%s\n", "Failed to init phaser"); return -1; } if (0 != hdr_init( 1, INT64_C(24) * 60 * 60 * 1000000, 3, (struct hdr_histogram**) &recorder.active)) { fprintf(stderr, "%s\n", "Failed to init hdr_histogram"); return -1; } if (0 != hdr_init( 1, INT64_C(24) * 60 * 60 * 1000000, 3, (struct hdr_histogram**) &recorder.inactive)) { fprintf(stderr, "%s\n", "Failed to init hdr_histogram"); return -1; } if (pthread_create(&recording_thread, NULL, record_hiccups, &recorder)) { fprintf(stderr, "%s\n", "Failed to create thread"); return -1; } hdr_gettime(&start_timestamp); hdr_log_writer_init(&log_writer); hdr_log_write_header(&log_writer, output, "foobar", ×tamp); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmissing-noreturn" while (true) { sleep(config.interval); hdr_reset(recorder.inactive); struct hdr_histogram* h = hdr_interval_recorder_sample(&recorder); hdr_gettime(&end_timestamp); timestamp = start_timestamp; hdr_gettime(&start_timestamp); hdr_log_write(&log_writer, output, ×tamp, &end_timestamp, h); fflush(output); } #pragma clang diagnostic pop pthread_exit(NULL); }
static int hdr_decode_compressed_v2( _compression_flyweight* compression_flyweight, size_t length, struct hdr_histogram** histogram) { struct hdr_histogram* h = NULL; int result = 0; uint8_t* counts_array = NULL; _encoding_flyweight_v1 encoding_flyweight; z_stream strm; strm_init(&strm); if (inflateInit(&strm) != Z_OK) { FAIL_AND_CLEANUP(cleanup, result, HDR_INFLATE_FAIL); } int32_t compressed_length = be32toh(compression_flyweight->length); if (compressed_length < 0 || length - sizeof(_compression_flyweight) < (size_t)compressed_length) { FAIL_AND_CLEANUP(cleanup, result, EINVAL); } strm.next_in = compression_flyweight->data; strm.avail_in = (uInt) compressed_length; strm.next_out = (uint8_t *) &encoding_flyweight; strm.avail_out = sizeof(_encoding_flyweight_v1); if (inflate(&strm, Z_SYNC_FLUSH) != Z_OK) { FAIL_AND_CLEANUP(cleanup, result, HDR_INFLATE_FAIL); } int32_t encoding_cookie = get_cookie_base(be32toh(encoding_flyweight.cookie)); if (V2_ENCODING_COOKIE != encoding_cookie) { FAIL_AND_CLEANUP(cleanup, result, HDR_ENCODING_COOKIE_MISMATCH); } int32_t counts_limit = be32toh(encoding_flyweight.payload_len); int64_t lowest_trackable_value = be64toh(encoding_flyweight.lowest_trackable_value); int64_t highest_trackable_value = be64toh(encoding_flyweight.highest_trackable_value); int32_t significant_figures = be32toh(encoding_flyweight.significant_figures); if (hdr_init( lowest_trackable_value, highest_trackable_value, significant_figures, &h) != 0) { FAIL_AND_CLEANUP(cleanup, result, ENOMEM); } // Make sure there at least 9 bytes to read // if there is a corrupt value at the end // of the array we won't read corrupt data or crash. if ((counts_array = calloc(1, (size_t) counts_limit + 9)) == NULL) { FAIL_AND_CLEANUP(cleanup, result, ENOMEM); } strm.next_out = counts_array; strm.avail_out = (uInt) counts_limit; if (inflate(&strm, Z_FINISH) != Z_STREAM_END) { FAIL_AND_CLEANUP(cleanup, result, HDR_INFLATE_FAIL); } int r = _apply_to_counts_zz(h, counts_array, counts_limit); if (0 != r) { FAIL_AND_CLEANUP(cleanup, result, r); } h->normalizing_index_offset = be32toh(encoding_flyweight.normalizing_index_offset); h->conversion_ratio = int64_bits_to_double(be64toh(encoding_flyweight.conversion_ratio_bits)); hdr_reset_internal_counters(h); cleanup: (void)inflateEnd(&strm); free(counts_array); if (result != 0) { free(h); } else if (NULL == *histogram) { *histogram = h; } else { hdr_add(*histogram, h); free(h); } return result; }
calc_kernel(str_quake_params *eq,structopt *opt,sachdr *hd_synth,int nsac,char *itype,int nd,double *dv,double *tv, double ***G,FILE *o_log) { int i,j,ns,jsac,nsects,flag,flag2,ngfcomp=6,ierror=1 ; long int nerr ; char ori ; float *stlats,*stlons,*dists,*azs,*bazs,*xdegs ; double **GFs,*S,*TH,*PH,*x_conv ; double gcarc,Ptt,twp_beg,twp_end, *ref_vm=NULL ; double *b1,*b2,*a1,*a2,gain,dt=1.; sachdr hdr; make_chan_list(hd_synth,nsac,&stlats,&stlons); /* Memory Allocations */ if (opt->ref_flag) { ref_vm = double_alloc(NM); for(i=0;i<NM;i++) ref_vm[i] = eq->vm[1][i] ; } dists = float_calloc(nsac) ; azs = float_calloc(nsac) ; bazs = float_calloc(nsac) ; xdegs = float_calloc(nsac) ; GFs = double_alloc2(10,__LEN_SIG__) ;/* GFs: Rrr, Rtt, Rpp, Rrt */ S = double_alloc(__LEN_SIG__) ;/* Vertical components */ TH = double_alloc(__LEN_SIG__) ;/* Radial components */ PH = double_alloc(__LEN_SIG__) ;/* Transverse components */ x_conv = double_alloc(__LEN_SIG__) ; hdr_init(&hdr) ; /* SAC header allocation */ nsects = (eq->flow > 0.)? eq->filtorder : eq->filtorder/2 ; b1 = double_alloc(nsects) ; b2 = double_alloc(nsects) ; a1 = double_alloc(nsects) ; a2 = double_alloc(nsects) ; /* Distance, azimuth, back-azimuth, etc */ distaz(eq->evla, eq->evlo, stlats, stlons, nsac, dists, azs, bazs, xdegs, &nerr) ; flag = 0 ; for(i=0;i<ngfcomp;i++) /* Main loop */ { ns = 0 ; /* Channel counter */ for(j=0;j<ngfcomp;j++) /* Inititializing the MT components */ eq->vm[1][j] = 0. ; eq->vm[1][i] = 1. ; for(jsac=0;jsac<nsac;jsac++) { gcarc = (double)hd_synth[jsac].gcarc; trav_time(gcarc,tv,dv,nd,&Ptt,&ierror) ; wp_time_window(gcarc,eq->wp_win4,&twp_beg,&twp_end) ; /* Data Time Window */ flag2 = 0; ori = hd_synth[ns].kcmpnm[2]; if ( ori == 'Z' ) fast_synth_only_Z_sub(azs[jsac],bazs[jsac],xdegs[jsac], tv,dv,nd,eq,&hdr,GFs,S); else if ( ori == 'N' || ori == 'E' || ori == '1' || ori == '2' ) { fast_synth_only_Hs_sub(azs[jsac],bazs[jsac],xdegs[jsac],tv,dv,nd,eq,&hdr,GFs,TH,PH); rotate_traces(TH, PH, hd_synth[jsac].baz-hd_synth[jsac].cmpaz, hdr.npts, S) ; /*Rotating TH, PH to H*/ } else continue; conv_by_stf(eq->ts,eq->hd,itype,&hdr,S,x_conv) ;/* Perform convolution */ if (flag == 0) /* Set the butterworth sos (dt must be the same for all stations) */ { flag = 1 ; dt = (double)hdr.delta; if (eq->flow>0.) bpbu2sos(eq->flow,eq->fhigh,dt,eq->filtorder,&gain,b1,b2,a1,a2); else lpbu2sos(eq->fhigh,dt,eq->filtorder,&gain,b1,b2,a1,a2); } else if (dt != (double)hdr.delta) /* Check the sampling frequency (must be uniform) */ { fprintf(stderr, "*** ERROR: non uniform samp. period between sac files (%s)\n",hd_synth[jsac].kstnm); fprintf(stderr," -> Exiting\n") ; fflush(stderr); exit(1); } filter_with_sos(gain,b1,b2,a1,a2,nsects,x_conv,hdr.npts) ; /* Apply sos */ flag2 = fill_kernel_G(&hdr,&(hd_synth[jsac]),Ptt,twp_beg,twp_end,x_conv,G[jsac][i],opt,o_log); if (flag2) { fprintf(stderr,"*** ERROR: Incomplete green function for %s\n",hd_synth[jsac].kstnm) ; fprintf(stderr," -> Exiting\n") ; fflush(stderr); exit(1); } hd_synth[jsac].b = hdr.b + hd_synth[jsac].o - hdr.o; ns++; } /*endfor nsac*/ if (nsac!=ns) { fprintf(stderr,"\n*** ERROR: Kernel G is incomplete (%d vs %d)\n",nsac,ns); fprintf(stderr," -> Exiting\n"); fflush(stderr); exit(1); } } /*endfor ngfcomp*/ /* Free Memory */ if (opt->ref_flag) { for(i=0;i<NM;i++) eq->vm[1][i] = ref_vm[i] ; free((void*)ref_vm); } free((void*)stlats) ; free((void*)stlons) ; free((void*)dists) ; free((void*)xdegs) ; free((void*)bazs) ; free((void*)azs) ; for(i=0;i<10;i++) free((void*)GFs[i]); free((void**)GFs) ; free((void*)x_conv) ; free((void*)S) ; free((void*)TH) ; free((void*)PH) ; free((void*)a1) ; free((void*)a2) ; free((void*)b1) ; free((void*)b2) ; }
int main(int argc, char **argv) { struct sigaction action; char *pps_filename; char *lat_filename; uint64_t expected_packets; socklen_t len = 4; int enabled = 1; int val = 0; unsigned char *buffers = (unsigned char *)malloc(BUFFER_SIZE * NUM_BUFFERS); pps = (unsigned int *)malloc(sizeof(unsigned int) * MAX_SECONDS); memset(pps, 0, sizeof(unsigned int) * MAX_SECONDS); if (!handle_opts(argc, argv, &expected_packets, &pps_filename, &lat_filename)) { printf("%s", USAGE); return 0; } if (pps_filename) { pps_output = fopen(pps_filename, "a+"); if (!pps_output) ERROR("Failed to open/create file: %s, %s", pps_filename, strerror(errno)); } else { pps_output = stdout; } if (lat_filename) { lat_output = fopen(lat_filename, "a+"); if (!lat_output) ERROR("Failed to open/create file: %s, %s", lat_filename, strerror(errno)); } else { lat_output = stdout; } if (hdr_init(LOWEST_LAT_US, HIGHEST_LAT_US, 4, &hist) != 0) { ERROR("Failed to init histogram"); return -1; } sock_raw = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sock_raw < 0) ERROR("Socket error: %s", strerror(errno)); if (setsockopt(sock_raw, SOL_SOCKET, SO_TIMESTAMPNS, &enabled, sizeof(enabled)) < 0 || getsockopt(sock_raw, SOL_SOCKET, SO_TIMESTAMPNS, &val, &len) < 0 || val == 0) { ERROR("Failed to configure rx timestamps: %s", strerror(errno)); } memset(&action, 0, sizeof(struct sigaction)); action.sa_handler = on_signal; sigaction(SIGINT, &action, NULL); if (expected_packets == 0) expected_packets = ~UINT64_C(0); while (received_packets < expected_packets) { int npkts = rcv_packet(sock_raw, buffers, hist); if (npkts == 0) continue; if (start == -1) start = time(NULL); uint64_t second = time(NULL) - start; pps[second] += npkts; received_packets += npkts; } report(); return 0; }
static int hdr_decode_compressed_v0( _compression_flyweight* compression_flyweight, size_t length, struct hdr_histogram** histogram) { struct hdr_histogram* h = NULL; int result = 0; uint8_t* counts_array = NULL; _encoding_flyweight_v0 encoding_flyweight; z_stream strm; strm_init(&strm); if (inflateInit(&strm) != Z_OK) { FAIL_AND_CLEANUP(cleanup, result, HDR_INFLATE_FAIL); } int32_t compressed_length = be32toh(compression_flyweight->length); if (compressed_length < 0 || length - sizeof(_compression_flyweight) < (size_t)compressed_length) { FAIL_AND_CLEANUP(cleanup, result, EINVAL); } strm.next_in = compression_flyweight->data; strm.avail_in = (uInt) compressed_length; strm.next_out = (uint8_t *) &encoding_flyweight; strm.avail_out = sizeof(_encoding_flyweight_v0); if (inflate(&strm, Z_SYNC_FLUSH) != Z_OK) { FAIL_AND_CLEANUP(cleanup, result, HDR_INFLATE_FAIL); } int32_t encoding_cookie = get_cookie_base(be32toh(encoding_flyweight.cookie)); if (V0_ENCODING_COOKIE != encoding_cookie) { FAIL_AND_CLEANUP(cleanup, result, HDR_ENCODING_COOKIE_MISMATCH); } int32_t word_size = word_size_from_cookie(be32toh(encoding_flyweight.cookie)); int64_t lowest_trackable_value = be64toh(encoding_flyweight.lowest_trackable_value); int64_t highest_trackable_value = be64toh(encoding_flyweight.highest_trackable_value); int32_t significant_figures = be32toh(encoding_flyweight.significant_figures); if (hdr_init( lowest_trackable_value, highest_trackable_value, significant_figures, &h) != 0) { FAIL_AND_CLEANUP(cleanup, result, ENOMEM); } int32_t counts_array_len = h->counts_len * word_size; if ((counts_array = calloc(1, (size_t) counts_array_len)) == NULL) { FAIL_AND_CLEANUP(cleanup, result, ENOMEM); } strm.next_out = counts_array; strm.avail_out = (uInt) counts_array_len; if (inflate(&strm, Z_FINISH) != Z_STREAM_END) { FAIL_AND_CLEANUP(cleanup, result, HDR_INFLATE_FAIL); } _apply_to_counts(h, word_size, counts_array, h->counts_len); hdr_reset_internal_counters(h); h->normalizing_index_offset = 0; h->conversion_ratio = 1.0; cleanup: (void)inflateEnd(&strm); free(counts_array); if (result != 0) { free(h); } else if (NULL == *histogram) { *histogram = h; } else { hdr_add(*histogram, h); free(h); } return result; }
int main(int argc, char *argv[]) { int i, j, ns, flag, flagr, ierror, nsects, nh=NDEPTHS, nd=NDISTAS ; long int nerr ; double **GFs,*S,*TH,*PH,*x_conv ; double *b1, *b2, *a1, *a2, gain, dt=1.0 ; double *tv, *dv ; float dist,az,baz,xdeg; char i_master[FSIZE], i_wpfilname[FSIZE], datafile[FSIZE], buf[200] ; char o_dir[FSIZE], *o_file,stnm[9],netwk[9],cmpnm[9], khole[9]; char stacmp[]= {'Z','N','E','1','2'} ; char itype[2]="l", ori; str_quake_params eq ; sachdr hd_data, hd_synt ; FILE *i_wp ; /* Input params */ if (argc < 5) { fprintf(stderr,"Error input params \n"); fprintf(stderr,"Syntax : %s i_master cmtfile i_wpinversion o_direct [stftype]\n", argv[0]); fprintf(stderr,"stftype (optionnal) can be either:\n g (gaussian),\n q (parabolic),\n l (triangle,\n default),\n b(boxcar) or\n c (cosine)\n"); exit(1); } strcpy( i_master, argv[1]) ; strcpy(i_wpfilname, argv[3]) ; strcpy( o_dir, argv[4]) ; get_params(i_master, &eq) ; strcpy( eq.cmtfile, argv[2]) ; if (argc==6) { if (strlen(argv[5])==1) strcpy(itype,argv[5]); else { fprintf(stderr,"Error input params \n"); fprintf(stderr,"Syntax : %s i_master cmtfile i_wpinversion o_direct [stftype]\n", argv[0]); fprintf(stderr,"stftype (optionnal) can be either:\n g (gaussian),\n q (parabolic),\n l (triangle,\n default),\n b(boxcar) or\n c (cosine)\n"); exit(1); } } /* Allocates memory */ eq.vm = double_alloc2p(2) ; eq.vm[0] = double_calloc(6) ; eq.vm[1] = double_calloc(6) ; GFs = double_alloc2(10,__LEN_SIG__) ;/* GFs: Rrr, Rtt, Rpp, Rrt */ S = double_alloc(__LEN_SIG__) ;/* Vertical components */ TH = double_alloc(__LEN_SIG__) ;/* Radial components */ PH = double_alloc(__LEN_SIG__) ;/* Transverse components */ x_conv = double_alloc(__LEN_SIG__) ; hdr_init(&hd_data) ; hdr_init(&hd_synt) ; nsects = (eq.flow > 0.)? eq.filtorder : eq.filtorder/2 ; b1 = double_alloc(nsects) ; b2 = double_alloc(nsects) ; a1 = double_alloc(nsects) ; a2 = double_alloc(nsects) ; tv = double_alloc(nd); /* travel times */ dv = double_alloc(nd); /* distances */ /* Read CMTFILE */ get_cmtf(&eq,2) ; /* Set travel time table for depth = dep */ ierror = 1 ; trav_time_init(nh,nd,eq.evdp,dv,tv,&ierror) ; /* Read list of data files */ flag = 0 ; i_wp = openfile_rt(i_wpfilname, &ns); for(i=0; i<ns; i++) { flagr = fscanf (i_wp, "%s", datafile) ; fgets(buf,200,i_wp); /* end of line */ check_scan(1, flagr, i_wpfilname, i_wp) ; rhdrsac(datafile, &hd_data, &ierror) ; /* Calculate azimuths, back-azimuths */ dist = 0. ; az = 0. ; baz = 0. ; xdeg = 0. ; distaz(eq.evla,eq.evlo,&hd_data.stla,&hd_data.stlo,1,&dist,&az,&baz,&xdeg,&nerr) ; ori = hd_data.kcmpnm[2]; if ( ori == 'Z' ) fast_synth_only_Z_sub(az,baz,xdeg, tv,dv,nd,&eq,&hd_synt,GFs,S); else if ( ori == 'N' || ori == 'E' || ori == '1' || ori == '2' ) { fast_synth_only_Hs_sub(az,baz,xdeg,tv,dv,nd,&eq,&hd_synt,GFs,TH,PH); rotate_traces(TH, PH, baz-hd_data.cmpaz,hd_synt.npts, S) ; /*Rotating TH, PH to H*/ } else continue; sscanf(hd_data.kstnm,"%s",stnm); sscanf(hd_data.knetwk,"%s",netwk); sscanf(hd_data.kcmpnm,"%s",cmpnm); strcpy(khole, hd_data.khole); // It can contain blanks for(j=0; j<5; j++) { if (cmpnm[2] == stacmp[j]) break; } if (j==5) { fprintf(stderr,"*** ERROR: Unknownk component %s for sta %s\n",cmpnm,stnm) ; fprintf(stderr," -> Exiting\n") ; fflush(stderr); exit(1); } conv_by_stf(eq.ts,eq.hd,itype,&hd_synt,S,x_conv) ;/* Perform convolution */ strcpy(hd_synt.kstnm,hd_data.kstnm) ; strcpy(hd_synt.kcmpnm,hd_data.kcmpnm) ; strcpy(hd_synt.knetwk,hd_data.knetwk) ; hd_synt.stla = hd_data.stla ; hd_synt.stlo = hd_data.stlo ; hd_synt.evla = eq.pde_evla; hd_synt.evlo = eq.pde_evlo; hd_synt.evdp = eq.pde_evdp; /* Write output file 1 */ o_file = get_gf_filename(o_dir,stnm,netwk,cmpnm,khole,".complete_synth.sac") ; wsac(o_file,&hd_synt,x_conv); free((void*)o_file) ; if (flag == 0) /* Set the butterworth sos (dt must be the same for all stations) */ { flag = 1 ; dt = (double)hd_data.delta; if (eq.flow>0.) bpbu2sos(eq.flow,eq.fhigh,dt,eq.filtorder,&gain,b1,b2,a1,a2); else lpbu2sos(eq.fhigh,dt,eq.filtorder,&gain,b1,b2,a1,a2); } else if ((int)(dt*1000+0.5) != (int)((double)hd_data.delta*1000+0.5)) { fprintf(stderr, "ERROR: non uniform samp. period between sac files, file : %s\n",datafile); exit(1); } filter_with_sos(gain,b1,b2,a1,a2,nsects,x_conv,hd_synt.npts) ; /* Apply sos */ /* Write output file 2 */ o_file = get_gf_filename(o_dir,stnm,netwk,cmpnm,khole,".complete_synth.bp.sac") ; printf("Writing sac file : %s\n",o_file) ; wsac(o_file,&hd_synt,x_conv); free((void*)o_file) ; } fclose(i_wp); free((void*)S); free((void*)TH); free((void*)PH); for(j=0; j<10; j++) free((void*)GFs[j]); free((void**)GFs); free((void*)x_conv); free((void*)b1); free((void*)b2); free((void*)a1); free((void*)a2); return 0; }
int main(int argc, char **argv) { int i,j,flag,jchan,nchans,ngfcomp=6,nsects,ierror=1 ; int tapering=NON,nh=NDEPTHS,nd=NDISTAS ; long int nerr = 0 ; char stat_file[FSIZE],i_master[FSIZE],path[FSIZE]; char sacfile[FSIZE],itype[2], ori; char *gfcomp[]={"rr","tt","pp","rt","rp","tp"}; char **stats, **nets, **cmps, **locs ; float *stlats,*stlons,*dists,*cmpazs, *azs,*bazs,*xdegs ; double **GFs,*S,*TH,*PH,*x_conv,*tv,*dv ; double *b1,*b2,*a1,*a2,gain,dt=1.; str_quake_params eq; sachdr hdr; /* Set input parameters */ get_params(argc, argv, stat_file, itype, i_master, &eq, &tapering) ; get_cmtf(&eq, 1) ; /* Allocations */ GFs = double_alloc2(10,__LEN_SIG__);/* GFs: Rrr, Rtt, Rpp, Rrt */ TH = double_alloc(__LEN_SIG__) ; /* Radial components */ PH = double_alloc(__LEN_SIG__) ; /* Transverse components */ S = double_alloc(__LEN_SIG__) ; /* work copy */ x_conv = double_alloc((int)__LEN_SIG__) ; eq.vm = double_alloc2p(2) ; eq.vm[1] = double_alloc(6) ; eq.vm[0] = double_alloc(6) ; hdr_init(&hdr) ; /* SAC header allocation */ nsects = (eq.flow > 0.)? eq.filtorder : eq.filtorder/2 ; b1 = double_alloc(nsects) ; b2 = double_alloc(nsects) ; a1 = double_alloc(nsects) ; a2 = double_alloc(nsects) ; tv = double_alloc(nd); /* travel times */ dv = double_alloc(nd); /* distances */ /* Reading CMTSOLUTION and STAT_FILE */ nchans = r_scr_dat_fil_list(stat_file, &stats, &nets, &cmps, &locs, &stlats, &stlons,&cmpazs) ; dists = float_calloc(nchans) ; azs = float_calloc(nchans) ; bazs = float_calloc(nchans) ; xdegs = float_calloc(nchans) ; /* Distance, azimuth, back-azimuth, etc */ distaz(eq.evla, eq.evlo, stlats, stlons, nchans, dists, azs, bazs, xdegs, &nerr) ; /* Set travel time table for depth = dep */ trav_time_init(nh,nd,eq.evdp,dv,tv,&ierror); /* Excitation kernels calculation */ crea_dir(eq.gf_dir) ; flag = 0 ; for(i=0;i<ngfcomp;i++) { printf("**************************************\n"); printf("Computing synthetics for M_%s...\n",gfcomp[i]); strcpy(path,eq.gf_dir) ; strcat(path,"gf_") ; strcat(path,gfcomp[i]) ; crea_dir(path) ; strcat(path,"/") ; for(j=0;j<ngfcomp;j++)/* Inititializing the MT components */ eq.vm[1][j] = 0. ; eq.vm[1][i] = 1. ; for(jchan=0;jchan<nchans;jchan++) /* Computing kernels for MT component #i at each station */ { flag = 0; /* Computing Z, TH, PH */ ori = cmps[jchan][2]; printf("%-5s", stats[jchan]) ; if ( ori == 'Z' ) { fast_synth_only_Z_sub(azs[jchan], bazs[jchan], xdegs[jchan], tv,dv,nd,&eq,&hdr,GFs,S); hdr.cmpaz = 0.; hdr.cmpinc = 0.; } else if ( ori == 'N' || ori == 'E' || ori == '1' || ori == '2' ) { fast_synth_only_Hs_sub(azs[jchan], bazs[jchan], xdegs[jchan],tv,dv,nd,&eq,&hdr,GFs,TH,PH); rotate_traces(TH, PH, bazs[jchan]-cmpazs[jchan], hdr.npts, S) ; /*Rotating TH, PH to H*/ hdr.cmpaz = cmpazs[jchan]; hdr.cmpinc = 90.; } else continue; if (tapering == YES) taper_one_trace(S,&hdr); strcpy(sacfile,path) ; /* Save Raw GF SAC */ strcat(sacfile,stats[jchan]) ; strcat(sacfile,".") ; strcat(sacfile,nets[jchan]) ; strcat(sacfile,".") ; strcat(sacfile,cmps[jchan]) ; strcat(sacfile,".") ; strcat(sacfile,locs[jchan]) ; strcat(sacfile,".SAC") ; save_gf_sac(sacfile,stats[jchan],nets[jchan],cmps[jchan],locs[jchan],&stlats[jchan],&stlons[jchan],&hdr,S) ; conv_by_stf(eq.ts,eq.hd,itype,&hdr,S,x_conv) ;/* Perform convolution */ if (flag == 0) /* Set the butterworth sos (dt must be the same for all stations) */ { flag = 1 ; dt = (double)hdr.delta ; if (eq.flow>0.) bpbu2sos(eq.flow,eq.fhigh,dt,eq.filtorder,&gain,b1,b2,a1,a2); else lpbu2sos(eq.fhigh,dt,eq.filtorder,&gain,b1,b2,a1,a2); } else if (dt != (double)hdr.delta) { fprintf(stderr, "ERROR: non uniform samp. period between sac files, file : %s\n",sacfile); fprintf(stderr, "%f != %f\n", dt, hdr.delta); exit(1); } strcat(sacfile,".sac") ; /* Save SAC after STF convolution */ save_gf_sac(sacfile,stats[jchan],nets[jchan],cmps[jchan],locs[jchan],&stlats[jchan],&stlons[jchan],&hdr,x_conv) ; filter_with_sos(gain,b1,b2,a1,a2,nsects,x_conv,hdr.npts) ; /* Apply sos */ strcat(sacfile,".bp") ; /* Save SAC after bandpass filtering */ save_gf_sac(sacfile,stats[jchan],nets[jchan],cmps[jchan],locs[jchan],&stlats[jchan],&stlons[jchan],&hdr,x_conv) ; } printf("\n"); } /* Freeing memory */ free((void*)b1) ; free((void*)b2) ; free((void*)a1) ; free((void*)a2) ; free((void*)S) ; free((void*)PH); free((void*)TH); free((void*)x_conv); for(i=0; i<10; i++) free((void *)GFs[i]) ; free((void**)GFs) ; free((void*)eq.vm[0]) ; free((void*)eq.vm[1]) ; free((void**)eq.vm) ; free((void*)dists) ; free((void*)azs) ; free((void*)bazs) ; free((void*)xdegs) ; for (i=0;i< nchans;i++) { free((void*)stats[i]) ; free((void*)nets[i]) ; } free((void**)stats) ; free((void**)nets) ; free((void*)stlats) ; free((void*)stlons) ; free((void*)cmpazs) ; if(tapering == YES) { free((void*)tv); free((void*)dv); } printf("\n"); return 0; }
Histogram(ThreadState* thread_state) : thread_state_(thread_state) , histograms_(new PerThreadHistogram[thread_state->max_threads()]) { hdr_init(1LL, HIGHEST_TRACKABLE_VALUE, 3, &histogram_); uv_mutex_init(&mutex_); }
PerThreadHistogram() : active_index_(0) { hdr_init(1LL, HIGHEST_TRACKABLE_VALUE, 3, &histograms_[0]); hdr_init(1LL, HIGHEST_TRACKABLE_VALUE, 3, &histograms_[1]); }
PerThreadHistogram() { hdr_init(1LL, HIGHEST_TRACKABLE_VALUE, 3, &histogram_); }