@@ -1858,4 +1858,17 @@ SRST
``xen-event-list``
List event channels in the guest
ERST
+
+ {
+ .name = "trim-memory",
+ .args_type = "reserved:l?",
+ .params = "[reserved]",
+ .help = "trim momory",
+ .cmd = hmp_trim_memory,
+ },
+
+SRST
+``trim-memory`` *reserved*
+ try to release free memory and keep reserved bytes of free memory
untrimmed
+ERST
#endif
@@ -181,5 +181,6 @@ void hmp_boot_set(Monitor *mon, const QDict *qdict);
void hmp_info_mtree(Monitor *mon, const QDict *qdict);
void hmp_info_cryptodev(Monitor *mon, const QDict *qdict);
void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
+void hmp_trim_memory(Monitor *mon, const QDict *qdict);
#endif
@@ -460,3 +460,17 @@ void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
monitor_printf(mon, "dtb dumped to %s", filename);
}
#endif
+
+void hmp_trim_memory(Monitor *mon, const QDict *qdict)
+{
+ int64_t reserved;
+ bool has_reserved = qdict_haskey(qdict, "reserved");
+ Error *err = NULL;
+
+ if (has_reserved) {
+ reserved = qdict_get_int(qdict, "reserved");
+ }
+
+ qmp_trim_memory(has_reserved, reserved, &err);
+ hmp_handle_error(mon, err);
+}
@@ -31,6 +31,7 @@
#include "qapi/type-helpers.h"
#include "hw/mem/memory-device.h"
#include "hw/intc/intc.h"
+#include <malloc.h>
NameInfo *qmp_query_name(Error **errp)
{
@@ -161,6 +162,23 @@ void qmp_add_client(const char *protocol, const
char *fdname,
}
}
+void qmp_trim_memory(bool has_reserved, int64_t reserved, Error **errp)
+{
+#if defined(CONFIG_MALLOC_TRIM)
+ if (!has_reserved) {
+ reserved = 1024 * 1024;
+ }
+ if (reserved < 0) {
+ error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
+ "reserved", "a >0 reserved");
+ return;
+ }
+ malloc_trim(reserved);
+#else
+ error_setg(errp, "malloc_trim feature not configured");
+#endif
+}
+
char *qmp_human_monitor_command(const char *command_line, bool
has_cpu_index,
int64_t cpu_index, Error **errp)
{
@@ -605,3 +605,16 @@
{ 'event': 'VFU_CLIENT_HANGUP',
'data': { 'vfu-id': 'str', 'vfu-qom-path': 'str',
'dev-id': 'str', 'dev-qom-path': 'str' } }
+
+##
+# @trim-memory:
+#
+# try to release free memory
+#
+# @reserved: specifies the amount of free space to leave untrimmed.
+# default to 1MB if not specified.
+#
+# Since: 9.0
+##
+{'command': 'trim-memory',
+ 'data': {'*reserved': 'int'} }