1 Star 0 Fork 1

hanhai-modules/infra-time

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
infra_time.h 6.89 KB
一键复制 编辑 原始数据 按行查看 历史
like 提交于 2024-06-19 13:25 . fix:fix gtc count get #86372
/*
*Copyright (c) 2024 Black Sesame Technologies
*
*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.
*/
#ifndef INFRA_TIME_H
#define INFRA_TIME_H
#include <limits>
#include <time.h>
#include <stdint.h>
#include <string>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <math.h>
#include <float.h>
#include "gtc_nl_msg.h"
#if defined(QNX) || defined(QNX__) || defined(__QNX__)
#include <sys/syspage.h>
#endif
#ifndef CLOCK_INVALID
#define CLOCK_INVALID -1
#endif
#ifndef UIO_ENABLE
#define UIO_ENABLE 0
#endif
#ifndef KERNEL5
#define KERNEL5 0
#endif
#ifndef KERNEL6
#define KERNEL6 true
#endif
using namespace hanhai;
namespace hanhai {
static int g_nInfraTimeErrCode = 0;
class InfraTime
{
public:
InfraTime();
~InfraTime();
/**
* @brief 静态函数,获取当前时间
*
* @return 返回当前时间的对象
*/
static InfraTime& Now();
/**
* @brief 将时间转换为秒
* @param nErrCode: 错误码 0:成功,-1:获取失败
* @return 转换成功的时间
*/
uint64_t ToSecond(int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 将时间转换为纳秒
* @param nErrCode: 错误码 0:成功,-1:获取失败
* @return 转换成功的时间
*/
uint64_t ToNanosecond(int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 将时间格式化为字符串
* @param nErrCode: 错误码 0:成功,-1:获取失败
* @param bToNano = false:返回到毫秒级别,bToNano = true:返回到纳秒级别
* @return 年-月-日 时:分:秒.毫秒/纳秒
*/
std::string ToDateString(int& nErrCode = g_nInfraTimeErrCode,bool bToNano = false) ;
#if defined(QNX) || defined(QNX__) || defined(__QNX__)
/**
* @brief 将系统时间更新到模块内
* @param nErrCode 错误码 0:成功,-1:失败
* @return
*/
void UpdateClockCyclesFromSysTime(int& nErrCode = g_nInfraTimeErrCode);
#else
/**
* @brief 获取PPS时间
* @param strDev 设备名
* @param oTime 返回的PPS时间信息
* @param nErrCode 错误码 0:成功,-1:设备打开失败, -2:设备使能失败, -3:时间信息获取失败
* @return
*/
void GetPulseSnapShotTS(std::string strDev,timespec& oTime, int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 获取PPS时间
* @param strDev 设备名
* @param nErrCode 错误码 0:成功,-1:设备打开失败, -2:设备使能失败, -3:时间信息获取失败
* @return PPS时间信息
*/
uint64_t GetPulseSnapShotTS(std::string strDev, int& nErrCode = g_nInfraTimeErrCode);
uint64_t GetPulseSnapShotTS(std::string strDev, uint64_t& gtcCount, int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 设置Ptp时间
* @param strDev 设备名
* @param u64Time:需要修改的时间
* @param nErrCode 错误码 0:成功,-1:设备名称错误 , -2:打开设备失败,-3:使能设备失败, -4:clockid 获取失败,
* -5: 设置的时间超出边界, -6:通过clockid获取时间失败, -7:设置时间失败
* @return
*/
void SetPHC(std::string strDev, uint64_t u64Time, int& nErrCode = g_nInfraTimeErrCode);
#endif
/**
* @brief 获取Ptp时间
* @param strDev 设备名
* @param nErrCode 错误码 0:成功,-1:设备名称错误 , -2:打开设备失败,-3:使能设备失败, -4:clockid 获取失败,-5:通过clockid获取时间失败
* @return Ptp时间
*/
uint64_t GetPHC(std::string strDev, int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 获取Counter中断锁存信息 /dev/ptp0
* @param nErrCode 错误码 0:成功,-1:get gtc latch data failed , -2:get gtc latch data failed
* @return Ptp时间
*/
uint64_t GetGTCCounter(int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 设置todpps 输出模式,salve:不输出pps和phc master 输出pps和phc
* @param masterEnable 是否作为master输出
* @param nErrCode 错误码 0:成功,-1和-2:TodppsEnableSet failed , -3:Interface not supported
* @return Ptp时间
*/
void TodppsEnableSet(bool masterEnable, int& nErrCode = g_nInfraTimeErrCode);
/**
* @brief 获取芯片上电的clock cycles
*
* @return cntvct_el0寄存器的值
*/
__inline __attribute__((always_inline)) uint64_t GetClockCycles()
{
#if defined(__x86_64__) || defined(__amd64__)
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return (((uint64_t)hi << 32) | lo);
#else
int64_t timerCycle;
asm volatile("mrs %0, cntvct_el0" : "=r"(timerCycle));
return timerCycle;
#endif
}
/**
* @brief 两段clock cycle的流逝时间
* @param begin: 第一段clock cycle的值
* @param end: 第二段clock cycle的值
* @return 以纳秒为单位的时间值
*/
uint64_t GetElapseTime(const uint64_t begin,const uint64_t end);
/**
*
* @brief system counter的频率
* @return 以纳秒为单位的时间值
*/
__inline __attribute__((always_inline)) uint64_t GetFreq(void)
{
uint64_t freq;
#if defined(QNX) || defined(QNX__) || defined(__QNX__)
freq = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
#else
// asm ("mrs %0, cntfrq_el0" : "=r" (freq));
#endif
return freq;
}
bool GTLNetlinkRelease();
private:
uint64_t sysCounterFreq;
#if defined(QNX) || defined(QNX__) || defined(__QNX__)
uint64_t m_u64LastTime;
uint64_t m_u64LastCycleNum;
double m_dCpns;
int m_nPtpFd;
#else
int GetGMacAddr(std::string strDev);
int CheckPtpEnable(int nFd);
int CheckPtpEnable(std::string strDev);
clockid_t GetClockid(int fd);
void Double2Timespec(double d, struct timespec *ts);
void GTCNLMsgProcess();
bool m_bPtpEnable;
std::unordered_map<std::string ,std::pair<unsigned int*,unsigned int>> m_mGMacAddrs;
std::shared_ptr<std::thread> p_gtc_nl_thread{nullptr};
std::shared_ptr<GTCNLMsg> p_gtc_nl_msg{nullptr};
#endif
std::mutex m_mtx;
}; // class InfraTime
#define INFRA_TIME_NOW_NANO hanhai::InfraTime::Now().ToNanosecond()
#define INFRA_TIME_NOW_SECOND hanhai::InfraTime::Now().ToSecond()
#define INFRA_TIME_NOW_STRING hanhai::InfraTime::Now().ToDateString()
#define INFRA_TIME_NOW_STRING_NAN0 hanhai::InfraTime::Now().ToDateString(true)
//可用于计算代码段耗时
struct TimeUsed
{
TimeUsed(uint64_t* pValue)
{
m_pValue = pValue;
m_u64TimeStart = INFRA_TIME_NOW_NANO;
}
~TimeUsed()
{
*m_pValue = INFRA_TIME_NOW_NANO - m_u64TimeStart;
}
uint64_t* m_pValue;
uint64_t m_u64TimeStart;
};
} // namesapce hanhai
#endif // INFRA_TIME_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hanhai-modules/infra-time.git
git@gitee.com:hanhai-modules/infra-time.git
hanhai-modules
infra-time
infra-time
master

搜索帮助