summaryrefslogtreecommitdiff
path: root/qa/src/torture_trace.c
blob: 6e48182511efadea722b90fac8b7d4162b7198a8 (plain)
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
151
152
153
154
155
156
#include <pcp/pmapi.h>
#include <pcp/impl.h>
#include <pcp/trace.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <time.h>
#ifdef IS_SOLARIS
#include <sys/rtpriocntl.h>
#include <sys/tspriocntl.h>
#include <sys/processor.h>
#endif
#ifdef HAVE_PRCTL
#include <sys/prctl.h>
#endif
#include <sys/wait.h>

#ifndef HAVE_SPROC

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#endif /*HAVE_SPROC*/

#define MAXTAGNAMELEN    256

static int	obscount = 0;
static int	pointcount = 0;
static int	transactcount = 0;

void
gentag(char *buf)
{
    int	i, len;

    len = (int)lrand48() % 10 + 1;
    for (i=0; i < len; i++)
	buf[i] = (char)((int)lrand48() % 88 + 40);   /* some ascii */
    buf[i] = '\0';
    /*fprintf(stderr, "Generated tag \"%s\" for pid=%d\n", buf, getpid());*/
}

void
run(int sts, char *tag)
{
    char	*err;
    err = pmtraceerrstr(sts);
    if (sts < 0)
	fprintf(stderr, "failed on tag \"%s\": %s\n", tag, err);
}

/*ARGSUSED*/
void
sproc1(void *foo)
{
    int		i;
    char	buf[64];
    double	nan=0.0;

    for (i = 0; i < 5; i++)
	nan = 0.0/nan;
    for (i = 0; i < 1000; i++) {
	gentag(buf);
	run(pmtraceobs(buf, nan), buf);
	run(pmtraceobs(buf, 2.239801), buf);
	obscount++;
    }
}

/*ARGSUSED*/
void
sproc2(void *foo)
{
    int		i;
    char	buf[64];

    for (i = 0; i < 2000; i++) {
	gentag(buf);
	run(pmtracepoint(buf), buf);
	pointcount++;
    }
}

/*ARGSUSED*/
void
sproc3(void *foo)
{
    int		i;
    char	buf1[64], buf2[64], buf3[64], buf4[64];

    gentag(buf1);
    gentag(buf2);
    gentag(buf3);
    gentag(buf4);

    for (i=0; i < 500; i++) {
	run(pmtracebegin(buf1), buf1);
	run(pmtracebegin(buf2), buf2);
	run(pmtracebegin(buf3), buf3);
	run(pmtraceend(buf2), buf2);	transactcount++;
	run(pmtracebegin(buf4), buf4);
	run(pmtraceend(buf4), buf4);	transactcount++;
	run(pmtraceend(buf3), buf3);	transactcount++;
	run(pmtraceend(buf1), buf1);	transactcount++;
    }
}


int
main(int argc, char *argv[])
{
    int		i;
#ifndef HAVE_SPROC
    pthread_t threads[3];
#endif

    /* exercise the zero-observation case */
    run(pmtraceobs("zero", 0), "zero");
    obscount++;

    srand48((long)time(0));
    /*pmtracestate(PMTRACE_STATE_COMMS | PMTRACE_STATE_API);*/

    /* eat this, pmdatrace! */
#ifdef HAVE_SPROC
    sproc(sproc1, PR_SADDR, NULL);
    sproc(sproc2, PR_SADDR, NULL);
    sproc(sproc3, PR_SADDR, NULL);
#else
    pthread_create(threads, NULL, (void (*))sproc1, NULL);
    pthread_create(threads+1, NULL, (void (*))sproc2, NULL);
    pthread_create(threads+2, NULL, (void (*))sproc3, NULL);
#endif

    for (i=0; i < 3; i++) {
#ifdef HAVE_SPROC
	wait(NULL);
#else
	void * rv;

	pthread_join (threads[i], &rv);
#endif
	fprintf(stderr, "reaped sproc #%d\n", i);
    }

    fprintf(stderr, "torture_trace counters:\n");
    fprintf(stderr, "    pmtraceobs      %d\n", obscount);
    fprintf(stderr, "    pmtracepoint    %d\n", pointcount);
    fprintf(stderr, "    pmtracetransact %d\n", transactcount);

    exit(0);
}