HP-UX 11i v3 Memory Management Subsystem
24
Appendix 5 – source code for locinfo program
The source code for the locinfo program follows, in the hope that it will prove useful in analyzing the
memory distribution on NUMA servers. The code is based on the example that appears in the
pstat_getlocality(2) manpage.
/*
* This program returns system-wide and per-process memory locality information.
* To compile the 32-bit version, use -D_PSTAT64.
* The 64-bit version does not need any special compiler flags.
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/pstat.h>
#include <sys/errno.h>
#define BURST ((size_t)3)
#define STRSZ 80
unsigned long pgsize;
void pid_locinfo ( pid_t pid );
void sys_locinfo ( void );
void pages_to_str ( uint64_t pages, char *str );
void
usage ( int argc, char **argv )
{
fprintf ( stderr, "Usage: %s [-p pid]\n", argv[0] );
fprintf ( stderr, "This program prints out per locality " );
fprintf ( stderr, "memory usage.\nIf 'pid' is supplied, " );
fprintf ( stderr, "information on that process is\n" );
fprintf ( stderr, "returned in addition to system-wide " );
fprintf ( stderr, "information.\n\n" );
exit(1);
}
/*
* Verify arguments, call sys_locinfo(), and call pid_locinfo()
* if desired.
*/
int
main ( int argc, char **argv )
{
pid_t pid = (pid_t) 0;
if ( (argc == 2) || (argc > 3) ||
((argc == 3) && (strncmp(argv[1], "-p", 2))) ) {
usage(argc, argv);
}
if ( argc == 3 ) {
pid = atoi(argv[2]);
if (pid < 0) {
/* note that pid 0 is "this process" */
usage(argc, argv);
}
}
/* Get the size of a page for later calculations */
pgsize = sysconf ( _SC_PAGE_SIZE );
sys_locinfo();
if ( argc == 3 ) {
pid_locinfo ( pid );
}
return 0;
}
/*
* Display the system-wide memory usage per locality.
*/
void
sys_locinfo ( void )
{
int i; /* index within pstl[] */
int count; /* the actual number of pstl structures */
int idx = 0; /* index within the context of localities */
struct pst_locality pstl[BURST];
char total_str[STRSZ], free_str[STRSZ], used_str[STRSZ], ffree_str[STRSZ];
uint64_t total=0, free=0, floating_free=0;
printf ( " --- System wide locality info: --- \n" );
printf ( "%6s%6s%7s%7s%6s%10s%10s%10s%10s\n",
"index", "ldom", "physid", "type", "spus",
"total", "free", "float", "used" );