Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: components/browser_watcher/system_session_analyzer_win.cc

Issue 2715903003: Bound the impact of system instability on chrome instability. (Closed)
Patch Set: Merge fixups Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/browser_watcher/system_session_analyzer_win.h"
6
7 #include <windows.h>
8 #include <winevt.h>
9
10 #include <utility>
11
12 #include "base/macros.h"
13 #include "base/time/time.h"
14
15 namespace browser_watcher {
16
17 namespace {
18
19 // The name of the log channel to query.
20 const wchar_t kChannelName[] = L"System";
21
22 // Event ids of system startup / shutdown events. These were obtained from
23 // inspection of the System log in Event Viewer on Windows 10:
24 // - id 6005: "The Event log service was started."
25 // - id 6006: "The Event log service was stopped."
26 // - id 6008: "The previous system shutdown at <time> on <date> was
27 // unexpected."
28 const uint16_t kIdSessionStart = 6005U;
29 const uint16_t kIdSessionEnd = 6006U;
30 const uint16_t kIdSessionEndUnclean = 6008U;
31
32 // An XPATH expression to query for system startup / shutdown events. The query
33 // is expected to retrieve exactly one event for each startup (kIdSessionStart)
34 // and one event for each shutdown (either kIdSessionEnd or
35 // kIdSessionEndUnclean).
36 const wchar_t kSessionEventsQuery[] =
37 L"*[System[Provider[@Name='eventlog']"
38 L" and (EventID=6005 or EventID=6006 or EventID=6008)]]";
39
40 // XPath expressions to attributes of interest.
41 const wchar_t kEventIdPath[] = L"Event/System/EventID";
42 const wchar_t kEventTimePath[] = L"Event/System/TimeCreated/@SystemTime";
43
44 // The timeout to use for calls to ::EvtNext.
45 const uint32_t kTimeoutMs = 5000;
46
47 struct EvtHandleCloser {
48 using pointer = EVT_HANDLE;
49 void operator()(EVT_HANDLE handle) const {
50 if (handle)
51 ::EvtClose(handle);
52 }
53 };
54
55 using EvtHandle = std::unique_ptr<EVT_HANDLE, EvtHandleCloser>;
56
57 base::Time ULLFileTimeToTime(ULONGLONG time_ulonglong) {
58 // Copy low / high parts as FILETIME is not always 64bit aligned.
59 ULARGE_INTEGER time;
60 time.QuadPart = time_ulonglong;
61 FILETIME ft;
62 ft.dwLowDateTime = time.LowPart;
63 ft.dwHighDateTime = time.HighPart;
64
65 return base::Time::FromFileTime(ft);
66 }
67
68 // Create a render context (i.e. specify attributes of interest).
69 EvtHandle CreateRenderContext() {
70 LPCWSTR value_paths[] = {kEventIdPath, kEventTimePath};
71 const DWORD kValueCnt = arraysize(value_paths);
72
73 EVT_HANDLE context = NULL;
74 context =
75 ::EvtCreateRenderContext(kValueCnt, value_paths, EvtRenderContextValues);
76 if (!context)
77 DLOG(ERROR) << "Failed to create render context.";
78
79 return EvtHandle(context);
80 }
81
82 bool GetEventInfo(EVT_HANDLE context,
83 EVT_HANDLE event,
84 SystemSessionAnalyzer::EventInfo* info) {
85 DCHECK(context);
86 DCHECK(event);
87 DCHECK(info);
88
89 // Retrieve attributes of interest from the event. We expect the context to
90 // specify the retrieval of two attributes (event id and event time), each
91 // with a specific type.
92 const DWORD kAttributeCnt = 2U;
93 std::vector<EVT_VARIANT> buffer(kAttributeCnt);
94 DWORD buffer_size = kAttributeCnt * sizeof(EVT_VARIANT);
95 DWORD buffer_used = 0U;
96 DWORD retrieved_attribute_cnt = 0U;
97 if (!::EvtRender(context, event, EvtRenderEventValues, buffer_size,
98 buffer.data(), &buffer_used, &retrieved_attribute_cnt)) {
99 DLOG(ERROR) << "Failed to render the event.";
100 return false;
101 }
102
103 // Validate the count and types of the retrieved attributes.
104 if ((retrieved_attribute_cnt != kAttributeCnt) ||
105 (buffer[0].Type != EvtVarTypeUInt16) ||
106 (buffer[1].Type != EvtVarTypeFileTime)) {
107 return false;
108 }
109
110 info->event_id = buffer[0].UInt16Val;
111 info->event_time = ULLFileTimeToTime(buffer[1].FileTimeVal);
112
113 return true;
114 }
115
116 } // namespace
117
118 SystemSessionAnalyzer::SystemSessionAnalyzer(uint32_t session_cnt)
119 : session_cnt_(session_cnt) {}
120
121 SystemSessionAnalyzer::~SystemSessionAnalyzer() {}
122
123 SystemSessionAnalyzer::Status SystemSessionAnalyzer::IsSessionUnclean(
124 base::Time timestamp) {
125 if (!initialized_) {
126 DCHECK(!init_success_);
127 init_success_ = Initialize();
128 initialized_ = true;
129 }
130 if (!init_success_)
131 return FAILED;
132 if (timestamp < coverage_start_)
133 return OUTSIDE_RANGE;
134
135 // Get the first session starting after the timestamp.
136 std::map<base::Time, base::TimeDelta>::const_iterator it =
137 unclean_sessions_.upper_bound(timestamp);
138 if (it == unclean_sessions_.begin())
139 return CLEAN; // No prior unclean session.
140
141 // Get the previous session and see if it encompasses the timestamp.
142 --it;
143 bool is_spanned = (timestamp - it->first) <= it->second;
144 return is_spanned ? UNCLEAN : CLEAN;
145 }
146
147 bool SystemSessionAnalyzer::FetchEvents(
148 std::vector<EventInfo>* event_infos) const {
149 DCHECK(event_infos);
150
151 // Query for the events. Note: requesting events from newest to oldest.
152 EVT_HANDLE query_raw =
153 ::EvtQuery(nullptr, kChannelName, kSessionEventsQuery,
154 EvtQueryChannelPath | EvtQueryReverseDirection);
155 if (!query_raw) {
156 DLOG(ERROR) << "Event query failed.";
157 return false;
158 }
159 EvtHandle query(query_raw);
160
161 // Retrieve events: 2 events per session, plus the current session's start.
162 DWORD desired_event_cnt = 2U * session_cnt_ + 1U;
163 std::vector<EVT_HANDLE> events_raw(desired_event_cnt, NULL);
164 DWORD event_cnt = 0U;
165 BOOL success = ::EvtNext(query.get(), desired_event_cnt, events_raw.data(),
166 kTimeoutMs, 0, &event_cnt);
167
168 // Ensure handles get closed. The MSDN sample seems to imply handles may need
169 // to be closed event in if EvtNext failed.
170 std::vector<EvtHandle> events(desired_event_cnt);
171 for (size_t i = 0; i < event_cnt; ++i)
172 events[i].reset(events_raw[i]);
173
174 if (!success) {
175 DLOG(ERROR) << "Failed to retrieve events.";
176 return false;
177 }
178
179 // Extract information from the events.
180 EvtHandle render_context = CreateRenderContext();
181 if (!render_context.get())
182 return false;
183
184 std::vector<EventInfo> event_infos_tmp;
185 event_infos_tmp.reserve(event_cnt);
186
187 EventInfo info = {};
188 for (size_t i = 0; i < event_cnt; ++i) {
189 if (!GetEventInfo(render_context.get(), events[i].get(), &info))
190 return false;
191 event_infos_tmp.push_back(info);
192 }
193
194 event_infos->swap(event_infos_tmp);
195 return true;
196 }
197
198 bool SystemSessionAnalyzer::Initialize() {
199 std::vector<SystemSessionAnalyzer::EventInfo> events;
200 if (!FetchEvents(&events))
201 return false;
202
203 // Validate the number and ordering of events (newest to oldest). The
204 // expectation is a (start / [unclean]shutdown) pair of events for each
205 // session, as well as an additional event for the current session's start.
206 size_t event_cnt = events.size();
207 if ((event_cnt % 2) == 0)
208 return false; // Even number is unexpected.
209 if (event_cnt < 3)
210 return false; // Not enough data for a single session.
211 for (size_t i = 1; i < event_cnt; ++i) {
212 if (events[i - 1].event_time < events[i].event_time)
213 return false;
214 }
215
216 // Step through (start / shutdown) event pairs, validating the types of events
217 // and recording unclean sessions.
218 std::map<base::Time, base::TimeDelta> unclean_sessions;
219 size_t start = 2U;
220 size_t end = 1U;
221 for (; start < event_cnt && end < event_cnt; start += 2, end += 2) {
222 uint16_t start_id = events[start].event_id;
223 uint16_t end_id = events[end].event_id;
224 if (start_id != kIdSessionStart)
225 return false; // Unexpected event type.
226 if (end_id != kIdSessionEnd && end_id != kIdSessionEndUnclean)
227 return false; // Unexpected event type.
228
229 if (end_id == kIdSessionEnd)
230 continue; // This is a clean session.
231
232 unclean_sessions.insert(
233 std::make_pair(events[start].event_time,
234 events[end].event_time - events[start].event_time));
235 }
236
237 unclean_sessions_.swap(unclean_sessions);
238 DCHECK_GT(event_cnt, 0U);
239 coverage_start_ = events[event_cnt - 1].event_time;
240
241 return true;
242 }
243
244 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698