diff options
Diffstat (limited to 'common/src/statistics.c')
-rwxr-xr-x | common/src/statistics.c | 188 |
1 files changed, 137 insertions, 51 deletions
diff --git a/common/src/statistics.c b/common/src/statistics.c index 5eaa56e..9b8284b 100755 --- a/common/src/statistics.c +++ b/common/src/statistics.c @@ -1,19 +1,18 @@ -/* - * Copyright 2012 Samsung Electronics Co., Ltd - * - * Licensed under the Flora License, Version 1.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.tizenopensource.org/license - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +/*
+ * Copyright 2012 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.tizenopensource.org/license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
#include <sys/time.h> // gettimeofday @@ -31,10 +30,70 @@ #define EXPORT_API #endif +#ifdef _WIN32 +#include <time.h> +#include <Windows.h> + +#pragma comment(lib,"Ws2_32.lib") + +#define _CRT_SECURE_NO_WARNINGS +#define _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_SECURE_NO_DEPRECATE + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} + +#else #include <sys/time.h> #include <sys/utsname.h> #include <sys/resource.h> #include <unistd.h> +#endif + #include <stdarg.h> #include "statistics.h" @@ -46,7 +105,7 @@ #define MM_TA_MAX_ACCUM 100 -typedef struct _mm_ta_accum_item +typedef struct _iv_ta_accum_item { unsigned long long elapsed_accum; unsigned long num_calls; @@ -56,11 +115,12 @@ typedef struct _mm_ta_accum_item unsigned long long last_end; char* name; + int lvl; unsigned long long timestamp; int on_estimate; int num_unpair; -} mm_ta_accum_item; +} iv_ta_accum_item; static void PrintLog(const char *file, int line, const char *msg, ...) @@ -79,24 +139,24 @@ static void PrintLog(const char *file, int line, const char *msg, ...) // internal func. static void __free_accums(void); -static int __get_accum_index(const char* name); +static int __get_accum_index(int lvl, const char* name); // global var. -static mm_ta_accum_item ** g_accums = NULL; +static iv_ta_accum_item ** g_accums = NULL; static int g_accum_index = 0; static int g_accum_longest_name = 0; static unsigned long long g_accum_first_time = MAX_UINT64; // jmlee -int PERF_INIT(void) +int IV_PERF_INIT(void) { if (g_accums) { return 0; } - g_accums = (mm_ta_accum_item **) malloc ( MM_TA_MAX_ACCUM * sizeof(mm_ta_accum_item *) ); + g_accums = (iv_ta_accum_item **) malloc ( MM_TA_MAX_ACCUM * sizeof(iv_ta_accum_item *) ); if(!g_accums) { assert(0); @@ -108,7 +168,7 @@ int PERF_INIT(void) return 0; } -int PERF_DEINIT(void) +int IV_PERF_DEINIT(void) { if ( ! g_accums ) { @@ -123,7 +183,7 @@ int PERF_DEINIT(void) } -static int __get_accum_index(const char* name) +static int __get_accum_index(int lvl, const char* name) { int i; @@ -132,7 +192,7 @@ static int __get_accum_index(const char* name) // find index for ( i = 0; i < g_accum_index; i++ ) { - if ( strcmp( name, g_accums[i]->name ) == 0 ) + if ( (lvl == g_accums[i]->lvl) && (strcmp( name, g_accums[i]->name ) == 0) ) return i; } @@ -168,9 +228,9 @@ static void __free_accums(void) -int mm_ta_accum_item_begin(const char* name, bool show, const char* filename, int line) +int iv_ta_accum_item_begin(int lvl, const char* name, bool show, const char* filename, int line) { - mm_ta_accum_item * accum = NULL; + iv_ta_accum_item * accum = NULL; int index = 0; int name_len = 0; struct timeval t; @@ -184,14 +244,14 @@ int mm_ta_accum_item_begin(const char* name, bool show, const char* filename, in if ( !name ) return -1; - name_len = strlen(name); + name_len = strlen(name) + (lvl * 2); if( name_len == 0 ) return -1; // if 'name' is new one. create new item. - if ( (index = __get_accum_index(name)) == -1 ) + if ( (index = __get_accum_index(lvl, name)) == -1 ) { - accum = ( mm_ta_accum_item * ) malloc( sizeof( mm_ta_accum_item ) ); + accum = ( iv_ta_accum_item * ) malloc( sizeof( iv_ta_accum_item ) ); if ( !accum ) { assert(0); @@ -199,10 +259,11 @@ int mm_ta_accum_item_begin(const char* name, bool show, const char* filename, in } // clear first. - memset( accum, 0, sizeof (mm_ta_accum_item) ); + memset( accum, 0, sizeof (iv_ta_accum_item) ); accum->elapsed_min = MAX_UINT64; accum->name = strdup(name); + accum->lvl = lvl; // add it to list. g_accums[g_accum_index] = accum; g_accum_index++; @@ -248,9 +309,9 @@ int mm_ta_accum_item_begin(const char* name, bool show, const char* filename, in return 0; } -int mm_ta_accum_item_end(const char* name, bool show, const char* filename, int line) +int iv_ta_accum_item_end(int lvl, const char* name, bool show, const char* filename, int line) { - mm_ta_accum_item * accum = NULL; + iv_ta_accum_item * accum = NULL; unsigned long long tval = 0LL; int index = 0; struct timeval t; @@ -268,7 +329,7 @@ int mm_ta_accum_item_end(const char* name, bool show, const char* filename, int return -1; // varify the 'name' is already exist. - if ( (index = __get_accum_index(name)) == -1 ) + if ( (index = __get_accum_index(lvl, name)) == -1 ) { MyPrintf("[%s] is not added before!\n", name); return -1; @@ -291,6 +352,13 @@ int mm_ta_accum_item_end(const char* name, bool show, const char* filename, int // update last_end accum->last_end = tval; +#if 0 + if ( accum->first_start > accum->last_end ) + { + MyPrintf("Invalied timestamp:%s. Start:%lu End=%lu\n", accum->name, accum->first_start , accum->last_end); + } +#endif + // make get elapsed time. tval = tval - accum->timestamp; @@ -310,9 +378,6 @@ int mm_ta_accum_item_end(const char* name, bool show, const char* filename, int void __print_some_info(FILE* fp) { - if (!fp) - return; - // General infomation { time_t t_val; @@ -366,28 +431,29 @@ void __print_some_info(FILE* fp) } } - fprintf(fp, "g_accum_first_time = %llu\n", g_accum_first_time); + fprintf(fp, "g_accum_first_time = %llu", g_accum_first_time); fprintf(fp, "\n\n"); } -void mm_ta_accum_show_result_fp(FILE *fp) +void iv_ta_accum_show_result_fp(FILE *fp) { int i = 0; char format[256]; -// __print_some_info(fp); + __print_some_info(fp); + fprintf(fp, "\n\n"); fprintf(fp, "============================ BEGIN RESULT ACCUM (usec) ====================\n"); - snprintf(format, (size_t)sizeof(format), "[Idx] %%-%ds %%10s %%6s %%10s %%10s %%10s %%4s \n", g_accum_longest_name); + snprintf(format, (size_t)sizeof(format), "[Idx] [Lvl] %%-%ds %%10s %%10s %%6s %%10s %%10s %%4s \n", g_accum_longest_name); - fprintf(fp, format, "Name", "avg", "hit", "total", "min", "max", "pair"); + fprintf(fp, format, "Name", "avg", "total", "hit", "min", "max", "pair"); - snprintf(format, (size_t)sizeof(format), "[%%3d] %%-%ds %%10llu %%6lu %%10llu %%10llu %%10llu %%4s \n", g_accum_longest_name); + snprintf(format, (size_t)sizeof(format), "[%%3d] [%%3d] %%-%ds %%10llu %%10llu %%6lu %%10llu %%10llu %%4s \n", g_accum_longest_name); for ( i = 0; i < g_accum_index; i++ ) { @@ -395,13 +461,23 @@ void mm_ta_accum_show_result_fp(FILE *fp) if (g_accums[i]->num_calls == 0) g_accums[i]->num_calls = 1; + static char space[256]; + + int j; + for ( j = 0; j < g_accums[i]->lvl * 2; j++) + { + space[j] = '_'; + } + space[g_accums[i]->lvl*2] = '\0'; + fprintf(fp, format, i, - g_accums[i]->name, + g_accums[i]->lvl, + strcat(space, g_accums[i]->name), (g_accums[i]->elapsed_accum == 0)?0:(g_accums[i]->elapsed_accum / g_accums[i]->num_calls), // Fix it! : devide by zero. - g_accums[i]->num_calls, g_accums[i]->elapsed_accum, + g_accums[i]->num_calls, g_accums[i]->elapsed_min, g_accums[i]->elapsed_max, g_accums[i]->num_unpair == 1 ? "F" : "T" ); @@ -409,13 +485,23 @@ void mm_ta_accum_show_result_fp(FILE *fp) fprintf(fp, "============================ END RESULT ACCUM ============================\n"); - if ( fp != stdout && fp != stderr ) - { - fclose(fp); - } } +#define _CONSTRUCTOR __attribute__ ((constructor)) +#define _DESTRUCTOR __attribute__ ((destructor)) + +static void _CONSTRUCTOR _DLLInit(void) +{ + IV_PERF_INIT(); +} + +static void _DESTRUCTOR _DLLExit(void) +{ + IV_PERF_DEINIT(); + +} + #ifdef STANDALONE int main(int argc, char* argv[]) @@ -423,7 +509,7 @@ int main(int argc, char* argv[]) int a = 0, b = 0; - PERF_CHECK_BEGIN("Test 1"); + PERF_CHECK_BEGIN(LVL1, "Test 1"); for ( a = 0 ; a < 10; a++) { @@ -431,7 +517,7 @@ int main(int argc, char* argv[]) usleep(1*10E6); } - PERF_CHECK_END("Test 1"); + PERF_CHECK_END(LVL1, "Test 1"); printf("Test 111\n"); return 0; |