|
Meanwhile I found some code snippet that uses a tiny extension to ldebug.c:This code is the only change to the Lua core, the remaining code is using the official API:
LUA_API int lua_getcurrenthookcount(lua_State *L) {return L->hookcount;}
lua_sethook(L, count_hook, LUA_MASKCOUNT, 1000000000);
void count_hook(lua_State *L, lua_Debug *ar) {
void *ud;
lua_getallocf(L, &ud); // get the UD pointer from lua_newstate
struct tUserData * UD = (struct tUserData *)ud; // your userdata structure
UD->billion_steps++; // should have some field for the counter overflow
lua_sethook(L, count_hook, LUA_MASKCOUNT, 1000000000); // register handler again
}
uint64_t getcount(lua_State *L) {
unsigned long l = lua_getcurrenthookcount(L); // missing for the next billion
return (uint64_t)UD->billion_steps * 1000000000 + 1000000000 - l; // total
}I don't know if this is accessible without any changes to the Lua core, but this change is minimal.On Sun, May 2, 2021 at 6:19 PM Flyer31 Test <flyer31@googlemail.com> wrote:Hi Bel,I somehow always had problems in the last weeks in answering in this mailing list, but now it looks like it might work.... this is what I am doing now: I use a debug hook with n=100, and then count these 100 "command hook" events... . Generally quite ok. Just I thought it possibly might be somehow nicer to give this value more exactly... ... but I will survive this +-100 command counter accuracy, this is "quite ok". Anyway I am so impressed and happy with the flawless working of Lua together with my software, and especially also the really very smart and nice error info possibilities, that I am very happy and cannot complain really about Lua... (I compare it to _javascript_ which I had used before, but it was really very bloated up and really difficult for me to implement nice error message to users in case of script errors...).On Sat, May 1, 2021 at 6:11 PM bel <bel2125@gmail.com> wrote:What about using a hook with a high count value and reading the hook count before and after your calls?On Sat, May 1, 2021 at 1:37 PM nerditation <nerditation@outlook.com> wrote:On 2021/4/14 22:47, Flyer31 Test wrote:
> Hi,
>
[...]
>
> Is it possible to readout the current value of this Lua command counter
in an easy way in C or Lua?
>
if by "command counter" you mean the number of VM instructions (or OPCODEs), the answer is no,
one obvious reason there no such API is because the VM is an "implementation detail", and as far
as I know, Lua does not keep track of it at all,
if you insist on using it, you must patch the VM and do it yourself, but I highly suggest against this idea,
and I don't think this counter would make much sense any way.
the VM OPCODEs are designed to be easily mapped to semantics of the Lua language, and easy to implement
efficiently. it is not meant to be used as performance measurement.
becase some of the OPCODEs represent very simple computations such as a single arithmetic addition,
or logical shift; while other OPCODEs might represent quite complex (thus
resource heavy) operations,
such as the generic `for` loop, or creating a closure; the counter of OPCODEs executed/interpreted
by the VM is meaningless for most users.