1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
From 7f00006ea65378e7b27049ff3f0eb3fa70e69b09 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Thu, 16 Feb 2023 20:20:54 +0800
Subject: [PATCH 29/53] support isula update when runtime is runc
Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
.../executor/container_cb/execution_extend.c | 1 +
src/daemon/modules/api/runtime_api.h | 1 +
.../modules/runtime/isula/isula_rt_ops.c | 180 ++++++++++++++++--
3 files changed, 161 insertions(+), 21 deletions(-)
diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c
index 67d0845a..2f565d78 100644
--- a/src/daemon/executor/container_cb/execution_extend.c
+++ b/src/daemon/executor/container_cb/execution_extend.c
@@ -1134,6 +1134,7 @@ static int do_update_resources(const container_update_request *request, containe
if (container_is_running(cont->state)) {
params.rootpath = cont->root_path;
params.hostconfig = hostconfig;
+ params.state = cont->state_path;
if (runtime_update(id, cont->runtime, ¶ms)) {
ERROR("Update container %s failed", id);
ret = -1;
diff --git a/src/daemon/modules/api/runtime_api.h b/src/daemon/modules/api/runtime_api.h
index b245ebf9..199c9f4b 100644
--- a/src/daemon/modules/api/runtime_api.h
+++ b/src/daemon/modules/api/runtime_api.h
@@ -169,6 +169,7 @@ typedef struct _rt_attach_params_t {
typedef struct _rt_update_params_t {
const char *rootpath;
const host_config *hostconfig;
+ const char *state;
} rt_update_params_t;
typedef struct _rt_listpids_params_t {
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index 6f2b4f7d..41791388 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -21,6 +21,7 @@
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
+#include <isula_libutils/auto_cleanup.h>
#include <isula_libutils/defs.h>
#include <isula_libutils/isulad_daemon_configs.h>
#include <isula_libutils/json_common.h>
@@ -39,6 +40,7 @@
#include "constants.h"
#include "isula_libutils/shim_client_process_state.h"
#include "isula_libutils/shim_client_runtime_stats.h"
+#include "isula_libutils/shim_client_cgroup_resources.h"
#include "isula_libutils/oci_runtime_state.h"
#include "isulad_config.h"
#include "utils_string.h"
@@ -54,6 +56,9 @@
#define RESIZE_DATA_SIZE 100
#define PID_WAIT_TIME 120
+// file name formats of cgroup resources json
+#define RESOURCE_FNAME_FORMATS "%s/resources.json"
+
// handle string from stderr output.
typedef int(*handle_output_callback_t)(const char *output);
@@ -229,34 +234,27 @@ bool rt_isula_detect(const char *runtime)
static int create_process_json_file(const char *workdir, const shim_client_process_state *p)
{
struct parser_context ctx = { OPT_GEN_SIMPLIFY, 0 };
- parser_error perr = NULL;
- char *data = NULL;
+ __isula_auto_free parser_error perr = NULL;
+ __isula_auto_free char *data = NULL;
char fname[PATH_MAX] = { 0 };
- int retcode = 0;
if (snprintf(fname, sizeof(fname), "%s/process.json", workdir) < 0) {
- ERROR("failed make process.json full path");
+ ERROR("Failed make process.json full path");
return -1;
}
data = shim_client_process_state_generate_json(p, &ctx, &perr);
if (data == NULL) {
- retcode = -1;
- ERROR("failed generate json for process.json error=%s", perr);
- goto out;
+ ERROR("Failed generate json for process.json error=%s", perr);
+ return -1;
}
if (util_write_file(fname, data, strlen(data), DEFAULT_SECURE_FILE_MODE) != 0) {
- retcode = -1;
- ERROR("failed write process.json");
- goto out;
+ ERROR("Failed write process.json");
+ return -1;
}
-out:
- UTIL_FREE_AND_SET_NULL(perr);
- UTIL_FREE_AND_SET_NULL(data);
-
- return retcode;
+ return 0;
}
static void get_runtime_cmd(const char *runtime, const char **cmd)
@@ -733,18 +731,18 @@ static int shim_create(bool fg, const char *id, const char *workdir, const char
runtime_exec_param_dump(params);
if (snprintf(fpid, sizeof(fpid), "%s/shim-pid", workdir) < 0) {
- ERROR("failed make shim-pid full path");
+ ERROR("Failed make shim-pid full path");
return -1;
}
if (pipe2(exec_fd, O_CLOEXEC) != 0) {
- ERROR("failed to create pipe for shim create");
+ ERROR("Failed to create pipe for shim create");
return -1;
}
pid = fork();
if (pid < 0) {
- ERROR("failed fork for shim parent %s", strerror(errno));
+ ERROR("Failed fork for shim parent %s", strerror(errno));
close(exec_fd[0]);
close(exec_fd[1]);
return -1;
@@ -1264,13 +1262,153 @@ int rt_isula_attach(const char *id, const char *runtime, const rt_attach_params_
return -1;
}
-int rt_isula_update(const char *id, const char *runtime, const rt_update_params_t *params)
+static int to_engine_resources(const host_config *hostconfig, shim_client_cgroup_resources *cr)
+{
+ uint64_t period = 0;
+ int64_t quota = 0;
+
+ if (hostconfig == NULL || cr == NULL) {
+ return -1;
+ }
+
+ cr->block_io = util_common_calloc_s(sizeof(shim_client_cgroup_resources_block_io));
+ if (cr->block_io == NULL) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ cr->cpu = util_common_calloc_s(sizeof(shim_client_cgroup_resources_cpu));
+ if (cr->cpu == NULL) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ cr->memory = util_common_calloc_s(sizeof(shim_client_cgroup_resources_memory));
+ if (cr->memory == NULL) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ cr->block_io->weight = hostconfig->blkio_weight;
+ cr->cpu->shares = (uint64_t)hostconfig->cpu_shares;
+ cr->cpu->period = (uint64_t)hostconfig->cpu_period;
+ cr->cpu->quota = hostconfig->cpu_quota;
+ cr->cpu->cpus = util_strdup_s(hostconfig->cpuset_cpus);
+ cr->cpu->mems = util_strdup_s(hostconfig->cpuset_mems);
+ cr->memory->limit = (uint64_t)hostconfig->memory;
+ cr->memory->swap = (uint64_t)hostconfig->memory_swap;
+ cr->memory->reservation = (uint64_t)hostconfig->memory_reservation;
+ cr->memory->kernel = (uint64_t)hostconfig->kernel_memory;
+ cr->cpu->realtime_period = hostconfig->cpu_realtime_period;
+ cr->cpu->realtime_runtime = hostconfig->cpu_realtime_runtime;
+
+ // when --cpus=n is set, nano_cpus = n * 1e9.
+ if (hostconfig->nano_cpus > 0) {
+ // in the case, period will be set to the default value of 100000(0.1s).
+ period = (uint64_t)(100 * Time_Milli / Time_Micro);
+ // set quota = period * n, in order to let container process fully occupy n cpus.
+ if ((hostconfig->nano_cpus / 1e9) > (INT64_MAX / (int64_t)period)) {
+ ERROR("Overflow of quota");
+ return -1;
+ }
+ quota = hostconfig->nano_cpus / 1e9 * (int64_t)period;
+ cr->cpu->period = period;
+ cr->cpu->quota = quota;
+ }
+
+ return 0;
+}
+
+static int create_resources_json_file(const char *workdir, const shim_client_cgroup_resources *cr, char *fname,
+ size_t fname_size)
{
- ERROR("isula update not support on isulad-shim");
- isulad_set_error_message("isula update not support on isulad-shim");
+ struct parser_context ctx = { OPT_GEN_SIMPLIFY, 0 };
+ __isula_auto_free parser_error perr = NULL;
+ __isula_auto_free char *data = NULL;
+
+ if (snprintf(fname, fname_size, RESOURCE_FNAME_FORMATS, workdir) < 0) {
+ ERROR("Failed make resources.json full path");
+ return -1;
+ }
+
+ data = shim_client_cgroup_resources_generate_json(cr, &ctx, &perr);
+ if (data == NULL) {
+ return -1;
+ }
+
+ if (util_write_file(fname, data, strlen(data), DEFAULT_SECURE_FILE_MODE) != 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+// show std error msg, always return -1.
+static int show_stderr(const char *err)
+{
+ isulad_set_error_message(err);
return -1;
}
+int rt_isula_update(const char *id, const char *runtime, const rt_update_params_t *params)
+{
+ int ret = 0;
+ char workdir[PATH_MAX] = { 0 };
+ char resources_fname[PATH_MAX] = { 0 };
+ const char *opts[2] = { 0 };
+ shim_client_cgroup_resources *cr = NULL;
+
+ if (id == NULL || runtime == NULL || params == NULL) {
+ ERROR("Nullptr arguments not allowed");
+ return -1;
+ }
+
+ ret = snprintf(workdir, sizeof(workdir), "%s/%s/update", params->state, id);
+ if (ret < 0) {
+ ERROR("Failed join update full path");
+ return ret;
+ }
+
+ ret = util_mkdir_p(workdir, DEFAULT_SECURE_DIRECTORY_MODE);
+ if (ret < 0) {
+ ERROR("Failed mkdir update workdir %s", workdir);
+ return ret;
+ }
+
+ cr = util_common_calloc_s(sizeof(shim_client_cgroup_resources));
+ if (cr == NULL) {
+ ERROR("Out of memory");
+ goto del_out;
+ }
+
+ ret = to_engine_resources(params->hostconfig, cr);
+ if (ret < 0) {
+ ERROR("Failed to get resources for update");
+ goto del_out;
+ }
+
+ ret = create_resources_json_file(workdir, cr, resources_fname, sizeof(resources_fname));
+ if (ret != 0) {
+ ERROR("%s: failed create update json file", id);
+ goto del_out;
+ }
+
+ opts[0] = "--resources";
+ opts[1] = resources_fname;
+
+ if (runtime_call_simple(workdir, runtime, "update", opts, 2, id, show_stderr) != 0) {
+ ERROR("Call runtime update id failed");
+ ret = -1;
+ }
+
+del_out:
+ if (util_recursive_rmdir(workdir, 0)) {
+ ERROR("Rmdir %s failed", workdir);
+ }
+ free_shim_client_cgroup_resources(cr);
+ return ret;
+}
+
int rt_isula_pause(const char *id, const char *runtime, const rt_pause_params_t *params)
{
char workdir[PATH_MAX] = { 0 };
--
2.25.1
|