• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/tools/mem-trace.h

00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 
00003 /*
00004  * Copyright (C) 1998 by the University of Southern California
00005  * $Id: mem-trace.h,v 1.16 2005/08/25 18:58:12 johnh Exp $
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  *
00020  *
00021  * The copyright of this module includes the following
00022  * linking-with-specific-other-licenses addition:
00023  *
00024  * In addition, as a special exception, the copyright holders of
00025  * this module give you permission to combine (via static or
00026  * dynamic linking) this module with free software programs or
00027  * libraries that are released under the GNU LGPL and with code
00028  * included in the standard release of ns-2 under the Apache 2.0
00029  * license or under otherwise-compatible licenses with advertising
00030  * requirements (or modified versions of such code, with unchanged
00031  * license).  You may copy and distribute such a system following the
00032  * terms of the GNU GPL for this module and the licenses of the
00033  * other code concerned, provided that you include the source code of
00034  * that other code when and as the GNU GPL requires distribution of
00035  * source code.
00036  *
00037  * Note that people who make modified versions of this module
00038  * are not obligated to grant this special exception for their
00039  * modified versions; it is their choice whether to do so.  The GNU
00040  * General Public License gives permission to release a modified
00041  * version without this exception; this exception also makes it
00042  * possible to release a modified version which carries forward this
00043  * exception.
00044  *
00045  */
00046 
00047 #include "config.h"
00048 #include <stdio.h>
00049 
00050 /* Unix platforms should get these from configure */
00051 #ifdef WIN32
00052 #undef HAVE_GETRUSAGE
00053 #undef HAVE_SBRK
00054 #endif
00055 
00056 #ifdef HAVE_GETRUSAGE
00057 #include <sys/time.h>
00058 #include <sys/resource.h>
00059 #include <unistd.h>
00060 #endif
00061 
00062 #ifdef HAVE_SBRK
00063 #include <unistd.h>
00064 #endif /* HAVE_SBRK */
00065 
00066 /* hpux patch from Ketil Danielsen <Ketil.Danielsen@hiMolde.no> */
00067 #ifdef hpux
00068 #include <sys/resource.h>
00069 #include <sys/syscall.h>
00070 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
00071 #endif
00072 
00073 
00074 
00075 #define fDIFF(FIELD) (now_.FIELD - start_.FIELD)
00076 #define absolute(var) (var > 0 ? var : -var)
00077 #define normalize(var) var.tv_sec * 1000 + (int) (var.tv_usec / 1000)
00078 
00079 class MemInfo {
00080 public:
00081         MemInfo() {}
00082         long stack_;
00083         long heap_;
00084         struct timeval utime_, stime_;
00085 
00086         void checkpoint() {
00087                 int i;
00088                 stack_ = (long)&i;
00089 
00090 #ifdef HAVE_GETRUSAGE
00091                 struct rusage ru;
00092                 getrusage(RUSAGE_SELF, &ru);
00093                 utime_ = ru.ru_utime;
00094                 stime_ = ru.ru_stime;
00095 #else /* ! HAVE_GETRUSAGE */
00096                 utime_.tv_sec = utime_.tv_usec = 0;
00097                 stime_.tv_sec = stime_.tv_usec = 0;
00098 #endif /* HAVE_GETRUSAGE */
00099 
00100 #ifdef HAVE_SBRK
00101                 heap_ = (long)sbrk(0);
00102 #else /* ! HAVE_SBRK */
00103                 heap_ = 0;
00104 #endif /* HAVE_SBRK */
00105         }
00106 };
00107 
00108 
00109 class MemTrace {
00110 private:
00111         MemInfo start_, now_;
00112 
00113 public:
00114         MemTrace() {
00115                 start_.checkpoint();
00116         }
00117         void diff(char* prompt) {
00118                 now_.checkpoint();
00119                 fprintf (stdout, "%s: utime/stime: %ld %ld \tstack/heap: %ld %ld\n",
00120                          prompt, 
00121                          normalize(now_.utime_) - normalize(start_.utime_), 
00122                          normalize(now_.stime_) - normalize(start_.stime_), 
00123                          fDIFF(stack_), fDIFF(heap_));
00124                 start_.checkpoint();
00125         }
00126 };
00127 

Generated on Tue Aug 10 2010 16:16:08 for ns-2.33 by  doxygen 1.7.1