Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 2/3] Use static_alloc() instead of 'static char[]' where possible
Date: Sun, 28 Apr 2019 19:56:26 +0300	[thread overview]
Message-ID: <8de9fb5ed675fcc1ae9669f001a887f27245d46d.1556470563.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1556470563.git.v.shpilevoy@tarantool.org>

This patch harnesses freshly introduced static memory allocator
to eliminate wasteful usage of BSS memory. This commit frees
11Kb per each thread.
---
 src/box/iproto.cc      |  2 +-
 src/box/memtx_engine.c |  8 ++++----
 src/box/xlog.c         | 24 ++++++++++--------------
 src/box/xlog.h         |  2 +-
 src/cfg.c              | 15 +++++----------
 src/httpc.c            |  7 +++++--
 src/lib/core/sio.c     | 10 +++++-----
 7 files changed, 31 insertions(+), 37 deletions(-)

diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index b9d2004f0..873548e09 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -1810,7 +1810,7 @@ tx_process_connect(struct cmsg *m)
 			diag_raise();
 		con->session->meta.connection = con;
 		tx_fiber_init(con->session, 0);
-		static __thread char greeting[IPROTO_GREETING_SIZE];
+		char *greeting = static_alloc(IPROTO_GREETING_SIZE);
 		/* TODO: dirty read from tx thread */
 		struct tt_uuid uuid = INSTANCE_UUID;
 		random_bytes(con->salt, IPROTO_SALT_SIZE);
diff --git a/src/box/memtx_engine.c b/src/box/memtx_engine.c
index 4d99910cb..64a311b64 100644
--- a/src/box/memtx_engine.c
+++ b/src/box/memtx_engine.c
@@ -771,7 +771,7 @@ memtx_engine_commit_checkpoint(struct engine *engine,
 		char to[PATH_MAX];
 		snprintf(to, sizeof(to), "%s",
 			 xdir_format_filename(dir, lsn, NONE));
-		char *from = xdir_format_filename(dir, lsn, INPROGRESS);
+		const char *from = xdir_format_filename(dir, lsn, INPROGRESS);
 #ifndef NDEBUG
 		struct errinj *delay = errinj(ERRINJ_SNAP_COMMIT_DELAY,
 					       ERRINJ_BOOL);
@@ -814,7 +814,7 @@ memtx_engine_abort_checkpoint(struct engine *engine)
 	small_alloc_setopt(&memtx->alloc, SMALL_DELAYED_FREE_MODE, false);
 
 	/** Remove garbage .inprogress file. */
-	char *filename =
+	const char *filename =
 		xdir_format_filename(&memtx->checkpoint->dir,
 				     vclock_sum(&memtx->checkpoint->vclock),
 				     INPROGRESS);
@@ -837,8 +837,8 @@ memtx_engine_backup(struct engine *engine, const struct vclock *vclock,
 		    engine_backup_cb cb, void *cb_arg)
 {
 	struct memtx_engine *memtx = (struct memtx_engine *)engine;
-	char *filename = xdir_format_filename(&memtx->snap_dir,
-					      vclock_sum(vclock), NONE);
+	const char *filename = xdir_format_filename(&memtx->snap_dir,
+						    vclock_sum(vclock), NONE);
 	return cb(filename, cb_arg);
 }
 
diff --git a/src/box/xlog.c b/src/box/xlog.c
index b70e8e25f..8254cce20 100644
--- a/src/box/xlog.c
+++ b/src/box/xlog.c
@@ -41,7 +41,7 @@
 #include <msgpuck.h>
 
 #include "coio_file.h"
-
+#include "tt_static.h"
 #include "error.h"
 #include "xrow.h"
 #include "iproto_constants.h"
@@ -646,17 +646,14 @@ xdir_check(struct xdir *dir)
 	return 0;
 }
 
