[comp.sys.att] How much memory is left?

lenny@icus.UUCP (Lenny Tropiano) (05/29/88)

In article <459@bacchus> darren@bacchus.UUCP (Darren Friedlein) writes:
|>
|>When smgr (found out I had the wrong mgr) displayed the [!!] icon before,
|>the message I got was that phdaemon died because SOMETHING was swapped
|>out, either phdaemon or a program it was monitoring.  This time, smgr
|>quit completely right after the [!!] icon appeared.  From ps, I could
|>see that the system load was real heavy.  This doesn't assure that
|>swapping was the cause, but that would be my best guess.
|>
phdaemon doesn't support (cause I have too much memory and I can't test
it) when the process it is monitoring get's swapped out (written to /dev/swap)
because the 4MB of virtual memory was used up.  If anyone has a piece of
code to add to phdaemon to handle swapped processes, let me know... 

Here's your troublesome function...

char *getcommand()
{
    static struct user users;
    long   upage;

    if (!proc.p_stat || proc.p_stat == SIDL || proc.p_stat == SZOMB)
	return 0;

    if (!(proc.p_flag & SLOAD)) {
	sprintf(buffer,"can't handle swapped process %d (flag=%05x)",
		proc.p_pid, proc.p_flag);
	werror(buffer,1);
    }

    upage = (long)ctob(proc.p_addr[0]);
    read_mem((char *)&users, upage + U_OFFSET, (long) sizeof (struct user));

    return(users.u_comm);
}

The function "werror()" writes to /dev/error by calling eprintf() and
that might be what is crashing your smgr since the smgr reads the /dev/error
device and prints the "[!!] [!]" icons...
|>What is the difference between a process being swapped out and normal
|>VM paging?  I thought the UNIXpc could only support 4M or virtual memory,
|>but when I formatted the drive, it reserved 6M of space.

I assume a process only gets swapped after you use up the 4MB of virtual
memory.  The 6MB you claim is disk storage for the swap partition, not
the virtual memory...they are two different animals.

Here's a short program that will tell you how much memory you have...

$ cc -c -O memory.c 
$ ld -s -o memory memory.o /lib/crt0s.o /lib/shlib.ifile
$ su
Password:
# chown root memory
# chmod 4755 memory
# memory
...
-Lenny

--- cut here --- --- cut here --- --- cut here --- --- cut here ---
#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <nlist.h>

#ifndef ctob
#include <sys/sysmacros.h>
#endif

#define UNIX	"/unix"
#define KMEM	"/dev/kmem"

struct nlist nl[] = {
#define X_PHYSMEM	0
	{ "physmem" },
#define X_MAXMEM	1
	{ "maxmem" },
#define X_FREEMEM	2
	{ "freemem" },
	{ NULL }
};

main()
{
	int kmem;
	int maxmem, physmem, freemem;

	/*
	 * Look up system addresses in the kernel
	 */
	if (nlist(UNIX, nl) < 0) {
		fprintf(stderr, "%s: no namelist.\n", UNIX);
		exit(1);
	}

	/*
	 * Open kernel memory.
	 */
	if ((kmem = open(KMEM, 0)) < 0) {
		perror(KMEM);
		exit(1);
	}

	/*
	 * Read variables.
	 */
	lseek(kmem, (long) nl[X_PHYSMEM].n_value, 0);
	read(kmem, (char *) &physmem, sizeof(int));

	lseek(kmem, (long) nl[X_MAXMEM].n_value, 0);
	read(kmem, (char *) &maxmem, sizeof(int));

	lseek(kmem, (long) nl[X_FREEMEM].n_value, 0);
	read(kmem, (char *) &freemem, sizeof(int));

	close(kmem);

	/*
	 * Print the numbers.  The internal representation is
	 * in units of core clicks; convert to bytes using ctob macro.
	 */
	printf("Physical machine memory: %d\n", ctob(physmem));
	printf("Max memory available to a process: %d\n", ctob(maxmem));
	printf("Free memory available to a process: %d\n", ctob(freemem)); 

	exit(0);
}

