summaryrefslogtreecommitdiff
path: root/common/src/statistics.c
blob: 9b8284bc57732ac32ab1e37662c1aed7c0e1c1d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * 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

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>
#include <time.h>

#include "statistics.h"
#include "debug.h"


#ifdef STANDALONE
#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"

#define MAX_UINT32 (0xFFFFFFFFL)
#define MAX_UINT64 (0xFFFFFFFFFFFFFFFFLL)

// defs.
#define MM_TA_MAX_ACCUM			100


typedef struct _iv_ta_accum_item
{
	unsigned long long elapsed_accum;
	unsigned long num_calls;
	unsigned long long elapsed_min;
	unsigned long long elapsed_max;
	unsigned long long first_start;
	unsigned long long last_end;

	char* name;
	int lvl;

	unsigned long long timestamp;
	int on_estimate;
	int num_unpair;
} iv_ta_accum_item;


static void PrintLog(const char *file, int line, const char *msg, ...)
{
	va_list va;

	va_start(va, msg);
	fprintf(stderr ,"[STAT] %s:%d:",file, line);
	vfprintf(stderr ,msg, va);
	fprintf(stderr, "\n");
	va_end(va);
}

#define MyPrintf(...)  PrintLog(__FILE__, __LINE__, ##__VA_ARGS__ )


// internal func.
static void __free_accums(void);
static int __get_accum_index(int lvl, const char* name);


// global var.
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 IV_PERF_INIT(void)
{
	if (g_accums)
	{
		return 0;
	}

	g_accums = (iv_ta_accum_item **) malloc ( MM_TA_MAX_ACCUM * sizeof(iv_ta_accum_item *) );
	if(!g_accums)
	{
		assert(0);
		return -1;
	}

	g_accum_first_time = MAX_UINT64;

	return 0;
}

int IV_PERF_DEINIT(void)
{
	if ( ! g_accums )
	{
		return 0;
	}

	__free_accums();

	g_accum_first_time = MAX_UINT64;

	return 0;
}


static int __get_accum_index(int lvl, const char* name)
{
	int i;

	assert(name);

	// find index
	for ( i = 0; i < g_accum_index; i++ )
	{
		if ( (lvl == g_accums[i]->lvl) && (strcmp( name, g_accums[i]->name ) == 0) )
			return i;
	}

	return -1;
}

static void __free_accums(void)
{
	int i = 0;

	if ( ! g_accums )
		return;

	for ( i = 0; i < g_accum_index; i++ )
	{
		if ( g_accums[i] )
		{
			if ( g_accums[i]->name )
				free ( g_accums[i]->name );

			free ( g_accums[i] );

			g_accums[i] = NULL;
		}
	}

	g_accum_index = 0;
	g_accum_longest_name = 0;

	free ( g_accums );
	g_accums = NULL;
}



int iv_ta_accum_item_begin(int lvl, const char* name, bool show, const char* filename, int line)
{
	iv_ta_accum_item * accum = NULL;
	int index = 0;
	int name_len = 0;
	struct timeval t;

	if (!g_accums)
		return 0;

	if ( g_accum_index == MM_TA_MAX_ACCUM )
		return -1;

	if ( !name )
		return -1;

	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(lvl, name)) == -1 )
	{
		accum = ( iv_ta_accum_item * ) malloc( sizeof( iv_ta_accum_item ) );
		if ( !accum )
		{
			assert(0);
			return -1;
		}

		// clear first.
		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++;

		if ( g_accum_longest_name < name_len )
			g_accum_longest_name = name_len;

	}
	else
	{
		accum = g_accums[index];
	}

	// verify pairs of begin, end.
	if (accum->on_estimate)
	{
		MyPrintf("[%s] is not 'end'ed!\n", accum->name);
		accum->num_unpair ++;
		return -1;
	}

	accum->on_estimate = 1;

	// get timestamp
	gettimeofday( &t, NULL );
	accum->timestamp = t.tv_sec * 1000000UL + t.tv_usec;

	if ( accum->first_start == 0 )
	{	// assum that timestamp never could be zero.
		accum->first_start = accum->timestamp;

		if ( g_accum_first_time > accum->first_start )
		{
			g_accum_first_time = accum->first_start ;
		}
	}

	if ( show )
		MyPrintf("[ACCUM BEGIN] %s : %ld ---(%s:%d)\n", name, accum->timestamp, filename, line );

	accum->num_calls++;

	return 0;
}

