1 /*
2  * Copyright (c) 2007 The Regents of the University of California.
3  * Copyright (c) 2007-2009 Voltaire, Inc. All rights reserved.
4  * Copyright (c) 2009,2010 HNR Consulting. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  */
35 
36 #ifndef _OSM_PERFMGR_H_
37 #define _OSM_PERFMGR_H_
38 
39 #if HAVE_CONFIG_H
40 #  include <config.h>
41 #endif				/* HAVE_CONFIG_H */
42 
43 #ifdef ENABLE_OSM_PERF_MGR
44 
45 #include <iba/ib_types.h>
46 #include <complib/cl_passivelock.h>
47 #include <complib/cl_event.h>
48 #include <complib/cl_timer.h>
49 #include <opensm/osm_subnet.h>
50 #include <opensm/osm_log.h>
51 #include <opensm/osm_perfmgr_db.h>
52 #include <opensm/osm_sm.h>
53 #include <opensm/osm_base.h>
54 #include <opensm/osm_event_plugin.h>
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif				/* __cplusplus */
59 
60 /****h* OpenSM/PerfMgr
61 * NAME
62 *	PerfMgr
63 *
64 * DESCRIPTION
65 *       Performance manager thread which takes care of polling the fabric for
66 *       Port counters values.
67 *
68 *	The PerfMgr object is thread safe.
69 *
70 * AUTHOR
71 *	Ira Weiny, LLNL
72 *
73 *********/
74 
75 #define OSM_PERFMGR_DEFAULT_SWEEP_TIME_S 180
76 #define OSM_PERFMGR_DEFAULT_DUMP_FILE "opensm_port_counters.log"
77 #define OSM_PERFMGR_DEFAULT_MAX_OUTSTANDING_QUERIES 500
78 #define OSM_PERFMGR_DEFAULT_XMIT_WAIT_THRESHOLD 0x0000FFFF
79 
80 /****s* OpenSM: PerfMgr/osm_perfmgr_state_t */
81 typedef enum {
82 	PERFMGR_STATE_DISABLE,
83 	PERFMGR_STATE_ENABLED,
84 	PERFMGR_STATE_NO_DB
85 } osm_perfmgr_state_t;
86 
87 /****s* OpenSM: PerfMgr/osm_perfmgr_sweep_state_t */
88 typedef enum {
89 	PERFMGR_SWEEP_SLEEP,
90 	PERFMGR_SWEEP_ACTIVE,
91 	PERFMGR_SWEEP_SUSPENDED,
92 	PERFMGR_SWEEP_POST_PROCESSING
93 } osm_perfmgr_sweep_state_t;
94 
95 typedef struct monitored_port {
96 	uint16_t pkey_ix;
97 	ib_net16_t orig_lid;
98 	boolean_t redirection;
99 	boolean_t valid;
100 	/* Redirection fields from ClassPortInfo */
101 	ib_gid_t gid;
102 	ib_net16_t lid;
103 	ib_net16_t pkey;
104 	ib_net32_t qp;
105 	/* ClassPortInfo fields */
106 	boolean_t cpi_valid;
107 	ib_net16_t cap_mask;
108 	/* Remote end connected to */
109 	boolean_t remote_valid;
110 	uint64_t remote_guid;
111 	char *remote_name;
112 	uint8_t remote_port;
113 } monitored_port_t;
114 
115 /* Node to store information about nodes being monitored */
116 typedef struct monitored_node {
117 	cl_map_item_t map_item;
118 	struct monitored_node *next;
119 	uint64_t guid;
120 	uint8_t node_type;
121 	boolean_t esp0;
122 	char *name;
123 	uint32_t num_ports;
124 	monitored_port_t port[1];
125 } monitored_node_t;
126 
127 struct osm_opensm;
128 
129 /****s* OpenSM: PerfMgr/osm_perfmgr_t
130 *  This object should be treated as opaque and should
131 *  be manipulated only through the provided functions.
132 */
133 typedef struct osm_perfmgr {
134 	cl_timer_t sweep_timer;
135 	struct osm_opensm *osm;
136 	osm_subn_t *subn;
137 	osm_sm_t *sm;
138 	osm_log_t *log;
139 	osm_mad_pool_t *mad_pool;
140 	atomic32_t trans_id;
141 	osm_vendor_t *vendor;
142 	osm_bind_handle_t bind_handle;
143 	cl_disp_reg_handle_t pc_disp_h;
144 	osm_perfmgr_state_t state;
145 	osm_perfmgr_sweep_state_t sweep_state;
146 	cl_spinlock_t lock;
147 	uint16_t sweep_time_s;
148 	perfmgr_db_t *db;
149 	atomic32_t outstanding_queries;	/* this along with sig_query */
150 	cl_event_t sig_query;	/* will throttle our queries */
151 	uint32_t max_outstanding_queries;
152 	boolean_t ignore_cas;
153 	cl_qmap_t monitored_map;	/* map the nodes being tracked */
154 	monitored_node_t *remove_list;
155 	ib_net64_t port_guid;
156 	int16_t local_port;
157 	int rm_nodes;
158 	boolean_t query_cpi;
159 	boolean_t xmit_wait_log;
160 	uint32_t xmit_wait_threshold;
161 } osm_perfmgr_t;
162 /*
163 * FIELDS
164 *	subn
165 *	      Subnet object for this subnet.
166 *
167 *	log
168 *	      Pointer to the log object.
169 *
170 *	mad_pool
171 *	      Pointer to the MAD pool.
172 *
173 *	mad_ctrl
174 *	      Mad Controller
175 *********/
176 
177 /****f* OpenSM: Creation Functions */
178 void osm_perfmgr_shutdown(osm_perfmgr_t * p_perfmgr);
179 void osm_perfmgr_destroy(osm_perfmgr_t * p_perfmgr);
180 
181 /****f* OpenSM: Inline accessor functions */
182 inline static void osm_perfmgr_set_state(osm_perfmgr_t * p_perfmgr,
183 					 osm_perfmgr_state_t state)
184 {
185 	p_perfmgr->state = state;
186 	if (state == PERFMGR_STATE_ENABLED) {
187 		cl_timer_start(&p_perfmgr->sweep_timer, p_perfmgr->sweep_time_s * 1000);
188 	} else {
189 		cl_timer_stop(&p_perfmgr->sweep_timer);
190 	}
191 }
192 
193 inline static osm_perfmgr_state_t osm_perfmgr_get_state(osm_perfmgr_t * perfmgr)
194 {
195 	return perfmgr->state;
196 }
197 
198 inline static void osm_perfmgr_set_rm_nodes(osm_perfmgr_t *perfmgr,
199 					    int rm_nodes)
200 {
201 	perfmgr->rm_nodes = rm_nodes;
202 }
203 
204 inline static int osm_perfmgr_get_rm_nodes(osm_perfmgr_t *perfmgr)
205 {
206 	return perfmgr->rm_nodes;
207 }
208 
209 inline static void osm_perfmgr_set_query_cpi(osm_perfmgr_t *perfmgr,
210 					    int query_cpi)
211 {
212 	perfmgr->query_cpi = query_cpi;
213 }
214 
215 inline static int osm_perfmgr_get_query_cpi(osm_perfmgr_t *perfmgr)
216 {
217 	return perfmgr->query_cpi;
218 }
219 
220 inline static const char *osm_perfmgr_get_state_str(osm_perfmgr_t * p_perfmgr)
221 {
222 	switch (p_perfmgr->state) {
223 	case PERFMGR_STATE_DISABLE:
224 		return "Disabled";
225 		break;
226 	case PERFMGR_STATE_ENABLED:
227 		return "Enabled";
228 		break;
229 	case PERFMGR_STATE_NO_DB:
230 		return "No Database";
231 		break;
232 	}
233 	return "UNKNOWN";
234 }
235 
236 inline static const char *osm_perfmgr_get_sweep_state_str(osm_perfmgr_t * perfmgr)
237 {
238 	switch (perfmgr->sweep_state) {
239 	case PERFMGR_SWEEP_SLEEP:
240 		return "Sleeping";
241 		break;
242 	case PERFMGR_SWEEP_ACTIVE:
243 		return "Active";
244 		break;
245 	case PERFMGR_SWEEP_SUSPENDED:
246 		return "Suspended";
247 		break;
248 	case PERFMGR_SWEEP_POST_PROCESSING:
249 		return "PostProcessing";
250 		break;
251 	}
252 	return "UNKNOWN";
253 }
254 
255 inline static void osm_perfmgr_set_sweep_time_s(osm_perfmgr_t * p_perfmgr,
256 						uint16_t time_s)
257 {
258 	p_perfmgr->sweep_time_s = time_s;
259 	osm_sm_signal(p_perfmgr->sm, OSM_SIGNAL_PERFMGR_SWEEP);
260 }
261 
262 inline static uint16_t osm_perfmgr_get_sweep_time_s(osm_perfmgr_t * p_perfmgr)
263 {
264 	return p_perfmgr->sweep_time_s;
265 }
266 
267 inline static unsigned osm_perfmgr_delete_inactive(osm_perfmgr_t * pm)
268 {
269 	unsigned rc;
270 	perfmgr_db_delete_inactive(pm->db, &rc);
271 	return (rc);
272 }
273 
274 void osm_perfmgr_clear_counters(osm_perfmgr_t * p_perfmgr);
275 void osm_perfmgr_dump_counters(osm_perfmgr_t * p_perfmgr,
276 			       perfmgr_db_dump_t dump_type);
277 void osm_perfmgr_print_counters(osm_perfmgr_t *pm, char *nodename, FILE *fp,
278 				char *port, int err_only);
279 void osm_perfmgr_update_nodename(osm_perfmgr_t *pm, uint64_t node_guid,
280 				char *nodename);
281 
282 ib_api_status_t osm_perfmgr_bind(osm_perfmgr_t * p_perfmgr,
283 				 ib_net64_t port_guid);
284 
285 void osm_perfmgr_process(osm_perfmgr_t * pm);
286 
287 /****f* OpenSM: PerfMgr/osm_perfmgr_init */
288 ib_api_status_t osm_perfmgr_init(osm_perfmgr_t * perfmgr,
289 				 struct osm_opensm *osm,
290 				 const osm_subn_opt_t * p_opt);
291 /*
292 * PARAMETERS
293 *	perfmgr
294 *		[in] Pointer to an osm_perfmgr_t object to initialize.
295 *
296 *	osm
297 *		[in] Pointer to the OpenSM object.
298 *
299 *	p_opt
300 *		[in] Pointer to the subnet options structure.
301 *
302 * RETURN VALUES
303 *	IB_SUCCESS if the PerfMgr object was initialized successfully.
304 *********/
305 
306 #ifdef __cplusplus
307 }
308 #endif				/* __cplusplus */
309 
310 #endif				/* ENABLE_OSM_PERF_MGR */
311 
312 #endif				/* _OSM_PERFMGR_H_ */
313