2 * Copyright (c) 2011 Ken McDonell. All Rights Reserved.
4 * exercise multi-threaded multiple host contexts with pmLookupDesc()
5 * as the simplest possible case
10 #include <pcp/pmapi.h>
14 #ifndef HAVE_PTHREAD_BARRIER_T
15 #include "pthread_barrier.h"
20 static char *namelist
[NMETRIC
] = {
22 "sampledso.milliseconds",
23 "sample.ulonglong.bin_ctr",
27 static pmID pmidlist
[NMETRIC
];
29 static pthread_barrier_t barrier
;
36 foo(FILE *f
, char *fn
, int i
)
42 sts
= pmLookupDesc(pmidlist
[i
], &desc
);
44 fprintf(f
, "%s: pmLookupDesc[%s] -> %s\n", fn
, pmIDStr_r(pmidlist
[i
], strbuf
, sizeof(strbuf
)), pmErrStr(sts
));
45 pthread_exit("botch");
47 else if (pmidlist
[i
] != desc
.pmid
) {
48 fprintf(f
, "%s: pmLookupDesc: Expecting PMID: %s", fn
, pmIDStr_r(pmidlist
[i
], strbuf
, sizeof(strbuf
)));
49 fprintf(f
, " got: %s\n", pmIDStr_r(desc
.pmid
, strbuf
, sizeof(strbuf
)));
50 pthread_exit("botch");
53 fprintf(f
, "%s: %s (%s) ->", fn
, namelist
[i
], pmIDStr_r(pmidlist
[i
], strbuf
, sizeof(strbuf
)));
54 fprintf(f
, " %s", pmTypeStr_r(desc
.type
, strbuf
, sizeof(strbuf
)));
55 fprintf(f
, " %s", pmInDomStr_r(desc
.indom
, strbuf
, sizeof(strbuf
)));
56 if (desc
.sem
== PM_SEM_COUNTER
) fprintf(f
, " counter");
57 else if (desc
.sem
== PM_SEM_INSTANT
) fprintf(f
, " instant");
58 else if (desc
.sem
== PM_SEM_DISCRETE
) fprintf(f
, " discrete");
59 else fprintf(f
, " sem-%d", desc
.sem
);
60 fprintf(f
, " %s\n", pmUnitsStr_r(&desc
.units
, strbuf
, sizeof(strbuf
)));
72 if ((f
= fopen("/tmp/func1.out", "w")) == NULL
) {
73 perror("func1 fopen");
74 pthread_exit("botch");
77 j
= pmUseContext(ctx1
);
79 fprintf(f
, "Error: %s: pmUseContext(%d) -> %s\n", fn
, ctx1
, pmErrStr(j
));
81 pthread_exit("botch");
84 pthread_barrier_wait(&barrier
);
86 for (j
= 0; j
< 100; j
++) {
87 for (i
= 0; i
< NMETRIC
; i
++)
103 if ((f
= fopen("/tmp/func2.out", "w")) == NULL
) {
104 perror("func2 fopen");
105 pthread_exit("botch");
108 j
= pmUseContext(ctx2
);
110 fprintf(f
, "Error: %s: pmUseContext(%d) -> %s\n", fn
, ctx2
, pmErrStr(j
));
112 pthread_exit("botch");
115 pthread_barrier_wait(&barrier
);
117 for (j
= 0; j
< 100; j
++) {
118 for (i
= NMETRIC
-1; i
>= 0; i
--)
134 if ((f
= fopen("/tmp/func3.out", "w")) == NULL
) {
135 perror("func3 fopen");
136 pthread_exit("botch");
139 j
= pmUseContext(ctx3
);
141 fprintf(f
, "Error: %s: pmUseContext(%d) -> %s\n", fn
, ctx3
, pmErrStr(j
));
143 pthread_exit("botch");
146 pthread_barrier_wait(&barrier
);
148 for (j
= 0; j
< 100; j
++) {
149 for (i
= 0; i
< NMETRIC
; i
+= 2)
151 for (i
= 1; i
< NMETRIC
; i
+= 2)
160 main(int argc
, char **argv
)
170 __pmSetProgname(argv
[0]);
172 while ((c
= getopt(argc
, argv
, "D:")) != EOF
) {
175 case 'D': /* debug flag */
176 sts
= __pmParseDebug(optarg
);
178 fprintf(stderr
, "%s: unrecognized debug flag specification (%s)\n",
193 if (errflag
|| optind
== argc
|| argc
-optind
> 3) {
194 fprintf(stderr
, "Usage: %s [-D...] host1 [host2 [host3]]\n", pmProgname
);
198 ctx1
= pmNewContext(PM_CONTEXT_HOST
, argv
[optind
]);
200 printf("Error: pmNewContext(%s) -> %s\n", argv
[optind
], pmErrStr(ctx1
));
206 ctx2
= pmNewContext(PM_CONTEXT_HOST
, argv
[optind
]);
208 printf("Error: pmNewContext(%s) -> %s\n", argv
[optind
], pmErrStr(ctx2
));
217 ctx3
= pmNewContext(PM_CONTEXT_HOST
, argv
[optind
]);
219 printf("Error: pmNewContext(%s) -> %s\n", argv
[optind
], pmErrStr(ctx2
));
227 sts
= pmLookupName(NMETRIC
, namelist
, pmidlist
);
228 if (sts
!= NMETRIC
) {
231 printf("Error: pmLookupName -> %s\n", pmErrStr(sts
));
233 printf("Error: pmLookupName returned %d, expected %d\n", sts
, NMETRIC
);
234 for (i
= 0; i
< NMETRIC
; i
++) {
235 printf(" %s -> %s\n", namelist
[i
], pmIDStr(pmidlist
[i
]));
240 sts
= pthread_barrier_init(&barrier
, NULL
, 3);
242 printf("pthread_barrier_init: sts=%d\n", sts
);
246 sts
= pthread_create(&tid1
, NULL
, func1
, NULL
);
248 printf("thread_create: tid1: sts=%d\n", sts
);
251 sts
= pthread_create(&tid2
, NULL
, func2
, NULL
);
253 printf("thread_create: tid2: sts=%d\n", sts
);
256 sts
= pthread_create(&tid3
, NULL
, func3
, NULL
);
258 printf("thread_create: tid3: sts=%d\n", sts
);
262 pthread_join(tid1
, (void *)&msg
);
263 if (msg
!= NULL
) printf("tid1: %s\n", msg
);
264 pthread_join(tid2
, (void *)&msg
);
265 if (msg
!= NULL
) printf("tid2: %s\n", msg
);
266 pthread_join(tid3
, (void *)&msg
);
267 if (msg
!= NULL
) printf("tid3: %s\n", msg
);
268 pthread_cancel(tid1
);
269 pthread_cancel(tid2
);
270 pthread_cancel(tid3
);