Blob Blame History Raw
From a7a56c079df2eb0253efdd53e1538656c0ce9095 Mon Sep 17 00:00:00 2001
From: Yaniv Kaul <ykaul@redhat.com>
Date: Mon, 25 Nov 2019 15:37:46 +0200
Subject: [PATCH 564/584] store.c/glusterd-store.c: remove sys_stat calls

Instead of querying for the file size and allocating a char array
according to its size, let's just use a fixed size.
Those calls are not really needed, and are either expensive or
cached anyway. Since we do dynamic allocation/free, let's just use
a fixed array instead.

I'll see if there are other sys_stat() calls that are not really
useful and try to eliminate them in separate patches.

Backport of:
> Upstream-patch: https://review.gluster.org/#/c/glusterfs/+/23752/
> Change-Id: I76b40e78a52ab38f613fc0cdef4be60e6253bf20
> updates: bz#1193929
> Signed-off-by: Yaniv Kaul <ykaul@redhat.com>

BUG: 1925425
Change-Id: I76b40e78a52ab38f613fc0cdef4be60e6253bf20
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/c/rhs-glusterfs/+/244958
Tested-by: RHGS Build Bot <nigelb@redhat.com>
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
---
 libglusterfs/src/glusterfs/store.h         |  4 +-
 libglusterfs/src/store.c                   | 71 ++++--------------------------
 xlators/mgmt/glusterd/src/glusterd-store.c |  5 +--
 3 files changed, 12 insertions(+), 68 deletions(-)

diff --git a/libglusterfs/src/glusterfs/store.h b/libglusterfs/src/glusterfs/store.h
index 3b3a24c..f63bd05 100644
--- a/libglusterfs/src/glusterfs/store.h
+++ b/libglusterfs/src/glusterfs/store.h
@@ -59,8 +59,8 @@ int32_t
 gf_store_unlink_tmppath(gf_store_handle_t *shandle);
 
 int
-gf_store_read_and_tokenize(FILE *file, char *str, int size, char **iter_key,
-                           char **iter_val, gf_store_op_errno_t *store_errno);
+gf_store_read_and_tokenize(FILE *file, char **iter_key, char **iter_val,
+                           gf_store_op_errno_t *store_errno);
 
 int32_t
 gf_store_retrieve_value(gf_store_handle_t *handle, char *key, char **value);
diff --git a/libglusterfs/src/store.c b/libglusterfs/src/store.c
index cdf0aea..fa3649b 100644
--- a/libglusterfs/src/store.c
+++ b/libglusterfs/src/store.c
@@ -184,8 +184,8 @@ out:
 }
 
 int
