From 969ca6d9bf20e8c2940e28af9d2e66a8c0e20db7 Mon Sep 17 00:00:00 2001 From: Pavel Emelyanov Date: Fri, 17 Aug 2012 00:20:58 +0400 Subject: [PATCH] shmem: Get rid of static array of dumped shmem areas These are used by single process, no need in keeping them in such a strange way. Plus, turn this array into a hash table for better search. Signed-off-by: Pavel Emelyanov --- cr-dump.c | 4 ---- include/shmem.h | 2 -- shmem.c | 52 +++++++++++++++++++++++-------------------------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/cr-dump.c b/cr-dump.c index 690a79a55..c7ca94785 100644 --- a/cr-dump.c +++ b/cr-dump.c @@ -1523,9 +1523,6 @@ int cr_dump_tasks(pid_t pid, const struct cr_options *opts) if (!glob_fdset) goto err; - if (init_shmem_dump()) - goto err; - for_each_pstree_item(item) { if (dump_one_task(item)) goto err; @@ -1548,7 +1545,6 @@ int cr_dump_tasks(pid_t pid, const struct cr_options *opts) fd_id_show_tree(); err: - fini_shmem_dump(); close_cr_fdset(&glob_fdset); /* diff --git a/include/shmem.h b/include/shmem.h index f109d462b..18eafc594 100644 --- a/include/shmem.h +++ b/include/shmem.h @@ -13,6 +13,4 @@ extern struct shmems *rst_shmems; int cr_dump_shmem(void); int add_shmem_area(pid_t pid, VmaEntry *vma); -int init_shmem_dump(void); -void fini_shmem_dump(void); #endif diff --git a/shmem.c b/shmem.c index 024ee823b..cb5263405 100644 --- a/shmem.c +++ b/shmem.c @@ -229,41 +229,45 @@ struct shmem_info_dump { unsigned long start; unsigned long end; int pid; + + struct shmem_info_dump *next; }; -static int nr_shmems; -static struct shmem_info_dump *dump_shmems; +#define SHMEM_HASH_SIZE 32 +static struct shmem_info_dump *shmems_hash[SHMEM_HASH_SIZE]; -static struct shmem_info_dump *shmem_find(unsigned long shmid) +static struct shmem_info_dump *shmem_find(struct shmem_info_dump **chain, + unsigned long shmid) { - int i; + struct shmem_info_dump *sh; - for (i = 0; i < nr_shmems; i++) - if (dump_shmems[i].shmid == shmid) - return &dump_shmems[i]; + for (sh = *chain; sh; sh = sh->next) + if (sh->shmid == shmid) + return sh; return NULL; } int add_shmem_area(pid_t pid, VmaEntry *vma) { - struct shmem_info_dump *si; + struct shmem_info_dump *si, **chain; unsigned long size = vma->pgoff + (vma->end - vma->start); - si = shmem_find(vma->shmid); + chain = &shmems_hash[vma->shmid % SHMEM_HASH_SIZE]; + si = shmem_find(chain, vma->shmid); if (si) { if (si->size < size) si->size = size; return 0; } - nr_shmems++; - if (nr_shmems * sizeof(*si) == SHMEMS_SIZE) { - pr_err("OOM storing shmems\n"); + si = xmalloc(sizeof(*si)); + if (!si) return -1; - } - si = &dump_shmems[nr_shmems - 1]; + si->next = *chain; + *chain = si; + si->size = size; si->pid = pid; si->start = vma->start; @@ -273,15 +277,19 @@ int add_shmem_area(pid_t pid, VmaEntry *vma) return 0; } +#define for_each_shmem_dump(_i, _si) \ + for (i = 0; i < SHMEM_HASH_SIZE; i++) \ + for (si = shmems_hash[i]; si; si = si->next) + int cr_dump_shmem(void) { - int err, fd; + int i, err, fd; unsigned char *map = NULL; void *addr = NULL; struct shmem_info_dump *si; unsigned long pfn, nrpages; - for (si = dump_shmems; si < &dump_shmems[nr_shmems]; si++) { + for_each_shmem_dump (i, si) { pr_info("Dumping shared memory 0x%lx\n", si->shmid); nrpages = (si->size + PAGE_SIZE - 1) / PAGE_SIZE; @@ -346,15 +354,3 @@ err: xfree(map); return -1; } - -int init_shmem_dump(void) -{ - nr_shmems = 0; - dump_shmems = xmalloc(SHMEMS_SIZE); - return dump_shmems == NULL ? -1 : 0; -} - -void fini_shmem_dump(void) -{ - xfree(dump_shmems); -}