2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "common/profiler.h"
22 #include "common/logger.h"
23 #include "common/string_utils.h"
29 void PrintProfileTime(const char* step, const struct timespec& start) {
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());
41 void PrintProfileLog(const char* func, const char* tag) {
42 LOGGER_RAW(DLOG_DEBUG, LOGGER_TAG)
43 << "[PROF] [" << utils::GetCurrentMilliSeconds() << "] "
44 << func << ":" << tag;
47 ScopeProfile::ScopeProfile(const char* step, const bool isStep)
48 : step_(step), expired_(false), isStep_(isStep) {
49 clock_gettime(CLOCK_REALTIME, &start_);
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;
56 step_ = step_.substr(ss, se - ss);
60 PrintProfileLog(step_.c_str(), "START");
63 traceAsyncBegin(TTRACE_TAG_WEB, 0, "%s%s", "XWALK:", step_.c_str());
65 traceBegin(TTRACE_TAG_WEB,"%s%s", "XWALK:", step_.c_str());
68 ScopeProfile::~ScopeProfile() {
70 PrintProfileTime(step_.c_str(), start_);
73 traceAsyncEnd(TTRACE_TAG_WEB, 0, "%s%s", "XWALK:", step_.c_str());
75 traceEnd(TTRACE_TAG_WEB);
79 void ScopeProfile::Reset() {
80 clock_gettime(CLOCK_REALTIME, &start_);
81 PrintProfileLog(step_.c_str(), "START-updated");
84 traceAsyncEnd(TTRACE_TAG_WEB, 0, "%s%s", "XWALK:", step_.c_str());
86 traceEnd(TTRACE_TAG_WEB);
89 void ScopeProfile::End() {
91 PrintProfileTime(step_.c_str(), start_);
94 StepProfile* StepProfile::GetInstance() {
95 static StepProfile instance;
99 StepProfile::StepProfile() {
102 StepProfile::~StepProfile() {
105 void StepProfile::Start(const char* step) {
106 map_[step].reset(new ScopeProfile(step, true));
109 void StepProfile::End(const char* step) {
113 } // namespace common