-char *
+const char *
 xdir_format_filename(struct xdir *dir, int64_t signature,
 		enum log_suffix suffix)
 {
-	static __thread char filename[PATH_MAX + 1];
-	const char *suffix_str = (suffix == INPROGRESS ?
-				  inprogress_suffix : "");
-	snprintf(filename, PATH_MAX, "%s/%020lld%s%s",
-		 dir->dirname, (long long) signature,
-		 dir->filename_ext, suffix_str);
-	return filename;
+	return tt_snprintf(PATH_MAX + 1, "%s/%020lld%s%s",
+			   dir->dirname, (long long) signature,
+			   dir->filename_ext, suffix == INPROGRESS ?
+					      inprogress_suffix : "");
 }
 
 static void
@@ -683,8 +680,8 @@ xdir_collect_garbage(struct xdir *dir, int64_t signature, unsigned flags)
 	struct vclock *vclock;
 	while ((vclock = vclockset_first(&dir->index)) != NULL &&
 	       vclock_sum(vclock) < signature) {
-		char *filename = xdir_format_filename(dir, vclock_sum(vclock),
-						      NONE);
+		const char *filename =
+			xdir_format_filename(dir, vclock_sum(vclock), NONE);
 		if (flags & XDIR_GC_ASYNC)
 			eio_unlink(filename, 0, xdir_complete_gc, NULL);
 		else
@@ -946,9 +943,8 @@ err:
 int
 xdir_touch_xlog(struct xdir *dir, const struct vclock *vclock)
 {
-	char *filename;
 	int64_t signature = vclock_sum(vclock);
-	filename = xdir_format_filename(dir, signature, NONE);
+	const char *filename = xdir_format_filename(dir, signature, NONE);
 
 	if (dir->type != SNAP) {
 		assert(false);
@@ -987,7 +983,7 @@ xdir_create_xlog(struct xdir *dir, struct xlog *xlog,
 	xlog_meta_create(&meta, dir->filetype, dir->instance_uuid,
 			 vclock, prev_vclock);
 
-	char *filename = xdir_format_filename(dir, signature, NONE);
+	const char *filename = xdir_format_filename(dir, signature, NONE);
 	if (xlog_create(xlog, filename, dir->open_wflags, &meta,
 			&dir->opts) != 0)
 		return -1;
diff --git a/src/box/xlog.h b/src/box/xlog.h
index 6539d1b0b..a48b05fc4 100644
--- a/src/box/xlog.h
+++ b/src/box/xlog.h
@@ -199,7 +199,7 @@ xdir_check(struct xdir *dir);
  * Return a file name based on directory type, vector clock
  * sum, and a suffix (.inprogress or not).
  */
-char *
+const char *
 xdir_format_filename(struct xdir *dir, int64_t signature,
 		     enum log_suffix suffix);
 
diff --git a/src/cfg.c b/src/cfg.c
index 78654be62..46cff1999 100644
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -31,14 +31,15 @@
 #include "say.h"
 #include "cfg.h"
 #include "lua/utils.h"
+#include "tt_static.h"
 
-enum { MAX_OPT_NAME_LEN = 256, MAX_OPT_VAL_LEN = 256, MAX_STR_OPTS = 8 };
+enum { MAX_OPT_NAME_LEN = 256, MAX_OPT_VAL_LEN = 256 };
 
 static void
 cfg_get(const char *param)
 {
-	char buf[MAX_OPT_NAME_LEN];
-	snprintf(buf, sizeof(buf), "return box.cfg.%s", param);
+	const char *buf =
+		tt_snprintf(MAX_OPT_NAME_LEN, "return box.cfg.%s", param);
 	if (luaL_dostring(tarantool_L, buf) != 0)
 		panic("cfg_get('%s')", param);
 }
@@ -90,15 +91,9 @@ cfg_geti64(const char *param)
 static const char *
 cfg_tostring(struct lua_State *L)
 {
-	static __thread char values[MAX_STR_OPTS][MAX_OPT_VAL_LEN];
-	static __thread int i = 0;
 	if (lua_isnil(L, -1))
 		return NULL;
-	else {
-		snprintf(values[i % MAX_STR_OPTS], MAX_OPT_VAL_LEN,
-			 "%s", lua_tostring(L, -1));
-		return values[i++ % MAX_STR_OPTS];
-	}
+	return tt_snprintf(MAX_OPT_VAL_LEN, "%s", lua_tostring(L, -1));
 }
 
 const char *
diff --git a/src/httpc.c b/src/httpc.c
index 65eeaa743..80dabd59c 100644
--- a/src/httpc.c
+++ b/src/httpc.c
@@ -33,12 +33,15 @@
 
 #include <assert.h>
 #include <curl/curl.h>
-
+#include "tt_static.h"
 #include "fiber.h"
 #include "errinj.h"
 
 #define MAX_HEADER_LEN 8192
 
+static_assert(MAX_HEADER_LEN < SMALL_STATIC_SIZE,
+	      "HTTP header fits into the static buffer");
+
 /** The HTTP headers that may be set automatically. */
 #define HTTP_ACCEPT_HEADER	"Accept:"
 #define HTTP_CONNECTION_HEADER	"Connection:"
@@ -178,7 +181,7 @@ httpc_request_delete(struct httpc_request *req)
 int
 httpc_set_header(struct httpc_request *req, const char *fmt, ...)
 {
-	static __thread char header[MAX_HEADER_LEN + 1];
+	char *header = static_alloc(MAX_HEADER_LEN + 1);
 	va_list ap;
 	va_start(ap, fmt);
 	int rc = vsnprintf(header, MAX_HEADER_LEN + 1, fmt, ap);
diff --git a/src/lib/core/sio.c b/src/lib/core/sio.c
index 64aae97bc..996b7faad 100644
--- a/src/lib/core/sio.c
+++ b/src/lib/core/sio.c
@@ -51,22 +51,22 @@ sio_socketname(int fd)
 {
 	/* Preserve errno */
 	int save_errno = errno;
-	static __thread char name[2 * SERVICE_NAME_MAXLEN];
-	int n = snprintf(name, sizeof(name), "fd %d", fd);
+	int name_size = 2 * SERVICE_NAME_MAXLEN;
+	char *name = static_alloc(name_size);
+	int n = snprintf(name, name_size, "fd %d", fd);
 	if (fd >= 0) {
 		struct sockaddr_storage addr;
 		socklen_t addrlen = sizeof(addr);
 		int rc = getsockname(fd, (struct sockaddr *) &addr, &addrlen);
 		if (rc == 0) {
-			n += snprintf(name + n,
-				sizeof(name) - n, ", aka %s",
+			n += snprintf(name + n, name_size - n, ", aka %s",
 				sio_strfaddr((struct sockaddr *)&addr,
 								addrlen));
 		}
 		addrlen = sizeof(addr);
 		rc = getpeername(fd, (struct sockaddr *) &addr, &addrlen);
 		if (rc == 0) {
-			n += snprintf(name + n, sizeof(name) - n,
+			n += snprintf(name + n, name_size - n,
 				      ", peer of %s",
 				      sio_strfaddr((struct sockaddr *)&addr,
 								addrlen));
-- 
2.20.1 (Apple Git-117)

  parent reply	other threads:[~2019-04-28 16:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-28 16:56 [tarantool-patches] [PATCH 0/3] introduce static allocator Vladislav Shpilevoy
2019-04-28 16:56 ` [tarantool-patches] [PATCH 1/3] small: introduce small/static Vladislav Shpilevoy
2019-04-28 16:56 ` Vladislav Shpilevoy [this message]
2019-04-28 16:56 ` [tarantool-patches] [PATCH 3/3] sio: optimize sio_strfaddr() for the most common case Vladislav Shpilevoy
2019-05-14  8:09   ` Vladimir Davydov
2019-04-29  4:14 ` [tarantool-patches] Re: [PATCH 0/3] introduce static allocator Konstantin Osipov
2019-04-29  8:35   ` Vladislav Shpilevoy
2019-05-13 22:57 ` Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8de9fb5ed675fcc1ae9669f001a887f27245d46d.1556470563.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 2/3] Use static_alloc() instead of '\''static char[]'\'' where possible' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox