szaydel
2/16/2017 - 1:10 PM

Memory lockup and hoarding utilities for system robustness testing

Memory lockup and hoarding utilities for system robustness testing

#include <inttypes.h>
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

const int SLEEP = 1000000 ; /* how long do we want this waiting? */
int allocate = 50 ; /* set 50 mb aside for this */

int main( int argc, char **argv )
{
    if ((argv[1] != NULL) && (strlen(argv[1]) > 0)) {
        intmax_t num = strtoimax(argv[1], NULL, 10);
        if (num == INTMAX_MAX && errno == ERANGE) {
            printf("ERROR: Failed to parse\n"); return 1 ;
        } else {
            if (num > 0) {
                allocate = (int)num ;
            }
        }
    }
    printf("Locking: %d MBytes\n", allocate) ;
    /* mmap `allocate` megabytes of shared anonymous memory */
    char *p = mmap(NULL, allocate << 20, PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_SHARED, -1, 0);
 
    /* Make pages resident */
    for (int i = 0; i < (allocate << 20) / 4096; i++) {
        p[i * 4096] = 1;
    }
    
    /* Now we can take our time and examine it. */
    sleep(SLEEP);
 
    return 0;
}
/*
 * name: leakheap.c
 * CC=/opt/gcc-5.1.0/bin gcc -m64 -o leakheap -Wall leakheap.c
 */
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int LOOPCNT   = 100 ;
const int PGSIZE    = 4096 ;
const int SLEEPTIME = 3600 ;
const int PPERLOOP  = 256 ; /* 4K x PPERLOOP == KBytes leaked */

int main( int argc, char *argv[] ) {
    int loopcnt = LOOPCNT ;

    if (argc > 2) {
        fputs("ERROR: Too Many Arguments; at most 1 optional argument expected.\n", 
            stderr);
        return 1 ;
    }
    if ((argc == 2) && (strlen(argv[1]) > 0)) {
        intmax_t num = strtoimax(argv[1], NULL, 10);
        if ((num == INTMAX_MAX && errno == ERANGE) || (num == 0)) {
            fputs("ERROR: Failed to parse numeric argument!\n", stderr); return 1 ;
        } else {
            if (num > 0) {
                loopcnt = (int)num ;
            }
        }
    }
    size_t consumed = 0;
    for ( int i = 0 ; i < loopcnt ; i++ ) {
        /* Fill with random data */
        void *ptr = malloc(sizeof(uint8_t) * PGSIZE*PPERLOOP);
        if (ptr == NULL) {
            fprintf(stderr, "Failed to malloc: %lu(KB)\n", 
            (sizeof(uint8_t) * PGSIZE*PPERLOOP) >> 10);
        }
        arc4random_buf((void *)ptr, PGSIZE*PPERLOOP) ;

        consumed += (sizeof(uint8_t) * PGSIZE*PPERLOOP) ;
    }
    if ((consumed >> 20) < 100) {
        printf("Consumed: %lu(KB)\n", consumed >> 10) ;
    } else {
        printf("Consumed: %lu(MB)\n", consumed >> 20) ;
    }
        
    sleep(SLEEPTIME);
    return 0;
}
package main

import (
	"fmt"
	"log"
	"os"
	"strconv"
	"time"
)

func main() {
	if len(os.Args) != 3 || os.Args[1] == "-h" {
		fmt.Println("Usage: memtest /path/to/huge/file #_of_min_to_sleep")
		os.Exit(1)
	}

	if fileInfo, err := os.Stat(os.Args[1]); err != nil {
		log.Fatal(err)
	} else {
		// Open file for reading
		file, err := os.Open(os.Args[1])
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		needToAlign := fileInfo.Size() % 4096
		numBytesToLock := (4096 - needToAlign) + fileInfo.Size()
		byteSlice := make([]byte, numBytesToLock)

		bytesRead, err := file.Read(byteSlice)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Number of bytes read: %d\n", bytesRead)

		t, err := strconv.ParseInt(os.Args[2], 10, 64)
		if err != nil {
			log.Fatalf("Invalid sleep time.")
		}

		// wait for X minutes before exit
		time.Sleep(time.Minute * time.Duration(t))
	}
}