The qemuBuildMemoryBackendProps() function is already long enough. Move code that decides what hugepages to use into a separate function. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/qemu/qemu_command.c | 148 +++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 62 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index fb87649e65..858aa0211a 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3603,6 +3603,86 @@ qemuBuildMemoryGetDefaultPagesize(virQEMUDriverConfig *cfg, } +static int +qemuBuildMemoryGetPagesize(virQEMUDriverConfig *cfg, + const virDomainDef *def, + const virDomainMemoryDef *mem, + unsigned long long *pagesizeRet, + bool *needHugepageRet, + bool *useHugepageRet) +{ + const long system_page_size = virGetSystemPageSizeKB(); + unsigned long long pagesize = mem->pagesize; + bool needHugepage = !!pagesize; + bool useHugepage = !!pagesize; + + if (pagesize == 0) { + virDomainHugePage *master_hugepage = NULL; + virDomainHugePage *hugepage = NULL; + bool thisHugepage = false; + size_t i; + + /* Find the huge page size we want to use */ + for (i = 0; i < def->mem.nhugepages; i++) { + hugepage = &def->mem.hugepages[i]; + + if (!hugepage->nodemask) { + master_hugepage = hugepage; + continue; + } + + /* just find the master hugepage in case we don't use NUMA */ + if (mem->targetNode < 0) + continue; + + if (virBitmapGetBit(hugepage->nodemask, mem->targetNode, + &thisHugepage) < 0) { + /* Ignore this error. It's not an error after all. Well, + * the nodemask for this <page/> can contain lower NUMA + * nodes than we are querying in here. */ + continue; + } + + if (thisHugepage) { + /* Hooray, we've found the page size */ + needHugepage = true; + break; + } + } + + if (i == def->mem.nhugepages) { + /* We have not found specific huge page to be used with this + * NUMA node. Use the generic setting then (<page/> without any + * @nodemask) if possible. */ + hugepage = master_hugepage; + } + + if (hugepage) { + pagesize = hugepage->size; + useHugepage = true; + } + } + + if (pagesize == system_page_size) { + /* However, if user specified to use "huge" page + * of regular system page size, it's as if they + * hasn't specified any huge pages at all. */ + pagesize = 0; + needHugepage = false; + useHugepage = false; + } else if (useHugepage && pagesize == 0) { + if (qemuBuildMemoryGetDefaultPagesize(cfg, &pagesize) < 0) + return -1; + } + + *pagesizeRet = pagesize; + *needHugepageRet = needHugepage; + *useHugepageRet = useHugepage; + + return 0; +} + + /** * qemuBuildMemoryBackendProps: * @backendProps: [out] constructed object @@ -3640,18 +3720,16 @@ qemuBuildMemoryBackendProps(virJSONValue **backendProps, { const char *backendType = "memory-backend-file"; virDomainNumatuneMemMode mode; - const long system_page_size = virGetSystemPageSizeKB(); virDomainMemoryAccess memAccess = mem->access; - size_t i; g_autofree char *memPath = NULL; bool prealloc = false; virBitmap *nodemask = NULL; int rc; g_autoptr(virJSONValue) props = NULL; bool nodeSpecified = virDomainNumatuneNodeSpecified(def->numa, mem->targetNode); - unsigned long long pagesize = mem->pagesize; - bool needHugepage = !!pagesize; - bool useHugepage = !!pagesize; + unsigned long long pagesize = 0; + bool needHugepage = false; + bool useHugepage = false; int discard = mem->discard; bool disableCanonicalPath = false; @@ -3696,63 +3774,9 @@ qemuBuildMemoryBackendProps(virJSONValue **backendProps, virDomainNumatuneGetMode(def->numa, -1, &mode) < 0) mode = VIR_DOMAIN_NUMATUNE_MEM_STRICT; - if (pagesize == 0) { - virDomainHugePage *master_hugepage = NULL; - virDomainHugePage *hugepage = NULL; - bool thisHugepage = false; - - /* Find the huge page size we want to use */ - for (i = 0; i < def->mem.nhugepages; i++) { - hugepage = &def->mem.hugepages[i]; - - if (!hugepage->nodemask) { - master_hugepage = hugepage; - continue; - } - - /* just find the master hugepage in case we don't use NUMA */ - if (mem->targetNode < 0) - continue; - - if (virBitmapGetBit(hugepage->nodemask, mem->targetNode, - &thisHugepage) < 0) { - /* Ignore this error. It's not an error after all. Well, - * the nodemask for this <page/> can contain lower NUMA - * nodes than we are querying in here. */ - continue; - } - - if (thisHugepage) { - /* Hooray, we've found the page size */ - needHugepage = true; - break; - } - } - - if (i == def->mem.nhugepages) { - /* We have not found specific huge page to be used with this - * NUMA node. Use the generic setting then (<page/> without any - * @nodemask) if possible. */ - hugepage = master_hugepage; - } - - if (hugepage) { - pagesize = hugepage->size; - useHugepage = true; - } - } - - if (pagesize == system_page_size) { - /* However, if user specified to use "huge" page - * of regular system page size, it's as if they - * hasn't specified any huge pages at all. */ - pagesize = 0; - needHugepage = false; - useHugepage = false; - } else if (useHugepage && pagesize == 0) { - if (qemuBuildMemoryGetDefaultPagesize(cfg, &pagesize) < 0) - return -1; - } + if (qemuBuildMemoryGetPagesize(cfg, def, mem, &pagesize, + &needHugepage, &useHugepage) < 0) + return -1; props = virJSONValueNewObject(); -- 2.34.1