int iv_ta_accum_item_end(int lvl, const char* name, bool show, const char* filename, int line)
{
	iv_ta_accum_item * accum = NULL;
	unsigned long long tval = 0LL;
	int index = 0;
	struct timeval	t;

	if (!g_accums)
		return 0;

	if ( g_accum_index == MM_TA_MAX_ACCUM )
		return -1;

	if ( !name )
		return -1;

	if( strlen ( name ) == 0 )
		return -1;

	// varify the 'name' is already exist.
	if ( (index = __get_accum_index(lvl, name)) == -1 )
	{
		MyPrintf("[%s] is not added before!\n", name);
		return -1;
	}

	accum = g_accums[index];

	// verify pairs of begin, end.
	if (!accum->on_estimate)
	{
		MyPrintf("[%s] is not 'begin' yet!\n", accum->name);
		accum->num_unpair ++;
		return -1;
	}

	// get time first for more accuracy.
	gettimeofday( &t, NULL );
	tval = t.tv_sec*1000000UL + t.tv_usec;

	// 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;

	// update min/max
	accum->elapsed_max = tval > accum->elapsed_max ? tval : accum->elapsed_max;
	accum->elapsed_min = tval < accum->elapsed_min ? tval : accum->elapsed_min;

	if ( show )
		MyPrintf("[ACCUM END] %s : %llu + %llu ---(%s:%d)\n", name, accum->elapsed_accum, tval, filename, line );

	// add elapsed time
	accum->elapsed_accum = accum->elapsed_accum + tval;
	accum->on_estimate = 0;

	return 0;
}

void __print_some_info(FILE* fp)
{
	// General infomation
	{
		time_t t_val;
		char hostname[256] = {'\0',};
#ifdef LINUX
		struct utsname uts;
		struct rusage r_usage;
#endif
		fprintf(fp, "\n[[ General info ]]\n");

		// time and date
		time(&t_val);
		fprintf(fp, "Date : %s", ctime(&t_val) );

		// system
		if ( gethostname(hostname, 255) == 0 )
		{
			fprintf(fp, "Hostname : %s\n", hostname);
		}
#ifdef LINUX
		if ( uname(&uts) >= 0 )
		{
			fprintf(fp, "System : %s\n", uts.sysname);
			fprintf(fp, "Machine : %s\n", uts.machine);
			fprintf(fp, "Nodename : %s\n", uts.nodename);
			fprintf(fp, "Release : %s \n", uts.release);
			fprintf(fp, "Version : %s \n", uts.version);
		}

		// process info.
		fprintf(fp, "Process priority : %d\n", getpriority(PRIO_PROCESS, getpid()) );

		getrusage(RUSAGE_SELF, &r_usage);
		fprintf(fp, "CPU usage : User = %ld.%06ld, System = %ld.%06ld\n",
				r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
				r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec );
#endif

	}

	// host environment variables
	{
		extern char** environ;
		char** env = environ;

		fprintf(fp, "\n[[ Host environment variables ]]\n");
		while(*env)
		{
			fprintf(fp, "%s\n", *env);
			env++;
		}
	}

	fprintf(fp, "g_accum_first_time = %llu", g_accum_first_time);

	fprintf(fp, "\n\n");

}


void iv_ta_accum_show_result_fp(FILE *fp)
{
	int i = 0;
	char format[256];

	__print_some_info(fp);

	fprintf(fp, "\n\n");

	fprintf(fp, "============================ BEGIN RESULT ACCUM (usec) ====================\n");

	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", "total", "hit", "min", "max", "pair");

	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++ )
	{
		// prevent 'devide by zero' error
		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]->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]->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" );
	}

	fprintf(fp, "============================ END RESULT ACCUM  ============================\n");

}


#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[])
{
	int a = 0, b = 0;


	PERF_CHECK_BEGIN(LVL1, "Test 1");

	for ( a = 0 ; a < 10; a++)
	{
		printf("AAA=%d\n", a);
		usleep(1*10E6);
	}

	PERF_CHECK_END(LVL1, "Test 1");

	printf("Test 111\n");
	return 0;
}
#endif