From: Lee Duncan <[email protected]>

When scanning current session to find a match for
a particular session, keep track of the current session
and sort the list such that this session is presented
first in sort order. This will make matching the current
session orders of magnitude faster when hundreds of
sessions are present.

Signed-off-by: Lee Duncan <[email protected]>
---
 usr/iscsi_sysfs.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/usr/iscsi_sysfs.c b/usr/iscsi_sysfs.c
index c916ed8e11a1..61414aff0495 100644
--- a/usr/iscsi_sysfs.c
+++ b/usr/iscsi_sysfs.c
@@ -1407,6 +1407,28 @@ int iscsi_sysfs_get_sessioninfo_by_id(struct 
session_info *info, char *session)
        return 0;
 }
 
+/*
+ * If one of the names being sorted matches 'last_valid_session',
+ * return a value which makes the last_valid_session appear to be the
+ * 'lowest' string (in sort order). This will sort last_valid_session
+ * so it will be the first item on the list to speed up the session
+ * lookup. Otherwise, let alphasort() return a normal sort-order
+ * result.
+ */
+static char *last_valid_session;
+int valid_session_alphasort(const struct dirent **s1, const struct dirent **s2)
+{
+       if (last_valid_session &&
+           !strcmp(last_valid_session, (*s1)->d_name)) {
+               return -1;
+       }
+       if (last_valid_session &&
+           !strcmp(last_valid_session, (*s2)->d_name)) {
+               return 1;
+       }
+       return alphasort(s1, s2);
+}
+
 int iscsi_sysfs_for_each_session(void *data, int *nr_found,
                                 iscsi_sysfs_session_op_fn *fn,
                                 int in_parallel)
@@ -1421,7 +1443,7 @@ int iscsi_sysfs_for_each_session(void *data, int 
*nr_found,
                return ISCSI_ERR_NOMEM;
 
        n = scandir(ISCSI_SESSION_DIR, &namelist, trans_filter,
-                   alphasort);
+                   valid_session_alphasort);
        if (n <= 0)
                goto free_info;
 
@@ -1484,6 +1506,34 @@ int iscsi_sysfs_for_each_session(void *data, int 
*nr_found,
                }
        }
 
+       /*
+        * If rc > 0 then a session ID match has been found. If there
+        * is already a session ID in last_valid_session see if the
+        * session ID is changing. If so, then delete the old
+        * last_valid_session number and mark last_valid_session as
+        * NULL. It will be filled in directly.
+        *
+        * If last_valid_session is NULL update it with the current
+        * session ID that was found and flush the lookup cache. The
+        * cache will get repopulated on the next lookup cycle but
+        * the cache will get filled with information from just the
+        * current session ID since it will be sorted to the top of
+        * the scan list so it will be the first session scanned and
+        * matched.
+        */
+       if (rc > 0) {
+               if (last_valid_session &&
+                   strcmp(last_valid_session, namelist[i]->d_name)) {
+                       free(last_valid_session);
+                       last_valid_session = NULL;
+               }
+
+               if (last_valid_session == NULL) {
+                       last_valid_session = strdup(namelist[i]->d_name);
+                       sysfs_cleanup();
+               }
+       }
+
        for (i = 0; i < n; i++)
                free(namelist[i]);
        free(namelist);
-- 
2.1.2

-- 
You received this message because you are subscribed to the Google Groups 
"open-iscsi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/open-iscsi.
For more options, visit https://groups.google.com/d/optout.

Reply via email to