NFS cache writeback is now invoked periodically by a timer.
[bluesky.git] / nfs3 / rpc.c
index 468c58c..a7d108c 100644 (file)
@@ -212,12 +212,21 @@ async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
     if (req->raw_args != NULL)
         g_string_free(req->raw_args, TRUE);
 
+    while (req->cleanup != NULL) {
+        struct cleanup_list *c = req->cleanup;
+        req->cleanup = c->next;
+        c->func(c->arg);
+        g_free(c);
+    }
+
     g_free(req);
 }
 
 void
 async_rpc_send_reply(RPCRequest *req, void *result)
 {
+    bluesky_time_hires time_end;
+
     GString *str = g_string_new("");
     XDR xdr_out;
     xdr_string_create(&xdr_out, str, XDR_ENCODE);
@@ -242,6 +251,11 @@ async_rpc_send_reply(RPCRequest *req, void *result)
     async_rpc_write(req->connection, str->str, str->len);
     g_io_channel_flush(req->connection->channel, NULL);
 
+    time_end = bluesky_now_hires();
+
+    printf("RPC[%"PRIx32"]: time = %"PRId64" ns\n",
+           req->xid, time_end - req->time_start);
+
     /* Clean up. */
     g_string_free(str, TRUE);
 
@@ -257,6 +271,13 @@ async_rpc_send_reply(RPCRequest *req, void *result)
     if (req->raw_args != NULL)
         g_string_free(req->raw_args, TRUE);
 
+    while (req->cleanup != NULL) {
+        struct cleanup_list *c = req->cleanup;
+        req->cleanup = c->next;
+        c->func(c->arg);
+        g_free(c);
+    }
+
     g_free(req);
 }
 
@@ -450,7 +471,6 @@ nfs_program_3(RPCRequest *req)
     req->xdr_result = _xdr_result;
     result = (*local)((char *)req->args, req);
 
-    bluesky_flushd_invoke(fs);
     bluesky_debug_dump(fs);
 
     return;
@@ -462,10 +482,22 @@ nfs_program_3(RPCRequest *req)
 static GMainContext *main_context;
 static GMainLoop *main_loop;
 
+static gboolean async_flushd(gpointer data)
+{
+    bluesky_flushd_invoke(fs);
+    return TRUE;
+}
+
 static async_rpc_init()
 {
     main_context = g_main_context_new();
     main_loop = g_main_loop_new(main_context, FALSE);
+
+    /* Arrange to have the cache writeback code run every five seconds. */
+    GSource *source = g_timeout_source_new_seconds(5);
+    g_source_set_callback(source, async_flushd, NULL, NULL);
+    g_source_attach(source, main_context);
+    g_source_unref(source);
 }
 
 struct rpc_call_header {
@@ -487,6 +519,7 @@ struct rpc_auth {
  * and the transport should be closed. */
 static gboolean async_rpc_dispatch(RPCConnection *rpc)
 {
+    bluesky_time_hires time_start = bluesky_now_hires();
     int i;
     GString *msg = rpc->msgbuf;
     const char *buf = msg->str;
@@ -510,6 +543,7 @@ static gboolean async_rpc_dispatch(RPCConnection *rpc)
 
     RPCRequest *req = g_new0(RPCRequest, 1);
     req->connection = rpc;
+    req->time_start = time_start;
     req->xid = xid;
 
     if (ntohl(header->prog) != NFS_PROGRAM) {