Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
refactor: make clock decoupled from rdsn runtime (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 authored Feb 18, 2020
1 parent baa3e91 commit ef73706
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 31 deletions.
9 changes: 0 additions & 9 deletions include/dsn/c/api_layer1.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,6 @@ typedef struct
int size;
} dsn_file_buffer_t;

/*!
@defgroup env Environment
Non-deterministic Environment Input
Note developers can easily plugin their own implementation to
replace the underneath implementation of these primitives.
@{
*/
extern DSN_API uint64_t dsn_now_ns();

__inline uint64_t dsn_now_us() { return dsn_now_ns() / 1000; }
Expand Down
2 changes: 0 additions & 2 deletions include/dsn/tool-api/env_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class env_provider
typedef env_provider *(*factory)(env_provider *);

env_provider(env_provider *inner_provider);

DSN_API virtual uint64_t now_ns() const;
};
/*@}*/
} // end namespace
32 changes: 32 additions & 0 deletions include/dsn/utility/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#pragma once

#include <memory>

namespace dsn {
namespace utils {

class clock
{
public:
clock() = default;
virtual ~clock() = default;

// Gets current time in nanoseconds.
virtual uint64_t now_ns() const;

// Gets singleton instance. eager singleton, which is thread safe
static const clock *instance();

// Resets the global clock implementation (not thread-safety)
static void mock(clock *mock_clock);

private:
static std::unique_ptr<clock> _clock;
};

} // namespace utils
} // namespace dsn
24 changes: 24 additions & 0 deletions src/core/core/clock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <dsn/utility/clock.h>
#include <dsn/utility/time_utils.h>
#include <dsn/utility/dlib.h>
#include <dsn/utility/smart_pointers.h>

DSN_API uint64_t dsn_now_ns() { return dsn::utils::clock::instance()->now_ns(); }

namespace dsn {
namespace utils {

std::unique_ptr<clock> clock::_clock = make_unique<clock>();

const clock *clock::instance() { return _clock.get(); }

uint64_t clock::now_ns() const { return get_current_physical_time_ns(); }

void clock::mock(clock *mock_clock) { _clock.reset(mock_clock); }

} // namespace utils
} // namespace dsn
2 changes: 0 additions & 2 deletions src/core/core/env_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ namespace dsn {

env_provider::env_provider(env_provider *inner_provider) {}

uint64_t env_provider::now_ns() const { return utils::get_current_physical_time_ns(); }

} // end namespace
11 changes: 0 additions & 11 deletions src/core/core/service_api_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,6 @@ DSN_API void dsn_rpc_forward(dsn::message_ex *request, dsn::rpc_address addr)
::dsn::rpc_address(addr));
}

//------------------------------------------------------------------------------
//
// env
//
//------------------------------------------------------------------------------
DSN_API uint64_t dsn_now_ns()
{
// return ::dsn::task::get_current_env()->now_ns();
return ::dsn::service_engine::instance().env()->now_ns();
}

//------------------------------------------------------------------------------
//
// system
Expand Down
2 changes: 0 additions & 2 deletions src/core/tools/simulator/env.sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ namespace tools {

/*static*/ int sim_env_provider::_seed;

uint64_t sim_env_provider::now_ns() const { return scheduler::instance().now_ns(); }

void sim_env_provider::on_worker_start(task_worker *worker)
{
rand::reseed_thread_local_rng(
Expand Down
3 changes: 0 additions & 3 deletions src/core/tools/simulator/env.sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class sim_env_provider : public env_provider
{
public:
sim_env_provider(env_provider *inner_provider);

// service local time (can be logical or physical)
virtual uint64_t now_ns() const override;
static int seed() { return _seed; }

private:
Expand Down
24 changes: 24 additions & 0 deletions src/core/tools/simulator/sim_clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#pragma once

#include <dsn/utility/clock.h>
#include "scheduler.h"

namespace dsn {
namespace tools {

class sim_clock : public utils::clock
{
public:
sim_clock() = default;
virtual ~sim_clock() = default;

// Gets simulated time in nanoseconds.
virtual uint64_t now_ns() const { return scheduler::instance().now_ns(); }
};

} // namespace tools
} // namespace dsn
8 changes: 6 additions & 2 deletions src/core/tools/simulator/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "diske.sim.h"
#include "env.sim.h"
#include "task_engine.sim.h"
#include "sim_clock.h"

namespace dsn {
namespace tools {
Expand Down Expand Up @@ -118,6 +119,9 @@ void simulator::install(service_spec &spec)
}

sys_exit.put_front(simulator::on_system_exit, "simulator");

// the new sim_clock is taken over by unique_ptr in clock instance
utils::clock::instance()->mock(new sim_clock());
}

void simulator::on_system_exit(sys_exit_type st)
Expand All @@ -131,5 +135,5 @@ void simulator::run()
scheduler::instance().start();
tool_app::run();
}
}
} // end namespace dsn::tools
} // namespace tools
} // namespace dsn

0 comments on commit ef73706

Please sign in to comment.