Skip to content

Commit 6f7e65c

Browse files
committed
[WIP] just add basic plumbing around
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
1 parent 9a21680 commit 6f7e65c

6 files changed

Lines changed: 137 additions & 1 deletion

File tree

Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ libcrun_SOURCES = src/libcrun/utils.c \
6767
src/libcrun/linux.c \
6868
src/libcrun/mount_flags.c \
6969
src/libcrun/scheduler.c \
70+
src/libcrun/mempolicy.c \
7071
src/libcrun/seccomp.c \
7172
src/libcrun/seccomp_notify.c \
7273
src/libcrun/signals.c \
@@ -160,7 +161,7 @@ EXTRA_DIST = COPYING COPYING.libcrun README.md NEWS SECURITY.md rpm/crun.spec au
160161
src/libcrun/custom-handler.h src/libcrun/io_priority.h \
161162
src/libcrun/handlers/handler-utils.h \
162163
src/libcrun/linux.h src/libcrun/utils.h src/libcrun/error.h src/libcrun/criu.h \
163-
src/libcrun/scheduler.h src/libcrun/status.h src/libcrun/terminal.h \
164+
src/libcrun/scheduler.h src/libcrun/mempolicy.h src/libcrun/status.h src/libcrun/terminal.h \
164165
src/libcrun/mount_flags.h src/libcrun/intelrdt.h src/libcrun/ring_buffer.h src/libcrun/string_map.h \
165166
src/libcrun/net_device.h \
166167
crun.1.md crun.1 libcrun.lds \
@@ -225,6 +226,7 @@ PYTHON_TESTS = tests/test_capabilities.py \
225226
tests/test_hostname.py \
226227
tests/test_limits.py \
227228
tests/test_oci_features.py \
229+
tests/test_mempolicy.py \
228230
tests/test_mounts.py \
229231
tests/test_paths.py \
230232
tests/test_pid.py \

src/libcrun/container.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "container.h"
2424
#include "utils.h"
2525
#include "seccomp.h"
26+
#include "mempolicy.h"
2627
#ifdef HAVE_SECCOMP
2728
# include <seccomp.h>
2829
#endif
@@ -3944,6 +3945,14 @@ libcrun_container_exec_with_options (libcrun_context_t *context, const char *id,
39443945
if (UNLIKELY (ret < 0))
39453946
return ret;
39463947

3948+
/* set_mempolicy(2) config is inherited by forks, execv etc. */
3949+
if (container->container_def->linux && container->container_def->linux->memory_policy)
3950+
{
3951+
ret = libcrun_set_mempolicy (container->container_def->linux->memory_policy, err);
3952+
if (UNLIKELY (ret < 0))
3953+
return ret;
3954+
}
3955+
39473956
ret = pipe2 (container_ret_status, O_CLOEXEC);
39483957
if (UNLIKELY (ret < 0))
39493958
return crun_make_error (err, errno, "pipe");

src/libcrun/linux.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "status.h"
5151
#include "criu.h"
5252
#include "scheduler.h"
53+
#include "mempolicy.h"
5354
#include "intelrdt.h"
5455
#include "io_priority.h"
5556
#include "net_device.h"

src/libcrun/mempolicy.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* crun - OCI runtime written in C
3+
*
4+
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
5+
* crun is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation; either version 2.1 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* crun is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with crun. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <config.h>
20+
#include "linux.h"
21+
#include "utils.h"
22+
#include <ocispec/runtime_spec_schema_config_schema.h>
23+
24+
#ifdef HAVE_NUMA
25+
#include <numa.h>
26+
#include <numaif.h>
27+
#endif
28+
29+
int
30+
libcrun_set_mempolicy (runtime_spec_schema_config_linux_memory_policy *mempolicy, libcrun_error_t *err)
31+
{
32+
#ifdef HAVE_NUMA
33+
// something here
34+
#endif
35+
return 0;
36+
}

src/libcrun/mempolicy.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* crun - OCI runtime written in C
3+
*
4+
* Copyright (C) 2017, 2018, 2019, 2021 Giuseppe Scrivano <giuseppe@scrivano.org>
5+
* crun is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation; either version 2.1 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* crun is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with crun. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#ifndef MEMPOLICY_H
19+
#define MEMPOLICY_H
20+
#include <config.h>
21+
#include "error.h"
22+
#include "container.h"
23+
#include "status.h"
24+
25+
int libcrun_set_mempolicy (runtime_spec_schema_config_linux_memory_policy *mempolicy, libcrun_error_t *err);
26+
27+
#endif

tests/test_mempolicy.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/env python3
2+
# crun - OCI runtime written in C
3+
#
4+
# Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
5+
# crun is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# crun is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with crun. If not, see <http://www.gnu.org/licenses/>.
17+
18+
import subprocess
19+
import sys
20+
import json
21+
import os
22+
from tests_utils import *
23+
24+
def check_mempolicy_prerequisites():
25+
"""Check all prerequisites for numa mempolicy tests. Returns 77 (skip) if not met, 0 if OK"""
26+
# TODO: stub entry for now.
27+
# there is no numa python bindings packaged for most distros.
28+
# the only way would be to call numactl shell and parse the output.
29+
return 0
30+
31+
def test_mempolicy():
32+
"""Test numa mempolicy"""
33+
ret = check_mempolicy_prerequisites()
34+
if ret != 0:
35+
return ret
36+
37+
conf = base_config()
38+
conf['process']['args'] = ['/init', 'pause']
39+
add_all_namespaces(conf)
40+
conf['linux']['memoryPolicy'] = {}
41+
42+
cid = None
43+
try:
44+
_, cid = run_and_get_output(conf, command='run', detach=True)
45+
out = run_crun_command(["exec", cid, "/init", "cat", "/proc/self/numa_maps"])
46+
print(out)
47+
except Exception as e:
48+
sys.stderr.write("# Test failed with exception: %s\n" % str(e))
49+
return -1
50+
finally:
51+
if cid is not None:
52+
run_crun_command(["delete", "-f", cid])
53+
54+
return 0
55+
56+
all_tests = {
57+
"mempolicy": test_mempolicy,
58+
}
59+
60+
if __name__ == "__main__":
61+
tests_main(all_tests)

0 commit comments

Comments
 (0)