performance/shared.c
///////////////////////////////////////////////////////////////////////////////
// Filename: shared.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: measure how long it takes to add/remove a shared memory segment
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date Time Name Description
// -------- -------- -------- ------------------------------------------------
// 96/02/29 07:17:38 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
/* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
// Local headers ///////////////////////////////////////////// Local headers //
#include "../common.h"
// Macros /////////////////////////////////////////////////////////// Macros //
/* NONE */
// File scope objects /////////////////////////////////// File scope objects //
const int NUMBER_REPEAT = 10000;// how often for measurements
const int measurement_max = 64; // how many measurements do we want to do?
// 16 should be enough....
const int SHM_SIZE = 100000; // shared memory size for getting number of
// available shared memory segments
const int SHM_MODE = (SHM_R | SHM_W); // user read/write
// External variables, functions, and classes ///////////// External objects //
/* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
/* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
/* NONE */
// Functions and class implementation /// Functions and class implementation //
/* NONE */
// Main /////////////////////////////////////////////////////////////// Main //
int
main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
int i;
// do some performance measurements
cout << "Measuring how long it takes to create, attach, detach and\n"
<< "destroy a shared memory region (buffersize = how big is region "
<< "in bytes)" << endl << endl;
measurement mes(argv[0], "mes", measurement_max);
int size;
int shmid;
char *shmptr;
#ifndef SHMMAX
#define SHMMAX 2000000
#endif
for(size=1024; size <= SHMMAX; size*=2 )
{
// start timer
mes.start(size, NUMBER_REPEAT);
for(i=0; i < NUMBER_REPEAT; i++)
{
// get shared memory
if ((shmid = shmget(IPC_PRIVATE, size, SHM_MODE)) == -1)
error.system("shmget error");
if ((shmptr = (char *)shmat(shmid, 0, 0)) == (char *)-1)
error.system("shmat error");
// destry shared memory
if (shmdt(shmptr) == -1)
error.system("shmdt error");
if (shmctl(shmid, IPC_RMID, 0) == -1)
error.system("shmctl error");
}
// store data about measurement
mes.end();
}
mes.writeout_logfile(false, true, true);// use cout to display results
mes.writeout_plain_logfile(false, true);// and write gnuplot result file
return(EXIT_SUCCESS);
}