c460ee
From a7a56c079df2eb0253efdd53e1538656c0ce9095 Mon Sep 17 00:00:00 2001
c460ee
From: Yaniv Kaul <ykaul@redhat.com>
c460ee
Date: Mon, 25 Nov 2019 15:37:46 +0200
c460ee
Subject: [PATCH 564/584] store.c/glusterd-store.c: remove sys_stat calls
c460ee
c460ee
Instead of querying for the file size and allocating a char array
c460ee
according to its size, let's just use a fixed size.
c460ee
Those calls are not really needed, and are either expensive or
c460ee
cached anyway. Since we do dynamic allocation/free, let's just use
c460ee
a fixed array instead.
c460ee
c460ee
I'll see if there are other sys_stat() calls that are not really
c460ee
useful and try to eliminate them in separate patches.
c460ee
c460ee
Backport of:
c460ee
> Upstream-patch: https://review.gluster.org/#/c/glusterfs/+/23752/
c460ee
> Change-Id: I76b40e78a52ab38f613fc0cdef4be60e6253bf20
c460ee
> updates: bz#1193929
c460ee
> Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
c460ee
c460ee
BUG: 1925425
c460ee
Change-Id: I76b40e78a52ab38f613fc0cdef4be60e6253bf20
c460ee
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
c460ee
Reviewed-on: https://code.engineering.redhat.com/gerrit/c/rhs-glusterfs/+/244958
c460ee
Tested-by: RHGS Build Bot <nigelb@redhat.com>
c460ee
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
c460ee
---
c460ee
 libglusterfs/src/glusterfs/store.h         |  4 +-
c460ee
 libglusterfs/src/store.c                   | 71 ++++--------------------------
c460ee
 xlators/mgmt/glusterd/src/glusterd-store.c |  5 +--
c460ee
 3 files changed, 12 insertions(+), 68 deletions(-)
c460ee
c460ee
diff --git a/libglusterfs/src/glusterfs/store.h b/libglusterfs/src/glusterfs/store.h
c460ee
index 3b3a24c..f63bd05 100644
c460ee
--- a/libglusterfs/src/glusterfs/store.h
c460ee
+++ b/libglusterfs/src/glusterfs/store.h
c460ee
@@ -59,8 +59,8 @@ int32_t
c460ee
 gf_store_unlink_tmppath(gf_store_handle_t *shandle);
c460ee
 
c460ee
 int
c460ee
-gf_store_read_and_tokenize(FILE *file, char *str, int size, char **iter_key,
c460ee
-                           char **iter_val, gf_store_op_errno_t *store_errno);
c460ee
+gf_store_read_and_tokenize(FILE *file, char **iter_key, char **iter_val,
c460ee
+                           gf_store_op_errno_t *store_errno);
c460ee
 
c460ee
 int32_t
c460ee
 gf_store_retrieve_value(gf_store_handle_t *handle, char *key, char **value);
c460ee
diff --git a/libglusterfs/src/store.c b/libglusterfs/src/store.c
c460ee
index cdf0aea..fa3649b 100644
c460ee
--- a/libglusterfs/src/store.c
c460ee
+++ b/libglusterfs/src/store.c
c460ee
@@ -184,8 +184,8 @@ out:
c460ee
 }
c460ee
 
c460ee
 int
