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
|
/*
* Copyright (c) 2011 Ken McDonell. All Rights Reserved.
*
* exercise multi-threaded support for traverse and load/unload
* PMNS operations
*/
#include <stdio.h>
#include <stdlib.h>
#include <pcp/pmapi.h>
#include <pcp/impl.h>
#include <pthread.h>
#ifndef HAVE_PTHREAD_BARRIER_T
#include "pthread_barrier.h"
#endif
#define ITER 10
static int nmetric;
void
dometric(const char *name)
{
nmetric++;
}
static void *
func1(void *arg)
{
int sts;
int i;
char msgbuf[PM_MAXERRMSGLEN];
for (i = 0; i < ITER; i++) {
nmetric = 0;
sts = pmTraversePMNS("", dometric);
if (sts >= 0)
printf("traverse: found %d metrics, sts %d\n", nmetric, sts);
else {
/*
* expect 0 metrics and PM_ERR_NOPMNS if the pmTraversePMNS
* gets in between the pmUnloadPMNS and the pmLoadPMNS in the
* other thread
*/
if (nmetric > 0 || sts != PM_ERR_NOPMNS)
printf("traverse: found %d metrics, sts %s\n", nmetric, pmErrStr_r(sts, msgbuf, PM_MAXERRMSGLEN));
else {
/*
* nmetric == 0 && sts == PM_ERR_NOPMNS, so try again ...
* won't loop forever because eventually func2() will
* finish
*/
i--;
}
}
}
pthread_exit(NULL);
}
static void *
func2(void *arg)
{
int sts;
char *fn = "func2";
int i;
char msgbuf[PM_MAXERRMSGLEN];
for (i = 0; i < ITER; i++) {
pmUnloadNameSpace();
if ((sts = pmLoadNameSpace(PM_NS_DEFAULT)) < 0) {
printf("%s: pmLoadNameSpace[%d]: %s\n", fn, i, pmErrStr_r(sts, msgbuf, PM_MAXERRMSGLEN));
exit(1);
}
}
pthread_exit(NULL);
}
int
main(int argc, char **argv)
{
pthread_t tid1;
pthread_t tid2;
int sts;
char *msg;
unsigned int in[PDU_MAX+1];
unsigned int out[PDU_MAX+1];
int i;
char msgbuf[PM_MAXERRMSGLEN];
setvbuf(stdout, NULL, _IONBF, 0);
if (argc != 1) {
printf("Usage: multithread4\n");
exit(1);
}
for (i = 0; i <= PDU_MAX; i++) {
in[i] = out[i] = 0;
}
__pmSetPDUCntBuf(in, out);
if ((sts = pmLoadNameSpace(PM_NS_DEFAULT)) < 0) {
printf("%s: pmLoadNameSpace: %s\n", argv[0], pmErrStr_r(sts, msgbuf, PM_MAXERRMSGLEN));
exit(1);
}
sts = pthread_create(&tid1, NULL, func1, NULL);
if (sts != 0) {
printf("thread_create: tid1: sts=%d\n", sts);
exit(1);
}
sts = pthread_create(&tid2, NULL, func2, NULL);
if (sts != 0) {
printf("thread_create: tid2: sts=%d\n", sts);
exit(1);
}
pthread_join(tid1, (void *)&msg);
if (msg != NULL) printf("tid1: %s\n", msg);
pthread_join(tid2, (void *)&msg);
if (msg != NULL) printf("tid2: %s\n", msg);
printf("Total PDU counts\n");
printf("in:");
for (i = 0; i <= PDU_MAX; i++)
printf(" %d", in[i]);
putchar('\n');
printf("out:");
for (i = 0; i <= PDU_MAX; i++)
printf(" %d", out[i]);
putchar('\n');
exit(0);
}
|