← All posts

Official example 03: cpu-info — read-only/volatile, instances and actions (8 C + 3 ODL)

Grow through practice

No need to finish reading — test now

One line: cpu-info is the “small example with the fullest data-model features”. It is bigger than amx-scheduler (8 C + 3 ODL), but covers read-only/volatile, instances and actions. Prerequisite: build the AMX base environment first, following “AMX / prplOS development environment”; this article only compiles/runs the example.

What it covers

  • read-only/volatile parameters (Usage);
  • multi-instance (CPU[]);
  • read/list/describe actions;
  • events + periodic inform.

Files

odl/cpu_info.odl
odl/cpu_info_definition.odl
odl/cpu_info_defaults.odl
src/cpu_main.c
src/cpu_info.c
src/cpu_stats.c
src/dm_cpu_actions.c
src/dm_cpu_mngt.c
src/dm_cpu_mon_actions.c
src/dm_events.c
src/dm_usage_actions.c

ODL: entry

cpu_info.odl:

%config {
    import-dbg = true;
    definition_file = "${name}_definition.odl";
    defaults_file = "${name}_defaults.odl";
}

import "${name}.so" as "${name}";
include "${definition_file}";
?include "${save_file}":"${defaults_file}";

%define {
    entry-point cpu_info.cpu_main;
}

ODL: the object tree

%define {
    object CPUMonitor {
        %read-only %volatile uint64 Usage {
            on action read call read_usage;
        }
        uint32 Interval = 0;
        bool PeriodicInform = false;
        event "cpu:usage";
        %read-only object CPU[] {
            on action read call cpu_read;
            on action list call cpu_list;
            on action describe call cpu_describe;
        }
    }
}
  • Usage is computed on read (on action read);
  • CPU[] list/describe are actions too;
  • event "cpu:usage" can be subscribed.

C: entry

int _cpu_main(int reason, amxd_dm_t* dm, amxo_parser_t* parser) {
    switch(reason) {
    case 0:     // START
        app.dm = dm;
        app.parser = parser;
        cpu_dm_add_objects();
        break;
    case 1:     // STOP
        app.dm = NULL;
        app.parser = NULL;
        break;
    }
    return 0;
}

START stores dm/parser for later actions.

Compile/test on the AMX base environment

0. Environment

Dependencies: libamxc/libamxd/libamxo/libamxp + amxrt + a ubus backend. Prerequisite: AMX base environment.

1. Build/install

git clone https://gitlab.com/prpl-foundation/components/ambiorix/examples/datamodel/cpu-info.git
cd cpu-info
make
sudo -E make install

The official install creates a cpu_info -> amxrt symlink.

2. Start the bus

sudo ubusd -s /var/run/ubus/ubus.sock &

3. Run

sudo cpu_info
# or amxrt directly (requires -o name=cpu_info)
sudo amxrt -N -o name=cpu_info \
  -u ubus:/var/run/ubus/ubus.sock \
  -B /usr/bin/mods/amxb/mod-amxb-ubus.so \
  /etc/amx/cpu_info/cpu_info.odl

Without -o name=cpu_info, ${name} becomes amxrt, so it looks for amxrt.so and fails with Import file not found (measured).

4. Verify

ubus list
# CPUMonitor
# CPUMonitor.CPU
# CPUMonitor.CPU.1 ... .4

ubus call CPUMonitor _list
ubus call CPUMonitor.CPU.1 _get

Measured (Ubuntu 22.04 / amxrt 2.6.1 / amxb_ubus 3.11.1):

Family=6, ID=0, MHz=2500.002000, Usage=61,
ModelName=Intel(R) Xeon(R) Platinum,
VendorId=GenuineIntel, Model=85

5. Cleanup

sudo pkill -x cpu_info
sudo pkill -x amxrt
sudo pkill -x ubusd

What you learn

  • %read-only / %volatile and on action read;
  • read/list/describe for CPU[];
  • how entry, actions and events fit together.

Limits

Many files (8 C); not the first example. Start with wait_for / amx-scheduler.

Next

greeter_plugin: instances, history, persistence, events.


Official example: cpu-info Series: AMX Official Examples.

Practice

Found this useful?

I write about AI agents × embedded / edge AI. Subscribe, or email me a concrete problem.

Guestbook →Subscribe (RSS)Support

Comments 0

No comments yet — be the first!

No login required; name is optional.