From owner-svn-src-all@FreeBSD.ORG Mon Jan 25 23:37:50 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 51C971065670; Mon, 25 Jan 2010 23:37:50 +0000 (UTC) (envelope-from gabor@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 3C8138FC08; Mon, 25 Jan 2010 23:37:50 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o0PNboFo012280; Mon, 25 Jan 2010 23:37:50 GMT (envelope-from gabor@svn.freebsd.org) Received: (from gabor@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o0PNbobw012278; Mon, 25 Jan 2010 23:37:50 GMT (envelope-from gabor@svn.freebsd.org) Message-Id: <201001252337.o0PNbobw012278@svn.freebsd.org> From: Gabor Kovesdan Date: Mon, 25 Jan 2010 23:37:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r202992 - head/lib/libc/nls X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jan 2010 23:37:50 -0000 Author: gabor (doc,ports committer) Date: Mon Jan 25 23:37:49 2010 New Revision: 202992 URL: http://svn.freebsd.org/changeset/base/202992 Log: Cache failing and opened catalogs in catopen() and related functions. Continuous catopen() calls cause 4 failig stat(2) each, which means a lot of overhead. It is also a good idea to keep the opened catalogs in the memory to speed up further catopen() calls to the same catalog since these catalogs are not big at all. In this case, we count references and only free() the allocated space when the reference count reaches 0. The reads and writes to the cache are syncronized with an rwlock when these functions are called from a threaded program. Requested by: kib Approved by: delphij Modified: head/lib/libc/nls/msgcat.c Modified: head/lib/libc/nls/msgcat.c ============================================================================== --- head/lib/libc/nls/msgcat.c Mon Jan 25 23:30:53 2010 (r202991) +++ head/lib/libc/nls/msgcat.c Mon Jan 25 23:37:49 2010 (r202992) @@ -1,5 +1,6 @@ /*********************************************************** Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. +Copyright 2010, Gabor Kovesdan All Rights Reserved @@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include /* for ntohl() */ @@ -47,6 +49,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -57,10 +60,49 @@ __FBSDID("$FreeBSD$"); #define _DEFAULT_NLS_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L:/usr/local/share/nls/%L/%N.cat:/usr/local/share/nls/%N/%L" +#define RLOCK(fail) { int ret; \ + if (__isthreaded && \ + ((ret = _pthread_rwlock_rdlock(&rwlock)) != 0)) { \ + errno = ret; \ + return (fail); \ + }} +#define WLOCK(fail) { int ret; \ + if (__isthreaded && \ + ((ret = _pthread_rwlock_wrlock(&rwlock)) != 0)) { \ + errno = ret; \ + return (fail); \ + }} +#define UNLOCK { if (__isthreaded) \ + _pthread_rwlock_unlock(&rwlock); } + #define NLERR ((nl_catd) -1) #define NLRETERR(errc) { errno = errc; return (NLERR); } +#define SAVEFAIL(n, e) { WLOCK(NLERR); \ + np = malloc(sizeof(struct catentry)); \ + if (np != NULL) { \ + np->name = strdup(n); \ + np->caterrno = e; \ + SLIST_INSERT_HEAD(&cache, np, list); \ + } \ + UNLOCK; \ + } + +static nl_catd load_msgcat(const char *, const char *, const char *); + +static pthread_rwlock_t rwlock; -static nl_catd load_msgcat(const char *); +struct catentry { + SLIST_ENTRY(catentry) list; + char *name; + char *path; + int caterrno; + nl_catd catd; + char *lang; + int refcount; +}; + +SLIST_HEAD(listhead, catentry) cache = + SLIST_HEAD_INITIALIZER(cache); nl_catd catopen(const char *name, int type) @@ -70,14 +112,14 @@ catopen(const char *name, int type) char *nlspath, *lang, *base, *cptr, *pathP, *tmpptr; char *cptr1, *plang, *pter, *pcode; struct stat sbuf; + struct catentry *np; if (name == NULL || *name == '\0') NLRETERR(EINVAL); - /* is it absolute path ? if yes, load immediately */ if (strchr(name, '/') != NULL) - return (load_msgcat(name)); - + lang = NULL; + else { if (type == NL_CAT_LOCALE) lang = setlocale(LC_MESSAGES, NULL); else @@ -88,6 +130,29 @@ catopen(const char *name, int type) (lang[1] == '\0' || (lang[1] == '.' && lang[2] == '\0'))) || strchr(lang, '/') != NULL) lang = "C"; + } + + /* Try to get it from the cache first */ + RLOCK(NLERR); + SLIST_FOREACH(np, &cache, list) { + if (strcmp(np->name, name) == 0) { + if (np->caterrno != 0) { + /* Found cached failing entry */ + UNLOCK; + NLRETERR(np->caterrno); + } else if (strcmp(np->lang, lang) == 0) { + /* Found cached successful entry */ + np->refcount++; + UNLOCK; + return (np->catd); + } + } + } + UNLOCK; + + /* is it absolute path ? if yes, load immediately */ + if (strchr(name, '/') != NULL) + return (load_msgcat(name, name, lang)); if ((plang = cptr1 = strdup(lang)) == NULL) return (NLERR); @@ -166,7 +231,7 @@ catopen(const char *name, int type) if (stat(path, &sbuf) == 0) { free(plang); free(base); - return (load_msgcat(path)); + return (load_msgcat(path, name, lang)); } } else { tmpptr = (char *)name; @@ -190,12 +255,12 @@ catgets(nl_catd catd, int set_id, int ms if (catd == NULL || catd == NLERR) { errno = EBADF; /* LINTED interface problem */ - return (char *) s; -} + return ((char *)s); + } - cat_hdr = (struct _nls_cat_hdr *)catd->__data; - set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data - + sizeof(struct _nls_cat_hdr)); + cat_hdr = (struct _nls_cat_hdr *)catd->__data; + set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data + + sizeof(struct _nls_cat_hdr)); /* binary search, see knuth algorithm b */ l = 0; @@ -228,7 +293,7 @@ catgets(nl_catd catd, int set_id, int ms } else { l = i + 1; } -} + } /* not found */ goto notfound; @@ -238,25 +303,41 @@ catgets(nl_catd catd, int set_id, int ms } else { l = i + 1; } -} + } notfound: /* not found */ errno = ENOMSG; /* LINTED interface problem */ - return (char *) s; + return ((char *)s); } int catclose(nl_catd catd) { + struct catentry *np; + if (catd == NULL || catd == NLERR) { errno = EBADF; return (-1); } + /* Remove from cache if not referenced any more */ + WLOCK(-1); + SLIST_FOREACH(np, &cache, list) { + if ((np->catd->__size == catd->__size) && + memcmp((const void *)np->catd, (const void *)catd, np->catd->__size) == 0) { + np->refcount--; + if (np->refcount == 0) { munmap(catd->__data, (size_t)catd->__size); free(catd); + SLIST_REMOVE(&cache, np, catentry, list); + free(np); + } + break; + } + } + UNLOCK; return (0); } @@ -265,19 +346,35 @@ catclose(nl_catd catd) */ static nl_catd -load_msgcat(const char *path) +load_msgcat(const char *path, const char *name, const char *lang) { struct stat st; nl_catd catd; + struct catentry *np; void *data; int fd; - /* XXX: path != NULL? */ + /* path/name will never be NULL here */ + + /* One more try in cache; if it was not found by name, + it might still be found by absolute path. */ + RLOCK(NLERR); + SLIST_FOREACH(np, &cache, list) { + if (strcmp(np->path, path) == 0) { + np->refcount++; + UNLOCK; + return (np->catd); + } + } + UNLOCK; - if ((fd = _open(path, O_RDONLY)) == -1) + if ((fd = _open(path, O_RDONLY)) == -1) { + SAVEFAIL(name, errno); return (NLERR); + } if (_fstat(fd, &st) != 0) { + SAVEFAIL(name, errno); _close(fd); return (NLERR); } @@ -286,22 +383,39 @@ load_msgcat(const char *path) (off_t)0); _close(fd); - if (data == MAP_FAILED) + if (data == MAP_FAILED) { + SAVEFAIL(name, errno); return (NLERR); + } if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != _NLS_MAGIC) { + SAVEFAIL(name, errno); munmap(data, (size_t)st.st_size); NLRETERR(EINVAL); } if ((catd = malloc(sizeof (*catd))) == NULL) { + SAVEFAIL(name, errno); munmap(data, (size_t)st.st_size); return (NLERR); } catd->__data = data; catd->__size = (int)st.st_size; + + /* Caching opened catalog */ + WLOCK(NLERR); + if ((np = malloc(sizeof(struct catentry))) != NULL) { + np->name = strdup(name); + np->path = strdup(path); + np->catd = catd; + np->lang = (lang == NULL) ? NULL : strdup(lang); + np->refcount = 1; + np->caterrno = 0; + SLIST_INSERT_HEAD(&cache, np, list); + } + UNLOCK; return (catd); }