ipc.c revision f2fc321be9b4df7748e8c31a5edd154b0177b139
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "config.h"
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
#include "filebench.h"
/* IPC Hub and Simple memory allocator */
static int shmfd;
/*
* Interprocess Communication mechanisms. If multiple processes
* are used, filebench opens a shared file in memory mapped mode to hold
* a variety of global variables and data structures. If only using
* multiple threads, it just allocates a region of local memory. A
* region of interprocess shared memory and a set of shared semaphores
* are also created. Routines are provided to manage the creation,
* destruction, and allocation of these resoures.
*/
/*
* Locks a mutex and logs any errors.
*/
int
{
int error;
#ifdef HAVE_ROBUST_MUTEX
if (error == EOWNERDEAD) {
if (pthread_mutex_consistent_np(mutex) != 0) {
return (-1);
}
return (0);
}
#endif /* HAVE_ROBUST_MUTEX */
if (error != 0) {
}
return (error);
}
/*
* Unlocks a mutex and logs any errors.
*/
int
{
int error;
#ifdef HAVE_ROBUST_MUTEX
if (error == EOWNERDEAD) {
if (pthread_mutex_consistent_np(mutex) != 0) {
return (-1);
}
return (0);
}
#endif /* HAVE_ROBUST_MUTEX */
if (error != 0) {
}
return (error);
}
/*
* On first invocation, allocates a mutex attributes structure
* and initializes it with appropriate attributes. In all cases,
* returns a pointer to the structure.
*/
ipc_mutexattr(void)
{
#ifdef USE_PROCESS_MODEL
if ((mutexattr =
}
#ifdef HAVE_PROCSCOPE_PTHREADS
(void) pthread_mutexattr_init(mutexattr);
PTHREAD_PROCESS_SHARED) != 0) {
"PROCESS_SHARED on this platform");
}
PTHREAD_PRIO_INHERIT) != 0) {
"PTHREAD_PRIO_INHERIT on this platform");
}
#endif /* HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL */
#endif /* HAVE_PROCSCOPE_PTHREADS */
#ifdef HAVE_ROBUST_MUTEX
PTHREAD_MUTEX_ROBUST_NP) != 0) {
"PTHREAD_MUTEX_ROBUST_NP on this platform");
}
PTHREAD_MUTEX_ERRORCHECK) != 0) {
"PTHREAD_MUTEX_ERRORCHECK on this platform");
}
#endif /* HAVE_ROBUST_MUTEX */
}
#endif /* USE_PROCESS_MODEL */
return (mutexattr);
}
/*
* On first invocation, allocates a condition variable attributes
* structure and initializes it with appropriate attributes. In
* all cases, returns a pointer to the structure.
*/
ipc_condattr(void)
{
#ifdef USE_PROCESS_MODEL
}
#ifdef HAVE_PROCSCOPE_PTHREADS
(void) pthread_condattr_init(condattr);
PTHREAD_PROCESS_SHARED) != 0) {
"cannot set cond attr PROCESS_SHARED");
}
#endif /* HAVE_PROCSCOPE_PTHREADS */
}
#endif /* USE_PROCESS_MODEL */
return (condattr);
}
/*
* structure and initializes it with appropriate attributes.
* In all cases, returns a pointer to the structure.
*/
static pthread_rwlockattr_t *
ipc_rwlockattr(void)
{
#ifdef USE_PROCESS_MODEL
if (rwlockattr == NULL) {
if ((rwlockattr =
}
#ifdef HAVE_PROCSCOPE_PTHREADS
(void) pthread_rwlockattr_init(rwlockattr);
PTHREAD_PROCESS_SHARED) != 0) {
"cannot set rwlock attr PROCESS_SHARED");
}
#endif /* HAVE_PROCSCOPE_PTHREADS */
}
#endif /* USE_PROCESS_MODEL */
return (rwlockattr);
}
/*
* Calls semget() to get a set of shared system V semaphores.
*/
void
ipc_seminit(void)
{
/* Already done? */
if (filebench_shm->seminit)
return;
"could not create sysv semaphore set "
"(need to increase sems?): %s",
exit(1);
}
}
/*
* Initialize the Interprocess Communication system and its
* associated shared memory structure. It first creates a
* temporary file using either the mkstemp() function or the
* tempnam() and open() functions. If the process model is in
* use,it than sets the file large enough to hold the
* filebench_shm and an additional Megabyte. The file is then
* memory mapped. If the process model is not in use, it simply
* mallocs a region of sizeof (filebench_shm_t).
*
* Once the shared memory region / file is created, ipc_init
* initializes various locks pointers, and variables in the
* shared memory. It also uses ftok() to get a shared memory
* semaphore key for later use in allocating shared semaphores.
*/
void
ipc_init(void)
{
#ifdef HAVE_SEM_RMID
int semid;
#endif
#ifdef HAVE_MKSTEMP
#else
#endif /* HAVE_MKSTEMP */
#ifdef USE_PROCESS_MODEL
if (shmfd < 0) {
exit(1);
}
exit(1);
}
/* LINTED E_BAD_PTR_CAST_ALIGN */
exit(1);
}
#else
if ((filebench_shm =
exit(1);
}
#endif /* USE_PROCESS_MODEL */
/* Setup mutexes for object lists */
ipc_mutexattr());
ipc_mutexattr());
ipc_mutexattr());
ipc_mutexattr());
ipc_mutexattr());
ipc_rwlockattr());
/* Create semaphore */
exit(1);
}
#ifdef HAVE_SEM_RMID
#endif
filebench_shm->eventgen_hz = 0;
}
/*
* If compiled to use process model, just unlinks the shmpath.
* Otherwise a no-op.
*/
void
ipc_cleanup(void)
{
#ifdef USE_PROCESS_MODEL
#endif /* USE_PROCESS_MODEL */
}
/*
* Attach to shared memory. Used by worker processes to open
* and mmap the shared memory region. If successful, it
* initializes the worker process' filebench_shm to point to
* the region and returns 0. Otherwise it returns -1.
*/
int
{
return (-1);
}
/* LINTED E_BAD_PTR_CAST_ALIGN */
return (-1);
}
return (0);
}
static int filebench_sizes[] = {
/*
* Allocates filebench objects from pre allocated region of
* shareable memory. The memory region is partitioned into sets
* of objects during initialization. This routine scans for
* the first unallocated object of type "type" in the set of
* available objects, and makes it as allocated. The routine
* returns a pointer to the object, or NULL if all objects have
* been allocated.
*/
void *
ipc_malloc(int type)
{
int i;
for (i = 0; i < max; i++) {
break;
}
if (i >= max) {
return (NULL);
}
switch (type) {
case FILEBENCH_FILEOBJ:
sizeof (fileobj_t));
return ((char *)&filebench_shm->fileobj[i]);
case FILEBENCH_FILESET:
sizeof (fileset_t));
return ((char *)&filebench_shm->fileset[i]);
case FILEBENCH_FILESETENTRY:
sizeof (filesetentry_t));
return ((char *)&filebench_shm->filesetentry[i]);
case FILEBENCH_PROCFLOW:
sizeof (procflow_t));
return ((char *)&filebench_shm->procflow[i]);
case FILEBENCH_THREADFLOW:
sizeof (threadflow_t));
return ((char *)&filebench_shm->threadflow[i]);
case FILEBENCH_FLOWOP:
sizeof (flowop_t));
return ((char *)&filebench_shm->flowop[i]);
case FILEBENCH_INTEGER:
return ((char *)&filebench_shm->integer_ptrs[i]);
case FILEBENCH_STRING:
return ((char *)&filebench_shm->string_ptrs[i]);
case FILEBENCH_VARIABLE:
sizeof (var_t));
return ((char *)&filebench_shm->var[i]);
}
type);
return (NULL);
}
/*
* Frees a filebench object of type "type" at the location
* pointed to by "addr". It uses the type and address to
* calculate which object is being freed, and clears its
* allocation map entry.
*/
void
{
int item;
return;
}
switch (type) {
case FILEBENCH_FILEOBJ:
break;
case FILEBENCH_FILESET:
break;
case FILEBENCH_FILESETENTRY:
size = sizeof (filesetentry_t);
break;
case FILEBENCH_PROCFLOW:
size = sizeof (procflow_t);
break;
case FILEBENCH_THREADFLOW:
size = sizeof (threadflow_t);
break;
case FILEBENCH_FLOWOP:
break;
case FILEBENCH_INTEGER:
break;
case FILEBENCH_STRING:
break;
case FILEBENCH_VARIABLE:
break;
}
}
/*
* Allocate a string from filebench string memory. The length
* of the allocated string is the same as the length of the
* supplied string "string", and the contents of string are
* copied to the newly allocated string.
*/
char *
ipc_stralloc(char *string)
{
return (NULL);
}
return (allocstr);
}
/*
* Allocate a path string from filebench path string memory.
* Specifically used for allocating fileset paths. The length
* of the allocated path string is the same as the length of
* the supplied path string "path", and the contents of path
* are copied to the newly allocated path string. Checks for
* out-of-path-string-memory condition and returns NULL if so.
* Otherwise it returns a pointer to the newly allocated path
* string.
*/
char *
ipc_pathalloc(char *path)
{
return (NULL);
}
return (allocpath);
}
/*
* This is a limited functionality deallocator for path
* strings - it can only free all path strings at once,
* in order to avoid fragmentation.
*/
void
ipc_freepaths(void)
{
}
/*
* Allocates a semid from the table of semids for pre intialized
* semaphores. Searches for the first available semaphore, and
* sets the entry in the table to "1" to indicate allocation.
* Returns the allocated semid. Stops the run if all semaphores
* are already in use.
*/
int
ipc_semidalloc(void)
{
int semid;
;
if (semid == FILEBENCH_NSEMS) {
"Out of semaphores, increase system tunable limit");
}
return (semid);
}
/*
* Frees up the supplied semid by seting its position in the
* allocation table to "0".
*/
void
ipc_semidfree(int semid)
{
}
/*
* Create a pool of shared memory to fit the per-thread
* allocations. Uses shmget() to create a shared memory region
* of size "size", attaches to it using shmat(), and stores
* the returned address of the region in filebench_shm->shm_addr.
* The pool is only created on the first call. The routine
* returns 0 if successful or the pool already exists,
* -1 otherwise.
*/
int
{
#ifdef HAVE_SHM_SHARE_MMU
int flag = SHM_SHARE_MMU;
#else
int flag = 0;
#endif /* HAVE_SHM_SHARE_MMU */
/* Already done? */
return (0);
"Creating %zd bytes of ISM Shared Memory...", size);
if ((filebench_shm->shm_id =
"Failed to create %zd bytes of ISM shared memory", size);
return (-1);
}
0, flag)) == (void *)-1) {
"Failed to attach %zd bytes of created ISM shared memory",
size);
return (-1);
}
"Allocated %zd bytes of ISM Shared Memory... at %zx",
/* Locked until allocated to block allocs */
return (0);
}
/* Per addr space ism */
static int ism_attached = 0;
/*
* Attach to interprocess shared memory. If already attached
* just return, otherwise use shmat() to attached to the region
* with ID of filebench_shm->shm_id. Returns -1 if shmat()
* fails, otherwise 0.
*/
static int
ipc_ismattach(void)
{
#ifdef HAVE_SHM_SHARE_MMU
int flag = SHM_SHARE_MMU;
#else
int flag = 0;
#endif /* HAVE_SHM_SHARE_MMU */
if (ism_attached)
return (0);
/* Does it exist? */
return (0);
return (-1);
ism_attached = 1;
return (0);
}
/*
* Allocate from interprocess shared memory. Attaches to ism
* if necessary, then allocates "size" bytes, updates allocation
* information and returns a pointer to the allocated memory.
*/
/*
* XXX No check is made for out-of-memory condition
*/
char *
{
char *allocstr;
/* Map in shared memory */
(void) ipc_ismattach();
return (allocstr);
}
/*
* Deletes shared memory region and resets shared memory region
* information in filebench_shm.
*/
void
ipc_ismdelete(void)
{
return;
#ifdef HAVE_SEM_RMID
#endif
filebench_shm->shm_allocated = 0;
}