changeset 0:ba8d78ef46a9 default tip

first commit
author e165745 <e165745@ie.u-ryukyu.ac.jp>
date Tue, 13 Feb 2018 15:01:08 +0900
parents
children
files malloc_test.c mem.perl mlock_test.c
diffstat 3 files changed, 141 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/malloc_test.c	Tue Feb 13 15:01:08 2018 +0900
@@ -0,0 +1,102 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+
+
+
+long MEMORY   = 16000*1000*100;  // test memory limit
+long ALLOCATED   = 0;
+long COUNT   = 80000;   // repeat count
+long ACTIVE  = 10000;   // number of memroy segment
+long MAXSIZE = 400000;  // 
+long MINSIZE = 400;
+long MIN_UNIT = 1 ;
+
+typedef
+struct mem_list {
+        void *address;
+        long size;
+        struct mem_list *next;
+} MEM_LIST, *MEM_LIST_PTR;
+
+
+void
+print_mem_list(MEM_LIST_PTR m)
+{
+    MEM_LIST_PTR n;
+    if (!m) return;
+    n = m->next;
+    for(;n&&n!=m;n=n->next) {
+        if (n->address && ! (((long)n->address) & 0x700000000000))
+            printf("0x%08lx 0x%08lx\n",(long)n->address,n->size);
+    }
+}
+
+void
+die(char *msg)
+{
+    fprintf(stderr,"%s\n",msg);
+    exit(0);
+}
+
+void
+option(long ac, char *av[])
+{
+    long i = 1;
+    while(ac>1) {
+        if (av[i][0] == '-') {
+            switch (av[i][1]) {
+            case 'c': COUNT = atol(av[++i]); break;
+            case 'a': ACTIVE = atol(av[++i]); break;
+            case 'u': MIN_UNIT = atol(av[++i]); break;
+            case 'm': MAXSIZE = atol(av[++i]); break;
+            case 'l': MINSIZE = atol(av[++i]); break;
+            case 'M': MEMORY = atol(av[++i]); break;
+            }
+        }
+        ac--; i++;
+    }
+}
+
+int
+main(int ac, char *av[])
+{
+    MEM_LIST mlist;
+    MEM_LIST_PTR last = &mlist;
+    MEM_LIST_PTR new;
+    long i,size;
+
+    option(ac,av);
+
+    mlist.address = NULL;
+    mlist.size = 0;
+    for(i=0;i<ACTIVE;i++) {
+        new = (MEM_LIST_PTR)malloc(sizeof(MEM_LIST)); 
+        if (!new) die("malloc error");
+        new->address = NULL;
+        new->next = NULL;
+        last->next = new;
+        last = new;
+    }
+    last->next = &mlist;
+
+    for(i=0;i++<COUNT;last=last->next) {
+        size = ((random()%(MAXSIZE-MINSIZE))+MINSIZE)*MIN_UNIT;
+        if (last->address) {
+            ALLOCATED -= last->size;
+            last->size = 0;
+            free(last->address);
+            last->address = 0;
+        }
+        if ( ALLOCATED + size > MEMORY) continue;
+        last->size = size;
+        last->address = (void *)malloc(size);
+        memset(last->address, random(), size);
+        ALLOCATED += size;
+        if (!last->address) die("malloc error");
+    }
+    print_mem_list(&mlist);
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mem.perl	Tue Feb 13 15:01:08 2018 +0900
@@ -0,0 +1,8 @@
+#!/usr/local/bin/perl
+     
+my @h = ();
+
+while(1){
+  #sleep 1;
+  push(@h, 1024 * 1024 * 100 );
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mlock_test.c	Tue Feb 13 15:01:08 2018 +0900
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#define K 1024
+#define SIZE 1024*100
+main(int ac,char *av[]) 
+{
+    int i,j,count;
+    char *pool = (char *)malloc(SIZE*K);
+    int flag = atoi(av[1]);
+    count = 0;
+    j = 0;
+    if (flag) {
+    if (mlock((char *)((int)main ), 4096)) {
+      perror("main mlock");
+	exit(1);
+    }
+    if (mlock((char *)((int)pool ), SIZE*K)) {
+      perror("pool mlock");
+	exit(1);
+    }
+    }
+
+    for(;;) {	
+	for(i = 0; i < SIZE*K; i++) {
+	    pool[i] = j++;
+	}
+	fprintf(stderr,"loop %5d\n",count++);
+    }
+}