Export epoch time via proc_global_info (XCC)
[akaros.git] / user / benchutil / alarm.c
index 811e998..0d5bc59 100644 (file)
  * Barret Rhoden <brho@cs.berkeley.edu>
  * See LICENSE for details.
  *
- * Userspace alarm service, based off a slimmed down version of the kernel
- * alarms.  Under the hood, it uses the kernel alarm service for the root of
- * the alarm chain.
+ * Userspace alarms.  There are lower level helpers to build your own alarms
+ * from the #alarm device and an alarm service, based off a slimmed down version
+ * of the kernel alarms.  Under the hood, the user alarm uses the #alarm service
+ * for the root of the alarm chain.
  *
  * There's only one timer chain, unlike in the kernel, for the entire process.
  * If you want one-off timers unrelated to the chain (and sent to other vcores),
- * use #A directly.
+ * use #alarm directly.
  *
  * Your handlers will run from vcore context.
  *
  * Code differences from the kernel (for future porting):
- * - init_alarm_service, run once out of init_awaiter (or wherever).
+ * - init_alarm_service, run as a constructor
  * - set_alarm() and friends are __tc_set_alarm(), passing global_tchain.
- * - reset_tchain_interrupt() uses #A
+ * - reset_tchain_interrupt() uses #alarm
  * - removed anything related to semaphores or kthreads
  * - spinlocks -> spin_pdr_locks
- * - ev_q wrappers for converting #A events to __triggers
+ * - ev_q wrappers for converting #alarm events to __triggers
  * - printks, and other minor stuff. */
 
 #include <sys/queue.h>
-#include <alarm.h>
+#include <sys/time.h>
+#include <benchutil/alarm.h>
 #include <stdio.h>
-#include <assert.h>
+#include <parlib/assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <parlib.h>
-#include <event.h>
-#include <measure.h>
-#include <uthread.h>
-#include <spinlock.h>
+#include <parlib/parlib.h>
+#include <parlib/event.h>
+#include <benchutil/measure.h>
+#include <parlib/uthread.h>
+#include <parlib/spinlock.h>
+#include <parlib/timing.h>
+#include <sys/plan9_helpers.h>
+
+/* Helper to get your own alarm.   If you don't care about a return value, pass
+ * 0 and it'll be ignored.  The alarm is built, but has no evq or timer set. */
+int devalarm_get_fds(int *ctlfd_r, int *timerfd_r, int *alarmid_r)
+{
+       int ctlfd, timerfd, alarmid, ret;
+       char buf[20];
+       char path[32];
+
+       ctlfd = open("#alarm/clone", O_RDWR | O_CLOEXEC);
+       if (ctlfd < 0)
+               return -1;
+       ret = read(ctlfd, buf, sizeof(buf) - 1);
+       if (ret <= 0)
+               return -1;
+       buf[ret] = 0;
+       alarmid = atoi(buf);
+       snprintf(path, sizeof(path), "#alarm/a%s/timer", buf);
+       timerfd = open(path, O_RDWR | O_CLOEXEC);
+       if (timerfd < 0)
+               return -1;
+       if (ctlfd_r)
+               *ctlfd_r = ctlfd;
+       else
+               close(ctlfd);
+       if (timerfd_r)
+               *timerfd_r = timerfd;
+       else
+               close(timerfd);
+       if (alarmid_r)
+               *alarmid_r = alarmid;
+       return 0;
+}
+
+int devalarm_set_evq(int timerfd, struct event_queue *ev_q, int alarmid)
+{
+       struct fd_tap_req tap_req = {0};
+
+       tap_req.fd = timerfd;
+       tap_req.cmd = FDTAP_CMD_ADD;
+       tap_req.filter = FDTAP_FILT_WRITTEN;
+       tap_req.ev_id = EV_ALARM;
+       tap_req.ev_q = ev_q;
+       tap_req.data = (void*)(long)alarmid;
+       if (sys_tap_fds(&tap_req, 1) != 1)
+               return -1;
+       return 0;
+}
+
+int devalarm_set_time(int timerfd, uint64_t tsc_time)
+{
+       return write_hex_to_fd(timerfd, tsc_time);
+}
+
+int devalarm_get_id(struct event_msg *ev_msg)
+{
+       if (!ev_msg)
+               return -1;
+       return (int)(long)ev_msg->ev_arg3;
+}
+
+int devalarm_disable(int timerfd)
+{
+       return write_hex_to_fd(timerfd, 0);
+}
 
 /* Helpers, basically renamed kernel interfaces, with the *tchain. */
 static void __tc_locked_set_alarm(struct timer_chain *tchain,
@@ -46,7 +115,8 @@ static bool __tc_unset_alarm(struct timer_chain *tchain,
 static void __tc_reset_alarm_abs(struct timer_chain *tchain,
                                  struct alarm_waiter *waiter,
                                  uint64_t abs_time);
-static void handle_user_alarm(struct event_msg *ev_msg, unsigned int ev_type);
+static void handle_user_alarm(struct event_msg *ev_msg, unsigned int ev_type,
+                              void *data);
 
 /* One chain to rule them all. */
 struct timer_chain global_tchain;
@@ -66,11 +136,9 @@ static void reset_tchain_times(struct timer_chain *tchain)
        }
 }
 
