When will LuaJIT create objects with GCT as Str type?

50 views Asked by At
# luajit lj_obj.h
```c
#define LJ_TSTR         (~4u)
```
# ebpf code
```c
static long loop_gc_obj(u32 index, callback_ctx *ctx)
{
    int res;
    GCRef p = ctx->p;
    // read o
    GCobj *o;
    res = bpf_probe_read(&o, sizeof(o), (GCobj *)(p.gcptr64));
    if (res != 0) {
        return 1;
    }

    // read gct type
    uint8_t gct;
    res = bpf_probe_read(&gct, sizeof(gct), &(o->gch.gct));
    if (res != 0) {
        return 1;
    }
    int gct_size = luajit_objlen(o, gct);

    // next gco
    res = bpf_probe_read(&p, sizeof(p), &(o->gch.nextgc));
    if (res != 0) {
        return 1;
    }
    ctx->p = p;

    return 0;
}

SEC("kprobe/handle_entry_lua")
int handle_entry_lua(struct pt_regs *ctx)
{
    // ...
    GCRef p;
    bpf_probe_read(&p, sizeof(p), &(g->gc.root));
    callback_ctx loop_ctx = {
        .p = p,
    };
    bpf_loop(1 << 23, loop_gc_obj, &loop_ctx, 0);

    return 0;
}

    # question
    ```text
    +--------------------------------------------------------------------+
    | Gct   Type Name   Count     Sum             Max        Min         |
    +--------------------------------------------------------------------+
    | 5     upvalue     183       8784            48         48          |
    | 6     thread      1         0               0          0           |
    | 7     proto       77        30043           1868       120         |
    | 8     function    215       11984           160        40          |
    | 9     trace       8         0               0          0           |
    | 10    cdata       203       0               0          0           |
    | 11    table       72        21568           3136       64          |
    | 12    udata       2         144             80         64          |
    +--------------------------------------------------------------------+

As shown in the above code, I am using eBPF to analyze the memory usage of luajit GC objects, but I have found that no matter how I create String type objects, I cannot obtain objects with gct of string type.

local str1 = "Hello, "
local str2 = "World!"
local result = str1 .. str2

1

There are 1 answers

2
fsfod On

String objects don't get put in the main object linked list. They are instead tracked by the interned string hashtable(StrInternState::tab) stored in the global_State, also there nextgc field is used for chaining in the hashtable when a slot has multiple values.

You can use this snippet of code taken from a system i made to create a snapshots of the GC heap as guide how to walk the table

int collect_strings(global_State *g)
{
  uint32_t count = 0;

  for (MSize i = 0; i <= g->str.mask; i++) {  
    GCobj *o = (GCobj *)(gcrefu(g->str.tab[i]) & ~(uintptr_t)1);

    /* Walk a string hash chain. */
    for (; o != NULL; o = gcref(o->gch.nextgc)) {
      if (((uintptr_t)o) <= 1) {
        break;
      }
      dostuff(o);
      count++;
    }
  }

  return count;
}