Exemple #1
0
/**
 * repad_net_init - Initialize all structures used to receive packets from network
 *
 * @param id	The id of thread which call this function
 *
 * @return	<0 for errors
 */
static int repad_net_init(long id) {
	int result_raw = 0, result_udp = 0, result_overlay = 0;

	repad_attr.th_network_threads[TH_RAW_SOCKET] = 0;
	repad_attr.th_network_threads[TH_UDP_SOCKET] = 0;
	repad_attr.th_network_threads[TH_OVERLAY_NET] = 0;

	/* Create a new prod_cons for receive packets from network */
	repad_attr.pc_receive_packets = pc_create(10, NULL, NULL, NULL,
			(void(*)(void*))skb_release);
	if (repad_attr.pc_receive_packets == NULL) {
		repad_printf(LOG_INFO, "Th%ld: ERROR: On create prod_cons for received packets\n");
		return -1;
	}

	/* Create HashMap for memory message */
	repad_attr.hm_memory_message = hmap_create(hmap_hash_mem_fn,
			hmap_eq_mem_fn, NULL, (void (*)(hmap_key_t))dll_destroy_all, 0, 0.0);
	if (repad_attr.hm_memory_message == NULL) {
		repad_printf(LOG_INFO, "Th%ld: ERROR: On create map for memory message\n");
		return -1;
	}

	/* Create list of packet with no match */
	repad_attr.list_pkt_without_match = dll_create(upwm_compare_packet, NULL);
	if (repad_attr.list_pkt_without_match == NULL) {
		repad_printf(LOG_INFO, "Th%ld: ERROR: On create list for packets without match\n");
		return -1;
	}

	/* Load ifaces informations */
	load_all_ifaces_info(&repad_attr.list_all_ifaces);
	repad_printf(LOG_INFO, "Th%ld: It was found %d interface(s)...\n",
				id, dll_get_num_elements(repad_attr.list_all_ifaces));

	/* Init raw socket */
	repad_printf(LOG_INFO, "Th%ld: raw_socket_enable=%s - udp_socket_enable=%s"
			" - overlay_enable=%s\n", id,
			strbool(repad_attr.bl_raw_socket_enable),
			strbool(repad_attr.bl_udp_socket_enable),
			strbool(repad_attr.bl_overlay_enable));
	if (repad_attr.bl_raw_socket_enable) {
		result_raw = repad_net_init_raw_socket(id);
	}

	/* Init UDP socket */
	if (repad_attr.bl_udp_socket_enable) {
		result_udp = repad_net_init_udp_socket(id);
	}

	/* Init overlay */
	if (repad_attr.bl_overlay_enable) {
		result_overlay = repad_net_init_overlay(id);
	}

	return ((result_raw >= 0) ? 0 :
			(result_udp >= 0) ? 0 :
			(result_overlay >= 0) ? 0 : -1);
}
Datum
probabilistic_add_item_agg2(PG_FUNCTION_ARGS)
{

    ProbabilisticCounter pcounter;

    /* info for anyelement */
    Oid         element_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
    Datum       element = PG_GETARG_DATUM(1);
    int16       typlen;
    bool        typbyval;
    char        typalign;

    /* create a new estimator (with requested error rate) or reuse the existing one */
    if (PG_ARGISNULL(0)) {
        pcounter = pc_create(DEFAULT_NBYTES, DEFAULT_NSALTS);
    } else { /* existing estimator */
        pcounter = (ProbabilisticCounter)PG_GETARG_BYTEA_P(0);
    }

    /* add the item to the estimator (skip NULLs) */
    if (! PG_ARGISNULL(1)) {

        /* TODO The requests for type info shouldn't be a problem (thanks to lsyscache),
         * but if it turns out to have a noticeable impact it's possible to cache that
         * between the calls (in the estimator). */
        
        /* get type information for the second parameter (anyelement item) */
        get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);

        /* it this a varlena type, passed by reference or by value ? */
        if (typlen == -1) {
            /* varlena */
            pc_add_element(pcounter, VARDATA(element), VARSIZE(element) - VARHDRSZ);
        } else if (typbyval) {
            /* fixed-length, passed by value */
            pc_add_element(pcounter, (char*)&element, typlen);
        } else {
            /* fixed-length, passed by reference */
            pc_add_element(pcounter, (char*)element, typlen);
        }
    }

    /* return the updated bytea */
    PG_RETURN_BYTEA_P(pcounter);
    
}
Datum
probabilistic_init(PG_FUNCTION_ARGS)
{
      ProbabilisticCounter pc;
      int nbytes;
      int nsalts;
      
      nbytes = PG_GETARG_INT32(0);
      nsalts = PG_GETARG_INT32(1);
      
      /* nbytes and nsalts have to be positive */
      if ((nbytes < 1) || (nbytes > MAX_NBYTES)) {
          elog(ERROR, "number of bytes per bitmap has to be between 1 and %d", MAX_NBYTES);
      } else if (nsalts < 1) {
          elog(ERROR, "number salts has to be between 1 and %d", MAX_NSALTS);
      }
      
      pc = pc_create(nbytes, nsalts);
      
      PG_RETURN_BYTEA_P(pc);
}