DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Platform Engineering: A Strategic Response to the Growing Complexity of Modern Software Architectures
  • Curating Efficient Distributed Application Runtime (Dapr) Workflows
  • How to Implement Specific Distributed System Patterns Using Spring Boot: Introduction
  • Overcoming the Retry Dilemma in Distributed Systems

Trending

  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  • Unlocking the Benefits of a Private API in AWS API Gateway
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  1. DZone
  2. Coding
  3. Languages
  4. User Space Dynamic Tracing and Feedback Control

User Space Dynamic Tracing and Feedback Control

Learn how to use Melon for surprisingly robust dynamic tracing.

By 
Niklaus Schen user avatar
Niklaus Schen
·
Feb. 10, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
1.9K Views

Join the DZone community and get the full member experience.

Join For Free

This 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:

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:

C
 
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:

  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.

Thanks for reading!

C Language Distributed Computing

Published at DZone with permission of Niklaus Schen. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Platform Engineering: A Strategic Response to the Growing Complexity of Modern Software Architectures
  • Curating Efficient Distributed Application Runtime (Dapr) Workflows
  • How to Implement Specific Distributed System Patterns Using Spring Boot: Introduction
  • Overcoming the Retry Dilemma in Distributed Systems

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!