1 /*	$OpenBSD: rf_acctrace.c,v 1.4 2002/12/16 07:01:02 tdeval Exp $	*/
2 /*	$NetBSD: rf_acctrace.c,v 1.4 1999/08/13 03:41:52 oster Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Mark Holland
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 /*****************************************************************************
32  *
33  * acctrace.c -- Code to support collecting information about each access.
34  *
35  *****************************************************************************/
36 
37 
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 
41 #include "rf_threadstuff.h"
42 #include "rf_types.h"
43 #include "rf_debugMem.h"
44 #include "rf_acctrace.h"
45 #include "rf_general.h"
46 #include "rf_raid.h"
47 #include "rf_etimer.h"
48 #include "rf_hist.h"
49 #include "rf_shutdown.h"
50 
51 static long numTracesSoFar;
52 static int accessTraceBufCount = 0;
53 static RF_AccTraceEntry_t *access_tracebuf;
54 static long traceCount;
55 
56 int	rf_stopCollectingTraces;
57 RF_DECLARE_MUTEX(rf_tracing_mutex);
58 int	rf_trace_fd;
59 
60 void rf_ShutdownAccessTrace(void *);
61 
62 void
rf_ShutdownAccessTrace(void * ignored)63 rf_ShutdownAccessTrace(void *ignored)
64 {
65 	if (rf_accessTraceBufSize) {
66 		if (accessTraceBufCount)
67 			rf_FlushAccessTraceBuf();
68 		RF_Free(access_tracebuf, rf_accessTraceBufSize *
69 		    sizeof(RF_AccTraceEntry_t));
70 	}
71 	rf_mutex_destroy(&rf_tracing_mutex);
72 }
73 
74 int
rf_ConfigureAccessTrace(RF_ShutdownList_t ** listp)75 rf_ConfigureAccessTrace(RF_ShutdownList_t **listp)
76 {
77 	int rc;
78 
79 	numTracesSoFar = accessTraceBufCount = rf_stopCollectingTraces = 0;
80 	if (rf_accessTraceBufSize) {
81 		RF_Malloc(access_tracebuf, rf_accessTraceBufSize *
82 		    sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
83 		accessTraceBufCount = 0;
84 	}
85 	traceCount = 0;
86 	numTracesSoFar = 0;
87 	rc = rf_mutex_init(&rf_tracing_mutex);
88 	if (rc) {
89 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d.\n",
90 		    __FILE__, __LINE__, rc);
91 	}
92 	rc = rf_ShutdownCreate(listp, rf_ShutdownAccessTrace, NULL);
93 	if (rc) {
94 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d"
95 		    " rc=%d.\n", __FILE__, __LINE__, rc);
96 		if (rf_accessTraceBufSize) {
97 			RF_Free(access_tracebuf, rf_accessTraceBufSize *
98 			    sizeof(RF_AccTraceEntry_t));
99 			rf_mutex_destroy(&rf_tracing_mutex);
100 		}
101 	}
102 	return (rc);
103 }
104 
105 /*
106  * Install a trace record. Cause a flush to disk or to the trace collector
107  * daemon if the trace buffer is at least 1/2 full.
108  */
109 void
rf_LogTraceRec(RF_Raid_t * raid,RF_AccTraceEntry_t * rec)110 rf_LogTraceRec(RF_Raid_t *raid, RF_AccTraceEntry_t *rec)
111 {
112 	RF_AccTotals_t *acc = &raid->acc_totals;
113 #if 0
114 	RF_Etimer_t timer;
115 	int	i, n;
116 #endif
117 
118 	if (rf_stopCollectingTraces || ((rf_maxNumTraces >= 0) &&
119 	    (numTracesSoFar >= rf_maxNumTraces)))
120 		return;
121 
122 	/* Update AccTotals for this device. */
123 	if (!raid->keep_acc_totals)
124 		return;
125 
126 	acc->num_log_ents++;
127 	if (rec->reconacc) {
128 		acc->recon_start_to_fetch_us +=
129 		    rec->specific.recon.recon_start_to_fetch_us;
130 		acc->recon_fetch_to_return_us +=
131 		    rec->specific.recon.recon_fetch_to_return_us;
132 		acc->recon_return_to_submit_us +=
133 		    rec->specific.recon.recon_return_to_submit_us;
134 		acc->recon_num_phys_ios += rec->num_phys_ios;
135 		acc->recon_phys_io_us += rec->phys_io_us;
136 		acc->recon_diskwait_us += rec->diskwait_us;
137 		acc->recon_reccount++;
138 	} else {
139 		RF_HIST_ADD(acc->tot_hist, rec->total_us);
140 		RF_HIST_ADD(acc->dw_hist, rec->diskwait_us);
141 		/*
142 		 * Count of physical IOs that are too big. (often due to
143 		 * thermal recalibration)
144 		 *
145 		 * If bigvals > 0, you should probably ignore this data set.
146 		 */
147 		if (rec->diskwait_us > 100000)
148 			acc->bigvals++;
149 
150 		acc->total_us += rec->total_us;
151 		acc->suspend_ovhd_us += rec->specific.user.suspend_ovhd_us;
152 		acc->map_us += rec->specific.user.map_us;
153 		acc->lock_us += rec->specific.user.lock_us;
154 		acc->dag_create_us += rec->specific.user.dag_create_us;
155 		acc->dag_retry_us += rec->specific.user.dag_retry_us;
156 		acc->exec_us += rec->specific.user.exec_us;
157 		acc->cleanup_us += rec->specific.user.cleanup_us;
158 		acc->exec_engine_us += rec->specific.user.exec_engine_us;
159 		acc->xor_us += rec->xor_us;
160 		acc->q_us += rec->q_us;
161 		acc->plog_us += rec->plog_us;
162 		acc->diskqueue_us += rec->diskqueue_us;
163 		acc->diskwait_us += rec->diskwait_us;
164 		acc->num_phys_ios += rec->num_phys_ios;
165 		acc->phys_io_us = rec->phys_io_us;
166 		acc->user_reccount++;
167 	}
168 }
169 
170 
171 /*
172  * Assumes the tracing mutex is locked at entry. In order to allow this to
173  * be called from interrupt context, we don't do any copyouts here, but rather
174  * just wake the trace buffer collector thread.
175  */
176 void
rf_FlushAccessTraceBuf(void)177 rf_FlushAccessTraceBuf(void)
178 {
179 	accessTraceBufCount = 0;
180 }
181