summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcpc/common/obsoleted.c
blob: 7c6d3b805e9377195484028517e8edd0e3b0007d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <signal.h>
#include <libintl.h>
#include <dirent.h>
#include <sys/cpc_impl.h>

#include "libcpc.h"
#include "libcpc_impl.h"

/*
 * CPC library handle for use by CPCv1 implementation.
 */
cpc_t *__cpc = NULL;
mutex_t __cpc_lock;		/* protects __cpc handle */
int __cpc_v1_cpuver;		/* CPU version in use by CPCv1 client */

#ifdef __sparc
uint64_t __cpc_v1_pcr;		/* last bound %pcr value */
#else
uint32_t __cpc_v1_pes[2];	/* last bound %pes values */
#endif /* __sparc */

int
__cpc_init(void)
{
	const char *fn = "__cpc_init";
	extern cpc_t *__cpc;	/* CPC handle for obsolete clients to share */

	(void) mutex_lock(&__cpc_lock);
	if (__cpc == NULL && (__cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
		__cpc_error(fn, dgettext(TEXT_DOMAIN,
		    "Couldn't open CPC library handle\n"));
		(void) mutex_unlock(&__cpc_lock);
		return (-1);
	}
	(void) mutex_unlock(&__cpc_lock);

	return (0);
}

int
cpc_bind_event(cpc_event_t *this, int flags)
{
	cpc_set_t		*set;
	cpc_request_t		*rp;
	int			ret;

	if (this == NULL) {
		(void) cpc_rele();
		return (0);
	}

	if (__cpc_init() != 0) {
		errno = ENXIO;
		return (-1);
	}

	/*
	 * The cpuver and control fields of the cpc_event_t must be saved off
	 * for later. The user may call cpc_take_sample(), expecting these to
	 * be copied into a different cpc_event_t struct by the kernel. We have
	 * to fake that behavior for CPCv1 clients.
	 */
	__cpc_v1_cpuver = this->ce_cpuver;
#ifdef __sparc
	__cpc_v1_pcr = this->ce_pcr;
#else
	__cpc_v1_pes[0] = this->ce_pes[0];
	__cpc_v1_pes[1] = this->ce_pes[1];
#endif /* __sparc */

	if ((set = __cpc_eventtoset(__cpc, this, flags)) == NULL) {
		errno = EINVAL;
		return (-1);
	}

	/*
	 * Convert flags to CPC2.
	 */
	if (flags & CPC_BIND_EMT_OVF) {
		for (rp = set->cs_request; rp != NULL; rp = rp->cr_next)
			rp->cr_flags |= CPC_OVF_NOTIFY_EMT;
		flags &= ~CPC_BIND_EMT_OVF;
	}

	ret = cpc_bind_curlwp(__cpc, set, flags);

	(void) cpc_set_destroy(__cpc, set);

	return (ret);
}

int
cpc_take_sample(cpc_event_t *this)
{
	this->ce_cpuver = __cpc_v1_cpuver;
#ifdef __sparc
	this->ce_pcr = __cpc_v1_pcr;
#else
	this->ce_pes[0] = __cpc_v1_pes[0];
	this->ce_pes[1] = __cpc_v1_pes[1];
#endif /* __sparc */

	return (syscall(SYS_cpc, CPC_SAMPLE, -1, this->ce_pic, &this->ce_hrt,
	    &CPC_TICKREG(this), 0));
}

int
cpc_count_usr_events(int enable)
{
	return (syscall(SYS_cpc, CPC_USR_EVENTS, -1, enable, 0));
}

int
cpc_count_sys_events(int enable)
{
	return (syscall(SYS_cpc, CPC_SYS_EVENTS, -1, enable, 0));
}

int
cpc_rele(void)
{
	return (syscall(SYS_cpc, CPC_RELE, -1, NULL, 0));
}

/*
 * See if the system call is working and installed.
 *
 * We invoke the system call with nonsense arguments - if it's
 * there and working correctly, it will return EINVAL.
 *
 * (This avoids the user getting a SIGSYS core dump when they attempt
 * to bind on older hardware)
 */
int
cpc_access(void)
{
	void (*handler)(int);
	int error = 0;
	const char fn[] = "access";

	handler = signal(SIGSYS, SIG_IGN);
	if (syscall(SYS_cpc, -1, -1, NULL, 0) == -1 &&
	    errno != EINVAL)
		error = errno;
	(void) signal(SIGSYS, handler);

	switch (error) {
	case EAGAIN:
		__cpc_error(fn, dgettext(TEXT_DOMAIN, "Another process may be "
		    "sampling system-wide CPU statistics\n"));
		break;
	case ENOSYS:
		__cpc_error(fn,
		    dgettext(TEXT_DOMAIN, "CPU performance counters "
		    "are inaccessible on this machine\n"));
		break;
	default:
		__cpc_error(fn, "%s\n", strerror(errno));
		break;
	case 0:
		return (0);
	}

	errno = error;
	return (-1);
}

/*
 * To look at the system-wide counters, we have to open the
 * 'shared' device.  Once that device is open, no further contexts
 * can be installed (though one open is needed per CPU)
 */
int
cpc_shared_open(void)
{
	const char driver[] = CPUDRV_SHARED;

	return (open(driver, O_RDWR));
}

void
cpc_shared_close(int fd)
{
	(void) cpc_shared_rele(fd);
	(void) close(fd);
}

int
cpc_shared_bind_event(int fd, cpc_event_t *this, int flags)
{
	extern cpc_t		*__cpc;
	cpc_set_t		*set;
	int			ret;
	char			*packed_set;
	size_t			packsize;
	int			subcode;
	__cpc_args_t		cpc_args;

	if (this == NULL) {
		(void) cpc_shared_rele(fd);
		return (0);
	} else if (flags != 0) {
		errno = EINVAL;
		return (-1);
	}

	if (__cpc_init() != 0) {
		errno = ENXIO;
		return (-1);
	}

	if ((set = __cpc_eventtoset(__cpc, this, flags)) == NULL) {
		errno = EINVAL;
		return (-1);
	}

	__cpc_v1_cpuver = this->ce_cpuver;

	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
		errno = ENOMEM;
		return (-1);
	}

	cpc_args.udata1 = packed_set;
	cpc_args.udata2 = (void *)packsize;
	cpc_args.udata3 = (void *)&subcode;

	ret = ioctl(fd, CPCIO_BIND, &cpc_args);

	free(packed_set);
	(void) cpc_set_destroy(__cpc, set);

	return (ret);
}

