@@ -430,13 +430,17 @@ bool ProcessRequest(
430
430
boost::beast::http::response<boost::beast::http::string_body>& response,
431
431
HttpServerConnection& server,
432
432
bool & hasStartedStreaming,
433
+ std::chrono::steady_clock::duration& cpuBoundWorkTime,
433
434
boost::asio::yield_context& yc
434
435
)
435
436
{
436
437
namespace http = boost::beast::http;
437
438
438
439
try {
440
+ // Cache the elapsed time to acquire a CPU semaphore used to detect extremely heavy workloads.
441
+ auto start (std::chrono::steady_clock::now ());
439
442
CpuBoundWork handlingRequest (yc);
443
+ cpuBoundWorkTime = std::chrono::steady_clock::now () - start;
440
444
441
445
HttpHandler::ProcessRequest (stream, authenticatedUser, request, response, yc, server);
442
446
} catch (const std::exception& ex) {
@@ -527,9 +531,15 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
527
531
<< " , user: " << (authenticatedUser ? authenticatedUser->GetName () : " <unauthenticated>" )
528
532
<< " , agent: " << request[http::field::user_agent]; // operator[] - Returns the value for a field, or "" if it does not exist.
529
533
530
- Defer addRespCode ([&response, start, &logMsg]() {
531
- logMsg << " , status: " << response.result () << " ) took "
532
- << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now () - start).count () << " ms." ;
534
+ ch::steady_clock::duration cpuBoundWorkTime (0 );
535
+ Defer addRespCode ([&response, start, &logMsg, &cpuBoundWorkTime]() {
536
+ logMsg << " , status: " << response.result () << " )" ;
537
+
538
+ if (ch::duration_cast<ch::seconds>(cpuBoundWorkTime).count ()) {
539
+ logMsg << " waited '" << ch::duration_cast<ch::milliseconds>(cpuBoundWorkTime).count () << " ms' on semaphore and" ;
540
+ }
541
+
542
+ logMsg << " took total " << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now () - start).count () << " ms." ;
533
543
});
534
544
535
545
if (!HandleAccessControl (*m_Stream, request, response, yc)) {
@@ -550,7 +560,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
550
560
551
561
m_Seen = std::numeric_limits<decltype (m_Seen)>::max ();
552
562
553
- if (!ProcessRequest (*m_Stream, request, authenticatedUser, response, *this , m_HasStartedStreaming, yc)) {
563
+ if (!ProcessRequest (*m_Stream, request, authenticatedUser, response, *this , m_HasStartedStreaming, cpuBoundWorkTime, yc)) {
554
564
break ;
555
565
}
556
566
0 commit comments