void run(void (*action)(void*)) { short* ai_buffer = calloc(NSHORTS, sizeof(short)); unsigned tl0 = 0xdeadbeef; /* always run one dummy loop */ unsigned tl1; unsigned sample; int println = 0; mlockall(MCL_CURRENT); memset(host_buffer, 0, VI_LEN); if (!dummy_first_loop){ *TLATCH = tl0; } for (sample = 0; sample <= nsamples; ++sample, tl0 = tl1){ memcpy(ai_buffer, host_buffer, VI_LEN); while((tl1 = *TLATCH) == tl0){ sched_yield(); memcpy(ai_buffer, host_buffer, VI_LEN); } control(ao_buffer, ai_buffer); action(ai_buffer); if (verbose){ print_sample(sample, tl1); } } }
int main(void) { header hdr_1; header hdr_2; read_header(&hdr_1); read_header(&hdr_2); sample data[MAX_ROWS]; size_t numrows = read_samples(MAX_ROWS, data); print_header("H1", &hdr_1); print_header("H2", &hdr_2); printf("Number of rows: %zu\n", numrows); for (size_t i = 0; i < numrows; i++) print_sample(i, &data[i]); return 0; }
void clamav_stats_submit(struct cl_engine *engine, void *cbdata) { char *json; cli_intel_t *intel, myintel; cli_flagged_sample_t *sample, *next; int err; intel = (cli_intel_t *)cbdata; if (!(intel) || !(engine)) return; if (engine->dconf->stats & DCONF_STATS_DISABLED) return; if (!(engine->cb_stats_get_hostid)) { /* Submitting stats is disabled due to HostID being turned off */ if ((engine->cb_stats_flush)) engine->cb_stats_flush(engine, cbdata); return; } cli_dbgmsg("stats - start\n"); #ifdef CL_THREAD_SAFE err = pthread_mutex_lock(&(intel->mutex)); if (err) { cli_warnmsg("clamav_stats_submit: locking mutex failed (err: %d): %s\n", err, strerror(err)); if ((intel->engine) && (intel->engine->cb_stats_flush)) intel->engine->cb_stats_flush(intel->engine, cbdata); return; } #endif /* Empty out the cached intelligence data so that other threads don't sit waiting to add data to the cache */ memcpy(&myintel, intel, sizeof(cli_intel_t)); intel->samples = NULL; intel->nsamples = 0; json = export_stats_to_json(engine, &myintel); #ifdef CL_THREAD_SAFE err = pthread_mutex_unlock(&(intel->mutex)); if (err) { cli_warnmsg("clamav_stats_submit: unlocking mutex failed (err: %d): %s\n", err, strerror(err)); } #endif for (sample=myintel.samples; sample != NULL; sample = next) { #if DEBUG_STATS print_sample(sample); #endif next = sample->next; free_sample(sample); } if (json) { submit_post(STATS_HOST, STATS_PORT, "PUT", "/clamav/1/submit/stats", json, myintel.timeout); free(json); } if (myintel.hostid && !(intel->hostid)) { free(myintel.hostid); myintel.hostid = NULL; } cli_dbgmsg("stats - end\n"); }
/* all-in-one function testing epidemics growth, summary statistics, etc. */ void test_epidemics(int seqLength, double mutRate, int npop, int *nHostPerPop, double beta, int nStart, int t1, int t2, int Nsample, int *Tsample, int duration, int *nbnb, int *listnb, double *pdisp){ int i, j, nstep=0, tabidx, counter_sample = 0; /* Initialize random number generator */ time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = seqLength; par->mu = mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = npop; par->npop = npop; par->popsizes = nHostPerPop; par->beta = beta; par->nstart = nStart; par->t1 = t1; par->t2 = t2; par->t_sample = Tsample; par->n_sample = Nsample; par->duration = duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample ** samplist = (struct sample **) malloc(tabdates->n * sizeof(struct sample *)); struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw samples */ if((tabidx = int_in_vec(nstep, tabdates->items, tabdates->n)) > -1){ /* TRUE if step must be sampled */ samplist[counter_sample++] = draw_sample(metapop, tabdates->times[tabidx], par); } fill_ts_groupsizes(grpsizes, metapop, nstep); } /* we stopped after 'nstep' steps */ if(nstep < par->duration){ printf("\nEpidemics ended at time %d, before last sampling time (%d).\n", nstep, par->duration); } else { printf("\n\n-- FINAL METAPOPULATION --"); print_metapopulation(metapop, TRUE); /* test samples */ for(i=0;i<tabdates->n;i++) { printf("\nsample %d\n", i); print_sample(samplist[i], TRUE); } samp = merge_samples(samplist, tabdates->n, par) ; print_sample(samp, TRUE); /* test allele listing */ struct snplist *snpbilan; snpbilan = list_snps(samp, par); print_snplist(snpbilan); /* test allele frequencies */ struct allfreq *freq; freq = get_frequencies(samp, par); print_allfreq(freq); /* test Hs*/ double Hs = hs(samp,par); printf("\nHs = %0.3f\n", Hs); /* test Hs full genome */ Hs = hs_full_genome(samp,par); printf("\nHs (full genome) = %0.5f\n", Hs); /* test nb of snps */ int nball = nb_snps(samp,par); printf("\nnumber of SNPs = %d\n", nball); /* test mean nb of snps */ double temp = mean_nb_snps(samp); printf("\nmean number of SNPs = %.2f\n", temp); /* test var nb of snps */ temp = var_nb_snps(samp); printf("\nvariance of number of alleles = %.2f\n", temp); /* test pairwise distances */ struct distmat_int *mat = pairwise_dist(samp, par); print_distmat_int(mat); /* test mean pairwise distances */ temp = mean_pairwise_dist(samp,par); printf("\nmean pairwise distance: %.2f", temp); /* test variance of pairwise distances */ temp = var_pairwise_dist(samp,par); printf("\nvar pairwise distance: %.2f", temp); /* test Fst */ temp = fst(samp,par); printf("\nfst: %.2f", temp); printf("\n\n"); /* free memory */ free_sample(samp); free_snplist(snpbilan); free_allfreq(freq); free_distmat_int(mat); } /* write group sizes to file */ printf("\n\nPrinting group sizes to file 'out-popsize.txt'"); write_ts_groupsizes(grpsizes); /* free memory */ free_metapopulation(metapop); free_param(par); for(i=0;i<counter_sample;i++) free_sample(samplist[i]); free(samplist); free_table_int(tabdates); free_network(cn); free_ts_groupsizes(grpsizes); }
static void * gtick(void *arg) { struct tstate *state = arg; char *errstr; uint_t nsamples; uint_t sample_cnt = 1; hrtime_t ht, htdelta, restdelta; cpc_setgrp_t *sgrp = state->sgrp; cpc_set_t *this = cpc_setgrp_getset(sgrp); const char *name = cpc_setgrp_getname(sgrp); cpc_buf_t **data1, **data2, **scratch; cpc_buf_t *tmp; int nreqs; thread_t tid; htdelta = NSECS_PER_MSEC * opts->mseconds; restdelta = NSECS_PER_MSEC * opts->mseconds_rest; ht = gethrtime(); /* * If this CPU is SMT, we run one gtick() thread per _physical_ CPU, * instead of per cpu_t. The following check returns if it detects that * this cpu_t has not been designated to do the counting for this * physical CPU. */ if (smt && chip_designees[state->chip_id] != state->cpuid) return (NULL); /* * If we need to run a soaker thread on this CPU, start it here. */ if (opts->dosoaker) { if (cond_init(&state->soak_cv, USYNC_THREAD, NULL) != 0) goto bad; if (mutex_init(&state->soak_lock, USYNC_THREAD, NULL) != 0) goto bad; (void) mutex_lock(&state->soak_lock); state->soak_state = SOAK_PAUSE; if (thr_create(NULL, 0, soaker, state, NULL, &tid) != 0) goto bad; while (state->soak_state == SOAK_PAUSE) (void) cond_wait(&state->soak_cv, &state->soak_lock); (void) mutex_unlock(&state->soak_lock); /* * If the soaker needs to pause for the first set, stop it now. */ if (cpc_setgrp_sysonly(sgrp) == 0) { (void) mutex_lock(&state->soak_lock); state->soak_state = SOAK_PAUSE; (void) mutex_unlock(&state->soak_lock); } } if (cpc_bind_cpu(cpc, state->cpuid, this, 0) == -1) goto bad; for (nsamples = opts->nsamples; nsamples; nsamples--, sample_cnt++) { hrtime_t htnow; struct timespec ts; nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); ht += htdelta; htnow = gethrtime(); if (ht <= htnow) continue; ts.tv_sec = (time_t)((ht - htnow) / NSECS_PER_SEC); ts.tv_nsec = (suseconds_t)((ht - htnow) % NSECS_PER_SEC); (void) nanosleep(&ts, NULL); if (opts->nsets == 1) { /* * If we're dealing with one set, buffer usage is: * * data1 = most recent data snapshot * data2 = previous data snapshot * scratch = used for diffing data1 and data2 * * Save the snapshot from the previous sample in data2 * before putting the current sample in data1. */ tmp = *data1; *data1 = *data2; *data2 = tmp; if (cpc_set_sample(cpc, this, *data1) != 0) goto bad; cpc_buf_sub(cpc, *scratch, *data1, *data2); print_sample(state->cpuid, *scratch, nreqs, name, 0); } else { /* * More than one set is in use (multiple -c options * given). Buffer usage in this case is: * * data1 = total counts for this set since program began * data2 = unused * scratch = most recent data snapshot */ name = cpc_setgrp_getname(sgrp); nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); if (cpc_set_sample(cpc, this, *scratch) != 0) goto bad; cpc_buf_add(cpc, *data1, *data1, *scratch); if (cpc_unbind(cpc, this) != 0) (void) fprintf(stderr, gettext("%s: error " "unbinding on cpu %d - %s\n"), opts->pgmname, state->cpuid, strerror(errno)); this = cpc_setgrp_nextset(sgrp); print_sample(state->cpuid, *scratch, nreqs, name, 0); /* * If periodic behavior was requested, rest here. */ if (opts->doperiod && opts->mseconds_rest > 0 && (sample_cnt % opts->nsets) == 0) { /* * Stop the soaker while the tool rests. */ if (opts->dosoaker) { (void) mutex_lock(&state->soak_lock); if (state->soak_state == SOAK_RUN) state->soak_state = SOAK_PAUSE; (void) mutex_unlock(&state->soak_lock); } htnow = gethrtime(); ht += restdelta; ts.tv_sec = (time_t)((ht - htnow) / NSECS_PER_SEC); ts.tv_nsec = (suseconds_t)((ht - htnow) % NSECS_PER_SEC); (void) nanosleep(&ts, NULL); } /* * Start or stop the soaker if needed. */ if (opts->dosoaker) { (void) mutex_lock(&state->soak_lock); if (cpc_setgrp_sysonly(sgrp) && state->soak_state == SOAK_PAUSE) { /* * Soaker is paused but the next set is * sysonly: start the soaker. */ state->soak_state = SOAK_RUN; (void) cond_signal(&state->soak_cv); } else if (cpc_setgrp_sysonly(sgrp) == 0 && state->soak_state == SOAK_RUN) /* * Soaker is running but the next set * counts user events: stop the soaker. */ state->soak_state = SOAK_PAUSE; (void) mutex_unlock(&state->soak_lock); } if (cpc_bind_cpu(cpc, state->cpuid, this, 0) != 0) goto bad; } } if (cpc_unbind(cpc, this) != 0) (void) fprintf(stderr, gettext("%s: error unbinding on" " cpu %d - %s\n"), opts->pgmname, state->cpuid, strerror(errno)); /* * We're done, so stop the soaker if needed. */ if (opts->dosoaker) { (void) mutex_lock(&state->soak_lock); if (state->soak_state == SOAK_RUN) state->soak_state = SOAK_PAUSE; (void) mutex_unlock(&state->soak_lock); } return (NULL); bad: state->status = 3; errstr = strerror(errno); (void) fprintf(stderr, gettext("%s: cpu%d - %s\n"), opts->pgmname, state->cpuid, errstr); return (NULL); }
static void print_sample(processorid_t cpuid, cpc_buf_t *buf, int nreq, const char *setname, int sibling) { char line[1024]; int ccnt; int i; uint64_t val; uint64_t tick; hrtime_t hrtime; hrtime = cpc_buf_hrtime(cpc, buf); tick = cpc_buf_tick(cpc, buf); ccnt = snprintf(line, sizeof (line), "%7.3f %3d %5s ", mstimestamp(hrtime), (int)cpuid, "tick"); if (opts->dotick) ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, "%9" PRId64 " ", tick); for (i = 0; i < nreq; i++) { (void) cpc_buf_get(cpc, buf, i, &val); ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, "%9" PRId64 " ", val); } if (opts->nsets > 1) ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, " # %s\n", setname); else ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, "\n"); if (sibling) { /* * This sample is being printed for a "sibling" CPU -- that is, * a CPU which does not have its own CPC set bound. It is being * measured via a set bound to another CPU sharing its physical * processor. */ int designee = chip_designees[gstate[cpuid].chip_id]; char *p; if ((p = strrchr(line, '#')) == NULL) p = strrchr(line, '\n'); if (p != NULL) { *p = '\0'; ccnt = strlen(line); ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, "# counter shared with CPU %d\n", designee); } } if (timestamp_fmt != NODATE) print_timestamp(timestamp_fmt); if (ccnt > sizeof (line)) ccnt = sizeof (line); if (ccnt > 0) (void) write(1, line, ccnt); /* * If this CPU is the chip designee for any other CPUs, print a line for * them here. */ if (smt && (sibling == 0)) { for (i = 0; i < ncpus; i++) { if ((i != cpuid) && (gstate[i].cpuid != -1) && (chip_designees[gstate[i].chip_id] == cpuid)) print_sample(i, buf, nreq, setname, 1); } } }