Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/aso/tproc.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "terror.h"
21
22
#include <sys/mman.h>
23
#include <sys/time.h>
24
25
#ifndef N_PROC
26
#define N_PROC 8
27
#endif
28
29
#define N_STEP 1000 /* #working steps */
30
#define N_REP 1000 /* #repeats per step */
31
32
static unsigned int *Lock; /* Asolock */
33
34
/* all threads wait until this is == N_PROC */
35
static unsigned int *Active; /* active processes */
36
37
/* Count is the shared resource updated by different threads. */
38
static unsigned int *Count;
39
40
static void workload(unsigned int pid)
41
{
42
int k, r;
43
44
asoincint(Active); /* indicate that we are active */
45
while(*Active < N_PROC) /* wait for all to be ready */
46
asorelax(1);
47
48
for(k = 0; k < N_STEP; ++k)
49
for(r = 0; r < N_REP; ++r)
50
asoincint(Count);
51
}
52
53
tmain()
54
{
55
ssize_t k;
56
Asometh_t *meth;
57
Void_t *addr;
58
int status;
59
int zerof;
60
int type;
61
char *lockid;
62
pid_t pid, cpid[N_PROC];
63
struct timeval tv1, tv2;
64
65
taso(0);
66
tchild();
67
type = Tstall ? ASO_PROCESS : ASO_INTRINSIC;
68
69
/* get shared memory */
70
if((zerof = open("/dev/zero", O_RDWR)) < 0)
71
terror("Can't open /dev/zero");
72
addr = mmap(0, 3*sizeof(unsigned int), PROT_READ|PROT_WRITE, MAP_SHARED, zerof, 0);
73
if(!addr || addr == (Void_t*)(-1))
74
terror("mmap failed");
75
76
Lock = (unsigned int*)addr; /* this is used by asolock() */
77
Count = Lock+1; /* this is the shared counter to be updated asynchronously */
78
Active = Count+1; /* this counter sets all processes to work at the same time */
79
80
lockid = tstfile("aso", 0);
81
meth = 0;
82
while (meth = asometh(ASO_NEXT, meth))
83
{
84
if (!(meth->type & type))
85
continue;
86
if (asoinit(lockid, meth, 0))
87
{
88
twarn("%s method initialization failed", meth->name);
89
continue;
90
}
91
tinfo("testing %s method with %d processes", meth->name, N_PROC);
92
gettimeofday(&tv1, 0); /* start the timer */
93
94
*Lock = 0;
95
*Count = 0;
96
*Active = 0;
97
98
#if N_PROC > 1
99
for(k = 0; k < N_PROC; ++k)
100
if((pid = fork()) < 0)
101
terror("Can't create a child process");
102
else if(pid > 0) /* parent process */
103
{ cpid[k] = pid;
104
continue;
105
}
106
else /* child process */
107
{ workload((unsigned int)getpid()); /* now start working concurrently */
108
texit(0);
109
}
110
111
if (twait(cpid, N_PROC))
112
terror("workload subprocess error");
113
#else
114
workload((unsigned int)getpid());
115
#endif
116
117
if(*Lock != 0)
118
terror("Aso lock is still held by %d", *Lock);
119
if(*Count != N_PROC*N_STEP*N_REP)
120
terror("%s method count error -- expected %d, got %d", meth->name, N_PROC*N_STEP*N_REP, *Count);
121
122
gettimeofday(&tv2, 0); /* get end of time */
123
124
if (tv1.tv_usec > tv2.tv_usec)
125
{ tv2.tv_sec -= 1;
126
tv2.tv_usec += 1000000;
127
}
128
129
tinfo("%s method elapsed time %ld.%lds", meth->name,
130
tv2.tv_sec - tv1.tv_sec, tv2.tv_usec - tv1.tv_usec);
131
}
132
133
texit(0);
134
}
135
136