Skip to contentSkip to conversation

tools/xenstore: start of rework of xenstored

RFC PATCH New Juergen Gross 1 participant 5 messages Jan 22, 2021

lore
Share & tools
lore
#0 lore raw

cover tools/xenstore: start of rework of xenstored

This is an overture of a major rework of the xenstored internals I've had lying around for about one year now. It has been blocked by all the XSA work and live update stuff.

This series has not seen much testing. It is rather meant as a starting point for discussions whether the approach especially to drop tdb is accepted or not.

The main reason to drop using tdb is to be able to have references between nodes, which will be rather interesting for adding the capability to do live updates even with open transactions.

Other optimizations made possible by this would be to e.g.:

- hook watches directly to nodes, dropping the need to scan all active watches when modifying a node

- switch transaction handling to a rather simple copy-on-write scheme

- avoid lots of node copying when traversing the tree

- drop the list children names by replacing it with a linked list of child nodes

Patches 1 and 2 are in this series basically because they are in my todo list for xenstored and they need to wait for the tree to be opened anyway before being able to go in.

Patch 3 is some preparation for removing tdb, but could be taken as a cleanup patch even without dropping tdb.

In case no one is objecting to drop tdb I'll continue with some further optimizations.

Juergen Gross (4):
tools/xenstore: simplify xenstored main loop tools/xenstore: claim resources when running as daemon tools/xenstore: rework hashtable interfaces tools/xenstore: drop use of tdb

 .gitignore                             |    2 -
 tools/xenstore/Makefile                |    9 +-
 tools/xenstore/hashtable.c             |  234 ++--
 tools/xenstore/hashtable.h             |  137 +-
 tools/xenstore/hashtable_private.h     |   85 --
 tools/xenstore/tdb.c                   | 1748 ------------------------
 tools/xenstore/tdb.h                   |  134 --
 tools/xenstore/xenstored_core.c        |  313 ++---
 tools/xenstore/xenstored_core.h        |   30 +-
 tools/xenstore/xenstored_domain.c      |   11 +-
 tools/xenstore/xenstored_minios.c      |    4 +
 tools/xenstore/xenstored_posix.c       |   46 +
 tools/xenstore/xenstored_transaction.c |   71 +-
 tools/xenstore/xenstored_transaction.h |    4 +-
 tools/xenstore/xs_tdb_dump.c           |   86 --
 15 files changed, 426 insertions(+), 2488 deletions(-)
 delete mode 100644 tools/xenstore/hashtable_private.h
 delete mode 100644 tools/xenstore/tdb.c
 delete mode 100644 tools/xenstore/tdb.h
 delete mode 100644 tools/xenstore/xs_tdb_dump.c
Signature · 1 line
2.26.2
#1 lore raw

RFC PATCH 1/4 tools/xenstore: simplify xenstored main loop

The main loop of xenstored is rather complicated due to different handling of socket and ring-page interfaces. Unify that handling by introducing interface type specific functions can_read() and can_write().

Put the interface type specific functions in an own structure and let struct connection contain only a pointer to that new function vector.

---
 tools/xenstore/xenstored_core.c   | 118 +++++++++++++++++-------------
 tools/xenstore/xenstored_core.h   |  16 ++--
 tools/xenstore/xenstored_domain.c |  11 ++-
 3 files changed, 85 insertions(+), 60 deletions(-)
tools/xenstore/xenstored_core.c +66 -52
index 65ecdd0568..c89f5202fe 100644
@@ -226,8 +226,8 @@ static bool write_messages(struct connection *conn)
 				sockmsg_string(out->hdr.msg.type),
 				out->hdr.msg.len,
 				out->buffer, conn);
-		ret = conn->write(conn, out->hdr.raw + out->used,
-				  sizeof(out->hdr) - out->used);
+		ret = conn->funcs->write(conn, out->hdr.raw + out->used,
+					 sizeof(out->hdr) - out->used);
 		if (ret < 0)
 			return false;
 
@@ -243,8 +243,8 @@ static bool write_messages(struct connection *conn)
 			return true;
 	}
 
-	ret = conn->write(conn, out->buffer + out->used,
-			  out->hdr.msg.len - out->used);
+	ret = conn->funcs->write(conn, out->buffer + out->used,
+				 out->hdr.msg.len - out->used);
 	if (ret < 0)
 		return false;
 
@@ -1520,8 +1520,8 @@ static void handle_input(struct connection *conn)
 	/* Not finished header yet? */
 	if (in->inhdr) {
 		if (in->used != sizeof(in->hdr)) {
-			bytes = conn->read(conn, in->hdr.raw + in->used,
-					   sizeof(in->hdr) - in->used);
+			bytes = conn->funcs->read(conn, in->hdr.raw + in->used,
+						  sizeof(in->hdr) - in->used);
 			if (bytes < 0)
 				goto bad_client;
 			in->used += bytes;
@@ -1546,8 +1546,8 @@ static void handle_input(struct connection *conn)
 		in->inhdr = false;
 	}
 
-	bytes = conn->read(conn, in->buffer + in->used,
-			   in->hdr.msg.len - in->used);
+	bytes = conn->funcs->read(conn, in->buffer + in->used,
+				  in->hdr.msg.len - in->used);
 	if (bytes < 0)
 		goto bad_client;
 
@@ -1570,7 +1570,7 @@ static void handle_output(struct connection *conn)
 		ignore_connection(conn);
 }
 
-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
+struct connection *new_connection(struct interface_funcs *funcs)
 {
 	struct connection *new;
 
@@ -1580,8 +1580,7 @@ struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
 
 	new->fd = -1;
 	new->pollfd_idx = -1;
-	new->write = write;
-	new->read = read;
+	new->funcs = funcs;
 	new->is_ignored = false;
 	new->transaction_started = 0;
 	INIT_LIST_HEAD(&new->out_list);
@@ -1622,6 +1621,16 @@ int readfd(struct connection *conn, void *data, unsigned int len)
 	errno = EBADF;
 	return -1;
 }
+
+static bool socket_can_write(struct connection *conn)
+{
+	return false;
+}
+
+static bool socket_can_read(struct connection *conn)
+{
+	return false;
+}
 #else
 int writefd(struct connection *conn, const void *data, unsigned int len)
 {
@@ -1661,6 +1670,32 @@ int readfd(struct connection *conn, void *data, unsigned int len)
 	return rc;
 }
 
+static bool socket_can_write(struct connection *conn)
+{
+	if (conn->pollfd_idx == -1)
+		return false;
+
+	if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
+		talloc_free(conn);
+		return false;
+	}
+
+	return fds[conn->pollfd_idx].revents & POLLOUT;
+}
+
+static bool socket_can_read(struct connection *conn)
+{
+	if (conn->pollfd_idx == -1)
+		return false;
+
+	if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
+		talloc_free(conn);
+		return false;
+	}
+
+	return fds[conn->pollfd_idx].revents & POLLIN;
+}
+
 static void accept_connection(int sock)
 {
 	int fd;
@@ -1670,7 +1705,7 @@ static void accept_connection(int sock)
 	if (fd < 0)
 		return;
 
-	conn = new_connection(writefd, readfd);
+	conn = new_connection(&socket_funcs);
 	if (conn)
 		conn->fd = fd;
 	else
@@ -1678,6 +1713,13 @@ static void accept_connection(int sock)
 }
 #endif
 
+struct interface_funcs socket_funcs = {
+	.write = writefd,
+	.read = readfd,
+	.can_write = socket_can_write,
+	.can_read = socket_can_read,
+};
+
 static int tdb_flags;
 
 /* We create initial nodes manually. */
@@ -2280,47 +2322,19 @@ int main(int argc, char *argv[])
 			if (&next->list != &connections)
 				talloc_increase_ref_count(next);
 
