tmpfs & mmap oddity on Solaris 2.2 and 2.3

tmpfs & mmap oddity on Solaris 2.2 and 2.3

Post by Ian Kemmi » Thu, 25 Nov 1993 05:36:44



There seems to be something very odd in the handling of mmap'd files
which happen to reside on tmpfs filesystems.

I use temporary files to build up display lists which may be of
arbitrary size, and which periodically get emptied out again.

My first strategy was to create a file, make it 128Mb big, and map it.
On a ufs filesystem, everything works as it should - disc space is
only used up as I touch pages in the mapped address range, and I can
clear out the display list by truncating the file to 0 bytes, then
back up to 128Mb.

On tmpfs, two very strange things happen:

1) the pages appear to be allocated immediately I truncate the file
up, rather than when I first touch them.

2) I can't then truncate it back to zero bytes.

See the short program, blimey.c below.  The current directory was on a
ufs filesystem with about 1.4Mb free, and I can happily truncate a
file up to 100,000,000 bytes.  /tmp was on a tmpfs filesystem with
about 55Mb free.

This example was run on Solaris 2.3.  I'm told the same thing happens
on 2.2, and at least part of the problem occurs on 2.1.

----------------------------------------------------------------------
Script started on Tue Nov 23 11:37:57 1993
ipc% cat blimey.c

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/param.h>

#include <stdio.h>
#include <fcntl.h>

main (int argc, char **argv)
{
    int                 fd;
    caddr_t             val;
    char                cmd[MAXPATHLEN + 16];
    int                 bytes;
    int                 j;

    if (argc != 3) {
        fprintf (stderr, "Usage: blimey filename bytes\n");
        exit (1);
    }

    bytes = atoi (argv[2]);
    if (bytes == 0) {
        fprintf (stderr, "Usage: blimey filename bytes\n");
        exit (1);
    }

    fd = open (argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);
    if (fd < 0) {
        perror ("open");
        exit (1);
    }

    printf ("Opened %s OK\n", argv[1]);
    sprintf (cmd, "ls -l %s", argv[1]);
    printf (" %% %s\n", cmd);
    system (cmd);
    printf ("\n");

    if (ftruncate (fd, bytes) < 0) {
        perror ("ftruncate");
        exit (1);
    }

    printf ("Truncated %s OK\n", argv[1]);
    system (cmd);
    printf ("\n");

    val = mmap (0, bytes,
                PROT_READ | PROT_WRITE,
                MAP_SHARED,
                fd, 0);
    if (val == (caddr_t) -1) {
        perror ("mmap");
        exit (1);
    }

    printf ("Mapped %s OK\n", argv[1]);
    system (cmd);
    printf ("\n");

    for (j = 0; j < 100; j++) {
        ((char *) val)[rand() % bytes] = 0;
    }

    if (ftruncate (fd, 0) < 0) {
        perror ("ftruncate");
        exit (1);
    }

    printf ("Truncated %s OK\n", argv[1]);
    system (cmd);
    printf ("\n");

    close (fd);

    if (unlink (argv[1]) < 0) {
        perror ("unlink");
        exit (1);
    }

    printf ("Unlinked %s OK\n", argv[1]);
    system (cmd);
    printf ("\n");

    exit (0);

Quote:}

ipc% blimey foo 10000
Opened foo OK
 % ls -l foo
-rw-r--r--   1 ian      staff          0 Nov 23 11:38 foo

Truncated foo OK
-rw-r--r--   1 ian      staff      10000 Nov 23 11:38 foo

Mapped foo OK
-rw-r--r--   1 ian      staff      10000 Nov 23 11:38 foo

Truncated foo OK
-rw-r--r--   1 ian      staff          0 Nov 23 11:38 foo

Unlinked foo OK
foo: No such file or directory

ipc% blimey /tmp/foo 10000
Opened /tmp/foo OK
 % ls -l /tmp/foo
-rw-r--r--   1 ian      staff          0 Nov 23 11:38 /tmp/foo

Truncated /tmp/foo OK
-rw-r--r--   1 ian      staff      10000 Nov 23 11:38 /tmp/foo

Mapped /tmp/foo OK
-rw-r--r--   1 ian      staff      10000 Nov 23 11:38 /tmp/foo

ftruncate: Device busy
ipc% blimey foo 100000000
Opened foo OK
 % ls -l foo
-rw-r--r--   1 ian      staff          0 Nov 23 11:38 foo

Truncated foo OK
-rw-r--r--   1 ian      staff    100000000 Nov 23 11:38 foo

Mapped foo OK
-rw-r--r--   1 ian      staff    100000000 Nov 23 11:38 foo

Truncated foo OK
-rw-r--r--   1 ian      staff          0 Nov 23 11:38 foo

Unlinked foo OK
foo: No such file or directory

ipc% blimey /tmp/foo 100000000
Opened /tmp/foo OK
 % ls -l /tmp/foo
-rw-r--r--   1 ian      staff          0 Nov 23 11:38 /tmp/foo

ftruncate: No space left on device
ipc% exit
ipc%
script done on Tue Nov 23 11:39:02 1993
----------------------------------------------------------------------

My problem is that the location of my temporary files is up to the OEM
and/or end user.  There are significant advantages to mapping the
temporary files, but at the moment I can't do this, because there are
people out there who are in the habit of putting them on tmpfs
filesystems.

Help!  Because of this problem, at the moment, the most advanced
version of my stuff is the one that runs on Windows NT.  Help me avoid
the shame of it!  Are there any workarounds?

--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ian Kemmish                     18 Durham Close, Biggleswade, Beds SG18 8HZ

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 
 
 

1. Solaris 2.2 (& 2.3) performance problems

A month or two ago, a discussion of how to fix performance problem experienced
with Solaris 2.2 was discussed in this news group. It had something to do with
a table that divided up the CPU time. It was of little interest to me at the
time and I did not save the messages. I was just upgraded to 2.2 in the past
week, and it has become very important. I have tried reading every FAQ I could
find but see nothing about a magic table which increases performance.

I will upgraded to 2.3 in the next week or two. Does the problem still exist in
2.3, if so, does the same fix work or is another required? Any information would
be appreciated. Thanks.

Symptoms
========

1) The progress bar in my rendering program moved smoothly in 2.1, but under 2.2
   it moves very jerky.

2) The screenlock (/usr/openwin/bin/xlock) software I have used for years, just
   barely moves under 2.2. It always ran great in the past.

THANKS !!

==============================================================

Biomedical Imaging Resource      
Mayo Foundation                Talk:       (507)-284-4147
Rochester, MN 55905            FAX:        (507)-284-1632
===============================================================

2. Kernel panic: ext2_read_inode

3. Solaris 2.2 vs. Solaris 2.3

4. ufsrestore

5. SLIP for solaris 2.2 or 2.3?

6. in.h differences

7. CDROM for Solaris 2.2/2.3 ...

8. [4/6] CPU Hotplug net/

9. Solaris 2.2 problem building libg++-2.4 if 2.3 is installed

10. solaris 2.3 upgrade from 2.2

11. What versions of SunOS come with Solaris 2.1,2.2, and 2.3?

12. XmCreateSimpleOptionMenu, Solaris 2.3, M1.2.2,xr5

13. incremental linker for Solaris 2.2/2.3 wanted