return low;
}
+/** Return the lowest x such that x is at least <b>number</b>, and x modulo
+ * <b>divisor</b> == 0. */
+unsigned
+round_to_next_multiple_of(unsigned number, unsigned divisor)
+{
+ number += divisor - 1;
+ number -= number % divisor;
+ return number;
+}
+
+/** Return the lowest x such that x is at least <b>number</b>, and x modulo
+ * <b>divisor</b> == 0. */
+uint32_t
+round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
+{
+ number += divisor - 1;
+ number -= number % divisor;
+ return number;
+}
+
+/** Return the lowest x such that x is at least <b>number</b>, and x modulo
+ * <b>divisor</b> == 0. */
+uint64_t
+round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
+{
+ number += divisor - 1;
+ number -= number % divisor;
+ return number;
+}
+
/* =====
* String manipulation
* ===== */
#define EXIT_STATS_NUM_PORTS 65536
/** Reciprocal of threshold (= 0.01%) of total bytes that a port needs to
* see in order to be included in exit stats. */
-#define EXIT_STATS_THRESHOLD 10000
+#define EXIT_STATS_THRESHOLD_RECIPROCAL 10000
/* The following data structures are arrays and no fancy smartlists or maps,
* so that all write operations can be done in constant time. This comes at
{
char t[ISO_TIME_LEN+1];
int r, i, comma;
- uint64_t *b, total_bytes, other_bytes;
+ uint64_t *b, total_bytes, threshold_bytes, other_bytes;
uint32_t other_streams;
char *filename = get_datadir_fname("exit-stats");
goto done;
/* Count the total number of bytes, so that we can attribute all
- * observations below a threshold of 1 / EXIT_STATS_THRESHOLD of all
- * bytes to a special port 'other'. */
+ * observations below a threshold of 1 / EXIT_STATS_THRESHOLD_RECIPROCAL
+ * of all bytes to a special port 'other'. */
total_bytes = 0;
for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
total_bytes += exit_bytes_read[i];
total_bytes += exit_bytes_written[i];
}
+ threshold_bytes = total_bytes / EXIT_STATS_THRESHOLD_RECIPROCAL;
/* kibibytes-(read|written) port=kibibytes,.. */
for (r = 0; r < 2; r++) {
other_bytes = 0;
for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
if (b[i] > 0) {
- if (exit_bytes_read[i] + exit_bytes_written[i] > 0 &&
- (total_bytes / (exit_bytes_read[i] + exit_bytes_written[i])
- < EXIT_STATS_THRESHOLD)) {
- uint64_t num = round_to_next_multiple_of(b[i],
- EXIT_STATS_ROUND_UP_BYTES);
+ if (exit_bytes_read[i] + exit_bytes_written[i] > threshold_bytes) {
+ uint64_t num = round_uint64_to_next_multiple_of(b[i],
+ EXIT_STATS_ROUND_UP_BYTES);
num /= 1024;
if (fprintf(out, "%s%d="U64_FORMAT,
comma++ ? "," : "", i,
other_bytes += b[i];
}
}
- other_bytes = round_to_next_multiple_of(other_bytes,
- EXIT_STATS_ROUND_UP_BYTES);
+ other_bytes = round_uint64_to_next_multiple_of(other_bytes,
+ EXIT_STATS_ROUND_UP_BYTES);
other_bytes /= 1024;
if (fprintf(out, "%sother="U64_FORMAT"\n",
comma ? "," : "", other_bytes)<0)
other_streams = 0;
for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
if (exit_streams[i] > 0) {
- if (exit_bytes_read[i] + exit_bytes_written[i] > 0 &&
- (total_bytes / (exit_bytes_read[i] + exit_bytes_written[i])
- < EXIT_STATS_THRESHOLD)) {
- uint32_t num = round_to_next_multiple_of(exit_streams[i],
- EXIT_STATS_ROUND_UP_STREAMS);
+ if (exit_bytes_read[i] + exit_bytes_written[i] > threshold_bytes) {
+ uint32_t num = round_uint32_to_next_multiple_of(exit_streams[i],
+ EXIT_STATS_ROUND_UP_STREAMS);
if (fprintf(out, "%s%d=%u",
comma++ ? "," : "", i, num)<0)
goto done;
other_streams += exit_streams[i];
}
}
- other_streams = round_to_next_multiple_of(other_streams,
- EXIT_STATS_ROUND_UP_STREAMS);
+ other_streams = round_uint32_to_next_multiple_of(other_streams,
+ EXIT_STATS_ROUND_UP_STREAMS);
if (fprintf(out, "%sother=%u\n",
comma ? "," : "", other_streams)<0)
goto done;