-- 
US MAIL  : Lenny Tropiano, ICUS Computer Group        IIIII  CCC U   U  SSS
           PO Box 1                                     I   C    U   U S
           Islip Terrace, New York  11752               I   C    U   U  SS 
PHONE    : (516) 968-8576 [H] (516) 582-5525 [W]        I   C    U   U    S
TELEX    : 154232428 [ICUS]                           IIIII  CCC  UUU  SSS 
AT&T MAIL: ...attmail!icus!lenny  
UUCP     : ...{mtune, ihnp4, boulder, talcott, sbcs, bc-cis}!icus!lenny 

ford@elgar.UUCP (Ford Prefect ) (06/01/88)

In article <371@icus.UUCP> lenny@icus.UUCP (Lenny Tropiano) writes:
>    if (!(proc.p_flag & SLOAD)) {
>	sprintf(buffer,"can't handle swapped process %d (flag=%05x)",
>		proc.p_pid, proc.p_flag);
>	werror(buffer,1);
>    }
>
>    upage = (long)ctob(proc.p_addr[0]);
>    read_mem((char *)&users, upage + U_OFFSET, (long) sizeof (struct user));

Try this:

	if (proc[procidx].p_flag & SLOAD)
	{
	    upage = (long)ctob(proc.p_addr[0]);
	    read_mem((char *)&users, upage + U_OFFSET,
		      (long) sizeof (struct user));
	}
	else
	{
	    upage = dbtob((long)proc.p_swaddr);
	    read_swap((char *)&users, upage + U_OFFSET,
		      (long) sizeof (struct user));
	}

where read_mem is to /dev/mem as read_swap is to /dev/swap.


>Here's a short program that will tell you how much memory you have...
>
>$ cc -c -O memory.c 
> ...

Here's an even shorter program that will tell you the same stuff;
fancy formatting or converting into bytes is left as an excersise for
the reader...

	echo 'maxmem/D
	physmem/
	freemem/' | adb /unix /dev/kmem


Of course you can't make mine setuid, but what do you want for
nothing, Rubber Biscuits?   :-) :-)


					-=] Ford [=-

"Once there were parking lots,		(In Real Life:  Mike Ditto)
now it's a peaceful oasis.		ford%kenobi@crash.CTS.COM
This was a Pizza Hut,			...!sdcsvax!crash!kenobi!ford
now it's all covered with daisies." -- Talking Heads

lenny@icus.UUCP (Lenny Tropiano) (06/03/88)

In article <166@elgar.UUCP> ford@elgar.UUCP (Ford Prefect ) writes:
|>
|>>Here's a short program that will tell you how much memory you have...
|>>
|>>$ cc -c -O memory.c 
|>> ...
|>
|>Here's an even shorter program that will tell you the same stuff;
|>fancy formatting or converting into bytes is left as an excersise for
|>the reader...
|>
|>	echo 'maxmem/D
|>	physmem/
|>	freemem/' | adb /unix /dev/kmem
|>
Geez.  You're an *adb-guru*.   I guess that's how you easily reverse
engineered things like the CAPCTRL (kbd.o) driver.
|>
|>Of course you can't make mine setuid, but what do you want for
|>nothing, Rubber Biscuits?   :-) :-)
|>
Well if you have the version of the KSH that allows for setuid shell
scripts you could ;-)

Ruuuubbbber Bissscuits... (You remember that song too) :-)

-Lenny

-- 
US MAIL  : Lenny Tropiano, ICUS Computer Group        IIIII  CCC U   U  SSS
           PO Box 1                                     I   C    U   U S
           Islip Terrace, New York  11752               I   C    U   U  SS 
PHONE    : (516) 968-8576 [H] (516) 582-5525 [W]        I   C    U   U    S
TELEX    : 154232428 [ICUS]                           IIIII  CCC  UUU  SSS 
AT&T MAIL: ...attmail!icus!lenny  
UUCP     : ...{mtune, ihnp4, boulder, talcott, sbcs, bc-cis}!icus!lenny