User Space Dynamic Tracing and Feedback Control
Learn how to use Melon for surprisingly robust dynamic tracing.
Join the DZone community and get the full member experience.
Join For FreeThis article describes the use of a C library named Melon for user space dynamic tracing, processing the tracing data, and feeding back the results to the program itself and other remote programs.
When it comes to dynamic tracing, the 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.
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?
Advantages:
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:
#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:
Initialization of the Melon library (
mln_core_init
)Initialize the event object
Initialize the tracing script
Set the function used to process the data sent by the script layer
Set the timeout event
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 to the terminal. And after the output, it will 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:
There is no need to add additional statistical variables and structures in the C layer
The C layer and the script layer are separated on the code management aspect
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.
Thanks for reading!
Published at DZone with permission of Niklaus Schen. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments