Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shannon): Resource management #34

Open
RingsC opened this issue Nov 27, 2023 · 1 comment
Open

feat(shannon): Resource management #34

RingsC opened this issue Nov 27, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request feature it will be implemented as a new feature

Comments

@RingsC
Copy link
Contributor

RingsC commented Nov 27, 2023

It's a key module to HTAP.
TP workloads and AP workloads dont interfere with each other. AKA resource isolation.

@ShannonBase ShannonBase self-assigned this May 28, 2024
@ShannonBase ShannonBase added enhancement New feature or request feature it will be implemented as a new feature labels May 28, 2024
@ShannonBase
Copy link
Contributor

A demo to show how to use cgroup to manage the compute resource.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define CGROUP_FOLDER "/sys/fs/cgroup/cpu/demo_cgroup"
#define CPU_QUOTA_FILE "/sys/fs/cgroup/cpu/demo_cgroup/cpu.cfs_quota_us"
#define CPU_PERIOD_FILE "/sys/fs/cgroup/cpu/demo_cgroup/cpu.cfs_period_us"
#define TASKS_FILE "/sys/fs/cgroup/cpu/demo_cgroup/tasks"

void write_to_file(const std::string &file_path, const std::string &content) {
    std::ofstream file(file_path);
    if (file.is_open()) {
        file << content;
        file.close();
    } else {
        std::cerr << "Failed to open " << file_path << std::endl;
        exit(EXIT_FAILURE);
    }
}

void setup_cgroup() {
    // Create cgroup directory
    if (system(("mkdir -p " + std::string(CGROUP_FOLDER)).c_str()) != 0) {
        std::cerr << "Failed to create cgroup folder." << std::endl;
        exit(EXIT_FAILURE);
    }

    // Set CPU quota and period
    write_to_file(CPU_QUOTA_FILE, "50000"); // 50ms quota
    write_to_file(CPU_PERIOD_FILE, "100000"); // 100ms period
}

void add_self_to_cgroup() {
    // Get current process ID
    pid_t pid = getpid();

    // Add this process to the cgroup
    write_to_file(TASKS_FILE, std::to_string(pid));
}

int main() {
    setup_cgroup();
    add_self_to_cgroup();

    // Simulate some CPU intensive work
    std::cout << "Starting CPU intensive task..." << std::endl;
    for (int i = 0; i < 1000000000; ++i) {
        // Busy work
        if (i % 100000000 == 0) {
            std::cout << "Working... " << i << std::endl;
        }
    }

    std::cout << "Task completed." << std::endl;
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature it will be implemented as a new feature
Projects
None yet
Development

No branches or pull requests

2 participants