int libxl__set_domain_configuration(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config) { char *d_config_json; int rc; d_config_json = libxl_domain_config_to_json(CTX, d_config); if (!d_config_json) { LOGE(ERROR, "failed to convert domain configuration to JSON for domain %d", domid); rc = ERROR_FAIL; goto out; } rc = libxl__userdata_store(gc, domid, "libxl-json", (const uint8_t *)d_config_json, strlen(d_config_json) + 1 /* include '\0' */); if (rc) { LOGEV(ERROR, rc, "failed to store domain configuration for domain %d", domid); rc = ERROR_FAIL; goto out; } out: free(d_config_json); return rc; }
/* Hotplug scripts caller functions */ static int libxl__hotplug(libxl__gc *gc, libxl__device *dev, char ***args, libxl__device_action action) { char *be_path = libxl__device_backend_path(gc, dev); char *script; int nr = 0, rc = 0, arraysize = 4; script = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/%s", be_path, "script")); if (!script) { LOGEV(ERROR, errno, "unable to read script from %s", be_path); rc = ERROR_FAIL; goto out; } GCNEW_ARRAY(*args, arraysize); (*args)[nr++] = script; (*args)[nr++] = be_path; (*args)[nr++] = GCSPRINTF("%d", action == LIBXL__DEVICE_ACTION_ADD ? XenbusStateInitWait : XenbusStateClosed); (*args)[nr++] = NULL; assert(nr == arraysize); out: return rc; }
int libxl__get_domain_configuration(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config) { uint8_t *data = NULL; int rc, len; rc = libxl__userdata_retrieve(gc, domid, "libxl-json", &data, &len); if (rc) { LOGEV(ERROR, rc, "failed to retrieve domain configuration for domain %d", domid); rc = ERROR_FAIL; goto out; } if (len == 0) { /* No logging, not necessary an error from caller's PoV. */ rc = ERROR_JSON_CONFIG_EMPTY; goto out; } rc = libxl_domain_config_from_json(CTX, d_config, (const char *)data); out: free(data); return rc; }
static int libxl__hotplug_disk(libxl__gc *gc, libxl__device *dev, char ***args, char ***env, libxl__device_action action) { char *be_path = libxl__device_backend_path(gc, dev); char *script; int nr = 0, rc = 0; script = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/%s", be_path, "script")); if (!script) { LOGEV(ERROR, errno, "unable to read script from %s", be_path); rc = ERROR_FAIL; goto error; } *env = get_hotplug_env(gc, script, dev); if (!*env) { rc = ERROR_FAIL; goto error; } const int arraysize = 3; GCNEW_ARRAY(*args, arraysize); (*args)[nr++] = script; (*args)[nr++] = action == DEVICE_CONNECT ? "add" : "remove"; (*args)[nr++] = NULL; assert(nr == arraysize); rc = 1; error: return rc; }
void libxl__xc_domain_restore_done(libxl__egc *egc, void *dcs_void, int rc, int retval, int errnoval) { libxl__domain_create_state *dcs = dcs_void; libxl__stream_read_state *stream = &dcs->srs; STATE_AO_GC(dcs->ao); if (rc) goto err; if (retval) { LOGEV(ERROR, errnoval, "restoring domain"); rc = ERROR_FAIL; goto err; } err: check_all_finished(egc, stream, rc); /* * This function is the callback associated with the save helper * task, not the stream task. We do not know whether the stream is * alive, and check_all_finished() may have torn it down around us. * If the stream is not still alive, we must not continue any work. */ if (libxl__stream_read_inuse(stream)) { /* * Libxc has indicated that it is done with the stream. Resume reading * libxl records from it. */ stream_continue(egc, stream); } }
/************************************************************************** * Compute a pvalue for a particular bin and quadrant. * Store the location of the bin if the pvalue is significant. **************************************************************************/ static void compute_spacing_pvalue(int tests, double threshold, int quad, int bin, int test_max, double prob, SECONDARY_MOTIF_T *smotif) { double pvalue; int counts; counts = smotif->spacings[quad].bins[bin]; //keep track of the maximum count for the histogram. if (counts > smotif->max_in_one_bin) { smotif->max_in_one_bin = counts; } if (bin < test_max) { pvalue = one_minus_binomial_cdf(prob, counts, smotif->total_spacings); // adjust p-value for multiple tests pvalue = exp(LOGEV(log(tests), log(pvalue))); smotif->spacings[quad].pvalues[bin] = pvalue; // record minimum pvalue over all possible spacings of secondary motif if (pvalue < smotif->min_pvalue) smotif->min_pvalue = pvalue; if (pvalue <= threshold) { SIGSPACE_T *sig; smotif->sig_count += 1; smotif->sigs = mm_realloc(smotif->sigs, sizeof(SIGSPACE_T) * smotif->sig_count); sig = smotif->sigs+(smotif->sig_count - 1); sig->pvalue = pvalue; sig->bin = bin; sig->quad = quad; } } else { //don't test this bin, set pvalue to special value to indicate it is untested pvalue = 2; smotif->spacings[quad].pvalues[bin] = pvalue; } }
static void async_exec_timeout(libxl__egc *egc, libxl__ev_time *ev, const struct timeval *requested_abs, int rc) { libxl__async_exec_state *aes = CONTAINER_OF(ev, *aes, time); STATE_AO_GC(aes->ao); if (!aes->rc) aes->rc = rc; libxl__ev_time_deregister(gc, &aes->time); assert(libxl__ev_child_inuse(&aes->child)); LOG(ERROR, "killing execution of %s because of timeout", aes->what); if (kill(aes->child.pid, SIGKILL)) { LOGEV(ERROR, errno, "unable to kill %s [%ld]", aes->what, (unsigned long)aes->child.pid); } return; }
void libxl__xc_domain_restore_done(libxl__egc *egc, void *dcs_void, int ret, int retval, int errnoval) { libxl__domain_create_state *dcs = dcs_void; STATE_AO_GC(dcs->ao); libxl_ctx *ctx = libxl__gc_owner(gc); char **vments = NULL, **localents = NULL; struct timeval start_time; int i, esave, flags; /* convenience aliases */ const uint32_t domid = dcs->guest_domid; libxl_domain_config *const d_config = dcs->guest_config; libxl_domain_build_info *const info = &d_config->b_info; libxl__domain_build_state *const state = &dcs->build_state; const int fd = dcs->restore_fd; if (ret) goto out; if (retval) { LOGEV(ERROR, errnoval, "restoring domain"); ret = ERROR_FAIL; goto out; } gettimeofday(&start_time, NULL); switch (info->type) { case LIBXL_DOMAIN_TYPE_HVM: vments = libxl__calloc(gc, 7, sizeof(char *)); vments[0] = "rtc/timeoffset"; vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : ""; vments[2] = "image/ostype"; vments[3] = "hvm"; vments[4] = "start_time"; vments[5] = libxl__sprintf(gc, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000); break; case LIBXL_DOMAIN_TYPE_PV: vments = libxl__calloc(gc, 11, sizeof(char *)); i = 0; vments[i++] = "image/ostype"; vments[i++] = "linux"; vments[i++] = "image/kernel"; vments[i++] = (char *) state->pv_kernel.path; vments[i++] = "start_time"; vments[i++] = libxl__sprintf(gc, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000); if (state->pv_ramdisk.path) { vments[i++] = "image/ramdisk"; vments[i++] = (char *) state->pv_ramdisk.path; } if (state->pv_cmdline) { vments[i++] = "image/cmdline"; vments[i++] = (char *) state->pv_cmdline; } break; default: ret = ERROR_INVAL; goto out; } ret = libxl__build_post(gc, domid, info, state, vments, localents); if (ret) goto out; if (info->type == LIBXL_DOMAIN_TYPE_HVM) { state->saved_state = GCSPRINTF( XC_DEVICE_MODEL_RESTORE_FILE".%d", domid); } out: if (info->type == LIBXL_DOMAIN_TYPE_PV) { libxl__file_reference_unmap(&state->pv_kernel); libxl__file_reference_unmap(&state->pv_ramdisk); } esave = errno; flags = fcntl(fd, F_GETFL); if (flags == -1) { LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to get flags on restore fd"); } else { flags &= ~O_NONBLOCK; if (fcntl(fd, F_SETFL, flags) == -1) LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to put restore fd" " back to blocking mode"); } errno = esave; domcreate_rebuild_done(egc, dcs, ret); }
/************************************************************************* * Compute the enrichment of the central region *************************************************************************/ static MOTIF_STATS_T* compute_stats(int max_window, int sequence_length, MOTIF_DB_T* db, MOTIF_T* motif, SITE_COUNTS_T* counts) { // variables MOTIF_STATS_T *stats; double window_counts, max_sites; int i, max_bins, is_centered, big_window, middle, window, bins; double log_p_value; // allocate memory for stats stats = mm_malloc(sizeof(MOTIF_STATS_T)); // initilise stats to defaults stats->db = db; stats->motif = motif; stats->total_sites = counts->total_sites; stats->n_win_tested = 0; stats->max_prob = 0; stats->central_sites = 0; stats->central_prob = 0.0; stats->central_window = 0; stats->log_pvalue = 0; stats->log_adj_pvalue = 0; // find the largest site count max_sites = 0; for (i = 0; i < counts->allocated; i++) { if (max_sites < counts->sites[i]) max_sites = counts->sites[i]; } // calculate the max probability stats->max_prob = (counts->total_sites == 0 ? 0 : max_sites / (double)counts->total_sites); // get the number of bins that the motif could possibly have landed in max_bins = sequence_length - get_motif_length(motif) + 1; // determine if this motif can have sites in a completely central bin is_centered = (max_bins % 2); // calculate the window that contains all sites from this motif stats->all_window = max_bins - is_centered; // calculate the biggest window which might have a p-value big_window = stats->all_window - 2; // check that max window is ok if (max_window == -1 || max_window > big_window) max_window = big_window; if (max_window < 0) return stats; // no windows to test! // calculate the number of tested windows stats->n_win_tested = (is_centered ? (max_window / 2) + 1 : (max_window + 1) / 2); if (stats->n_win_tested == 0) return stats; // no windows to test! // the index of the bin in the center middle = sequence_length - 1; // initialise counts stats->log_pvalue = BIG; //ensure it is replaced by the loop window_counts = 0; bins = 0; if (is_centered) { // test the central window bins++; window_counts = counts->sites[middle]; stats->log_pvalue = window_enrichment(0, window_counts, counts->total_sites, bins, max_bins); stats->central_sites = window_counts; stats->central_prob = (double)bins / (double)max_bins; } // find the best window by trying all possible windows for (bins += 2, window = bins-1; window <= max_window; bins += 2, window += 2) { window_counts += counts->sites[middle - window]; window_counts += counts->sites[middle + window]; // calculate the window p-value log_p_value = window_enrichment(window, window_counts, counts->total_sites, bins, max_bins); // check if the p-value is better if (log_p_value < stats->log_pvalue) { stats->log_pvalue = log_p_value; stats->central_window = window; stats->central_sites = window_counts; stats->central_prob = (double)bins / (double)max_bins; } } stats->log_adj_pvalue = LOGEV(log(stats->n_win_tested), stats->log_pvalue); DEBUG_FMT(HIGHER_VERBOSE, "best bin: %d sites: %g " "log_adj_p-value: %g (%d tests)\n", stats->central_window+1, stats->central_sites, stats->log_adj_pvalue, stats->n_win_tested); return stats; }