]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/app/iscsi_top/iscsi_top.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / spdk / app / iscsi_top / iscsi_top.cpp
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/types.h>
35 #include <sys/uio.h>
36
37 #include <fcntl.h>
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdint.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <signal.h>
46 #include <termios.h>
47 #include <algorithm>
48 #include <map>
49 #include <vector>
50
51 extern "C" {
52 #include "spdk/trace.h"
53 #include "iscsi/conn.h"
54 }
55
56 static char *exe_name;
57 static int g_shm_id = 0;
58
59 static void usage(void)
60 {
61 fprintf(stderr, "usage:\n");
62 fprintf(stderr, " %s <option>\n", exe_name);
63 fprintf(stderr, " option = '-i' to specify the shared memory ID,"
64 " (required)\n");
65 }
66
67 static bool
68 conns_compare(struct spdk_iscsi_conn *first, struct spdk_iscsi_conn *second)
69 {
70 if (first->lcore < second->lcore)
71 return true;
72
73 if (first->lcore > second->lcore)
74 return false;
75
76 if (first->id < second->id)
77 return true;
78
79 return false;
80 }
81
82 static void
83 print_connections(void)
84 {
85 std::vector<struct spdk_iscsi_conn *> v;
86 std::vector<struct spdk_iscsi_conn *>::iterator iter;
87 size_t conns_size;
88 struct spdk_iscsi_conn *conns, *conn;
89 void *conns_ptr;
90 int fd, i;
91 char shm_name[64];
92
93 snprintf(shm_name, sizeof(shm_name), "spdk_iscsi_conns.%d", g_shm_id);
94 fd = shm_open(shm_name, O_RDONLY, 0600);
95 if (fd < 0) {
96 fprintf(stderr, "Cannot open shared memory: %s\n", shm_name);
97 usage();
98 exit(1);
99 }
100
101 conns_size = sizeof(*conns) * MAX_ISCSI_CONNECTIONS;
102
103 conns_ptr = mmap(NULL, conns_size, PROT_READ, MAP_SHARED, fd, 0);
104 if (conns_ptr == NULL) {
105 fprintf(stderr, "Cannot mmap shared memory\n");
106 exit(1);
107 }
108
109 conns = (struct spdk_iscsi_conn *)conns_ptr;
110
111 for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
112 if (!conns[i].is_valid) {
113 continue;
114 }
115 v.push_back(&conns[i]);
116 }
117
118 stable_sort(v.begin(), v.end(), conns_compare);
119 for (iter = v.begin(); iter != v.end(); iter++) {
120 conn = *iter;
121 printf("lcore %2d conn %3d T:%-8s I:%s (%s)\n",
122 conn->lcore, conn->id,
123 conn->target_short_name, conn->initiator_name,
124 conn->initiator_addr);
125 }
126
127 printf("\n");
128 munmap(conns, conns_size);
129 close(fd);
130 }
131
132 int main(int argc, char **argv)
133 {
134 void *history_ptr;
135 struct spdk_trace_histories *histories;
136 struct spdk_trace_history *history;
137
138 uint64_t tasks_done, last_tasks_done[SPDK_TRACE_MAX_LCORE];
139 int delay, old_delay, history_fd, i, quit, rc;
140 int tasks_done_delta, tasks_done_per_sec;
141 int total_tasks_done_per_sec;
142 struct timeval timeout;
143 fd_set fds;
144 char ch;
145 struct termios oldt, newt;
146 char spdk_trace_shm_name[64];
147 int op;
148
149 exe_name = argv[0];
150 while ((op = getopt(argc, argv, "i:")) != -1) {
151 switch (op) {
152 case 'i':
153 g_shm_id = atoi(optarg);
154 break;
155 default:
156 usage();
157 exit(1);
158 }
159 }
160
161 snprintf(spdk_trace_shm_name, sizeof(spdk_trace_shm_name), "/iscsi_trace.%d", g_shm_id);
162 history_fd = shm_open(spdk_trace_shm_name, O_RDONLY, 0600);
163 if (history_fd < 0) {
164 fprintf(stderr, "Unable to open history shm %s\n", spdk_trace_shm_name);
165 usage();
166 exit(1);
167 }
168
169 history_ptr = mmap(NULL, sizeof(*histories), PROT_READ, MAP_SHARED, history_fd, 0);
170 if (history_ptr == MAP_FAILED) {
171 fprintf(stderr, "Unable to mmap history shm\n");
172 exit(1);
173 }
174
175 histories = (struct spdk_trace_histories *)history_ptr;
176
177 memset(last_tasks_done, 0, sizeof(last_tasks_done));
178
179 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
180 history = &histories->per_lcore_history[i];
181 last_tasks_done[i] = history->tpoint_count[TRACE_ISCSI_TASK_DONE];
182 }
183
184 delay = 1;
185 quit = 0;
186
187 tcgetattr(0, &oldt);
188 newt = oldt;
189 newt.c_lflag &= ~(ICANON);
190 tcsetattr(0, TCSANOW, &newt);
191
192 while (1) {
193
194 FD_ZERO(&fds);
195 FD_SET(0, &fds);
196 timeout.tv_sec = delay;
197 timeout.tv_usec = 0;
198 rc = select(2, &fds, NULL, NULL, &timeout);
199
200 if (rc > 0) {
201 if (read(0, &ch, 1) != 1) {
202 fprintf(stderr, "Read error on stdin\n");
203 goto cleanup;
204 }
205
206 printf("\b");
207 switch (ch) {
208 case 'd':
209 printf("Enter num seconds to delay (1-10): ");
210 old_delay = delay;
211 rc = scanf("%d", &delay);
212 if (rc != 1) {
213 fprintf(stderr, "Illegal delay value\n");
214 delay = old_delay;
215 } else if (delay < 1 || delay > 10) {
216 delay = 1;
217 }
218 break;
219 case 'q':
220 quit = 1;
221 break;
222 default:
223 fprintf(stderr, "'%c' not recognized\n", ch);
224 break;
225 }
226
227 if (quit == 1) {
228 break;
229 }
230 }
231
232 printf("\e[1;1H\e[2J");
233 print_connections();
234 printf("lcore tasks\n");
235 printf("=============\n");
236 total_tasks_done_per_sec = 0;
237 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
238 history = &histories->per_lcore_history[i];
239 tasks_done = history->tpoint_count[TRACE_ISCSI_TASK_DONE];
240 tasks_done_delta = tasks_done - last_tasks_done[i];
241 if (tasks_done_delta == 0) {
242 continue;
243 }
244 last_tasks_done[i] = tasks_done;
245 tasks_done_per_sec = tasks_done_delta / delay;
246 printf("%5d %7d\n", history->lcore, tasks_done_per_sec);
247 total_tasks_done_per_sec += tasks_done_per_sec;
248 }
249 printf("Total %7d\n", total_tasks_done_per_sec);
250 }
251
252 cleanup:
253 tcsetattr(0, TCSANOW, &oldt);
254
255 munmap(history_ptr, sizeof(*histories));
256 close(history_fd);
257
258 return (0);
259 }