-static void init_alarm_service(void)
+static void __attribute__((constructor)) init_alarm_service(void)
 {
-       int ctlfd, timerfd, alarm_nr, ret;
-       char buf[20];
-       char path[32];
+       int ctlfd, timerfd, alarmid;
        struct event_queue *ev_q;
 
        /* Sets up timer chain (only one chain per process) */
@@ -78,43 +146,29 @@ static void init_alarm_service(void)
        TAILQ_INIT(&global_tchain.waiters);
        reset_tchain_times(&global_tchain);
 
-       ctlfd = open("#A/clone", O_RDWR | O_CLOEXEC);
-       if (ctlfd < 0) {
-               perror("Useralarm: Can't clone an alarm");
-               return;
-       }
-       ret = read(ctlfd, buf, sizeof(buf) - 1);
-       if (ret <= 0) {
-               if (!ret)
-                       printf("Useralarm: Got early EOF from ctl\n");
-               else
-                       perror("Useralarm: Can't read ctl");
-               return;
-       }
-       buf[ret] = 0;
-       snprintf(path, sizeof(path), "#A/a%s/timer", buf);
-       timerfd = open(path, O_RDWR | O_CLOEXEC);
-       if (timerfd < 0) {
-               perror("Useralarm: Can't open timer");
+       if (devalarm_get_fds(&ctlfd, &timerfd, &alarmid)) {
+               perror("Useralarm: devalarm_get_fds");
                return;
        }
        /* Since we're doing SPAM_PUBLIC later, we actually don't need a big ev_q.
         * But someone might copy/paste this and change a flag. */
-       ev_handlers[EV_ALARM] = handle_user_alarm;
-       if (!(ev_q = get_big_event_q())) {
+       register_ev_handler(EV_ALARM, handle_user_alarm, 0);
+       if (!(ev_q = get_eventq(EV_MBOX_UCQ))) {
                perror("Useralarm: Failed ev_q");
                return;
        }
        ev_q->ev_vcore = 0;
-       ev_q->ev_flags = EVENT_IPI | EVENT_NOMSG | EVENT_SPAM_PUBLIC;
-       ret = snprintf(path, sizeof(path), "evq %llx", ev_q);
-       ret = write(ctlfd, path, ret);
-       if (ret <= 0) {
-               perror("Useralarm: Failed to write ev_q");
+       /* We could get multiple events for a single alarm.  It's okay, since
+        * __trigger can handle spurious upcalls.  If it ever is not okay, then use
+        * an INDIR (probably with SPAM_INDIR too) instead of SPAM_PUBLIC. */
+       ev_q->ev_flags = EVENT_IPI | EVENT_SPAM_PUBLIC | EVENT_WAKEUP;
+       if (devalarm_set_evq(timerfd, ev_q, alarmid)) {
+               perror("set_alarm_evq");
                return;
        }
        /* now the alarm is all set, just need to write the timer whenever we want
         * it to go off. */
+       global_tchain.alarmid = alarmid;
        global_tchain.ctlfd = ctlfd;
        global_tchain.timerfd = timerfd;
        global_tchain.ev_q = ev_q;      /* mostly for debugging */
@@ -125,7 +179,6 @@ static void init_alarm_service(void)
 void init_awaiter(struct alarm_waiter *waiter,
                   void (*func) (struct alarm_waiter *awaiter))
 {
-       run_once_racy(init_alarm_service());
        waiter->wake_up_time = ALARM_POISON_TIME;
        assert(func);
        waiter->func = func;
@@ -134,11 +187,18 @@ void init_awaiter(struct alarm_waiter *waiter,
 
 /* Give this the absolute time.  For now, abs_time is the TSC time that you want
  * the alarm to go off. */
-void set_awaiter_abs(struct alarm_waiter *waiter, uint64_t abs_time)
+static void __set_awaiter_abs(struct alarm_waiter *waiter, uint64_t abs_time)
 {
        waiter->wake_up_time = abs_time;
 }
 
+/* Give this the absolute unix time (in microseconds) that you want the alarm
+ * to go off. */
+void set_awaiter_abs_unix(struct alarm_waiter *waiter, uint64_t abs_usec)
+{
+       __set_awaiter_abs(waiter, epoch_nsec_to_tsc(abs_usec * 1000));
+}
+
 /* Give this a relative time from now, in microseconds.  This might be easier to
  * use than dealing with the TSC. */
 void set_awaiter_rel(struct alarm_waiter *waiter, uint64_t usleep)
@@ -149,7 +209,7 @@ void set_awaiter_rel(struct alarm_waiter *waiter, uint64_t usleep)
        /* This will go off if we wrap-around the TSC.  It'll never happen for legit
         * values, but this might catch some bugs with large usleeps. */
        assert(now <= then);
-       set_awaiter_abs(waiter, then);
+       __set_awaiter_abs(waiter, then);
 }
 
 /* Increment the timer that was already set, so that it goes off usleep usec
@@ -185,13 +245,10 @@ void reset_alarm_abs(struct alarm_waiter *waiter, uint64_t abs_time)
 /* Helper, makes sure the kernel alarm is turned on at the right time. */
 static void reset_tchain_interrupt(struct timer_chain *tchain)
 {
-       int ret;
-       char buf[20];
        if (TAILQ_EMPTY(&tchain->waiters)) {
                /* Turn it off */
                printd("Turning alarm off\n");
-               ret = write(tchain->ctlfd, "cancel", sizeof("cancel"));
-               if (ret <= 0) {
+               if (devalarm_disable(tchain->timerfd)) {
                        printf("Useralarm: unable to disarm alarm!\n");
                        return;
                }
@@ -200,9 +257,7 @@ static void reset_tchain_interrupt(struct timer_chain *tchain)
                assert(tchain->earliest_time != ALARM_POISON_TIME);
                /* TODO: check for times in the past or very close to now */
                printd("Turning alarm on for %llu\n", tchain->earliest_time);
-               ret = snprintf(buf, sizeof(buf), "%llx", tchain->earliest_time);
-               ret = write(tchain->timerfd, buf, ret);
-               if (ret <= 0) {
+               if (devalarm_set_time(tchain->timerfd, tchain->earliest_time)) {
                        perror("Useralarm: Failed to set timer");
                        return;
                }
@@ -248,10 +303,12 @@ static void __trigger_tchain(struct timer_chain *tchain)
        spin_pdr_unlock(&tchain->lock);
 }
 
-static void handle_user_alarm(struct event_msg *ev_msg, unsigned int ev_type)
+static void handle_user_alarm(struct event_msg *ev_msg, unsigned int ev_type,
+                              void *data)
 {
        assert(ev_type == EV_ALARM);
-       __trigger_tchain(&global_tchain);
+       if (devalarm_get_id(ev_msg) == global_tchain.alarmid)
+               __trigger_tchain(&global_tchain);
 }
 
 /* Helper, inserts the waiter into the tchain, returning TRUE if we still need
@@ -374,7 +431,7 @@ static void __tc_reset_alarm_abs(struct timer_chain *tchain,
         * on the tchain).  If it has fired, it's like a fresh insert */
        if (waiter->on_tchain)
                reset_int = __remove_awaiter(tchain, waiter);
-       set_awaiter_abs(waiter, abs_time);
+       __set_awaiter_abs(waiter, abs_time);
        /* regardless, we need to be reinserted */
        if (__insert_awaiter(tchain, waiter) || reset_int)
                reset_tchain_interrupt(tchain);
@@ -393,3 +450,20 @@ void print_chain(struct timer_chain *tchain)
               tchain->latest_time);
        spin_pdr_unlock(&tchain->lock);
 }
+
+/* "parlib" alarm handlers */
+void alarm_abort_sysc(struct alarm_waiter *awaiter)
+{
+       struct uthread *uth = awaiter->data;
+       assert(uth);
+       if (!uth->sysc) {
+               /* It's possible the sysc hasn't blocked yet or is in the process of
+                * unblocking, or even has returned, but hasn't cancelled the alarm.
+                * regardless, we request a new alarm (the uthread will cancel us one
+                * way or another). */
+               set_awaiter_inc(awaiter, 1000000);
+               __set_alarm(awaiter);
+               return;
+       }
+       sys_abort_sysc(uth->sysc);
+}