Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 91cf0bd5 authored by James Hawkins's avatar James Hawkins Committed by Gerrit Code Review
Browse files

Merge "Revert "bootstat: Refactor init/utils/boot_clock into base/chrono_utils.""

parents aa9548db c8ac0677
Loading
Loading
Loading
Loading
+4 −22
Original line number Diff line number Diff line
@@ -45,32 +45,21 @@ cc_library {
            srcs: [
                "errors_unix.cpp",
                "properties.cpp",
                "chrono_utils.cpp",
            ],
            cppflags: ["-Wexit-time-destructors"],
        },
        darwin: {
            srcs: [
                "chrono_utils.cpp",
                "errors_unix.cpp",
            ],
            srcs: ["errors_unix.cpp"],
            cppflags: ["-Wexit-time-destructors"],
        },
        linux_bionic: {
            srcs: [
                "chrono_utils.cpp",
                "errors_unix.cpp",
            ],
            srcs: ["errors_unix.cpp"],
            cppflags: ["-Wexit-time-destructors"],
            enabled: true,
        },
        linux: {
            srcs: [
                "chrono_utils.cpp",
                "errors_unix.cpp",
            ],
            srcs: ["errors_unix.cpp"],
            cppflags: ["-Wexit-time-destructors"],
            host_ldlibs: ["-lrt"],
        },
        windows: {
            srcs: [
@@ -105,14 +94,7 @@ cc_test {
    },
    target: {
        android: {
            srcs: [
                "chrono_utils_test.cpp",
                "properties_test.cpp"
            ],
        },
        linux: {
            srcs: ["chrono_utils_test.cpp"],
            host_ldlibs: ["-lrt"],
            srcs: ["properties_test.cpp"],
        },
        windows: {
            srcs: ["utf8_test.cpp"],

base/chrono_utils_test.cpp

deleted100644 → 0
+0 −47
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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 "android-base/chrono_utils.h"

#include <time.h>

#include <chrono>

#include <gtest/gtest.h>

namespace android {
namespace base {

std::chrono::seconds GetBootTimeSeconds() {
  struct timespec now;
  clock_gettime(CLOCK_BOOTTIME, &now);

  auto now_tp = boot_clock::time_point(
      std::chrono::seconds(now.tv_sec) + std::chrono::nanoseconds(now.tv_nsec));
  return std::chrono::duration_cast<std::chrono::seconds>(
      now_tp.time_since_epoch());
}

// Tests (at least) the seconds accuracy of the boot_clock::now() method.
TEST(ChronoUtilsTest, BootClockNowSeconds) {
  auto now = GetBootTimeSeconds();
  auto boot_seconds = std::chrono::duration_cast<std::chrono::seconds>(
      boot_clock::now().time_since_epoch());
  EXPECT_EQ(now, boot_seconds);
}

}  // namespace base
}  // namespace android
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

bootstat_lib_src_files = [
    "boot_event_record_store.cpp",
    "uptime_parser.cpp",
]

cc_defaults {
+2 −7
Original line number Diff line number Diff line
@@ -20,16 +20,13 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <utime.h>

#include <chrono>
#include <cstdlib>
#include <string>
#include <utility>

#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include "uptime_parser.h"

namespace {

@@ -58,9 +55,7 @@ BootEventRecordStore::BootEventRecordStore() {
}

void BootEventRecordStore::AddBootEvent(const std::string& event) {
  auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
      android::base::boot_clock::now().time_since_epoch());
  AddBootEventWithValue(event, uptime.count());
  AddBootEventWithValue(event, bootstat::ParseUptime());
}

// The implementation of AddBootEventValue makes use of the mtime file
+3 −12
Original line number Diff line number Diff line
@@ -21,18 +21,15 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

#include <chrono>
#include <cstdint>
#include <cstdlib>

#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/test_utils.h>
#include <android-base/unique_fd.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "uptime_parser.h"

using testing::UnorderedElementsAreArray;

@@ -92,12 +89,6 @@ void DeleteDirectory(const std::string& path) {
  rmdir(path.c_str());
}

// Returns the time in seconds since boot.
time_t GetUptimeSeconds() {
  return std::chrono::duration_cast<std::chrono::seconds>(
      android::base::boot_clock::now().time_since_epoch()).count();
}

class BootEventRecordStoreTest : public ::testing::Test {
 public:
  BootEventRecordStoreTest() {
@@ -135,7 +126,7 @@ TEST_F(BootEventRecordStoreTest, AddSingleBootEvent) {
  BootEventRecordStore store;
  store.SetStorePath(GetStorePathForTesting());

  time_t uptime = GetUptimeSeconds();
  time_t uptime = bootstat::ParseUptime();
  ASSERT_NE(-1, uptime);

  store.AddBootEvent("cenozoic");
@@ -150,7 +141,7 @@ TEST_F(BootEventRecordStoreTest, AddMultipleBootEvents) {
  BootEventRecordStore store;
  store.SetStorePath(GetStorePathForTesting());

  time_t uptime = GetUptimeSeconds();
  time_t uptime = bootstat::ParseUptime();
  ASSERT_NE(-1, uptime);

  store.AddBootEvent("cretaceous");
Loading