소스 검색

rpc增加服务端耗时统计

wuxiaofeng1 1 년 전
부모
커밋
7d32e277bb

+ 8 - 0
servant/libservant/Application.cpp

@@ -1427,6 +1427,14 @@ void Application::bindAdapter(vector<TC_EpollServer::BindAdapterPtr>& adapters)
 
                 p = _communicator->getStatReport()->createPropertyReport(bindAdapter->getName() + ".timeoutNum", PropertyReport::sum());
                 bindAdapter->_pReportTimeoutNum = p.get();
+
+                p = _communicator->getStatReport()->createPropertyReport(bindAdapter->getName() + ".queueWaitTime",
+                                                                         PropertyReport::avg(), PropertyReport::min(), PropertyReport::max(), PropertyReport::count());
+                bindAdapter->_pReportQueueWaitTime = p.get();
+
+                p = _communicator->getStatReport()->createPropertyReport(bindAdapter->getName() + ".servantHandleTime",
+                                                                         PropertyReport::avg(), PropertyReport::min(), PropertyReport::max(), PropertyReport::count());
+                bindAdapter->_pReportServantHandleTime = p.get();
             }
         }
     }

+ 2 - 0
servant/libservant/Current.cpp

@@ -168,6 +168,8 @@ int Current::getCloseType() const
 
 void Current::initialize(const shared_ptr<TC_EpollServer::RecvContext> &data)
 {
+    _reqTime.setRecvTimeStamp(data->recvTimeStampUs());
+
 	_data = data;
 
 	Application *application = (Application*)this->_servantHandle->getApplication();

+ 19 - 0
servant/libservant/ServantHandle.cpp

@@ -254,6 +254,7 @@ CurrentPtr ServantHandle::createCurrent(const shared_ptr<TC_EpollServer::RecvCon
     try
     {
         current->initialize(data);
+        current->_reqTime.setDispatchTimeStamp(TNOWUS);
     }
     catch (TarsDecodeException &ex)
     {
@@ -355,6 +356,8 @@ void ServantHandle::handleTimeout(const shared_ptr<TC_EpollServer::RecvContext>
     {
         current->sendResponse(TARSSERVERQUEUETIMEOUT);
     }
+
+    reportReqTime(data, current);
 }
 
 void ServantHandle::handleOverload(const shared_ptr<TC_EpollServer::RecvContext> &data)
@@ -373,6 +376,8 @@ void ServantHandle::handleOverload(const shared_ptr<TC_EpollServer::RecvContext>
     {
         current->sendResponse(TARSSERVEROVERLOAD);
     }
+
+    reportReqTime(data, current);
 }
 
 void ServantHandle::handle(const shared_ptr<TC_EpollServer::RecvContext> &data)
@@ -389,6 +394,8 @@ void ServantHandle::handle(const shared_ptr<TC_EpollServer::RecvContext> &data)
     {
         handleNoTarsProtocol(current);
     }
+
+    reportReqTime(data, current);
 }
 
 
@@ -787,5 +794,17 @@ void ServantHandle::handleNoTarsProtocol(const TarsCurrentPtr &current)
 	}
 }
 
+void ServantHandle::reportReqTime(const shared_ptr<TC_EpollServer::RecvContext> &data, const CurrentPtr &current) {
+    current->_reqTime.setFinishTimeStamp(TNOWUS);
+
+    // 上报队列等待时间
+    if (data->adapter()->_pReportQueueWaitTime)
+        data->adapter()->_pReportQueueWaitTime->report(current->_reqTime.queueWaitTime());
+
+    // 上报业务线程处理时间
+    if (data->adapter()->_pReportServantHandleTime)
+        data->adapter()->_pReportServantHandleTime->report(current->_reqTime.servantHandleTime());
+}
+
 ////////////////////////////////////////////////////////////////////////////
 }

+ 6 - 0
servant/servant/Current.h

@@ -21,6 +21,7 @@
 #include "tup/RequestF.h"
 #include "tup/tup.h"
 #include "servant/BaseF.h"
+#include "ReqTime.h"
 
 namespace tars
 {
@@ -325,6 +326,11 @@ protected:
     }
 
 protected:
+    /**
+     * 相关时间戳
+     */
+    ReqTime _reqTime;
+
     /**
      * 操作类指针
      */

+ 43 - 0
servant/servant/ReqTime.h

@@ -0,0 +1,43 @@
+/**
+* Tencent is pleased to support the open source community by making Tars available.
+*
+* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
+*
+* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
+* in compliance with the License. You may obtain a copy of the License at
+*
+* https://opensource.org/licenses/BSD-3-Clause
+*
+* Unless required by applicable law or agreed to in writing, software distributed
+* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+* CONDITIONS OF ANY KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations under the License.
+*/
+#ifndef __TARS_REQTIME_H_
+#define __TARS_REQTIME_H_
+
+namespace tars
+{
+    class ReqTime
+    {
+    public:
+        inline void setRecvTimeStamp(int64_t t) { _recvTimeStamp = t; }
+        inline void setDispatchTimeStamp(int64_t t) { _dispatchTimeStamp = t; }
+        inline void setFinishTimeStamp(int64_t t) { _finishTimeStamp = t; }
+
+        inline int64_t recvTimeStamp() const { return _recvTimeStamp; }
+        inline int64_t dispatchTimeStamp() const { return _dispatchTimeStamp; }
+        inline int64_t finishTimeStamp() const { return _finishTimeStamp; }
+
+        inline int64_t queueWaitTime() const { return _dispatchTimeStamp > 0 ? _dispatchTimeStamp-_recvTimeStamp : 0; }
+        inline int64_t servantHandleTime() const { return _finishTimeStamp > 0 ? _finishTimeStamp-_dispatchTimeStamp : 0; }
+        inline int64_t totalTime() const { return _finishTimeStamp > 0 ? _finishTimeStamp-_recvTimeStamp : 0;}
+
+    protected:
+        int64_t _recvTimeStamp = TNOWUS;     // 接收到数据的时间,微秒
+        int64_t _dispatchTimeStamp = 0; // 投递给业务线程的时间,微秒
+        int64_t _finishTimeStamp = 0;   // 业务线程处理完成时间,微秒
+    };
+}
+
+#endif  // __TARS_REQTIME_H_

+ 2 - 0
servant/servant/ServantHandle.h

@@ -184,6 +184,8 @@ protected:
      * @return bool 如果调用合法返回true,如果调用非法则返回false
      */
 	bool checkValidSetInvoke(const CurrentPtr &current);
+
+    void reportReqTime(const shared_ptr<TC_EpollServer::RecvContext> &data, const CurrentPtr &current);
 protected:
 	/**
 	 * application

+ 2 - 0
util/include/util/tc_epoll_server.h

@@ -1454,6 +1454,8 @@ public:
         PropertyReport *_pReportQueue = NULL;
         PropertyReport *_pReportConRate = NULL;
         PropertyReport *_pReportTimeoutNum = NULL;
+        PropertyReport *_pReportQueueWaitTime = NULL;
+        PropertyReport *_pReportServantHandleTime = NULL;
 
     protected:
         /**