Re: cygwin: push/pull takes very long time

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Alex Riesen, Thu, Mar 02, 2006 18:09:23 +0100:
> I'll cleanup the profiling code and send it as well soon
> (I had to instrument x*alloc).

This is not exactly the same. It counts free as well, even if that is
not really interesting - there are places were there is more frees
than allocs. Probably something missed or a result coming from libc.

Also it is _not_ the code I used for windows. I had to have a global
variable for argv[0], which needs modification of all main()s, which
gets too easily out of sync.

BTW, maybe someone has an idea as to how attach valgrind to everything?
(I mean, without changing every script. Maybe modify git.c?)

Anyway, here it is, could be useful for something.

---

diff --git a/Makefile b/Makefile
index a5eb0c4..18fc802 100644
--- a/Makefile
+++ b/Makefile
@@ -206,7 +206,7 @@ LIB_OBJS = \
 	quote.o read-cache.o refs.o run-command.o \
 	server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
 	tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
-	fetch-clone.o \
+	fetch-clone.o alloc.o \
 	$(DIFF_OBJS)
 
 LIBS = $(LIB_FILE)
diff --git a/alloc.c b/alloc.c
new file mode 100644
index 0000000..76727e6
--- /dev/null
+++ b/alloc.c
@@ -0,0 +1,108 @@
+/* simple allocation logging */
+#include <unistd.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "git-compat-util.h"
+
+#undef free
+
+#define SHIFT 2
+static unsigned cnts[12] = {0,0,0,0, 0,0,0,0};
+static unsigned fcnt = 0;
+
+static int exit_hook = 1;
+
+static void alloc_profile()
+{
+	char argv0[PATH_MAX];
+	FILE *fp;
+	fp = fopen("/proc/self/cmdline", "r");
+	if ( !fp ) {
+		sprintf(argv0, "%d", getpid());
+	} else {
+		fread(argv0, sizeof(argv0), 1, fp);
+		fclose(fp);
+		char *s = strrchr(argv0, '/');
+		if ( s )
+			memmove(argv0, s + 1, strlen(s));
+	}
+	fp = fopen("/tmp/git-alloc", "ab");
+	if ( !fp )
+		return;
+	unsigned i, c = 0;
+	for ( i = 0; i < sizeof(cnts)/sizeof(*cnts); ++i ) {
+		if ( !cnts[i] )
+			continue;
+		fprintf(fp, "%s %u %u:%u %u times\n",
+			argv0,
+			i,
+			i ? 1 << i * SHIFT: 0,
+			1 << (i+1) * SHIFT,
+			cnts[i]);
+		c += cnts[i];
+	}
+	fprintf(fp, "%s alloc-free = %u\n", argv0, c - fcnt);
+	fclose(fp);
+}
+
+static inline void count(size_t size)
+{
+	if ( exit_hook ) {
+		exit_hook = 0;
+		atexit(alloc_profile);
+	}
+        unsigned i = 0;
+	while ( size && i < sizeof(cnts)/sizeof(*cnts) ) {
+		size >>= SHIFT;
+		if ( size )
+			++i;
+		else {
+			++cnts[i];
+			return;
+		}
+	}
+	++cnts[sizeof(cnts)/sizeof(*cnts)-1];
+}
+
+void *xmalloc(size_t size)
+{
+	void *ret = malloc(size);
+	count(size);
+	if (!ret && !size)
+		ret = malloc(1);
+	if (!ret)
+		die("Out of memory, malloc failed, %u bytes requested", size);
+	return ret;
+}
+
+void *xrealloc(void *ptr, size_t size)
+{
+	void *ret = realloc(ptr, size);
+	count(size);
+	if (!ret && !size)
+		ret = realloc(ptr, 1);
+	if (!ret)
+		die("Out of memory, realloc failed, %u bytes requested", size);
+	return ret;
+}
+
+void *xcalloc(size_t nmemb, size_t size)
+{
+	void *ret = calloc(nmemb, size);
+	count(size);
+	if (!ret && (!nmemb || !size))
+		ret = calloc(1, 1);
+	if (!ret)
+		die("Out of memory, calloc failed");
+	return ret;
+}
+
+void xfree(void *ptr)
+{
+	if ( !ptr )
+		return;
+	free(ptr);
+	++fcnt;
+}
+
diff --git a/git-compat-util.h b/git-compat-util.h
index f982b8e..ab4f855 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -73,35 +73,11 @@ extern void gitunsetenv(const char *);
 extern char *gitstrcasestr(const char *haystack, const char *needle);
 #endif
 
-static inline void *xmalloc(size_t size)
-{
-	void *ret = malloc(size);
-	if (!ret && !size)
-		ret = malloc(1);
-	if (!ret)
-		die("Out of memory, malloc failed");
-	return ret;
-}
-
-static inline void *xrealloc(void *ptr, size_t size)
-{
-	void *ret = realloc(ptr, size);
-	if (!ret && !size)
-		ret = realloc(ptr, 1);
-	if (!ret)
-		die("Out of memory, realloc failed");
-	return ret;
-}
-
-static inline void *xcalloc(size_t nmemb, size_t size)
-{
-	void *ret = calloc(nmemb, size);
-	if (!ret && (!nmemb || !size))
-		ret = calloc(1, 1);
-	if (!ret)
-		die("Out of memory, calloc failed");
-	return ret;
-}
+void *xmalloc(size_t size);
+void *xrealloc(void *ptr, size_t size);
+void *xcalloc(size_t nmemb, size_t size);
+void xfree(void *ptr);
+#define free(p) xfree(p)
 
 static inline ssize_t xread(int fd, void *buf, size_t len)
 {
diff --git a/prof-decode.sh b/prof-decode.sh
new file mode 100755
index 0000000..9c07e05
--- /dev/null
+++ b/prof-decode.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+if [ $# = 0 ]; then
+    set -- /tmp/git-alloc
+fi
+cat "$@" | perl -e '
+    while(<>) {
+        if (/(\S*) \d+ (\d+:\d+) (\d+)/) {
+		$c{$1}->{$2} += $3;
+		$c{$1}->{all} += $3
+	}
+        if (/(\S*) alloc-free = (\d+)/) { $d{$1} += $2 }
+    }
+    foreach $k (sort {$c{$b}->{all} <=> $c{$a}->{all}} grep {$c{$_}} keys %c) {
+	for ( sort {
+		if ( $a eq "all" ) { -1 }
+		elsif ( $b eq "all" ) { 1 }
+		else {
+		"$a $b" =~ /(\d+):\d+ (\d+):\d+/;
+		$1 <=> $2 }
+	    } grep { $c{$k}->{$_} } keys %{$c{$k}} ) {
+	    printf "%20s\t%20s\t%d\n", $k, $_, $c{$k}->{$_}
+	}
+	printf "%20s\t%d leaks\n\n", $k, $d{$k}
+    }
+'
-
: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]