]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - cmd/zed/agents/fmd_serd.c
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2016, Intel Corporation.
38 #include "../zed_log.h"
41 #define FMD_STR_BUCKETS 211
45 #define serd_log_msg(fmt, ...) \
46 zed_log_msg(LOG_INFO, fmt, __VA_ARGS__)
48 #define serd_log_msg(fmt, ...)
57 * Compute the delta between events in nanoseconds. To account for very old
58 * events which are replayed, we must handle the case where time is negative.
59 * We convert the hrtime_t's to unsigned 64-bit integers and then handle the
60 * case where 'old' is greater than 'new' (i.e. high-res time has wrapped).
63 fmd_event_delta(hrtime_t t1
, hrtime_t t2
)
68 return (new >= old
? new - old
: (UINT64_MAX
- old
) + new + 1);
71 static fmd_serd_eng_t
*
72 fmd_serd_eng_alloc(const char *name
, uint64_t n
, hrtime_t t
)
76 sgp
= malloc(sizeof (fmd_serd_eng_t
));
77 bzero(sgp
, sizeof (fmd_serd_eng_t
));
79 sgp
->sg_name
= strdup(name
);
80 sgp
->sg_flags
= FMD_SERD_DIRTY
;
84 list_create(&sgp
->sg_list
, sizeof (fmd_serd_elem_t
),
85 offsetof(fmd_serd_elem_t
, se_list
));
91 fmd_serd_eng_free(fmd_serd_eng_t
*sgp
)
93 fmd_serd_eng_reset(sgp
);
95 list_destroy(&sgp
->sg_list
);
100 * sourced from fmd_string.c
103 fmd_strhash(const char *key
)
108 for (p
= key
; *p
!= '\0'; p
++) {
111 if ((g
= (h
& 0xf0000000)) != 0) {
121 fmd_serd_hash_create(fmd_serd_hash_t
*shp
)
123 shp
->sh_hashlen
= FMD_STR_BUCKETS
;
124 shp
->sh_hash
= calloc(shp
->sh_hashlen
, sizeof (void *));
129 fmd_serd_hash_destroy(fmd_serd_hash_t
*shp
)
131 fmd_serd_eng_t
*sgp
, *ngp
;
134 for (i
= 0; i
< shp
->sh_hashlen
; i
++) {
135 for (sgp
= shp
->sh_hash
[i
]; sgp
!= NULL
; sgp
= ngp
) {
137 fmd_serd_eng_free(sgp
);
142 bzero(shp
, sizeof (fmd_serd_hash_t
));
146 fmd_serd_hash_apply(fmd_serd_hash_t
*shp
, fmd_serd_eng_f
*func
, void *arg
)
151 for (i
= 0; i
< shp
->sh_hashlen
; i
++) {
152 for (sgp
= shp
->sh_hash
[i
]; sgp
!= NULL
; sgp
= sgp
->sg_next
)
158 fmd_serd_eng_insert(fmd_serd_hash_t
*shp
, const char *name
,
159 uint_t n
, hrtime_t t
)
161 uint_t h
= fmd_strhash(name
) % shp
->sh_hashlen
;
162 fmd_serd_eng_t
*sgp
= fmd_serd_eng_alloc(name
, n
, t
);
164 serd_log_msg(" SERD Engine: inserting %s N %d T %llu",
165 name
, (int)n
, (long long unsigned)t
);
167 sgp
->sg_next
= shp
->sh_hash
[h
];
168 shp
->sh_hash
[h
] = sgp
;
175 fmd_serd_eng_lookup(fmd_serd_hash_t
*shp
, const char *name
)
177 uint_t h
= fmd_strhash(name
) % shp
->sh_hashlen
;
180 for (sgp
= shp
->sh_hash
[h
]; sgp
!= NULL
; sgp
= sgp
->sg_next
) {
181 if (strcmp(name
, sgp
->sg_name
) == 0)
189 fmd_serd_eng_delete(fmd_serd_hash_t
*shp
, const char *name
)
191 uint_t h
= fmd_strhash(name
) % shp
->sh_hashlen
;
192 fmd_serd_eng_t
*sgp
, **pp
= &shp
->sh_hash
[h
];
194 serd_log_msg(" SERD Engine: deleting %s", name
);
196 for (sgp
= *pp
; sgp
!= NULL
; sgp
= sgp
->sg_next
) {
197 if (strcmp(sgp
->sg_name
, name
) != 0)
205 fmd_serd_eng_free(sgp
);
206 assert(shp
->sh_count
!= 0);
212 fmd_serd_eng_discard(fmd_serd_eng_t
*sgp
, fmd_serd_elem_t
*sep
)
214 list_remove(&sgp
->sg_list
, sep
);
217 serd_log_msg(" SERD Engine: discarding %s, %d remaining",
218 sgp
->sg_name
, (int)sgp
->sg_count
);
224 fmd_serd_eng_record(fmd_serd_eng_t
*sgp
, hrtime_t hrt
)
226 fmd_serd_elem_t
*sep
, *oep
;
229 * If the fired flag is already set, return false and discard the
230 * event. This means that the caller will only see the engine "fire"
231 * once until fmd_serd_eng_reset() is called. The fmd_serd_eng_fired()
232 * function can also be used in combination with fmd_serd_eng_record().
234 if (sgp
->sg_flags
& FMD_SERD_FIRED
) {
235 serd_log_msg(" SERD Engine: record %s already fired!",
237 return (FMD_B_FALSE
);
240 while (sgp
->sg_count
>= sgp
->sg_n
)
241 fmd_serd_eng_discard(sgp
, list_tail(&sgp
->sg_list
));
243 sep
= malloc(sizeof (fmd_serd_elem_t
));
246 list_insert_head(&sgp
->sg_list
, sep
);
249 serd_log_msg(" SERD Engine: recording %s of %d (%llu)",
250 sgp
->sg_name
, (int)sgp
->sg_count
, (long long unsigned)hrt
);
253 * Pick up the oldest element pointer for comparison to 'sep'. We must
254 * do this after adding 'sep' because 'oep' and 'sep' can be the same.
256 oep
= list_tail(&sgp
->sg_list
);
258 if (sgp
->sg_count
>= sgp
->sg_n
&&
259 fmd_event_delta(oep
->se_hrt
, sep
->se_hrt
) <= sgp
->sg_t
) {
260 sgp
->sg_flags
|= FMD_SERD_FIRED
| FMD_SERD_DIRTY
;
261 serd_log_msg(" SERD Engine: fired %s", sgp
->sg_name
);
265 sgp
->sg_flags
|= FMD_SERD_DIRTY
;
266 return (FMD_B_FALSE
);
270 fmd_serd_eng_fired(fmd_serd_eng_t
*sgp
)
272 return (sgp
->sg_flags
& FMD_SERD_FIRED
);
276 fmd_serd_eng_empty(fmd_serd_eng_t
*sgp
)
278 return (sgp
->sg_count
== 0);
282 fmd_serd_eng_reset(fmd_serd_eng_t
*sgp
)
284 serd_log_msg(" SERD Engine: reseting %s", sgp
->sg_name
);
286 while (sgp
->sg_count
!= 0)
287 fmd_serd_eng_discard(sgp
, list_head(&sgp
->sg_list
));
289 sgp
->sg_flags
&= ~FMD_SERD_FIRED
;
290 sgp
->sg_flags
|= FMD_SERD_DIRTY
;
294 fmd_serd_eng_gc(fmd_serd_eng_t
*sgp
)
296 fmd_serd_elem_t
*sep
, *nep
;
299 if (sgp
->sg_count
== 0 || (sgp
->sg_flags
& FMD_SERD_FIRED
))
300 return; /* no garbage collection needed if empty or fired */
302 sep
= list_head(&sgp
->sg_list
);
306 hrt
= sep
->se_hrt
- sgp
->sg_t
;
308 for (sep
= list_head(&sgp
->sg_list
); sep
!= NULL
; sep
= nep
) {
309 if (sep
->se_hrt
>= hrt
)
310 break; /* sep and subsequent events are all within T */
312 nep
= list_next(&sgp
->sg_list
, sep
);
313 fmd_serd_eng_discard(sgp
, sep
);
314 sgp
->sg_flags
|= FMD_SERD_DIRTY
;