/var/lib/pcp/testsuite/src/multithread10.c is in pcp-testsuite 4.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* exercise multi-threaded concurrent context opens
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <strings.h>
#include <pcp/pmapi.h>
#include <pthread.h>
void _pcp_warn(int sts, const char* file, int line)
{
char message[512];
if (sts == 0)
return;
fprintf(stderr, "warn fail %s:%d %d %s\n", file, line, sts, pmErrStr_r(sts, message, sizeof(message)));
fflush(stderr);
}
#define pcp_warn(x) _pcp_warn(x,__FILE__,__LINE__)
struct workitem /* one per thread */
{
const char *host_or_archive_name; /* pointer into argv[] */
int ncpu; /* desired output */
};
void*
thread_fn(void *data)
{
struct workitem *work = (struct workitem *)data;
pmFG fg;
int rc, rc2;
pmAtomValue ncpu;
if (rindex (work->host_or_archive_name, pmPathSeparator()) != NULL) /* path name? */
rc = pmCreateFetchGroup(&fg, PM_CONTEXT_ARCHIVE, work->host_or_archive_name);
else
rc = pmCreateFetchGroup(&fg, PM_CONTEXT_HOST, work->host_or_archive_name);
pcp_warn (rc);
if (rc) {
work->ncpu = rc;
return NULL;
}
rc = pmExtendFetchGroup_item(fg, "hinv.ncpu", NULL, NULL, &ncpu, PM_TYPE_32, & rc2);
if (rc)
work->ncpu = rc; /* metric not found perhaps */
else {
rc = pmFetchGroup(fg);
/* BZ1325037 precludes us from testing for error codes reliably. */
/* pcp_warn (rc < 0 ? rc : 0); */
/* pcp_warn (rc2); */
work->ncpu = rc2 ? rc2 : ncpu.l; /* might be negative */
}
rc = pmDestroyFetchGroup(fg);
pcp_warn (rc);
return NULL;
}
int
main(int argc, char **argv)
{
struct workitem *work;
pthread_t *tids;
int i;
int rc;
tids = malloc(sizeof(pthread_t) * argc);
assert (tids != NULL);
work = malloc(sizeof(struct workitem) * argc);
assert (work != NULL);
alarm (30); /* somewhat longer than $PMCD_CONNECT_TIMEOUT */
for (i=0; i<argc-1; i++) {
work[i].host_or_archive_name = argv[i+1];
rc = pthread_create(& tids[i], NULL, &thread_fn, &work[i]);
pcp_warn (rc);
assert (rc == 0);
}
for (i=0; i<argc-1; i++) {
rc = pthread_join (tids[i], NULL);
/* NB: pthread_cancel not appropriate or necessary. */
pcp_warn (rc);
}
for (i=0; i<argc-1; i++) {
printf("%s %d\n", work[i].host_or_archive_name, work[i].ncpu);
}
exit(0);
}
|