【LuatOS】修改LuatOS源码为PC模拟器添加高精度时间戳库timeplus

0x00 缘起

LuatOS以及Lua能够提供微秒或者毫秒的时间戳获取工具,但并没有提供获取纳秒的工具。通过编辑LuatOS源码以及相关BSP源码,添加能够获取纳秒的timeplus库并重新编译,以解决在64位Windows操作系统中LuatOS模拟器获取纳秒的问题,其运行效果如下所示:

  • getunixtime:获取秒时间戳字符串
  • getmillisecond:获取毫秒时间戳字符串
  • getmicrosecond:获取微秒时间戳字符串
  • getnanosecond:获取纳秒时间戳字符串

0x01 方法

参照LuatOS官方文档,对timeplus进行开发,其步骤如下:

  1. Git拉取LuatOS和PC模拟器源代码;
  2. 新增一个通用库的实例;
  3. 注册库函数;
  4. 验证库函数;

1 Git拉取LuatOS和PC模拟器源代码

使用Git命令拉取"LuatOS"项目和"LuatOS跑在PC上"两个项目到本地同级目录下,拉取命令如下:

bash 复制代码
git clone https://gitee.com/openLuat/LuatOS.git
git clone https://gitee.com/openLuat/luatos-soc-pc.git

2 新增一个通用库的实例

进入LuatOS源码文件夹lua/src/目录下,添加名为:luat_lib_timeplus.c的文件,文件内容如下:

c 复制代码
#include <lauxlib.h>
#include <lua.h>
#include <stdio.h>
#include <windows.h>

#include "luat_base.h"

// 获取当前时间的 Unix 时间戳(秒)
static int l_timeplus_getunixtime(lua_State *L) {
  FILETIME ft;
  ULARGE_INTEGER ull;
  GetSystemTimeAsFileTime(&ft);

  ull.LowPart = ft.dwLowDateTime;
  ull.HighPart = ft.dwHighDateTime;

  // 将 FILETIME 转换为 Unix 时间戳(秒)
  time_t unixTime = (ull.QuadPart / 10000000ULL) - 11644473600ULL;

  lua_pushinteger(L, unixTime);
  return 1;
}

// 获取当前时间的 Unix 时间戳(毫秒)
static int l_timeplus_getmillisecond(lua_State *L) {
  FILETIME ft;
  ULARGE_INTEGER ull;
  GetSystemTimeAsFileTime(&ft);

  ull.LowPart = ft.dwLowDateTime;
  ull.HighPart = ft.dwHighDateTime;

  // 将 FILETIME 转换为 Unix 时间戳(毫秒)
  uint64_t milliseconds = (ull.QuadPart / 10000ULL) - 11644473600000ULL;

  lua_pushinteger(L, milliseconds);
  return 1;
}

// 获取当前时间的 Unix 时间戳(微秒)
static int l_timeplus_getmicrosecond(lua_State *L) {
  FILETIME ft;
  ULARGE_INTEGER ull;
  GetSystemTimeAsFileTime(&ft);

  ull.LowPart = ft.dwLowDateTime;
  ull.HighPart = ft.dwHighDateTime;

  // 将 FILETIME 转换为 Unix 时间戳(微秒)
  uint64_t microseconds = (ull.QuadPart / 10ULL) - 11644473600000000ULL;

  lua_pushinteger(L, microseconds);
  return 1;
}

// 获取当前时间的 Unix 时间戳(纳秒)
static int l_timeplus_getnanosecond(lua_State *L) {
  FILETIME ft;
  ULARGE_INTEGER ull;
  GetSystemTimeAsFileTime(&ft);

  ull.LowPart = ft.dwLowDateTime;
  ull.HighPart = ft.dwHighDateTime;

  // 将 FILETIME 转换为 Unix 时间戳(纳秒)
  // 乘以100将100纳秒单位转换为纳秒
  uint64_t nanoseconds_since_epoch =
      (ull.QuadPart - 116444736000000000ULL) * 100ULL;

  lua_pushinteger(L, nanoseconds_since_epoch);
  return 1;
}

// 库函数注册表
#include "rotable2.h"
static const rotable_Reg_t reg_timeplus[] = {
    {"getunixtime", ROREG_FUNC(l_timeplus_getunixtime)},
    {"getmillisecond", ROREG_FUNC(l_timeplus_getmillisecond)},
    {"getmicrosecond", ROREG_FUNC(l_timeplus_getmicrosecond)},
    {"getnanosecond", ROREG_FUNC(l_timeplus_getnanosecond)},
    {NULL, ROREG_INT(0)}};

// 库的声明
LUAMOD_API int luaopen_timeplus(lua_State *L) {
  luat_newlib2(L, reg_timeplus);
  return 1;
}

3 注册库函数

修改LuatOS代码库的 luat/include/luat_libs.h, 新增一行:

c 复制代码
// TimePlus 能够取微秒
LUAMOD_API int luaopen_timeplus(lua_State *L);

修改luatos-soc-pc代码库的port\luat_base_mini.c, 在static const luaL_Reg loadedlibs[]中 {NULL, NULL}};之上添加一行代码:

c 复制代码
{"timeplus", luaopen_timeplus},

4 验证库函数

在luatos-soc-pc代码库中,运行文件:build_windows_64bit_msvc.bat进行编译,输出文件在build文件夹下,编译之前请确保编译工具已经配置可用。运行所编译的文件luatos-lua.exe,在命令行中执行以下代码,并验证输出:

lua 复制代码
timeplus.getunixtime()
timeplus.getmillisecond()
timeplus.getmicrosecond()
timeplus.getnanosecond()

0x02 总结

针对LuatOS Windows模拟器不能提供纳秒时间戳的工具问题,通过添加自定义库timeplus并重新编译以使得模拟器具有输出纳秒时间戳的能力。值得注意的是,当前纳秒的获取方法仅适用与Windows,对于Linux以及其他操作系统,需要修改luat_lib_timeplus.c的实现过程,以提供该功能。

0x03 资源

0x04 后记

  • 己欲立而立人,己欲达而达人。
相关推荐
努力努力再努力wz1 天前
【Lua 进阶系列】从 C++ 对象到 Lua 变量:一文理解 userdata、Lua 栈与元表绑定机制
c++·junit·lua
阿卡好可爱2 天前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
Joy T2 天前
Redis Lua 原子限流:Lua 脚本、竞态条件与并发控制
数据库·redis·lua·限流·竞态条件·redis scripting
努力努力再努力wz6 天前
【Lua进阶系列】Lua 调试机制详解:从虚拟机调用栈、错误传播到 debug 库
junit·单元测试·lua
炸薯条!8 天前
博客系统测试报告
开发语言·lua
BuHuaX10 天前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua
努力努力再努力wz10 天前
【Lua 入门系列】模块与包(一):从多文件组织到require 运行时加载机制、模块缓存、预加载搜索与 Lua 文件搜索机制
开发语言·junit·lua
UWA10 天前
多人战斗中Lua持续高耗时,是瞬时峰值还是高频调用在作祟
性能优化·lua·游戏开发
努力努力再努力wz11 天前
【Lua 入门系列】从执行模型到事件驱动:一文理解 Lua 有栈协程、挂起恢复与协作式调度
开发语言·lua
努力努力再努力wz11 天前
【Lua 入门系列】Lua数据结构:从 Table 出发,一文串联数组、链表、栈、队列、集合、映射与树的实现原理
数据结构·链表·lua