2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
2008-09-01 05:51:50 -07:00
|
|
|
* Copyright (C) 2008 Cedric Bail, Vincent Torri
|
2008-08-06 11:15:24 -07:00
|
|
|
*
|
|
|
|
* This library is 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.
|
|
|
|
*
|
|
|
|
* This library 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 this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-08-29 06:34:30 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <time.h>
|
|
|
|
#else
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
|
|
|
# undef WIN32_LEAN_AND_MEAN
|
2008-08-31 07:05:17 -07:00
|
|
|
#endif /* _WIN2 */
|
2008-08-06 08:35:56 -07:00
|
|
|
|
|
|
|
#include "eina_counter.h"
|
|
|
|
#include "eina_inlist.h"
|
|
|
|
#include "eina_error.h"
|
|
|
|
#include "eina_private.h"
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-08-06 08:35:56 -07:00
|
|
|
/*============================================================================*
|
2008-08-28 00:46:42 -07:00
|
|
|
* Local *
|
2008-08-06 08:35:56 -07:00
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-08-29 06:49:06 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
typedef struct timespec Eina_Nano_Time;
|
|
|
|
#else
|
|
|
|
typedef LARGE_INTEGER Eina_Nano_Time;
|
|
|
|
#endif
|
|
|
|
|
2008-08-06 08:35:56 -07:00
|
|
|
typedef struct _Eina_Clock Eina_Clock;
|
|
|
|
|
|
|
|
struct _Eina_Counter
|
|
|
|
{
|
|
|
|
Eina_Inlist __list;
|
|
|
|
|
|
|
|
Eina_Inlist *clocks;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Eina_Clock
|
|
|
|
{
|
|
|
|
Eina_Inlist __list;
|
|
|
|
|
2008-08-29 06:49:06 -07:00
|
|
|
Eina_Nano_Time start;
|
|
|
|
Eina_Nano_Time end;
|
2008-08-06 08:35:56 -07:00
|
|
|
int specimen;
|
|
|
|
|
|
|
|
Eina_Bool valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int _eina_counter_init_count = 0;
|
|
|
|
static int EINA_COUNTER_ERROR_OUT_OF_MEMORY = 0;
|
|
|
|
|
2008-08-29 06:34:30 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
static inline int
|
2008-08-29 06:49:06 -07:00
|
|
|
_eina_counter_time_get(Eina_Nano_Time *tp)
|
2008-08-29 06:34:30 -07:00
|
|
|
{
|
|
|
|
return clock_gettime(CLOCK_PROCESS_CPUTIME_ID, tp);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int EINA_COUNTER_ERROR_WINDOWS = 0;
|
|
|
|
static LARGE_INTEGER _eina_counter_frequency;
|
|
|
|
|
|
|
|
static inline int
|
2008-08-29 06:49:06 -07:00
|
|
|
_eina_counter_time_get(Eina_Nano_Time *tp)
|
2008-08-29 06:34:30 -07:00
|
|
|
{
|
2008-08-29 06:49:06 -07:00
|
|
|
return QueryPerformanceCounter(tp);
|
2008-08-29 06:34:30 -07:00
|
|
|
}
|
|
|
|
#endif /* _WIN2 */
|
|
|
|
|
2008-08-06 08:35:56 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-08-06 08:35:56 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
eina_counter_init(void)
|
|
|
|
{
|
|
|
|
_eina_counter_init_count++;
|
|
|
|
|
|
|
|
if (_eina_counter_init_count == 1)
|
|
|
|
{
|
|
|
|
eina_error_init();
|
|
|
|
EINA_COUNTER_ERROR_OUT_OF_MEMORY = eina_error_register("Eina_Counter out of memory");
|
2008-08-29 06:34:30 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!QueryPerformanceFrequency(&_eina_counter_frequency))
|
|
|
|
{
|
|
|
|
EINA_COUNTER_ERROR_WINDOWS = eina_error_register("Change your OS, you moron !");
|
|
|
|
_eina_counter_init_count--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* _WIN2 */
|
2008-08-06 08:35:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return _eina_counter_init_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
eina_counter_shutdown(void)
|
|
|
|
{
|
|
|
|
_eina_counter_init_count--;
|
|
|
|
|
|
|
|
if (_eina_counter_init_count == 0) eina_error_shutdown();
|
|
|
|
|
|
|
|
return _eina_counter_init_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Counter *
|
|
|
|
eina_counter_add(const char *name)
|
|
|
|
{
|
|
|
|
Eina_Counter *counter;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
if (!name) return NULL;
|
|
|
|
|
|
|
|
length = strlen(name) + 1;
|
|
|
|
|
|
|
|
counter = calloc(1, sizeof (Eina_Counter) + length);
|
|
|
|
if (!counter)
|
|
|
|
{
|
|
|
|
eina_error_set(EINA_COUNTER_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter->name = (char*) (counter + 1);
|
|
|
|
memcpy((char*) counter->name, name, length);
|
|
|
|
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
eina_counter_delete(Eina_Counter *counter)
|
|
|
|
{
|
|
|
|
if (!counter) return ;
|
|
|
|
|
|
|
|
while (counter->clocks)
|
|
|
|
{
|
|
|
|
Eina_Clock *clock = (Eina_Clock *) counter->clocks;
|
|
|
|
|
|
|
|
counter->clocks = eina_inlist_remove(counter->clocks, counter->clocks);
|
|
|
|
free(clock);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
eina_counter_start(Eina_Counter *counter)
|
|
|
|
{
|
|
|
|
Eina_Clock *clk;
|
2008-08-29 06:49:06 -07:00
|
|
|
Eina_Nano_Time tp;
|
2008-08-06 08:35:56 -07:00
|
|
|
|
|
|
|
if (!counter) return ;
|
2008-08-29 06:49:06 -07:00
|
|
|
if (_eina_counter_time_get(&tp) != 0) return;
|
2008-08-06 08:35:56 -07:00
|
|
|
|
|
|
|
clk = calloc(1, sizeof (Eina_Clock));
|
|
|
|
if (!clk)
|
|
|
|
{
|
|
|
|
eina_error_set(EINA_COUNTER_ERROR_OUT_OF_MEMORY);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter->clocks = eina_inlist_prepend(counter->clocks, clk);
|
|
|
|
|
|
|
|
clk->valid = EINA_FALSE;
|
|
|
|
clk->start = tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
eina_counter_stop(Eina_Counter *counter, int specimen)
|
|
|
|
{
|
|
|
|
Eina_Clock *clk;
|
2008-08-29 06:49:06 -07:00
|
|
|
Eina_Nano_Time tp;
|
2008-08-06 08:35:56 -07:00
|
|
|
|
|
|
|
if (!counter) return ;
|
2008-08-29 06:49:06 -07:00
|
|
|
if (_eina_counter_time_get(&tp) != 0) return;
|
2008-08-06 08:35:56 -07:00
|
|
|
|
|
|
|
clk = (Eina_Clock *) counter->clocks;
|
|
|
|
|
|
|
|
if (clk->valid == EINA_TRUE) return ;
|
|
|
|
|
|
|
|
clk->end = tp;
|
|
|
|
clk->specimen = specimen;
|
|
|
|
clk->valid = EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
eina_counter_dump(Eina_Counter *counter, FILE *out)
|
|
|
|
{
|
|
|
|
Eina_Clock *clk;
|
|
|
|
|
|
|
|
fprintf(out, "# specimen\texperiment time\tstarting time\tending time\n");
|
|
|
|
|
|
|
|
EINA_INLIST_ITER_LAST(counter->clocks, clk)
|
|
|
|
{
|
2008-08-29 06:34:30 -07:00
|
|
|
long int start;
|
|
|
|
long int end;
|
|
|
|
long int diff;
|
|
|
|
|
2008-08-29 06:49:06 -07:00
|
|
|
if (clk->valid == EINA_FALSE) continue ;
|
|
|
|
|
2008-08-29 06:34:30 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
start = clk->start.tv_sec * 1000000000 + clk->start.tv_nsec;
|
|
|
|
end = clk->end.tv_sec * 1000000000 + clk->end.tv_nsec;
|
|
|
|
diff = (clk->end.tv_sec - clk->start.tv_sec) * 1000000000 + clk->end.tv_nsec - clk->start.tv_nsec;
|
|
|
|
#else
|
2008-08-29 07:45:55 -07:00
|
|
|
start = (long int)(((long long int)clk->start.QuadPart * 1000000000ll) / (long long int)_eina_counter_frequency.QuadPart);
|
|
|
|
end = (long int)(((long long int)clk->end.QuadPart * 1000000000LL) / (long long int)_eina_counter_frequency.QuadPart);
|
|
|
|
diff = (long int)(((long long int)(clk->end.QuadPart - clk->start.QuadPart) * 1000000000LL) / (long long int)_eina_counter_frequency.QuadPart);
|
2008-08-29 06:34:30 -07:00
|
|
|
#endif /* _WIN2 */
|
|
|
|
|
2008-08-29 06:49:06 -07:00
|
|
|
fprintf(out, "%i\t%li\t%li\t%li\n",
|
|
|
|
clk->specimen,
|
|
|
|
diff,
|
|
|
|
start,
|
|
|
|
end);
|
2008-08-06 08:35:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|