2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSFramework
19 /// \brief File defines FrameworkunifiedMsgProfiler class which keeps profiler information for message dispatcher
20 //////////////////////////////////////////////////////////////////////////////////////////////////
22 /* For FRAMEWORKUNIFIEDLOG*/
27 #include <native_service/ns_logger_if.h>
28 #include <native_service/ns_message_center_if.h>
29 /* For PROTOCOL_DIS_MSGPROFILER */
30 #include <native_service/frameworkunified_service_protocol.h>
31 /* For FrameworkunifiedGetAppName */
32 #include <native_service/frameworkunified_framework_if.h>
36 /* Required for OpenSender, SendMessage, McSend, McOpenSender */
37 #include "ns_msg_queue.h"
38 #include "frameworkunified_msgprofiler.h"
40 BOOL FrameworkunifiedMsgProfiler::m_bMsgProfilerEnabled = FALSE;
42 //////////////////////////////////////////
44 //////////////////////////////////////////
45 // LCOV_EXCL_START 8: dead code
46 FrameworkunifiedMsgProfiler::FrameworkunifiedMsgProfiler(const std::string &f_cAppName)
47 : m_cAppName(f_cAppName),
48 m_ui32TotalNoOfMsgsReceived(0),
49 m_ui64MinMsgProcessingTime(720575940), // assign some big value
50 m_ui64MaxMsgProcessingTime(0),
51 m_ui64TotalMsgExecutionTime(0),
52 m_ui64LastMsgReceivedTime(0),
54 m_bLastMsgWasPrintProfile(FALSE) {
55 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
56 m_ui64AppInitTime = GetClock();
60 //////////////////////////////////////////
62 //////////////////////////////////////////
63 // LCOV_EXCL_START 8: dead code
64 FrameworkunifiedMsgProfiler::~FrameworkunifiedMsgProfiler() {
65 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
69 //////////////////////////////////////////
70 // Function : GetAppName
71 //////////////////////////////////////////
72 // LCOV_EXCL_START 8: dead code
73 std::string FrameworkunifiedMsgProfiler::GetAppName() {
74 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
79 //////////////////////////////////////////
80 // Function : MsgReceived
81 //////////////////////////////////////////
82 // LCOV_EXCL_START 8: dead code
83 VOID FrameworkunifiedMsgProfiler::MsgReceived() {
84 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
85 m_ui32TotalNoOfMsgsReceived++;
86 m_ui64LastMsgReceivedTime = GetClock();
90 //////////////////////////////////////////
91 // Function : MsgProcessed
92 //////////////////////////////////////////
93 // LCOV_EXCL_START 8: dead code
94 VOID FrameworkunifiedMsgProfiler::MsgProcessed() {
95 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
96 if (!m_bLastMsgWasPrintProfile) {
97 UI_64 l_ui64MsgProcessedTime = GetClock() - m_ui64LastMsgReceivedTime;
99 // set minimum execution time
100 if (l_ui64MsgProcessedTime < m_ui64MinMsgProcessingTime) {
101 m_ui64MinMsgProcessingTime = l_ui64MsgProcessedTime;
104 // set maximum execution time
105 if (l_ui64MsgProcessedTime > m_ui64MaxMsgProcessingTime) {
106 m_ui64MaxMsgProcessingTime = l_ui64MsgProcessedTime;
109 m_ui64TotalMsgExecutionTime += l_ui64MsgProcessedTime;
111 m_bLastMsgWasPrintProfile = FALSE;
114 // reset the last msg received time
115 m_ui64LastMsgReceivedTime = 0;
119 //////////////////////////////////////////
120 // Function : PrintProfileInfo
121 //////////////////////////////////////////
122 // LCOV_EXCL_START 8: dead code
123 EFrameworkunifiedStatus FrameworkunifiedMsgProfiler::PrintProfileInfo(HANDLE f_hApp) {
124 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
125 HANDLE l_hProfileQ = OpenSender(FRAMEWORKUNIFIED_NS_MSGPROFILERUTIL);
126 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __PRETTY_FUNCTION__, "Inside");
127 CHAR l_cData[MAX_QUEUE_MSG_SIZE] = {};
128 m_bLastMsgWasPrintProfile = TRUE;
129 // don't consider this message which is profiler specific. So decreasing the msg count by 1
130 m_ui32TotalNoOfMsgsReceived--;
131 if (NULL != l_hProfileQ) {
132 UI_64 l_ui64TimeSinceAppStart = (GetClock() - m_ui64AppInitTime) / 1000;
133 UI_64 l_ui64AvgMsgProcessingTime = m_ui64TotalMsgExecutionTime / m_ui32TotalNoOfMsgsReceived;
135 // MSG FORMAT: Tick, App/ThreadName, TotalMsgs, Freq(msgs/sec),
136 // Min MsgProcessingtime(cs,ms),:, Avg MsgProcessingtime (cs,ms) ,:, Max MsgProcessingtime (cs,ms)
137 snprintf(l_cData, MAX_QUEUE_MSG_SIZE, "%010lld, %-23s, %9d, %6.3f, "
138 "%30lld,%-5lld ,:, %30lld,%-5lld ,:, %30lld,%-5lld\n",
139 static_cast<long long int>(l_ui64TimeSinceAppStart), // NOLINT (readability/nolint)
141 m_ui32TotalNoOfMsgsReceived,
142 (F_64)m_ui32TotalNoOfMsgsReceived / (F_64)l_ui64TimeSinceAppStart,
143 static_cast<long long int>(m_ui64MinMsgProcessingTime), // NOLINT (readability/nolint)
144 static_cast<long long int>(m_ui64MinMsgProcessingTime), // NOLINT (readability/nolint)
145 static_cast<long long int>(l_ui64AvgMsgProcessingTime), // NOLINT (readability/nolint)
146 static_cast<long long int>(l_ui64AvgMsgProcessingTime), // NOLINT (readability/nolint)
147 static_cast<long long int>(m_ui64MaxMsgProcessingTime), // NOLINT (readability/nolint)
148 static_cast<long long int>(m_ui64MaxMsgProcessingTime)); // NOLINT (readability/nolint)
150 SendMessage(l_hProfileQ, MAX_QUEUE_MSG_SIZE, l_cData);
152 if (eFrameworkunifiedStatusOK == CloseSender(l_hProfileQ)) {
156 // inform all childs to print msg profiler information.
157 std::vector<std::string>::iterator l_itChildList = m_vChildNames.begin();
159 HANDLE l_hChild = NULL;
160 while (m_vChildNames.end() != l_itChildList) {
161 if (!((*l_itChildList).empty())) {
162 l_hChild = McOpenSender((*l_itChildList).c_str());
163 if (NULL != l_hChild) {
164 if (eFrameworkunifiedStatusOK != McSend(l_hChild, FrameworkunifiedGetAppName(f_hApp), PROTOCOL_DIS_MSGPROFILER, 0, NULL)) {
165 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __PRETTY_FUNCTION__, "Error sending PROTOCOL_DIS_MSGPROFILER to child");
167 FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __PRETTY_FUNCTION__, "Msg PROTOCOL_DIS_MSGPROFILER sent to child");
177 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __PRETTY_FUNCTION__, "Exiting");
178 return eFrameworkunifiedStatusOK;
182 //////////////////////////////////////////
183 // Function : AddChildName
184 //////////////////////////////////////////
185 // LCOV_EXCL_START 8: dead code
186 VOID FrameworkunifiedMsgProfiler::AddChildName(const std::string &f_cChildName) {
187 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
188 if (!f_cChildName.empty()) {
189 m_vChildNames.push_back(f_cChildName);
194 //////////////////////////////////////////
195 // Function : GetClock
196 //////////////////////////////////////////
197 // LCOV_EXCL_START 8: dead code
198 UI_64 FrameworkunifiedMsgProfiler::GetClock() {
199 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 8: dead code
202 if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
203 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "clock_gettime errno:%d", errno);
206 return (UI_64)tp.tv_sec * 1000 * 1000 + tp.tv_nsec / 1000;