2012-05-19 04:55:05 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
profile.c
|
|
|
|
|
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
|
|
|
|
|
|
|
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; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program 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 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 to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __PROFILE_C
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
static bool _init = FALSE;
|
|
|
|
static FILE *_file;
|
|
|
|
|
|
|
|
static uint64_t get_time(void)
|
|
|
|
{
|
2012-05-20 19:58:34 +02:00
|
|
|
static uint64_t last = 0;
|
|
|
|
|
2012-05-19 04:55:05 +02:00
|
|
|
struct timeval time;
|
2012-05-20 19:58:34 +02:00
|
|
|
uint64_t t;
|
|
|
|
|
2012-05-19 04:55:05 +02:00
|
|
|
gettimeofday(&time, NULL);
|
2012-05-20 19:58:34 +02:00
|
|
|
t = (uint64_t)time.tv_sec * 1000000 + (uint64_t)time.tv_usec - last;
|
|
|
|
last += t;
|
|
|
|
return t;
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROFILE_init(void)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX + 1];
|
|
|
|
|
|
|
|
if (_init)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sprintf(path, ".prof.%d", getpid());
|
|
|
|
|
|
|
|
//fprintf(stderr, "gb.profile: start profiling: %s\n", path);
|
|
|
|
|
|
|
|
_file = fopen(path, "w");
|
|
|
|
if (!_file)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gb.profile: cannot create profile file '%s': %s\n", path, strerror(errno));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
_init = TRUE;
|
2012-05-20 19:58:34 +02:00
|
|
|
get_time();
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROFILE_exit(void)
|
|
|
|
{
|
|
|
|
if (!_init)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//fprintf(stderr, "gb.profile: stop profiling\n");
|
|
|
|
|
|
|
|
fclose(_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PROFILE_add(void *cp, void *fp, void *pc)
|
|
|
|
{
|
|
|
|
ushort line;
|
|
|
|
|
2012-05-20 19:58:34 +02:00
|
|
|
line = 0;
|
|
|
|
DEBUG_calc_line_from_position(cp, fp, pc, &line);
|
|
|
|
fprintf(_file, "%d %" PRId64 "\n", line, get_time());
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROFILE_begin(void *cp, void *fp)
|
|
|
|
{
|
2012-05-20 19:58:34 +02:00
|
|
|
const char *where = cp ? DEBUG_get_position(cp, fp, NULL) : ".System.EventLoop";
|
|
|
|
fprintf(_file, "+%s %" PRId64 "\n", where, get_time());
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROFILE_end(void *cp, void *fp)
|
|
|
|
{
|
2012-05-20 19:58:34 +02:00
|
|
|
const char *where = cp ? DEBUG_get_position(cp, fp, NULL) : ".System.EventLoop";
|
|
|
|
fprintf(_file, "-%s %" PRId64 "\n", where, get_time());
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|