Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_FrameworkCore / src / frameworkunified_msgprofiler.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NSFramework
19 /// \brief    File defines FrameworkunifiedMsgProfiler class which keeps profiler information for message dispatcher
20 //////////////////////////////////////////////////////////////////////////////////////////////////
21
22 /* For FRAMEWORKUNIFIEDLOG*/
23 #include <stdio.h>
24 #include <time.h>
25 #include <errno.h>
26
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>
33
34 #include <string>
35 #include <vector>
36 /* Required for OpenSender, SendMessage, McSend, McOpenSender */
37 #include "ns_msg_queue.h"
38 #include "frameworkunified_msgprofiler.h"
39
40 BOOL FrameworkunifiedMsgProfiler::m_bMsgProfilerEnabled = FALSE;
41
42 //////////////////////////////////////////
43 // Constructor
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),
53     m_ui64AppInitTime(0),
54     m_bLastMsgWasPrintProfile(FALSE) {
55   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 8: dead code
56   m_ui64AppInitTime = GetClock();
57 }
58 // LCOV_EXCL_STOP
59
60 //////////////////////////////////////////
61 // Destructor
62 //////////////////////////////////////////
63 // LCOV_EXCL_START 8: dead code
64 FrameworkunifiedMsgProfiler::~FrameworkunifiedMsgProfiler() {
65   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 8: dead code
66 }
67 // LCOV_EXCL_STOP
68
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
75   return m_cAppName;
76 }
77 // LCOV_EXCL_STOP
78
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();
87 }
88 // LCOV_EXCL_STOP
89
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;
98
99     // set minimum execution time
100     if (l_ui64MsgProcessedTime < m_ui64MinMsgProcessingTime) {
101       m_ui64MinMsgProcessingTime = l_ui64MsgProcessedTime;
102     }
103
104     // set maximum execution time
105     if (l_ui64MsgProcessedTime > m_ui64MaxMsgProcessingTime) {
106       m_ui64MaxMsgProcessingTime = l_ui64MsgProcessedTime;
107     }
108
109     m_ui64TotalMsgExecutionTime += l_ui64MsgProcessedTime;
110   } else {
111     m_bLastMsgWasPrintProfile = FALSE;
112   }
113
114   // reset the last msg received time
115   m_ui64LastMsgReceivedTime = 0;
116 }
117 // LCOV_EXCL_STOP
118
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;
134
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)
140              m_cAppName.c_str(),
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)
149
150     SendMessage(l_hProfileQ, MAX_QUEUE_MSG_SIZE, l_cData);
151
152     if (eFrameworkunifiedStatusOK == CloseSender(l_hProfileQ)) {
153       l_hProfileQ = NULL;
154     }
155
156     // inform all childs to print msg profiler information.
157     std::vector<std::string>::iterator l_itChildList =  m_vChildNames.begin();
158
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");
166           } else {
167             FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __PRETTY_FUNCTION__, "Msg PROTOCOL_DIS_MSGPROFILER sent to child");
168           }
169
170           McClose(l_hChild);
171           l_hChild = NULL;
172         }
173       }
174       l_itChildList++;
175     }
176   }
177   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __PRETTY_FUNCTION__, "Exiting");
178   return eFrameworkunifiedStatusOK;
179 }
180 // LCOV_EXCL_STOP
181
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);
190   }
191 }
192 // LCOV_EXCL_STOP
193
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
200   struct timespec tp;
201
202   if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
203     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "clock_gettime errno:%d", errno);
204     return 0;
205   }
206   return (UI_64)tp.tv_sec * 1000 * 1000 + tp.tv_nsec / 1000;
207 }
208 // LCOV_EXCL_STOP
209
210 // EOF