int
cpc_shared_take_sample(int fd, cpc_event_t *this)
{
	__cpc_args_t args;

	args.udata1 = this->ce_pic;
	args.udata2 = &this->ce_hrt;
	args.udata3 = &CPC_TICKREG(this);

	this->ce_cpuver = __cpc_v1_cpuver;

	return (ioctl(fd, CPCIO_SAMPLE, &args));
}

int
cpc_shared_rele(int fd)
{
	return (ioctl(fd, CPCIO_RELE, 0));
}

int
cpc_pctx_bind_event(pctx_t *pctx, id_t lwpid, cpc_event_t *event, int flags)
{
	cpc_set_t		*set;
	int			ret;

	if (event == NULL)
		return (cpc_pctx_rele(pctx, lwpid));

	if (__cpc_init() != 0) {
		errno = ENXIO;
		return (-1);
	}

	else if (flags != 0) {
		errno = EINVAL;
		return (-1);
	}

	if ((set = __cpc_eventtoset(__cpc, event, flags)) == NULL) {
		errno = EINVAL;
		return (-1);
	}

	/*
	 * The cpuver and control fields of the cpc_event_t must be saved off
	 * for later. The user may call cpc_take_sample(), expecting these to
	 * be copied into a different cpc_event_t struct by the kernel. We have
	 * to fake that behavior for CPCv1 clients.
	 */
	__cpc_v1_cpuver = event->ce_cpuver;

	ret = cpc_bind_pctx(__cpc, pctx, lwpid, set, 0);

	(void) cpc_set_destroy(__cpc, set);

	return (ret);
}

int
cpc_pctx_take_sample(pctx_t *pctx, id_t lwpid, cpc_event_t *event)
{
	event->ce_cpuver = __cpc_v1_cpuver;

	return (__pctx_cpc(pctx, __cpc, CPC_SAMPLE, lwpid, event->ce_pic,
	    &event->ce_hrt, &CPC_TICKREG(event), CPC1_BUFSIZE));
}

/*
 * Given a process context and an lwpid, mark the CPU performance
 * counter context as invalid.
 */
int
cpc_pctx_invalidate(pctx_t *pctx, id_t lwpid)
{
	return (__pctx_cpc(pctx, __cpc, CPC_INVALIDATE, lwpid, 0, 0, 0, 0));
}

/*
 * Given a process context and an lwpid, remove all our
 * hardware context from it.
 */
int
cpc_pctx_rele(pctx_t *pctx, id_t lwpid)
{
	return (__pctx_cpc(pctx, __cpc, CPC_RELE, lwpid, 0, 0, 0, 0));
}

static cpc_errfn_t *__cpc_uerrfn;

/*PRINTFLIKE2*/
void
__cpc_error(const char *fn, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	if (__cpc_uerrfn)
		__cpc_uerrfn(fn, fmt, ap);
	else {
		(void) fprintf(stderr, "libcpc: %s: ", fn);
		(void) vfprintf(stderr, fmt, ap);
	}
	va_end(ap);
}

void
cpc_seterrfn(cpc_errfn_t *errfn)
{
	__cpc_uerrfn = errfn;
}

/*
 * cpc_version() is only for CPC1 clients.
 */
uint_t __cpc_workver = CPC_VER_1;

uint_t
cpc_version(uint_t ver)
{
	__cpc_workver = CPC_VER_1;

	switch (ver) {
	case CPC_VER_NONE:
	case CPC_VER_CURRENT:
		return (CPC_VER_CURRENT);
	case CPC_VER_1:
		/*
		 * As long as the client is using cpc_version() at all, it is
		 * a CPCv1 client.  We still allow CPCv1 clients to compile on
		 * CPCv2 systems.
		 */
		return (CPC_VER_1);
	}

	return (CPC_VER_NONE);
}