Skip to content

Commit 130c391

Browse files
authored
Merge pull request #1866 from giuseppe/support-rdt-monitoring
intelrdt: add support for EnableMonitoring
2 parents 99b6cad + dfdcf77 commit 130c391

File tree

7 files changed

+138
-76
lines changed

7 files changed

+138
-76
lines changed

src/libcrun/container.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,17 +1816,17 @@ container_delete_internal (libcrun_context_t *context, runtime_spec_schema_confi
18161816
}
18171817
}
18181818

1819-
if (killall && force)
1819+
if (def == NULL)
18201820
{
1821-
if (def == NULL)
1822-
{
1823-
ret = read_container_config_from_state (&container, state_root, id, err);
1824-
if (UNLIKELY (ret < 0))
1825-
return ret;
1821+
ret = read_container_config_from_state (&container, state_root, id, err);
1822+
if (UNLIKELY (ret < 0))
1823+
return ret;
18261824

1827-
def = container->container_def;
1828-
}
1825+
def = container->container_def;
1826+
}
18291827

1828+
if (killall && force)
1829+
{
18301830
/* If the container has a pid namespace, it is enough to kill the first
18311831
process (pid=1 in the namespace).
18321832
*/
@@ -1853,9 +1853,9 @@ container_delete_internal (libcrun_context_t *context, runtime_spec_schema_confi
18531853
}
18541854
}
18551855

