User space dynamic tracing and feedback control
Tags: c, linux, programming, github
Published: 2023-02-03 16:53:11

## Preface

Based on the latest features of the open source C language library Melon, this article describes the use of the library for user space dynamic tracing, processing the tracing data, and feeding back the results to the program.

When it comes to dynamic tracing, your first impression may be bpf, dtrace, systemtap, etc., but the dynamic tracing introduced in this article does not depend on these. **The functions provided in Melon are more inclined to allow the program to complete its own dynamic tracing in the user space, without relying on the kernel, nor on uprobe or usdt**.

> Regarding the Melon library, this is an open source C language library, which has the advantages of out-of-the-box, no third-party dependencies, simple installation and deployment, and complete documentation.
>
> Github: https://github.com/Water-Melon/Melon

## Implementation principle

The dynamic tracing of the Melon library is implemented by adding tracing points in the program.
In Melon, an application is divided into two layers (but both are run in the same process):

- C code layer
- Melang script code layer

The two code levels can run in the same **thread**, or in different threads, but they need to run in the same process.

In other words: **The tracing point data will be thrown at the C layer and passed to the specified script task, and then the script task will receive the data and process it. **

This seems to be no different from logging in one program and reading the log in another program for processing, so what are the benefits of doing so?

**Advantage**:

- No need to parse the format of the log, you can directly get the corresponding type of data
- Remote transmission or storage can be performed after script side processing (script library function guarantee)
- Even under the same thread, the script task execution will not interrupt the C layer logic for a long time, and the script and C logic are automatically time-sharing scheduling
- Using the feedback API on the script side, the calculation result can be sent back to the C layer, so that the execution logic of the C code layer can be changed according to the result, for example: service degradation

## Example

Let's look at a very simple example:

```c
#include <stdio.h>
#include "mln_log.h"
#include "mln_core.h"
#include "mln_trace.h"
#include "mln_conf.h"
#include "mln_event.h"

int timeout = 100;

static void timeout_handler(mln_event_t *ev, void *data)
{
    mln_trace("sir", "Hello", getpid(), 3.1);
    mln_event_timer_set(ev, timeout, NULL, timeout_handler);
}

static int recv_handler(mln_lang_ctx_t *ctx, mln_lang_val_t *val)
{
    timeout += val->data.i;
    return 0;
}

int main(int argc, char *argv[])
{
    mln_event_t *ev;
    struct mln_core_attr cattr;

    cattr.argc = argc;
    cattr.argv = argv;
    cattr.global_init = NULL;
    cattr.main_thread = NULL;
    cattr.master_process = NULL;
    cattr.worker_process = NULL;

    if (mln_core_init(&cattr) < 0) {
       fprintf(stderr, "Melon init failed.\n");
       return -1;
    }

    if ((ev = mln_event_new()) == NULL) {
        mln_log(error, "event new error\n");
        return -1;
    }

    if (mln_trace_init(ev, mln_trace_path()) < 0) {
        mln_log(error, "trace init error\n");
        return -1;
    }
    mln_trace_recv_handler_set(recv_handler);

    mln_event_timer_set(ev, 1000, NULL, timeout_handler);

    mln_event_dispatch(ev);

    return 0;
}
```

Briefly describe the program flow:

1. Initialization of the Melon library (`mln_core_init`)
2. Initialize the event object
3. Initialize the tracing script
4. Set the function used to process the data sent by the script layer
5. Set the timeout event
6. Event dispatching, timeout event will be triggered

In the timeout processing function `timeout_handler`, we use `mln_trace` to send three different types of data to the script task, and then continue to set the timeout event.

The timeout period is a global variable `timeout`, which is initially 100 milliseconds.

When the script layer sends data, here we agree that the script layer must send an integer, then in the receiving function `recv_handler`, we accumulate this value and `timeout` as the subsequent timeout period.

From this, it can be guessed that the amount of data delivered to the script layer per second in the program will be less and less.

The script layer code is given below:

```
sys = Import('sys');

Pipe('subscribe');
while (1) {
     ret = Pipe('recv');
     if (ret) {
         for (i = 0; i < sys. size(ret); ++i) {
             sys.print(ret[i]);
         }
         Pipe('send', 100);
     } fi
     sys.msleep(1000);
}
Pipe('unsubscribe');
```

A brief description of the logic of the script layer is to receive a batch of data from the C layer every second, and then output it to the terminal, and after the output, send an integer `100` to the C layer.



Let's take a look at the results of running the program:

```
...
[Hello, 72173, 3.100000, ]
[Hello, 72173, 3.100000, ]
[Hello, 72173, 3.100000, ]
...
```

You will see a lot of the above output, but if you run it yourself, you will find that the number of output lines per second will be less and less, which is consistent with our program logic.



## Conclusion

As can be seen from this example, we can not only trace and process on the script side, but also use the processing results to feedback and control the C layer. And doing so has three advantages:

1. There is no need to add additional statistical variables and structures in the C layer
2. The C layer and the script layer are separated on the code management aspect
3. Both levels of code run in the same thread

In order to simplify the demonstration code, the above example does not show network communication and data storage at the script layer, but all these functions are supported by Melang scripts. Interested readers can refer to [Melang official website](https://melang.org/ ).

Finally, readers interested in the Melon library can visit its [Github repository](https://github.com/Water-Melon/Melon) for more details.

Thanks for reading!