aboutsummaryrefslogtreecommitdiff
path: root/slowbaud.c
diff options
context:
space:
mode:
Diffstat (limited to 'slowbaud.c')
-rw-r--r--slowbaud.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/slowbaud.c b/slowbaud.c
index 54bc8de..2cfb138 100644
--- a/slowbaud.c
+++ b/slowbaud.c
@@ -1,6 +1,12 @@
/* Simulate low bitrate serial connection, like a 1980s modem.
Author: B. Watson. License: WTFPL. */
+
+/* configurables: */
+#define FRACTIONAL_USEC
+#define SHOW_CPU_TIME
+
+
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -16,6 +22,7 @@
#include <signal.h>
#include <time.h>
#include <string.h>
+#include <math.h>
/* portability stuff only tested on Linux and OSX. Hope it's OK. */
#if defined(__FreeBSD__) || defined(HAVE_LIBUTIL_H)
@@ -41,8 +48,6 @@ struct timeval tv;
struct itimerval itv, itv_disarm;
sigset_t sigmask;
-#define FRACTIONAL_USEC
-
#ifdef FRACTIONAL_USEC
#define FRAC_US_DENOM 100
int frac_us_num;
@@ -115,9 +120,22 @@ void debug_stats(void) {
double actual = ((double)outbytes * 10.0L) / elapsed_sec;
double offby = 100.0L * (((double)bps / actual) - 1.0L);
fprintf(stderr,
- "outbytes %lu, elapsed_us %lu, tv_usec %lu, requested bps %d (%.2fms), "
+ "outbytes %lu, elapsed_us %lu, tv_usec %lu\nrequested bps %d (%.2fms), "
"actual %.2f, accuracy %.2f%%\n",
outbytes, elapsed_us, itv.it_value.tv_usec, bps, finterval, actual, 100.0 - offby);
+#ifdef SHOW_CPU_TIME
+ {
+ struct timespec ts;
+ long end_cpu_us;
+ double cpu_pct;
+
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
+ end_cpu_us = ts.tv_sec * 1000000L + ts.tv_nsec / 1000L;
+ cpu_pct = (double)(end_cpu_us) / (double)(elapsed_us) * 100.0L;
+ fprintf(stderr, "CPU usage %luus, %.2f%%\n", end_cpu_us, cpu_pct);
+ }
+#endif
+
}
}
@@ -290,18 +308,13 @@ int main(int argc, char **argv) {
}
/* if we used only integer math here, we couldn't support bps not
- a multiple of 10 (e.g. 75 would be taken as 70).
- We still have a rounding problem: 115200bps is 86.81ms/char, but
- it gets rounded down to 86 here. Which would be around 116279bps,
- or almost 1% too fast. We never reach 100% speed anyway, so the
- loss from overhead actually offsets the extra bit of speed.
- */
+ a multiple of 10 (e.g. 75 would be taken as 70). */
interval = (unsigned long)(1000000.0L / ((double)bps / 10.0L));
#ifdef FRACTIONAL_USEC
- frac_us_num = (double)FRAC_US_DENOM * ((1000000.0L / ((double)bps / 10.0L) - interval));
+ frac_us_num = roundl((double)FRAC_US_DENOM * ((1000000.0L / ((double)bps / 10.0L) - interval)));
if(debug) fprintf(stderr, "interval %ld + %d/%d us\n", interval, frac_us_num, FRAC_US_DENOM);
- // srandom(NOW_USEC()); // don't think we should do this
+ srandom(NOW_USEC());
#else
if(debug) fprintf(stderr, "interval %ld us\n", interval);
#endif