From: Alex Riesen <raa.lkml@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Subject: Re: cygwin: push/pull takes very long time
Date: Thu, 2 Mar 2006 22:54:08 +0100 [thread overview]
Message-ID: <20060302215408.GC6183@steel.home> (raw)
In-Reply-To: <81b0412b0603020909p179ed9bx4ed8fc2ddf77e868@mail.gmail.com>
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}
+ }
+'
next prev parent reply other threads:[~2006-03-02 21:54 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-02 13:26 cygwin: push/pull takes very long time Alex Riesen
2006-03-02 17:09 ` Alex Riesen
2006-03-02 21:54 ` Alex Riesen [this message]
2006-03-03 0:28 ` Christopher Faylor
2006-03-03 9:10 ` Alex Riesen
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=20060302215408.GC6183@steel.home \
--to=raa.lkml@gmail.com \
--cc=git@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).