-			if (conn->domain) {
-				if (domain_can_read(conn))
-					handle_input(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-				if (domain_can_write(conn) &&
-				    !list_empty(&conn->out_list))
-					handle_output(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-			} else {
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLIN) &&
-						 !conn->is_ignored)
-						handle_input(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLOUT) &&
-						 !conn->is_ignored)
-						handle_output(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
+			if (conn->funcs->can_read(conn))
+				handle_input(conn);
+			if (talloc_free(conn) == 0)
+				continue;
 
-				conn->pollfd_idx = -1;
-			}
+			talloc_increase_ref_count(conn);
+
+			if (conn->funcs->can_write(conn))
+				handle_output(conn);
+			if (talloc_free(conn) == 0)
+				continue;
+
+			conn->pollfd_idx = -1;
 		}
 
 		if (delayed_requests) {
tools/xenstore/xenstored_core.h +10 -6
index 589699e833..84b364d82c 100644
@@ -87,8 +87,13 @@ struct delayed_request {
 };
 
 struct connection;
-typedef int connwritefn_t(struct connection *, const void *, unsigned int);
-typedef int connreadfn_t(struct connection *, void *, unsigned int);
+
+struct interface_funcs {
+	int (*write)(struct connection *, const void *, unsigned int);
+	int (*read)(struct connection *, void *, unsigned int);
+	bool (*can_write)(struct connection *);
+	bool (*can_read)(struct connection *);
+};
 
 struct connection
 {
@@ -132,9 +137,8 @@ struct connection
 	/* My watches. */
 	struct list_head watches;
 
-	/* Methods for communicating over this connection: write can be NULL */
-	connwritefn_t *write;
-	connreadfn_t *read;
+	/* Methods for communicating over this connection. */
+	struct interface_funcs *funcs;
 
 	/* Support for live update: connection id. */
 	unsigned int conn_id;
@@ -197,7 +201,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
 struct node *read_node(struct connection *conn, const void *ctx,
 		       const char *name);
 
-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
+struct connection *new_connection(struct interface_funcs *funcs);
 struct connection *get_connection_by_id(unsigned int conn_id);
 void check_store(void);
 void corrupt(struct connection *conn, const char *fmt, ...);
tools/xenstore/xenstored_domain.c +9 -2
index 3d4d0649a2..04f2a974e4 100644
@@ -172,6 +172,13 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
 	return len;
 }
 
+static struct interface_funcs domain_funcs = {
+	.write = writechn,
+	.read = readchn,
+	.can_write = domain_can_write,
+	.can_read = domain_can_read,
+};
+
 static void *map_interface(domid_t domid)
 {
 	return xengnttab_map_grant_ref(*xgt_handle, domid,
@@ -389,7 +396,7 @@ static int new_domain(struct domain *domain, int port, bool restore)
 
 	domain->introduced = true;
 
-	domain->conn = new_connection(writechn, readchn);
+	domain->conn = new_connection(&domain_funcs);
 	if (!domain->conn)  {
 		errno = ENOMEM;
 		return errno;
@@ -1288,7 +1295,7 @@ void read_state_connection(const void *ctx, const void *state)
 	struct domain *domain, *tdomain;
 
 	if (sc->conn_type == XS_STATE_CONN_TYPE_SOCKET) {
-		conn = new_connection(writefd, readfd);
+		conn = new_connection(&socket_funcs);
 		if (!conn)
 			barf("error restoring connection");
 		conn->fd = sc->spec.socket_fd;
Signature · 1 line
2.26.2
#2 lore raw

RFC PATCH 2/4 tools/xenstore: claim resources when running as daemon

In reply to Juergen Gross

Xenstored is absolutely mandatory for a Xen host and it can't be restarted, so being killed by OOM-killer in case of memory shortage is to be avoided.

Set /proc/$pid/oom_score_adj (if available) to -500 in order to allow xenstored to use large amounts of memory without being killed.

In order to support large numbers of domains the limit for open file descriptors might need to be raised. Each domain needs 2 file descriptors (one for the event channel and one for the xl per-domain daemon to connect to xenstored).

Try to raise ulimit for open files to 65536. First the hard limit if needed, and then the soft limit.

---
 tools/xenstore/xenstored_core.c   |  2 ++
 tools/xenstore/xenstored_core.h   |  3 ++
 tools/xenstore/xenstored_minios.c |  4 +++
 tools/xenstore/xenstored_posix.c  | 46 +++++++++++++++++++++++++++++++
 4 files changed, 55 insertions(+)
tools/xenstore/xenstored_core.c +2 -0
index c89f5202fe..a957055d87 100644
@@ -2244,6 +2244,8 @@ int main(int argc, char *argv[])
 		xprintf = trace;
 #endif
 
+	claim_resources();
+
 	signal(SIGHUP, trigger_reopen_log);
 	if (tracefile)
 		tracefile = talloc_strdup(NULL, tracefile);
tools/xenstore/xenstored_core.h +3 -0
index 84b364d82c..052db6a56a 100644
@@ -258,6 +258,9 @@ void daemonize(void);
 /* Close stdin/stdout/stderr to complete daemonize */
 void finish_daemonize(void);
 
+/* Set OOM-killer score and raise ulimit. */
+void claim_resources(void);
+
 /* Open a pipe for signal handling */
 void init_pipe(int reopen_log_pipe[2]);
 
tools/xenstore/xenstored_minios.c +4 -0
index c94493e52a..df8ff580b0 100644
@@ -32,6 +32,10 @@ void finish_daemonize(void)
 {
 }
 
+void claim_resources(void)
+{
+}
+
 void init_pipe(int reopen_log_pipe[2])
 {
 	reopen_log_pipe[0] = -1;
tools/xenstore/xenstored_posix.c +46 -0

58 lines not on this page · raw message

Signature · 1 line
2.26.2
#3 lore raw

RFC PATCH 3/4 tools/xenstore: rework hashtable interfaces

In reply to Juergen Gross

Rework the interfaces to the hashtable code in order to adapt it better to Xenstore:

- don't use user-specified hash- and compare-functions
- modify the type of the key to be const char *
- instead of using calloc() and free() switch to the talloc interfaces
- drop the due to talloc usage no longer needed free_values parameter from hashtable_destroy()
- add a hashtable name parameter to create_hashtable() for the talloc context
- drop the minsize parameter from create_hashtable()
- merge hashtable_private.h into hashtable.c
- add a hashtable_traverse() function to iterate over all entries
- add a hashtable_replace() function to replace an existing entry
- change hashtable_remove() to free the entry and return an int
- modify all int return values to 0 for success
- introduce a define for "no value"
- remove unused macros
- fix a bug in hashtable_expand()

---
 tools/xenstore/hashtable.c             | 234 ++++++++++++++++---------
 tools/xenstore/hashtable.h             | 137 +++++----------
 tools/xenstore/hashtable_private.h     |  85 ---------
 tools/xenstore/xenstored_core.c        |  49 ++----
 tools/xenstore/xenstored_transaction.c |   4 +-
 5 files changed, 211 insertions(+), 298 deletions(-)
 delete mode 100644 tools/xenstore/hashtable_private.h
tools/xenstore/hashtable.c +149 -85
index 394b1cf9d0..6438ec78bf 100644
@@ -1,12 +1,28 @@
 /* Copyright (C) 2004 Christopher Clark <[email protected]> */
 
 #include "hashtable.h"
-#include "hashtable_private.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
 #include <stdint.h>
+#include "talloc.h"
+
+struct entry
+{
+    const char *k;
+    void *v;
+    unsigned int h;
+    struct entry *next;
+};
+
+struct hashtable {
+    unsigned int tablelength;
+    struct entry **table;
+    unsigned int entrycount;
+    unsigned int loadlimit;
+    unsigned int primeindex;
+};
 
 /*
 Credit for primes table: Aaron Krowne
@@ -25,51 +41,57 @@ static const unsigned int primes[] = {
 const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]);
 const unsigned int max_load_factor = 65; /* percentage */
 
+/*****************************************************************************/
+/* indexFor */
+static inline unsigned int
+indexFor(unsigned int tablelength, unsigned int hashvalue) {
+    return (hashvalue % tablelength);
+}
+
+static unsigned int
+loadlimit(unsigned int pindex)
+{
+    return (unsigned int)(((uint64_t)primes[pindex] * max_load_factor) / 100);
+}
+
 /*****************************************************************************/
 struct hashtable *
-create_hashtable(unsigned int minsize,
-                 unsigned int (*hashf) (void*),
-                 int (*eqf) (void*,void*))
+create_hashtable(const char *name)
 {
     struct hashtable *h;
-    unsigned int pindex, size = primes[0];
 
-    /* Check requested hashtable isn't too large */
-    if (minsize > (1u << 30)) return NULL;
-
-    /* Enforce size as prime */
-    for (pindex=0; pindex < prime_table_length; pindex++) {
-        if (primes[pindex] > minsize) { size = primes[pindex]; break; }
-    }
-
-    h = (struct hashtable *)calloc(1, sizeof(struct hashtable));
+    h = talloc_zero(NULL, struct hashtable);
+    talloc_set_name_const(h, name);
     if (NULL == h)
         goto err0;
-    h->table = (struct entry **)calloc(size, sizeof(struct entry *));
+    h->table = talloc_zero_array(h, struct entry *, primes[0]);
     if (NULL == h->table)
         goto err1;
 
-    h->tablelength  = size;
-    h->primeindex   = pindex;
+    h->tablelength  = primes[0];
+    h->primeindex   = 0;
     h->entrycount   = 0;
-    h->hashfn       = hashf;
-    h->eqfn         = eqf;
-    h->loadlimit    = (unsigned int)(((uint64_t)size * max_load_factor) / 100);
+    h->loadlimit    = loadlimit(0);
     return h;
 
 err1:
-   free(h);
+   talloc_free(h);
 err0:
    return NULL;
 }
 
 /*****************************************************************************/
-unsigned int
-hash(struct hashtable *h, void *k)
+static unsigned int
+hash(struct hashtable *h, const char *k)
 {
+    unsigned int i = 5381;
+    char c;
+
+    while ((c = *k++))
+        i = ((i << 5) + i) + c;
+
     /* Aim to protect against poor hash functions by adding logic here
      * - logic taken from java 1.4 hashtable source */
-    unsigned int i = h->hashfn(k);
     i += ~(i << 9);
     i ^=  ((i >> 14) | (i << 18)); /* >>> */
     i +=  (i << 4);
@@ -78,7 +100,7 @@ hash(struct hashtable *h, void *k)
 }
 
 /*****************************************************************************/
-static int
+static void
 hashtable_expand(struct hashtable *h)
 {
     /* Double the size of the table to accomodate more entries */
@@ -87,10 +109,10 @@ hashtable_expand(struct hashtable *h)
     struct entry **pE;
     unsigned int newsize, i, index;
     /* Check we're not hitting max capacity */
-    if (h->primeindex == (prime_table_length - 1)) return 0;
+    if (h->primeindex == (prime_table_length - 1)) return;
     newsize = primes[++(h->primeindex)];
 
-    newtable = (struct entry **)calloc(newsize, sizeof(struct entry*));
+    newtable = talloc_zero_array(h, struct entry *, newsize);
     if (NULL != newtable)
     {
         /* This algorithm is not 'stable'. ie. it reverses the list
@@ -103,17 +125,17 @@ hashtable_expand(struct hashtable *h)
                 newtable[index] = e;
             }
         }
-        free(h->table);
+        talloc_free(h->table);
         h->table = newtable;
     }
     /* Plan B: realloc instead */
     else 
     {
-        newtable = (struct entry **)
-                   realloc(h->table, newsize * sizeof(struct entry *));
-        if (NULL == newtable) { (h->primeindex)--; return 0; }
+        newtable = talloc_realloc(h, h->table, struct entry *, newsize);
+        if (NULL == newtable) { (h->primeindex)--; return; }
         h->table = newtable;
-        memset(newtable[h->tablelength], 0, newsize - h->tablelength);
+        memset(newtable + h->tablelength, 0,
+               (newsize - h->tablelength) * sizeof(*newtable));
         for (i = 0; i < h->tablelength; i++) {
             for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) {
                 index = indexFor(newsize,e->h);
@@ -131,9 +153,7 @@ hashtable_expand(struct hashtable *h)
         }
     }
     h->tablelength = newsize;
-    h->loadlimit   = (unsigned int)
-        (((uint64_t)newsize * max_load_factor) / 100);
-    return -1;
+    h->loadlimit   = loadlimit(h->primeindex);
 }
 
 /*****************************************************************************/
@@ -145,11 +165,71 @@ hashtable_count(struct hashtable *h)
 
 /*****************************************************************************/
 int
-hashtable_insert(struct hashtable *h, void *k, void *v)
+hashtable_traverse(struct hashtable *h,
+                   int (fn)(struct hashtable *, const char *, void *, void *),
+                   void *par)
+{
+    unsigned int i;
+    struct entry *e;
+    struct entry **table = h->table;
+    int ret;
+
+    for (i = 0; i < h->tablelength; i++)
+    {
+        e = table[i];
+        for (e = table[i]; e; e = e->next)
+        {
+            ret = fn(h, e->k, e->v, par);
+            if (ret)
+                return ret;
+        }
+    }
+    return 0;
+}
+
+/*****************************************************************************/
+static struct entry *
+find_entry(struct hashtable *h, const char *k)
+{
+    struct entry *e;
+    unsigned int hashvalue, index;
+
+    hashvalue = hash(h,k);
+    index = indexFor(h->tablelength,hashvalue);
+    e = h->table[index];
+    while (NULL != e)
+    {
+        /* Check hash value to short circuit heavier comparison */
+        if ((hashvalue == e->h) && !strcmp(k, e->k)) return e;
+        e = e->next;
+    }
+    return NULL;
+}
+
+/*****************************************************************************/
+static int
+insert_or_replace(struct hashtable *h, const char *k, void *v, int r)
 {
-    /* This method allows duplicate keys - but they shouldn't be used */
     unsigned int index;
     struct entry *e;
+
+    e = find_entry(h, k);
+    if (e)
+    {
+        if (!r)
+            return -1;
+        if (e->v != HASHTABLE_NO_VALUE && e->v != v)
+            talloc_free(e->v);
+        if (e->k != k)
+            talloc_free((void *)e->k);
+        e->k = k;
+        talloc_steal(e, k);
+        e->v = v;
+        if (v != HASHTABLE_NO_VALUE)
+            talloc_steal(e, v);
+        return 0;
+    }
+
     if (++(h->entrycount) > h->loadlimit)
     {
         /* Ignore the return value. If expand fails, we should
@@ -158,45 +238,53 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
          * element may be ok. Next time we insert, we'll try expanding again.*/
         hashtable_expand(h);
     }
-    e = (struct entry *)calloc(1, sizeof(struct entry));
-    if (NULL == e) { --(h->entrycount); return 0; } /*oom*/
+    e = talloc_zero(h, struct entry);
+    if (NULL == e) { --(h->entrycount); return -1; } /*oom*/
     e->h = hash(h,k);
     index = indexFor(h->tablelength,e->h);
     e->k = k;
+    talloc_steal(e, k);
     e->v = v;
+    if (v != HASHTABLE_NO_VALUE)
+        talloc_steal(e, v);
     e->next = h->table[index];
     h->table[index] = e;
-    return -1;
+    return 0;
+}
+
+/*****************************************************************************/
+int
+hashtable_insert(struct hashtable *h, const char *k, void *v)
+{
+    return insert_or_replace(h, k, v, 0);
+}
+
+/*****************************************************************************/
+int
+hashtable_replace(struct hashtable *h, const char *k, void *v)
+{
+    return insert_or_replace(h, k, v, 1);
 }
 
 /*****************************************************************************/
 void * /* returns value associated with key */
-hashtable_search(struct hashtable *h, void *k)
+hashtable_search(struct hashtable *h, const char *k)
 {
     struct entry *e;
-    unsigned int hashvalue, index;
-    hashvalue = hash(h,k);
-    index = indexFor(h->tablelength,hashvalue);
-    e = h->table[index];
-    while (NULL != e)
-    {
-        /* Check hash value to short circuit heavier comparison */
-        if ((hashvalue == e->h) && (h->eqfn(k, e->k))) return e->v;
-        e = e->next;
-    }
-    return NULL;
+
+    e = find_entry(h, k);
+    return e ? e->v : NULL;
 }
 
 /*****************************************************************************/
-void * /* returns value associated with key */
-hashtable_remove(struct hashtable *h, void *k)
+int
+hashtable_remove(struct hashtable *h, const char *k)
 {
     /* TODO: consider compacting the table when the load factor drops enough,
      *       or provide a 'compact' method. */
 
     struct entry *e;
     struct entry **pE;
-    void *v;
     unsigned int hashvalue, index;
 
     hashvalue = hash(h,k);
@@ -206,49 +294,25 @@ hashtable_remove(struct hashtable *h, void *k)
     while (NULL != e)
     {
         /* Check hash value to short circuit heavier comparison */
-        if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
+        if ((hashvalue == e->h) && !strcmp(k, e->k))
         {
             *pE = e->next;
             h->entrycount--;
-            v = e->v;
-            freekey(e->k);
-            free(e);
-            return v;
+            talloc_free(e);
+            return 0;
         }
         pE = &(e->next);
         e = e->next;
     }
-    return NULL;
+    return -1;
 }
 
 /*****************************************************************************/
 /* destroy */
 void
-hashtable_destroy(struct hashtable *h, int free_values)
+hashtable_destroy(struct hashtable *h)
 {
-    unsigned int i;
-    struct entry *e, *f;
-    struct entry **table = h->table;
-    if (free_values)
-    {
-        for (i = 0; i < h->tablelength; i++)
-        {
-            e = table[i];
-            while (NULL != e)
-            { f = e; e = e->next; freekey(f->k); free(f->v); free(f); }
-        }
-    }
-    else
-    {
-        for (i = 0; i < h->tablelength; i++)
-        {
-            e = table[i];
-            while (NULL != e)
-            { f = e; e = e->next; freekey(f->k); free(f); }
-        }
-    }
-    free(h->table);
-    free(h);
+    talloc_free(h);
 }
 
 /*
tools/xenstore/hashtable.h +44 -93

195 lines not on this page · raw message

tools/xenstore/hashtable_private.h deleted +0 -85

85 lines not on this page · raw message

tools/xenstore/xenstored_core.c +16 -33

94 lines not on this page · raw message

tools/xenstore/xenstored_transaction.c +2 -2
index cd07fb0f21..8b8b38cea7 100644
@@ -607,7 +607,7 @@ int check_transactions(struct hashtable *hash)
 		list_for_each_entry(trans, &conn->transaction_list, list) {
 			tname = talloc_asprintf(trans, "%"PRIu64,
 						trans->generation);
-			if (!tname || !remember_string(hash, tname))
+			if (!tname || remember_string(hash, tname))
 				goto nomem;
 
 			list_for_each_entry(i, &trans->accessed, list) {
@@ -615,7 +615,7 @@ int check_transactions(struct hashtable *hash)
 					continue;
 				tnode = transaction_get_node_name(tname, trans,
 								  i->node);
-				if (!tnode || !remember_string(hash, tnode))
+				if (!tnode || remember_string(hash, tnode))
 					goto nomem;
 				talloc_free(tnode);
 			}
Signature · 1 line
2.26.2
#4 lore raw

RFC PATCH 4/4 tools/xenstore: drop use of tdb

In reply to Juergen Gross

Today all Xenstore nodes are stored in a TDB data base. This makes it rather complicated to walk nodes for different purposes, as all walked nodes need to be fetched from TDB before, and references between nodes are difficult to handle.

So drop using TDB and store the nodes directly in memory making them easily accessible. Use a hash-based lookup mechanism for fast lookup of nodes by their full path.

For now only replace TDB keeping the current access functions.

---
 .gitignore                             |    2 -
 tools/xenstore/Makefile                |    9 +-
 tools/xenstore/tdb.c                   | 1748 ------------------------
 tools/xenstore/tdb.h                   |  134 --
 tools/xenstore/xenstored_core.c        |  146 +-
 tools/xenstore/xenstored_core.h        |   11 +-
 tools/xenstore/xenstored_transaction.c |   67 +-
 tools/xenstore/xenstored_transaction.h |    4 +-
 tools/xenstore/xs_tdb_dump.c           |   86 --
 9 files changed, 76 insertions(+), 2131 deletions(-)
 delete mode 100644 tools/xenstore/tdb.c
 delete mode 100644 tools/xenstore/tdb.h
 delete mode 100644 tools/xenstore/xs_tdb_dump.c
.gitignore +0 -2
index 9e0488e0cd..48dcf9d441 100644
@@ -294,8 +294,6 @@ tools/xenstore/xenstore-control
 tools/xenstore/xenstore-ls
 tools/xenstore/xenstored
 tools/xenstore/xenstored_test
-tools/xenstore/xs_tdb_dump
-tools/xentop/xentop
 tools/xentrace/xentrace_setsize
 tools/xentrace/tbctl
 tools/xentrace/xenctx
tools/xenstore/Makefile +3 -6
index ab89e22d3a..333248f1f3 100644
@@ -24,7 +24,7 @@ CLIENTS += xenstore-write xenstore-ls xenstore-watch
 
 XENSTORED_OBJS = xenstored_core.o xenstored_watch.o xenstored_domain.o
 XENSTORED_OBJS += xenstored_transaction.o xenstored_control.o
-XENSTORED_OBJS += xs_lib.o talloc.o utils.o tdb.o hashtable.o
+XENSTORED_OBJS += xs_lib.o talloc.o utils.o hashtable.o
 
 XENSTORED_OBJS_$(CONFIG_Linux) = xenstored_posix.o
 XENSTORED_OBJS_$(CONFIG_SunOS) = xenstored_solaris.o xenstored_posix.o xenstored_probes.o
@@ -37,7 +37,7 @@ LDLIBS_xenstored += -lrt
 
 ALL_TARGETS = clients
 ifeq ($(XENSTORE_XENSTORED),y)
-ALL_TARGETS += xs_tdb_dump xenstored
+ALL_TARGETS += xenstored
 endif
 
 ifdef CONFIG_STUBDOM
@@ -84,14 +84,11 @@ xenstore: xenstore_client.o
 xenstore-control: xenstore_control.o
 	$(CC) $< $(LDFLAGS) $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
 
-xs_tdb_dump: xs_tdb_dump.o utils.o tdb.o talloc.o
-	$(CC) $^ $(LDFLAGS) -o $@ $(APPEND_LDFLAGS)
-
 .PHONY: clean
 clean:
 	rm -f *.a *.o xenstored_probes.h
 	rm -f xenstored xs_random xs_stress xs_crashme
-	rm -f xs_tdb_dump xenstore-control init-xenstore-domain
+	rm -f xenstore-control init-xenstore-domain
 	rm -f xenstore $(CLIENTS)
 	$(RM) $(DEPS_RM)
 
tools/xenstore/tdb.c deleted +0 -1748

1,748 lines not on this page · raw message

tools/xenstore/tdb.h deleted +0 -134

134 lines not on this page · raw message

tools/xenstore/xenstored_core.c +42 -104

335 lines not on this page · raw message

tools/xenstore/xenstored_core.h +4 -7
index 052db6a56a..d4f135de78 100644
@@ -32,7 +32,6 @@
 #include "xenstore_lib.h"
 #include "xenstore_state.h"
 #include "list.h"
-#include "tdb.h"
 #include "hashtable.h"
 
 #ifndef O_CLOEXEC
@@ -193,11 +192,11 @@ char *canonicalize(struct connection *conn, const void *ctx, const char *node);
 enum xs_perm_type perm_for_conn(struct connection *conn,
 				const struct node_perms *perms);
 
-/* Write a node to the tdb data base. */
-int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
+/* Write a node to the data base. */
+int write_node_raw(struct connection *conn, const char *key, struct node *node,
 		   bool no_quota_check);
 
-/* Get a node from the tdb data base. */
+/* Get a node from the data base. */
 struct node *read_node(struct connection *conn, const void *ctx,
 		       const char *name);
 
@@ -232,7 +231,7 @@ extern char **orig_argv;
 extern char *tracefile;
 extern int tracefd;
 
-extern TDB_CONTEXT *tdb_ctx;
+extern struct hashtable *nodes;
 extern int dom0_domid;
 extern int dom0_event;
 extern int priv_domid;
@@ -272,8 +271,6 @@ extern xengnttab_handle **xgt_handle;
 
 int remember_string(struct hashtable *hash, const char *str);
 
-void set_tdb_key(const char *name, TDB_DATA *key);
-
 const char *dump_state_global(FILE *fp);
 const char *dump_state_buffered_data(FILE *fp, const struct connection *c,
 				     const struct connection *conn,
tools/xenstore/xenstored_transaction.c +25 -42

162 lines not on this page · raw message

tools/xenstore/xenstored_transaction.h +2 -2
index 43a162bea3..89a79ddaa8 100644
@@ -40,11 +40,11 @@ void transaction_entry_dec(struct transaction *trans, unsigned int domid);
 
 /* This node was accessed. */
 int access_node(struct connection *conn, struct node *node,
-                enum node_access_type type, TDB_DATA *key);
+                enum node_access_type type, const char **key);
 
 /* Prepend the transaction to name if appropriate. */
 int transaction_prepend(struct connection *conn, const char *name,
-                        TDB_DATA *key);
+                        const char **key);
 
 void conn_delete_all_transactions(struct connection *conn);
 int check_transactions(struct hashtable *hash);
tools/xenstore/xs_tdb_dump.c deleted +0 -86

86 lines not on this page · raw message

Signature · 1 line
2.26.2