Revert "Resolving memory leak issue in Reply function of AppControl"
[platform/framework/web/crosswalk-tizen.git] / common / profiler.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "common/profiler.h"
18
19 #include <math.h>
20 #include <ttrace.h>
21
22 #include "common/logger.h"
23 #include "common/string_utils.h"
24
25 namespace common {
26
27 namespace {
28
29 void PrintProfileTime(const char* step, const struct timespec& start) {
30   struct timespec end;
31   clock_gettime(CLOCK_REALTIME, &end);
32   int64_t diff_in_milli = (end.tv_sec - start.tv_sec) * 1000
33                        + round((end.tv_nsec - start.tv_nsec) * 0.000001);
34   std::ostringstream ss;
35   ss << "END (" << diff_in_milli << "ms)";
36   PrintProfileLog(step, ss.str().c_str());
37 }
38
39 }  //  namespace
40
41 void PrintProfileLog(const char* func, const char* tag) {
42   LOGGER_RAW(DLOG_DEBUG, LOGGER_TAG)
43       << "[PROF] [" << utils::GetCurrentMilliSeconds() << "] "
44       << func << ":" << tag;
45 }
46
47 ScopeProfile::ScopeProfile(const char* step, const bool isStep)
48   : step_(step), expired_(false), isStep_(isStep) {
49   clock_gettime(CLOCK_REALTIME, &start_);
50
51   if (!isStep) {
52     // Remove return type and parameter info from __PRETTY_FUNCTION__
53     int se = step_.find_first_of('(');
54     int ss = step_.find_last_of(' ', se) + 1;
55     if (ss < se) {
56       step_ = step_.substr(ss, se - ss);
57     }
58   }
59
60   PrintProfileLog(step_.c_str(), "START");
61
62   if(isStep_)
63     traceAsyncBegin(TTRACE_TAG_WEB, 0, "%s%s", "XWALK:", step_.c_str());
64   else
65     traceBegin(TTRACE_TAG_WEB,"%s%s", "XWALK:", step_.c_str());
66 }
67
68 ScopeProfile::~ScopeProfile() {
69   if (!expired_) {
70     PrintProfileTime(step_.c_str(), start_);
71
72     if(isStep_)
73       traceAsyncEnd(TTRACE_TAG_WEB, 0, "%s%s", "XWALK:", step_.c_str());
74     else
75       traceEnd(TTRACE_TAG_WEB);
76   }
77 }
78
79 void ScopeProfile::Reset() {
80   clock_gettime(CLOCK_REALTIME, &start_);
81   PrintProfileLog(step_.c_str(), "START-updated");
82
83   if(isStep_)
84     traceAsyncEnd(TTRACE_TAG_WEB, 0, "%s%s", "XWALK:", step_.c_str());
85   else
86     traceEnd(TTRACE_TAG_WEB);
87 }
88
89 void ScopeProfile::End() {
90   expired_ = true;
91   PrintProfileTime(step_.c_str(), start_);
92 }
93
94 StepProfile* StepProfile::GetInstance() {
95   static StepProfile instance;
96   return &instance;
97 }
98
99 StepProfile::StepProfile() {
100 }
101
102 StepProfile::~StepProfile() {
103 }
104
105 void StepProfile::Start(const char* step) {
106   map_[step].reset(new ScopeProfile(step, true));
107 }
108
109 void StepProfile::End(const char* step) {
110   map_[step].reset();
111 }
112
113 }  // namespace common