blob: b762836cbce5b36c018d91a58a2210d1233fc72f [file] [log] [blame]
Florian Mayerb4334002018-02-01 11:10:36 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "perfetto/base/watchdog.h"
18
19#include "perfetto/base/logging.h"
20
21#include <signal.h>
22#include <stdint.h>
23
24namespace perfetto {
25namespace base {
26
27WatchDog::WatchDog(time_t millisecs) {
Florian Mayerf5b8d722018-02-01 13:12:17 +000028 struct sigevent sev = {};
Florian Mayerb4334002018-02-01 11:10:36 +000029 sev.sigev_notify = SIGEV_SIGNAL;
30 sev.sigev_signo = SIGABRT;
Florian Mayerb4334002018-02-01 11:10:36 +000031 PERFETTO_CHECK(timer_create(CLOCK_MONOTONIC, &sev, &timerid_) != -1);
Florian Mayerf5b8d722018-02-01 13:12:17 +000032 struct itimerspec its = {};
Florian Mayerb4334002018-02-01 11:10:36 +000033 its.it_value.tv_sec = millisecs / 1000;
34 its.it_value.tv_nsec = 1000000L * (millisecs % 1000);
Florian Mayerb4334002018-02-01 11:10:36 +000035 PERFETTO_CHECK(timer_settime(timerid_, 0, &its, nullptr) != -1);
36}
37
38WatchDog::~WatchDog() {
39 PERFETTO_CHECK(timer_delete(timerid_) != -1);
40}
41
42} // namespace base
43} // namespace perfetto