2 * Copyright (c) 2004-2008 Voltaire Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #endif /* HAVE_CONFIG_H */
46 #include <infiniband/common.h>
47 #include <infiniband/umad.h>
48 #include <infiniband/mad.h>
50 #include "ibdiag_common.h"
53 #define DEBUG if (verbose) IBWARN
55 static int dest_type = IB_DEST_LID;
60 enum ib_sysstat_attr_t {
62 IB_HOSTINFO_ATTR = 0x11,
63 IB_CPUINFO_ATTR = 0x12,
66 typedef struct cpu_info {
71 static cpu_info cpus[MAX_CPUS];
74 char *argv0 = "ibsysstat";
77 mk_reply(int attr, void *data, int sz)
84 break; /* nothing to do here, just reply */
85 case IB_HOSTINFO_ATTR:
86 if (gethostname(s, sz) < 0)
87 snprintf(s, sz, "?hostname?");
89 if ((n = strlen(s)) >= sz)
94 if (getdomainname(s, sz) < 0)
95 snprintf(s, sz, "?domainname?");
97 s[-1] = 0; /* no domain */
100 for (i = 0; i < host_ncpu && sz > 0; i++) {
101 n = snprintf(s, sz, "cpu %d: model %s MHZ %s\n",
102 i, cpus[i].model, cpus[i].mhz);
104 IBWARN("cpuinfo truncated");
112 DEBUG("unknown attr %d", attr);
123 DEBUG("starting to serve...");
125 while ((umad = mad_receive(0, -1))) {
127 mad = umad_get_mad(umad);
129 attr = mad_get_field(mad, 0, IB_MAD_ATTRID_F);
130 mod = mad_get_field(mad, 0, IB_MAD_ATTRMOD_F);
132 DEBUG("got packet: attr 0x%x mod 0x%x", attr, mod);
134 mk_reply(attr, (char *)mad + IB_VENDOR_RANGE2_DATA_OFFS, IB_VENDOR_RANGE2_DATA_SIZE);
136 if (mad_respond(umad, 0, 0) < 0)
137 DEBUG("respond failed");
147 match_attr(char *str)
149 if (!strcmp(str, "ping"))
151 if (!strcmp(str, "host"))
152 return IB_HOSTINFO_ATTR;
153 if (!strcmp(str, "cpu"))
154 return IB_CPUINFO_ATTR;
159 ibsystat(ib_portid_t *portid, int attr)
161 char data[IB_VENDOR_RANGE2_DATA_SIZE] = {0};
162 ib_vendor_call_t call;
164 DEBUG("Sysstat ping..");
166 call.method = IB_MAD_METHOD_GET;
167 call.mgmt_class = IB_VENDOR_OPENIB_SYSSTAT_CLASS;
170 call.oui = IB_OPENIB_OUI;
172 memset(&call.rmpp, 0, sizeof call.rmpp);
174 if (!ib_vendor_call(data, portid, &call))
175 return "vendor call failed";
177 DEBUG("Got sysstat pong..");
178 if (attr != IB_PING_ATTR)
181 printf("sysstat ping succeeded\n");
188 char line[1024] = {0}, *s, *e;
192 if (!(f = fopen("/proc/cpuinfo", "r"))) {
193 IBWARN("couldn't open /proc/cpuinfo");
197 while (fgets(line, sizeof(line) - 1, f)) {
198 if (!strncmp(line, "processor\t", 10)) {
205 if (!ncpu || !(s = strchr(line, ':')))
208 if ((e = strchr(s, '\n')))
210 if (!strncmp(line, "model name\t", 11))
211 cpus[ncpu-1].model = strdup(s+1);
212 else if (!strncmp(line, "cpu MHz\t", 8))
213 cpus[ncpu-1].mhz = strdup(s+1);
218 DEBUG("ncpu %d", ncpu);
228 if (!(basename = strrchr(argv0, '/')))
233 fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port "
234 "-t(imeout) timeout_ms -o oui -S(erver)] <dest lid|guid> [<op>]\n",
240 main(int argc, char **argv)
242 int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
243 int sysstat_class = IB_VENDOR_OPENIB_SYSSTAT_CLASS;
244 ib_portid_t portid = {0};
245 ib_portid_t *sm_id = 0, sm_portid = {0};
246 int timeout = 0, udebug = 0, server = 0;
247 int oui = IB_OPENIB_OUI, attr = IB_PING_ATTR;
253 static char const str_opts[] = "C:P:t:s:o:devGSVhu";
254 static const struct option long_opts[] = {
257 { "debug", 0, 0, 'd'},
258 { "err_show", 0, 0, 'e'},
259 { "verbose", 0, 0, 'v'},
260 { "Guid", 0, 0, 'G'},
261 { "timeout", 1, 0, 't'},
264 { "Server", 0, 0, 'S'},
265 { "Version", 0, 0, 'V'},
266 { "help", 0, 0, 'h'},
267 { "usage", 0, 0, 'u'},
274 int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
282 ca_port = strtoul(optarg, 0, 0);
286 madrpc_show_errors(1);
291 madrpc_show_errors(1);
294 dest_type = IB_DEST_GUID;
297 oui = strtoul(optarg, 0, 0);
300 if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
301 IBERROR("can't resolve SM destination port %s", optarg);
308 timeout = strtoul(optarg, 0, 0);
309 madrpc_set_timeout(timeout);
315 fprintf(stderr, "%s %s\n", argv0, get_build_version() );
325 if (!argc && !server)
328 if (argc > 1 && (attr = match_attr(argv[1])) < 0)
331 madrpc_init(ca, ca_port, mgmt_classes, 3);
334 if (mad_register_server(sysstat_class, 0, 0, oui) < 0)
335 IBERROR("can't serve class %d", sysstat_class);
337 host_ncpu = build_cpuinfo();
339 if ((err = ibsystat_serv()))
340 IBERROR("ibssystat to %s: %s", portid2str(&portid), err);
344 if (mad_register_client(sysstat_class, 0) < 0)
345 IBERROR("can't register to sysstat class %d", sysstat_class);
347 if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
348 IBERROR("can't resolve destination port %s", argv[0]);
350 if ((err = ibsystat(&portid, attr)))
351 IBERROR("ibsystat to %s: %s", portid2str(&portid), err);