c460ee
-gf_store_read_and_tokenize(FILE *file, char *str, int size, char **iter_key,
c460ee
-                           char **iter_val, gf_store_op_errno_t *store_errno)
c460ee
+gf_store_read_and_tokenize(FILE *file, char **iter_key, char **iter_val,
c460ee
+                           gf_store_op_errno_t *store_errno)
c460ee
 {
c460ee
     int32_t ret = -1;
c460ee
     char *savetok = NULL;
c460ee
@@ -193,15 +193,15 @@ gf_store_read_and_tokenize(FILE *file, char *str, int size, char **iter_key,
c460ee
     char *value = NULL;
c460ee
     char *temp = NULL;
c460ee
     size_t str_len = 0;
c460ee
+    char str[8192];
c460ee
 
c460ee
     GF_ASSERT(file);
c460ee
-    GF_ASSERT(str);
c460ee
     GF_ASSERT(iter_key);
c460ee
     GF_ASSERT(iter_val);
c460ee
     GF_ASSERT(store_errno);
c460ee
 
c460ee
 retry:
c460ee
-    temp = fgets(str, size, file);
c460ee
+    temp = fgets(str, 8192, file);
c460ee
     if (temp == NULL || feof(file)) {
c460ee
         ret = -1;
c460ee
         *store_errno = GD_STORE_EOF;
c460ee
@@ -241,13 +241,8 @@ int32_t
c460ee
 gf_store_retrieve_value(gf_store_handle_t *handle, char *key, char **value)
c460ee
 {
c460ee
     int32_t ret = -1;
c460ee
-    char *scan_str = NULL;
c460ee
     char *iter_key = NULL;
c460ee
     char *iter_val = NULL;
c460ee
-    char *free_str = NULL;
c460ee
-    struct stat st = {
c460ee
-        0,
c460ee
-    };
c460ee
     gf_store_op_errno_t store_errno = GD_STORE_SUCCESS;
c460ee
 
c460ee
     GF_ASSERT(handle);
c460ee
@@ -279,32 +274,9 @@ gf_store_retrieve_value(gf_store_handle_t *handle, char *key, char **value)
c460ee
     } else {
c460ee
         fseek(handle->read, 0, SEEK_SET);
c460ee
     }
c460ee
-    ret = sys_fstat(handle->fd, &st);
c460ee
-    if (ret < 0) {
c460ee
-        gf_msg("", GF_LOG_WARNING, errno, LG_MSG_FILE_OP_FAILED,
c460ee
-               "stat on file %s failed", handle->path);
c460ee
-        ret = -1;
c460ee
-        store_errno = GD_STORE_STAT_FAILED;
c460ee
-        goto out;
c460ee
-    }
c460ee
-
c460ee
-    /* "st.st_size + 1" is used as we are fetching each
c460ee
-     * line of a file using fgets, fgets will append "\0"
c460ee
-     * to the end of the string
c460ee
-     */
c460ee
-    scan_str = GF_CALLOC(1, st.st_size + 1, gf_common_mt_char);
c460ee
-
c460ee
-    if (scan_str == NULL) {
c460ee
-        ret = -1;
c460ee
-        store_errno = GD_STORE_ENOMEM;
c460ee
-        goto out;
c460ee
-    }
c460ee
-
c460ee
-    free_str = scan_str;
c460ee
-
c460ee
     do {
c460ee
-        ret = gf_store_read_and_tokenize(handle->read, scan_str, st.st_size + 1,
c460ee
-                                         &iter_key, &iter_val, &store_errno);
c460ee
+        ret = gf_store_read_and_tokenize(handle->read, &iter_key, &iter_val,
c460ee
+                                         &store_errno);
c460ee
         if (ret < 0) {
c460ee
             gf_msg_trace("", 0,
c460ee
                          "error while reading key '%s': "
c460ee
@@ -334,8 +306,6 @@ out:
c460ee
         sys_close(handle->fd);
c460ee
     }
c460ee
 
c460ee
-    GF_FREE(free_str);
c460ee
-
c460ee
     return ret;
c460ee
 }
c460ee
 
c460ee
@@ -561,40 +531,16 @@ gf_store_iter_get_next(gf_store_iter_t *iter, char **key, char **value,
c460ee
                        gf_store_op_errno_t *op_errno)
c460ee
 {
c460ee
     int32_t ret = -1;
c460ee
-    char *scan_str = NULL;
c460ee
     char *iter_key = NULL;
c460ee
     char *iter_val = NULL;
c460ee
-    struct stat st = {
c460ee
-        0,
c460ee
-    };
c460ee
     gf_store_op_errno_t store_errno = GD_STORE_SUCCESS;
c460ee
 
c460ee
     GF_ASSERT(iter);
c460ee
     GF_ASSERT(key);
c460ee
     GF_ASSERT(value);
c460ee
 
c460ee
-    ret = sys_stat(iter->filepath, &st);
c460ee
-    if (ret < 0) {
c460ee
-        gf_msg("", GF_LOG_WARNING, errno, LG_MSG_FILE_OP_FAILED,
c460ee
-               "stat on file failed");
c460ee
-        ret = -1;
c460ee
-        store_errno = GD_STORE_STAT_FAILED;
c460ee
-        goto out;
c460ee
-    }
c460ee
-
c460ee
-    /* "st.st_size + 1" is used as we are fetching each
c460ee
-     * line of a file using fgets, fgets will append "\0"
c460ee
-     * to the end of the string
c460ee
-     */
c460ee
-    scan_str = GF_CALLOC(1, st.st_size + 1, gf_common_mt_char);
c460ee
-    if (!scan_str) {
c460ee
-        ret = -1;
c460ee
-        store_errno = GD_STORE_ENOMEM;
c460ee
-        goto out;
c460ee
-    }
c460ee
-
c460ee
-    ret = gf_store_read_and_tokenize(iter->file, scan_str, st.st_size + 1,
c460ee
-                                     &iter_key, &iter_val, &store_errno);
c460ee
+    ret = gf_store_read_and_tokenize(iter->file, &iter_key, &iter_val,
c460ee
+                                     &store_errno);
c460ee
     if (ret < 0) {
c460ee
         goto out;
c460ee
     }
c460ee
@@ -619,7 +565,6 @@ gf_store_iter_get_next(gf_store_iter_t *iter, char **key, char **value,
c460ee
     ret = 0;
c460ee
 
c460ee
 out:
c460ee
-    GF_FREE(scan_str);
c460ee
     if (ret) {
c460ee
         GF_FREE(*key);
c460ee
         GF_FREE(*value);
c460ee
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c
c460ee
index 4fa8116..da63c03 100644
c460ee
--- a/xlators/mgmt/glusterd/src/glusterd-store.c
c460ee
+++ b/xlators/mgmt/glusterd/src/glusterd-store.c
c460ee
@@ -4092,7 +4092,6 @@ out:
c460ee
 int32_t
c460ee
 glusterd_store_retrieve_missed_snaps_list(xlator_t *this)
c460ee
 {
c460ee
-    char buf[PATH_MAX] = "";
c460ee
     char path[PATH_MAX] = "";
c460ee
     char *snap_vol_id = NULL;
c460ee
     char *missed_node_info = NULL;
c460ee
@@ -4129,8 +4128,8 @@ glusterd_store_retrieve_missed_snaps_list(xlator_t *this)
c460ee
     }
c460ee
 
c460ee
     do {
c460ee
-        ret = gf_store_read_and_tokenize(
c460ee
-            fp, buf, sizeof(buf), &missed_node_info, &value, &store_errno);
c460ee
+        ret = gf_store_read_and_tokenize(fp, &missed_node_info, &value,
c460ee
+                                         &store_errno);
c460ee
         if (ret) {
c460ee
             if (store_errno == GD_STORE_EOF) {
c460ee
                 gf_msg_debug(this->name, 0, "EOF for missed_snap_list");
c460ee
-- 
c460ee
1.8.3.1
c460ee