Skip to contentSkip to conversation

example of userspace application register it memory in ksm

RFC PATCH In review Izik Eidus 1 participant 1 message Jan 21, 2008

Subsystems (2)
lore
Share & tools
lore

Izik Eidus

author
#0 lore raw

RFC PATCH 5/5 example of userspace application register it memory in ksm

(this is the modification for qemu to make it work with ksm)

Signature · 1 line
woof.
Quoted 1 line

From 813266c7d59acb64c104448c255cc2c9d4f0187e Mon Sep 17 00:00:00 2001

From: Izik Eidus <izike-atKUWr5tajBWk0Htik3J/[email protected]>
Date: Mon, 21 Jan 2008 17:41:09 +0200
Subject: [PATCH] qemu: register the memory of qemu with ksm,
so it can be shared

---
 qemu/vl.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
qemu/vl.c +35 -0
index 756e13d..691db36 100644
@@ -21,6 +21,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+
+#include "ksm.h"
 #include "hw/hw.h"
 #include "hw/boards.h"
 #include "hw/usb.h"
@@ -8501,6 +8503,37 @@ void qemu_get_launch_info(int *argc, char ***argv, int *opt_daemonize, const cha
     *opt_incoming = incoming;
 }
 
+int ksm_register_memory(void)
+{
+    int fd;
+    int ksm_fd;
+    int r = 1;
+    struct ksm_memory_region ksm_region;
+
+    fd = open("/dev/ksm", O_RDWR | O_TRUNC, (mode_t)0600);
+    if (fd == -1)
+        goto out;
+
+    ksm_fd = ioctl(fd, KSM_CREATE_SHARED_MEMORY_AREA);
+    if (ksm_fd == -1)
+        goto out_free;
+
+    ksm_region.npages = phys_ram_size / TARGET_PAGE_SIZE;
+    ksm_region.addr = phys_ram_base;
+    r = ioctl(ksm_fd, KSM_REGISTER_MEMORY_REGION, &ksm_region);
+    if (r)
+        goto out_free1;
+
+    return r;
+
+out_free1:
+    close(ksm_fd);
+out_free:
+    close(fd);
+out:
+    return r;
+}
+
 int main(int argc, char **argv)
 {
 #ifdef CONFIG_GDBSTUB
@@ -9353,6 +9386,8 @@ int main(int argc, char **argv)
     }
 #endif
 
+    ksm_register_memory();
+
     bdrv_init();
 
     /* we always create the cdrom drive, even if no disk is there */
Signature · 1 line
1.5.3.6

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel