forked from GoogleCloudPlatform/cloud-debug-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks_monitor.cc
More file actions
106 lines (75 loc) · 2.74 KB
/
callbacks_monitor.cc
File metadata and controls
106 lines (75 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#include "callbacks_monitor.h"
#include <time.h>
namespace devtools {
namespace cdbg {
static CallbacksMonitor* g_instance = nullptr;
void CallbacksMonitor::InitializeSingleton(int max_interval_ms) {
DCHECK(g_instance == nullptr);
g_instance = new CallbacksMonitor(max_interval_ms);
}
void CallbacksMonitor::CleanupSingleton() {
delete g_instance;
g_instance = nullptr;
}
CallbacksMonitor* CallbacksMonitor::GetInstance() {
DCHECK(g_instance != nullptr);
return g_instance;
}
int64 CallbacksMonitor::MonotonicClockMillis() {
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return tp.tv_sec * 1000L + tp.tv_nsec / 1000000L;
}
CallbacksMonitor::Id CallbacksMonitor::RegisterCall(const char* tag) {
OngoingCall ongoing_call { GetCurrentTimeMillis(), tag };
std::lock_guard<std::mutex> lock(mu_);
return ongoing_calls_.insert(ongoing_calls_.begin(), ongoing_call);
}
void CallbacksMonitor::CompleteCall(CallbacksMonitor::Id id) {
int64 current_time_ms = GetCurrentTimeMillis();
std::lock_guard<std::mutex> lock(mu_);
if (current_time_ms - id->start_time_ms > max_call_duration_ms_) {
LOG(INFO) << "Cloud Debugger call \"" << id->tag
<< "\" completed after " << current_time_ms - id->start_time_ms
<< " ms";
last_unhealthy_time_ms_ = current_time_ms;
}
ongoing_calls_.erase(id);
}
bool CallbacksMonitor::IsHealthy(int64 timestamp) const {
bool rc = true;
int64 current_time_ms = GetCurrentTimeMillis();
std::lock_guard<std::mutex> lock(mu_);
if (last_unhealthy_time_ms_ >= timestamp) {
LOG(WARNING) << "Unhealthy callback completed "
<< current_time_ms - last_unhealthy_time_ms_ << " ms ago";
return false;
}
for (auto it = ongoing_calls_.begin(); it != ongoing_calls_.end(); ++it) {
int64 duration_ms = current_time_ms - it->start_time_ms;
if (duration_ms > max_call_duration_ms_) {
LOG(WARNING) << "Cloud Debugger call \"" << it->tag
<< "\" hasn't completed in " << duration_ms
<< " ms, possibly stuck";
rc = false;
}
}
return rc;
}
} // namespace cdbg
} // namespace devtools