Skip to content

Instantly share code, notes, and snippets.

@drakenclimber
Last active November 8, 2021 22:06
Show Gist options
  • Save drakenclimber/b3a6b4031807b31bef8ec2c98025453f to your computer and use it in GitHub Desktop.
Save drakenclimber/b3a6b4031807b31bef8ec2c98025453f to your computer and use it in GitHub Desktop.
Example of how to use the newly proposed cgroup_cgxget() and cgroup_cgxset() C APIs.
#include <libcgroup.h>
#include <stdlib.h>
#define CGNAME "cgx_example"
#define CONTROLLER "cpu"
#define SETTING "cpu.shares"
#define VALUE "999"
static int other_interesting_calls(void)
{
enum cg_version_t version;
struct cgroup_library_version const *lib_version;
int ret;
ret = cgroup_get_controller_version(CONTROLLER, &version);
if (ret != 0) {
fprintf(stderr, "cgroup_get_controller_version() failed: %d\n",
ret);
goto out;
}
fprintf(stdout, "Controller %s is mounted as cgroup version %d\n\n",
CONTROLLER, version);
lib_version = cgroup_version();
fprintf(stdout, "Currently running libcgroup version %d.%d.%d\n\n",
lib_version->major, lib_version->minor, lib_version->release);
out:
return ret;
}
static int read_setting(struct cgroup **cg, char *setting)
{
struct cgroup_controller *cgc;
char *value = NULL;
int ret;
ret = cgroup_cgxget(cg, CGROUP_V1, false);
if (ret != 0) {
fprintf(stderr, "cgroup_cgxget() failed: %d\n", ret);
goto out;
}
cgc = cgroup_get_controller(*cg, CONTROLLER);
if (cgc == NULL) {
fprintf(stderr, "cgroup_get_controller() failed\n");
ret = 4;
goto out;
}
ret = cgroup_get_value_string(cgc, setting, &value);
if (ret != 0) {
fprintf(stderr, "cgroup_get_value_string() failed: %d\n", ret);
goto out;
}
fprintf(stdout, "cgxget received: %s = %s\n", setting, value);
out:
if (value)
free(value);
return ret;
}
int main(void)
{
struct cgroup_controller *cgc;
struct cgroup *cg;
int ret;
/*----------------------------------------------------------------
* Set up the structures
****************************************************************/
ret = cgroup_init();
if (ret != 0) {
fprintf(stderr, "cgroup_init() failed: %d\n", ret);
return ret;
}
cg = cgroup_new_cgroup(CGNAME);
if (cg == NULL) {
fprintf(stderr, "cgroup_new_cgroup() failed\n");
return 2;
}
cgc = cgroup_add_controller(cg, CONTROLLER);
if (cgc == NULL) {
fprintf(stderr, "cgroup_add_controller() failed\n");
ret = 3;
goto out;
}
ret = cgroup_add_value_string(cgc, SETTING, NULL);
if (ret != 0) {
fprintf(stderr, "cgroup_add_value_string() failed: %d\n", ret);
goto out;
}
ret = other_interesting_calls();
if (ret != 0)
goto out;
/*----------------------------------------------------------------
* Read what's currently in sysfs
****************************************************************/
ret = read_setting(&cg, SETTING);
if (ret != 0)
goto out;
/*----------------------------------------------------------------
* Modify the in-ram copy of the cgroup and then set in sysfs
* via cgxset()
****************************************************************/
cgc = cgroup_get_controller(cg, CONTROLLER);
if (cgc == NULL) {
fprintf(stderr, "cgroup_get_controller() failed\n");
ret = 5;
goto out;
}
ret = cgroup_set_value_string(cgc, SETTING, VALUE);
if (ret != 0) {
fprintf(stderr, "cgroup_set_value_string() failed: %d\n", ret);
goto out;
}
ret = cgroup_cgxset(cg, CGROUP_V1, false);
if (ret != 0) {
fprintf(stderr, "cgroup_cgxset() failed: %d\n", ret);
goto out;
}
/*----------------------------------------------------------------
* Read what's currently in sysfs
****************************************************************/
ret = read_setting(&cg, SETTING);
if (ret != 0)
goto out;
out:
if (cg != NULL)
cgroup_free(&cg);
return ret;
}
@drakenclimber
Copy link
Author

Here's one way you could run the above code:

$ export SRCPATH=/path/to/libcgroupsrc
$ gcc libcgroup-cgxget-example.c -lcgroup -I $SRCPATH/include -L $SRCPATH/src/.libs -o example
$ sudo $SRCPATH/src/tools/cgcreate -g cpu:cgx_example
$ sudo LD_LIBRARY_PATH=$SRCPATH/src/.libs ./example

Controller cpu is mounted as cgroup version 2
Currently running libcgroup version 1.0.42
cgxget received: cpu.shares = 1024
cgxget received: cpu.shares = 993

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment