aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFengguang Wu <fengguang.wu@intel.com>2012-11-27 20:21:57 +0800
committerFengguang Wu <fengguang.wu@intel.com>2012-11-27 20:29:03 +0800
commitcfe0e261b76859678b7f7a6314c0694c07f3f2bf (patch)
treea0503c09ab846499671af1787333a16edf9e7fc9
parent72237135afc5cc83ea3b1f763fdc7df2274948b8 (diff)
downloadvm-scalability-cfe0e261b76859678b7f7a6314c0694c07f3f2bf.tar.gz
Abhinav's work on usemem_hugepages.c
Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>
-rw-r--r--usemem_hugepages.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/usemem_hugepages.c b/usemem_hugepages.c
new file mode 100644
index 0000000..00e3838
--- /dev/null
+++ b/usemem_hugepages.c
@@ -0,0 +1,51 @@
+/*
+ * code for allocating hugepages using shared memory segment
+ */
+
+#include "usemem_hugepages.h"
+#include <stdio.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+/* Initialize seg_id to 0 */
+int seg_id = 0;
+
+/* Set the shared memory segment to destruction */
+void shm_free(int seg_id)
+{
+ if (seg_id < 0) {
+ perror("Invalid seg_id\n");
+ exit(1);
+ } else
+ shmctl(seg_id, IPC_RMID, NULL);
+}
+
+/* Allocate huge pages using shm segment */
+unsigned long *allocate_hugepage_segment(unsigned long bytes)
+{
+ unsigned long *p;
+
+ if (!bytes) {
+ perror("invalid size\n");
+ exit(1);
+ }
+
+ /* create a shared sys V IPC shared object of size "bytes"*/
+ if ((seg_id = shmget(IPC_PRIVATE, bytes, SHM_HUGETLB | IPC_CREAT | SHM_R |SHM_W)) == -1) {
+ /* Check if the shmget call was successful */
+ fprintf(stderr, "Error creating shared segment: %s \n", strerror(errno));
+ exit(1);
+ } else { /* Attach the shared memory to callers virtual address space */
+ /* Check if the attachment was successful */
+ if ((p = shmat(seg_id, NULL, 0)) == (void *) -1) {
+ fprintf(stderr, "Error attaching shared memory: %s \n", strerror(errno));
+ shm_free(seg_id);
+ exit(1);
+ }
+ }
+
+ return p;
+}