public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: "Barry Naujok" <bnaujok@melbourne.sgi.com>
To: 'Christoph Hellwig' <hch@infradead.org>
Cc: xfs@oss.sgi.com, xfs-dev@sgi.com
Subject: RE: [PATCH] New xfs_repair handling for inode nlink counts
Date: Tue, 13 Mar 2007 12:51:35 +1100	[thread overview]
Message-ID: <200703130151.MAA03235@larry.melbourne.sgi.com> (raw)
In-Reply-To: <20070309073410.GA8798@infradead.org>

[-- Attachment #1: Type: text/plain, Size: 1779 bytes --]

Hi Christoph,

Thanks for the feedback. I've attached an update to the trackmem stuff 
for review. globals.c is now unmodified.

Regards,
Barry.

> -----Original Message-----
> From: xfs-bounce@oss.sgi.com [mailto:xfs-bounce@oss.sgi.com] 
> On Behalf Of Christoph Hellwig
> Sent: Friday, 9 March 2007 6:34 PM
> To: Barry Naujok
> Cc: xfs@oss.sgi.com; xfs-dev@sgi.com
> Subject: Re: [PATCH] New xfs_repair handling for inode nlink counts
> 
> 
> +#ifdef TRACK_MEMORY
> +
> +#undef calloc
> +#undef malloc
> +#undef memalign
> +#undef realloc
> +#undef free
> 
> 
> 	Can you put all thise into a memory_tracking.h file that
> 	gets include with:
> 
> 	#ifdef TRACK_MEMORY
> 	#include "track_memory.h"
> 	#endif
> 
> 	Instead of polluting the implementation file?
> 
> +	/* add pointer to hash list, very basic simple hash function */
> +	i = (((size_t)p) >> 8) & 0xff;
> 
> +	i = (((size_t)ptr) >> 8) & 0xff;
> 
> 	Note that there is not guarantee that size_t and 
> pointers have the
> 	same lenght, and there are system where it's not 
> (win64?), better
> 	cast things use uintptr_t here.
> 
> --- xfsprogs.orig/repair/globals.h
> +++ xfsprogs/repair/globals.h
> @@ -16,6 +16,16 @@
>   * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
>   */
>  
> +#ifdef TRACK_MEMORY
> +
> +#define calloc(n,s)	track_calloc(__FILE__, __LINE__, (n), (s))
> +#define malloc(s)	track_malloc(__FILE__, __LINE__, (s))
> +#define memalign(b,s)	track_memalign(__FILE__, 
> __LINE__, (b), (s))
> +#define realloc(p,s)	track_realloc(__FILE__, __LINE__, (p), (s))
> +#define free(p)		track_free(__FILE__, __LINE__, (p))
> +
> +#endif
> +
>  #ifndef _XFS_REPAIR_GLOBAL_H
>  #define _XFS_REPAIR_GLOBAL_H
> 
> 	The memory tracking should probably come after the inclusion
> 	guards.
> 
> 

[-- Attachment #2: trackmem_update.patch --]
[-- Type: application/octet-stream, Size: 9583 bytes --]


===========================================================================
xfsprogs/repair/Makefile
===========================================================================

--- a/xfsprogs/repair/Makefile	2007-03-13 12:48:40.000000000 +1100
+++ b/xfsprogs/repair/Makefile	2007-03-13 12:02:05.061400111 +1100
@@ -9,13 +9,14 @@ LTCOMMAND = xfs_repair
 
 HFILES = agheader.h attr_repair.h avl.h avl64.h bmap.h dinode.h dir.h \
 	dir2.h dir_stack.h err_protos.h globals.h incore.h protos.h rt.h \
-	progress.h scan.h versions.h prefetch.h threads.h
+	progress.h scan.h versions.h prefetch.h threads.h trackmem.h
 
 CFILES = agheader.c attr_repair.c avl.c avl64.c bmap.c dino_chunks.c \
 	dinode.c dir.c dir2.c dir_stack.c globals.c incore.c \
 	incore_bmc.c init.c incore_ext.c incore_ino.c phase1.c \
 	phase2.c phase3.c phase4.c phase5.c phase6.c phase7.c rt.c sb.c \
-	progress.c prefetch.c scan.c versions.c xfs_repair.c threads.c
+	progress.c prefetch.c scan.c versions.c xfs_repair.c threads.c \
+	trackmem.c
 
 LLDLIBS = $(LIBXFS) $(LIBXLOG) $(LIBUUID) $(LIBPTHREAD) $(LIBRT)
 LTDEPENDENCIES = $(LIBXFS) $(LIBXLOG)

===========================================================================
xfsprogs/repair/globals.h
===========================================================================

--- a/xfsprogs/repair/globals.h	2007-03-13 12:48:40.000000000 +1100
+++ b/xfsprogs/repair/globals.h	2007-03-13 12:39:46.487391946 +1100
@@ -23,6 +23,10 @@
 #define EXTERN extern
 #endif
 
+#ifdef TRACK_MEMORY
+#include "trackmem.h"
+#endif
+
 /* useful macros */
 
 #define rounddown(x, y) (((x)/(y))*(y))

===========================================================================
xfsprogs/repair/trackmem.c
===========================================================================

--- a/xfsprogs/repair/trackmem.c	2006-06-17 00:58:24.000000000 +1000
+++ b/xfsprogs/repair/trackmem.c	2007-03-13 12:48:25.331111073 +1100
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2007 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <libxfs.h>
+
+#undef calloc
+#undef malloc
+#undef memalign
+#undef realloc
+#undef free
+
+/*
+ * Track by file name pointer and also by return pointer
+ */
+
+typedef struct func {
+	const char	*file;
+	int		line;
+	int64_t		acount;
+	int64_t		fcount;
+	int64_t		rcount;
+	int64_t		current;
+	int64_t		peak;
+} func_t;
+
+typedef struct entry {
+	struct entry	*next;
+	func_t		*fileline;
+	size_t		size;
+	void		*ptr;
+} entry_t;
+
+static int	caller_count = 0;
+static int	caller_size = 0;
+static func_t	*callers = NULL;
+
+static entry_t	*ptrhash[256];
+
+static
+void track_alloc(const char *file, int line, size_t size, void *p)
+{
+	int		i;
+	entry_t		*e;
+
+	/* find an existing func call from file/line */
+	for (i = 0; i < caller_count; i++) {
+		if ((callers[i].file == file) && (callers[i].line == line))
+			break;
+	}
+	if (i == caller_count) {	/* add new func if not found */
+		if (caller_count == caller_size) {
+			caller_size += 64;
+			callers = realloc(callers, sizeof(func_t) * caller_size);
+		}
+		memset(&callers[i], 0, sizeof(func_t));
+		callers[i].file = file;
+		callers[i].line = line;
+		caller_count++;
+	}
+
+	e = malloc(sizeof(entry_t));
+	e->size = size;
+	e->ptr = p;
+	e->fileline = &callers[i];
+
+	callers[i].acount++;
+	callers[i].current += size;
+	if (callers[i].current > callers[i].peak)
+		callers[i].peak = callers[i].current;
+
+	/* add pointer to hash list, very basic simple hash function */
+	i = (((int)p) >> 8) & 0xff;
+
+	e->next = ptrhash[i];
+	ptrhash[i] = e;
+}
+
+void *track_calloc(const char *file, int line, size_t num, size_t size)
+{
+	void *retval = calloc(num, size);
+
+	if (retval != NULL)
+		track_alloc(file, line, num * size, retval);
+
+	return retval;
+}
+
+void *track_malloc(const char *file, int line, size_t size)
+{
+	void *retval = malloc(size);
+
+	if (retval != NULL)
+		track_alloc(file, line, size, retval);
+
+	return retval;
+}
+
+void *track_memalign(const char *file, int line, size_t boundary, size_t size)
+{
+	void *retval = memalign(boundary, size);
+
+	if (retval != NULL)
+		track_alloc(file, line, size, retval);
+
+	return retval;
+}
+
+void *track_realloc(const char *file, int line, void *ptr, size_t size)
+{
+	int		i;
+	entry_t		*e, *prev;
+	void 		*newptr = realloc(ptr, size);
+
+	if (ptr == NULL && newptr != NULL) {
+		track_alloc(file, line, size, newptr);
+		return newptr;
+	}
+
+	i = (((size_t)ptr) >> 8) & 0xff;
+
+	prev = NULL;
+	for (e = ptrhash[i]; e; e = e->next) {
+		if (e->ptr == ptr)
+			break;
+		prev = e;
+	}
+	if (!e)
+		return newptr;
+
+	e->fileline->rcount++;
+	e->fileline->current = e->fileline->current + size - e->size;
+	if (e->fileline->current > e->fileline->peak)
+		e->fileline->peak = e->fileline->current;
+	e->size = size;
+	e->ptr = newptr;
+
+	return newptr;
+}
+
+void track_free(const char *file, int line, void *ptr)
+{
+	int		i;
+	entry_t	*e, *prev;
+
+	free(ptr);
+
+	/* find associated entry */
+	i = (((size_t)ptr) >> 8) & 0xff;
+
+	prev = NULL;
+	for (e = ptrhash[i]; e; e = e->next) {
+		if (e->ptr == ptr)
+			break;
+		prev = e;
+	}
+	if (!e)
+		return;
+
+	e->fileline->fcount++;
+	e->fileline->current -= e->size;
+
+	if (prev)
+		prev->next = e->next;
+	else
+		ptrhash[i] = e->next;
+	free(e);
+}
+
+void print_memory_usage(void)
+{
+	int i;
+
+	printf("%20s:line \ta_cnt\tf_cnt\tr_cnt\tremain\tpeak\n", "file");
+	for (i = 0; i < caller_count; i++) {
+		printf("%20s:%-5d\t%lld\t%lld\t%lld\t%lld\t%lld\n",
+			callers[i].file, callers[i].line,
+			callers[i].acount, callers[i].fcount, callers[i].rcount,
+			callers[i].current, callers[i].peak);
+	}
+}

===========================================================================
xfsprogs/repair/trackmem.h
===========================================================================

--- a/xfsprogs/repair/trackmem.h	2006-06-17 00:58:24.000000000 +1000
+++ b/xfsprogs/repair/trackmem.h	2007-03-13 12:47:31.226230019 +1100
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2007 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _XFS_REPAIR_TRACK_MEM_H
+#define _XFS_REPAIR_TRACK_MEM_H
+
+#define calloc(n,s)	track_calloc(__FILE__, __LINE__, (n), (s))
+#define malloc(s)	track_malloc(__FILE__, __LINE__, (s))
+#define memalign(b,s)	track_memalign(__FILE__, __LINE__, (b), (s))
+#define realloc(p,s)	track_realloc(__FILE__, __LINE__, (p), (s))
+#define free(p)		track_free(__FILE__, __LINE__, (p))
+
+void print_memory_usage(void);
+void *track_calloc(const char *file, int line, size_t num, size_t size);
+void *track_malloc(const char *file, int line, size_t size);
+void *track_memalign(const char *file, int line, size_t boundary, size_t size);
+void *track_realloc(const char *file, int line, void *ptr, size_t size);
+void track_free(const char *file, int line, void *ptr);
+
+#endif /* _XFS_REPAIR_TRACK_MEM_H */

===========================================================================
xfsprogs/repair/xfs_repair.c
===========================================================================

--- a/xfsprogs/repair/xfs_repair.c	2007-03-13 12:48:40.000000000 +1100
+++ b/xfsprogs/repair/xfs_repair.c	2007-03-13 12:02:45.340085859 +1100
@@ -563,7 +563,7 @@ main(int argc, char **argv)
 
 	/* XXX: nathans - something in phase4 ain't playing by */
 	/* the buffer cache rules.. why doesn't IRIX hit this? */
-	libxfs_bcache_purge();
+	libxfs_bcache_flush();
 
 	if (no_modify)
 		printf(_("No modify flag set, skipping phase 5\n"));
@@ -576,6 +576,8 @@ main(int argc, char **argv)
 		phase6(mp);
 		timestamp(PHASE_END, 6, NULL);
 
+		libxfs_bcache_flush();
+
 		phase7(mp);
 		timestamp(PHASE_END, 7, NULL);
 	} else  {
@@ -640,6 +642,10 @@ _("Warning:  project quota information w
 	if (do_parallel && report_interval)
 		stop_progress_rpt();
 
+#ifdef TRACK_MEMORY
+	print_memory_usage();
+#endif
+
 	if (no_modify)  {
 		do_log(
 	_("No modify flag set, skipping filesystem flush and exiting.\n"));

  reply	other threads:[~2007-03-13  1:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-09  6:20 [PATCH] New xfs_repair handling for inode nlink counts Barry Naujok
2007-03-09  7:34 ` Christoph Hellwig
2007-03-13  1:51   ` Barry Naujok [this message]
2007-03-17 11:45     ` 'Christoph Hellwig'

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200703130151.MAA03235@larry.melbourne.sgi.com \
    --to=bnaujok@melbourne.sgi.com \
    --cc=hch@infradead.org \
    --cc=xfs-dev@sgi.com \
    --cc=xfs@oss.sgi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox