1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
dbench version 1
Copyright (C) Andrew Tridgell 1999
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "dbench.h"
static struct timeval tp1,tp2;
void start_timer(void)
{
gettimeofday(&tp1,NULL);
}
double end_timer(void)
{
gettimeofday(&tp2,NULL);
return((tp2.tv_sec - tp1.tv_sec) +
(tp2.tv_usec - tp1.tv_usec)*1.0e-6);
}
/* return a pointer to a anonymous shared memory segment of size "size"
which will persist across fork() but will disappear when all processes
exit
The memory is not zeroed
This function uses system5 shared memory. It takes advantage of a property
that the memory is not destroyed if it is attached when the id is removed
*/
void *shm_setup(int size)
{
int shmid;
void *ret;
shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
if (shmid == -1) {
printf("can't get private shared memory of %d bytes: %s\n",
size,
strerror(errno));
exit(1);
}
ret = (void *)shmat(shmid, 0, 0);
if (!ret || ret == (void *)-1) {
printf("can't attach to shared memory\n");
return NULL;
}
/* the following releases the ipc, but note that this process
and all its children will still have access to the memory, its
just that the shmid is no longer valid for other shm calls. This
means we don't leave behind lots of shm segments after we exit
See Stevens "advanced programming in unix env" for details
*/
shmctl(shmid, IPC_RMID, 0);
return ret;
}
void strupper(char *s)
{
while (*s) {
*s = toupper(*s);
s++;
}
}
/****************************************************************************
similar to string_sub() but allows for any character to be substituted.
Use with caution!
****************************************************************************/
void all_string_sub(char *s,const char *pattern,const char *insert)
{
char *p;
size_t ls,lp,li;
if (!insert || !pattern || !s) return;
ls = strlen(s);
lp = strlen(pattern);
li = strlen(insert);
if (!*pattern) return;
while (lp <= ls && (p = strstr(s,pattern))) {
memmove(p+li,p+lp,ls + 1 - (((int)(p-s)) + lp));
memcpy(p, insert, li);
s = p + li;
ls += (li-lp);
}
}
/****************************************************************************
Get the next token from a string, return False if none found
handles double-quotes.
Based on a routine by GJC@VILLAGE.COM.
Extensively modified by Andrew.Tridgell@anu.edu.au
****************************************************************************/
BOOL next_token(char **ptr,char *buff,char *sep)
{
static char *last_ptr=NULL;
char *s;
BOOL quoted;
if (!ptr) ptr = &last_ptr;
if (!ptr) return(False);
s = *ptr;
/* default to simple separators */
if (!sep) sep = " \t\n\r";
/* find the first non sep char */
while(*s && strchr(sep,*s)) s++;
/* nothing left? */
if (! *s) return(False);
/* copy over the token */
for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++) {
if (*s == '\"')
quoted = !quoted;
else
*buff++ = *s;
}
*ptr = (*s) ? s+1 : s;
*buff = 0;
last_ptr = *ptr;
return(True);
}
|