示例#1
0
/**
 * create_context
 *
 * Arguments: [options] <context_id>
 * Options: --system
 *          --no-overflow-msg
 *          --block-on-notify
 *          --sampler <sampler_name>
 *
 * Call the pfm_create_context system-call to create a new perfmon context.
 * Add a new entry to the global 'contexts' list.
 **/
static int create_context(int argc, char **argv)
{
	pfm_dfl_smpl_arg_t smpl_arg;
	struct context *new_ctx = NULL;
	char *sampler_name = NULL;
	void *smpl_p;
	int no_overflow_msg = FALSE;
	int block_on_notify = FALSE;
	int system_wide = FALSE;
	int c, ctx_id = 0;
	int rc;
	uint32_t ctx_flags;
	size_t sz;

	struct option long_opts[] = {
		{"sampler",         required_argument, NULL, 1},
		{"system",          no_argument,       NULL, 2},
		{"no-overflow-msg", no_argument,       NULL, 3},
		{"block-on-notify", no_argument,       NULL, 4},
		{NULL,              0,                 NULL, 0} };

	ctx_flags = 0;

	opterr = 0;
	optind = 0;
	while ((c = getopt_long_only(argc, argv, "",
				     long_opts, NULL)) != EOF) {
		switch (c) {
		case 1:
			sampler_name = optarg;
			break;
		case 2:
			system_wide = TRUE;
			break;
		case 3:
			no_overflow_msg = TRUE;
			break;
		case 4:
			block_on_notify = TRUE;
			break;
		default:
			LOG_ERROR("invalid option: %c", optopt);
			rc = EINVAL;
			goto error;
		}
	}

	if (argc < optind + 1) {
		USAGE("create_context [options] <context_id>");
		rc = EINVAL;
		goto error;
	}

	ctx_id = strtoul(argv[optind], NULL, 0);
	if (ctx_id <= 0) {
		LOG_ERROR("Invalid context ID (%s). Must be a positive "
			  "integer.", argv[optind]);
		rc = EINVAL;
		goto error;
	}

	/* Make sure we don't already have a context with this ID. */
	new_ctx = find_context(ctx_id);
	if (new_ctx) {
		LOG_ERROR("Context with ID %d already exists.", ctx_id);
		rc = EINVAL;
		goto error;
	}

	if (sampler_name) {
		smpl_arg.buf_size = getpagesize();
		smpl_p = &smpl_arg;
		sz = sizeof(smpl_arg);
	} else {
		smpl_p = NULL;
		sz = 0;
	}

	ctx_flags = (system_wide     ? PFM_FL_SYSTEM_WIDE  : 0) |
		    (no_overflow_msg ? PFM_FL_OVFL_NO_MSG  : 0) |
		    (block_on_notify ? PFM_FL_NOTIFY_BLOCK : 0);

	if (sampler_name)
		ctx_flags |= PFM_FL_SMPL_FMT;

	rc = pfm_create(ctx_flags, NULL, sampler_name, smpl_p, sz);
	if (rc == -1) {
		rc = errno;
		LOG_ERROR("pfm_create_context system call returned "
			  "an error: %d.", rc);
		goto error;
	}

	/* Allocate and initialize a new context structure and add it to the
	 * global list. Every new context automatically gets one event_set
	 * with an event ID of 0.
	 */
	new_ctx = calloc(1, sizeof(*new_ctx));
	if (!new_ctx) {
		LOG_ERROR("Can't allocate structure for new context %d.",
			  ctx_id);
		rc = ENOMEM;
		goto error;
	}

	new_ctx->event_sets = calloc(1, sizeof(*(new_ctx->event_sets)));
	if (!new_ctx->event_sets) {
		LOG_ERROR("Can't allocate event-set structure for new "
			  "context %d.", ctx_id);
		rc = ENOMEM;
		goto error;
	}

	new_ctx->id = ctx_id;
	new_ctx->fd = rc;
	new_ctx->cpu = -1;
	new_ctx->ctx_flags = ctx_flags;
	new_ctx->smpl_arg = smpl_arg;

	insert_context(new_ctx);

	LOG_INFO("Created context %d with file-descriptor %d.",
		 new_ctx->id, new_ctx->fd);

	return 0;

error:
	if (new_ctx) {
		close(new_ctx->fd);
		free(new_ctx->event_sets);
		free(new_ctx);
	}
	return rc;
}
void expr_context_simplifier::reduce_rec(app * a, expr_ref & result) {    
    if (m_manager.get_basic_family_id() == a->get_family_id()) {
        switch(a->get_decl_kind()) {
        case OP_AND: 
            reduce_and(a->get_num_args(), a->get_args(), result);
            return;
        case OP_OR:
            reduce_or(a->get_num_args(), a->get_args(), result);
            return;
        case OP_IFF: {
            expr_ref tmp1(m_manager), tmp2(m_manager);
            reduce_rec(a->get_arg(0), tmp1);
            reduce_rec(a->get_arg(1), tmp2);
            m_simp.mk_iff(tmp1.get(), tmp2.get(), result);
            return;
        }
        case OP_XOR: {
            expr_ref tmp1(m_manager), tmp2(m_manager);
            reduce_rec(a->get_arg(0), tmp1);
            reduce_rec(a->get_arg(1), tmp2);
            m_simp.mk_xor(tmp1.get(), tmp2.get(), result);
            return;
        }
        case OP_NOT: {
            expr_ref tmp(m_manager);
            reduce_rec(a->get_arg(0), tmp);
            m_simp.mk_not(tmp.get(), result);
            return;
        }
        case OP_IMPLIES: {
            app_ref tmp(m_manager);
            tmp = m_manager.mk_not(a->get_arg(0));
            expr* args[2] = { tmp.get(), a->get_arg(1) };
            reduce_or(2, args, result);
            return;
        }
        case OP_ITE: {
            expr_ref tmp(m_manager), tmp1(m_manager), tmp2(m_manager);
            reduce_rec(a->get_arg(0), tmp);
            if (is_true(tmp.get())) {
                reduce_rec(a->get_arg(1), result);
            }
            else if (is_false(tmp.get())) {
                reduce_rec(a->get_arg(2), result);
            }
            else {
                unsigned trail_size = m_trail.size();
                insert_context(tmp.get(), true);
                reduce_rec(a->get_arg(1), tmp1);
                clean_trail(trail_size);
                
                insert_context(tmp.get(), false);
                reduce_rec(a->get_arg(2), tmp2);
                clean_trail(trail_size);
                
                m_simp.mk_ite(tmp.get(), tmp1.get(), tmp2.get(), result);
            }
            return;
        }
        default:
            break;
        }
    }

    expr_ref_vector args(m_manager);
    for (unsigned i = 0; i < a->get_num_args(); ++i) {
        expr_ref tmp(m_manager);
        reduce_rec(a->get_arg(i), tmp);
        args.push_back(tmp.get());
    }
    result = m_manager.mk_app(a->get_decl(), args.size(), args.c_ptr());
}