[PATCH 02/18] vinyl: move quota methods implementation to vy_quota.c
Vladimir Davydov
vdavydov.dev at gmail.com
Thu Aug 16 19:11:56 MSK 2018
None of vy_quota methods is called from a hot path - even the most
frequently called ones, vy_quota_try_use and vy_quota_commit_use, are
only invoked once per a transactions. So there's no need to clog the
header with the methods implementation.
---
src/box/CMakeLists.txt | 1 +
src/box/vy_quota.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
src/box/vy_quota.h | 110 +++++++---------------------------------
3 files changed, 153 insertions(+), 91 deletions(-)
create mode 100644 src/box/vy_quota.c
diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
index ad544270..cab6a227 100644
--- a/src/box/CMakeLists.txt
+++ b/src/box/CMakeLists.txt
@@ -86,6 +86,7 @@ add_library(box STATIC
vy_history.c
vy_read_set.c
vy_scheduler.c
+ vy_quota.c
request.c
space.c
space_def.c
diff --git a/src/box/vy_quota.c b/src/box/vy_quota.c
new file mode 100644
index 00000000..6e93d652
--- /dev/null
+++ b/src/box/vy_quota.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2010-2018, Tarantool AUTHORS, please see AUTHORS file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include "vy_quota.h"
+
+#include <assert.h>
+#include <stddef.h>
+#include <tarantool_ev.h>
+
+#include "fiber.h"
+#include "fiber_cond.h"
+#include "say.h"
+
+void
+vy_quota_create(struct vy_quota *q, vy_quota_exceeded_f quota_exceeded_cb)
+{
+ q->limit = SIZE_MAX;
+ q->watermark = SIZE_MAX;
+ q->used = 0;
+ q->too_long_threshold = TIMEOUT_INFINITY;
+ q->quota_exceeded_cb = quota_exceeded_cb;
+ fiber_cond_create(&q->cond);
+}
+
+void
+vy_quota_destroy(struct vy_quota *q)
+{
+ fiber_cond_broadcast(&q->cond);
+ fiber_cond_destroy(&q->cond);
+}
+
+void
+vy_quota_set_limit(struct vy_quota *q, size_t limit)
+{
+ q->limit = q->watermark = limit;
+ if (q->used >= limit)
+ q->quota_exceeded_cb(q);
+ fiber_cond_broadcast(&q->cond);
+}
+
+void
+vy_quota_set_watermark(struct vy_quota *q, size_t watermark)
+{
+ q->watermark = watermark;
+ if (q->used >= watermark)
+ q->quota_exceeded_cb(q);
+}
+
+void
+vy_quota_force_use(struct vy_quota *q, size_t size)
+{
+ q->used += size;
+ if (q->used >= q->watermark)
+ q->quota_exceeded_cb(q);
+}
+
+void
+vy_quota_dump(struct vy_quota *q, size_t size)
+{
+ assert(q->used >= size);
+ q->used -= size;
+ fiber_cond_broadcast(&q->cond);
+}
+
+int
+vy_quota_try_use(struct vy_quota *q, size_t size, double timeout)
+{
+ double start_time = ev_monotonic_now(loop());
+ double deadline = start_time + timeout;
+ while (q->used + size > q->limit && timeout > 0) {
+ q->quota_exceeded_cb(q);
+ if (fiber_cond_wait_deadline(&q->cond, deadline) != 0)
+ break; /* timed out */
+ }
+ double wait_time = ev_monotonic_now(loop()) - start_time;
+ if (wait_time > q->too_long_threshold) {
+ say_warn("waited for %zu bytes of vinyl memory quota "
+ "for too long: %.3f sec", size, wait_time);
+ }
+ if (q->used + size > q->limit)
+ return -1;
+ q->used += size;
+ if (q->used >= q->watermark)
+ q->quota_exceeded_cb(q);
+ return 0;
+}
+
+void
+vy_quota_commit_use(struct vy_quota *q, size_t reserved, size_t used)
+{
+ if (reserved > used) {
+ size_t excess = reserved - used;
+ assert(q->used >= excess);
+ q->used -= excess;
+ fiber_cond_broadcast(&q->cond);
+ }
+ if (reserved < used)
+ vy_quota_force_use(q, used - reserved);
+}
+
+void
+vy_quota_wait(struct vy_quota *q)
+{
+ while (q->used > q->limit)
+ fiber_cond_wait(&q->cond);
+}
diff --git a/src/box/vy_quota.h b/src/box/vy_quota.h
index fd1004da..cf70b1ab 100644
--- a/src/box/vy_quota.h
+++ b/src/box/vy_quota.h
@@ -31,13 +31,8 @@
* SUCH DAMAGE.
*/
-#include <stdbool.h>
#include <stddef.h>
-#include <tarantool_ev.h>
-
-#include "fiber.h"
#include "fiber_cond.h"
-#include "say.h"
#if defined(__cplusplus)
extern "C" {
@@ -83,72 +78,39 @@ struct vy_quota {
vy_quota_exceeded_f quota_exceeded_cb;
};
-static inline void
-vy_quota_create(struct vy_quota *q, vy_quota_exceeded_f quota_exceeded_cb)
-{
- q->limit = SIZE_MAX;
- q->watermark = SIZE_MAX;
- q->used = 0;
- q->too_long_threshold = TIMEOUT_INFINITY;
- q->quota_exceeded_cb = quota_exceeded_cb;
- fiber_cond_create(&q->cond);
-}
-
-static inline void
-vy_quota_destroy(struct vy_quota *q)
-{
- fiber_cond_broadcast(&q->cond);
- fiber_cond_destroy(&q->cond);
-}
+void
+vy_quota_create(struct vy_quota *q, vy_quota_exceeded_f quota_exceeded_cb);
+
+void
+vy_quota_destroy(struct vy_quota *q);
/**
* Set memory limit. If current memory usage exceeds
* the new limit, invoke the callback.
*/
-static inline void
-vy_quota_set_limit(struct vy_quota *q, size_t limit)
-{
- q->limit = q->watermark = limit;
- if (q->used >= limit)
- q->quota_exceeded_cb(q);
- fiber_cond_broadcast(&q->cond);
-}
+void
+vy_quota_set_limit(struct vy_quota *q, size_t limit);
/**
* Set memory watermark. If current memory usage exceeds
* the new watermark, invoke the callback.
*/
-static inline void
-vy_quota_set_watermark(struct vy_quota *q, size_t watermark)
-{
- q->watermark = watermark;
- if (q->used >= watermark)
- q->quota_exceeded_cb(q);
-}
+void
+vy_quota_set_watermark(struct vy_quota *q, size_t watermark);
/**
* Consume @size bytes of memory. In contrast to vy_quota_try_use()
* this function does not throttle the caller.
*/
-static inline void
-vy_quota_force_use(struct vy_quota *q, size_t size)
-{
- q->used += size;
- if (q->used >= q->watermark)
- q->quota_exceeded_cb(q);
-}
+void
+vy_quota_force_use(struct vy_quota *q, size_t size);
/**
* Function called on dump completion to release quota after
* freeing memory.
*/
-static inline void
-vy_quota_dump(struct vy_quota *q, size_t size)
-{
- assert(q->used >= size);
- q->used -= size;
- fiber_cond_broadcast(&q->cond);
-}
+void
+vy_quota_dump(struct vy_quota *q, size_t size);
/**
* Try to consume @size bytes of memory, throttle the caller
@@ -184,28 +146,8 @@ vy_quota_dump(struct vy_quota *q, size_t size)
* are stored in the common memory level, which isn't taken into
* account while estimating the size of a memory allocation.
*/
-static inline int
-vy_quota_try_use(struct vy_quota *q, size_t size, double timeout)
-{
- double start_time = ev_monotonic_now(loop());
- double deadline = start_time + timeout;
- while (q->used + size > q->limit && timeout > 0) {
- q->quota_exceeded_cb(q);
- if (fiber_cond_wait_deadline(&q->cond, deadline) != 0)
- break; /* timed out */
- }
- double wait_time = ev_monotonic_now(loop()) - start_time;
- if (wait_time > q->too_long_threshold) {
- say_warn("waited for %zu bytes of vinyl memory quota "
- "for too long: %.3f sec", size, wait_time);
- }
- if (q->used + size > q->limit)
- return -1;
- q->used += size;
- if (q->used >= q->watermark)
- q->quota_exceeded_cb(q);
- return 0;
-}
+int
+vy_quota_try_use(struct vy_quota *q, size_t size, double timeout);
/**
* Adjust quota after allocating memory.
@@ -215,28 +157,14 @@ vy_quota_try_use(struct vy_quota *q, size_t size, double timeout)
*
* See also vy_quota_try_use().
*/
-static inline void
-vy_quota_commit_use(struct vy_quota *q, size_t reserved, size_t used)
-{
- if (reserved > used) {
- size_t excess = reserved - used;
- assert(q->used >= excess);
- q->used -= excess;
- fiber_cond_broadcast(&q->cond);
- }
- if (reserved < used)
- vy_quota_force_use(q, used - reserved);
-}
+void
+vy_quota_commit_use(struct vy_quota *q, size_t reserved, size_t used);
/**
* Block the caller until the quota is not exceeded.
*/
-static inline void
-vy_quota_wait(struct vy_quota *q)
-{
- while (q->used > q->limit)
- fiber_cond_wait(&q->cond);
-}
+void
+vy_quota_wait(struct vy_quota *q);
#if defined(__cplusplus)
} /* extern "C" */
--
2.11.0
More information about the Tarantool-patches
mailing list