FreeRDP
profiler.c
1 
20 #include <freerdp/config.h>
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include <freerdp/utils/profiler.h>
26 #include <freerdp/log.h>
27 
28 #define TAG FREERDP_TAG("utils")
29 
30 struct S_PROFILER
31 {
32  char* name;
33  STOPWATCH* stopwatch;
34 };
35 
36 PROFILER* profiler_create(const char* name)
37 {
38  PROFILER* profiler = (PROFILER*)calloc(1, sizeof(PROFILER));
39 
40  if (!profiler)
41  return NULL;
42 
43  profiler->name = _strdup(name);
44  profiler->stopwatch = stopwatch_create();
45 
46  if (!profiler->name || !profiler->stopwatch)
47  goto fail;
48 
49  return profiler;
50 fail:
51  profiler_free(profiler);
52  return NULL;
53 }
54 
55 void profiler_free(PROFILER* profiler)
56 {
57  if (profiler)
58  {
59  free(profiler->name);
60  stopwatch_free(profiler->stopwatch);
61  }
62 
63  free(profiler);
64 }
65 
66 void profiler_enter(PROFILER* profiler)
67 {
68  stopwatch_start(profiler->stopwatch);
69 }
70 
71 void profiler_exit(PROFILER* profiler)
72 {
73  stopwatch_stop(profiler->stopwatch);
74 }
75 
76 void profiler_print_header(void)
77 {
78  WLog_INFO(TAG,
79  "-------------------------------+------------+-------------+-----------+-------");
80  WLog_INFO(TAG,
81  "PROFILER NAME | COUNT | TOTAL | AVG | IPS");
82  WLog_INFO(TAG,
83  "-------------------------------+------------+-------------+-----------+-------");
84 }
85 
86 void profiler_print(PROFILER* profiler)
87 {
88  double s = stopwatch_get_elapsed_time_in_seconds(profiler->stopwatch);
89  double avg = profiler->stopwatch->count == 0 ? 0 : s / profiler->stopwatch->count;
90  WLog_INFO(TAG, "%-30s | %10u | %10.4fs | %8.6fs | %6.0f", profiler->name,
91  profiler->stopwatch->count, s, avg, profiler->stopwatch->count / s);
92 }
93 
94 void profiler_print_footer(void)
95 {
96  WLog_INFO(TAG,
97  "-------------------------------+------------+-------------+-----------+-------");
98 }