-gf_store_read_and_tokenize(FILE *file, char *str, int size, char **iter_key,
-                           char **iter_val, gf_store_op_errno_t *store_errno)
+gf_store_read_and_tokenize(FILE *file, char **iter_key, char **iter_val,
+                           gf_store_op_errno_t *store_errno)
 {
     int32_t ret = -1;
     char *savetok = NULL;
@@ -193,15 +193,15 @@ gf_store_read_and_tokenize(FILE *file, char *str, int size, char **iter_key,
     char *value = NULL;
     char *temp = NULL;
     size_t str_len = 0;
+    char str[8192];
 
     GF_ASSERT(file);
-    GF_ASSERT(str);
     GF_ASSERT(iter_key);
     GF_ASSERT(iter_val);
     GF_ASSERT(store_errno);
 
 retry:
-    temp = fgets(str, size, file);
+    temp = fgets(str, 8192, file);
     if (temp == NULL || feof(file)) {
         ret = -1;
         *store_errno = GD_STORE_EOF;
@@ -241,13 +241,8 @@ int32_t
 gf_store_retrieve_value(gf_store_handle_t *handle, char *key, char **value)
 {
     int32_t ret = -1;
-    char *scan_str = NULL;
     char *iter_key = NULL;
     char *iter_val = NULL;
-    char *free_str = NULL;
-    struct stat st = {
-        0,
-    };
     gf_store_op_errno_t store_errno = GD_STORE_SUCCESS;
 
     GF_ASSERT(handle);
@@ -279,32 +274,9 @@ gf_store_retrieve_value(gf_store_handle_t *handle, char *key, char **value)
     } else {
         fseek(handle->read, 0, SEEK_SET);
     }
-    ret = sys_fstat(handle->fd, &st);
-    if (ret < 0) {
-        gf_msg("", GF_LOG_WARNING, errno, LG_MSG_FILE_OP_FAILED,
-               "stat on file %s failed", handle->path);
-        ret = -1;
-        store_errno = GD_STORE_STAT_FAILED;
-        goto out;
-    }
-
-    /* "st.st_size + 1" is used as we are fetching each
-     * line of a file using fgets, fgets will append "\0"
-     * to the end of the string
-     */
-    scan_str = GF_CALLOC(1, st.st_size + 1, gf_common_mt_char);
-
-    if (scan_str == NULL) {
-        ret = -1;
-        store_errno = GD_STORE_ENOMEM;
-        goto out;
-    }
-
-    free_str = scan_str;
-
     do {
-        ret = gf_store_read_and_tokenize(handle->read, scan_str, st.st_size + 1,
-                                         &iter_key, &iter_val, &store_errno);
+        ret = gf_store_read_and_tokenize(handle->read, &iter_key, &iter_val,
+                                         &store_errno);
         if (ret < 0) {
             gf_msg_trace("", 0,
                          "error while reading key '%s': "
@@ -334,8 +306,6 @@ out:
         sys_close(handle->fd);
     }
 
-    GF_FREE(free_str);
-
     return ret;
 }
 
@@ -561,40 +531,16 @@ gf_store_iter_get_next(gf_store_iter_t *iter, char **key, char **value,
                        gf_store_op_errno_t *op_errno)
 {
     int32_t ret = -1;
-    char *scan_str = NULL;
     char *iter_key = NULL;
     char *iter_val = NULL;
-    struct stat st = {
-        0,
-    };
     gf_store_op_errno_t store_errno = GD_STORE_SUCCESS;
 
     GF_ASSERT(iter);
     GF_ASSERT(key);
     GF_ASSERT(value);
 
-    ret = sys_stat(iter->filepath, &st);
-    if (ret < 0) {
-        gf_msg("", GF_LOG_WARNING, errno, LG_MSG_FILE_OP_FAILED,
-               "stat on file failed");
-        ret = -1;
-        store_errno = GD_STORE_STAT_FAILED;
-        goto out;
-    }
-
-    /* "st.st_size + 1" is used as we are fetching each
-     * line of a file using fgets, fgets will append "\0"
-     * to the end of the string
-     */
-    scan_str = GF_CALLOC(1, st.st_size + 1, gf_common_mt_char);
-    if (!scan_str) {
-        ret = -1;
-        store_errno = GD_STORE_ENOMEM;
-        goto out;
-    }
-
-    ret = gf_store_read_and_tokenize(iter->file, scan_str, st.st_size + 1,
-                                     &iter_key, &iter_val, &store_errno);
+    ret = gf_store_read_and_tokenize(iter->file, &iter_key, &iter_val,
+                                     &store_errno);
     if (ret < 0) {
         goto out;
     }
@@ -619,7 +565,6 @@ gf_store_iter_get_next(gf_store_iter_t *iter, char **key, char **value,
     ret = 0;
 
 out:
-    GF_FREE(scan_str);
     if (ret) {
         GF_FREE(*key);
         GF_FREE(*value);
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c
index 4fa8116..da63c03 100644
--- a/xlators/mgmt/glusterd/src/glusterd-store.c
+++ b/xlators/mgmt/glusterd/src/glusterd-store.c
@@ -4092,7 +4092,6 @@ out:
 int32_t
 glusterd_store_retrieve_missed_snaps_list(xlator_t *this)
 {
-    char buf[PATH_MAX] = "";
     char path[PATH_MAX] = "";
     char *snap_vol_id = NULL;
     char *missed_node_info = NULL;
@@ -4129,8 +4128,8 @@ glusterd_store_retrieve_missed_snaps_list(xlator_t *this)
     }
 
     do {
-        ret = gf_store_read_and_tokenize(
-            fp, buf, sizeof(buf), &missed_node_info, &value, &store_errno);
+        ret = gf_store_read_and_tokenize(fp, &missed_node_info, &value,
+                                         &store_errno);
         if (ret) {
             if (store_errno == GD_STORE_EOF) {
                 gf_msg_debug(this->name, 0, "EOF for missed_snap_list");
-- 
1.8.3.1