1856-
if (! is_empty_string (status.intelrdt))
1856+
if (def->linux && def->linux->intel_rdt)
18571857
{
1858-
ret = libcrun_destroy_intelrdt (status.intelrdt, err);
1858+
ret = libcrun_destroy_intelrdt (id, def, err);
18591859
if (UNLIKELY (ret < 0))
18601860
crun_error_write_warning_and_release (context->output_handler_arg, &err);
18611861
}
@@ -1932,32 +1932,16 @@ write_container_status (libcrun_container_t *container, libcrun_context_t *conte
19321932
if (UNLIKELY (cwd == NULL))
19331933
libcrun_fail_with_error (errno, "getcwd failed");
19341934
cleanup_free char *owner = get_user_name (geteuid ());
1935-
cleanup_free char *intelrdt = NULL;
19361935
char *external_descriptors = libcrun_get_external_descriptors (container);
19371936
char *rootfs = container->container_def->root ? container->container_def->root->path : "";
19381937
char created[35];
19391938

1940-
if (container_has_intelrdt (container))
1941-
{
1942-
bool explicit = false;
1943-
const char *tmp;
1944-
1945-
tmp = libcrun_get_intelrdt_name (context->id, container, &explicit);
1946-
if (tmp == NULL)
1947-
return crun_make_error (err, 0, "internal error: cannot get intelrdt name");
1948-
/* It is stored in the status only for cleanup purposes. Delete the group only
1949-
if it was not explicitly set. */
1950-
if (! explicit)
1951-
intelrdt = xstrdup (tmp);
1952-
}
1953-
19541939
libcrun_container_status_t status = {
19551940
.pid = pid,
19561941
.rootfs = rootfs,
19571942
.bundle = cwd,
19581943
.created = created,
19591944
.owner = owner,
1960-
.intelrdt = intelrdt,
19611945
.systemd_cgroup = context->systemd_cgroup,
19621946
.detached = context->detach,
19631947
.external_descriptors = external_descriptors,

src/libcrun/intelrdt.c

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define INTEL_RDT_MOUNT_POINT "/sys/fs/resctrl"
3333
#define SCHEMATA_FILE "schemata"
3434
#define TASKS_FILE "tasks"
35+
#define MON_GROUPS "mon_groups"
3536
#define RDTGROUP_SUPER_MAGIC 0x7655821
3637

3738
static int
@@ -243,7 +244,7 @@ intelrdt_clean_l3_cache_schema (const char *l3_cache_schema)
243244
}
244245

245246
int
246-
resctl_create (const char *name, bool explicit_clos_id, bool *created, const char *l3_cache_schema, const char *mem_bw_schema, libcrun_error_t *err)
247+
resctl_create (const char *name, bool explicit_clos_id, bool *created, const char *l3_cache_schema, const char *mem_bw_schema, char *const *schemata, libcrun_error_t *err)
247248
{
248249
cleanup_free char *cleaned_l3_cache_schema = NULL;
249250
cleanup_free char *path = NULL;
@@ -269,9 +270,9 @@ resctl_create (const char *name, bool explicit_clos_id, bool *created, const cha
269270
if (l3_cache_schema && strstr (l3_cache_schema, "MB:"))
270271
l3_cache_schema = cleaned_l3_cache_schema = intelrdt_clean_l3_cache_schema (l3_cache_schema);
271272

272-
/* If the closID was specified and both l3cache and bwSchema are unset, the group
273-
must exist. */
274-
if (explicit_clos_id && l3_cache_schema == NULL && mem_bw_schema == NULL)
273+
/* If the closID was specified and both l3cache and bwSchema are unset, and schemata is empty,
274+
the group must exist. */
275+
if (explicit_clos_id && is_empty_string (l3_cache_schema) && is_empty_string (mem_bw_schema) && (schemata == NULL))
275276
{
276277
if (exist)
277278
return 0;
@@ -293,8 +294,9 @@ resctl_create (const char *name, bool explicit_clos_id, bool *created, const cha
293294
}
294295

295296
int
296-
resctl_move_task_to (const char *name, pid_t pid, libcrun_error_t *err)
297+
resctl_move_task_to (const char *name, const char *monitoring_name, pid_t pid, libcrun_error_t *err)
297298
{
299+
cleanup_free char *monitoring_path = NULL;
298300
cleanup_free char *path = NULL;
299301
char pid_str[32];
300302
int len;
@@ -308,7 +310,22 @@ resctl_move_task_to (const char *name, pid_t pid, libcrun_error_t *err)
308310
if (UNLIKELY (len >= (int) sizeof (pid_str)))
309311
return crun_make_error (err, 0, "internal error: static buffer too small");
310312

311-
return write_file (path, pid_str, len, err);
313+
ret = write_file (path, pid_str, len, err);
314+
if (UNLIKELY (ret < 0))
315+
return ret;
316+
317+
if (monitoring_name)
318+
{
319+
ret = append_paths (&monitoring_path, err, INTEL_RDT_MOUNT_POINT, name, MON_GROUPS, monitoring_name, TASKS_FILE, NULL);
320+
if (UNLIKELY (ret < 0))
321+
return ret;
322+
323+
ret = write_file (monitoring_path, pid_str, len, err);
324+
if (UNLIKELY (ret < 0))
325+
return ret;
326+
}
327+
328+
return 0;
312329
}
313330

314331
int
@@ -342,6 +359,35 @@ resctl_update (const char *name, const char *l3_cache_schema, const char *mem_bw
342359
return write_intelrdt_string (fd, path, actual_l3_cache_schema, mem_bw_schema, schemata, err);
343360
}
344361

362+
static int
363+
resctl_get_monitoring_path (const char *resctrl_group_name, const char *container_id, char **monitoring_path, libcrun_error_t *err)
364+
{
365+
int ret;
366+
367+
ret = append_paths (monitoring_path, err, INTEL_RDT_MOUNT_POINT, resctrl_group_name, MON_GROUPS, container_id, NULL);
368+
if (UNLIKELY (ret < 0))
369+
return ret;
370+
371+
return 0;
372+
}
373+
374+
int
375+
resctl_destroy_monitoring_group (const char *resctrl_group_name, const char *container_id, libcrun_error_t *err)
376+
{
377+
cleanup_free char *monitoring_path = NULL;
378+
int ret;
379+
380+
ret = resctl_get_monitoring_path (resctrl_group_name, container_id, &monitoring_path, err);
381+
if (UNLIKELY (ret < 0))
382+
return ret;
383+
384+
ret = rmdir (monitoring_path);
385+
if (UNLIKELY (ret < 0 && errno != ENOENT))
386+
return crun_make_error (err, errno, "rmdir `%s`", monitoring_path);
387+
388+
return 0;
389+
}
390+
345391
int
346392
resctl_destroy (const char *name, libcrun_error_t *err)
347393
{
@@ -353,8 +399,25 @@ resctl_destroy (const char *name, libcrun_error_t *err)
353399
return ret;
354400

355401
ret = rmdir (path);
356-
if (UNLIKELY (ret < 0))
402+
if (UNLIKELY (ret < 0 && errno != ENOENT))
357403
return crun_make_error (err, errno, "rmdir `%s`", path);
358404

359405
return 0;
360406
}
407+
408+
int
409+
resctl_create_monitoring_group (const char *resctrl_group_name, const char *container_id, libcrun_error_t *err)
410+
{
411+
cleanup_free char *monitoring_path = NULL;
412+
int ret;
413+
414+
ret = resctl_get_monitoring_path (resctrl_group_name, container_id, &monitoring_path, err);
415+
if (UNLIKELY (ret < 0))
416+
return ret;
417+
418+
ret = crun_ensure_directory (monitoring_path, 0755, true, err);
419+
if (UNLIKELY (ret < 0))
420+
return ret;
421+
422+
return 0;
423+
}

src/libcrun/intelrdt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
#include <stdbool.h>
2525
#include "error.h"
2626

27-
int resctl_create (const char *name, bool explicit_clos_id, bool *created, const char *l3_cache_schema, const char *mem_bw_schema, libcrun_error_t *err);
28-
int resctl_move_task_to (const char *name, pid_t pid, libcrun_error_t *err);
27+
int resctl_create (const char *name, bool explicit_clos_id, bool *created, const char *l3_cache_schema, const char *mem_bw_schema, char *const *schemata, libcrun_error_t *err);
28+
int resctl_move_task_to (const char *name, const char *monitoring_name, pid_t pid, libcrun_error_t *err);
2929
int resctl_update (const char *name, const char *l3_cache_schema, const char *mem_bw_schema, char *const *schemata, libcrun_error_t *err);
3030
int resctl_destroy (const char *name, libcrun_error_t *err);
3131

32+
int resctl_destroy_monitoring_group (const char *resctrl_group_name, const char *container_id, libcrun_error_t *err);
33+
int resctl_create_monitoring_group (const char *resctrl_group_name, const char *container_id, libcrun_error_t *err);
34+
3235
#endif

src/libcrun/linux.c

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6083,13 +6083,9 @@ libcrun_kill_linux (libcrun_container_status_t *status, int signal, libcrun_erro
60836083
return 0;
60846084
}
60856085

6086-
const char *
6087-
libcrun_get_intelrdt_name (const char *ctr_name, libcrun_container_t *container, bool *explicit)
6086+
static const char *
6087+
libcrun_get_intelrdt_name (const char *ctr_name, runtime_spec_schema_config_schema *def, bool *explicit)
60886088
{
6089-
runtime_spec_schema_config_schema *def = NULL;
6090-
6091-
def = container->container_def;
6092-
60936089
if (def == NULL || def->linux == NULL || def->linux->intel_rdt == NULL || def->linux->intel_rdt->clos_id == NULL)
60946090
{
60956091
if (explicit)
@@ -6106,6 +6102,7 @@ int
61066102
libcrun_apply_intelrdt (const char *ctr_name, libcrun_container_t *container, pid_t pid, int actions, libcrun_error_t *err)
61076103
{
61086104
runtime_spec_schema_config_schema *def = NULL;
6105+
bool has_monitoring = false;
61096106
bool explicit = false;
61106107
bool created = false;
61116108
const char *name;
@@ -6117,13 +6114,22 @@ libcrun_apply_intelrdt (const char *ctr_name, libcrun_container_t *container, pi
61176114
if (def == NULL || def->linux == NULL || def->linux->intel_rdt == NULL)
61186115
return 0;
61196116

6120-
name = libcrun_get_intelrdt_name (ctr_name, container, &explicit);
6117+
name = libcrun_get_intelrdt_name (ctr_name, def, &explicit);
61216118

61226119
if (actions & LIBCRUN_INTELRDT_CREATE)
61236120
{
6124-
ret = resctl_create (name, explicit, &created, def->linux->intel_rdt->l3cache_schema, def->linux->intel_rdt->mem_bw_schema, err);
6121+
ret = resctl_create (name, explicit, &created, def->linux->intel_rdt->l3cache_schema, def->linux->intel_rdt->mem_bw_schema, def->linux->intel_rdt->schemata, err);
61256122
if (UNLIKELY (ret < 0))
61266123
return ret;
6124+
6125+
if (def->linux->intel_rdt->enable_monitoring)
6126+
{
6127+
ret = resctl_create_monitoring_group (name, ctr_name, err);
6128+
if (UNLIKELY (ret < 0))
6129+
goto fail;
6130+
6131+
has_monitoring = true;
6132+
}
61276133
}
61286134

61296135
if (actions & LIBCRUN_INTELRDT_UPDATE)
@@ -6135,14 +6141,25 @@ libcrun_apply_intelrdt (const char *ctr_name, libcrun_container_t *container, pi
61356141

61366142
if (actions & LIBCRUN_INTELRDT_MOVE)
61376143
{
6138-
ret = resctl_move_task_to (name, pid, err);
6144+
const char *monitoring_name = def->linux->intel_rdt->enable_monitoring ? ctr_name : NULL;
6145+
ret = resctl_move_task_to (name, monitoring_name, pid, err);
61396146
if (UNLIKELY (ret < 0))
61406147
goto fail;
61416148
}
61426149

61436150
return 0;
61446151

61456152
fail:
6153+
if (has_monitoring)
6154+
{
6155+
libcrun_error_t tmp_err = NULL;
6156+
int tmp_ret;
6157+
6158+
tmp_ret = resctl_destroy_monitoring_group (name, ctr_name, &tmp_err);
6159+
if (tmp_ret < 0)
6160+
crun_error_release (&tmp_err);
6161+
}
6162+
61466163
/* Cleanup only if the resctl was created as part of this call. */
61476164
if (created)
61486165
{
@@ -6157,17 +6174,40 @@ libcrun_apply_intelrdt (const char *ctr_name, libcrun_container_t *container, pi
61576174
}
61586175

61596176
int
6160-
libcrun_destroy_intelrdt (const char *name, libcrun_error_t *err)
6177+
libcrun_destroy_intelrdt (const char *container_id, runtime_spec_schema_config_schema *def, libcrun_error_t *err)
61616178
{
6162-
return resctl_destroy (name, err);
6179+
bool explicit = false;
6180+
const char *clos_id = libcrun_get_intelrdt_name (container_id, def, &explicit);
6181+
6182+
if (def && def->linux && def->linux->intel_rdt && def->linux->intel_rdt->enable_monitoring)
6183+
{
6184+
int ret;
6185+
6186+
ret = resctl_destroy_monitoring_group (clos_id, container_id, err);
6187+
if (ret < 0)
6188+
return ret;
6189+
}
6190+
6191+
/* Do not destroy the clos_id if the name was set explicitly. */
6192+
if (explicit)
6193+
return 0;
6194+
6195+
return resctl_destroy (clos_id, err);
61636196
}
61646197

61656198
int
61666199
libcrun_update_intel_rdt (const char *ctr_name, libcrun_container_t *container, const char *l3_cache_schema, const char *mem_bw_schema, char *const *schemata, libcrun_error_t *err)
61676200
{
6201+
runtime_spec_schema_config_schema *def = NULL;
61686202
const char *name;
61696203

6170-
name = libcrun_get_intelrdt_name (ctr_name, container, NULL);
6204+
if (container)
6205+
def = container->container_def;
6206+
6207+
if (def == NULL || def->linux == NULL || def->linux->intel_rdt == NULL)
6208+
return 0;
6209+
6210+
name = libcrun_get_intelrdt_name (ctr_name, def, NULL);
61716211

61726212
return resctl_update (name, l3_cache_schema, mem_bw_schema, schemata, err);
61736213
}

src/libcrun/linux.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,11 @@ enum
127127

128128
#define LIBCRUN_INTELRDT_CREATE_UPDATE_MOVE (LIBCRUN_INTELRDT_CREATE | LIBCRUN_INTELRDT_UPDATE | LIBCRUN_INTELRDT_MOVE)
129129

130-
static inline bool
131-
container_has_intelrdt (libcrun_container_t *container)
132-
{
133-
runtime_spec_schema_config_schema *def = NULL;
134-
135-
def = container->container_def;
136-
137-
return def != NULL && def->linux != NULL && def->linux->intel_rdt != NULL;
138-
}
139-
140-
const char *libcrun_get_intelrdt_name (const char *ctr_name, libcrun_container_t *container, bool *explicit);
141-
142130
int libcrun_apply_intelrdt (const char *ctr_name, libcrun_container_t *container, pid_t pid, int actions, libcrun_error_t *err);
143131

144132
int libcrun_move_network_devices (libcrun_container_t *container, pid_t pid, libcrun_error_t *err);
145133

146-
int libcrun_destroy_intelrdt (const char *name, libcrun_error_t *err);
134+
int libcrun_destroy_intelrdt (const char *container_id, runtime_spec_schema_config_schema *def, libcrun_error_t *err);
147135

148136
int libcrun_update_intel_rdt (const char *ctr_name, libcrun_container_t *container, const char *l3_cache_schema, const char *mem_bw_schema, char *const *schemata, libcrun_error_t *err);
149137

src/libcrun/status.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,6 @@ libcrun_write_container_status (const char *state_root, const char *id, libcrun_
272272
if (UNLIKELY (r != yajl_gen_status_ok))
273273
goto yajl_error;
274274

275-
r = yajl_gen_string (gen, YAJL_STR ("intelrdt"), strlen ("intelrdt"));
276-
if (UNLIKELY (r != yajl_gen_status_ok))
277-
goto yajl_error;
278-
279-
tmp = status->intelrdt ? status->intelrdt : "";
280-
r = yajl_gen_string (gen, YAJL_STR (tmp), strlen (tmp));
281-
if (UNLIKELY (r != yajl_gen_status_ok))
282-
goto yajl_error;
283-
284275
r = yajl_gen_string (gen, YAJL_STR ("rootfs"), strlen ("rootfs"));
285276
if (UNLIKELY (r != yajl_gen_status_ok))
286277
goto yajl_error;
@@ -443,11 +434,6 @@ libcrun_read_container_status (libcrun_container_status_t *status, const char *s
443434
tmp = yajl_tree_get (tree, scope, yajl_t_string);
444435
status->scope = tmp ? xstrdup (YAJL_GET_STRING (tmp)) : NULL;
445436
}
446-
{
447-
const char *intelrdt[] = { "intelrdt", NULL };
448-
tmp = yajl_tree_get (tree, intelrdt, yajl_t_string);
449-
status->intelrdt = tmp ? xstrdup (YAJL_GET_STRING (tmp)) : NULL;
450-
}
451437
{
452438
const char *rootfs[] = { "rootfs", NULL };
453439
tmp = yajl_tree_get (tree, rootfs, yajl_t_string);
@@ -622,7 +608,6 @@ libcrun_free_container_status (libcrun_container_status_t *status)
622608
free (status->external_descriptors);
623609
free (status->created);
624610
free (status->scope);
625-
free (status->intelrdt);
626611
free (status->owner);
627612
}
628613

0 commit comments

Comments
 (0)