1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * Profiler Utils
4  *
5  * Copyright 2011 Stephen Erisman
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include <freerdp/utils/profiler.h>
28 #include <freerdp/log.h>
29 
30 #define TAG FREERDP_TAG("utils")
31 
32 struct _PROFILER
33 {
34 	char* name;
35 	STOPWATCH* stopwatch;
36 };
37 
profiler_create(const char * name)38 PROFILER* profiler_create(const char* name)
39 {
40 	PROFILER* profiler = (PROFILER*)calloc(1, sizeof(PROFILER));
41 
42 	if (!profiler)
43 		return NULL;
44 
45 	profiler->name = _strdup(name);
46 	profiler->stopwatch = stopwatch_create();
47 
48 	if (!profiler->name || !profiler->stopwatch)
49 		goto fail;
50 
51 	return profiler;
52 fail:
53 	profiler_free(profiler);
54 	return NULL;
55 }
56 
profiler_free(PROFILER * profiler)57 void profiler_free(PROFILER* profiler)
58 {
59 	if (profiler)
60 	{
61 		free(profiler->name);
62 		stopwatch_free(profiler->stopwatch);
63 	}
64 
65 	free(profiler);
66 }
67 
profiler_enter(PROFILER * profiler)68 void profiler_enter(PROFILER* profiler)
69 {
70 	stopwatch_start(profiler->stopwatch);
71 }
72 
profiler_exit(PROFILER * profiler)73 void profiler_exit(PROFILER* profiler)
74 {
75 	stopwatch_stop(profiler->stopwatch);
76 }
77 
profiler_print_header(void)78 void profiler_print_header(void)
79 {
80 	WLog_INFO(TAG,
81 	          "-------------------------------+------------+-------------+-----------+-------");
82 	WLog_INFO(TAG,
83 	          "PROFILER NAME                  |      COUNT |       TOTAL |       AVG |    IPS");
84 	WLog_INFO(TAG,
85 	          "-------------------------------+------------+-------------+-----------+-------");
86 }
87 
profiler_print(PROFILER * profiler)88 void profiler_print(PROFILER* profiler)
89 {
90 	double s = stopwatch_get_elapsed_time_in_seconds(profiler->stopwatch);
91 	double avg = profiler->stopwatch->count == 0 ? 0 : s / profiler->stopwatch->count;
92 	WLog_INFO(TAG, "%-30s | %10u | %10.4fs | %8.6fs | %6.0f", profiler->name,
93 	          profiler->stopwatch->count, s, avg, profiler->stopwatch->count / s);
94 }
95 
profiler_print_footer(void)96 void profiler_print_footer(void)
97 {
98 	WLog_INFO(TAG,
99 	          "-------------------------------+------------+-------------+-----------+-------");
100 }
101