[Libguestfs] [nbdkit PATCH 5/9] plugins: Implement no-op .cache for in-memory plugins

Eric Blake eblake at redhat.com
Fri May 10 03:03:38 UTC 2019


For our plugins which have no backing file but generate everything on
the fly or store things in memory, falling back to .pread on a cache
request is just wasted work. Implement a no-op .cache callback for the
drivers where there is no benefit to trying to cache anything.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 plugins/data/data.c           | 10 ++++++++++
 plugins/full/full.c           | 12 +++++++++++-
 plugins/memory/memory.c       | 10 ++++++++++
 plugins/null/null.c           | 12 +++++++++++-
 plugins/pattern/pattern.c     | 12 +++++++++++-
 plugins/random/random.c       | 12 +++++++++++-
 plugins/streaming/streaming.c | 11 +++++++++++
 plugins/zero/zero.c           | 12 +++++++++++-
 8 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/plugins/data/data.c b/plugins/data/data.c
index 55380c6..aaa3d2d 100644
--- a/plugins/data/data.c
+++ b/plugins/data/data.c
@@ -377,6 +377,15 @@ data_extents (void *handle, uint32_t count, uint64_t offset,
   return sparse_array_extents (sa, count, offset, extents);
 }

+/* Cache. */
+static int
+data_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "data",
   .version           = PACKAGE_VERSION,
@@ -394,6 +403,7 @@ static struct nbdkit_plugin plugin = {
   .zero              = data_zero,
   .trim              = data_trim,
   .extents           = data_extents,
+  .cache             = data_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
diff --git a/plugins/full/full.c b/plugins/full/full.c
index 7661856..51a9d67 100644
--- a/plugins/full/full.c
+++ b/plugins/full/full.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -137,6 +137,15 @@ full_extents (void *handle, uint32_t count, uint64_t offset, uint32_t flags,
                             NBDKIT_EXTENT_HOLE | NBDKIT_EXTENT_ZERO);
 }

+/* Cache. */
+static int
+full_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 /* Note that we don't need to handle flush: If there has been previous
  * write then we have already returned an error.  If there have been
  * no previous writes then flush can be ignored.
@@ -156,6 +165,7 @@ static struct nbdkit_plugin plugin = {
   .zero              = full_zero,
   .trim              = full_trim,
   .extents           = full_extents,
+  .cache             = full_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
diff --git a/plugins/memory/memory.c b/plugins/memory/memory.c
index 90fa99e..baa29e2 100644
--- a/plugins/memory/memory.c
+++ b/plugins/memory/memory.c
@@ -172,6 +172,15 @@ memory_extents (void *handle, uint32_t count, uint64_t offset,
   return sparse_array_extents (sa, count, offset, extents);
 }

+/* Cache. */
+static int
+memory_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "memory",
   .version           = PACKAGE_VERSION,
@@ -189,6 +198,7 @@ static struct nbdkit_plugin plugin = {
   .zero              = memory_zero,
   .trim              = memory_trim,
   .extents           = memory_extents,
+  .cache             = memory_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
diff --git a/plugins/null/null.c b/plugins/null/null.c
index 518b63b..5e40868 100644
--- a/plugins/null/null.c
+++ b/plugins/null/null.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -140,6 +140,15 @@ null_extents (void *handle, uint32_t count, uint64_t offset, uint32_t flags,
                             NBDKIT_EXTENT_HOLE | NBDKIT_EXTENT_ZERO);
 }

+/* Cache. */
+static int
+null_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "null",
   .version           = PACKAGE_VERSION,
@@ -155,6 +164,7 @@ static struct nbdkit_plugin plugin = {
   .can_fua           = null_can_fua,
   .flush             = null_flush,
   .extents           = null_extents,
+  .cache             = null_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
diff --git a/plugins/pattern/pattern.c b/plugins/pattern/pattern.c
index 115bd96..e25da36 100644
--- a/plugins/pattern/pattern.c
+++ b/plugins/pattern/pattern.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -117,6 +117,15 @@ pattern_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
   return 0;
 }

+/* Cache. */
+static int
+pattern_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "pattern",
   .version           = PACKAGE_VERSION,
@@ -127,6 +136,7 @@ static struct nbdkit_plugin plugin = {
   .get_size          = pattern_get_size,
   .can_multi_conn    = pattern_can_multi_conn,
   .pread             = pattern_pread,
+  .cache             = pattern_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
diff --git a/plugins/random/random.c b/plugins/random/random.c
index 7fb42c8..6219169 100644
--- a/plugins/random/random.c
+++ b/plugins/random/random.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -146,6 +146,15 @@ random_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
   return 0;
 }

+/* Cache. */
+static int
+random_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "random",
   .version           = PACKAGE_VERSION,
@@ -157,6 +166,7 @@ static struct nbdkit_plugin plugin = {
   .get_size          = random_get_size,
   .can_multi_conn    = random_can_multi_conn,
   .pread             = random_pread,
+  .cache             = random_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
diff --git a/plugins/streaming/streaming.c b/plugins/streaming/streaming.c
index 4ca3e76..505ed91 100644
--- a/plugins/streaming/streaming.c
+++ b/plugins/streaming/streaming.c
@@ -246,6 +246,16 @@ streaming_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
   return -1;
 }

+/* Cache. */
+static int
+streaming_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* nbdkit's default of falling back to .pread is pointless: it will
+   * fail for addresses already written, and waste a memset() for
+   * addresses not yet reached. Treat this as a no-op instead. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "streaming",
   .longname          = "nbdkit streaming plugin",
@@ -259,6 +269,7 @@ static struct nbdkit_plugin plugin = {
   .get_size          = streaming_get_size,
   .pwrite            = streaming_pwrite,
   .pread             = streaming_pread,
+  .cache             = streaming_cache,
   .errno_is_preserved = 1,
 };

diff --git a/plugins/zero/zero.c b/plugins/zero/zero.c
index 49ce08e..be8fc2a 100644
--- a/plugins/zero/zero.c
+++ b/plugins/zero/zero.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -76,6 +76,15 @@ zero_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
   return -1;
 }

+/* Cache. */
+static int
+zero_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
+{
+  /* Everything is already in memory, falling back to .pread is
+     actually slower than treating this as a no-op. */
+  return 0;
+}
+
 static struct nbdkit_plugin plugin = {
   .name              = "zero",
   .version           = PACKAGE_VERSION,
@@ -83,6 +92,7 @@ static struct nbdkit_plugin plugin = {
   .open              = zero_open,
   .get_size          = zero_get_size,
   .pread             = zero_pread,
+  .cache             = zero_cache,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
    */
-- 
2.20.1




More information about the Libguestfs mailing list