/**
 * Update and normalize atsi performance information
 *
 * @param address the address to update
 */
void
GAS_normalization_update_property (struct ATS_Address *address)
{
  const struct GNUNET_ATS_Properties *prop = &address->properties;
  struct PropertyRange range;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Updating properties for peer `%s'\n",
       GNUNET_i2s (&address->peer));
  GAS_plugin_solver_lock ();
  update_avg (prop->delay.rel_value_us,
              &address->norm_delay);
  update_avg (prop->distance,
              &address->norm_distance);
  update_avg (prop->utilization_in,
              &address->norm_utilization_in);
  update_avg (prop->utilization_in,
              &address->norm_utilization_out);

  init_range (&range);
  GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
                                         &find_min_max_it,
                                         &range);
  if (0 != memcmp (&range,
                   &property_range,
                   sizeof (struct PropertyRange)))
  {
    /* limits changed, (re)normalize all addresses */
    property_range = range;
    GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
                                           &normalize_address,
                                           NULL);
    GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
                                           &notify_change,
                                           NULL);
  }
  else
  {
    /* renormalize just this one address */
    normalize_address (NULL,
                       &address->peer,
                       address);
    notify_change (NULL,
                   &address->peer,
                   address);
  }
  GAS_plugin_solver_unlock ();
}
Ejemplo n.º 2
0
int
pfctl_update_qstats(int dev, struct pf_altq_node **root)
{
	struct pf_altq_node	*node;
	struct pfioc_altq	 pa;
	struct pfioc_qstats	 pq;
	u_int32_t		 mnr, nr;
	struct queue_stats	 qstats;
	static	u_int32_t	 last_ticket;

	memset(&pa, 0, sizeof(pa));
	memset(&pq, 0, sizeof(pq));
	memset(&qstats, 0, sizeof(qstats));
	if (ioctl(dev, DIOCGETALTQS, &pa)) {
		warn("DIOCGETALTQS");
		return (-1);
	}

	/* if a new set is found, start over */
	if (pa.ticket != last_ticket && *root != NULL) {
		pfctl_free_altq_node(*root);
		*root = NULL;
	}
	last_ticket = pa.ticket;

	mnr = pa.nr;
	for (nr = 0; nr < mnr; ++nr) {
		pa.nr = nr;
		if (ioctl(dev, DIOCGETALTQ, &pa)) {
			warn("DIOCGETALTQ");
			return (-1);
		}
		if (pa.altq.qid > 0) {
			pq.nr = nr;
			pq.ticket = pa.ticket;
			pq.buf = &qstats.data;
			pq.nbytes = sizeof(qstats.data);
			if (ioctl(dev, DIOCGETQSTATS, &pq)) {
				warn("DIOCGETQSTATS");
				return (-1);
			}
			if ((node = pfctl_find_altq_node(*root, pa.altq.qname,
			    pa.altq.ifname)) != NULL) {
				memcpy(&node->qstats.data, &qstats.data,
				    sizeof(qstats.data));
				update_avg(node);
			} else {
				pfctl_insert_altq_node(root, pa.altq, qstats);
			}
		}
	}
	return (mnr);
}
Ejemplo n.º 3
0
void Histogram::add(double value, int rank) {  
  assert(rank>=0);
  if(value < 0){
    if(value >= -DOUBLE_EPSILON){
      cout << "warning: negative double value " << value << " changed to 0.0f" << endl;
      value = 0.0f;
    } else {
      cerr << "error: rank " << my_rank << ", add a negative value " << value << " into histogram" << endl;
      cerr << toString() << endl;
      exit(0);
    }
  }

  if (num_elems == 0) {
    double bin_size = (value != 0.0) ? (2.0 * value)/num_bins : 1.0;

    for (int i=0; i < num_bins; i++) {
      HistoBin *bin = new HistoBin(i*bin_size, (i+1) * bin_size);
      bins.push_back(bin);
    }
  }

  num_elems++;

  // maintain min/max values.
  update_min(value, rank);
  update_max(value, rank);
  update_avg(value, 1);

  // put the value in the histogram
  int binIdx = find_bin(value);
  if(binIdx != -1){
    bins[binIdx]->add(value);
  } else {
    double tail_start = bins[num_bins-1]->getEnd();
    double tail_end = 2 * value - tail_start;
    /*if(tail_end < tail_start){
			cout << "tail_end: " << tail_end << endl;
			cout << "tail_start: " << tail_start << endl;
			cout << "value: " << value << endl;
			cout << toString() << endl;
			while(1);
		}*/
    assert(tail_end >= tail_start);
    HistoBin *tail = new HistoBin(tail_start, tail_end);
    tail->add(value);
    bins.push_back(tail);
    smooth();
  }

}
Ejemplo n.º 4
0
int main() {
    map<std::string, unsigned> b;
    std::unordered_map<std::string, unsigned> c;
    
    clock_t t, avg = 0.0;
    unsigned n = 1;

    unsigned tot = 100000, p = 0, m = 0, e = 0;

    while(tot--) {
      std::ifstream input("pride_book.txt");
      std::string word;

      t = clock();
      std::cout << t << " , ";

      while(input >> word)
          b[word] += 1;

      std::cout << clock() << "\n";

      t = clock() - t;

      avg = (avg == 0.0) ? t : update_avg(avg, t, n++);

      std::cout << "Avg: " << avg;

      if ((float)t < (float)avg) {
        m++;
        std::cout << " - ";
      } else if ((float)t > (float)avg) {
        p++;
        std::cout << " + ";
      } else {
        e++;
        std::cout << " = ";
      }

      std::cout.flush();

    }
    std::cout << "Plus: " << p << " Minus: " << m << " Equal: " << e << "\n";
}
Ejemplo n.º 5
0
void
pfctl_insert_altq_node(struct pf_altq_node **root,
    const struct pf_altq altq, const struct queue_stats qstats)
{
	struct pf_altq_node	*node;

	node = calloc(1, sizeof(struct pf_altq_node));
	if (node == NULL)
		err(1, "pfctl_insert_altq_node: calloc");
	memcpy(&node->altq, &altq, sizeof(struct pf_altq));
	memcpy(&node->qstats, &qstats, sizeof(qstats));
	node->next = node->children = NULL;

	if (*root == NULL)
		*root = node;
	else if (!altq.parent[0]) {
		struct pf_altq_node	*prev = *root;

		while (prev->next != NULL)
			prev = prev->next;
		prev->next = node;
	} else {
		struct pf_altq_node	*parent;

		parent = pfctl_find_altq_node(*root, altq.parent, altq.ifname);
		if (parent == NULL)
			errx(1, "parent %s not found", altq.parent);
		if (parent->children == NULL)
			parent->children = node;
		else {
			struct pf_altq_node *prev = parent->children;

			while (prev->next != NULL)
				prev = prev->next;
			prev->next = node;
		}
	}
	update_avg(node);
}
Ejemplo n.º 6
0
int
pfctl_update_qstats(int dev, struct pf_altq_node **root)
{
	struct pf_altq_node	*node;
	struct pfioc_altq	 pa;
	struct pfioc_qstats	 pq;
	u_int32_t		 mnr, nr;
	struct queue_stats	 qstats;
	static	u_int32_t	 last_ticket;

	memset(&pa, 0, sizeof(pa));
	memset(&pq, 0, sizeof(pq));
	memset(&qstats, 0, sizeof(qstats));
	if (ioctl(dev, DIOCGETALTQS, &pa)) {
		warn("DIOCGETALTQS");
		return (-1);
	}

	/* if a new set is found, start over */
	if (pa.ticket != last_ticket && *root != NULL) {
		pfctl_free_altq_node(*root);
		*root = NULL;
	}
	last_ticket = pa.ticket;

	mnr = pa.nr;
	for (nr = 0; nr < mnr; ++nr) {
		pa.nr = nr;
		if (ioctl(dev, DIOCGETALTQ, &pa)) {
			warn("DIOCGETALTQ");
			return (-1);
		}
#ifdef __FreeBSD__
		if (pa.altq.qid > 0 &&
		    !(pa.altq.local_flags & PFALTQ_FLAG_IF_REMOVED)) {
#else
		if (pa.altq.qid > 0) {
#endif
			pq.nr = nr;
			pq.ticket = pa.ticket;
			pq.buf = &qstats.data;
			pq.nbytes = sizeof(qstats.data);
			if (ioctl(dev, DIOCGETQSTATS, &pq)) {
				warn("DIOCGETQSTATS");
				return (-1);
			}
			if ((node = pfctl_find_altq_node(*root, pa.altq.qname,
			    pa.altq.ifname)) != NULL) {
				memcpy(&node->qstats.data, &qstats.data,
				    sizeof(qstats.data));
				update_avg(node);
			} else {
				pfctl_insert_altq_node(root, pa.altq, qstats);
			}
		}
#ifdef __FreeBSD__
		else if (pa.altq.local_flags & PFALTQ_FLAG_IF_REMOVED) {
			memset(&qstats.data, 0, sizeof(qstats.data));
			if ((node = pfctl_find_altq_node(*root, pa.altq.qname,
			    pa.altq.ifname)) != NULL) {
				memcpy(&node->qstats.data, &qstats.data,
				    sizeof(qstats.data));
				update_avg(node);
			} else {
				pfctl_insert_altq_node(root, pa.altq, qstats);
			}
		}
#endif
	}
	return (mnr);
}

void
pfctl_insert_altq_node(struct pf_altq_node **root,
    const struct pf_altq altq, const struct queue_stats qstats)
{
	struct pf_altq_node	*node;

	node = calloc(1, sizeof(struct pf_altq_node));
	if (node == NULL)
		err(1, "pfctl_insert_altq_node: calloc");
	memcpy(&node->altq, &altq, sizeof(struct pf_altq));
	memcpy(&node->qstats, &qstats, sizeof(qstats));
	node->next = node->children = NULL;

	if (*root == NULL)
		*root = node;
	else if (!altq.parent[0]) {
		struct pf_altq_node	*prev = *root;

		while (prev->next != NULL)
			prev = prev->next;
		prev->next = node;
	} else {
		struct pf_altq_node	*parent;

		parent = pfctl_find_altq_node(*root, altq.parent, altq.ifname);
		if (parent == NULL)
			errx(1, "parent %s not found", altq.parent);
		if (parent->children == NULL)
			parent->children = node;
		else {
			struct pf_altq_node *prev = parent->children;

			while (prev->next != NULL)
				prev = prev->next;
			prev->next = node;
		}
	}
	update_avg(node);
}

struct pf_altq_node *
pfctl_find_altq_node(struct pf_altq_node *root, const char *qname,
    const char *ifname)
{
	struct pf_altq_node	*node, *child;

	for (node = root; node != NULL; node = node->next) {
		if (!strcmp(node->altq.qname, qname)
		    && !(strcmp(node->altq.ifname, ifname)))
			return (node);
		if (node->children != NULL) {
			child = pfctl_find_altq_node(node->children, qname,
			    ifname);
			if (child != NULL)
				return (child);
		}
	}
	return (NULL);
}

void
pfctl_print_altq_node(int dev, const struct pf_altq_node *node,
    unsigned int level, int opts)
{
	const struct pf_altq_node	*child;

	if (node == NULL)
		return;

	print_altq(&node->altq, level, NULL, NULL);

	if (node->children != NULL) {
		printf("{");
		for (child = node->children; child != NULL;
		    child = child->next) {
			printf("%s", child->altq.qname);
			if (child->next != NULL)
				printf(", ");
		}
		printf("}");
	}
	printf("\n");

	if (opts & PF_OPT_VERBOSE)
		pfctl_print_altq_nodestat(dev, node);

	if (opts & PF_OPT_DEBUG)
		printf("  [ qid=%u ifname=%s ifbandwidth=%s ]\n",
		    node->altq.qid, node->altq.ifname,
		    rate2str((double)(node->altq.ifbandwidth)));

	for (child = node->children; child != NULL;
	    child = child->next)
		pfctl_print_altq_node(dev, child, level + 1, opts);
}

void
pfctl_print_altq_nodestat(int dev, const struct pf_altq_node *a)
{
	if (a->altq.qid == 0)
		return;

#ifdef __FreeBSD__
	if (a->altq.local_flags & PFALTQ_FLAG_IF_REMOVED)
		return;
#endif
	switch (a->altq.scheduler) {
	case ALTQT_CBQ:
		print_cbqstats(a->qstats);
		break;
	case ALTQT_PRIQ:
		print_priqstats(a->qstats);
		break;
	case ALTQT_HFSC:
		print_hfscstats(a->qstats);
		break;
	}
}

void
print_cbqstats(struct queue_stats cur)
{
	printf("  [ pkts: %10llu  bytes: %10llu  "
	    "dropped pkts: %6llu bytes: %6llu ]\n",
	    (unsigned long long)cur.data.cbq_stats.xmit_cnt.packets,
	    (unsigned long long)cur.data.cbq_stats.xmit_cnt.bytes,
	    (unsigned long long)cur.data.cbq_stats.drop_cnt.packets,
	    (unsigned long long)cur.data.cbq_stats.drop_cnt.bytes);
	printf("  [ qlength: %3d/%3d  borrows: %6u  suspends: %6u ]\n",
	    cur.data.cbq_stats.qcnt, cur.data.cbq_stats.qmax,
	    cur.data.cbq_stats.borrows, cur.data.cbq_stats.delays);

	if (cur.avgn < 2)
		return;

	printf("  [ measured: %7.1f packets/s, %s/s ]\n",
	    cur.avg_packets / STAT_INTERVAL,
	    rate2str((8 * cur.avg_bytes) / STAT_INTERVAL));
}
Ejemplo n.º 7
0
/*
 * Checks h/w handshake lines for "go" status.  Count the number of times it
 * was "stop".  In UART_HANDSHAKE_AUTO mode, after a limit is reached force
 * the status to "go".
 */
static boolean_t is_hw_tx_handshake_go(mon_serial_device_t *p_device)
{
	/* Modem Status Register image */
	uart_msr_t msr;
	/* flags transmit "go" by h/w handshake */
	boolean_t hw_handshake_go;

	if ((p_device->handshake_mode == UART_HANDSHAKE_AUTO) ||
	    (p_device->handshake_mode == UART_HANDSHAKE_HW)) {
		/* Read the h/w handshake signals */

		msr.data =
			hw_read_port_8(p_device->io_base + UART_REGISTER_MSR);
		hw_handshake_go = (msr.bits.cts == 1) && (msr.bits.dsr == 1);

		if (hw_handshake_go) {
			/* Other side set h/w handshake to "go".  Reset the counter. */

			update_avg(&p_device->hw_handshake_stopped_count_avg,
				p_device->hw_handshake_stopped_count);
			p_device->hw_handshake_stopped_count = 0;
		} else if (p_device->hw_handshake_stopped_count >=
			   hw_handshake_stopped_limit) {
			/* Other side has indicated h/w handshake "stop" for too long. */

			if (p_device->handshake_mode == UART_HANDSHAKE_AUTO) {
				/* In auto mode, assume the h/w handshake is stuck and force */
				/* the status to "go" */

				hw_handshake_go = TRUE;
				p_device->num_chars_hw_handshake_auto_go++;
				if (p_device->hw_handshake_stopped_count ==
				    hw_handshake_stopped_limit) {
					/* Update the statistic only on the first character */
					/* we decided to auto-go */

					update_avg(
						&p_device->hw_handshake_stopped_count_avg,
						p_device->hw_handshake_stopped_count);
					p_device->hw_handshake_stopped_count++;
				}
			}
		} else {
			/* Increment the stop count and update the statistics */

			if (p_device->hw_handshake_stopped_count == 0) {
				/* We just stopped, increment the stops statistics counter */

				p_device->num_chars_hw_handshake_stopped++;
			}

			p_device->hw_handshake_stopped_count++;

			update_max(&p_device->hw_handshake_stopped_count_max,
				p_device->hw_handshake_stopped_count);
		}
	} else {
		/* No h/w handshake, always "go" */

		hw_handshake_go = TRUE;
	}

	return hw_handshake_go;
}
Ejemplo n.º 8
0
/*
 * Updates the running average and maximum of a counter
 *    p_max - Maximum
 *    p_avg - Running average, as uint16_t.uint16_t
 *    count - New input
 */
static
void update_max_and_avg(uint32_t *p_max, uint32_t *p_avg, uint32_t count)
{
	update_max(p_max, count);
	update_avg(p_avg, count);
}