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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <kstat.h>
#include <signal.h>
#include <setjmp.h>
#include "sdbc_stats.h"
#include "report.h"
#include "common.h"
static sigjmp_buf env;
static sig_atomic_t sig_raised = 0;
static void sig_handler(int);
void
sig_handler(int sig)
{
switch (sig) {
case SIGSEGV:
sig_raised = 1;
siglongjmp(env, sig);
default:
exit(sig);
}
}
/*
* kstat_retrieve() - populate the ks_data field of the kstat_t structure
*
* This function is a user-land equivalent of a ks_snapshot
*
* parameters
* kstat_ctl_t *kc - kstat_ctl_t structure representing returned from
* kstat_open()
* kstat_t *ksp - kstat_t strcture to popluate ks_data into
*
* returns
* NULL pointer on failure
* kstat_t * structure on success
*/
kstat_t *
kstat_retrieve(kstat_ctl_t *kc, kstat_t *ksp)
{
kstat_t *rval;
kstat_named_t *knp;
char *end;
int i;
struct sigaction segv_act; /* default actions */
if (ksp == NULL)
return (NULL);
if (ksp->ks_data == NULL &&
kstat_read(kc, ksp, NULL) == -1)
return (NULL);
rval = (kstat_t *)calloc(1, sizeof (*ksp));
(void) memcpy(rval, ksp, sizeof (*ksp));
rval->ks_data = (void *) calloc(1, ksp->ks_data_size);
(void) memcpy(rval->ks_data, ksp->ks_data,
sizeof (kstat_named_t) * ksp->ks_ndata);
/* special handling for variable length string KSTAT_DATA_STRING */
knp = (kstat_named_t *)rval->ks_data;
end = (char *)(knp + ksp->ks_ndata);
for (i = 0; i < ksp->ks_ndata; i++, knp++) {
if (knp->data_type == KSTAT_DATA_STRING &&
KSTAT_NAMED_STR_PTR(knp) != NULL) {
/* catch SIGSEGV (bug 6384130) */
sig_raised = 0;
(void) sigaction(SIGSEGV, NULL, &segv_act);
(void) signal(SIGSEGV, sig_handler);
(void) strncpy(end, KSTAT_NAMED_STR_PTR(knp),
KSTAT_NAMED_STR_BUFLEN(knp));
KSTAT_NAMED_STR_PTR(knp) = end;
end += KSTAT_NAMED_STR_BUFLEN(knp);
/* bug 6384130 */
(void) sigsetjmp(env, 0);
if (sig_raised) {
bzero(end, KSTAT_NAMED_STR_BUFLEN(knp));
KSTAT_NAMED_STR_PTR(knp) = end;
end += KSTAT_NAMED_STR_BUFLEN(knp);
}
(void) sigaction(SIGSEGV, &segv_act, NULL);
}
}
return (rval);
}
/*
* kstat_value() - retrieve value of a field in a kstat_named_t kstat.
*
* parameters
* kstat_t *ksp - kstat containing the field
* char *name - text string representing the field name
*
* returns
* void * - pointer to data retrieved
*/
void *
kstat_value(kstat_t *ksp, char *name)
{
kstat_named_t *knm;
if ((knm = kstat_data_lookup(ksp, name)) == NULL)
return (NULL);
switch (knm->data_type) {
case KSTAT_DATA_CHAR :
return (knm->value.c);
case KSTAT_DATA_INT32 :
return (&knm->value.i32);
case KSTAT_DATA_UINT32 :
return (&knm->value.ui32);
case KSTAT_DATA_INT64 :
return (&knm->value.i64);
case KSTAT_DATA_UINT64 :
return (&knm->value.ui64);
case KSTAT_DATA_STRING :
return (KSTAT_NAMED_STR_PTR(knm));
}
return (NULL);
}
/*
* kstat_free() - deallocated memory associated with a kstat
*
* paramters
* kstat_t ksp - kstat to be deallocated
*
* returns
* void
*/
void
kstat_free(kstat_t *ksp)
{
if (ksp != NULL) {
if (ksp->ks_data != NULL)
free(ksp->ks_data);
free(ksp);
}
}
uint32_t
kstat_delta(kstat_t *pksp, kstat_t *cksp, char *name)
{
uint32_t *pv, *cv;
pv = kstat_value(pksp, name);
cv = kstat_value(cksp, name);
return (u32_delta(*pv, *cv));
}
|