1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-25 01:34:11 +03:00

node_memory: Do not fail if there is parameter unsupported

It makes no sense to fail the whole getting command if there is
a parameter unsupported by the kernel. This patch fixes it by
omitting the unsupported parameter for getMemoryParameters.

And for setMemoryParameters, this checks if there is an unsupported
parameter up front of the setting, and just returns failure if not
all parameters are supported.
This commit is contained in:
Osier Yang 2012-11-28 22:11:06 +08:00
parent b7aba48bca
commit ebdbe25a97
2 changed files with 89 additions and 49 deletions

View File

@ -6746,10 +6746,10 @@ error:
* @nparams: pointer to number of memory parameters; input and output * @nparams: pointer to number of memory parameters; input and output
* @flags: extra flags; not used yet, so callers should always pass 0 * @flags: extra flags; not used yet, so callers should always pass 0
* *
* Get all node memory parameters. On input, @nparams gives the size * Get all node memory parameters (parameters unsupported by OS will be
* of the @params array; on output, @nparams gives how many slots were * omitted). On input, @nparams gives the size of the @params array;
* filled with parameter information, which might be less but will * on output, @nparams gives how many slots were filled with parameter
* not exceed the input value. * information, which might be less but will not exceed the input value.
* *
* As a special case, calling with @params as NULL and @nparams as 0 on * As a special case, calling with @params as NULL and @nparams as 0 on
* input will cause @nparams on output to contain the number of parameters * input will cause @nparams on output to contain the number of parameters
@ -6811,7 +6811,8 @@ error:
* value nparams of virDomainGetSchedulerType) * value nparams of virDomainGetSchedulerType)
* @flags: extra flags; not used yet, so callers should always pass 0 * @flags: extra flags; not used yet, so callers should always pass 0
* *
* Change all or a subset of the node memory tunables. * Change all or a subset of the node memory tunables. The function
* fails if not all of the tunables are supported.
* *
* Note that it's not recommended to use this function while the * Note that it's not recommended to use this function while the
* outside tuning program is running (such as ksmtuned under Linux), * outside tuning program is running (such as ksmtuned under Linux),

View File

