OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/mach/mach_message_server.h" | 15 #include "util/mach/mach_message_server.h" |
16 | 16 |
17 #include <mach/mach_time.h> | |
18 | |
19 #include <limits> | 17 #include <limits> |
20 | 18 |
21 #include "base/mac/mach_logging.h" | |
22 #include "base/mac/scoped_mach_vm.h" | 19 #include "base/mac/scoped_mach_vm.h" |
| 20 #include "util/misc/clock.h" |
23 | 21 |
24 namespace crashpad { | 22 namespace crashpad { |
25 | 23 |
26 namespace { | 24 namespace { |
27 | 25 |
28 mach_timebase_info_data_t* TimebaseInternal() { | |
29 mach_timebase_info_data_t* timebase_info = new mach_timebase_info_data_t; | |
30 kern_return_t kr = mach_timebase_info(timebase_info); | |
31 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; | |
32 return timebase_info; | |
33 } | |
34 | |
35 mach_timebase_info_data_t* Timebase() { | |
36 static mach_timebase_info_data_t* timebase_info = TimebaseInternal(); | |
37 return timebase_info; | |
38 } | |
39 | |
40 uint64_t NanosecondTicks() { | |
41 uint64_t absolute_time = mach_absolute_time(); | |
42 mach_timebase_info_data_t* timebase_info = Timebase(); | |
43 return absolute_time * timebase_info->numer / timebase_info->denom; | |
44 } | |
45 | |
46 const int kNanosecondsPerMillisecond = 1E6; | 26 const int kNanosecondsPerMillisecond = 1E6; |
47 | 27 |
48 // TimerRunning determines whether |deadline| has passed. If |deadline| is in | 28 // TimerRunning determines whether |deadline| has passed. If |deadline| is in |
49 // the future, |*remaining_ms| is set to the number of milliseconds remaining, | 29 // the future, |*remaining_ms| is set to the number of milliseconds remaining, |
50 // which will always be a positive value, and this function returns true. If | 30 // which will always be a positive value, and this function returns true. If |
51 // |deadline| is zero (indicating that no timer is in effect), |*remaining_ms| | 31 // |deadline| is zero (indicating that no timer is in effect), |*remaining_ms| |
52 // is set to zero and this function returns true. Otherwise, this function | 32 // is set to zero and this function returns true. Otherwise, this function |
53 // returns false. |deadline| is specified on the same time base as is returned | 33 // returns false. |deadline| is specified on the same time base as is returned |
54 // by NanosecondTicks(). | 34 // by ClockMonotonicNanoseconds(). |
55 bool TimerRunning(uint64_t deadline, mach_msg_timeout_t* remaining_ms) { | 35 bool TimerRunning(uint64_t deadline, mach_msg_timeout_t* remaining_ms) { |
56 if (!deadline) { | 36 if (!deadline) { |
57 *remaining_ms = MACH_MSG_TIMEOUT_NONE; | 37 *remaining_ms = MACH_MSG_TIMEOUT_NONE; |
58 return true; | 38 return true; |
59 } | 39 } |
60 | 40 |
61 uint64_t now = NanosecondTicks(); | 41 uint64_t now = ClockMonotonicNanoseconds(); |
62 | 42 |
63 if (now >= deadline) { | 43 if (now >= deadline) { |
64 return false; | 44 return false; |
65 } | 45 } |
66 | 46 |
67 uint64_t remaining = deadline - now; | 47 uint64_t remaining = deadline - now; |
68 | 48 |
69 // Round to the nearest millisecond, taking care not to overflow. | 49 // Round to the nearest millisecond, taking care not to overflow. |
70 const int kHalfMillisecondInNanoseconds = kNanosecondsPerMillisecond / 2; | 50 const int kHalfMillisecondInNanoseconds = kNanosecondsPerMillisecond / 2; |
71 mach_msg_timeout_t remaining_mach; | 51 mach_msg_timeout_t remaining_mach; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 87 |
108 mach_msg_options_t timeout_options = MACH_RCV_TIMEOUT | MACH_SEND_TIMEOUT | | 88 mach_msg_options_t timeout_options = MACH_RCV_TIMEOUT | MACH_SEND_TIMEOUT | |
109 MACH_RCV_INTERRUPT | MACH_SEND_INTERRUPT; | 89 MACH_RCV_INTERRUPT | MACH_SEND_INTERRUPT; |
110 | 90 |
111 uint64_t deadline; | 91 uint64_t deadline; |
112 if (nonblocking == kNonblocking) { | 92 if (nonblocking == kNonblocking) { |
113 options |= timeout_options; | 93 options |= timeout_options; |
114 deadline = 0; | 94 deadline = 0; |
115 } else if (timeout_ms != MACH_MSG_TIMEOUT_NONE) { | 95 } else if (timeout_ms != MACH_MSG_TIMEOUT_NONE) { |
116 options |= timeout_options; | 96 options |= timeout_options; |
117 deadline = NanosecondTicks() + | 97 deadline = ClockMonotonicNanoseconds() + |
118 static_cast<uint64_t>(timeout_ms) * kNanosecondsPerMillisecond; | 98 static_cast<uint64_t>(timeout_ms) * kNanosecondsPerMillisecond; |
119 } else { | 99 } else { |
120 options &= ~timeout_options; | 100 options &= ~timeout_options; |
121 deadline = 0; | 101 deadline = 0; |
122 } | 102 } |
123 | 103 |
124 mach_msg_size_t trailer_alloc = REQUESTED_TRAILER_SIZE(options); | 104 mach_msg_size_t trailer_alloc = REQUESTED_TRAILER_SIZE(options); |
125 mach_msg_size_t max_request_size = interface->MachMessageServerRequestSize(); | 105 mach_msg_size_t max_request_size = interface->MachMessageServerRequestSize(); |
126 mach_msg_size_t request_alloc = round_page(max_request_size + trailer_alloc); | 106 mach_msg_size_t request_alloc = round_page(max_request_size + trailer_alloc); |
127 mach_msg_size_t request_size = (options & MACH_RCV_LARGE) | 107 mach_msg_size_t request_size = (options & MACH_RCV_LARGE) |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 // persistent mode, and just return success when not in persistent mode. | 257 // persistent mode, and just return success when not in persistent mode. |
278 return (persistent == kPersistent) ? MACH_RCV_TIMED_OUT : kr; | 258 return (persistent == kPersistent) ? MACH_RCV_TIMED_OUT : kr; |
279 } | 259 } |
280 } | 260 } |
281 } while (persistent == kPersistent); | 261 } while (persistent == kPersistent); |
282 | 262 |
283 return kr; | 263 return kr; |
284 } | 264 } |
285 | 265 |
286 } // namespace crashpad | 266 } // namespace crashpad |
OLD | NEW |