mem2swap.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:47:18 +0200
changeset 27 81a574d60c15
parent 9 c3fecc82ade6
child 29 7abf6146898e
permissions -rw-r--r--
typo in min2time format string committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 8
diff changeset
     2
 * test/demos/mem2swap.c
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 8
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 * mem2swpapc usage: mem2spap [<MB>]
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
 */
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdio.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <stdlib.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <unistd.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <errno.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#define N 1000
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
#ifndef DEFAULT_MEGS
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
#define DEFAULT_MEGS 64
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
#endif
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
int set_limit (int);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
int main (int argc, char **argv)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
{
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    int i, n = 0;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    24
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    long int *p;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    26
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    long int *p_new;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    char *args[] = { "/usr/bin/free", "-m", NULL };
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    int size = sizeof (*p);
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    30
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    int megs = DEFAULT_MEGS * 1024 * 1024;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    if (argc == 2)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        megs = atoi (argv[1]) * 1024 * 1024;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    printf ("Mem2Swap - Version 1.0\n");
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    if (!set_limit (megs))
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
        printf ("%d MB demanded ", megs / 1024 / 1024);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    else {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        printf ("\tUsage: %s [MEM]\n\n", argv[0]);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        printf ("Report bugs to mbroeker@largo.homelinux.org\n");
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        return EXIT_FAILURE;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    if ((p = malloc (N * size)) == NULL)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        return EXIT_SUCCESS;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    while (1) {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
        for (i = 0; i < N; i++) {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
            p[n * N + i] = size * (n * N + i);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
        }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        if ((p_new = realloc (p, (++n + 1) * N * size)))
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
            p = p_new;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        else
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
            break;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    if (p) {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
        printf ("and %ld MB allocated\n", p[n * N - 1] / 1024 / 1024);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        free (p);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
    printf ("\n\n");
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    execve ("/usr/bin/free", args, NULL);
8
96d16dfe787a We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 3
diff changeset
    66
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    return EXIT_SUCCESS;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
}