@ -1058,14 +1058,15 @@ nodeGetCPUBitmap(int *max_id ATTRIBUTE_UNUSED)
#ifdef __linux__ #ifdef __linux__
static int static int
nodeSetMemoryParameterValue(const char *field, nodeSetMemoryParameterValue(virTypedParameterPtr param)
virTypedParameterPtr param)
{ {
char *path = NULL; char *path = NULL;
char *strval = NULL; char *strval = NULL;
int ret = -1; int ret = -1;
int rc = -1; int rc = -1;
char *field = strchr(param->field, '_');
field++;
if (virAsprintf(&path, "%s/%s", if (virAsprintf(&path, "%s/%s",
SYSFS_MEMORY_SHARED_PATH, field) < 0) { SYSFS_MEMORY_SHARED_PATH, field) < 0) {
virReportOOMError(); virReportOOMError();
@ -1080,7 +1081,7 @@ nodeSetMemoryParameterValue(const char *field,
} }
if ((rc = virFileWriteStr(path, strval, 0)) < 0) { if ((rc = virFileWriteStr(path, strval, 0)) < 0) {
virReportSystemError(-rc, _("failed to set %s"), field); virReportSystemError(-rc, _("failed to set %s"), param->field);
goto cleanup; goto cleanup;
} }
@ -1090,6 +1091,38 @@ cleanup:
VIR_FREE(strval); VIR_FREE(strval);
return ret; return ret;
} }
static bool
nodeMemoryParametersIsAllSupported(virTypedParameterPtr params,
int nparams)
{
char *path = NULL;
int i;
for (i = 0; i < nparams; i++) {
virTypedParameterPtr param = &params[i];
char *field = strchr(param->field, '_');
field++;
if (virAsprintf(&path, "%s/%s",
SYSFS_MEMORY_SHARED_PATH, field) < 0) {
virReportOOMError();
return false;
}
if (!virFileExists(path)) {
virReportError(VIR_ERR_OPERATION_INVALID,
_("Parameter '%s' is not supported by "
"this kernel"), param->field);
VIR_FREE(path);
return false;
}
VIR_FREE(path);
}
return true;
}
#endif #endif
int int
@ -1101,8 +1134,8 @@ nodeSetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
virCheckFlags(0, -1); virCheckFlags(0, -1);
#ifdef __linux__ #ifdef __linux__
int ret = 0;
int i; int i;
int rc;
if (virTypedParameterArrayValidate(params, nparams, if (virTypedParameterArrayValidate(params, nparams,
VIR_NODE_MEMORY_SHARED_PAGES_TO_SCAN, VIR_NODE_MEMORY_SHARED_PAGES_TO_SCAN,
@ -1114,34 +1147,18 @@ nodeSetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
NULL) < 0) NULL) < 0)
return -1; return -1;
if (!nodeMemoryParametersIsAllSupported(params, nparams))
return -1;
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
virTypedParameterPtr param = &params[i]; rc = nodeSetMemoryParameterValue(&params[i]);
if (STREQ(param->field, /* Out of memory */
VIR_NODE_MEMORY_SHARED_PAGES_TO_SCAN)) { if (rc == -2)
ret = nodeSetMemoryParameterValue("pages_to_scan", param); return -1;
/* Out of memory */
if (ret == -2)
return -1;
} else if (STREQ(param->field,
VIR_NODE_MEMORY_SHARED_SLEEP_MILLISECS)) {
ret = nodeSetMemoryParameterValue("sleep_millisecs", param);
/* Out of memory */
if (ret == -2)
return -1;
} else if (STREQ(param->field,
VIR_NODE_MEMORY_SHARED_MERGE_ACROSS_NODES)) {
ret = nodeSetMemoryParameterValue("merge_across_nodes", param);
/* Out of memory */
if (ret == -2)
return -1;
}
} }
return ret; return 0;
#else #else
virReportError(VIR_ERR_NO_SUPPORT, "%s", virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("node set memory parameters not implemented" _("node set memory parameters not implemented"
@ -1167,6 +1184,11 @@ nodeGetMemoryParameterValue(const char *field,
goto cleanup; goto cleanup;
} }
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, 1024, &buf) < 0) if (virFileReadAll(path, 1024, &buf) < 0)
goto cleanup; goto cleanup;
@ -1217,6 +1239,7 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
unsigned long long pages_volatile; unsigned long long pages_volatile;
unsigned long long full_scans = 0; unsigned long long full_scans = 0;
int i; int i;
int ret;
if ((*nparams) == 0) { if ((*nparams) == 0) {
*nparams = NODE_MEMORY_PARAMETERS_NUM; *nparams = NODE_MEMORY_PARAMETERS_NUM;
@ -1228,8 +1251,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
switch (i) { switch (i) {
case 0: case 0:
if (nodeGetMemoryParameterValue("pages_to_scan", ret = nodeGetMemoryParameterValue("pages_to_scan", &pages_to_scan);
&pages_to_scan) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_TO_SCAN, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_TO_SCAN,
@ -1239,8 +1264,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 1: case 1:
if (nodeGetMemoryParameterValue("sleep_millisecs", ret = nodeGetMemoryParameterValue("sleep_millisecs", &sleep_millisecs);
&sleep_millisecs) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_SLEEP_MILLISECS, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_SLEEP_MILLISECS,
@ -1250,8 +1277,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 2: case 2:
if (nodeGetMemoryParameterValue("pages_shared", ret = nodeGetMemoryParameterValue("pages_shared", &pages_shared);
&pages_shared) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_SHARED, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_SHARED,
@ -1261,8 +1290,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 3: case 3:
if (nodeGetMemoryParameterValue("pages_sharing", ret = nodeGetMemoryParameterValue("pages_sharing", &pages_sharing);
&pages_sharing) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_SHARING, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_SHARING,
@ -1272,8 +1303,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 4: case 4:
if (nodeGetMemoryParameterValue("pages_unshared", ret = nodeGetMemoryParameterValue("pages_unshared", &pages_unshared);
&pages_unshared) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_UNSHARED, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_UNSHARED,
@ -1283,8 +1316,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 5: case 5:
if (nodeGetMemoryParameterValue("pages_volatile", ret = nodeGetMemoryParameterValue("pages_volatile", &pages_volatile);
&pages_volatile) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_VOLATILE, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_PAGES_VOLATILE,
@ -1294,8 +1329,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 6: case 6:
if (nodeGetMemoryParameterValue("full_scans", ret = nodeGetMemoryParameterValue("full_scans", &full_scans);
&full_scans) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_FULL_SCANS, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_FULL_SCANS,
@ -1305,8 +1342,10 @@ nodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
break; break;
case 7: case 7:
if (nodeGetMemoryParameterValue("merge_across_nodes", ret = nodeGetMemoryParameterValue("merge_across_nodes", &merge_across_nodes);
&merge_across_nodes) < 0) if (ret == -2)
continue;
else if (ret == -1)
return -1; return -1;
if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_MERGE_ACROSS_NODES, if (virTypedParameterAssign(param, VIR_NODE_MEMORY_SHARED_MERGE_ACROSS_NODES,