Skip to content

Commit

Permalink
issue: 3749310 Move loops_timer to chrono
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Briskin <[email protected]>
  • Loading branch information
alexbriskin committed Feb 22, 2024
1 parent 1c80375 commit 4ee01b9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
54 changes: 39 additions & 15 deletions src/vma/util/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/sockios.h>
#include <chrono>
#include <limits>
#include <math.h>
#include <linux/ip.h> //IP header (struct iphdr) definition
Expand All @@ -68,6 +69,7 @@
#endif

using namespace std;
using namespace std::chrono;

#undef MODULE_NAME
#define MODULE_NAME "utils:"
Expand Down Expand Up @@ -1212,18 +1214,18 @@ int validate_tso(int if_index)
}

loops_timer::loops_timer()
: m_start()
, m_elapsed(milliseconds(0))
, m_current()
, m_interval_it(2048)
, m_timer_countdown(0)
, m_timeout_msec(-1)
{
m_timeout_msec = -1;
m_timer_countdown = 0;
m_interval_it = 2048;
ts_clear(&m_start);
ts_clear(&m_elapsed);
ts_clear(&m_current);
}

void loops_timer::start()
{
ts_clear(&m_start);
m_start = steady_clock::time_point();
// set to 1 so the first loop is fast and only after it m_start will be initialized
m_timer_countdown = 1;
}
Expand All @@ -1233,23 +1235,45 @@ int loops_timer::time_left_msec()
if ( m_timeout_msec == -1 )
return -1;

if (!ts_isset(&m_start)) { //VMA_RX_POLL==0
gettime(&m_start);
auto current = steady_clock::now();
if (m_start.time_since_epoch() == steady_clock::duration::zero()) {
m_start = current;
}
timespec current;
gettime(&current);
ts_sub(&current, &m_start, &m_elapsed);
m_elapsed = duration_cast<milliseconds>(current - m_start);

//cover the case of left<0
return (m_timeout_msec-ts_to_msec(&m_elapsed))>0 ? m_timeout_msec-ts_to_msec(&m_elapsed) : 0;
return std::max<int>(0, m_timeout_msec - m_elapsed.count());
}

bool loops_timer::is_timeout()
{
if (m_timeout_msec == -1)
return false;

if (m_timer_countdown > 0) {
m_timer_countdown--;
return false;
}
//init counter
m_timer_countdown = m_interval_it;

if (m_start.time_since_epoch() == steady_clock::duration::zero()) {
m_start = steady_clock::now();
}
// update timer
m_current = steady_clock::now();
m_elapsed = duration_cast<milliseconds>(m_current - m_start);

__log_finer("update loops_timer elapsed time=%ld msec\n", m_elapsed.count());

// test for timeout
return m_timeout_msec <= m_elapsed.count();
}
///////////////////////////////////////////
uint32_t fd2inode(int fd)
{
struct stat buf;
int rc = fstat(fd, &buf);
return rc==0 ? buf.st_ino : 0; // no inode is 0
return rc == 0 ? buf.st_ino : 0; // no inode is 0
}

///////////////////////////////////////////
Expand Down
57 changes: 16 additions & 41 deletions src/vma/util/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#ifndef UTILS_H
#define UTILS_H

#include <chrono>
#include <time.h>
#include <string>
#include <string.h>
Expand All @@ -42,7 +43,6 @@
#include <exception>

#include "vtypes.h"
#include "utils/rdtsc.h"
#include "vlogger/vlogger.h"
#include "vma/proto/mem_buf_desc.h"
#include "vma/util/vma_stats.h"
Expand Down Expand Up @@ -396,46 +396,21 @@ static inline void create_mgid_from_ipv4_mc_ip(uint8_t *mgid, uint16_t pkey, uin
* special design for the rx loop.
*/
class loops_timer {
public:
loops_timer();
void start();
int time_left_msec();
void set_timeout_msec(int timeout_msec) { m_timeout_msec = timeout_msec; }
int get_timeout_msec() { return m_timeout_msec; }
inline bool is_timeout() {
if (m_timeout_msec == -1)
return false;

if (m_timer_countdown > 0) {
m_timer_countdown--;
return false;
}
//init counter
m_timer_countdown = m_interval_it;

if (!ts_isset(&m_start)) {
gettime(&m_start);
}
//update timer
gettime(&m_current);
ts_sub(&m_current, &m_start, &m_elapsed);
vlog_printf(VLOG_FUNC_ALL, "update loops_timer (elapsed time=%ld sec %ld usec\n", ts_to_sec(&m_elapsed), ts_to_usec(&m_elapsed));



// test for timeout
if (m_timeout_msec <= ts_to_msec(&m_elapsed))
return true;

return false;
}
private:
timespec m_start;
timespec m_elapsed;
timespec m_current;
int m_interval_it;
int m_timer_countdown;
int m_timeout_msec;
public:
loops_timer();
void start();
int time_left_msec();
void set_timeout_msec(int timeout_msec) { m_timeout_msec = timeout_msec; }
int get_timeout_msec() { return m_timeout_msec; }
bool is_timeout();

private:
std::chrono::time_point<std::chrono::steady_clock> m_start;
std::chrono::milliseconds m_elapsed;
std::chrono::time_point<std::chrono::steady_clock> m_current;
int m_interval_it;
int m_timer_countdown;
int m_timeout_msec;
};

// Returns the filesystem's inode number for the given 'fd' using 'fstat' system call that assumes 32 bit inodes
Expand Down

0 comments on commit 4ee01b9

Please sign in to comment.