Commit a6f33bcf authored by Mathieu Faverge's avatar Mathieu Faverge

Add ViTE memory trace (first version)

parent 3cded88c
......@@ -48,6 +48,8 @@ SET(VITE_HDRS
common/Tools.hpp
common/info.hpp
common/Session.hpp
common/trace.h
common/Memory.hpp
# Data structure headers
trace/values/Color.hpp
trace/values/Date.hpp
......@@ -134,6 +136,7 @@ SET(VITE_SRCS
common/Tools.cpp
common/info.cpp
common/Session.cpp
common/Memory.cpp
# Data structure code files
trace/values/Color.cpp
trace/values/Date.cpp
......@@ -216,6 +219,7 @@ IF(VITE_ENABLE_OTF)
ENDIF(VITE_ENABLE_OTF)
# ADD_DEFINITIONS(-DMEMORY_USAGE -DMEMORY_TRACE)
QT4_AUTOMOC(${VITE_SRCS})
FOREACH(_hdrs_file ${VITE_HDRS})
......
/* Copyright INRIA 2004
**
** This file is part of the Scotch distribution.
**
** The Scotch distribution is libre/free software; you can
** redistribute it and/or modify it under the terms of the
** GNU Lesser General Public License as published by the
** Free Software Foundation; either version 2.1 of the
** License, or (at your option) any later version.
**
** The Scotch distribution is distributed in the hope that
** it will be useful, but WITHOUT ANY WARRANTY; without even
** the implied warranty of MERCHANTABILITY or FITNESS FOR A
** PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General
** Public License along with the Scotch distribution; if not,
** write to the Free Software Foundation, Inc.,
** 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
**
** $Id: common_memory.c 176 2004-10-12 13:53:26Z goureman $
*/
/*
File: common_memory.c
Part of a parallel direct block solver.
This module handles errors.
Authors:
Mathieu Faverge - faverge@labri.fr
Xavier LACOSTE - lacoste@labri.fr
Francois PELLEGRINI - .
Dates:
Version 0.0 - from 07 sep 2001
to 07 sep 2001
Version 0.1 - from 14 apr 2001
to 24 mar 2003
Version 1.3 - from 25 feb 2004
to 25 feb 2004
*/
/*
** The defines and includes.
*/
#ifdef MEMORY_USAGE
#include <pthread.h>
#include "common/common.hpp"
#include "common/trace.h"
/*
Group: Variables
The static variables.
int: memallocmutexflag
Boolean indicating if <memallocmutexdat> mutex has been initialized.
pthread_mutex_t: memallocmutexdat
mutex protecting <memalloccurrent>, <memallocmax>, <memalloctraceflag>,
<trace_file>, <trace_timestamp> and <trace_procnum>
ulong: memalloccurrent
Current memory allocated using <memAlloc_func>.
ulong: memallocmax
Maximum value of memalloccurrent since the program started.
int: memalloctraceflag
Boolean indicating if we want to trace allocation.
stream: trace_file
File into which to write traces.
double: time_stamp
Origin of traces.
int: trace_procnum
Processor tracing allocations.
*/
extern "C" {
static int memallocmutexflag = 0;
static pthread_mutex_t memallocmutexdat; /*+ Local mutex +*/
unsigned long memalloccurrent = 0;
unsigned long memallocmax = 0;
int memalloctraceflag = 0;
#ifdef MEMORY_TRACE
static FILE *trace_file;
static double trace_timestamp;
static int trace_procnum;
/*
Function: memAllocTrace
Start tracing memory.
Initialize <memallocmutexdat> if not done.
Defines all tracing variables.
Parameters:
file - Stream where to write traces, opened in write mode.
timestamp - Traces origin.
procnum - Processor writting traces.
Returns:
void - In all cases.
*/
void memAllocTrace (FILE *file,
double timestamp,
int procnum)
{
if (memallocmutexflag == 0) { /* If memory mutex not yet initialized */
memallocmutexflag = 1;
pthread_mutex_init (&memallocmutexdat, NULL); /* Initialize local mutex */
}
pthread_mutex_lock (&memallocmutexdat); /* Lock local mutex */
memalloctraceflag = 1;
trace_file = file;
trace_timestamp = timestamp;
trace_procnum = procnum;
pthread_mutex_unlock (&memallocmutexdat); /* UnLock local mutex */
}
/*
Function: memAllocUntrace
Stop tracing allocations.
Returns:
void - in all cases.
*/
void memAllocUntrace ()
{
pthread_mutex_lock (&memallocmutexdat); /* Lock local mutex */
memalloctraceflag = 0;
pthread_mutex_unlock (&memallocmutexdat); /* UnLock local mutex */
}
#endif
/*
Function: memAllocGetCurrent
Get the current memory allocated.
Returns:
<memalloccurrent> value.
*/
unsigned long memAllocGetCurrent ()
{
return (memalloccurrent);
}
/*
Function: memAllocGetMax
Get the maximu memory allocated.
Returns:
<memallocmax> value.
*/
unsigned long memAllocGetMax () {
return (memallocmax);
}
/*
Function: memAllocTraceReset
Restarts tracing allocation with reseting <memallocmax>.
Returns:
void - in all cases.
*/
void memAllocTraceReset () {
if (memallocmutexflag == 0) { /* If memory mutex not yet initialized */
memallocmutexflag = 1;
pthread_mutex_init (&memallocmutexdat, NULL); /* Initialize local mutex */
}
pthread_mutex_lock (&memallocmutexdat); /* Lock local mutex */
memalloctraceflag = 1;
memallocmax = 0;
pthread_mutex_unlock (&memallocmutexdat); /* UnLock local mutex */
}
}
/*
Function: memAlloc_func
This is a thread-safe memory allocation routine.
Parameters:
size - Memory size wanted.
filename - Used for error message, file where the function is called.
line - Used for erro message, line where the function is called.
Returns:
!NULL - pointer to memory block.
NULL - no array allocated.
*/
void *operator new(std::size_t size) throw(std::bad_alloc)
{
double * memptr;
fprintf(stderr, "toto %d\n", memalloctraceflag);
// #ifdef DEBUG_ALLOC
// if (size == 0)
// errorPrint("%s:%d allocating 0\n",filename,line);
// #endif
if (memallocmutexflag == 0) { /* If memory mutex not yet initialized */
memallocmutexflag = 1;
pthread_mutex_init (&memallocmutexdat, NULL); /* Initialize local mutex */
}
pthread_mutex_lock (&memallocmutexdat); /* Lock local mutex */
memptr = (double*)operator new(size + sizeof(double), std::nothrow); /* Add a double containing the size of the array */
if (memptr != NULL) {
memptr[0] = (double) size;
memalloccurrent += (unsigned long)size;
memallocmax = MAX(memallocmax, memalloccurrent);
memptr++;
#ifdef MEMORY_TRACE
if (memalloctraceflag == 1)
trace_malloc(trace_file, (clockGet()-trace_timestamp),
trace_procnum, STATE_ALLOC, memalloccurrent);
#endif
}
else {
// fprintf (stderr, "File : %s:%d\nECHEC Occupe : %lu Demande : %lu\n",
// filename, line, memalloccurrent, (unsigned long)size);
fprintf (stderr, "ECHEC Occupe : %lu Demande : %lu\n",
memalloccurrent, (unsigned long)size);
}
pthread_mutex_unlock (&memallocmutexdat); /* Unlock local mutex */
return ((void*)memptr);
}
/*
Function: operator delete
This is a thread-safe memory deallocation routine.
It returns:
void - in all cases
*/
void operator delete(void *memptr)
{
double * newmemptr;
fprintf(stderr, "tutu\n");
pthread_mutex_lock (&memallocmutexdat); /* Lock local mutex */
if (!(memptr == NULL)){
newmemptr = (double*)memptr;
newmemptr --;
memalloccurrent -= (unsigned long) newmemptr [0] ;
operator delete (newmemptr, std::nothrow);
#ifdef MEMORY_TRACE
if (memalloctraceflag)
trace_malloc(trace_file, (clockGet()-trace_timestamp),
trace_procnum, STATE_FREE, memalloccurrent);
#endif
}
pthread_mutex_unlock (&memallocmutexdat); /* Unlock local mutex */
}
#endif /* MEMORY_USAGE */
/* Copyright INRIA 2004
**
** This file is part of the Scotch distribution.
**
** The Scotch distribution is libre/free software; you can
** redistribute it and/or modify it under the terms of the
** GNU Lesser General Public License as published by the
** Free Software Foundation; either version 2.1 of the
** License, or (at your option) any later version.
**
** The Scotch distribution is distributed in the hope that
** it will be useful, but WITHOUT ANY WARRANTY; without even
** the implied warranty of MERCHANTABILITY or FITNESS FOR A
** PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General
** Public License along with the Scotch distribution; if not,
** write to the Free Software Foundation, Inc.,
** 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
**
** $Id: common_memory.c 176 2004-10-12 13:53:26Z goureman $
*/
/*
File: common_memory.c
Part of a parallel direct block solver.
This module handles errors.
Authors:
Mathieu Faverge - faverge@labri.fr
Xavier LACOSTE - lacoste@labri.fr
Francois PELLEGRINI - .
Dates:
Version 0.0 - from 07 sep 2001
to 07 sep 2001
Version 0.1 - from 14 apr 2001
to 24 mar 2003
Version 1.3 - from 25 feb 2004
to 25 feb 2004
Version 2.0 (C++)
- from 16 dec 2009
- to 16 dec 2009
*/
/*
** The defines and includes.
*/
#ifndef MEMORY_HPP
#define MEMORY_HPP
#ifdef MEMORY_USAGE
#include <cstdlib>
#include <pthread.h>
/*
Group: Variables
The static variables.
int: memallocmutexflag
Boolean indicating if <memallocmutexdat> mutex has been initialized.
pthread_mutex_t: memallocmutexdat
mutex protecting <memalloccurrent>, <memallocmax>, <memalloctraceflag>,
<trace_file>, <trace_timestamp> and <trace_procnum>
ulong: memalloccurrent
Current memory allocated using <memAlloc_func>.
ulong: memallocmax
Maximum value of memalloccurrent since the program started.
int: memalloctraceflag
Boolean indicating if we want to trace allocation.
stream: trace_file
File into which to write traces.
double: time_stamp
Origin of traces.
int: trace_procnum
Processor tracing allocations.
*/
extern "C" {
#ifdef MEMORY_TRACE
/*
Function: memAllocTrace
Start tracing memory.
Initialize <memallocmutexdat> if not done.
Defines all tracing variables.
Parameters:
file - Stream where to write traces, opened in write mode.
timestamp - Traces origin.
procnum - Processor writting traces.
Returns:
void - In all cases.
*/
void memAllocTrace (FILE *file,
double timestamp,
int procnum);
/*
Function: memAllocUntrace
Stop tracing allocations.
Returns:
void - in all cases.
*/
void memAllocUntrace ();
#endif
/*
Function: memAllocGetCurrent
Get the current memory allocated.
Returns:
<memalloccurrent> value.
*/
unsigned long memAllocGetCurrent();
/*
Function: memAllocGetMax
Get the maximu memory allocated.
Returns:
<memallocmax> value.
*/
unsigned long memAllocGetMax ();
/*
Function: memAllocTraceReset
Restarts tracing allocation with reseting <memallocmax>.
Returns:
void - in all cases.
*/
void memAllocTraceReset ();
};
/*
Function: operator new
This is a thread-safe memory allocation routine.
Parameters:
size - Memory size wanted.
filename - Used for error message, file where the function is called.
line - Used for erro message, line where the function is called.
Returns:
!NULL - pointer to memory block.
NULL - no array allocated.
*/
void *operator new(std::size_t size) throw(std::bad_alloc);
/*
Function: operator new[]
This is a thread-safe memory allocation routine.
Parameters:
size - Memory size wanted.
filename - Used for error message, file where the function is called.
line - Used for erro message, line where the function is called.
Returns:
!NULL - pointer to memory block.
NULL - no array allocated.
*/
//void *operator new[](std::size_t size) throw(std::bad_alloc);
/*
Function: operator delete
This is a thread-safe memory deallocation routine.
It returns:
void - in all cases
*/
void operator delete(void *memptr);
/*
Function: operator delete[]
This is a thread-safe memory deallocation routine.
It returns:
void - in all cases
*/
//void operator delete[](void *memptr);
#endif /* MEMORY_USAGE */
#endif /* MEMORY_HPP */
......@@ -47,6 +47,7 @@
#include <QObject> // For translations
/* -- */
#include <locale.h> // For dots or commas separator in double numbers
#include <time.h>
/* -- */
#include "Tools.hpp"
/* -- */
......@@ -91,3 +92,29 @@ double convert_to_double(const string arg){
}
}
/*
Function: clockGet
Timing routine.
Uses different timing routines depending on the machine architecture.
Returns:
Returns the time ellapsed since <clockStart>.
*/
double clockGet (void)
{
#if (defined X_ARCHalpha_compaq_osf1) || (defined X_ARCHi686_mac)
struct rusage data;
getrusage (RUSAGE_SELF, &data);
return (((double) data.ru_utime.tv_sec + (double) data.ru_stime.tv_sec) +
((double) data.ru_utime.tv_usec + (double) data.ru_stime.tv_usec) *
1.0e-6L);
#else
struct timespec tp;
clock_gettime (CLOCK_REALTIME, &tp); /* Elapsed time */
return ((double) tp.tv_sec + (double) tp.tv_nsec * (double)1.0e-9L);
#endif
}
......@@ -58,5 +58,9 @@
*/
double convert_to_double(const std::string args);
#define MIN(x,y) (((x)<(y))?(x):(y))
#define MAX(x,y) (((x)<(y))?(y):(x))
double clockGet (void);
#endif // TOOLS_HPP
......@@ -58,8 +58,10 @@
#include <sstream>
/* -- */
/* -- */
#include "common/Errors.hpp" /* Message management */
#include "common/Errors.hpp" /* Errors management */
#include "common/Message.hpp" /* Message management */
#include "common/Tools.hpp" /* Tools */
#include "common/Memory.hpp" /* Memory management */
/* Example:
* *Message::get_instance() << "Test:" << 45 << " and " << true << Message::endi;
* -> display as an informative message.
......
/*
File: trace.h
trace instruction for different format
Authors:
Mathieu Faverge - faverge@labri.fr
Dates:
Varsion 0.0 - from 12 apr 2008
to 12 apr 2008
*/
#ifndef TRACE_H
#define TRACE_H
typedef enum Trace_State {
STATE_ALLOC = 27,
STATE_FREE = 28
} Trace_State_t;
/*
* Common part :
* - TraceFmt_t fmt : Format de trace
* - FILE *file : fichier de trace
* - double time : timestamp
* - INT procnum
* - INT thrdnum
* State :
* - INT level : niveau de trace (+il est grand plus c'est dtaill)
* - Trace_State_T state : Etat dans lequel se retrouve le proc
* - INT id : identifiant de la tche
* Comm :
* - INT dest/src : destinataire ou source du message
* - Trace_Comm_t Type : type de communication
* - INT id : identifiant du message
* - INT size : taille du message
*/
#define trace_start(file, time, procnum, thrdnum) \
fprintf(file, "%9.9lf %ld %ld 0 -1 0\n", (double)time, (long)(procnum), (long)(thrdnum));
#define trace_finish(file, time, procnum, thrdnum) \
fprintf(file, "%9.9lf %ld %ld 0 -1 1\n", (double)time, (long)(procnum), (long)(thrdnum));
#define trace_malloc(file, time, procnum, state, memory) \
fprintf(file, "%9.9lf %ld 0 0 4 %ld %f\n", \
(double)time, (long)procnum, (long)state, (double)memory/(1<<20));
#endif /* TRACE_H */
......@@ -62,9 +62,13 @@
#include <QApplication>
#include <cstdio>
/* Global informations */
#include "common/common.hpp"
#ifdef MEMORY_TRACE
#include "common/trace.h"
#endif
/* -- */
#include "common/info.hpp"
......@@ -81,12 +85,32 @@
int main(int argc, char **argv) {
#ifdef MEMORY_TRACE
FILE * file;
double timestamp;
#endif
Q_INIT_RESOURCE(vite);
Info::Render::_key_alt = false;/* no CTRL key pushed */
#ifdef MEMORY_TRACE
timestamp = clockGet();
file = fopen("toto.trace", "w");
memAllocTrace(file, timestamp, 0);
trace_start(file, timestamp, 0, -1);
#endif /* MEMORY_TRACE */
Core console(argc, argv);
return console.run();
console.run();
#ifdef MEMORY_USAGE
#ifdef MEMORY_TRACE
trace_finish(file, (clockGet()-timestamp), 0, -1);
memAllocUntrace();
#endif
fprintf(stdout, "Max Memory allocated : %ld\n", memAllocGetMax());
fprintf(stdout, "Memory still allocated : %ld\n", memAllocGetCurrent());
#endif
return EXIT_SUCCESS;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment