Skip to content

Commit

Permalink
Rename Clock::Value to Clock::Epoch, add Clock::Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
xguerin committed Feb 9, 2024
1 parent da7df83 commit 0063231
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 89 deletions.
14 changes: 7 additions & 7 deletions apps/raw_ofed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RawProcessor : public Processor
RawProcessor()
: m_ethto(nullptr)
, m_ethfrom(nullptr)
, m_last(system::Clock::read())
, m_last(system::Clock::now())
, m_lat(0)
, m_count(0)
{}
Expand All @@ -43,7 +43,7 @@ class RawProcessor : public Processor
* Get the timing data.
*/
if (m_last > 0) {
m_lat += system::Clock::read() - m_last;
m_lat += system::Clock::instant() - m_last;
}
/*
* Process the response.
Expand All @@ -62,7 +62,7 @@ class RawProcessor : public Processor
{
m_ethto->setType(len);
memcpy(m_buffer, data, len);
m_last = system::Clock::read();
m_last = system::Clock::now();
Status ret = m_ethto->commit(len, m_buffer);
if (ret != Status::Ok) {
return ret;
Expand All @@ -87,11 +87,11 @@ class RawProcessor : public Processor
return *this;
}

system::Clock::Value averageLatency()
size_t averageLatency()
{
uint64_t res = 0;
if (m_count > 0) {
res = m_lat / m_count;
res = system::Clock::toNanos(m_lat) / m_count;
}
m_lat = 0;
m_count = 0;
Expand All @@ -101,8 +101,8 @@ class RawProcessor : public Processor
private:
ethernet::Producer* m_ethto;
ethernet::Processor* m_ethfrom;
system::Clock::Value m_last;
system::Clock::Value m_lat;
system::Clock::Epoch m_last;
system::Clock::Epoch m_lat;
size_t m_count;
uint8_t* m_buffer;
};
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/User-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Client
*
* @return the average latency of the connection.
*/
virtual system::Clock::Value averageLatency(const ID id) = 0;
virtual size_t averageLatency(const ID id) = 0;
}
```
Clients can have multiple connections. The exact amount of connections can be
Expand Down
2 changes: 1 addition & 1 deletion include/tulips/api/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Client final
Status send(const ID id, const uint32_t len, const uint8_t* const data,
uint32_t& off) override;

system::Clock::Value averageLatency(const ID id) override;
size_t averageLatency(const ID id) override;

/*
* Client-specific interface.
Expand Down
8 changes: 4 additions & 4 deletions include/tulips/api/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class Connection
*/

#ifdef TULIPS_ENABLE_LATENCY_MONITOR
void markOnSent(const system::Clock::Value ts) { m_history.push_back(ts); }
void markOnSent(const system::Clock::Epoch ts) { m_history.push_back(ts); }

void markOnAcked(const system::Clock::Value ts)
void markOnAcked(const system::Clock::Epoch ts)
{
m_count += 1;
m_lat += ts - m_history.front();
Expand All @@ -125,7 +125,7 @@ class Connection

private:
#ifdef TULIPS_ENABLE_LATENCY_MONITOR
using History = std::list<system::Clock::Value>;
using History = std::list<system::Clock::Epoch>;
#endif

State m_state;
Expand All @@ -134,7 +134,7 @@ class Connection
std::optional<std::string> m_host;
#ifdef TULIPS_ENABLE_LATENCY_MONITOR
size_t m_count;
system::Clock::Value m_lat;
size_t m_lat;
History m_history;
#endif
};
Expand Down
4 changes: 2 additions & 2 deletions include/tulips/api/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Delegate
/**
* Timestamp type alias.
*/
using Timestamp = system::Clock::Value;
using Timestamp = system::Clock::Epoch;

/*
* Virtual default destructor.
Expand Down Expand Up @@ -249,7 +249,7 @@ class Client : public transport::Processor
*
* @return the average latency of the connection.
*/
virtual system::Clock::Value averageLatency(const ID id) = 0;
virtual size_t averageLatency(const ID id) = 0;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/tulips/ssl/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Client final
Status send(const ID id, const uint32_t len, const uint8_t* const data,
uint32_t& off) override;

system::Clock::Value averageLatency(const ID id) override;
size_t averageLatency(const ID id) override;

/*
* Client delegate.
Expand Down
14 changes: 7 additions & 7 deletions include/tulips/ssl/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ class Connection
* Open the connection.
*/
void open(SSL_CTX* const ctx, const ID id, void* const cookie,
const system::Clock::Value ts, const int keyfd);
const system::Clock::Epoch ts, const int keyfd);

/**
* Open the connection.
*/
void accept(SSL_CTX* const ctx, const ID id, void* const cookie,
const system::Clock::Value ts, const int keyfd);
const system::Clock::Epoch ts, const int keyfd);

/**
* Close the connection.
Expand All @@ -77,15 +77,15 @@ class Connection
* Process pending data on ACK.
*/
Action onAcked(system::Logger& log, ID const& id, Delegate& delegate,
const system::Clock::Value ts, const uint32_t savl,
const system::Clock::Epoch ts, const uint32_t savl,
uint8_t* const sdat, uint32_t& slen);

/**
* Processing incoming data and encrypt the response.
*/
Action onNewData(system::Logger& log, ID const& id, Delegate& delegate,
const uint8_t* const rdat, const uint32_t rlen,
const bool pushed, const system::Clock::Value ts,
const bool pushed, const system::Clock::Epoch ts,
const uint32_t savl, uint8_t* const sdat, uint32_t& slen);

/**
Expand Down Expand Up @@ -116,7 +116,7 @@ class Connection
/**
* Return the connection's timestamp.
*/
constexpr system::Clock::Value timestamp() const { return m_ts; }
constexpr system::Clock::Epoch timestamp() const { return m_ts; }

/**
* Return the key file's descriptor.
Expand Down Expand Up @@ -178,11 +178,11 @@ class Connection
* Initialize the connection's state.
*/
void initialize(SSL_CTX* const ctx, const ID id, void* const cookie,
const system::Clock::Value ts, const int keyfd);
const system::Clock::Epoch ts, const int keyfd);

ID m_id;
void* m_cookie;
system::Clock::Value m_ts;
system::Clock::Epoch m_ts;
int m_keyfd;
BIO* m_bin;
BIO* m_bout;
Expand Down
2 changes: 1 addition & 1 deletion include/tulips/stack/tcpv4/EventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EventHandler
/**
* Timestamp type alias.
*/
using Timestamp = system::Clock::Value;
using Timestamp = system::Clock::Epoch;

/**
* Default destructor.
Expand Down
43 changes: 32 additions & 11 deletions include/tulips/system/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ namespace tulips::system {
class Clock
{
public:
using Value = uint64_t;
using Epoch = uint64_t;
using Instant = uint64_t;

static constexpr const Value SECOND = 1000000000ULL;
static constexpr const Value MILLISECOND = 1000000ULL;
static const size_t TICKS_PER_SECOND;

static constexpr const size_t SECOND = 1000000000ULL;
static constexpr const size_t MILLISECOND = 1000000ULL;

inline static Clock& get()
{
Expand All @@ -20,26 +23,44 @@ class Clock
}

#ifdef TULIPS_CLOCK_HAS_OFFSET
inline static Value read() { return clock() + get().offset(); }

inline void offsetBy(const Value offset) { m_offset += offset; }
inline static Epoch instant() { return cycles() + toTicks(get().offset()); }
inline static Epoch now() { return clock() + get().offset(); }

inline Value offset() const { return m_offset; }
inline void offsetBy(const size_t offset) { m_offset += offset; }
inline Epoch offset() const { return m_offset; }
#else
inline static Value read() { return clock(); }
inline static Instant instant() { return cycles(); }
inline static Epoch now() { return clock(); }
#endif

inline static size_t toNanos(const size_t v)
{
return (v * SECOND) / TICKS_PER_SECOND;
}

inline static size_t toTicks(const size_t v)
{
return (v * TICKS_PER_SECOND) / SECOND;
}

private:
inline static Value clock()
inline static uint64_t clock()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
}

#if defined(__x86_64__)
inline static Instant cycles() { return __rdtsc(); }
#elif defined(__aarch64__)
inline static Instant cycles() { return clock(); }
#else
#error "Processor architecture not supported"
#endif

#ifdef TULIPS_CLOCK_HAS_OFFSET
Value m_offset = 0;
Epoch m_offset = 0;
#endif
};

}
21 changes: 11 additions & 10 deletions include/tulips/system/Timer.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#pragma once

#include "Clock.h"
#include <chrono>

namespace tulips::system {

class Timer
{
public:
inline void set(const Clock::Value interval)
inline void set(const size_t interval_ns)
{
m_interval = interval;
m_start = Clock::read();
m_interval = Clock::toTicks(interval_ns);
m_start = Clock::instant();
}

inline void reset(const Clock::Value ts) { m_start = ts; }
inline void reset() { m_start = Clock::instant(); }

inline bool expired(const Clock::Value ts) const
inline bool expired() const
{
return ts - m_start >= m_interval;
return Clock::instant() - m_start >= m_interval;
}

inline size_t ticks(const Clock::Value ts) const
inline size_t ticks() const
{
return (ts - m_start) / m_interval;
return (Clock::instant() - m_start) / m_interval;
}

private:
Clock::Value m_start = 0;
Clock::Value m_interval = 0;
Clock::Epoch m_start = 0;
size_t m_interval = 0;
};

}
2 changes: 1 addition & 1 deletion include/tulips/transport/Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace tulips::transport {
class Processor
{
public:
using Timestamp = system::Clock::Value;
using Timestamp = system::Clock::Epoch;

/**
* Virtual destructor.
Expand Down
2 changes: 1 addition & 1 deletion src/api/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Client::send(const ID id, const uint32_t len, const uint8_t* const data,
return m_tcp.send(id, len, data, off);
}

system::Clock::Value
size_t
Client::averageLatency(UNUSED const ID id)
{
#ifdef TULIPS_ENABLE_LATENCY_MONITOR
Expand Down
2 changes: 1 addition & 1 deletion src/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include_directories(${TCLAP_INCLUDE_DIRS})
set(CMAKE_POSITION_INDEPENDENT_CODE 1)

# We need to disable format checking here because various versions of GCC/CLANG
# are not interpreting the Clock::Value type the same way.
# are not interpreting the Clock::Epoch type the same way.
#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format")

Expand Down
10 changes: 4 additions & 6 deletions src/apps/TCPLatency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,10 @@ run(Options const& options, transport::Device::Ref dev)
* Process the delay.
*/
if (options.usDelay() != 0) {
auto ts = system::Clock::read();
if (!timer.expired(ts)) {
if (!timer.expired()) {
break;
}
timer.reset(ts);
timer.reset();
}
/*
* Check if we need to stop.
Expand Down Expand Up @@ -436,11 +435,10 @@ run(Options const& options, transport::Device::Ref dev)
* Process the artificial delay.
*/
if (options.usDelay() != 0) {
auto ts = system::Clock::read();
if (!timer.expired(ts)) {
if (!timer.expired()) {
continue;
}
timer.reset(ts);
timer.reset();
}
/*
* Process the stack
Expand Down
2 changes: 1 addition & 1 deletion src/ssl/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ Client::send(const ID id, const uint32_t len, const uint8_t* const data,
return flush(id);
}

system::Clock::Value
size_t
Client::averageLatency(const ID id)
{
return m_client.averageLatency(id);
Expand Down
Loading

0 comments on commit 0063231

Please sign in to comment.