lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am currently porting the AIX-libperfstat.a interface to lua.

The data returned by libperfstat is organized in C-structs that are repeated.

If your machine has 5 disc-adapters, you have to reserve 5*sizeof(perfstat_diskadapter_t) bytes of storage and the whole memory is filled with data in one call.

Currently I am returning the data to lua as 5 arrays, but i would like to return the data as a two-dimensional array.
My problem is that I do not know how to put the 5 arrays that I have created on the stack in a new array.

This is the syntax that is working for me right now:

#!/usr/bin/env lua
require "perfstat"

adapter1,adapter2,adapter3,adapter4,adapter5 =
perfstat.diskadapters();
print( adapter1["name"] )
print( adapter2["name"] )
print( adapter3["name"] )
print( adapter4["name"] )
print( adapter5["name"] )


returns:

sisscsia0
sisscsia1
fcs2
fcs3
fcs1

This is what I would like to do:

adapters = perfstat.diskadapters();
print(adapters["fcs1"]["destription"])
print(adapters["fcs2"]["destription"])
print(adapters["fcs3"]["destription"])


Here's the relevant part of my C-Code for creating the arrays:

static int l_perfstat_diskadapters (lua_State *L)
{
        int adapters;
        int ret;
        int i;
        perfstat_id_t id;
        perfstat_diskadapter_t * stats;


        adapters = perfstat_diskadapter(NULL,NULL,sizeof(perfstat_diskadapter_t),0);
        if ( adapters == -1)
        {
                luaL_error(L,"No adapters found");
        }

        strcpy(id.name, FIRST_DISKADAPTER);
        stats = malloc(sizeof(perfstat_diskadapter_t) * adapters);
        if ( stats == NULL )
        {
                luaL_error(L,"malloc for perfstat_diskadapter failed");
        }

        ret = perfstat_diskadapter((perfstat_id_t * )&id, stats,sizeof(perfstat_diskadapter_t),adapters);
        if ( ret == -1 )
        {
                luaL_error(L,"perfstat_diskadapter failed");
        }

        for (i = 0; i < ret ; i++)
        {
                lua_newtable(L);

                lua_pushstring(L,"name");
                lua_pushstring(L,stats[i].name);
                lua_rawset(L,-3);

                lua_pushstring(L,"description");
                lua_pushstring(L,stats[i].description);
                lua_rawset(L,-3);

                lua_pushstring(L,"number");
                lua_pushnumber(L,stats[i].number);
                lua_rawset(L,-3);

                lua_pushstring(L,"size");
                lua_pushnumber(L,stats[i].size);
                lua_rawset(L,-3);

                lua_pushstring(L,"free");
                lua_pushnumber(L,stats[i].free);
                lua_rawset(L,-3);

                lua_pushstring(L,"xrate");
                lua_pushnumber(L,stats[i].xrate);
                lua_rawset(L,-3);

                lua_pushstring(L,"xfers");
                lua_pushnumber(L,stats[i].xfers);
                lua_rawset(L,-3);

                lua_pushstring(L,"rblks");
                lua_pushnumber(L,stats[i].rblks);
                lua_rawset(L,-3);

                lua_pushstring(L,"wblks");
                lua_pushnumber(L,stats[i].wblks);
                lua_rawset(L,-3);

                lua_pushstring(L,"time");
                lua_pushnumber(L,stats[i].time);
                lua_rawset(L,-3);
        }

        free ( stats );

        return ret;
}