2 * Copyright (c) 2011 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.
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
20 * @brief This is implementation file for profiling function.
23 #include "profiling_util.h"
32 #include <dpl/foreach.h>
33 #include <dpl/log/log.h>
34 #include <dpl/mutex.h>
37 const int PROFILING_OUTPUT_DESCRIPTOR = 3;
39 unsigned long long toULong(const struct timeval& value)
41 unsigned long long ret = static_cast<unsigned long long>(value.tv_sec) *
43 ret += static_cast<unsigned long long>(value.tv_usec);
49 unsigned long long time;
52 const char* description;
59 PacketResult(unsigned long long t,
69 void Print(FILE *filePtr)
77 fprintf(filePtr, "%s#%s#%llu#[%s]\n", prefix, name, time, description);
81 std::vector<PacketResult> results;
86 int newfd = dup(PROFILING_OUTPUT_DESCRIPTOR);
88 fp = fdopen(newfd, "w");
94 fp = stdout; //fallback
96 fprintf(fp, "###PROFILING###START###\n");
97 FOREACH(result, results)
101 fprintf(fp, "###PROFILING###STOP###\n");
104 fclose(fp); // newfd is also closed
108 void sigUsrHandler(int /*num*/)
115 const int i = initialize();
116 DPL::Mutex* m_mutex = NULL;
121 m_mutex = new DPL::Mutex;
122 results.reserve(64 * 1024);
123 signal(SIGUSR1, &sigUsrHandler);
124 signal(SIGUSR2, &sigUsrHandler);
125 LogDebug("Initialized profiling");
126 AddProfilingMeasurment("Profiling_Started");
130 std::string GetFormattedTime()
135 gettimeofday(&tv, NULL);
136 localtime_r(&tv.tv_sec, &localNowTime);
141 "%02i:%02i:%02i.%03i",
142 localNowTime.tm_hour,
145 static_cast<int>(tv.tv_usec / 1000));
148 } // namespace anonymous
150 void AddStdoutProfilingMeasurment(const char* name, bool start)
152 std::ostringstream output;
153 output << "[" << GetFormattedTime() << "] [](): " << name << " ";
154 output << (start ? "profiling##start" : "profiling##stop");
155 fprintf(stdout, "%s\n", output.str().c_str());
159 void AddProfilingMeasurment(const char* name,
161 const char* description)
163 DPL::Mutex::ScopedLock lock(m_mutex);
164 struct timeval value;
165 gettimeofday(&value, NULL);
167 PacketResult(toULong(value), name, prefix, description));