void malloc_test(struct thread_st *st) { struct block *bl; int i, b, r; struct lran2_st ld; /* data for random number generator */ unsigned long rsize[BINS_PER_BLOCK]; int rnum[BINS_PER_BLOCK]; lran2_init(&ld, st->u.seed); for(i=0; i<=st->u.max;) { #if TEST > 1 bin_test(); #endif bl = &blocks[RANDOM(&ld, n_blocks)]; r = RANDOM(&ld, 1024); if(r < 200) { /* free only */ mutex_lock(&bl->mutex); for(b=0; b<BINS_PER_BLOCK; b++) bin_free(&bl->b[b]); mutex_unlock(&bl->mutex); i += BINS_PER_BLOCK; } else { /* alloc/realloc */ /* Generate random numbers in advance. */ for(b=0; b<BINS_PER_BLOCK; b++) { rsize[b] = RANDOM(&ld, st->u.size) + 1; rnum[b] = lran2(&ld); } mutex_lock(&bl->mutex); for(b=0; b<BINS_PER_BLOCK; b++) bin_alloc(&bl->b[b], rsize[b], rnum[b]); mutex_unlock(&bl->mutex); i += BINS_PER_BLOCK; } #if TEST > 2 bin_test(); #endif } }
void malloc_test(struct thread_st *st) { int b, i, j, actions, pid = 1; struct bin_info p; struct lran2_st ld; /* data for random number generator */ lran2_init(&ld, st->u.seed); #if TEST_FORK>0 if(RANDOM(&ld, TEST_FORK) == 0) { int status; #if !USE_THR pid = fork(); #else pid = fork1(); #endif if(pid > 0) { /*printf("forked, waiting for %d...\n", pid);*/ waitpid(pid, &status, 0); printf("done with %d...\n", pid); if(!WIFEXITED(status)) { printf("child term with signal %d\n", WTERMSIG(status)); exit(1); } return; } exit(0); } #endif p.m = (struct bin *)malloc(st->u.bins*sizeof(*p.m)); p.bins = st->u.bins; p.size = st->u.size; for(b=0; b<p.bins; b++) { p.m[b].size = 0; p.m[b].ptr = NULL; if(RANDOM(&ld, 2) == 0) bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld)); } for(i=0; i<=st->u.max;) { #if TEST > 1 bin_test(&p); #endif actions = RANDOM(&ld, ACTIONS_MAX); #if USE_MALLOC && MALLOC_DEBUG if(actions < 2) { mallinfo(); } #endif for(j=0; j<actions; j++) { b = RANDOM(&ld, p.bins); bin_free(&p.m[b]); } i += actions; actions = RANDOM(&ld, ACTIONS_MAX); for(j=0; j<actions; j++) { b = RANDOM(&ld, p.bins); bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld)); #if TEST > 2 bin_test(&p); #endif } #if 0 /* Test illegal free()s while setting MALLOC_CHECK_ */ for(j=0; j<8; j++) { b = RANDOM(&ld, p.bins); if(p.m[b].ptr) { int offset = (RANDOM(&ld, 11) - 5)*8; char *rogue = (char*)(p.m[b].ptr) + offset; /*printf("p=%p rogue=%p\n", p.m[b].ptr, rogue);*/ free(rogue); } } #endif i += actions; } for(b=0; b<p.bins; b++) bin_free(&p.m[b]); free(p.m); if(pid == 0) exit(0); }
int correlateJ(const char *data_file, const char *corr_file, current_data *data1, current_data *data2) { int s = 0; size_t i, j; bin2d_workspace *bin2d_p; bin_workspace *bin_p; const double phi_min = -5.0; const double phi_max = 25.0; const size_t nphi = 7; const double t_min = -60.0; const double t_max = 60.0; const size_t nt = 2; FILE *fp_data, *fp_corr; double *dp, *x, *y, *r; size_t n = 0; double sigma; fp_data = fopen(data_file, "w"); fp_corr = fopen(corr_file, "w"); bin2d_p = bin2d_alloc(phi_min, phi_max, nphi, t_min, t_max, nt); bin_p = bin_alloc(phi_min, phi_max, nphi); dp = malloc(data1->n * sizeof(double)); x = malloc(data1->n * sizeof(double)); y = malloc(data1->n * sizeof(double)); r = malloc(data1->n * sizeof(double)); i = 1; fprintf(fp_data, "# Field %zu: timestamp of satellite 1 crossing (UT)\n", i++); fprintf(fp_data, "# Field %zu: delta t (minutes)\n", i++); fprintf(fp_data, "# Field %zu: delta phi (degrees)\n", i++); fprintf(fp_data, "# Field %zu: satellite 1 peak current (mA/m)\n", i++); fprintf(fp_data, "# Field %zu: satellite 2 peak current (mA/m)\n", i++); for (i = 0; i < data1->n; ++i) { curr_profile *prof = &(data1->profiles[i]); curr_profile *prof2; time_t dt; double dphi, dt_min; size_t idx; double lt = get_localtime(prof->t, prof->phi * M_PI / 180.0); s = find_profile_t(prof->t, data2, &idx); if (s) continue; prof2 = &(data2->profiles[idx]); if (prof->npeak != 1 || prof2->npeak != 1) continue; #if 0 if (prof->peak_J > 20.0) continue; /* a couple outliers */ #endif #if 0 if (lt > 20.0) continue; #endif dt = prof2->t - prof->t; dphi = wrap180(prof2->phi - prof->phi); dt_min = (double)dt / 60.0; #if 0 bin2d_add_element_corr(dphi, dt_min, prof->peak_J, prof2->peak_J, bin2d_p); #endif if (fabs(dt_min) <= 30.0 && dphi >= phi_min && dphi <= phi_max) { /* store this point */ x[n] = prof->peak_J; y[n] = prof2->peak_J; dp[n] = dphi; ++n; fprintf(fp_data, "%ld %f %f %f %f\n", prof->t, dt_min, dphi, prof->peak_J, prof2->peak_J); } } /* robust fit straight line to (x,y) */ { curvefit_workspace *curvefit_p = curvefit_alloc(gsl_multifit_robust_bisquare, curvefit_poly, n, 2); curvefit(0, x, y, curvefit_p); curvefit_residuals(x, y, r, curvefit_p); curvefit_free(curvefit_p); const double alpha = 2.5; sigma = gsl_stats_sd(r, 1, n); fprintf(stderr, "sigma = %f\n", sigma); /* loop through again and add (J1,J2) points which aren't outliers */ for (i = 0; i < n; ++i) { if (fabs(r[i]) > alpha*sigma) continue; bin_add_element_corr(dp[i], x[i], y[i], bin_p); } } #if 0 i = 1; fprintf(fp_corr, "# Field %zu: delta longitude (degrees)\n", i++); fprintf(fp_corr, "# Field %zu: delta t (minutes)\n", i++); fprintf(fp_corr, "# Field %zu: correlation\n", i++); fprintf(fp_corr, "# Field %zu: number of points in bin\n", i++); for (i = 0; i < nphi; ++i) { for (j = 0; j < nt; ++j) { double dt, dlon; size_t n; double r; bin2d_xyval(i, j, &dlon, &dt, bin2d_p); n = bin2d_n(dlon, dt, bin2d_p); r = bin2d_correlation(dlon, dt, bin2d_p); fprintf(fp_corr, "%f %f %f %zu\n", dlon, dt, r, n); } fprintf(fp_corr, "\n"); } #else i = 1; fprintf(fp_corr, "# Field %zu: delta longitude (degrees)\n", i++); fprintf(fp_corr, "# Field %zu: correlation\n", i++); fprintf(fp_corr, "# Field %zu: number of points in bin\n", i++); for (i = 0; i < nphi; ++i) { double dlon; size_t n; double r; bin_xval(i, &dlon, bin_p); n = bin_n(dlon, bin_p); r = bin_correlation(dlon, bin_p); fprintf(fp_corr, "%f %f %zu\n", dlon, r, n); } #endif fclose(fp_data); fclose(fp_corr); free(x); free(y); free(r); return s; }
/** * Creates a snapshots of the dialogs data and then calls the dumping function. * @returns 1 on success or 0 on failure */ int make_snapshot_dialogs() { bin_data x={0,0,0}; p_dialog *d; int i,k; time_t unique = time(0); FILE *f; switch (pcscf_persistency_mode) { case NO_PERSISTENCY: return 0; case WITH_FILES: f = bin_dump_to_file_create(pcscf_persistency_location,"pdialogs",unique); if (!f) return 0; for(i=0;i<p_dialogs_hash_size;i++){ if (!bin_alloc(&x,1024)) goto error; d_lock(i); d = p_dialogs[i].head; if (d){ while(d){ if (!bin_encode_p_dialog(&x,d)) goto error; d = d->next; } d_unlock(i); k = bind_dump_to_file_append(f,&x); if (k!=x.len) { LOG(L_ERR,"ERR:"M_NAME":make_snapshot_registrar: error while dumping to file - only wrote %d bytes of %d \n",k,x.len); d_unlock(i); bin_free(&x); return 0; } } else d_unlock(i); bin_free(&x); } return bind_dump_to_file_close(f,pcscf_persistency_location,"pdialogs",unique); break; case WITH_DATABASE_BULK: if (!bin_alloc(&x,1024)) goto error; for(i=0;i<p_dialogs_hash_size;i++){ d_lock(i); d = p_dialogs[i].head; while(d){ if (!bin_encode_p_dialog(&x,d)) goto error; d = d->next; } d_unlock(i); } return bin_dump_to_db(&x, P_DIALOGS); case WITH_DATABASE_CACHE: return bin_dump_to_db(NULL, P_DIALOGS); //ignore x, x is empty default: LOG(L_ERR,"ERR:"M_NAME":make_snapshot_registrar: Snapshot done but no such mode %d\n",pcscf_persistency_mode); return 0; } error: if (x.s) bin_free(&x); return 0; }
/** * Loads the subscriptions data from the last snapshot. * @returns 1 on success or 0 on failure */ int load_snapshot_subscriptions() { bin_data x; r_subscription *s; int k,max; FILE *f; switch (pcscf_persistency_mode){ case NO_PERSISTENCY: k=0; case WITH_FILES: f = bin_load_from_file_open(pcscf_persistency_location,"psubscriptions"); if (!f) return 0; bin_alloc(&x,128*1024); k=bin_load_from_file_read(f,&x); max = x.max; x.max=0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_subscriptions: max %d len %d\n",x.max,x.len); while(x.max<x.len){ s = bin_decode_r_subscription(&x); if (!s) return 0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_subscriptions: Loaded r_subscription for <%.*s>\n",s->req_uri.len,s->req_uri.s); subs_lock(s->hash); s->prev = subscriptions[s->hash].tail; s->next = 0; if (subscriptions[s->hash].tail) subscriptions[s->hash].tail->next = s; subscriptions[s->hash].tail = s; if (!subscriptions[s->hash].head) subscriptions[s->hash].head = s; subs_unlock(s->hash); memmove(x.s,x.s+x.max,x.len-x.max); x.len = x.max; x.max = max; k=bin_load_from_file_read(f,&x); max = x.max; x.max = 0; } bin_free(&x); bin_load_from_file_close(f); k = 1; break; case WITH_DATABASE_BULK: k=bin_load_from_db(&x, P_SUBSCRIPTIONS); x.max=0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_subscriptions: max %d len %d\n",x.max,x.len); while(x.max<x.len){ s = bin_decode_r_subscription(&x); if (!s) return 0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_subscriptions: Loaded r_subscription for <%.*s>\n",s->req_uri.len,s->req_uri.s); subs_lock(s->hash); s->prev = subscriptions[s->hash].tail; s->next = 0; if (subscriptions[s->hash].tail) subscriptions[s->hash].tail->next = s; subscriptions[s->hash].tail = s; if (!subscriptions[s->hash].head) subscriptions[s->hash].head = s; subs_unlock(s->hash); } bin_free(&x); break; case WITH_DATABASE_CACHE: k=bin_load_from_db(NULL, P_SUBSCRIPTIONS); //ignore x, x is empty break; default: LOG(L_ERR,"ERR:"M_NAME":load_snapshot_subscriptions: Can't resume because no such mode %d\n",pcscf_persistency_mode); k=0; } if (!k) goto error; return 1; error: return 0; }
/** * Loads the registrar data from the last snapshot. * @returns 1 on success or 0 on failure */ int load_snapshot_registrar() { bin_data x; r_contact *c; int k,max; FILE *f; switch (pcscf_persistency_mode){ case NO_PERSISTENCY: k=0; case WITH_FILES: f = bin_load_from_file_open(pcscf_persistency_location,"pregistrar"); if (!f) return 0; bin_alloc(&x,128*1024); k=bin_load_from_file_read(f,&x); max = x.max; x.max=0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_registrar: max %d len %d\n",x.max,x.len); while(x.max<x.len){ c = bin_decode_r_contact(&x); if (!c) return 0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_registrar: Loaded r_contact for <%.*s>\n",c->uri.len,c->uri.s); r_lock(c->hash); c->prev = registrar[c->hash].tail; c->next = 0; if (registrar[c->hash].tail) registrar[c->hash].tail->next = c; registrar[c->hash].tail = c; if (!registrar[c->hash].head) registrar[c->hash].head = c; r_unlock(c->hash); memmove(x.s,x.s+x.max,x.len-x.max); x.len -= x.max; x.max = max; k=bin_load_from_file_read(f,&x); max = x.max; x.max = 0; } bin_free(&x); bin_load_from_file_close(f); k = 1; break; case WITH_DATABASE_BULK: k=bin_load_from_db(&x, P_REGISTRAR); x.max=0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_registrar: max %d len %d\n",x.max,x.len); while(x.max<x.len){ c = bin_decode_r_contact(&x); if (!c) return 0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_registrar: Loaded r_contact for <%.*s>\n",c->uri.len,c->uri.s); r_lock(c->hash); c->prev = registrar[c->hash].tail; c->next = 0; if (registrar[c->hash].tail) registrar[c->hash].tail->next = c; registrar[c->hash].tail = c; if (!registrar[c->hash].head) registrar[c->hash].head = c; r_unlock(c->hash); } bin_free(&x); break; case WITH_DATABASE_CACHE: k=bin_load_from_db(NULL, P_REGISTRAR); //ignore x, x is empty break; default: LOG(L_ERR,"ERR:"M_NAME":load_snapshot_registrar: Can't resume because no such mode %d\n",pcscf_persistency_mode); k=0; } if (!k) goto error; return 1; error: return 0; }
/** * Loads the dialogs data from the last snapshot. * @returns 1 on success or 0 on failure */ int load_snapshot_dialogs() { bin_data x; p_dialog *d; int k,max; FILE *f; switch (pcscf_persistency_mode){ case NO_PERSISTENCY: k=0; case WITH_FILES: f = bin_load_from_file_open(pcscf_persistency_location,"pdialogs"); if (!f) return 0; bin_alloc(&x,128*1024); k=bin_load_from_file_read(f,&x); max = x.max; x.max=0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_dialogs: max %d len %d\n",x.max,x.len); while(x.max<x.len){ d = bin_decode_p_dialog(&x); if (!d) return 0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_dialogs: Loaded p_dialog for <%.*s>\n",d->host.len,d->host.s); d_lock(d->hash); d->prev = p_dialogs[d->hash].tail; d->next = 0; if (p_dialogs[d->hash].tail) p_dialogs[d->hash].tail->next = d; p_dialogs[d->hash].tail = d; if (!p_dialogs[d->hash].head) p_dialogs[d->hash].head = d; d_unlock(d->hash); memmove(x.s,x.s+x.max,x.len-x.max); x.len -= x.max; x.max = max; k=bin_load_from_file_read(f,&x); max = x.max; x.max = 0; } bin_free(&x); bin_load_from_file_close(f); k = 1; break; case WITH_DATABASE_BULK: k=bin_load_from_db(&x, P_DIALOGS); x.max=0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_dialogs: max %d len %d\n",x.max,x.len); while(x.max<x.len){ d = bin_decode_p_dialog(&x); if (!d) return 0; LOG(L_INFO,"INFO:"M_NAME":load_snapshot_dialogs: Loaded p_dialog for <%.*s>\n",d->host.len,d->host.s); d_lock(d->hash); d->prev = p_dialogs[d->hash].tail; d->next = 0; if (p_dialogs[d->hash].tail) p_dialogs[d->hash].tail->next = d; p_dialogs[d->hash].tail = d; if (!p_dialogs[d->hash].head) p_dialogs[d->hash].head = d; d_unlock(d->hash); } bin_free(&x); break; case WITH_DATABASE_CACHE: k=bin_load_from_db(NULL, P_DIALOGS); //ignore x, x is empty break; default: LOG(L_ERR,"ERR:"M_NAME":load_snapshot_dialogs: Can't resume because no such mode %d\n",pcscf_persistency_mode); k=0; } if (!k) goto error; return 1; error: return 0; }