[or-cvs] Refactor ISO-style (yyyy-mm-dd hh:mm:ss) time parsing into ...
Nick Mathewson
nickm at seul.org
Sat Aug 7 02:46:24 UTC 2004
Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv18598/src/or
Modified Files:
directory.c dirserv.c main.c or.h rephist.c router.c
routerparse.c test.c
Log Message:
Refactor ISO-style (yyyy-mm-dd hh:mm:ss) time parsing into util.c; rename format/parse_rfc1123_time; make rephist remember used bandwidth; published used bandwidth in descriptors in 15-minute chunks. Breaks unittests.
Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.122
retrieving revision 1.123
diff -u -d -r1.122 -r1.123
--- directory.c 6 Aug 2004 20:00:16 -0000 1.122
+++ directory.c 7 Aug 2004 02:46:15 -0000 1.123
@@ -538,7 +538,7 @@
return 0;
}
- tor_format_rfc1123_time(date, time(NULL));
+ format_rfc1123_time(date, time(NULL));
snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\n\r\n",
date,
(int)dlen);
@@ -563,7 +563,7 @@
}
switch(rend_cache_lookup_desc(url+strlen(rend_fetch_url), &descp, &desc_len)) {
case 1: /* valid */
- tor_format_rfc1123_time(date, time(NULL));
+ format_rfc1123_time(date, time(NULL));
snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: application/octet-stream\r\n\r\n",
date,
desc_len); /* can't include descp here, because it's got nuls */
Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dirserv.c,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -d -r1.73 -r1.74
--- dirserv.c 6 Aug 2004 22:15:25 -0000 1.73
+++ dirserv.c 7 Aug 2004 02:46:15 -0000 1.74
@@ -558,7 +558,7 @@
return -1;
dirserv_remove_old_servers(ROUTER_MAX_AGE);
published_on = time(NULL);
- strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
+ format_iso_time(published, published_on);
snprintf(s, maxlen,
"signed-directory\n"
"published %s\n"
@@ -706,7 +706,7 @@
if (list_running_servers(&cp))
return -1;
published_on = time(NULL);
- strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
+ format_iso_time(published, published_on);
sprintf(s, "network-status\n"
"published %s\n"
"running-routers %s\n"
Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.312
retrieving revision 1.313
diff -u -d -r1.312 -r1.313
--- main.c 6 Aug 2004 22:15:25 -0000 1.312
+++ main.c 7 Aug 2004 02:46:15 -0000 1.313
@@ -418,7 +418,7 @@
static int decide_if_publishable_server(time_t now) {
int bw;
- bw = rep_hist_bandwidth_assess(now);
+ bw = rep_hist_bandwidth_assess();
router_set_advertised_bandwidth(bw);
if(options.ClientOnly)
Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.397
retrieving revision 1.398
diff -u -d -r1.397 -r1.398
--- or.h 6 Aug 2004 22:15:25 -0000 1.397
+++ or.h 7 Aug 2004 02:46:16 -0000 1.398
@@ -1262,7 +1262,8 @@
void rep_hist_dump_stats(time_t now, int severity);
void rep_hist_note_bytes_read(int num_bytes, time_t when);
void rep_hist_note_bytes_written(int num_bytes, time_t when);
-int rep_hist_bandwidth_assess(time_t when);
+int rep_hist_bandwidth_assess(void);
+char *rep_hist_get_bandwidth_lines(void);
/********************************* rendclient.c ***************************/
Index: rephist.c
===================================================================
RCS file: /home/or/cvsroot/src/or/rephist.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- rephist.c 6 Aug 2004 20:46:50 -0000 1.19
+++ rephist.c 7 Aug 2004 02:46:16 -0000 1.20
@@ -327,26 +327,37 @@
int total_obs; /**< Total for all members of obs except obs[cur_obs_idx] */
int max_total; /**< Largest value that total_obs has taken on in the current
* period. */
+ int total_in_period; /**< Total bytes transferred in the current period. */
/** When does the next period begin? */
time_t next_period;
/** Where in 'maxima' should the maximum bandwidth usage for the current
* period be stored? */
int next_max_idx;
- /** Circular array of the maximum bandwidth usage for the last NUM_TOTALS
- * periods */
+ /** How many values in maxima/totals have been set ever? */
+ int num_maxes_set;
+ /** Circular array of the maximum
+ * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
+ * NUM_TOTALS periods */
int maxima[NUM_TOTALS];
+ /** Circular array of the total bandwidth usage for the last NUM_TOTALS
+ * periods */
+ int totals[NUM_TOTALS];
} bw_array_t;
/** Shift the current period of b forward by one.
*/
static void commit_max(bw_array_t *b) {
+ /* Store total from current period. */
+ b->totals[b->next_max_idx] = b->total_in_period;
/* Store maximum from current period. */
b->maxima[b->next_max_idx++] = b->max_total;
/* Advance next_period and next_max_idx */
b->next_period += NUM_SECS_BW_SUM_INTERVAL;
if (b->next_max_idx == NUM_TOTALS)
b->next_max_idx = 0;
+ if (b->num_maxes_set < NUM_TOTALS)
+ ++b->num_maxes_set;
/* Reset max_total. */
b->max_total = 0;
}
@@ -388,6 +399,7 @@
advance_obs(b);
b->obs[b->cur_obs_idx] += n;
+ b->total_in_period += n;
}
/** Allocate, initialize, and return a new bw_array.
@@ -463,7 +475,7 @@
*
* Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
*/
-int rep_hist_bandwidth_assess(time_t when) {
+int rep_hist_bandwidth_assess(void) {
int w,r;
r = find_largest_max(read_array);
w = find_largest_max(write_array);
@@ -475,6 +487,42 @@
return 0;
}
+/**
+ * DOCDOC
+ */
+char *rep_hist_get_bandwidth_lines(void)
+{
+ char *buf, *cp;
+ char t[ISO_TIME_LEN+1];
+ int r, i, n;
+ bw_array_t *b;
+ size_t len;
+
+ /* opt (read|write)history yyyy-mm-dd HH:MM:SS n,n,n,n,n... */
+ len = (60+12*NUM_TOTALS)*2;
+ buf = tor_malloc_zero(len);
+ cp = buf;
+ for (r=0;r<2;++r) {
+ b = r?read_array:write_array;
+ format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
+ sprintf(cp, "opt %s-history %s (%d s)", r?"read":"write", t,
+ NUM_SECS_BW_SUM_INTERVAL);
+ cp += strlen(cp);
+ for (i=b->num_maxes_set+1,n=0; n<b->num_maxes_set; ++n,++i) {
+ if (i >= NUM_TOTALS) i -= NUM_TOTALS;
+ if (n==(b->num_maxes_set-1))
+ sprintf(cp, "%d", b->totals[i]);
+ else
+ sprintf(cp, "%d,", b->totals[i]);
+ cp += strlen(cp);
+ }
+ strcat(cp, "\n");
+ ++cp;
+ }
+ return buf;
+}
+
+
/*
Local Variables:
mode:c
Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.78
retrieving revision 1.79
diff -u -d -r1.78 -r1.79
--- router.c 4 Aug 2004 05:10:49 -0000 1.78
+++ router.c 7 Aug 2004 02:46:16 -0000 1.79
@@ -581,6 +581,7 @@
int written;
int result=0;
struct exit_policy_t *tmpe;
+ char *bandwidth_usage;
#ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
char *s_tmp, *s_dup;
const char *cp;
@@ -615,8 +616,11 @@
}
/* Encode the publication time. */
- strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
+ format_iso_time(published, router->published_on);
+ /* How busy have we been? */
+ bandwidth_usage = rep_hist_get_bandwidth_lines();
+
/* Generate the easy portion of the router descriptor. */
result = snprintf(s, maxlen,
"router %s %s %d %d %d\n"
@@ -626,7 +630,7 @@
"opt uptime %ld\n"
"bandwidth %d %d %d\n"
"onion-key\n%s"
- "signing-key\n%s",
+ "signing-key\n%s%s",
router->nickname,
router->address,
router->or_port,
@@ -642,7 +646,8 @@
(int) router->bandwidthrate,
(int) router->bandwidthburst,
(int) router->advertisedbandwidth,
- onion_pkey, identity_pkey);
+ onion_pkey, identity_pkey,
+ bandwidth_usage);
tor_free(onion_pkey);
tor_free(identity_pkey);
Index: routerparse.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerparse.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- routerparse.c 7 Aug 2004 01:48:50 -0000 1.25
+++ routerparse.c 7 Aug 2004 02:46:16 -0000 1.26
@@ -8,10 +8,6 @@
* \brief Code to parse and validate router descriptors and directories.
**/
-/* This is required on rh7 to make strptime not complain.
- */
-#define _GNU_SOURCE
-
#include "or.h"
/****************************************************************************/
@@ -164,38 +160,6 @@
"network-status","\ndirectory-signature");
}
-/** Parse a date of the format "YYYY-MM-DD hh:mm:ss" and store the result into
- * *<b>t</b>.
- */
-/* XXX this should go in util.c, yes? -RD */
-static int parse_time(const char *cp, time_t *t)
-{
- struct tm st_tm;
-#ifdef HAVE_STRPTIME
- if (!strptime(cp, "%Y-%m-%d %H:%M:%S", &st_tm)) {
- log_fn(LOG_WARN, "Published time was unparseable"); return -1;
- }
-#else
- unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
- if (sscanf(cp, "%u-%u-%u %u:%u:%u", &year, &month,
- &day, &hour, &minute, &second) < 6) {
- log_fn(LOG_WARN, "Published time was unparseable"); return -1;
- }
- if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
- hour > 23 || minute > 59 || second > 61) {
- log_fn(LOG_WARN, "Published time was nonsensical"); return -1;
- }
- st_tm.tm_year = year;
- st_tm.tm_mon = month-1;
- st_tm.tm_mday = day;
- st_tm.tm_hour = hour;
- st_tm.tm_min = minute;
- st_tm.tm_sec = second;
-#endif
- *t = tor_timegm(&st_tm);
- return 0;
-}
-
/**
* Find the first instance of "recommended-software ...\n" at the start of
* a line; return a newly allocated string containing the "..." portion.
@@ -405,7 +369,7 @@
}
tor_assert(tok->n_args == 1);
- if (parse_time(tok->args[0], &published_on) < 0) {
+ if (parse_iso_time(tok->args[0], &published_on) < 0) {
goto err;
}
@@ -517,7 +481,7 @@
goto err;
}
tor_assert(tok->n_args == 1);
- if (parse_time(tok->args[0], &published_on) < 0) {
+ if (parse_iso_time(tok->args[0], &published_on) < 0) {
goto err;
}
@@ -783,7 +747,7 @@
log_fn(LOG_WARN, "Missing published time"); goto err;
}
tor_assert(tok->n_args == 1);
- if (parse_time(tok->args[0], &router->published_on) < 0)
+ if (parse_iso_time(tok->args[0], &router->published_on) < 0)
goto err;
if (!(tok = find_first_by_keyword(tokens, K_ONION_KEY))) {
Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.106
retrieving revision 1.107
diff -u -d -r1.106 -r1.107
--- test.c 4 Aug 2004 01:11:15 -0000 1.106
+++ test.c 7 Aug 2004 02:46:16 -0000 1.107
@@ -482,13 +482,13 @@
a_time.tm_mday = 10;
test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
- tor_format_rfc1123_time(timestr, 0);
+ format_rfc1123_time(timestr, 0);
test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
- tor_format_rfc1123_time(timestr, (time_t)1091580502UL);
+ format_rfc1123_time(timestr, (time_t)1091580502UL);
test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
t_res = 0;
- i = tor_parse_rfc1123_time(timestr, &t_res);
+ i = parse_rfc1123_time(timestr, &t_res);
test_eq(i,0);
test_eq(t_res, (time_t)1091580502UL);
@@ -923,8 +923,9 @@
#endif
crypto_seed_rng();
-
setup_directory();
+ rep_hist_init();
+
// puts("========================== Buffers =========================");
// test_buffers();
puts("\n========================== Crypto ==========================");
More information about the tor-commits
mailing list